[New-bugs-announce] [issue46415] ipaddress.ip_{address, network, interface} raise TypeError instead of ValueError if given a tuple as address

Thomas Cellerier report at bugs.python.org
Mon Jan 17 10:21:20 EST 2022


New submission from Thomas Cellerier <thomascellerier at gmail.com>:

`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

----------
components: Library (Lib)
messages: 410798
nosy: thomascellerier
priority: normal
severity: normal
status: open
title: ipaddress.ip_{address,network,interface} raise TypeError instead of ValueError if given a tuple as address
type: behavior
versions: Python 3.8

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue46415>
_______________________________________


More information about the New-bugs-announce mailing list