Commit db7920b9 authored by Nick Coghlan's avatar Nick Coghlan

Close #14814: Avoid depending on struct by using newer features. Also use...

Close #14814: Avoid depending on struct by using newer features. Also use enumerate where appropriate (patch by Serhiy Storchaka). Declaring PEP 3144 final at this point - any further changes to code or docs can go in new issues.
parent a8517ad3
...@@ -11,7 +11,6 @@ and networks. ...@@ -11,7 +11,6 @@ and networks.
__version__ = '1.0' __version__ = '1.0'
import struct
import functools import functools
IPV4LENGTH = 32 IPV4LENGTH = 32
...@@ -135,7 +134,7 @@ def v4_int_to_packed(address): ...@@ -135,7 +134,7 @@ def v4_int_to_packed(address):
""" """
try: try:
return struct.pack('!I', address) return address.to_bytes(4, 'big')
except: except:
raise ValueError("Address negative or too large for IPv4") raise ValueError("Address negative or too large for IPv4")
...@@ -151,7 +150,7 @@ def v6_int_to_packed(address): ...@@ -151,7 +150,7 @@ def v6_int_to_packed(address):
""" """
try: try:
return struct.pack('!QQ', address >> 64, address & (2**64 - 1)) return address.to_bytes(16, 'big')
except: except:
raise ValueError("Address negative or too large for IPv6") raise ValueError("Address negative or too large for IPv6")
...@@ -1195,7 +1194,7 @@ class IPv4Address(_BaseV4, _BaseAddress): ...@@ -1195,7 +1194,7 @@ class IPv4Address(_BaseV4, _BaseAddress):
# Constructing from a packed address # Constructing from a packed address
if isinstance(address, bytes): if isinstance(address, bytes):
self._check_packed_address(address, 4) self._check_packed_address(address, 4)
self._ip = struct.unpack('!I', address)[0] self._ip = int.from_bytes(address, 'big')
return return
# Assume input argument to be string or any object representation # Assume input argument to be string or any object representation
...@@ -1632,8 +1631,8 @@ class _BaseV6: ...@@ -1632,8 +1631,8 @@ class _BaseV6:
best_doublecolon_len = 0 best_doublecolon_len = 0
doublecolon_start = -1 doublecolon_start = -1
doublecolon_len = 0 doublecolon_len = 0
for index in range(len(hextets)): for index, hextet in enumerate(hextets):
if hextets[index] == '0': if hextet == '0':
doublecolon_len += 1 doublecolon_len += 1
if doublecolon_start == -1: if doublecolon_start == -1:
# Start of a sequence of zeros. # Start of a sequence of zeros.
...@@ -1750,8 +1749,7 @@ class IPv6Address(_BaseV6, _BaseAddress): ...@@ -1750,8 +1749,7 @@ class IPv6Address(_BaseV6, _BaseAddress):
# Constructing from a packed address # Constructing from a packed address
if isinstance(address, bytes): if isinstance(address, bytes):
self._check_packed_address(address, 16) self._check_packed_address(address, 16)
tmp = struct.unpack('!QQ', address) self._ip = int.from_bytes(address, 'big')
self._ip = (tmp[0] << 64) | tmp[1]
return return
# Assume input argument to be string or any object representation # Assume input argument to be string or any object representation
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment