[Checkins] SVN: zope.interface/branches/tseaver-no_2to3/src/zope/interface/tests/test_declarations.py The C impl. caches BuiltinImmplentationSpecifications too early.

Tres Seaver cvs-admin at zope.org
Thu Apr 5 19:04:07 UTC 2012


Log message for revision 124972:
  The C impl. caches BuiltinImmplentationSpecifications too early.
  
  So, we need to monkey-patch its contents, rather than stuff a new dict into
  the module.

Changed:
  U   zope.interface/branches/tseaver-no_2to3/src/zope/interface/tests/test_declarations.py

-=-
Modified: zope.interface/branches/tseaver-no_2to3/src/zope/interface/tests/test_declarations.py
===================================================================
--- zope.interface/branches/tseaver-no_2to3/src/zope/interface/tests/test_declarations.py	2012-04-05 16:02:48 UTC (rev 124971)
+++ zope.interface/branches/tseaver-no_2to3/src/zope/interface/tests/test_declarations.py	2012-04-05 19:04:03 UTC (rev 124972)
@@ -269,8 +269,9 @@
         foo = Foo()
         foo.__implemented__ = None
         reg = object()
-        specs = {foo: reg}
-        with _Monkey(declarations, BuiltinImplementationSpecifications=specs):
+        with _MonkeyDict(declarations,
+                         'BuiltinImplementationSpecifications') as specs:
+            specs[foo] = reg
             self.failUnless(self._callFUT(foo) is reg)
 
     def test_dictless_w_existing_Implements(self):
@@ -302,23 +303,26 @@
         from zope.interface import declarations
         from zope.interface.declarations import Implements
         from zope.interface._compat import _BUILTINS
-        specs = {}
-        with _Monkey(declarations, BuiltinImplementationSpecifications=specs):
+        with _MonkeyDict(declarations,
+                         'BuiltinImplementationSpecifications') as specs:
             self.assertEqual(list(self._callFUT(tuple)), [])
             self.assertEqual(list(self._callFUT(list)), [])
             self.assertEqual(list(self._callFUT(dict)), [])
-        for typ in (tuple, list, dict):
-            spec = specs[typ]
-            self.failUnless(isinstance(spec, Implements))
-            self.assertEqual(repr(spec),
-                             '<implementedBy %s.%s>'
-                                % (_BUILTINS, typ.__name__))
+            for typ in (tuple, list, dict):
+                spec = specs[typ]
+                self.failUnless(isinstance(spec, Implements))
+                self.assertEqual(repr(spec),
+                                '<implementedBy %s.%s>'
+                                    % (_BUILTINS, typ.__name__))
 
     def test_builtins_w_existing_cache(self):
         from zope.interface import declarations
         t_spec, l_spec, d_spec = object(), object(), object()
-        specs = {tuple: t_spec, list: l_spec, dict: d_spec}
-        with _Monkey(declarations, BuiltinImplementationSpecifications=specs):
+        with _MonkeyDict(declarations,
+                         'BuiltinImplementationSpecifications') as specs:
+            specs[tuple] = t_spec
+            specs[list] = l_spec
+            specs[dict] = d_spec
             self.failUnless(self._callFUT(tuple) is t_spec)
             self.failUnless(self._callFUT(list) is l_spec)
             self.failUnless(self._callFUT(dict) is d_spec)
@@ -1467,6 +1471,23 @@
             setattr(self.module, key, value)
 
 
+class _MonkeyDict(object):
+    # context-manager for restoring a dict w/in a module in the scope of a test.
+    def __init__(self, module, attrname, **kw):
+        self.module = module
+        self.target = getattr(module, attrname)
+        self.to_restore = self.target.copy()
+        self.target.clear()
+        self.target.update(kw)
+
+    def __enter__(self):
+        return self.target
+
+    def __exit__(self, exc_type, exc_val, exc_tb):
+        self.target.clear()
+        self.target.update(self.to_restore)
+
+
 def test_suite():
     return unittest.TestSuite((
         unittest.makeSuite(DeclarationTests),



More information about the checkins mailing list