cpython: 5abea8a43f19 (original) (raw)
Mercurial > cpython
changeset 78426:5abea8a43f19
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. [#15559]
Nick Coghlan ncoghlan@gmail.com | |
---|---|
date | Sun, 05 Aug 2012 18:20:17 +1000 |
parents | 5284e65e865b |
children | 7c81caf2dd5a |
files | Lib/ipaddress.py Lib/test/test_ipaddress.py |
diffstat | 2 files changed, 9 insertions(+), 14 deletions(-)[+] [-] Lib/ipaddress.py 6 Lib/test/test_ipaddress.py 17 |
line wrap: on
line diff
--- 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 int(self): return self._ip @@ -571,9 +568,6 @@ class _BaseNetwork(_IPAddressBase): def init(self, address): self._cache = {}
- def int(self): return int(self.network_address)
--- 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[](#l2.16)
# bytes constructor. Thus, we disallow implicit use as an integer[](#l2.17)
self.assertRaises(TypeError, operator.index, self.factory(1))[](#l2.18)
self.assertRaises(TypeError, hex, self.factory(1))[](#l2.19)
self.assertRaises(TypeError, bytes, self.factory(1))[](#l2.20)
+ + 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))[](#l2.30)
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),[](#l2.39)
hex(self.ipv4_address))[](#l2.40)
self.assertEqual(hex(0x20010658022ACAFE0200000000000001),[](#l2.42)
hex(self.ipv6_address))[](#l2.43)
- def testForceVersion(self): self.assertEqual(ipaddress.ip_network(1).version, 4) self.assertEqual(ipaddress.IPv6Network(1).version, 6)