summaryrefslogtreecommitdiff
path: root/Lib/ipaddress.py
diff options
context:
space:
mode:
authorNick Coghlan <ncoghlan@gmail.com>2012-07-08 23:06:45 +1000
committerNick Coghlan <ncoghlan@gmail.com>2012-07-08 23:06:45 +1000
commit56e19ee70c28181f45fe42915eead9d3cf4ce0b9 (patch)
tree5641aa1e561412ff75bf511ee1f8b82349995faf /Lib/ipaddress.py
parentaaf837499ee944feffaf9840413bdbeba36a4e1f (diff)
downloadcpython-56e19ee70c28181f45fe42915eead9d3cf4ce0b9.tar.gz
Issue 14814: The new systematic tests aren't just about error reporting any more - change names accordingly. Added and tweaked some example to ensure they were covering the intended code paths
Diffstat (limited to 'Lib/ipaddress.py')
-rw-r--r--Lib/ipaddress.py23
1 files changed, 13 insertions, 10 deletions
diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py
index 7e6f03f0a2..c6eea7fac8 100644
--- a/Lib/ipaddress.py
+++ b/Lib/ipaddress.py
@@ -1009,15 +1009,21 @@ class _BaseV4:
raise ValueError("Empty octet not permitted")
# Whitelist the characters, since int() allows a lot of bizarre stuff.
if not self._DECIMAL_DIGITS.issuperset(octet_str):
- raise ValueError("Only decimal digits permitted in %r" % octet_str)
+ msg = "Only decimal digits permitted in %r"
+ raise ValueError(msg % octet_str)
+ # We do the length check second, since the invalid character error
+ # is likely to be more informative for the user
+ if len(octet_str) > 3:
+ msg = "At most 3 characters permitted in %r"
+ raise ValueError(msg % octet_str)
# Convert to integer (we know digits are legal)
octet_int = int(octet_str, 10)
# Any octets that look like they *might* be written in octal,
# and which don't look exactly the same in both octal and
# decimal are rejected as ambiguous
if octet_int > 7 and octet_str[0] == '0':
- raise ValueError("Ambiguous leading zero in %r not permitted" %
- octet_str)
+ msg = "Ambiguous (octal/decimal) value in %r not permitted"
+ raise ValueError(msg % octet_str)
if octet_int > 255:
raise ValueError("Octet %d (> 255) not permitted" % octet_int)
return octet_int
@@ -1560,18 +1566,15 @@ 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("Only hex digits permitted in %r" % hextet_str)
+ # We do the length check second, since the invalid character error
+ # is likely to be more informative for the user
if len(hextet_str) > 4:
msg = "At most 4 characters permitted in %r"
raise ValueError(msg % hextet_str)
- hextet_int = int(hextet_str, 16)
- if hextet_int > 0xFFFF:
- # This is unreachable due to the string length check above
- msg = "Part 0x%X (> 0xFFFF) not permitted"
- raise ValueError(msg % hextet_int)
- return hextet_int
+ # Length check means we can skip checking the integer value
+ return int(hextet_str, 16)
def _compress_hextets(self, hextets):
"""Compresses a list of hextets.