summaryrefslogtreecommitdiffstats
path: root/Lib/ipaddress.py
diff options
context:
space:
mode:
authorCheryl Sabella <cheryl.sabella@gmail.com>2017-10-22 21:39:49 (GMT)
committerAntoine Pitrou <pitrou@free.fr>2017-10-22 21:39:49 (GMT)
commit91dc64ba3f51100540b2ab6c6cd72c3bb18a6d49 (patch)
tree011cf75c270c0a1ba07a023caa0c2f1a547e0d86 /Lib/ipaddress.py
parent04e36af9b88cd5e80fc818e51d46f07252a2ff3f (diff)
downloadcpython-91dc64ba3f51100540b2ab6c6cd72c3bb18a6d49.zip
cpython-91dc64ba3f51100540b2ab6c6cd72c3bb18a6d49.tar.gz
cpython-91dc64ba3f51100540b2ab6c6cd72c3bb18a6d49.tar.bz2
bpo-20825: Containment test for ip_network in ip_network.
Diffstat (limited to 'Lib/ipaddress.py')
-rw-r--r--Lib/ipaddress.py29
1 files changed, 23 insertions, 6 deletions
diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py
index 70746f8..e8ce4ce 100644
--- a/Lib/ipaddress.py
+++ b/Lib/ipaddress.py
@@ -776,8 +776,7 @@ class _BaseNetwork(_IPAddressBase):
if not isinstance(other, _BaseNetwork):
raise TypeError("%s is not a network object" % other)
- if not (other.network_address >= self.network_address and
- other.broadcast_address <= self.broadcast_address):
+ if not other.subnet_of(self):
raise ValueError('%s not contained in %s' % (other, self))
if other == self:
return
@@ -788,12 +787,10 @@ class _BaseNetwork(_IPAddressBase):
s1, s2 = self.subnets()
while s1 != other and s2 != other:
- if (other.network_address >= s1.network_address and
- other.broadcast_address <= s1.broadcast_address):
+ if other.subnet_of(s1):
yield s2
s1, s2 = s1.subnets()
- elif (other.network_address >= s2.network_address and
- other.broadcast_address <= s2.broadcast_address):
+ elif other.subnet_of(s2):
yield s1
s1, s2 = s2.subnets()
else:
@@ -975,6 +972,26 @@ class _BaseNetwork(_IPAddressBase):
return (self.network_address.is_multicast and
self.broadcast_address.is_multicast)
+ @staticmethod
+ def _is_subnet_of(a, b):
+ try:
+ # Always false if one is v4 and the other is v6.
+ if a._version != b._version:
+ raise TypeError(f"{a} and {b} are not of the same version")
+ return (b.network_address <= a.network_address and
+ b.broadcast_address >= a.broadcast_address)
+ except AttributeError:
+ raise TypeError(f"Unable to test subnet containment "
+ f"between {a} and {b}")
+
+ def subnet_of(self, other):
+ """Return True if this network is a subnet of other."""
+ return self._is_subnet_of(self, other)
+
+ def supernet_of(self, other):
+ """Return True if this network is a supernet of other."""
+ return self._is_subnet_of(other, self)
+
@property
def is_reserved(self):
"""Test if the address is otherwise IETF reserved.