summaryrefslogtreecommitdiff
path: root/Lib/ipaddress.py
diff options
context:
space:
mode:
authorNick Coghlan <ncoghlan@gmail.com>2012-07-07 01:13:55 +1000
committerNick Coghlan <ncoghlan@gmail.com>2012-07-07 01:13:55 +1000
commit9aad6ab8fa66f0c4b29a0832653c3e9ce2732fe2 (patch)
treeb569a0786d66f7433f9adb2bc05198ce54344413 /Lib/ipaddress.py
parentc9c71db5ca5a821aa0b88a800fdf8c3b498eefe0 (diff)
downloadcpython-9aad6ab8fa66f0c4b29a0832653c3e9ce2732fe2.tar.gz
Issue 14814: Better handling of cases where octet/hextet parsing fails, including ensuring that tracebacks are still clean even when calling class constructors directly
Diffstat (limited to 'Lib/ipaddress.py')
-rw-r--r--Lib/ipaddress.py10
1 files changed, 7 insertions, 3 deletions
diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py
index 05ea4533ff..352c9b87c4 100644
--- a/Lib/ipaddress.py
+++ b/Lib/ipaddress.py
@@ -1024,7 +1024,7 @@ class _BaseV4:
try:
packed_ip = (packed_ip << 8) | self._parse_octet(oc)
except ValueError:
- raise AddressValueError(ip_str)
+ raise AddressValueError(ip_str) from None
return packed_ip
def _parse_octet(self, octet_str):
@@ -1041,6 +1041,7 @@ class _BaseV4:
"""
# Whitelist the characters, since int() allows a lot of bizarre stuff.
+ # Higher level wrappers convert these to more informative errors
if not self._DECIMAL_DIGITS.issuperset(octet_str):
raise ValueError
octet_int = int(octet_str, 10)
@@ -1497,7 +1498,7 @@ class _BaseV6:
[None])
except ValueError:
# Can't have more than one '::'
- raise AddressValueError(ip_str)
+ raise AddressValueError(ip_str) from None
# parts_hi is the number of parts to copy from above/before the '::'
# parts_lo is the number of parts to copy from below/after the '::'
@@ -1538,7 +1539,7 @@ class _BaseV6:
ip_int |= self._parse_hextet(parts[i])
return ip_int
except ValueError:
- raise AddressValueError(ip_str)
+ raise AddressValueError(ip_str) from None
def _parse_hextet(self, hextet_str):
"""Convert an IPv6 hextet string into an integer.
@@ -1555,8 +1556,11 @@ class _BaseV6:
"""
# Whitelist the characters, since int() allows a lot of bizarre stuff.
+ # Higher level wrappers convert these to more informative errors
if not self._HEX_DIGITS.issuperset(hextet_str):
raise ValueError
+ if len(hextet_str) > 4:
+ raise ValueError
hextet_int = int(hextet_str, 16)
if hextet_int > 0xFFFF:
raise ValueError