[Zope-Checkins] CVS: Zope3/lib/python/Persistence - Function.py:1.6
Jeremy Hylton
jeremy@zope.com
Wed, 10 Jul 2002 18:53:41 -0400
Update of /cvs-repository/Zope3/lib/python/Persistence
In directory cvs.zope.org:/tmp/cvs-serv23914
Modified Files:
Function.py
Log Message:
Deal with lambdas stored as default arguments.
=== Zope3/lib/python/Persistence/Function.py 1.5 => 1.6 ===
import dis
import new
import sys
+# in 2.3, this will be spelled new.function
+from types import FunctionType as function
from Persistence import Persistent
@@ -27,7 +29,7 @@
co.co_lnotab,
co.co_freevars,
co.co_cellvars)
-
+
class PersistentFunction(Persistent):
def __init__(self, func, module):
@@ -41,10 +43,30 @@
self._v_side_effect = has_side_effect(func)
self._pf_module = module
self._pf_code = {}
+ self._fixup_contained()
def __repr__(self):
return "<PersistentFunction %s.%s>" % (self._pf_module.__name__,
self._pf_func.func_name)
+
+ def _fixup_contained(self):
+ # The function object may contain other function objects as a
+ # default value for an argument. These functions are
+ # converted to persistent objects, but are not updated in
+ # place when the containing module is changed.
+ new = {}
+ defaults = self._pf_func.func_defaults
+ if defaults is None:
+ return
+ for i in range(len(defaults)):
+ obj = defaults[i]
+ if isinstance(obj, function):
+ new[i] = PersistentFunction(obj, self._pf_module)
+ if new:
+ new_defs = list(defaults)
+ for i, pf in new.items():
+ new_defs[i] = pf
+ self._pf_func.func_defaults = tuple(new_defs)
# We need attribute hooks to handle access to _pf_ attributes in a
# special way. All other attributes should be looked up on