Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Lib/ipaddress.py
Original file line number Diff line number Diff line change
Expand Up @@ -1033,8 +1033,9 @@ def _is_subnet_of(a, b):
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}")
raise TypeError(
f"Unable to test subnet containment between {a} and {b}: "
f"both must be network objects") from None

def subnet_of(self, other):
"""Return True if this network is a subnet of other."""
Expand Down
13 changes: 13 additions & 0 deletions Lib/test/test_ipaddress.py
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,19 @@ def test_subnet_of_mixed_types(self):
ipaddress.IPv6Network('::1/128').subnet_of(
ipaddress.IPv4Network('10.0.0.0/30'))

def test_subnet_of_non_network(self):
# Passing something that is not a network object (e.g. an address)
# must raise a clear TypeError rather than masking an internal
# AttributeError (gh-153769).
net = ipaddress.IPv4Network('10.0.0.0/30')
for other in (ipaddress.IPv4Address('10.0.0.1'), '10.0.0.0/30', 42):
for method in (net.subnet_of, net.supernet_of):
with self.assertRaises(TypeError) as cm:
method(other)
self.assertIn('network', str(cm.exception))
# The internal AttributeError is not shown to the caller.
self.assertTrue(cm.exception.__suppress_context__)


class NetmaskTestMixin_v6(CommonTestMixin_v6):
"""Input validation on interfaces and networks is very similar"""
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -2008,6 +2008,7 @@ Wm. Keith van der Meulen
Eric N. Vander Weele
Andrew Vant
Atul Varma
Vyron Vasileiadis
Dmitry Vasiliev
Sebastian Ortiz Vasquez
Alexandre Vassalotti
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
:meth:`~ipaddress.IPv4Network.subnet_of` and
:meth:`~ipaddress.IPv4Network.supernet_of` now say that both arguments must be
network objects when passed something else, such as an address, and no longer
show the internal :exc:`AttributeError` behind it.
Loading