[Zope-Checkins] CVS: Zope3/lib/python/Persistence - Module.py:1.2

Jeremy Hylton jeremy@zope.com
Fri, 21 Jun 2002 15:28:44 -0400


Update of /cvs-repository/Zope3/lib/python/Persistence
In directory cvs.zope.org:/tmp/cvs-serv14649

Modified Files:
	Module.py 
Log Message:
Add support for in-place update of modules and functions.



=== Zope3/lib/python/Persistence/Module.py 1.1 => 1.2 ===
 
     def module_from_source(self, name, source):
-        m = self._modules[name] = PersistentModule(name)
+        self._modules[name] = PersistentModule(name)
+        self.update_module(name, source)
+
+    def update_module(self, name, source):
         self._sources[name] = source
+        m = self._modules[name]
+        copy = m.__dict__.copy()
         exec source in m.__dict__
-        self.fixup(m)
+        self.fixup(m.__dict__, copy, m)
+        
+    def fixup(self, new, old, module):
+        # Update persistent objects in place, and
+        # convert new functions to persistent functions
+        # XXX should convert classes, too
 
-    def fixup(self, m):
-        # Converts function objects in module m to persistent functions
-        # XXX should do classes, too
-        for k, v in m.__dict__.items():
+        for k, v in new.items():
             if isinstance(v, function):
-                m.__dict__[k] = PersistentFunction(v, m)
+                v = new[k] = PersistentFunction(v, module)
+            
+            old_v = old.get(k)
+            if old_v is not None:
+                # XXX the type test below works for functions, but may
+                # not work for classes or other objects
+                if (isinstance(old_v, Persistent)
+                    and type(old_v) == type(v)):
+                    state = v.__getstate__()
+                    old_v.__setstate__(state)
+                    new[k] = old_v