Commit 454a74df authored by Hynek Schlawack's avatar Hynek Schlawack

#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.
parent efce00c5
...@@ -578,6 +578,10 @@ class _BaseNetwork(_IPAddressBase): ...@@ -578,6 +578,10 @@ class _BaseNetwork(_IPAddressBase):
def __repr__(self): def __repr__(self):
return '%s(%r)' % (self.__class__.__name__, str(self)) return '%s(%r)' % (self.__class__.__name__, str(self))
def __str__(self):
return '%s/%d' % (str(self.network_address),
self.prefixlen)
def hosts(self): def hosts(self):
"""Generate Iterator over usable hosts in a network. """Generate Iterator over usable hosts in a network.
...@@ -663,9 +667,6 @@ class _BaseNetwork(_IPAddressBase): ...@@ -663,9 +667,6 @@ class _BaseNetwork(_IPAddressBase):
return NotImplemented return NotImplemented
return not eq return not eq
def __str__(self):
return '%s/%s' % (self.ip, self._prefixlen)
def __hash__(self): def __hash__(self):
return hash(int(self.network_address) ^ int(self.netmask)) return hash(int(self.network_address) ^ int(self.netmask))
...@@ -708,15 +709,15 @@ class _BaseNetwork(_IPAddressBase): ...@@ -708,15 +709,15 @@ class _BaseNetwork(_IPAddressBase):
@property @property
def with_prefixlen(self): def with_prefixlen(self):
return '%s/%d' % (str(self.ip), self._prefixlen) return '%s/%d' % (str(self.network_address), self._prefixlen)
@property @property
def with_netmask(self): def with_netmask(self):
return '%s/%s' % (str(self.ip), str(self.netmask)) return '%s/%s' % (str(self.network_address), str(self.netmask))
@property @property
def with_hostmask(self): def with_hostmask(self):
return '%s/%s' % (str(self.ip), str(self.hostmask)) return '%s/%s' % (str(self.network_address), str(self.hostmask))
@property @property
def num_addresses(self): def num_addresses(self):
...@@ -1447,10 +1448,6 @@ class IPv4Network(_BaseV4, _BaseNetwork): ...@@ -1447,10 +1448,6 @@ class IPv4Network(_BaseV4, _BaseNetwork):
"""The binary representation of this address.""" """The binary representation of this address."""
return v4_int_to_packed(self.network_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): def _is_valid_netmask(self, netmask):
"""Verify that the netmask is valid. """Verify that the netmask is valid.
...@@ -1498,18 +1495,6 @@ class IPv4Network(_BaseV4, _BaseNetwork): ...@@ -1498,18 +1495,6 @@ class IPv4Network(_BaseV4, _BaseNetwork):
return True return True
return False 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: class _BaseV6:
...@@ -2108,10 +2093,6 @@ class IPv6Network(_BaseV6, _BaseNetwork): ...@@ -2108,10 +2093,6 @@ class IPv6Network(_BaseV6, _BaseNetwork):
if self._prefixlen == (self._max_prefixlen - 1): if self._prefixlen == (self._max_prefixlen - 1):
self.hosts = self.__iter__ self.hosts = self.__iter__
def __str__(self):
return '%s/%d' % (str(self.network_address),
self.prefixlen)
def _is_valid_netmask(self, prefixlen): def _is_valid_netmask(self, prefixlen):
"""Verify that the netmask/prefixlen is valid. """Verify that the netmask/prefixlen is valid.
...@@ -2128,15 +2109,3 @@ class IPv6Network(_BaseV6, _BaseNetwork): ...@@ -2128,15 +2109,3 @@ class IPv6Network(_BaseV6, _BaseNetwork):
except ValueError: except ValueError:
return False return False
return 0 <= prefixlen <= self._max_prefixlen 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))
...@@ -972,6 +972,22 @@ class IpaddrUnitTest(unittest.TestCase): ...@@ -972,6 +972,22 @@ class IpaddrUnitTest(unittest.TestCase):
self.assertTrue(self.ipv4_address in dummy) self.assertTrue(self.ipv4_address in dummy)
self.assertTrue(ip2 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): def testCopyConstructor(self):
addr1 = ipaddress.ip_network('10.1.1.0/24') addr1 = ipaddress.ip_network('10.1.1.0/24')
addr2 = ipaddress.ip_network(addr1) addr2 = ipaddress.ip_network(addr1)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment