-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
Remove check for float subclasses with nb_float == NULL in PyNumber_Float() ? #112636
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I still not sure what is more correct. Should we remove the check for float subclasses without cc @mdickinson |
Simple question: when it could be |
You can create a float subclass in C and explicitly set |
@serhiy-storchaka, I think in this way we could break anything:) Here is a minimal example, when Subclass code: static PyNumberMethods xxx_as_number = {
.nb_float = NULL, /* nb_float will be inherited anyway */
};
static PyObject*
fun(PyObject *o, PyObject *Py_UNUSED(ignored))
{
PyNumberMethods *m = Py_TYPE(o)->tp_as_number;
m->nb_float = NULL;
Py_RETURN_NONE;
}
static PyMethodDef xxx_methods[] = {
{"break_it", (PyCFunction) fun, METH_NOARGS, NULL},
{NULL},
};
PyTypeObject XXX_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "xxx",
.tp_basicsize = sizeof(PyFloatObject),
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
.tp_base = &PyFloat_Type,
.tp_as_number = &xxx_as_number,
.tp_methods = xxx_methods,
}; Session example: >>> a = xxx(3.14)
>>> float(a) # nb_float == NULL is not triggered, due to inheritance
3.14
>>> a.break_it()
>>> float(a) # and only NOW this branch will work...
XXX
3.14 Edit: pr description was updated with this example. |
@serhiy-storchaka, I'm closing issue as well. Let me know if we should add a test for this. PS: As noted in the pr #112637 (comment), the integer workaround shouldn't be restored. |
Uh oh!
There was an error while loading. Please reload this page.
Similar check for int subclasses without nb_int was removed in 31a6554 from PyNumber_Long().
// transformed to a separate issue per @serhiy-storchaka suggestion in #112145
Linked PRs
The text was updated successfully, but these errors were encountered: