From 454a74df23f2c39e34250361184dbff68b0d5b08 Mon Sep 17 00:00:00 2001 From: Hynek Schlawack Date: Mon, 4 Jun 2012 18:14:02 +0200 Subject: #14814: Remove dead code from ipaddress _BaseNetwork contained (faulty) methods for creating string representations. I've fixed them and put them to use by eliminating identical overrides. --- Lib/ipaddress.py | 45 +++++++-------------------------------------- Lib/test/test_ipaddress.py | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 38 deletions(-) diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py index 25bcccd..bf4e01e 100644 --- a/Lib/ipaddress.py +++ b/Lib/ipaddress.py @@ -578,6 +578,10 @@ class _BaseNetwork(_IPAddressBase): def __repr__(self): return '%s(%r)' % (self.__class__.__name__, str(self)) + def __str__(self): + return '%s/%d' % (str(self.network_address), + self.prefixlen) + def hosts(self): """Generate Iterator over usable hosts in a network. @@ -663,9 +667,6 @@ class _BaseNetwork(_IPAddressBase): return NotImplemented return not eq - def __str__(self): - return '%s/%s' % (self.ip, self._prefixlen) - def __hash__(self): return hash(int(self.network_address) ^ int(self.netmask)) @@ -708,15 +709,15 @@ class _BaseNetwork(_IPAddressBase): @property def with_prefixlen(self): - return '%s/%d' % (str(self.ip), self._prefixlen) + return '%s/%d' % (str(self.network_address), self._prefixlen) @property def with_netmask(self): - return '%s/%s' % (str(self.ip), str(self.netmask)) + return '%s/%s' % (str(self.network_address), str(self.netmask)) @property def with_hostmask(self): - return '%s/%s' % (str(self.ip), str(self.hostmask)) + return '%s/%s' % (str(self.network_address), str(self.hostmask)) @property def num_addresses(self): @@ -1447,10 +1448,6 @@ class IPv4Network(_BaseV4, _BaseNetwork): """The binary representation of this address.""" return v4_int_to_packed(self.network_address) - def __str__(self): - return '%s/%d' % (str(self.network_address), - self.prefixlen) - def _is_valid_netmask(self, netmask): """Verify that the netmask is valid. @@ -1498,18 +1495,6 @@ class IPv4Network(_BaseV4, _BaseNetwork): return True return False - @property - def with_prefixlen(self): - return '%s/%d' % (str(self.network_address), self._prefixlen) - - @property - def with_netmask(self): - return '%s/%s' % (str(self.network_address), str(self.netmask)) - - @property - def with_hostmask(self): - return '%s/%s' % (str(self.network_address), str(self.hostmask)) - class _BaseV6: @@ -2108,10 +2093,6 @@ class IPv6Network(_BaseV6, _BaseNetwork): if self._prefixlen == (self._max_prefixlen - 1): self.hosts = self.__iter__ - def __str__(self): - return '%s/%d' % (str(self.network_address), - self.prefixlen) - def _is_valid_netmask(self, prefixlen): """Verify that the netmask/prefixlen is valid. @@ -2128,15 +2109,3 @@ class IPv6Network(_BaseV6, _BaseNetwork): except ValueError: return False return 0 <= prefixlen <= self._max_prefixlen - - @property - def with_prefixlen(self): - return '%s/%d' % (str(self.network_address), self._prefixlen) - - @property - def with_netmask(self): - return '%s/%s' % (str(self.network_address), str(self.netmask)) - - @property - def with_hostmask(self): - return '%s/%s' % (str(self.network_address), str(self.hostmask)) diff --git a/Lib/test/test_ipaddress.py b/Lib/test/test_ipaddress.py index 5b7d013..17c32f2 100644 --- a/Lib/test/test_ipaddress.py +++ b/Lib/test/test_ipaddress.py @@ -972,6 +972,22 @@ class IpaddrUnitTest(unittest.TestCase): self.assertTrue(self.ipv4_address in dummy) self.assertTrue(ip2 in dummy) + def testIPv6NetworkHelpers(self): + net = self.ipv6_network + self.assertEqual('2001:658:22a:cafe::/64', net.with_prefixlen) + self.assertEqual('2001:658:22a:cafe::/ffff:ffff:ffff:ffff::', + net.with_netmask) + self.assertEqual('2001:658:22a:cafe::/::ffff:ffff:ffff:ffff', + net.with_hostmask) + self.assertEqual('2001:658:22a:cafe::/64', str(net)) + + def testIPv4NetworkHelpers(self): + net = self.ipv4_network + self.assertEqual('1.2.3.0/24', net.with_prefixlen) + self.assertEqual('1.2.3.0/255.255.255.0', net.with_netmask) + self.assertEqual('1.2.3.0/0.0.0.255', net.with_hostmask) + self.assertEqual('1.2.3.0/24', str(net)) + def testCopyConstructor(self): addr1 = ipaddress.ip_network('10.1.1.0/24') addr2 = ipaddress.ip_network(addr1) -- cgit v0.12