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
can't set big int-like objects to items in array 'Q', 'L' and 'I' #72485
Comments
------------ current state ------------ However, in the following, an OverflowError('Python int too large to convert to C long') is raised on the last line: The reason for this behavior is the implementation of the function LL_setitem (in Modules/arraymodule.c) (edited brutally for brevity):
LL_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
{
unsigned long x;
if (PyLong_Check(v)) {
x = PyLong_AsUnsignedLong(v);
}
else {
long y;
PyArg_Parse(v, "l;array item must be integer", &y);
x = (unsigned long)y;
}
(ap->ob_item)[i] = x;
}
The problem is that PyArg_Parse is used to convert a Python int into a C long. So PyArg_Parse fails when it is given a Python int which is in range(LONG_MAX + 1, ULONG_MAX + 1), even though such Python int can be stored in a C unsigned long. It is quite the same for array('I') and array('Q') (i.e. in II_setitem and in QQ_setitem, respectively). With regard to relevant changes made in the past, PyArg_Parse was always used (in case '!PyLong_Check(v)'), since adding the original versions of: ------------ proposed changes ------------ 1. In Modules/arraymodule.c, change the implementation of LL_setitem (and likewise, of II_setitem and QQ_setitem) roughly to the following:
LL_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
{
unsigned long x;
if (!PyLong_Check(v)) {
v = _PyLong_FromNbInt(v);
}
x = PyLong_AsUnsignedLong(v);
(ap->ob_item)[i] = x;
}
Note that issue bpo-12974 (opened in 2011) proposes deprecating the ability to set int-like objects to items in an integers array. (I am not sure how that affects my patch, but I guess it should be noted.) ------------ diff ------------ (Note that the I didn't propose to change the perplexing error message 'unsigned int is greater than maximum' in II_setitem, as there are many such messages in the codebase, and I am working on a patch for them as part of issue bpo-15988.) ------------ tests ------------ |
One comment here: it's not the presence of |
You are right about the terminology of course. My bad. Anyway, the bug is not related to __index__ (or nb_index), because the three aforementioned functions are using (in case '!PyLong_Check(v)') PyArg_Parse with the formats 'l' or 'L'. But just in case, I ran the following: |
Right, see bpo-12974 for more on arrays, |
Ah, I should have read more about __int__ and __index__ before writing my last reply. With regard to my patch - should I make the following changes? |
ping |
LGTM. But maybe combine PyFloat_Check+_PyLong_FromNbInt in one helper function? Could you please create a PR Oren? |
yes and yes. and thanks for the review :) |
Thank you for your contribution Oren. |
Thanks for the reviews :) |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: