mirror of
https://github.com/python/cpython.git
synced 2025-08-02 16:13:13 +00:00
* Python/bltinmodule.c: restructured coerce(), divmod(), pow() to
use new instancebinop interface
This commit is contained in:
parent
a10f512dba
commit
6a00cd8b89
1 changed files with 86 additions and 58 deletions
|
@ -256,17 +256,13 @@ builtin_cmp(self, args)
|
||||||
}
|
}
|
||||||
|
|
||||||
static object *
|
static object *
|
||||||
builtin_coerce(self, args)
|
do_coerce(v, w)
|
||||||
object *self;
|
|
||||||
object *args;
|
|
||||||
{
|
|
||||||
object *v, *w;
|
object *v, *w;
|
||||||
|
{
|
||||||
object *res;
|
object *res;
|
||||||
|
|
||||||
if (!newgetargs(args, "OO:coerce", &v, &w))
|
|
||||||
return NULL;
|
|
||||||
if (is_instanceobject(v) || is_instanceobject(w))
|
if (is_instanceobject(v) || is_instanceobject(w))
|
||||||
return instancebinop(v, w, "__coerce__", "__rcoerce__");
|
return instancebinop(v, w, "__coerce__", "__rcoerce__",
|
||||||
|
do_coerce);
|
||||||
if (coerce(&v, &w) < 0)
|
if (coerce(&v, &w) < 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
res = mkvalue("(OO)", v, w);
|
res = mkvalue("(OO)", v, w);
|
||||||
|
@ -275,6 +271,18 @@ builtin_coerce(self, args)
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static object *
|
||||||
|
builtin_coerce(self, args)
|
||||||
|
object *self;
|
||||||
|
object *args;
|
||||||
|
{
|
||||||
|
object *v, *w;
|
||||||
|
|
||||||
|
if (!newgetargs(args, "OO:coerce", &v, &w))
|
||||||
|
return NULL;
|
||||||
|
return do_coerce(v, w);
|
||||||
|
}
|
||||||
|
|
||||||
static object *
|
static object *
|
||||||
builtin_compile(self, args)
|
builtin_compile(self, args)
|
||||||
object *self;
|
object *self;
|
||||||
|
@ -336,16 +344,14 @@ builtin_dir(self, args)
|
||||||
}
|
}
|
||||||
|
|
||||||
static object *
|
static object *
|
||||||
builtin_divmod(self, args)
|
do_divmod(v, w)
|
||||||
object *self;
|
object *v, *w;
|
||||||
object *args;
|
|
||||||
{
|
{
|
||||||
object *v, *w, *x;
|
object *res;
|
||||||
|
|
||||||
if (!newgetargs(args, "OO:divmod", &v, &w))
|
|
||||||
return NULL;
|
|
||||||
if (is_instanceobject(v) || is_instanceobject(w))
|
if (is_instanceobject(v) || is_instanceobject(w))
|
||||||
return instancebinop(v, w, "__divmod__", "__rdivmod__");
|
return instancebinop(v, w, "__divmod__", "__rdivmod__",
|
||||||
|
do_divmod);
|
||||||
if (v->ob_type->tp_as_number == NULL ||
|
if (v->ob_type->tp_as_number == NULL ||
|
||||||
w->ob_type->tp_as_number == NULL) {
|
w->ob_type->tp_as_number == NULL) {
|
||||||
err_setstr(TypeError,
|
err_setstr(TypeError,
|
||||||
|
@ -354,10 +360,22 @@ builtin_divmod(self, args)
|
||||||
}
|
}
|
||||||
if (coerce(&v, &w) != 0)
|
if (coerce(&v, &w) != 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
x = (*v->ob_type->tp_as_number->nb_divmod)(v, w);
|
res = (*v->ob_type->tp_as_number->nb_divmod)(v, w);
|
||||||
DECREF(v);
|
DECREF(v);
|
||||||
DECREF(w);
|
DECREF(w);
|
||||||
return x;
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
static object *
|
||||||
|
builtin_divmod(self, args)
|
||||||
|
object *self;
|
||||||
|
object *args;
|
||||||
|
{
|
||||||
|
object *v, *w;
|
||||||
|
|
||||||
|
if (!newgetargs(args, "OO:divmod", &v, &w))
|
||||||
|
return NULL;
|
||||||
|
return do_divmod(v, w);
|
||||||
}
|
}
|
||||||
|
|
||||||
static object *
|
static object *
|
||||||
|
@ -897,57 +915,67 @@ builtin_ord(self, args)
|
||||||
}
|
}
|
||||||
|
|
||||||
static object *
|
static object *
|
||||||
builtin_pow(self, args)
|
do_pow(v, w)
|
||||||
object *self;
|
object *v, *w;
|
||||||
object *args;
|
|
||||||
{
|
{
|
||||||
object *v, *w, *z = None, *x;
|
object *res;
|
||||||
|
if (is_instanceobject(v) || is_instanceobject(w))
|
||||||
if (!newgetargs(args, "OO|O:pow", &v, &w, &z))
|
return instancebinop(v, w, "__pow__", "__rpow__", do_pow);
|
||||||
return NULL;
|
|
||||||
if (z == None) {
|
|
||||||
if (is_instanceobject(v) || is_instanceobject(w))
|
|
||||||
return instancebinop(v, w, "__pow__", "__rpow__");
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
/* XXX The ternary version doesn't do coercions */
|
|
||||||
if (is_instanceobject(v))
|
|
||||||
return v->ob_type->tp_as_number->nb_power(v, w, z);
|
|
||||||
}
|
|
||||||
if (v->ob_type->tp_as_number == NULL ||
|
if (v->ob_type->tp_as_number == NULL ||
|
||||||
(z!=None && z->ob_type->tp_as_number == NULL) ||
|
|
||||||
w->ob_type->tp_as_number == NULL) {
|
w->ob_type->tp_as_number == NULL) {
|
||||||
err_setstr(TypeError, "pow() requires numeric arguments");
|
err_setstr(TypeError, "pow() requires numeric arguments");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (coerce(&v, &w) != 0)
|
if (coerce(&v, &w) != 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
if (z == None) {
|
res = (*v->ob_type->tp_as_number->nb_power)(v, w, None);
|
||||||
x = (*v->ob_type->tp_as_number->nb_power)(v, w, z);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
object *v1, *z1, *w2, *z2;
|
|
||||||
x = NULL;
|
|
||||||
v1 = v;
|
|
||||||
z1 = z;
|
|
||||||
if (coerce(&v1, &z1) != 0)
|
|
||||||
goto error2;
|
|
||||||
w2 = w;
|
|
||||||
z2 = z1;
|
|
||||||
if (coerce(&w2, &z2) != 0)
|
|
||||||
goto error1;
|
|
||||||
x = (*v1->ob_type->tp_as_number->nb_power)(v1, w2, z2);
|
|
||||||
DECREF(w2);
|
|
||||||
DECREF(z2);
|
|
||||||
error1:
|
|
||||||
DECREF(v1);
|
|
||||||
DECREF(z1);
|
|
||||||
error2:
|
|
||||||
;
|
|
||||||
}
|
|
||||||
DECREF(v);
|
DECREF(v);
|
||||||
DECREF(w);
|
DECREF(w);
|
||||||
return x;
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
static object *
|
||||||
|
builtin_pow(self, args)
|
||||||
|
object *self;
|
||||||
|
object *args;
|
||||||
|
{
|
||||||
|
object *v, *w, *z = None, *res;
|
||||||
|
object *v1, *z1, *w2, *z2;
|
||||||
|
|
||||||
|
if (!newgetargs(args, "OO|O:pow", &v, &w, &z))
|
||||||
|
return NULL;
|
||||||
|
if (z == None)
|
||||||
|
return do_pow(v, w);
|
||||||
|
/* XXX The ternary version doesn't do class instance coercions */
|
||||||
|
if (is_instanceobject(v))
|
||||||
|
return v->ob_type->tp_as_number->nb_power(v, w, z);
|
||||||
|
if (v->ob_type->tp_as_number == NULL ||
|
||||||
|
z->ob_type->tp_as_number == NULL ||
|
||||||
|
w->ob_type->tp_as_number == NULL) {
|
||||||
|
err_setstr(TypeError, "pow() requires numeric arguments");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
if (coerce(&v, &w) != 0)
|
||||||
|
return NULL;
|
||||||
|
res = NULL;
|
||||||
|
v1 = v;
|
||||||
|
z1 = z;
|
||||||
|
if (coerce(&v1, &z1) != 0)
|
||||||
|
goto error2;
|
||||||
|
w2 = w;
|
||||||
|
z2 = z1;
|
||||||
|
if (coerce(&w2, &z2) != 0)
|
||||||
|
goto error1;
|
||||||
|
res = (*v1->ob_type->tp_as_number->nb_power)(v1, w2, z2);
|
||||||
|
DECREF(w2);
|
||||||
|
DECREF(z2);
|
||||||
|
error1:
|
||||||
|
DECREF(v1);
|
||||||
|
DECREF(z1);
|
||||||
|
error2:
|
||||||
|
DECREF(v);
|
||||||
|
DECREF(w);
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
static object *
|
static object *
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue