[Zope3-checkins] SVN: Zope3/branches/3.3/src/zope/app/interface/ Backport changes from trunk to pass tests on Mac.

Ross Patterson me at rpatterson.net
Thu Mar 8 13:36:13 EST 2007


Log message for revision 73069:
  Backport changes from trunk to pass tests on Mac.

Changed:
  U   Zope3/branches/3.3/src/zope/app/interface/__init__.py
  U   Zope3/branches/3.3/src/zope/app/interface/tests/test_interface.py
  A   Zope3/branches/3.3/src/zope/app/interface/wref.py

-=-
Modified: Zope3/branches/3.3/src/zope/app/interface/__init__.py
===================================================================
--- Zope3/branches/3.3/src/zope/app/interface/__init__.py	2007-03-08 15:52:07 UTC (rev 73068)
+++ Zope3/branches/3.3/src/zope/app/interface/__init__.py	2007-03-08 18:36:12 UTC (rev 73069)
@@ -21,92 +21,21 @@
 
 
 from persistent import Persistent
-from persistent.wref import PersistentWeakKeyDictionary
-from zodbcode.patch import registerWrapper, Wrapper
+from zodbcode.patch import registerWrapper, Wrapper, NameFinder
 
 from zope.interface.interface import InterfaceClass
 from zope.interface import Interface
 from zope.security.proxy import removeSecurityProxy
 
-persistentFactories = {}
-def getPersistentKey(v_key):
-    try:
-        reduce = v_key.__reduce__()
-    except AttributeError:
-        return
-    except TypeError:
-        return
-        
-    lookups = reduce[0], type(v_key), getattr(v_key, '__class__')
-    for lookup in lookups:
-        p_factory = persistentFactories.get(lookup, None)
-        if p_factory is not None:
-            return p_factory(v_key)
+from wref import FlexibleWeakKeyDictionary
 
-class DependentsDict(PersistentWeakKeyDictionary):
-    """Intercept non-persistent keys and swap in persistent
-    equivalents."""
-
-    def __setstate__(self, state):
-        data = state['data']
-        for v_key, value in data:
-            p_key = getPersistentKey(v_key)
-            if p_key is not None:
-                data[p_key] = data[v_key]
-        state['data'] = data
-        return super(DependentsDict, self).__setstate__(state)
-                
-    def __setitem__(self, key, value):
-        p_key = getPersistentKey(key)
-        if p_key is not None:
-            key = p_key
-        return super(DependentsDict, self).__setitem__(key, value)
-
-    def __len__(self): return len(self.data)
-
-    def get(self, key, default=None):
-        if not hasattr(key, '_p_oid') or not hasattr(key, '_p_jar'):
-            return default
-        return super(DependentsDict, self).get(key, default)
-
-    def update(self, adict):
-        for v_key in adict.keys():
-            p_key = getPersistentKey(v_key)
-            if p_key is not None:
-                adict[p_key] = adict[v_key]
-        return super(DependentsDict, self).update(adict)
-
-    def keys(self): return [k() for k in self.data.keys()]
-
-from zope.interface.declarations import ProvidesClass, Provides
-class PersistentProvidesClass(Persistent, ProvidesClass):
-    """A persistent Provides class."""
-    def __init__(self, *args, **kw):
-        Persistent.__init__(self)
-        ProvidesClass.__init__(self, *args, **kw)
-        self.dependents = DependentsDict()
-def persistentProvides(obj):
-    return PersistentProvidesClass(*obj.__reduce__()[1])
-persistentFactories[Provides] = persistentProvides
-
-from zope.interface.declarations import Implements
-class PersistentImplements(Persistent, Implements):
-    """A persistent Implements class."""
-    def __init__(self, *args, **kw):
-        Persistent.__init__(self)
-        Implements.__init__(self, *args, **kw)
-        self.dependents = DependentsDict()
-def persistentImplements(obj):
-    return PersistentImplements(*obj.__bases__)
-persistentFactories[Implements] = persistentImplements
-
 class PersistentInterfaceClass(Persistent, InterfaceClass):
 
     def __init__(self, *args, **kw):
         Persistent.__init__(self)
         InterfaceClass.__init__(self, *args, **kw)
         
-        self.dependents = DependentsDict()
+        self.dependents = FlexibleWeakKeyDictionary()
 
 # PersistentInterface is equivalent to the zope.interface.Interface object
 # except that it is also persistent.  It is used in conjunction with
@@ -115,10 +44,6 @@
                                                (Interface, ))
 
 
-def persistentInterface(iface):
-    return PersistentInterfaceClass(iface.__name__)
-persistentFactories[InterfaceClass] = persistentInterface
-
 class PersistentInterfaceWrapper(Wrapper):
 
     def unwrap(self):
@@ -126,19 +51,11 @@
 
 
 def getInterfaceStateForPersistentInterfaceCreation(iface):
+    # Need to convert the dependents weakref dict to a persistent dict
     dict = iface.__dict__.copy()
-
-    deps = dict['dependents']
-    dependents = DependentsDict()
-    for k, v in deps.iteritems():
+    dependents = FlexibleWeakKeyDictionary()
+    for k, v in dict['dependents'].iteritems():
         dependents[k] = v
-    del dict['dependents']
-
-    for key, value in dict.iteritems():
-        p_value = getPersistentKey(value)
-        if p_value is not None:
-            dict[key] = p_value
-    
     dict['dependents'] = dependents
     return dict
 
@@ -147,6 +64,11 @@
                 getInterfaceStateForPersistentInterfaceCreation,
                 )
 
+NameFinder.classTypes[InterfaceClass] = True
+NameFinder.types[InterfaceClass] = True
+NameFinder.classTypes[PersistentInterfaceClass] = True
+NameFinder.types[PersistentInterfaceClass] = True
+
 from zope.interface.declarations import providedBy
 
 def queryType(object, interface):

Modified: Zope3/branches/3.3/src/zope/app/interface/tests/test_interface.py
===================================================================
--- Zope3/branches/3.3/src/zope/app/interface/tests/test_interface.py	2007-03-08 15:52:07 UTC (rev 73068)
+++ Zope3/branches/3.3/src/zope/app/interface/tests/test_interface.py	2007-03-08 18:36:12 UTC (rev 73069)
@@ -30,7 +30,6 @@
 
 from zope.interface import Interface, implements, directlyProvides
 from zope.interface.interfaces import IInterface
-from zope.component.interface import provideInterface
 from zope.app.interface import PersistentInterface
 
 # TODO: for some reason changing this code to use implements() does not
@@ -58,15 +57,26 @@
 class IBah(IQuux): pass
 class IBaz(Interface): pass
 class IBlah(IBaz): pass
+
 """
 
+provide_iface_code = """\
+from zope.interface import Interface
+from zope.component.interface import provideInterface
+from zope.app.interface.tests.test_interface import IBarInterface
+
+class IBar(Interface): pass
+provideInterface('', IBar, iface_type=IBarInterface)
+
+"""
+
+class IBarInterface(IInterface): pass
+
 class Bar(Persistent): pass
 class Baz(Persistent): pass
 
 class IQux(Interface): pass
 
-class IBarInterface(IInterface): pass
-
 class PersistentInterfaceTest(unittest.TestCase):
 
     def setUp(self):
@@ -124,9 +134,14 @@
         self.root['blah'] = blah
         self.assertTrue(barmodule.IBlah.providedBy(blah))
 
+        # Update the code to make sure everything works on update
+        self.registry.updateModule('barmodule',
+                                   bar_code + '\nfoo = 1')
+
         transaction.commit()
         self.db.close()
         root = self.db.open().root()
+
         barmodule = root['registry'].findModule("barmodule")
 
         bar = root['bar']
@@ -162,30 +177,18 @@
         barmodule = root['registry'].findModule("barmodule")
         self.assertEqual(barmodule.IBar.dependents.keys(), [])
 
-    def test_persistentDeclarations(self):
-        """Verify equivalency of persistent declarations
+    def test_persistentProvides(self):
+        """Verify that provideInterface works."""
 
-        Make sure that the persistent declaration instance are
-        equivalent to the non-persistent instances they originate
-        from."""
+        self.registry.newModule("barmodule", provide_iface_code)
+        barmodule = self.registry.findModule("barmodule")
+        self.assertTrue(IBarInterface.providedBy(barmodule.IBar))
 
-        self.registry.newModule("barmodule", bar_code)
+        self.registry.updateModule('barmodule',
+                                   provide_iface_code + '\nfoo = 1')
+        transaction.commit()
         barmodule = self.registry.findModule("barmodule")
-
-        class Baz(object):
-            implements(barmodule.IBar)
-
-        bar = Bar()
-        directlyProvides(bar, barmodule.IBar)
-
-        self.assertEqual(
-            bar.__provides__._Provides__args,
-            barmodule.IBar.dependents.keys()[0]._Provides__args
-            )
-        self.assertEqual(
-            Baz.__implemented__.__bases__,
-            barmodule.IBar.dependents.keys()[1].__bases__
-            )
+        self.assertTrue(IBarInterface.providedBy(barmodule.IBar))
         
 def test_suite():
     return unittest.makeSuite(PersistentInterfaceTest)

Copied: Zope3/branches/3.3/src/zope/app/interface/wref.py (from rev 73068, Zope3/trunk/src/zope/app/interface/wref.py)



More information about the Zope3-Checkins mailing list