[Python-ideas] Why can't we pickle module objects?

Haoyi Li haoyi.sg at gmail.com
Tue Jul 9 12:20:53 CEST 2013


> You can work around it fairly easily with copyreg:

I actually just realized that after i downloading the python repo and was
poking around how to proceed (actually, no, i found a way manually
monkeypatching pickle.Pickler.dispatch, but i guess it's close enough).

Regardless, *should* it work with modules? I'm in the mood to try and make
a (first) contribution to python, and this seems like a feasible (not too
hard) patch, If a patch is desirable/potentially desirable.

I guess the (hypothetical) patch would cover all versions {2.7, 3.3} X
{pickle, cPickle}?

-Haoyi

-Haoyi


On Tue, Jul 9, 2013 at 5:22 PM, Richard Oudkerk <shibturn at gmail.com> wrote:

> You can work around it fairly easily with copyreg:
>
> >>> import sys, copyreg, pickle
> >>> def reduce_mod(m):
> ...   assert sys.modules[m.__name__] is m
> ...   return rebuild_mod, (m.__name__,)
> ...
> >>> def rebuild_mod(name):
> ...   __import__(name)
> ...   return sys.modules[name]
> ...
> >>> copyreg.pickle(type(sys), reduce_mod)
> >>> pickle.loads(pickle.dumps(sys)**)
> <module 'sys' (built-in)>
>
> --
> Richard
>
>
> ______________________________**_________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/**mailman/listinfo/python-ideas<http://mail.python.org/mailman/listinfo/python-ideas>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130709/d7ff3a23/attachment.html>


More information about the Python-ideas mailing list