◐ Shell
clean mode source ↗

Message 144131 - Python tracker

Mark Dickinson <report@bugs.python.org> wrote:
> Yes, that's intentional.  When use of __int__ was deprecated, a bug
> report popped up from someone who wanted to be able to have their own
> objects treated as integers for the purposes of struct.pack. 
> (I don't recall which issue;  Meador, do you remember?)
>  So we added use of __index__ at that point.

Yes, I think that's #1530559, and the bug report was about PyCUDA. I can
see why 'bBhHiIlLqQ' allow __index__(), since they previously allowed
__int__().

I specifically meant the 'P' format. As far as I can see, PyLong_AsVoidPtr()
never allowed __int__(), but now index objects can be packed as pointers.
It isn't a big deal, I just have to know for features/pep-3118.

To illustrate, this is python2.5.0; INT is an object with an __int__() method:

'\x07\x00\x00\x00\x00\x00\x00\x00'
>>> struct.pack('P', INT(7))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/stefan/hg/r25/Lib/struct.py", line 63, in pack
    return o.pack(*args)
struct.error: cannot convert argument to long
>>>