Skip to content

Outdated statement in docs comparing pickle with marshal #143928

@aescarias

Description

@aescarias

I found this statement in the pickle docs comparing the pickle module with the marshal module:

The pickle module differs from marshal in several significant ways:

  • The pickle module keeps track of the objects it has already serialized, so that later references to the same object won’t be serialized again. marshal doesn’t do this.

    This has implications both for recursive objects and object sharing. Recursive objects are objects that contain references to themselves. These are not handled by marshal, and in fact, attempting to marshal recursive objects will crash your Python interpreter. Object sharing happens when there are multiple references to the same object in different places in the object hierarchy being serialized. pickle stores such objects only once, and ensures that all other references point to the master copy. Shared objects remain shared, which can be very important for mutable objects.

However, this appears to no longer be the case as marshal version 3 (added in Python 3.4) supports keeping track of objects via references and as such is capable of serializing recursive objects.

>>> import marshal
>>> xs = [1, 2, 3]
>>> xs.append(xs)
>>> marshal.dumps(xs)
b'\xdb\x04\x00\x00\x00\xe9\x01\x00\x00\x00\xe9\x02\x00\x00\x00\xe9\x03\x00\x00\x00r\x00\x00\x00\x00'

(in Python 3.3, marshal.dumps(xs) fails with ValueError: object too deeply nested to marshal)

So I don't see any reason for this statement to be in the docs in its current state.

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dir

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions