diff options
author | Nick Coghlan <ncoghlan@gmail.com> | 2012-07-06 15:43:31 (GMT) |
---|---|---|
committer | Nick Coghlan <ncoghlan@gmail.com> | 2012-07-06 15:43:31 (GMT) |
commit | 5cf896fea8005478bcc2bcd6e10e4572ae5fe2be (patch) | |
tree | 12c10c0e0469c5d57f967e9993cb6992ae2f31d7 /Lib/ipaddress.py | |
parent | 3c2570caf219fab4fea9d17a5e6bc5cba741b547 (diff) | |
download | cpython-5cf896fea8005478bcc2bcd6e10e4572ae5fe2be.zip cpython-5cf896fea8005478bcc2bcd6e10e4572ae5fe2be.tar.gz cpython-5cf896fea8005478bcc2bcd6e10e4572ae5fe2be.tar.bz2 |
Issue 14814: Eliminate bytes warnings from ipaddress by correctly throwing an exception early when given bytes data of the wrong length. Also removes 2.x backwards compatibility code from associated tests.
Diffstat (limited to 'Lib/ipaddress.py')
-rw-r--r-- | Lib/ipaddress.py | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py index 352c9b8..9a1ba72 100644 --- a/Lib/ipaddress.py +++ b/Lib/ipaddress.py @@ -1250,7 +1250,9 @@ class IPv4Address(_BaseV4, _BaseAddress): return # Constructing from a packed address - if isinstance(address, bytes) and len(address) == 4: + if isinstance(address, bytes): + if len(address) != 4: + raise AddressValueError(address) self._ip = struct.unpack('!I', address)[0] return @@ -1379,7 +1381,9 @@ class IPv4Network(_BaseV4, _BaseNetwork): _BaseNetwork.__init__(self, address) # Constructing from a packed address - if isinstance(address, bytes) and len(address) == 4: + if isinstance(address, bytes): + if len(address) != 4: + raise AddressValueError(address) self.network_address = IPv4Address( struct.unpack('!I', address)[0]) self._prefixlen = self._max_prefixlen @@ -1864,7 +1868,9 @@ class IPv6Address(_BaseV6, _BaseAddress): return # Constructing from a packed address - if isinstance(address, bytes) and len(address) == 16: + if isinstance(address, bytes): + if len(address) != 16: + raise AddressValueError(address) tmp = struct.unpack('!QQ', address) self._ip = (tmp[0] << 64) | tmp[1] return @@ -1996,7 +2002,9 @@ class IPv6Network(_BaseV6, _BaseNetwork): return # Constructing from a packed address - if isinstance(address, bytes) and len(address) == 16: + if isinstance(address, bytes): + if len(address) != 16: + raise AddressValueError(address) tmp = struct.unpack('!QQ', address) self.network_address = IPv6Address((tmp[0] << 64) | tmp[1]) self._prefixlen = self._max_prefixlen |