summaryrefslogtreecommitdiff
path: root/src/zope/interface/tests
diff options
context:
space:
mode:
authorTres Seaver <tseaver@palladion.com>2011-08-11 19:49:19 +0000
committerTres Seaver <tseaver@palladion.com>2011-08-11 19:49:19 +0000
commit05a899a8275bbd6b2e332a4fbfeab4d7e1dc40f8 (patch)
tree003d20205db43dbe9d15a19b291cde66e2d65360 /src/zope/interface/tests
parent0f7eb19312fa4cc67dab35b539a83fd89609b428 (diff)
downloadzope-interface-05a899a8275bbd6b2e332a4fbfeab4d7e1dc40f8.tar.gz
Work around buggy behavior in some subclasses of `InterfaceClass``
Some sublcasses invoke '__hash__' before initializing '__module__' and '__name__'. The workaround returns a fixed constant hash in such cases, and issues a UserWarning. Addresses LP #811792.
Diffstat (limited to 'src/zope/interface/tests')
-rw-r--r--src/zope/interface/tests/ifoo_other.py26
-rw-r--r--src/zope/interface/tests/test_interface.py37
2 files changed, 55 insertions, 8 deletions
diff --git a/src/zope/interface/tests/ifoo_other.py b/src/zope/interface/tests/ifoo_other.py
new file mode 100644
index 0000000..29a7877
--- /dev/null
+++ b/src/zope/interface/tests/ifoo_other.py
@@ -0,0 +1,26 @@
+##############################################################################
+#
+# Copyright (c) 2001, 2002 Zope Foundation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""IFoo test module
+"""
+from zope.interface import Interface
+
+class IFoo(Interface):
+ """
+ Dummy interface for unit tests.
+ """
+
+ def bar(baz):
+ """
+ Just a note.
+ """
diff --git a/src/zope/interface/tests/test_interface.py b/src/zope/interface/tests/test_interface.py
index 034c8ec..2b8c8ce 100644
--- a/src/zope/interface/tests/test_interface.py
+++ b/src/zope/interface/tests/test_interface.py
@@ -387,14 +387,35 @@ class InterfaceTests(unittest.TestCase):
self.failUnless(IEmpty >= IEmpty)
self.failIf(IEmpty > IEmpty)
- def test_hash(self):
- from zope.interface import Interface
-
- class IEmpty(Interface):
- pass
-
- self.assertEqual(hash(IEmpty),
- hash((IEmpty.__name__, IEmpty.__module__)))
+ def test_comparison_with_same_named_instance_in_other_module(self):
+ from zope.interface.tests.ifoo import IFoo as IFoo1
+ from zope.interface.tests.ifoo_other import IFoo as IFoo2
+
+ self.failUnless(IFoo1 < IFoo2)
+ self.failUnless(IFoo1 <= IFoo2)
+ self.failIf(IFoo1 == IFoo2)
+ self.failUnless(IFoo1 != IFoo2)
+ self.failIf(IFoo1 >= IFoo2)
+ self.failIf(IFoo1 > IFoo2)
+
+ def test_hash_normal(self):
+ from zope.interface.tests.ifoo import IFoo
+ self.assertEqual(hash(IFoo),
+ hash((('IFoo', 'zope.interface.tests.ifoo'))))
+
+ def test_hash_missing_required_attrs(self):
+ from warnings import catch_warnings
+ from zope.interface.interface import InterfaceClass
+ class Derived(InterfaceClass):
+ def __init__(self):
+ pass # Don't call base class.
+ derived = Derived()
+ with catch_warnings(record=True) as warned:
+ self.assertEqual(hash(derived), 1)
+ self.assertEqual(len(warned), 1)
+ self.failUnless(warned[0].category is UserWarning)
+ self.assertEqual(str(warned[0].message),
+ 'Hashing uninitialized InterfaceClass instance')
if sys.version_info >= (2, 4):