[Python-Dev] an idea for improving struct.unpack api

Guido van Rossum gvanrossum at gmail.com
Sat Jan 8 17:52:31 CET 2005


First, let me say two things:

(a) A higher-level API can and should be constructed which acts like a
(binary) stream but has additional methods for reading and writing
values using struct format codes (or, preferably, somewhat
higher-level type names, as suggested). Instances of this API should
be constructable from a stream or from a "buffer" (e.g. a string).

(b) -1 on Ilya's idea of having a special object that acts as an
input-output integer; it is too unpythonic (no matter your objection).

[Paul Moore]
> OTOH, Nick's idea of returning a tuple with the new offset might make
> your example shorter without sacrificing readability:
> 
>     result, newpos = struct.unpack('>l', self.__buf, self.__pos)
>     self.__pos = newpos # retained "newpos" for readability...
>     return result

This is okay, except I don't want to overload this on unpack() --
let's pick a different function name like unpack_at().

> A third possibility - rather than "magically" adding an additional
> return value because you supply a position, you could have a "where am
> I?" format symbol (say & by analogy with the C "address of" operator).
> Then you'd say
> 
>     result, newpos = struct.unpack('>l&', self.__buf, self.__pos)
> 
> Please be aware, I don't have a need myself for this feature - my
> interest is as a potential reader of others' code...

I think that adding more magical format characters is probably not
doing the readers of this code a service.

I do like the idea of not introducing an extra level of tuple to
accommodate the position return value but instead make it the last
item in the tuple when using unpack_at().

Then the definition would be:

def unpack_at(fmt, buf, pos):
    size = calcsize(fmt)
    end = pos + size
    data = buf[pos:end]
    if len(data) < size:
        raise struct.error("not enough data for format")
    # if data is too long that would be a bug in buf[pos:size] and
cause an error below
    ret = unpack(fmt, data)
    ret = ret + (end,)
    return ret

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-Dev mailing list