mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
Add additional coercion support for "self subtypes" to int, long,
float (compare the recent checkin to complex). Added tests for these.
This commit is contained in:
parent
d5d8e4a436
commit
1952e388ca
4 changed files with 50 additions and 1 deletions
|
@ -590,6 +590,11 @@ float_coerce(PyObject **pv, PyObject **pw)
|
|||
Py_INCREF(*pv);
|
||||
return 0;
|
||||
}
|
||||
else if (PyFloat_Check(*pw)) {
|
||||
Py_INCREF(*pv);
|
||||
Py_INCREF(*pw);
|
||||
return 0;
|
||||
}
|
||||
return 1; /* Can't do it */
|
||||
}
|
||||
|
||||
|
|
|
@ -782,6 +782,17 @@ int_or(PyIntObject *v, PyIntObject *w)
|
|||
return PyInt_FromLong(a | b);
|
||||
}
|
||||
|
||||
static int
|
||||
int_coerce(PyObject **pv, PyObject **pw)
|
||||
{
|
||||
if (PyInt_Check(*pw)) {
|
||||
Py_INCREF(*pv);
|
||||
Py_INCREF(*pw);
|
||||
return 0;
|
||||
}
|
||||
return 1; /* Can't do it */
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
int_int(PyIntObject *v)
|
||||
{
|
||||
|
@ -904,7 +915,7 @@ static PyNumberMethods int_as_number = {
|
|||
(binaryfunc)int_and, /*nb_and*/
|
||||
(binaryfunc)int_xor, /*nb_xor*/
|
||||
(binaryfunc)int_or, /*nb_or*/
|
||||
0, /*nb_coerce*/
|
||||
int_coerce, /*nb_coerce*/
|
||||
(unaryfunc)int_int, /*nb_int*/
|
||||
(unaryfunc)int_long, /*nb_long*/
|
||||
(unaryfunc)int_float, /*nb_float*/
|
||||
|
|
|
@ -2134,6 +2134,11 @@ long_coerce(PyObject **pv, PyObject **pw)
|
|||
Py_INCREF(*pv);
|
||||
return 0;
|
||||
}
|
||||
else if (PyLong_Check(*pw)) {
|
||||
Py_INCREF(*pv);
|
||||
Py_INCREF(*pw);
|
||||
return 0;
|
||||
}
|
||||
return 1; /* Can't do it */
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue