Skip to content

Commit ad5cc10

Browse files
committed
Apply suggestions from @picnixz in #129844
1 parent 8b6da42 commit ad5cc10

File tree

5 files changed

+23
-10
lines changed

5 files changed

+23
-10
lines changed

Doc/library/stdtypes.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2738,7 +2738,7 @@ data and are closely related to string objects in a variety of other ways.
27382738
:meth:`bytes.fromhex` now skips all ASCII whitespace in the string,
27392739
not just spaces.
27402740

2741-
.. versionchanged:: 3.14
2741+
.. versionchanged:: next
27422742
:meth:`bytes.fromhex` now accepts an ASCII :class:`bytes` object as
27432743
input.
27442744

@@ -2828,7 +2828,7 @@ objects.
28282828
:meth:`bytearray.fromhex` now skips all ASCII whitespace in the string,
28292829
not just spaces.
28302830

2831-
.. versionchanged:: 3.14
2831+
.. versionchanged:: next
28322832
:meth:`bytearray.fromhex` now accepts an ASCII :class:`bytes` object as
28332833
input.
28342834

Doc/whatsnew/3.14.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,10 @@ Other language changes
278278
making it a :term:`generic type`.
279279
(Contributed by Brian Schubert in :gh:`126012`.)
280280

281+
* The :func:`bytes.fromhex` and :func:`bytearray.fromhex` methods now accept
282+
ASCII :class:`bytes` and :class:`bytearray` objects.
283+
(Contributed by Daniel Pope in :gh:`129349`.)
284+
281285
* ``\B`` in :mod:`regular expression <re>` now matches empty input string.
282286
Now it is always the opposite of ``\b``.
283287
(Contributed by Serhiy Storchaka in :gh:`124130`.)

Lib/test/test_bytes.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@ def test_fromhex(self):
446446

447447
# check that ASCII whitespace is ignored
448448
self.assertEqual(self.type2test.fromhex(' 1A\n2B\t30\v'), b)
449+
self.assertEqual(self.type2test.fromhex(b' 1A\n2B\t30\v'), b)
449450
for c in "\x09\x0A\x0B\x0C\x0D\x20":
450451
self.assertEqual(self.type2test.fromhex(c), self.type2test())
451452
for c in "\x1C\x1D\x1E\x1F\x85\xa0\u2000\u2002\u2028":
@@ -457,6 +458,10 @@ def test_fromhex(self):
457458
self.type2test.fromhex(bytearray(b' 012abc')),
458459
b'\x01\x2a\xbc',
459460
)
461+
# Invalid bytes are rejected
462+
for u8 in b"\0\x1C\x1D\x1E\x1F\x85\xa0":
463+
b = bytes([30, 31, u8])
464+
self.assertRaises(ValueError, self.type2test.fromhex, b)
460465

461466
self.assertEqual(self.type2test.fromhex('0000'), b'\0\0')
462467
self.assertRaises(ValueError, self.type2test.fromhex, 'a')
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
``bytes.fromhex()``/``bytearray.fromhex()`` now accept ASCII ``bytes``.
1+
:meth:`bytes.fromhex` and :meth:`bytearray.fromhex()` now accepts ASCII
2+
:class:`bytes` objects.

Objects/bytesobject.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2535,16 +2535,19 @@ _PyBytes_FromHex(PyObject *string, int use_bytearray)
25352535

25362536
assert(PyUnicode_KIND(string) == PyUnicode_1BYTE_KIND);
25372537
str = start = PyUnicode_1BYTE_DATA(string);
2538-
} else if (PyBytes_Check(string)) {
2538+
}
2539+
else if (PyBytes_Check(string)) {
25392540
hexlen = PyBytes_GET_SIZE(string);
2540-
str = start = (Py_UCS1 *) PyBytes_AS_STRING(string);
2541-
} else if (PyByteArray_Check(string)) {
2541+
str = start = (Py_UCS1 *)PyBytes_AS_STRING(string);
2542+
}
2543+
else if (PyByteArray_Check(string)) {
25422544
hexlen = PyByteArray_GET_SIZE(string);
2543-
str = start = (Py_UCS1 *) PyByteArray_AS_STRING(string);
2544-
} else {
2545+
str = start = (Py_UCS1 *)PyByteArray_AS_STRING(string);
2546+
}
2547+
else {
25452548
PyErr_Format(PyExc_TypeError,
2546-
"fromhex() argument must be str or bytes, not %s",
2547-
Py_TYPE(string)->tp_name);
2549+
"fromhex() argument must be str or bytes, not %T",
2550+
string);
25482551
return NULL;
25492552
}
25502553

0 commit comments

Comments
 (0)