except (AddressValueError, NetmaskValueError):
pass
- raise ValueError('%r does not appear to be an IPv4 or IPv6 address' %
- address)
+ raise ValueError(f'{address!r} does not appear to be an IPv4 or IPv6 address')
def ip_network(address, strict=True):
except (AddressValueError, NetmaskValueError):
pass
- raise ValueError('%r does not appear to be an IPv4 or IPv6 network' %
- address)
+ raise ValueError(f'{address!r} does not appear to be an IPv4 or IPv6 network')
def ip_interface(address):
except (AddressValueError, NetmaskValueError):
pass
- raise ValueError('%r does not appear to be an IPv4 or IPv6 interface' %
- address)
+ raise ValueError(f'{address!r} does not appear to be an IPv4 or IPv6 interface')
def v4_int_to_packed(address):
"""Helper to split the netmask and raise AddressValueError if needed"""
addr = str(address).split('/')
if len(addr) > 2:
- raise AddressValueError("Only one '/' permitted in %r" % address)
+ raise AddressValueError(f"Only one '/' permitted in {address!r}")
return addr
# which converts into a formatted IP string.
addr_str = str(address)
if '/' in addr_str:
- raise AddressValueError("Unexpected '/' in %r" % address)
+ raise AddressValueError(f"Unexpected '/' in {address!r}")
self._ip = self._ip_int_from_string(addr_str)
@property
# which converts into a formatted IP string.
addr_str = str(address)
if '/' in addr_str:
- raise AddressValueError("Unexpected '/' in %r" % address)
+ raise AddressValueError(f"Unexpected '/' in {address!r}")
addr_str, self._scope_id = self._split_scope_id(addr_str)
self._ip = self._ip_int_from_string(addr_str)
self.assertEqual(ipaddress.IPv4Interface((3221225985, 24)),
ipaddress.IPv4Interface('192.0.2.1/24'))
+ # Invalid netmask
+ with self.assertRaises(ValueError):
+ ipaddress.IPv4Network(('192.0.2.1', '255.255.255.255.0'))
+
+ # Invalid netmask using factory
+ with self.assertRaises(ValueError):
+ ipaddress.ip_network(('192.0.2.1', '255.255.255.255.0'))
+
# issue #16531: constructing IPv6Network from an (address, mask) tuple
def testIPv6Tuple(self):
# /128
ipaddress.IPv6Network((ip_scoped, 96))
# strict=False and host bits set
+ # Invalid netmask
+ with self.assertRaises(ValueError):
+ ipaddress.IPv6Network(('2001:db8::1', '255.255.255.0'))
+
+ # Invalid netmask using factory
+ with self.assertRaises(ValueError):
+ ipaddress.ip_network(('2001:db8::1', '255.255.255.0'))
+
# issue57
def testAddressIntMath(self):
self.assertEqual(ipaddress.IPv4Address('1.1.1.1') + 255,