diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py index 15507d61dec8f9..169bf9ba7432fe 100644 --- a/Lib/ipaddress.py +++ b/Lib/ipaddress.py @@ -1519,7 +1519,13 @@ def __init__(self, address, strict=True): # Constructing from a tuple (addr, [mask]) elif isinstance(address, tuple): addr = address[0] - mask = address[1] if len(address) > 1 else self._max_prefixlen + if len(address) > 1: + if isinstance(address[1], int) and address[1] > 32: + raise NetmaskValueError(f"The netmask is not valid {address[1]}") + mask = address[1] + else: + mask = self._max_prefixlen + #mask = address[1] if len(address) > 1 else self._max_prefixlen # Assume input argument to be string or any object representation # which converts into a formatted IP prefix string. else: diff --git a/Lib/test/test_ipaddress.py b/Lib/test/test_ipaddress.py index 0e0753f34c4905..373eb5e828aedf 100644 --- a/Lib/test/test_ipaddress.py +++ b/Lib/test/test_ipaddress.py @@ -839,6 +839,8 @@ def testIPv4Tuple(self): # strict=True and host bits set with self.assertRaises(ValueError): ipaddress.IPv4Network(('192.0.2.1', 24)) + with self.assertRaises(ValueError): + ipaddress.IPv4Network(('192.0.2.1', 33)) with self.assertRaises(ValueError): ipaddress.IPv4Network((ip, 24)) with self.assertRaises(ValueError): diff --git a/Misc/NEWS.d/next/Library/2019-02-13-21-43-31.bpo-35989.8L9aBw.rst b/Misc/NEWS.d/next/Library/2019-02-13-21-43-31.bpo-35989.8L9aBw.rst new file mode 100644 index 00000000000000..810a4828e12e08 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-02-13-21-43-31.bpo-35989.8L9aBw.rst @@ -0,0 +1,2 @@ +IPv4Network raises a ``NetmaskValueError`` exception if the netmask is not +between 0 and 32. Contributed by Stéphane Wirtel.