From 42d43aace98fca1866f413d7a4aa532736f56526 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Sat, 5 Feb 2022 10:26:24 +0300 Subject: [PATCH] bpo-46646: mention that `bytes` are accepted in `ipaddress` docs --- Doc/library/ipaddress.rst | 17 ++++++++++++----- Lib/ipaddress.py | 16 ++++++++-------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/Doc/library/ipaddress.rst b/Doc/library/ipaddress.rst index 9c2dff55703273..a20cd08a54ff5f 100644 --- a/Doc/library/ipaddress.rst +++ b/Doc/library/ipaddress.rst @@ -40,7 +40,9 @@ IP addresses, networks and interfaces: .. function:: ip_address(address) Return an :class:`IPv4Address` or :class:`IPv6Address` object depending on - the IP address passed as argument. Either IPv4 or IPv6 addresses may be + the IP address passed as argument. + *address* is a string or integer or bytes representing the IP address. + Either IPv4 or IPv6 addresses may be supplied; integers less than ``2**32`` will be considered to be IPv4 by default. A :exc:`ValueError` is raised if *address* does not represent a valid IPv4 or IPv6 address. @@ -54,8 +56,9 @@ IP addresses, networks and interfaces: .. function:: ip_network(address, strict=True) Return an :class:`IPv4Network` or :class:`IPv6Network` object depending on - the IP address passed as argument. *address* is a string or integer - representing the IP network. Either IPv4 or IPv6 networks may be supplied; + the IP address passed as argument. + *address* is a string or integer or bytes representing the IP network. + Either IPv4 or IPv6 networks may be supplied; integers less than ``2**32`` will be considered to be IPv4 by default. *strict* is passed to :class:`IPv4Network` or :class:`IPv6Network` constructor. A :exc:`ValueError` is raised if *address* does not represent a valid IPv4 or @@ -68,12 +71,16 @@ IP addresses, networks and interfaces: .. function:: ip_interface(address) Return an :class:`IPv4Interface` or :class:`IPv6Interface` object depending - on the IP address passed as argument. *address* is a string or integer - representing the IP address. Either IPv4 or IPv6 addresses may be supplied; + on the IP address passed as argument. + *address* is a string or integer or bytes representing the IP address. + Either IPv4 or IPv6 addresses may be supplied; integers less than ``2**32`` will be considered to be IPv4 by default. A :exc:`ValueError` is raised if *address* does not represent a valid IPv4 or IPv6 address. + >>> ipaddress.ip_interface('192.168.0.0/28') + IPv4Interface('192.168.0.0/28') + One downside of these convenience functions is that the need to handle both IPv4 and IPv6 formats means that error messages provide minimal information on the precise error, as the functions don't know whether the diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py index e601f6f476e4df..c7e64d8f335693 100644 --- a/Lib/ipaddress.py +++ b/Lib/ipaddress.py @@ -26,10 +26,10 @@ class NetmaskValueError(ValueError): def ip_address(address): - """Take an IP string/int and return an object of the correct type. + """Take an IP string/int/bytes and return an object of the correct type. Args: - address: A string or integer, the IP address. Either IPv4 or + address: A string or integer or bytes, the IP address. Either IPv4 or IPv6 addresses may be supplied; integers less than 2**32 will be considered to be IPv4 by default. @@ -56,10 +56,10 @@ def ip_address(address): def ip_network(address, strict=True): - """Take an IP string/int and return an object of the correct type. + """Take an IP string/int/bytes and return an object of the correct type. Args: - address: A string or integer, the IP network. Either IPv4 or + address: A string or integer or bytes, the IP network. Either IPv4 or IPv6 networks may be supplied; integers less than 2**32 will be considered to be IPv4 by default. @@ -67,7 +67,7 @@ def ip_network(address, strict=True): An IPv4Network or IPv6Network object. Raises: - ValueError: if the string passed isn't either a v4 or a v6 + ValueError: if the *address* passed isn't either a v4 or a v6 address. Or if the network has host bits set. """ @@ -86,10 +86,10 @@ def ip_network(address, strict=True): def ip_interface(address): - """Take an IP string/int and return an object of the correct type. + """Take an IP string/int/bytes and return an object of the correct type. Args: - address: A string or integer, the IP address. Either IPv4 or + address: A string or integer or bytes, the IP address. Either IPv4 or IPv6 addresses may be supplied; integers less than 2**32 will be considered to be IPv4 by default. @@ -97,7 +97,7 @@ def ip_interface(address): An IPv4Interface or IPv6Interface object. Raises: - ValueError: if the string passed isn't either a v4 or a v6 + ValueError: if the *address* passed isn't either a v4 or a v6 address. Notes: