-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
BUG: number.__int__ and int(number) are not the same #9972
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
Not only because of reassignment after It successfully calls It fails with
|
Just to clarify - |
I'm wondering if the culprit is |
That would add another layer of abstraction, slowing down the conversion for the 99% of the cases when a |
Right, but it also means that our implementation right now is incorrect. |
Another example: >>> a = np.array(None)
>>> a[()] = bytearray('10')
>>> int(a)
TypeError: cannot convert to an int; scalar object is not a number
>>> int(bytearray('10'))
10 |
Rather than assuming that any object array is self-referencing, we can just use PyEnter_RecursiveCall in: * `__int__` * ` 8000 __float__` * `__long__` * `__hex__` * `__oct__` This works towards (but does _not_ fix) numpy#9972, by not directly touching the `nb_*` slots ourselves. Substantial code deduplication here. Error message is different, but perhaps also better: ```python >>> int(np.array([None])) TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType' # now TypeError: cannot convert to an int; scalar object is not a number #before ```
#10042 fixed these. These now work
and
|
It looks like everything works here now. Can this be closed @mattip? |
Closing. Please reopen if more discrepancies appear |
Uh oh!
There was an error while loading. Please reload this page.
As worked around in #9181, we have two implementation of every
number.__int__
, one inscalarmath
(int
), and one inscalartypes
(__int__
).This happens because after calling
PyType_Ready
, we reassigntp_number
, but the__int__
slot wrapper has already been created.It's super misleading that this is the case, and exposes cpython implementation details.
It seems to me it would be safer to not set
__int__
at all until we fix this, rather than having two possibly different implementation to trap the end userThe text was updated successfully, but these errors were encountered: