[issue1172711] long long support for array module

Stefan Krah report at bugs.python.org
Mon Sep 12 21:55:03 CEST 2011


Stefan Krah <stefan-usenet at bytereef.org> added the comment:

I made the observation on Rietveld that the following code is never
executed by the test suite. The same applies to similar existing
passages in arraymodule.c:

http://bugs.python.org/review/1172711/diff/3310/10310#newcode394


Meador correctly pointed out that the code allows for duck typing.
But the struct module (and by extension memoryview that must follow
the struct module) don't:

>>> import array, struct
>>> a = array.array('L', [1,2,3])
>>> class T(object):
...     def __init__(self, value):
...         self.value = value
...     def __int__(self):
...          return self.value
...
>>> a = array.array('L', [1,2,3])
>>> struct.pack_into('L', a, 0, 9)
>>> a
array('L', [9, 2, 3])
>>> a[0] = T(100)
>>> a
array('L', [100, 2, 3])
>>> struct.pack_into('L', a, 0, T(200))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
struct.error: required argument is not an integer
>>>

I vastly prefer the struct module behavior. Since the code isn't executed
by any tests:

Is it really the intention for array to allow duck typing? The documentation
says:

"This module defines an object type which can compactly represent an array
 of basic values: characters, integers, floating point numbers."

"Basic value" doesn't sound to me like "anything that has an __int__() method".


Also, consider this:

>>> sum([T(1),T(2),T(3)])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'T'

>>> sum(array.array('L', [T(1),T(2),T(3)]))
6

----------

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


More information about the Python-bugs-list mailing list