Author skrah
Recipients cmcqueen1975, mark.dickinson, mattchaput, meador.inge, ocean-city, orenti, skrah, vstinner
Date 2011-09-12.19:55:02
SpamBayes Score 1.56782e-06
Marked as misclassified No
Message-id <1315857303.94.0.891677568361.issue1172711@psf.upfronthosting.co.za>
In-reply-to
Content
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
History
Date User Action Args
2011-09-12 19:55:04skrahsetrecipients: + skrah, orenti, mark.dickinson, vstinner, ocean-city, cmcqueen1975, meador.inge, mattchaput
2011-09-12 19:55:03skrahsetmessageid: <1315857303.94.0.891677568361.issue1172711@psf.upfronthosting.co.za>
2011-09-12 19:55:03skrahlinkissue1172711 messages
2011-09-12 19:55:02skrahcreate