bpo-45155: Apply new byteorder default values for int.to/from_bytes (GH-28465)

This commit is contained in:
Raymond Hettinger 2021-09-20 13:22:55 -05:00 committed by GitHub
parent 5846c9b71e
commit 9510e6f3c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 15 additions and 15 deletions

View File

@ -182,7 +182,7 @@ def _b32encode(alphabet, s):
from_bytes = int.from_bytes from_bytes = int.from_bytes
b32tab2 = _b32tab2[alphabet] b32tab2 = _b32tab2[alphabet]
for i in range(0, len(s), 5): for i in range(0, len(s), 5):
c = from_bytes(s[i: i + 5], 'big') c = from_bytes(s[i: i + 5]) # big endian
encoded += (b32tab2[c >> 30] + # bits 1 - 10 encoded += (b32tab2[c >> 30] + # bits 1 - 10
b32tab2[(c >> 20) & 0x3ff] + # bits 11 - 20 b32tab2[(c >> 20) & 0x3ff] + # bits 11 - 20
b32tab2[(c >> 10) & 0x3ff] + # bits 21 - 30 b32tab2[(c >> 10) & 0x3ff] + # bits 21 - 30
@ -234,13 +234,13 @@ def _b32decode(alphabet, s, casefold=False, map01=None):
acc = (acc << 5) + b32rev[c] acc = (acc << 5) + b32rev[c]
except KeyError: except KeyError:
raise binascii.Error('Non-base32 digit found') from None raise binascii.Error('Non-base32 digit found') from None
decoded += acc.to_bytes(5, 'big') decoded += acc.to_bytes(5) # big endian
# Process the last, partial quanta # Process the last, partial quanta
if l % 8 or padchars not in {0, 1, 3, 4, 6}: if l % 8 or padchars not in {0, 1, 3, 4, 6}:
raise binascii.Error('Incorrect padding') raise binascii.Error('Incorrect padding')
if padchars and decoded: if padchars and decoded:
acc <<= 5 * padchars acc <<= 5 * padchars
last = acc.to_bytes(5, 'big') last = acc.to_bytes(5) # big endian
leftover = (43 - 5 * padchars) // 8 # 1: 4, 3: 3, 4: 2, 6: 1 leftover = (43 - 5 * padchars) // 8 # 1: 4, 3: 3, 4: 2, 6: 1
decoded[-5:] = last[:leftover] decoded[-5:] = last[:leftover]
return bytes(decoded) return bytes(decoded)

View File

@ -235,15 +235,15 @@ except ImportError:
loop = 1 loop = 1
from_bytes = int.from_bytes from_bytes = int.from_bytes
while len(dkey) < dklen: while len(dkey) < dklen:
prev = prf(salt + loop.to_bytes(4, 'big')) prev = prf(salt + loop.to_bytes(4))
# endianness doesn't matter here as long to / from use the same # endianness doesn't matter here as long to / from use the same
rkey = int.from_bytes(prev, 'big') rkey = from_bytes(prev)
for i in range(iterations - 1): for i in range(iterations - 1):
prev = prf(prev) prev = prf(prev)
# rkey = rkey ^ prev # rkey = rkey ^ prev
rkey ^= from_bytes(prev, 'big') rkey ^= from_bytes(prev)
loop += 1 loop += 1
dkey += rkey.to_bytes(inner.digest_size, 'big') dkey += rkey.to_bytes(inner.digest_size)
return dkey[:dklen] return dkey[:dklen]

View File

@ -135,7 +135,7 @@ def v4_int_to_packed(address):
""" """
try: try:
return address.to_bytes(4, 'big') return address.to_bytes(4) # big endian
except OverflowError: except OverflowError:
raise ValueError("Address negative or too large for IPv4") raise ValueError("Address negative or too large for IPv4")
@ -151,7 +151,7 @@ def v6_int_to_packed(address):
""" """
try: try:
return address.to_bytes(16, 'big') return address.to_bytes(16) # big endian
except OverflowError: except OverflowError:
raise ValueError("Address negative or too large for IPv6") raise ValueError("Address negative or too large for IPv6")
@ -1297,7 +1297,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 = int.from_bytes(address, 'big') self._ip = int.from_bytes(address) # big endian
return return
# Assume input argument to be string or any object representation # Assume input argument to be string or any object representation

View File

@ -154,7 +154,7 @@ class Random(_random.Random):
elif version == 2 and isinstance(a, (str, bytes, bytearray)): elif version == 2 and isinstance(a, (str, bytes, bytearray)):
if isinstance(a, str): if isinstance(a, str):
a = a.encode() a = a.encode()
a = int.from_bytes(a + _sha512(a).digest(), 'big') a = int.from_bytes(a + _sha512(a).digest())
elif not isinstance(a, (type(None), int, float, str, bytes, bytearray)): elif not isinstance(a, (type(None), int, float, str, bytes, bytearray)):
raise TypeError('The only supported seed types are: None,\n' raise TypeError('The only supported seed types are: None,\n'
@ -795,14 +795,14 @@ class SystemRandom(Random):
def random(self): def random(self):
"""Get the next random number in the range [0.0, 1.0).""" """Get the next random number in the range [0.0, 1.0)."""
return (int.from_bytes(_urandom(7), 'big') >> 3) * RECIP_BPF return (int.from_bytes(_urandom(7)) >> 3) * RECIP_BPF
def getrandbits(self, k): def getrandbits(self, k):
"""getrandbits(k) -> x. Generates an int with k random bits.""" """getrandbits(k) -> x. Generates an int with k random bits."""
if k < 0: if k < 0:
raise ValueError('number of bits must be non-negative') raise ValueError('number of bits must be non-negative')
numbytes = (k + 7) // 8 # bits / 8 and rounded up numbytes = (k + 7) // 8 # bits / 8 and rounded up
x = int.from_bytes(_urandom(numbytes), 'big') x = int.from_bytes(_urandom(numbytes))
return x >> (numbytes * 8 - k) # trim excess bits return x >> (numbytes * 8 - k) # trim excess bits
def randbytes(self, n): def randbytes(self, n):

View File

@ -186,7 +186,7 @@ class UUID:
if len(bytes) != 16: if len(bytes) != 16:
raise ValueError('bytes is not a 16-char string') raise ValueError('bytes is not a 16-char string')
assert isinstance(bytes, bytes_), repr(bytes) assert isinstance(bytes, bytes_), repr(bytes)
int = int_.from_bytes(bytes, byteorder='big') int = int_.from_bytes(bytes) # big endian
if fields is not None: if fields is not None:
if len(fields) != 6: if len(fields) != 6:
raise ValueError('fields is not a 6-tuple') raise ValueError('fields is not a 6-tuple')
@ -284,7 +284,7 @@ class UUID:
@property @property
def bytes(self): def bytes(self):
return self.int.to_bytes(16, 'big') return self.int.to_bytes(16) # big endian
@property @property
def bytes_le(self): def bytes_le(self):