Skip to content

Commit 9510e6f

Browse files
authored
bpo-45155: Apply new byteorder default values for int.to/from_bytes (GH-28465)
1 parent 5846c9b commit 9510e6f

File tree

5 files changed

+15
-15
lines changed

5 files changed

+15
-15
lines changed

Lib/base64.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ def _b32encode(alphabet, s):
182182
from_bytes = int.from_bytes
183183
b32tab2 = _b32tab2[alphabet]
184184
for i in range(0, len(s), 5):
185-
c = from_bytes(s[i: i + 5], 'big')
185+
c = from_bytes(s[i: i + 5]) # big endian
186186
encoded += (b32tab2[c >> 30] + # bits 1 - 10
187187
b32tab2[(c >> 20) & 0x3ff] + # bits 11 - 20
188188
b32tab2[(c >> 10) & 0x3ff] + # bits 21 - 30
@@ -234,13 +234,13 @@ def _b32decode(alphabet, s, casefold=False, map01=None):
234234
acc = (acc << 5) + b32rev[c]
235235
except KeyError:
236236
raise binascii.Error('Non-base32 digit found') from None
237-
decoded += acc.to_bytes(5, 'big')
237+
decoded += acc.to_bytes(5) # big endian
238238
# Process the last, partial quanta
239239
if l % 8 or padchars not in {0, 1, 3, 4, 6}:
240240
raise binascii.Error('Incorrect padding')
241241
if padchars and decoded:
242242
acc <<= 5 * padchars
243-
last = acc.to_bytes(5, 'big')
243+
last = acc.to_bytes(5) # big endian
244244
leftover = (43 - 5 * padchars) // 8 # 1: 4, 3: 3, 4: 2, 6: 1
245245
decoded[-5:] = last[:leftover]
246246
return bytes(decoded)

Lib/hashlib.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,15 +235,15 @@ def prf(msg, inner=inner, outer=outer):
235235
loop = 1
236236
from_bytes = int.from_bytes
237237
while len(dkey) < dklen:
238-
prev = prf(salt + loop.to_bytes(4, 'big'))
238+
prev = prf(salt + loop.to_bytes(4))
239239
# endianness doesn't matter here as long to / from use the same
240-
rkey = int.from_bytes(prev, 'big')
240+
rkey = from_bytes(prev)
241241
for i in range(iterations - 1):
242242
prev = prf(prev)
243243
# rkey = rkey ^ prev
244-
rkey ^= from_bytes(prev, 'big')
244+
rkey ^= from_bytes(prev)
245245
loop += 1
246-
dkey += rkey.to_bytes(inner.digest_size, 'big')
246+
dkey += rkey.to_bytes(inner.digest_size)
247247

248248
return dkey[:dklen]
249249

Lib/ipaddress.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def v4_int_to_packed(address):
135135
136136
"""
137137
try:
138-
return address.to_bytes(4, 'big')
138+
return address.to_bytes(4) # big endian
139139
except OverflowError:
140140
raise ValueError("Address negative or too large for IPv4")
141141

@@ -151,7 +151,7 @@ def v6_int_to_packed(address):
151151
152152
"""
153153
try:
154-
return address.to_bytes(16, 'big')
154+
return address.to_bytes(16) # big endian
155155
except OverflowError:
156156
raise ValueError("Address negative or too large for IPv6")
157157

@@ -1297,7 +1297,7 @@ def __init__(self, address):
12971297
# Constructing from a packed address
12981298
if isinstance(address, bytes):
12991299
self._check_packed_address(address, 4)
1300-
self._ip = int.from_bytes(address, 'big')
1300+
self._ip = int.from_bytes(address) # big endian
13011301
return
13021302

13031303
# Assume input argument to be string or any object representation

Lib/random.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def seed(self, a=None, version=2):
154154
elif version == 2 and isinstance(a, (str, bytes, bytearray)):
155155
if isinstance(a, str):
156156
a = a.encode()
157-
a = int.from_bytes(a + _sha512(a).digest(), 'big')
157+
a = int.from_bytes(a + _sha512(a).digest())
158158

159159
elif not isinstance(a, (type(None), int, float, str, bytes, bytearray)):
160160
raise TypeError('The only supported seed types are: None,\n'
@@ -795,14 +795,14 @@ class SystemRandom(Random):
795795

796796
def random(self):
797797
"""Get the next random number in the range [0.0, 1.0)."""
798-
return (int.from_bytes(_urandom(7), 'big') >> 3) * RECIP_BPF
798+
return (int.from_bytes(_urandom(7)) >> 3) * RECIP_BPF
799799

800800
def getrandbits(self, k):
801801
"""getrandbits(k) -> x. Generates an int with k random bits."""
802802
if k < 0:
803803
raise ValueError('number of bits must be non-negative')
804804
numbytes = (k + 7) // 8 # bits / 8 and rounded up
805-
x = int.from_bytes(_urandom(numbytes), 'big')
805+
x = int.from_bytes(_urandom(numbytes))
806806
return x >> (numbytes * 8 - k) # trim excess bits
807807

808808
def randbytes(self, n):

Lib/uuid.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ def __init__(self, hex=None, bytes=None, bytes_le=None, fields=None,
186186
if len(bytes) != 16:
187187
raise ValueError('bytes is not a 16-char string')
188188
assert isinstance(bytes, bytes_), repr(bytes)
189-
int = int_.from_bytes(bytes, byteorder='big')
189+
int = int_.from_bytes(bytes) # big endian
190190
if fields is not None:
191191
if len(fields) != 6:
192192
raise ValueError('fields is not a 6-tuple')
@@ -284,7 +284,7 @@ def __str__(self):
284284

285285
@property
286286
def bytes(self):
287-
return self.int.to_bytes(16, 'big')
287+
return self.int.to_bytes(16) # big endian
288288

289289
@property
290290
def bytes_le(self):

0 commit comments

Comments
 (0)