This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: ipaddress.ip_{address,network,interface} raises TypeError instead of ValueError if given a tuple as address
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.8
process
Status: closed Resolution: duplicate
Dependencies: Superseder:
Assigned To: Nosy List: JelleZijlstra, thomascellerier
Priority: normal Keywords: patch

Created on 2022-01-17 15:21 by thomascellerier, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 30642 open thomascellerier, 2022-01-17 15:23
Messages (2)
msg410798 - (view) Author: Thomas Cellerier (thomascellerier) * Date: 2022-01-17 15:21
`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                                 Traceback (most recent call last)
	<ipython-input-7-7fc0bff07012> in <module>
	----> 1 ipaddress.ip_network(("192.168.100.0", "fooo"))

	/usr/lib/python3.8/ipaddress.py in ip_network(address, strict)
	     81         pass
	     82
	---> 83     raise ValueError('%r does not appear to be an IPv4 or IPv6 network' %
	     84                      address)
	     85

	TypeError: not all arguments converted during string formatting

Compared to:

	In [8]: ipaddress.IPv4Network(("192.168.100.0", "foo"))
	---------------------------------------------------------------------------
	NetmaskValueError                         Traceback (most recent call last)
	<ipython-input-8-79078758e653> in <module>
	----> 1 ipaddress.IPv4Network(("192.168.100.0", "foo"))

	/usr/lib/python3.8/ipaddress.py in __init__(self, address, strict)
	   1453
	   1454         self.network_address = IPv4Address(addr)
	-> 1455         self.netmask, self._prefixlen = self._make_netmask(mask)
	   1456         packed = int(self.network_address)
	   1457         if packed & int(self.netmask) != packed:

	/usr/lib/python3.8/ipaddress.py in _make_netmask(cls, arg)
	   1118                     # Check for a netmask or hostmask in dotted-quad form.
	   1119                     # This may raise NetmaskValueError.
	-> 1120                     prefixlen = cls._prefix_from_ip_string(arg)
	   1121             netmask = IPv4Address(cls._ip_int_from_prefix(prefixlen))
	   1122             cls._netmask_cache[arg] = netmask, prefixlen

	/usr/lib/python3.8/ipaddress.py in _prefix_from_ip_string(cls, ip_str)
	    516             ip_int = cls._ip_int_from_string(ip_str)
	    517         except AddressValueError:
	--> 518             cls._report_invalid_netmask(ip_str)
	    519
	    520         # Try matching a netmask (this would be /1*0*/ as a bitwise regexp).

	/usr/lib/python3.8/ipaddress.py in _report_invalid_netmask(cls, netmask_str)
	    472     def _report_invalid_netmask(cls, netmask_str):
	    473         msg = '%r is not a valid netmask' % netmask_str
	--> 474         raise NetmaskValueError(msg) from None
	    475
	    476     @classmethod

	NetmaskValueError: 'foo' is not a valid netmask
msg411081 - (view) Author: Jelle Zijlstra (JelleZijlstra) * (Python committer) Date: 2022-01-21 04:53
Duplicate of issue46141.
History
Date User Action Args
2022-04-11 14:59:54adminsetgithub: 90573
2022-01-21 04:53:15JelleZijlstrasetstatus: open -> closed

nosy: + JelleZijlstra
messages: + msg411081

resolution: duplicate
stage: patch review -> resolved
2022-01-17 15:37:47thomascelleriersettitle: ipaddress.ip_{address,network,interface} raise TypeError instead of ValueError if given a tuple as address -> ipaddress.ip_{address,network,interface} raises TypeError instead of ValueError if given a tuple as address
2022-01-17 15:23:46thomascelleriersetkeywords: + patch
stage: patch review
pull_requests: + pull_request28845
2022-01-17 15:21:20thomascelleriercreate