[Zope3-checkins] CVS: Zope3/src/zope/proxy/context/tests - test_wrapper.py:1.6
Steve Alexander
steve@cat-box.net
Thu, 10 Apr 2003 04:54:05 -0400
Update of /cvs-repository/Zope3/src/zope/proxy/context/tests
In directory cvs.zope.org:/tmp/cvs-serv14998/src/zope/proxy/context/tests
Modified Files:
test_wrapper.py
Log Message:
Restored the previous style of unit tests where new type objects are
created on each test-run.
Fixed various reference leaks in wrapper.c.
Running the wrapper tests with -Lr now reports no leaks.
=== Zope3/src/zope/proxy/context/tests/test_wrapper.py 1.5 => 1.6 ===
--- Zope3/src/zope/proxy/context/tests/test_wrapper.py:1.5 Wed Apr 9 10:50:52 2003
+++ Zope3/src/zope/proxy/context/tests/test_wrapper.py Thu Apr 10 04:54:05 2003
@@ -21,89 +21,6 @@
_marker = object()
-# We need to make a load of new types. If tests are run multiple times,
-# only make each required new type once, and cache it here.
-# Otherwise, it looks like we have a refcount leak.
-class_lookup = {}
-
-class ForTestContextAwareGetattr(ContextAware):
- def __init__(self, retval):
- self.args = None
- self.retval = retval
- def __getattr__(self, name):
- if name == '__del__':
- # We don't want Python's gc to think that we have a
- # __del__, otherwise cycles will not be collected.
- raise AttributeError, name
- self.args = self, name
- return self.__dict__['retval']
- def getArgs(self):
- # Need to get __dict__ from the clean object, because it
- # is a special descriptor and complains bitterly about
- # being got from the wrong kind of object.
- return getobject(self).__dict__['args']
-
-class ForTestNormalGetattr(object):
- def __init__(self, retval):
- self.args = None
- self.retval = retval
- def __getattr__(self, name):
- if name == '__del__':
- # We don't want Python's gc to think that we have a
- # __del__, otherwise cycles will not be collected.
- raise AttributeError, name
- self.__dict__['args'] = self, name
- return self.__dict__['retval']
- def getArgs(self):
- return self.__dict__['args']
-
-class ForTestContextMethodGetattr(object):
- def __getattr__(self, name):
- return 23
- __getattr__ = ContextMethod(__getattr__)
-
-class ForTestContextMethodGetattr2(object):
- def __getattr__(self, name):
- return 23
- __getattr__ = classmethod(__getattr__)
-
-class ForTestProperty(object):
- def getFoo(self):
- self.called_with = self
- return 42
- def setFoo(self, value):
- self.called_with = self, value
- foo = property(getFoo, setFoo)
- context_foo = ContextProperty(getFoo, setFoo)
-
-class ForContextAwareProperty(ContextAware):
- def getFoo(self):
- self.called_with = self
- return 42
- def setFoo(self, value):
- self.called_with = self, value
- foo = property(getFoo, setFoo)
-
-class ForTestWrapperSubclassAttributes(wrapper.Wrapper):
- def __init__(self, ob):
- super(ForTestWrapperSubclassAttributes, self).__init__(ob)
- self.foo = 1
-
-class ForTestSubclassConstructor(wrapper.Wrapper):
- def __init__(self, *args, **kwds):
- super(ForTestSubclassConstructor, self).__init__('foo', **kwds)
-
-class ForTestSetattr(object):
- def __setattr__(self, name, value):
- self.__dict__['value_called'] = self, name, value
-
-class ForTestSetattrContextAware(ForTestSetattr, ContextAware):
- pass
-
-class ForTestSetattrContextMethod(ForTestSetattr):
- __setattr__ = ContextMethod(ForTestSetattr.__setattr__)
-
-
class WrapperTestCase(ProxyTestCase):
def new_proxy(self, o, c=None):
return wrapper.Wrapper(o, c)
@@ -116,13 +33,18 @@
self.assertEquals(wrapper.getobject(w), (o1, o2))
self.assert_(wrapper.getcontext(w) is o3)
+
def test_subclass_constructor(self):
- w = ForTestSubclassConstructor(1, 2, key='value')
+ class MyWrapper(wrapper.Wrapper):
+ def __init__(self, *args, **kwds):
+ super(MyWrapper, self).__init__('foo', **kwds)
+
+ w = MyWrapper(1, 2, key='value')
self.assertEquals(wrapper.getobject(w), 'foo')
self.assertEquals(wrapper.getdict(w), {'key': 'value'})
# __new__ catches too many positional args:
- self.assertRaises(TypeError, ForTestSubclassConstructor, 1, 2, 3)
+ self.assertRaises(TypeError, MyWrapper, 1, 2, 3)
def test_wrapper_basics(self):
o1 = 1
@@ -149,10 +71,15 @@
self.assert_(wrapper.getcontext(w) is c)
def test_wrapper_subclass_attributes(self):
+ class MyWrapper(wrapper.Wrapper):
+ def __init__(self, ob):
+ super(MyWrapper, self).__init__(ob)
+ self.foo = 1
+
o = Thing()
o.foo = 'not 1'
o.bar = 2
- w = ForTestWrapperSubclassAttributes(o)
+ w = MyWrapper(o)
self.assert_(w.foo == 1)
self.assert_(w.bar == 2)
@@ -166,32 +93,38 @@
else:
return fixed_retval
- if slot in class_lookup:
- # The unit test for this slot has been run before.
- # Return the types we previously made.
- return class_lookup[slot]
- else:
- # This is the first time the unit-test for this slot has been
- # run. Make some new types, and cache them.
-
- # context-unaware object
- t1 = type('ContextUnawareObj', (), {slot: doit})
- proxy1 = self.new_proxy(t1(), context)
-
- # context-aware object
- t2 = type('ContextAwareObj', (ContextAware,), {slot: doit})
- proxy2 = self.new_proxy(t2(), context)
-
- # object with context method
- t3 = type('ContextMethodObj', (), {slot: ContextMethod(doit)})
- proxy3 = self.new_proxy(t3(), context)
+ # context-unaware object
+ t1 = type('ContextUnawareObj', (), {slot: doit})
+ proxy1 = self.new_proxy(t1(), context)
+
+ # context-aware object
+ t2 = type('ContextAwareObj', (ContextAware,), {slot: doit})
+ proxy2 = self.new_proxy(t2(), context)
+
+ # object with context method
+ t3 = type('ContextMethodObj', (), {slot: ContextMethod(doit)})
+ proxy3 = self.new_proxy(t3(), context)
- class_lookup[slot] = retval = proxy1, proxy2, proxy3, context
- return retval
+ return proxy1, proxy2, proxy3, context
def test_normal_getattr(self):
+ class X(object):
+ def __init__(self, retval):
+ self.args = None
+ self.retval = retval
+ def __getattr__(self, name):
+ if name == '__del__':
+ # We don't want Python's gc to think that we have a
+ # __del__, otherwise cycles will not be collected.
+ raise AttributeError, name
+ self.__dict__['args'] = self, name
+ return self.__dict__['retval']
+ def getArgs(self):
+ return self.__dict__['args']
+
context = object()
- x = ForTestNormalGetattr(23)
+
+ x = X(23)
p = self.new_proxy(x, context)
self.assertEquals(p.foo, 23)
# Nothing special happens; we don't rebind the self of __getattr__
@@ -199,7 +132,24 @@
self.assert_(p.getArgs()[0] is x)
def test_ContextAware_getattr(self):
- y = ForTestContextAwareGetattr(23)
+ class Y(ContextAware):
+ def __init__(self, retval):
+ self.args = None
+ self.retval = retval
+ def __getattr__(self, name):
+ if name == '__del__':
+ # We don't want Python's gc to think that we have a
+ # __del__, otherwise cycles will not be collected.
+ raise AttributeError, name
+ self.args = self, name
+ return self.__dict__['retval']
+ def getArgs(self):
+ # Need to get __dict__ from the clean object, because it
+ # is a special descriptor and complains bitterly about
+ # being got from the wrong kind of object.
+ return getobject(self).__dict__['args']
+
+ y = Y(23)
p = self.new_proxy(y, 23)
self.assertEquals(p.foo, 23)
# Nothing special happens; we don't rebind the self of __getattr__
@@ -207,18 +157,36 @@
self.assert_(p.getArgs()[0] is y)
def test_ContextMethod_getattr(self):
- z = ForTestContextMethodGetattr()
+ class Z(object):
+ def __getattr__(self, name):
+ return 23
+ __getattr__ = ContextMethod(__getattr__)
+
+ z = Z()
self.assertRaises(TypeError, getattr, z, 'foo')
p = self.new_proxy(z, 23)
self.assertRaises(TypeError, getattr, p, 'foo')
# This is the same behaviour that you get if you try to make
# __getattr__ a classmethod.
- zz = ForTestContextMethodGetattr2()
+ class ZZ(object):
+ def __getattr__(self, name):
+ return 23
+ __getattr__ = classmethod(__getattr__)
+
+ zz = ZZ()
self.assertRaises(TypeError, getattr, zz, 'foo')
def test_property(self):
- x = ForTestProperty()
+ class X(object):
+ def getFoo(self):
+ self.called_with = self
+ return 42
+ def setFoo(self, value):
+ self.called_with = self, value
+ foo = property(getFoo, setFoo)
+ context_foo = ContextProperty(getFoo, setFoo)
+ x = X()
p = self.new_proxy(x)
self.assertEquals(p.foo, 42)
self.assert_(x.called_with is x)
@@ -232,7 +200,14 @@
self.assert_(x.called_with[0] is p)
def test_ContextAware_property(self):
- y = ForContextAwareProperty()
+ class Y(ContextAware):
+ def getFoo(self):
+ self.called_with = self
+ return 42
+ def setFoo(self, value):
+ self.called_with = self, value
+ foo = property(getFoo, setFoo)
+ y = Y()
p = self.new_proxy(y)
self.assertEquals(p.foo, 42)
self.assert_(y.called_with is p)
@@ -241,21 +216,26 @@
self.assert_(y.called_with[0] is p)
def test_setattr(self):
- value_called = [None]
+ class X(object):
+ def __setattr__(self, name, value):
+ self.__dict__['value_called'] = self, name, value
- x = ForTestSetattr()
+ x = X()
p = self.new_proxy(x)
p.foo = 'bar'
self.assertEqual(x.value_called, (p, 'foo', 'bar'))
self.assert_(x.value_called[0] is x)
- cax = ForTestSetattrContextAware()
+ class ContextAwareX(X, ContextAware):
+ pass
+ cax = ContextAwareX()
p = self.new_proxy(cax)
p.foo = 'bar'
self.assertEqual(cax.value_called, (p, 'foo', 'bar'))
self.assert_(cax.value_called[0] is cax)
- x = ForTestSetattrContextMethod()
+ X.__setattr__ = ContextMethod(X.__setattr__)
+ x = X()
p = self.new_proxy(x)
p.foo = 'bar'
self.assertEqual(x.value_called, (p, 'foo', 'bar'))