Skip to content

Commit f771a5e

Browse files
committed
Apply suggestions from @picnixz in python#129844
1 parent 6be5be7 commit f771a5e

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
@@ -2744,7 +2744,7 @@ data and are closely related to string objects in a variety of other ways.
27442744
:meth:`bytes.fromhex` now skips all ASCII whitespace in the string,
27452745
not just spaces.
27462746

2747-
.. versionchanged:: 3.14
2747+
.. versionchanged:: next
27482748
:meth:`bytes.fromhex` now accepts an ASCII :class:`bytes` object as
27492749
input.
27502750

@@ -2834,7 +2834,7 @@ objects.
28342834
:meth:`bytearray.fromhex` now skips all ASCII whitespace in the string,
28352835
not just spaces.
28362836

2837-
.. versionchanged:: 3.14
2837+
.. versionchanged:: next
28382838
:meth:`bytearray.fromhex` now accepts an ASCII :class:`bytes` object as
28392839
input.
28402840

Doc/whatsnew/3.14.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,10 @@ Other language changes
354354
(with :func:`format` or :ref:`f-strings`).
355355
(Contrubuted by Sergey B Kirpichev in :gh:`87790`.)
356356

357+
* The :func:`bytes.fromhex` and :func:`bytearray.fromhex` methods now accept
358+
ASCII :class:`bytes` and :class:`bytearray` objects.
359+
(Contributed by Daniel Pope in :gh:`129349`.)
360+
357361
* ``\B`` in :mod:`regular expression <re>` now matches empty input string.
358362
Now it is always the opposite of ``\b``.
359363
(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
@@ -450,6 +450,7 @@ def test_fromhex(self):
450450

451451
# check that ASCII whitespace is ignored
452452
self.assertEqual(self.type2test.fromhex(' 1A\n2B\t30\v'), b)
453+
self.assertEqual(self.type2test.fromhex(b' 1A\n2B\t30\v'), b)
453454
for c in "\x09\x0A\x0B\x0C\x0D\x20":
454455
self.assertEqual(self.type2test.fromhex(c), self.type2test())
455456
for c in "\x1C\x1D\x1E\x1F\x85\xa0\u2000\u2002\u2028":
@@ -461,6 +462,10 @@ def test_fromhex(self):
461462
self.type2test.fromhex(bytearray(b' 012abc')),
462463
b'\x01\x2a\xbc',
463464
)
465+
# Invalid bytes are rejected
466+
for u8 in b"\0\x1C\x1D\x1E\x1F\x85\xa0":
467+
b = bytes([30, 31, u8])
468+
self.assertRaises(ValueError, self.type2test.fromhex, b)
464469

465470
self.assertEqual(self.type2test.fromhex('0000'), b'\0\0')
466471
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)