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

Fredrik Lundh fredrik at effbot.org
Mon Jan 29 12:28:23 EST 2001


Chuck won't just give up:
> I must be missing which post you refer to. I ultimately responded to
> the Tim Peters post that I see.

here's his post:

    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!

Note the second part ("more of a puzzle" is a polite way to say
"impossible, if you care about performance").

> I guess at some point I'll just have to hack this myself and report the
> benchmarks. Do you see what I'm saying about speed, though? It's
> generally faster to do operations in memory and then write one long
> byte string than it is to repeatedly write a small bits of data to disk.

Starting with 2.1a1, Python does this when importing from PYC
files  (if you follow the link I posted earlier, you might be able to
figure out who came up with that idea ;-).  But that's a special
case; in normal cases, load has to read the stream byte for byte.

But if all you want to do is write, use Tim's write(dumps) approach.
There's no way you can do it with fewer Python-level calls...

Cheers /F





More information about the Python-list mailing list