mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
This commit is contained in:
parent
879c581826
commit
5524a59b09
2 changed files with 40 additions and 50 deletions
|
@ -319,6 +319,39 @@ testbool(v)
|
|||
return res;
|
||||
}
|
||||
|
||||
/* Coerce two numeric types to the "larger" one.
|
||||
Increment the reference count on each argument.
|
||||
Return -1 and raise an exception if no coercion is possible
|
||||
(and then no reference count is incremented).
|
||||
*/
|
||||
|
||||
int
|
||||
coerce(pv, pw)
|
||||
object **pv, **pw;
|
||||
{
|
||||
register object *v = *pv;
|
||||
register object *w = *pw;
|
||||
int res;
|
||||
|
||||
if (v->ob_type == w->ob_type && !is_instanceobject(v)) {
|
||||
INCREF(v);
|
||||
INCREF(w);
|
||||
return 0;
|
||||
}
|
||||
if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
|
||||
res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
|
||||
if (res <= 0)
|
||||
return res;
|
||||
}
|
||||
if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
|
||||
res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
|
||||
if (res <= 0)
|
||||
return res;
|
||||
}
|
||||
err_setstr(TypeError, "number coercion failed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
NoObject is usable as a non-NULL undefined value, used by the macro None.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue