Iterating over marshal

Tim Lesher tlesher at
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:
    except EOFError:

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:
            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


Tim Lesher
tlesher at

More information about the Python-list mailing list