diff options
author | Nick Coghlan <ncoghlan@gmail.com> | 2012-08-05 08:20:17 (GMT) |
---|---|---|
committer | Nick Coghlan <ncoghlan@gmail.com> | 2012-08-05 08:20:17 (GMT) |
commit | e0c3f5edc0f20cc28363258df501758c1bdb1ca7 (patch) | |
tree | 641a52e5a0df70f91db0d7057e6680d1f286f662 | |
parent | 37d3ff14871a25429fb93167aeace0589be45426 (diff) | |
download | cpython-e0c3f5edc0f20cc28363258df501758c1bdb1ca7.zip cpython-e0c3f5edc0f20cc28363258df501758c1bdb1ca7.tar.gz cpython-e0c3f5edc0f20cc28363258df501758c1bdb1ca7.tar.bz2 |
Close #15559: Implementing __index__ creates a nasty interaction with the bytes constructor. At least for 3.3, ipaddress objects must now be explicitly converted with int() and thus can't be passed directly to the hex() builtin.
-rw-r--r-- | Lib/ipaddress.py | 6 | ||||
-rw-r--r-- | Lib/test/test_ipaddress.py | 17 |
2 files changed, 9 insertions, 14 deletions
diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py index c6eea7f..612236e 100644 --- a/Lib/ipaddress.py +++ b/Lib/ipaddress.py @@ -511,9 +511,6 @@ class _BaseAddress(_IPAddressBase): and '/' in str(address)): raise AddressValueError("Unexpected '/' in %r" % address) - def __index__(self): - return self._ip - def __int__(self): return self._ip @@ -571,9 +568,6 @@ class _BaseNetwork(_IPAddressBase): def __init__(self, address): self._cache = {} - def __index__(self): - return int(self.network_address) ^ self.prefixlen - def __int__(self): return int(self.network_address) diff --git a/Lib/test/test_ipaddress.py b/Lib/test/test_ipaddress.py index 619fa44..a44fa7f 100644 --- a/Lib/test/test_ipaddress.py +++ b/Lib/test/test_ipaddress.py @@ -7,6 +7,7 @@ import unittest import re import contextlib +import operator import ipaddress class BaseTestCase(unittest.TestCase): @@ -72,6 +73,14 @@ class CommonTestMixin: with self.assertAddressError(re.escape(repr("1.0"))): self.factory(1.0) + def test_not_an_index_issue15559(self): + # Implementing __index__ makes for a very nasty interaction with the + # bytes constructor. Thus, we disallow implicit use as an integer + self.assertRaises(TypeError, operator.index, self.factory(1)) + self.assertRaises(TypeError, hex, self.factory(1)) + self.assertRaises(TypeError, bytes, self.factory(1)) + + class CommonTestMixin_v4(CommonTestMixin): def test_leading_zeros(self): @@ -599,7 +608,6 @@ class IpaddrUnitTest(unittest.TestCase): self.assertEqual(first, last) 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)) def testMissingAddressVersion(self): class Broken(ipaddress._BaseAddress): @@ -1545,13 +1553,6 @@ class IpaddrUnitTest(unittest.TestCase): self.assertEqual(42540616829182469433547762482097946625, int(self.ipv6_address)) - def testHexRepresentation(self): - self.assertEqual(hex(0x1020304), - hex(self.ipv4_address)) - - self.assertEqual(hex(0x20010658022ACAFE0200000000000001), - hex(self.ipv6_address)) - def testForceVersion(self): self.assertEqual(ipaddress.ip_network(1).version, 4) self.assertEqual(ipaddress.IPv6Network(1).version, 6) |