Issue #28927: bytes.fromhex() and bytearray.fromhex() now ignore all ASCII

whitespace, not only spaces.  Patch by Robert Xiao.
This commit is contained in:
Serhiy Storchaka 2016-12-19 18:51:37 +02:00
parent f76df27806
commit dd1da7f74a
5 changed files with 27 additions and 4 deletions

View File

@ -2314,11 +2314,15 @@ the bytes type has an additional class method to read data in that format:
This :class:`bytes` class method returns a bytes object, decoding the
given string object. The string must contain two hexadecimal digits per
byte, with ASCII spaces being ignored.
byte, with ASCII whitespace being ignored.
>>> bytes.fromhex('2Ef0 F1f2 ')
b'.\xf0\xf1\xf2'
.. versionchanged:: 3.7
:meth:`bytes.fromhex` now skips all ASCII whitespace in the string,
not just spaces.
A reverse conversion function exists to transform a bytes object into its
hexadecimal representation.
@ -2382,11 +2386,15 @@ the bytearray type has an additional class method to read data in that format:
This :class:`bytearray` class method returns bytearray object, decoding
the given string object. The string must contain two hexadecimal digits
per byte, with ASCII spaces being ignored.
per byte, with ASCII whitespace being ignored.
>>> bytearray.fromhex('2Ef0 F1f2 ')
bytearray(b'.\xf0\xf1\xf2')
.. versionchanged:: 3.7
:meth:`bytearray.fromhex` now skips all ASCII whitespace in the string,
not just spaces.
A reverse conversion function exists to transform a bytearray object into its
hexadecimal representation.

View File

@ -79,6 +79,10 @@ Other Language Changes
now have more than 255 parameters.
(Contributed by Serhiy Storchaka in :issue:`12844` and :issue:`18896`.)
* :meth:`bytes.fromhex` and :meth:`bytearray.fromhex` now ignore all ASCII
whitespace, not only spaces.
(Contributed by Robert Xiao in :issue:`28927`.)
New Modules
===========

View File

@ -293,6 +293,14 @@ class BaseBytesTest:
b = bytearray([0x1a, 0x2b, 0x30])
self.assertEqual(self.type2test.fromhex('1a2B30'), b)
self.assertEqual(self.type2test.fromhex(' 1A 2B 30 '), b)
# check that ASCII whitespace is ignored
self.assertEqual(self.type2test.fromhex(' 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":
self.assertRaises(ValueError, self.type2test.fromhex, c)
self.assertEqual(self.type2test.fromhex('0000'), b'\0\0')
self.assertRaises(TypeError, self.type2test.fromhex, b'1B')
self.assertRaises(ValueError, self.type2test.fromhex, 'a')

View File

@ -10,6 +10,9 @@ What's New in Python 3.7.0 alpha 1
Core and Builtins
-----------------
- Issue #28927: bytes.fromhex() and bytearray.fromhex() now ignore all ASCII
whitespace, not only spaces. Patch by Robert Xiao.
- Issue #25677: Correct the positioning of the syntax error caret for
indented blocks. Based on patch by Michael Layzell.

View File

@ -2378,10 +2378,10 @@ _PyBytes_FromHex(PyObject *string, int use_bytearray)
end = str + hexlen;
while (str < end) {
/* skip over spaces in the input */
if (*str == ' ') {
if (Py_ISSPACE(*str)) {
do {
str++;
} while (*str == ' ');
} while (Py_ISSPACE(*str));
if (str >= end)
break;
}