Stefan Krah <stefan-use...@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 <rep...@bugs.python.org> <http://bugs.python.org/issue1172711> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com