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

Rainer Deyke root at rainerdeyke.com
Mon Jan 29 13:59:39 EST 2001


"Fredrik Lundh" <fredrik at effbot.org> wrote in message
news:XKhd6.10096$AH6.1624190 at newsc.telia.net...
> 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").

This could be fixed by modifying marshal like this:

1. Modify 'marshal.dumps' to prepend the string with its length encoded as a
fixed-length string.

2. Modify 'marshal.loads' to skip the first 'n' bytes of the string, where
'n' is the length of the fixed-length string from above.

3. Implement 'marshal.dump' in terms of 'marshal.dumps'.  This wins you
file-like objects without significant performance loss, since there is only
a single write.

4. 'marshal.load' loads the fixed-length string from above, translates it
into an integer 'n', and reads an additional 'n' bytes.  It then passes the
all data read onto 'marshal.loads'.  File-like objects are again practical,
since there are only two reads.


--
Rainer Deyke (root at rainerdeyke.com)
Shareware computer games           -           http://rainerdeyke.com
"In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" - Abigor





More information about the Python-list mailing list