[Zope3-checkins] CVS: Zope3/lib/python/Zope/ContextWrapper - SimpleMethodWrapper.py:1.4
Jim Fulton
jim@zope.com
Mon, 11 Nov 2002 14:40:43 -0500
Update of /cvs-repository/Zope3/lib/python/Zope/ContextWrapper
In directory cvs.zope.org:/tmp/cvs-serv10777
Modified Files:
SimpleMethodWrapper.py
Log Message:
It's possible to have a property, defined in a class with a name that
is the same as a key in an instance dictionary. This is useful if the
property mediates retrival and update of the stored value.
ContentProperties don't handle this case properly. I fixed the
attribute getting case. The setting case still needs to be fixed.
=== Zope3/lib/python/Zope/ContextWrapper/SimpleMethodWrapper.py 1.3 => 1.4 ===
--- Zope3/lib/python/Zope/ContextWrapper/SimpleMethodWrapper.py:1.3 Thu Oct 31 07:30:49 2002
+++ Zope3/lib/python/Zope/ContextWrapper/SimpleMethodWrapper.py Mon Nov 11 14:40:43 2002
@@ -12,6 +12,17 @@
#
##############################################################################
+##############################################################################
+#
+# Approach
+#
+# The facilities here work by adding markers on methods or properties
+# that a custom wrapper class looks for. We rely on the custom
+# wrapper class's getattr to rebind things on the way out.
+#
+##############################################################################
+
+
# This method wrapper does not work for builtin methods.
from Zope.ContextWrapper.wrapper import Wrapper, getbaseobject
@@ -58,30 +69,15 @@
getbaseobject=getbaseobject):
"""Support for ContextMethod and ContextProperty.__get__"""
obj = getbaseobject(self)
- if name not in (getattr(obj, '__dict__', empty_dict) or
- getattr(obj, '__slots__', empty_dict)):
- attr = getattr(obj.__class__, name, None)
- if attr is not None:
- attrdict = getattr(attr, '__dict__', empty_dict)
- if attrdict.get('Zope.ContextWrapper.contextful_get'):
- return attr.__get__(self)
+ class_ = obj.__class__
+ class_value = getattr(class_, name, None)
+ if hasattr(class_value, '__get__'):
+ attrdict = getattr(class_value, '__dict__', empty_dict)
+ if attrdict.get('Zope.ContextWrapper.contextful_get'):
+ return class_value.__get__(self, class_)
+
return Wrapper.__getattribute__(self, name)
- # This has much the same effect, but the call to
- # Wrapper.__getattribute__ is more "correct".
- #
- #return getattr(obj, name)
-
-
- # Original getattribute that doesn't support gettable properties.
- # Left in so it can be reenabled if we decide we don't actually
- # need gettable properties.
- #
- #def __getattribute__(self, name, empty_dict={}):
- # attr = Wrapper.__getattribute__(self, name)
- # attrdict = getattr(attr, '__dict__', empty_dict)
- # if attrdict.get('Zope.ContextWrapper.contextful'):
- # attr = attr.__get__(self)
- # return attr
+
def __setattr__(self, name, value, empty_dict={},