Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Apply suggestions from @picnixz in #129844
  • Loading branch information
lordmauve committed Mar 12, 2025
commit 76d28ce24bf68ebbcac722e367559e71efe3dc61
4 changes: 2 additions & 2 deletions Doc/library/stdtypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2744,7 +2744,7 @@ data and are closely related to string objects in a variety of other ways.
:meth:`bytes.fromhex` now skips all ASCII whitespace in the string,
not just spaces.

.. versionchanged:: 3.14
.. versionchanged:: next
:meth:`bytes.fromhex` now accepts an ASCII :class:`bytes` object as
Comment thread
lordmauve marked this conversation as resolved.
Outdated
input.

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

.. versionchanged:: 3.14
.. versionchanged:: next
:meth:`bytearray.fromhex` now accepts an ASCII :class:`bytes` object as
Comment thread
lordmauve marked this conversation as resolved.
Outdated
input.

Expand Down
4 changes: 4 additions & 0 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,10 @@ Other language changes
(with :func:`format` or :ref:`f-strings`).
(Contrubuted by Sergey B Kirpichev in :gh:`87790`.)

* The :func:`bytes.fromhex` and :func:`bytearray.fromhex` methods now accept
ASCII :class:`bytes` and :class:`bytearray` objects.
(Contributed by Daniel Pope in :gh:`129349`.)

* ``\B`` in :mod:`regular expression <re>` now matches empty input string.
Now it is always the opposite of ``\b``.
(Contributed by Serhiy Storchaka in :gh:`124130`.)
Expand Down
5 changes: 5 additions & 0 deletions Lib/test/test_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ def test_fromhex(self):

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

self.assertEqual(self.type2test.fromhex('0000'), b'\0\0')
self.assertRaises(ValueError, self.type2test.fromhex, 'a')
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
``bytes.fromhex()``/``bytearray.fromhex()`` now accept ASCII ``bytes``.
:meth:`bytes.fromhex` and :meth:`bytearray.fromhex()` now accepts ASCII
Comment thread
lordmauve marked this conversation as resolved.
Outdated
:class:`bytes` objects.
Comment thread
lordmauve marked this conversation as resolved.
Outdated
17 changes: 10 additions & 7 deletions Objects/bytesobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2535,16 +2535,19 @@ _PyBytes_FromHex(PyObject *string, int use_bytearray)

assert(PyUnicode_KIND(string) == PyUnicode_1BYTE_KIND);
str = start = PyUnicode_1BYTE_DATA(string);
} else if (PyBytes_Check(string)) {
}
else if (PyBytes_Check(string)) {
hexlen = PyBytes_GET_SIZE(string);
str = start = (Py_UCS1 *) PyBytes_AS_STRING(string);
} else if (PyByteArray_Check(string)) {
str = start = (Py_UCS1 *)PyBytes_AS_STRING(string);
}
else if (PyByteArray_Check(string)) {
hexlen = PyByteArray_GET_SIZE(string);
str = start = (Py_UCS1 *) PyByteArray_AS_STRING(string);
} else {
str = start = (Py_UCS1 *)PyByteArray_AS_STRING(string);
}
else {
PyErr_Format(PyExc_TypeError,
"fromhex() argument must be str or bytes, not %s",
Py_TYPE(string)->tp_name);
"fromhex() argument must be str or bytes, not %T",
string);
return NULL;
}
Comment thread
lordmauve marked this conversation as resolved.

Expand Down