[Zope3-checkins] CVS: Zope3/lib/python/Persistence - Module.py:1.31
Jeremy Hylton
jeremy@zope.com
Thu, 10 Oct 2002 18:26:05 -0400
Update of /cvs-repository/Zope3/lib/python/Persistence
In directory cvs.zope.org:/tmp/cvs-serv27986
Modified Files:
Module.py
Log Message:
If persistent modules are enabled, also make regular modules picklable.
=== Zope3/lib/python/Persistence/Module.py 1.30 => 1.31 ===
--- Zope3/lib/python/Persistence/Module.py:1.30 Thu Oct 10 17:36:18 2002
+++ Zope3/lib/python/Persistence/Module.py Thu Oct 10 18:26:05 2002
@@ -33,6 +33,23 @@
# builtins are explicitly assigned when a module is unpickled
import __builtin__
+# Modules aren't picklable by default, but we'd like them to be
+# pickled just like classes (by name).
+import copy_reg
+
+def _pickle_module(mod):
+ return mod.__name__
+
+def _unpickle_module(modname):
+ mod = __import__(modname)
+ if "." in modname:
+ parts = modname.split(".")[1:]
+ for part in parts:
+ mod = getattr(mod, part)
+ return mod
+
+copy_reg.pickle(type(copy_reg), _pickle_module, _unpickle_module)
+
# XXX Is this comment still relevant?
#
# There seems to be something seriously wrong with a module pickle