Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
28 changes: 12 additions & 16 deletions Include/internal/pycore_strhex.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,24 @@ extern "C" {

// Returns a str() containing the hex representation of argbuf.
// Export for '_hashlib' shared extension.
PyAPI_FUNC(PyObject*) _Py_strhex(const
char* argbuf,
const Py_ssize_t arglen);
PyAPI_FUNC(PyObject *) _Py_strhex(const char *argbuf, Py_ssize_t arglen);

// Returns a bytes() containing the ASCII hex representation of argbuf.
extern PyObject* _Py_strhex_bytes(
const char* argbuf,
const Py_ssize_t arglen);
extern PyObject *_Py_strhex_bytes(const char *argbuf, Py_ssize_t arglen);

// These variants include support for a separator between every N bytes:
extern PyObject* _Py_strhex_with_sep(
const char* argbuf,
const Py_ssize_t arglen,
PyObject* sep,
const int bytes_per_group);
extern PyObject *_Py_strhex_with_sep(
const char *argbuf,
Py_ssize_t arglen,
PyObject *sep,
Py_ssize_t bytes_per_group);

// Export for 'binascii' shared extension
PyAPI_FUNC(PyObject*) _Py_strhex_bytes_with_sep(
const char* argbuf,
const Py_ssize_t arglen,
PyObject* sep,
const int bytes_per_group);
PyAPI_FUNC(PyObject *) _Py_strhex_bytes_with_sep(
const char *argbuf,
Py_ssize_t arglen,
PyObject *sep,
Py_ssize_t bytes_per_group);

#ifdef __cplusplus
}
Expand Down
6 changes: 2 additions & 4 deletions Lib/test/test_base64.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,9 @@ def _common_test_wrapcol(self, func, data):
eq(func(data, wrapcol=80), expected)
eq(func(b'', wrapcol=0), func(b''))
eq(func(b'', wrapcol=1), func(b''))
if func is not base64.b16encode:
eq(func(data, wrapcol=sys.maxsize), expected)
eq(func(data, wrapcol=sys.maxsize), expected)
if check_impl_detail():
if func is not base64.b16encode:
eq(func(data, wrapcol=sys.maxsize*2), expected)
eq(func(data, wrapcol=sys.maxsize*2), expected)
with self.assertRaises(OverflowError):
func(data, wrapcol=2**1000)
with self.assertRaises(ValueError):
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,10 +551,10 @@ def test_hex_separator_basics(self):
self.assertEqual(three_bytes.hex('*', -2), 'b901*ef')
self.assertEqual(three_bytes.hex(sep=':', bytes_per_sep=2), 'b9:01ef')
self.assertEqual(three_bytes.hex(sep='*', bytes_per_sep=-2), 'b901*ef')
for bytes_per_sep in 3, -3, 2**31-1, -(2**31-1):
for bytes_per_sep in 3, -3, sys.maxsize, -sys.maxsize:
with self.subTest(bytes_per_sep=bytes_per_sep):
self.assertEqual(three_bytes.hex(':', bytes_per_sep), 'b901ef')
for bytes_per_sep in 2**31, -2**31, 2**1000, -2**1000:
for bytes_per_sep in sys.maxsize+1, -sys.maxsize-1, 2**1000, -2**1000:
with self.subTest(bytes_per_sep=bytes_per_sep):
try:
self.assertEqual(three_bytes.hex(':', bytes_per_sep), 'b901ef')
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_memoryview.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,10 +718,10 @@ def test_memoryview_hex_separator(self):
self.assertEqual(m2.hex(':', -2), '6564:6362:61')
self.assertEqual(m2.hex(sep=':', bytes_per_sep=2), '65:6463:6261')
self.assertEqual(m2.hex(sep=':', bytes_per_sep=-2), '6564:6362:61')
for bytes_per_sep in 5, -5, 2**31-1, -(2**31-1):
for bytes_per_sep in 5, -5, sys.maxsize, -sys.maxsize:
with self.subTest(bytes_per_sep=bytes_per_sep):
self.assertEqual(m2.hex(':', bytes_per_sep), '6564636261')
for bytes_per_sep in 2**31, -2**31, 2**1000, -2**1000:
for bytes_per_sep in sys.maxsize+1, -sys.maxsize-1, 2**1000, -2**1000:
with self.subTest(bytes_per_sep=bytes_per_sep):
try:
self.assertEqual(m2.hex(':', bytes_per_sep), '6564636261')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Accepted range for the *bytes_per_sep* argument of :meth:`bytes.hex`,
:meth:`bytearray.hex`, :meth:`memoryview.hex`, and :func:`binascii.b2a_hex`
is now increased, so passing ``sys.maxsize`` and ``-sys.maxsize`` is now
valid.
10 changes: 5 additions & 5 deletions Modules/binascii.c
Original file line number Diff line number Diff line change
Expand Up @@ -2067,7 +2067,7 @@ binascii.b2a_hex
data: Py_buffer
sep: object = NULL
An optional single character or byte to separate hex bytes.
bytes_per_sep: int = 1
bytes_per_sep: Py_ssize_t = 1
How many bytes between separators. Positive values count from the
right, negative values count from the left.

Expand All @@ -2087,8 +2087,8 @@ b'b9_01ef'

static PyObject *
binascii_b2a_hex_impl(PyObject *module, Py_buffer *data, PyObject *sep,
int bytes_per_sep)
/*[clinic end generated code: output=a26937946a81d2c7 input=ec0ade6ba2e43543]*/
Py_ssize_t bytes_per_sep)
/*[clinic end generated code: output=7d703f866f74a813 input=6a1606f01a87118c]*/
{
return _Py_strhex_bytes_with_sep((const char *)data->buf, data->len,
sep, bytes_per_sep);
Expand All @@ -2105,8 +2105,8 @@ available as "b2a_hex()".

static PyObject *
binascii_hexlify_impl(PyObject *module, Py_buffer *data, PyObject *sep,
int bytes_per_sep)
/*[clinic end generated code: output=d12aa1b001b15199 input=bc317bd4e241f76b]*/
Py_ssize_t bytes_per_sep)
/*[clinic end generated code: output=b99b3b39d234a3d4 input=bc317bd4e241f76b]*/
{
return _Py_strhex_bytes_with_sep((const char *)data->buf, data->len,
sep, bytes_per_sep);
Expand Down
39 changes: 28 additions & 11 deletions Modules/clinic/binascii.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions Objects/bytearrayobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2640,7 +2640,7 @@ bytearray.hex

sep: object = NULL
An optional single character or byte to separate hex bytes.
bytes_per_sep: int = 1
bytes_per_sep: Py_ssize_t = 1
How many bytes between separators. Positive values count from the
right, negative values count from the left.

Expand All @@ -2659,8 +2659,9 @@ Create a string of hexadecimal numbers from a bytearray object.
[clinic start generated code]*/

static PyObject *
bytearray_hex_impl(PyByteArrayObject *self, PyObject *sep, int bytes_per_sep)
/*[clinic end generated code: output=29c4e5ef72c565a0 input=7784107de7048873]*/
bytearray_hex_impl(PyByteArrayObject *self, PyObject *sep,
Py_ssize_t bytes_per_sep)
/*[clinic end generated code: output=c9563921aff1262b input=d2b23ef057cfcad5]*/
{
char* argbuf = PyByteArray_AS_STRING(self);
Py_ssize_t arglen = PyByteArray_GET_SIZE(self);
Expand Down
6 changes: 3 additions & 3 deletions Objects/bytesobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2743,7 +2743,7 @@ bytes.hex

sep: object = NULL
An optional single character or byte to separate hex bytes.
bytes_per_sep: int = 1
bytes_per_sep: Py_ssize_t = 1
How many bytes between separators. Positive values count from the
right, negative values count from the left.

Expand All @@ -2762,8 +2762,8 @@ Create a string of hexadecimal numbers from a bytes object.
[clinic start generated code]*/

static PyObject *
bytes_hex_impl(PyBytesObject *self, PyObject *sep, int bytes_per_sep)
/*[clinic end generated code: output=1f134da504064139 input=1a21282b1f1ae595]*/
bytes_hex_impl(PyBytesObject *self, PyObject *sep, Py_ssize_t bytes_per_sep)
/*[clinic end generated code: output=588821f02cb9d8f5 input=bd8eceb755d8230f]*/
{
const char *argbuf = PyBytes_AS_STRING(self);
Py_ssize_t arglen = PyBytes_GET_SIZE(self);
Expand Down
21 changes: 15 additions & 6 deletions Objects/clinic/bytearrayobject.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 14 additions & 6 deletions Objects/clinic/bytesobject.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 15 additions & 6 deletions Objects/clinic/memoryobject.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading