[Checkins] SVN: zope.app.interface/trunk/ Handle hashing / equality / inequality properly

Tres Seaver tseaver at palladion.com
Thu Aug 11 15:21:25 EDT 2011


Log message for revision 122544:
  Handle hashing / equality / inequality properly
  
  o Accomodates changes to ``zope.interface`` which are inappropriate for
    persistent objects.
  

Changed:
  U   zope.app.interface/trunk/CHANGES.txt
  U   zope.app.interface/trunk/src/zope/app/interface/__init__.py
  U   zope.app.interface/trunk/src/zope/app/interface/tests/test_interface.py

-=-
Modified: zope.app.interface/trunk/CHANGES.txt
===================================================================
--- zope.app.interface/trunk/CHANGES.txt	2011-08-11 19:08:31 UTC (rev 122543)
+++ zope.app.interface/trunk/CHANGES.txt	2011-08-11 19:21:25 UTC (rev 122544)
@@ -6,7 +6,8 @@
 3.6.1 (unreleased)
 ------------------
 
-- Nothing changed yet.
+- Handle hashing / equality / inequality properly given changes to
+  ``zope.interface`` which are inappropriate for persistent objects.
 
 
 3.6.0 (2010-09-20)

Modified: zope.app.interface/trunk/src/zope/app/interface/__init__.py
===================================================================
--- zope.app.interface/trunk/src/zope/app/interface/__init__.py	2011-08-11 19:08:31 UTC (rev 122543)
+++ zope.app.interface/trunk/src/zope/app/interface/__init__.py	2011-08-11 19:21:25 UTC (rev 122544)
@@ -38,6 +38,27 @@
 
         self.dependents = FlexibleWeakKeyDictionary()
 
+    def __hash__(self):
+        # Override the version in InterfaceClass to cope with the fact that
+        # we don't have __module__
+        return hash((self._p_jar, self._p_oid))
+
+    def __eq__(self, other):
+        # Override the version in InterfaceClass to cope with the fact that
+        # we don't have __module__
+        if self._p_oid is None:
+            return other is self
+        return (self._p_jar is getattr(other, '_p_jar', None) and
+                self._p_oid == getattr(other, '_p_oid', None))
+
+    def __ne__(self, other):
+        # Override the version in InterfaceClass to cope with the fact that
+        # we don't have __module__
+        if self._p_oid is None:
+            return other is not self
+        return (self._p_jar is not getattr(other, '_p_jar', None) or
+                self._p_oid != getattr(other, '_p_oid', None))
+
 # PersistentInterface is equivalent to the zope.interface.Interface object
 # except that it is also persistent.  It is used in conjunction with
 # zodb.code to support interfaces in persistent modules.

Modified: zope.app.interface/trunk/src/zope/app/interface/tests/test_interface.py
===================================================================
--- zope.app.interface/trunk/src/zope/app/interface/tests/test_interface.py	2011-08-11 19:08:31 UTC (rev 122543)
+++ zope.app.interface/trunk/src/zope/app/interface/tests/test_interface.py	2011-08-11 19:21:25 UTC (rev 122544)
@@ -42,6 +42,9 @@
 class IFoo(Interface):
     pass
 
+class ISpam(Interface):
+    pass
+
 # This must be a classobj
 class Foo:
     __implemented__ = IFoo
@@ -119,6 +122,54 @@
         # the conversion should not affect Interface
         self.assert_(imodule.Interface is Interface)
 
+    def test___hash___no_jar(self):
+        class IFoo(PersistentInterface):
+            pass
+        self.assertEqual(hash(IFoo), hash((None, None)))
+
+    def test___hash___w_jar(self):
+        self.registry.newModule("imodule", code)
+        transaction.commit()
+        imodule = self.registry.findModule("imodule")
+        self.assertEqual(hash(imodule.IFoo),
+                         hash((self.conn, imodule.IFoo._p_oid)))
+
+    def test___eq___no_jar(self):
+        class IFoo(PersistentInterface):
+            pass
+        class IBar(PersistentInterface):
+            pass
+        self.failUnless(IFoo == IFoo)
+        self.failIf(IFoo == IBar)
+
+    def test___eq___w_jar(self):
+        class IFoo(PersistentInterface):
+            pass
+        self.registry.newModule("imodule", code)
+        transaction.commit()
+        imodule = self.registry.findModule("imodule")
+        self.failUnless(imodule.IFoo == imodule.IFoo) # Don't use assertEqual
+        self.failIf(imodule.IFoo == imodule.ISpam)
+        self.failIf(imodule.IFoo == IFoo)
+
+    def test___ne___no_jar(self):
+        class IFoo(PersistentInterface):
+            pass
+        class IBar(PersistentInterface):
+            pass
+        self.failIf(IFoo != IFoo)
+        self.failUnless(IFoo != IBar)
+
+    def test___ne___w_jar(self):
+        class IFoo(PersistentInterface):
+            pass
+        self.registry.newModule("imodule", code)
+        transaction.commit()
+        imodule = self.registry.findModule("imodule")
+        self.failIf(imodule.IFoo != imodule.IFoo) # Don't use assertNotEqual
+        self.failUnless(imodule.IFoo != imodule.ISpam)
+        self.failUnless(imodule.IFoo != IFoo)
+
     def test_provides(self):
         """Provides are persistent."""
 



More information about the checkins mailing list