Is there anything that pickle + copy_reg cannot serialize?
mauriceling at acm.org
Thu Dec 8 19:17:10 CET 2005
> Since copy_reg lets you specify arbitrary code to serialize arbitrary
> objects, you shouldn't run into any single object that you cannot
> serialize to a pickle.
In http://www.effbot.org/librarybook/pickle.htm, it specifically
mentions that code objects cannot be pickled and require the use of
copy_reg, as follows:
import pickle, marshal, types
# register a pickle handler for code objects
return code_unpickler, (marshal.dumps(code),)
copy_reg.pickle(types.CodeType, code_pickler, code_unpickler)
# try it out
CODE = """
print "suppose he's got a pointed stick"
code = compile(CODE, "<string>", "exec")
I cannot understand 2 things, which I seek assistance for:
1. Is code object the only thing can cannot be pickled (less facing
2. In the above example, how copy_reg works with pickle?
Thanks and Cheers
> However, both pickle implementations are recursive, so you will be
> limited by the amount of memory you can allocate for your stack. By
> default, this will limit you to something like object graphs 333 edges
> deep or so (if I'm counting stack frames correctly). Note that this
> does not mean you cannot serialize more than 333 objects at a time,
> merely that if it takes 333 or more steps to go from the first object to
> any other object in the graph (using the traversal order pickle uses),
> the pickling will fail. You can raise this limit, to a point, with
More information about the Python-list