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

Paul Moore p.f.moore at gmail.com
Sat Nov 3 14:37:53 CET 2007


On 03/11/2007, Paul Moore <p.f.moore at gmail.com> wrote:
> On 03/11/2007, Christian Heimes <lists at cheimes.de> wrote:
> > No, it was a bug in the C code. 'H' represents a 16bit type. The correct
> > result is
>
> Ah, I misunderstood what the documentation was getting at. Thanks.
> Paul.

Hmm, I'm not sure if I did.

function:: array(typecode[, initializer])

   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.

>>> [n for n in b"1234"]
[49, 50, 51, 52]

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?

I think this needs clarifying - if your interpretation is correct, the
documentation needs an overhaul (and in the short term, the behaviour
needs explicitly stating in the PEP).

Paul.


More information about the Python-3000 mailing list