[Zope-Checkins] CVS: Zope3/lib/python/Persistence - Function.py:1.2
Jeremy Hylton
jeremy@zope.com
Fri, 21 Jun 2002 18:51:12 -0400
Update of /cvs-repository/Zope3/lib/python/Persistence
In directory cvs.zope.org:/tmp/cvs-serv16325
Modified Files:
Function.py
Log Message:
Remove dependency of code object on module's _p_serial.
Any time the module is modified it will update all the function
objects in it. A test on the _p_serial isn't effective, anyway; the
serial number doesn't change until after the transaction is committed,
but the function _code dict needs to be updated before the commit.
=== Zope3/lib/python/Persistence/Function.py 1.1 => 1.2 ===
self._code = {}
+ def __repr__(self):
+ return "<PersistentFunction %s.%s>" % (self._module.__name__,
+ self._func.func_name)
+
def __call__(self, *args, **kwargs):
+ # We must make sure that _module is loaded when func is
+ # executed because the function may reference a global
+ # variable and that global variable must be in the module's
+ # __dict__. We can't use a PersistentMapping because the
+ # interpreter requires that globals be a real dict.
+
+ self._module._p_activate()
+
+ # XXX What if the function module is deactivated while the
+ # function is executing? It seems like we need to expose
+ # refcounts at the Python level to guarantee that this will
+ # work.
+
return self._func(*args, **kwargs)
def __getstate__(self):
@@ -45,13 +62,9 @@
# running the same version of Python and with the same
# __debug__ value. Store code in a dict keyed by these two values.
- # The code object is only valid if the module hasn't been
- # changed. If the module is changed, then the code object
- # must be reloaded from the module. To detect this change,
- # store the serial number of the module.
key = sys.version_info, __debug__
if key not in self._code:
- self._code[key] = get_code_args(code), self._module._p_serial
+ self._code[key] = get_code_args(code)
return func_state, self._code, self._module
@@ -62,15 +75,11 @@
# recreate the code object
code = None
key = sys.version_info, __debug__
- co_args, serial = self._code.get(key, (None, None))
- if serial is not None:
- if serial != mod._p_serial:
- del self._code[key]
- else:
- code = new.code(*co_args)
- if code is None:
- # XXX get it from the module
- assert False, "not implemented"
+ co_args = self._code.get(key, None)
+ if co_args is None:
+ assert False, "not implemented yet"
+ else:
+ code = new.code(*co_args)
func_defaults, func_dict = func
func = new.function(code, mod.__dict__, func_defaults)