Iterating over marshal

Tim Lesher tlesher at gmail.com
Fri Oct 6 21:40:13 CEST 2006


I'm using the marshal library to unmarshal a file containing one or
more objects.  The canonical way seems to be:

objs = []
while 1:
    try:
        objs.append(marshal.load(fobj))
    except EOFError:
        break

Maybe it's just me, but it seems as if this should be iterable.  I can
get the behavior I want by writing:

def itermarshal(fobj):
    while 1:
        try:
            yield marshal.load(fobj)
        except EOFError:
            raise StopIteration

objs = [obj for obj in itermarshal(fobj)]

But it seems that this should be built-in somewhere.  Given that the
marshal library has been around since roughly forever, is it just that
no one's bothered to add iteration support to it, or am I missing
something?

Thanks.

-- 
Tim Lesher
tlesher at gmail.com




More information about the Python-list mailing list