]> granicus.if.org Git - python/commitdiff
Issue 14814: Correctly return NotImplemented from ipaddress._BaseNetwork.__eq__
authorNick Coghlan <ncoghlan@gmail.com>
Sat, 7 Jul 2012 13:05:59 +0000 (23:05 +1000)
committerNick Coghlan <ncoghlan@gmail.com>
Sat, 7 Jul 2012 13:05:59 +0000 (23:05 +1000)
Lib/ipaddress.py
Lib/test/test_ipaddress.py
Misc/NEWS

index e788c0a5b77cbc55dcc8b684d00dc08893d53bd0..b1e07fc992e5edecbcbe37428e04e9047cf6fccd 100644 (file)
@@ -651,12 +651,12 @@ class _BaseNetwork(_IPAddressBase):
         return not lt
 
     def __eq__(self, other):
-        if not isinstance(other, _BaseNetwork):
-            raise TypeError('%s and %s are not of the same type' % (
-                             self, other))
-        return (self._version == other._version and
-                self.network_address == other.network_address and
-                int(self.netmask) == int(other.netmask))
+        try:
+            return (self._version == other._version and
+                    self.network_address == other.network_address and
+                    int(self.netmask) == int(other.netmask))
+        except AttributeError:
+            return NotImplemented
 
     def __ne__(self, other):
         eq = self.__eq__(other)
index 2ac37e1c64b32df5dcde4cd17207c359c83cce4a..417c98677f00d45c7cf9d244b6dcd4fd01347fa4 100644 (file)
@@ -462,7 +462,6 @@ class IpaddrUnitTest(unittest.TestCase):
         self.assertEqual(128, ipaddress._count_righthand_zero_bits(0, 128))
         self.assertEqual("IPv4Network('1.2.3.0/24')", repr(self.ipv4_network))
         self.assertEqual('0x1020318', hex(self.ipv4_network))
-        self.assertRaises(TypeError, self.ipv4_network.__eq__, object())
 
     def testMissingAddressVersion(self):
         class Broken(ipaddress._BaseAddress):
@@ -496,6 +495,22 @@ class IpaddrUnitTest(unittest.TestCase):
         self.assertEqual(str(self.ipv6_network.hostmask),
                          '::ffff:ffff:ffff:ffff')
 
+    def testEqualityChecks(self):
+        # __eq__ should never raise TypeError directly
+        other = object()
+        def assertEqualityNotImplemented(instance):
+            self.assertEqual(instance.__eq__(other), NotImplemented)
+            self.assertEqual(instance.__ne__(other), NotImplemented)
+            self.assertFalse(instance == other)
+            self.assertTrue(instance != other)
+
+        assertEqualityNotImplemented(self.ipv4_address)
+        assertEqualityNotImplemented(self.ipv4_network)
+        assertEqualityNotImplemented(self.ipv4_interface)
+        assertEqualityNotImplemented(self.ipv6_address)
+        assertEqualityNotImplemented(self.ipv6_network)
+        assertEqualityNotImplemented(self.ipv6_interface)
+
     def testBadVersionComparison(self):
         # These should always raise TypeError
         v4addr = ipaddress.ip_address('1.1.1.1')
index d06ced8752024de1aba81595ac76d09d8532d31b..a109baf039fe9e707cbba424f02c6a9f8bc5a50a 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -23,6 +23,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #14814: ipaddress network objects correctly return NotImplemented
+  when compared to arbitrary objects instead of raising TypeError
+
 - Issue #14990: Correctly fail with SyntaxError on invalid encoding
   declaration.