diff options
author | Thomas Cellerier <thomascellerier@gmail.com> | 2022-05-03 12:12:58 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-03 12:12:58 (GMT) |
commit | 52dc9c3066bcdc67a7a45d41cf158ecb1434d5f3 (patch) | |
tree | 87388bf8306586e9ada724873ec5911e97352cf5 /Lib/ipaddress.py | |
parent | ec8d3adb99f1ad93786fed5c1def5119b6ec73c0 (diff) | |
download | cpython-52dc9c3066bcdc67a7a45d41cf158ecb1434d5f3.zip cpython-52dc9c3066bcdc67a7a45d41cf158ecb1434d5f3.tar.gz cpython-52dc9c3066bcdc67a7a45d41cf158ecb1434d5f3.tar.bz2 |
bpo-46415: Use f-string for ValueError in ipaddress.ip_{address,network,interface} helper functions (#30642)
`IPv*Network` and `IPv*Interface` constructors accept a 2-tuple of
(address description, netmask) as the address parameter.
When the tuple-based address is used errors are not propagated
correctly through the `ipaddress.ip_*` helper because of the %-formatting now expecting several arguments:
In [7]: ipaddress.ip_network(("192.168.100.0", "fooo"))
...
TypeError: not all arguments converted during string formatting
Compared to:
In [8]: ipaddress.IPv4Network(("192.168.100.0", "foo"))
...
NetmaskValueError: 'foo' is not a valid netmask
Use an f-string to make sure the error is always properly formatted.
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Diffstat (limited to 'Lib/ipaddress.py')
-rw-r--r-- | Lib/ipaddress.py | 15 |
1 files changed, 6 insertions, 9 deletions
diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py index e601f6f..3f15601 100644 --- a/Lib/ipaddress.py +++ b/Lib/ipaddress.py @@ -51,8 +51,7 @@ def ip_address(address): 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): @@ -81,8 +80,7 @@ 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): @@ -116,8 +114,7 @@ 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): @@ -160,7 +157,7 @@ def _split_optional_netmask(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 @@ -1304,7 +1301,7 @@ class IPv4Address(_BaseV4, _BaseAddress): # 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 @@ -1913,7 +1910,7 @@ class IPv6Address(_BaseV6, _BaseAddress): # 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) |