Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8cbd3df

Browse files
Issue #28992: Use bytes.fromhex().
1 parent 47bdc40 commit 8cbd3df

4 files changed

Lines changed: 7 additions & 10 deletions

File tree

Lib/email/_encoded_words.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262

6363
# regex based decoder.
6464
_q_byte_subber = functools.partial(re.compile(br'=([a-fA-F0-9]{2})').sub,
65-
lambda m: bytes([int(m.group(1), 16)]))
65+
lambda m: bytes.fromhex(m.group(1)))
6666

6767
def decode_q(encoded):
6868
encoded = encoded.replace(b'_', b' ')

Lib/test/multibytecodec_support.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ def _test_mapping_file_ucm(self):
338338
uc = re.findall('<a u="([A-F0-9]{4})" b="([0-9A-F ]+)"/>', ucmdata)
339339
for uni, coded in uc:
340340
unich = chr(int(uni, 16))
341-
codech = bytes(int(c, 16) for c in coded.split())
341+
codech = bytes.fromhex(coded)
342342
self._testpoint(codech, unich)
343343

344344
def test_mapping_supplemental(self):

Lib/test/test_unicode.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1793,9 +1793,6 @@ def test_issue8271(self):
17931793
self.assertEqual(seq.decode('utf-8', 'ignore'),
17941794
res.replace('\uFFFD', ''))
17951795

1796-
def to_bytestring(self, seq):
1797-
return bytes(int(c, 16) for c in seq.split())
1798-
17991796
def assertCorrectUTF8Decoding(self, seq, res, err):
18001797
"""
18011798
Check that an invalid UTF-8 sequence raises a UnicodeDecodeError when
@@ -1851,7 +1848,7 @@ def test_unexpected_end_of_data(self):
18511848
]
18521849
FFFD = '\ufffd'
18531850
for seq in sequences:
1854-
self.assertCorrectUTF8Decoding(self.to_bytestring(seq), '\ufffd',
1851+
self.assertCorrectUTF8Decoding(bytes.fromhex(seq), '\ufffd',
18551852
'unexpected end of data')
18561853

18571854
def test_invalid_cb_for_2bytes_seq(self):
@@ -1873,7 +1870,7 @@ def test_invalid_cb_for_2bytes_seq(self):
18731870
('DF C0', FFFDx2), ('DF FF', FFFDx2),
18741871
]
18751872
for seq, res in sequences:
1876-
self.assertCorrectUTF8Decoding(self.to_bytestring(seq), res,
1873+
self.assertCorrectUTF8Decoding(bytes.fromhex(seq), res,
18771874
'invalid continuation byte')
18781875

18791876
def test_invalid_cb_for_3bytes_seq(self):
@@ -1931,7 +1928,7 @@ def test_invalid_cb_for_3bytes_seq(self):
19311928
('EF BF C0', FFFDx2), ('EF BF FF', FFFDx2),
19321929
]
19331930
for seq, res in sequences:
1934-
self.assertCorrectUTF8Decoding(self.to_bytestring(seq), res,
1931+
self.assertCorrectUTF8Decoding(bytes.fromhex(seq), res,
19351932
'invalid continuation byte')
19361933

19371934
def test_invalid_cb_for_4bytes_seq(self):
@@ -2010,7 +2007,7 @@ def test_invalid_cb_for_4bytes_seq(self):
20102007
('F4 8F BF C0', FFFDx2), ('F4 8F BF FF', FFFDx2)
20112008
]
20122009
for seq, res in sequences:
2013-
self.assertCorrectUTF8Decoding(self.to_bytestring(seq), res,
2010+
self.assertCorrectUTF8Decoding(bytes.fromhex(seq), res,
20142011
'invalid continuation byte')
20152012

20162013
def test_codecs_idna(self):

Lib/urllib/parse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ def unquote_to_bytes(string):
574574
# if the function is never called
575575
global _hextobyte
576576
if _hextobyte is None:
577-
_hextobyte = {(a + b).encode(): bytes([int(a + b, 16)])
577+
_hextobyte = {(a + b).encode(): bytes.fromhex(a + b)
578578
for a in _hexdig for b in _hexdig}
579579
for item in bits[1:]:
580580
try:

0 commit comments

Comments
 (0)