[Python-3000] need help fixing broken tests in py3k-pep3137 branch

Christian Heimes lists at cheimes.de
Sat Nov 3 14:49:13 CET 2007

Paul Moore wrote:
>    Return a new array whose items are restricted by *typecode*, and initialized
>    from the optional *initializer* value, which must be a list,
> string, or iterable
>    over elements of the appropriate type.
> My instinct says that a bytes object is "an iterable over elements of
> the appropriate type" for 'H', in the sense that iterating over bytes
> returns a sequence of integers.

You are partly right. The documentation needs an update. It should say
"which mist be a list, string, byte sequence or iterable ...".

> On that basis, array.array('H', b"1234") should construct an array out
> of the 4 integers 49, 50, 51 and 52.
> You seem to be saying that array.array treats the bytes object as a
> block of memory, and splits it up according to the typecode. Isn't
> that the job of the struct module?

In Python 2.x the array module handles strings in the same way:

Python 2.5.1 (r251:54863, Oct  5 2007, 13:36:32)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import array
>>> array.array('H', "1234")
array('H', [12849, 13363])

Bytes are the successor of Python 2.x's byte strings (str). They are
even using the same C type PyString. My patch has only restored the old
behavior of PyString.


More information about the Python-3000 mailing list