[Python-ideas] Why can't we pickle module objects?
Haoyi Li
haoyi.sg at gmail.com
Tue Jul 9 05:36:04 CEST 2013
>>> import pickle
>>> pickle.dumps(pickle.dumps)
'cpickle\ndumps\np0\n.'
>>> pickle.dumps(pickle)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Runtimes\Python\lib\pickle.py", line 1374, in dumps
Pickler(file, protocol).dump(obj)
File "C:\Runtimes\Python\lib\pickle.py", line 224, in dump
self.save(obj)
File "C:\Runtimes\Python\lib\pickle.py", line 306, in save
rv = reduce(self.proto)
File "C:\Runtimes\Python\lib\copy_reg.py", line 70, in _reduce_ex
raise TypeError, "can't pickle %s objects" % base.__name__
TypeError: can't pickle module objects
I know that you can't, but why not? You can pickle class objects and
function objects in modules, and their static path is stored so that when
you unpickle it, the correct module is imported and the function is
retrieved from that module. It seems odd an inconsistent that you can't
pickle the module object itself; can't it just store itself as a name, and
have loads() import it and return the resultant module object?
This isn't entirely of academic interest; I'm working with some code which
is meant to pickle/unpickle arbitrary things, and occasionally it blows up
when i accidentally pass in a module. It's always possible to work around
the TypeErrors by just changing the stuff I pass in for serializing from
the module to the exact function/class i want, but it seems like needless
pain.
Is there any good reason we don't let people pickle module objects using
the same technique that we use to pickle classes and top-level functions?
-Haoyi
-Haoyi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130709/232c093b/attachment.html>
More information about the Python-ideas
mailing list