[Python-ideas] Why can't we pickle module objects?
Guido van Rossum
guido at python.org
Tue Jul 9 05:43:44 CEST 2013
I don't think there is any particularly *good* reason, although it
would probably require some changes to the unpickling code
specifically to support this case (currently when given the name
'foo.bar' it tries to import foo but never bar, IIRC).
A bad reason might be that enabling this would make some people think
that pickling a module would somehow store the code. But pickling
classes and functions already has that disadvantage, so I don't think
it's the real reason.
Maybe you should just try your hands at a patch? Possibly you'd end up
discovering the real reason. :-)
--Guido
On Mon, Jul 8, 2013 at 8:36 PM, Haoyi Li <haoyi.sg at gmail.com> wrote:
>>>> 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
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>
--
--Guido van Rossum (python.org/~guido)
More information about the Python-ideas
mailing list