[Zope-Checkins] CVS: Zope3/lib/python/Zope/ContextWrapper - SimpleMethodWrapper.py:1.1.2.1 __init__.py:1.1.2.3

Steve Alexander steve@cat-box.net
Wed, 17 Apr 2002 18:33:31 -0400


Update of /cvs-repository/Zope3/lib/python/Zope/ContextWrapper
In directory cvs.zope.org:/tmp/cvs-serv20787/lib/python/Zope/ContextWrapper

Modified Files:
      Tag: Zope-3x-branch
	__init__.py 
Added Files:
      Tag: Zope-3x-branch
	SimpleMethodWrapper.py 
Log Message:
Amended various modules to import Wrapper, getdict and getcontext from
Zope.ContextWrapper rather than Zope.ContextWrapper.wrapper

Added a SimpleMethodWrapper, which is a rather hacky python way of
rebinding methods so that the im_self gets wrapped.

Now, you'll get a SimpleMethodWrapper rather than a Wrapper when you
say from Zope.ContextWrapper import Wrapper

You say that a particular method should get its im_self rebound by
making it into a ContextMethod, in a similar way to making a staticmethod
or a classmethod.

See the testSimpleMethodWrapper.py module for an example.



=== Added File Zope3/lib/python/Zope/ContextWrapper/SimpleMethodWrapper.py ===
# This method wrapper does not work for builtin methods.

from Zope.ContextWrapper import Wrapper

class ContextMethod(object):
    def __new__(cls, method):
        try:
            method.__dict__['Zope.ContextWrapper.contextful'] = 1
        except AttributeError:
            raise TypeError, \
                "Cannot make %s into a contextmethod" % type(method)
        return method

class SimpleMethodWrapper(Wrapper):

    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



=== Zope3/lib/python/Zope/ContextWrapper/__init__.py 1.1.2.2 => 1.1.2.3 ===
 """
 from wrapper import *
+from SimpleMethodWrapper import SimpleMethodWrapper as Wrapper
+from SimpleMethodWrapper import ContextMethod