[issue12974] array module: deprecate '__int__' conversion support for array elements

Meador Inge report at bugs.python.org
Fri Sep 16 17:03:51 CEST 2011


Meador Inge <meadori at gmail.com> added the comment:

> 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

Huh, that's interesting.  It doesn't allow 'unsigned long' packs either (2.6.7):

Python 2.6.7+ (unknown, Sep 16 2011, 09:53:25)
[GCC 4.6.0 20110603 (Red Hat 4.6.0-10)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import struct
>>> class IDX(object):
...     def __init__(self, value):
...         self.value = value
...     def __int__(self):
...          return self.value
...
>>> for code in ['l', 'L', 'P']:
...    try:
...       struct.pack(code, IDX(9))
...    except Exception as e:
...       print "pack('%s'): %s" % (code, e)
...
'\t\x00\x00\x00'
pack('L'): unsupported operand type(s) for &: 'IDX' and 'long'
pack('P'): cannot convert argument to long

The behavior around '__int__' in previous versions seems somewhat accidental.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue12974>
_______________________________________


More information about the Python-bugs-list mailing list