proof-of-concept for a simple AOP implementation

git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@1968 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
helmutm 2007-08-27 19:39:25 +00:00
parent a3dac1b675
commit 1e1feb6fd5
2 changed files with 6 additions and 15 deletions

View file

@ -25,16 +25,12 @@ $Id$
def getNotifier(method): def getNotifier(method):
if isinstance(method, (Notifier, BoundNotifier)): if isinstance(method, (Notifier, BoundNotifier)):
# already wrapped
return method return method
if method.im_self is not None: if method.im_self is not None:
name = method.im_func.func_name # wrapping instance method - let's also wrap the class-level method
classMethod = getattr(method.im_class, name) classNotifier = getNotifier(getattr(method.im_class, method.im_func.func_name))
if not isinstance(classMethod, Notifier): return BoundNotifier(classNotifier, method.im_self)
classMethod = Notifier(classMethod)
setattr(method.im_class, name, classMethod)
notifier = BoundNotifier(classMethod, method.im_self)
setattr(method.im_self, name, notifier)
return notifier
return Notifier(method) return Notifier(method)
@ -56,9 +52,7 @@ class Notifier(object):
if isinstance(existing, BoundNotifier): if isinstance(existing, BoundNotifier):
# the instance's method is already wrapped # the instance's method is already wrapped
return existing return existing
notifier = BoundNotifier(self, instance) return BoundNotifier(self, instance)
setattr(instance, self.name, notifier)
return notifier
def __call__(self, *args, **kw): def __call__(self, *args, **kw):
for sub, sargs, skw in self.beforeSubs: for sub, sargs, skw in self.beforeSubs:
@ -80,6 +74,7 @@ class BoundNotifier(object):
def __init__(self, context, instance): def __init__(self, context, instance):
self.context = context self.context = context
self.instance = instance self.instance = instance
setattr(instance, context.name, self)
self.beforeSubs = [] self.beforeSubs = []
self.afterSubs = [] self.afterSubs = []

View file

@ -84,7 +84,3 @@ Combining class and instance level wrapping
logging: after foo, result=48 logging: after foo, result=48
48 48
TODO: check case where instance is wrapped first, then class -
that means: always wrap class when wrapping an instance, and always
wrap instance with BoundNotifier.