marshal doesn't work with "file-like" objects

Tim Peters tim.one at home.com
Fri Jan 26 15:17:10 EST 2001


[Chuck Esterbrook]
>  >>> from StringIO import StringIO
>  >>> f = StringIO()
>  >>> f.write('hi')
>  >>> import marshal
>  >>> marshal.dump(1, f)
> Traceback (most recent call last):
>    File "<stdin>", line 1, in ?
> TypeError: marshal.dump() 2nd arg must be file
>
> So is that a bug, a flaw or a feature?

It's functioning as documented (read the docs), and is also functioning as
designed (read next sentence), so it's not a bug.  As the docs say, marshal
is primarily intended for Python's own use in reading and writing .pyc and
.pyo files, and, as the docs don't say, to make it as fast as possible for
those purposes it doesn't support any luxuries.  So it's a feature from
Python's POV, but a flaw from yours.  Python wins <0.9 wink>.

You could do this instead:

    filelikeobject.write(marshal.dumps(object))

Reading a sequence of marshaled values back from a file-like object is more
of a puzzle!





More information about the Python-list mailing list