Skip to content

Commit 113cf3a

Browse files
gh-147944: Increase range of bytes_per_sep
Accepted range for the bytes_per_sep argument of bytes.hex(), bytearray.hex(), memoryview.hex(), and binascii.b2a_hex() is now increased, so passing sys.maxsize and -sys.maxsize is now valid.
1 parent 362145c commit 113cf3a

File tree

13 files changed

+119
-74
lines changed

13 files changed

+119
-74
lines changed

Include/internal/pycore_strhex.h

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,24 @@ extern "C" {
1010

1111
// Returns a str() containing the hex representation of argbuf.
1212
// Export for '_hashlib' shared extension.
13-
PyAPI_FUNC(PyObject*) _Py_strhex(const
14-
char* argbuf,
15-
const Py_ssize_t arglen);
13+
PyAPI_FUNC(PyObject *) _Py_strhex(const char *argbuf, Py_ssize_t arglen);
1614

1715
// Returns a bytes() containing the ASCII hex representation of argbuf.
18-
extern PyObject* _Py_strhex_bytes(
19-
const char* argbuf,
20-
const Py_ssize_t arglen);
16+
extern PyObject *_Py_strhex_bytes(const char *argbuf, Py_ssize_t arglen);
2117

2218
// These variants include support for a separator between every N bytes:
23-
extern PyObject* _Py_strhex_with_sep(
24-
const char* argbuf,
25-
const Py_ssize_t arglen,
26-
PyObject* sep,
27-
const int bytes_per_group);
19+
extern PyObject *_Py_strhex_with_sep(
20+
const char *argbuf,
21+
Py_ssize_t arglen,
22+
PyObject *sep,
23+
Py_ssize_t bytes_per_group);
2824

2925
// Export for 'binascii' shared extension
30-
PyAPI_FUNC(PyObject*) _Py_strhex_bytes_with_sep(
31-
const char* argbuf,
32-
const Py_ssize_t arglen,
33-
PyObject* sep,
34-
const int bytes_per_group);
26+
PyAPI_FUNC(PyObject *) _Py_strhex_bytes_with_sep(
27+
const char *argbuf,
28+
Py_ssize_t arglen,
29+
PyObject *sep,
30+
Py_ssize_t bytes_per_group);
3531

3632
#ifdef __cplusplus
3733
}

Lib/test/test_bytes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -551,10 +551,10 @@ def test_hex_separator_basics(self):
551551
self.assertEqual(three_bytes.hex('*', -2), 'b901*ef')
552552
self.assertEqual(three_bytes.hex(sep=':', bytes_per_sep=2), 'b9:01ef')
553553
self.assertEqual(three_bytes.hex(sep='*', bytes_per_sep=-2), 'b901*ef')
554-
for bytes_per_sep in 3, -3, 2**31-1, -(2**31-1):
554+
for bytes_per_sep in 3, -3, sys.maxsize, -sys.maxsize:
555555
with self.subTest(bytes_per_sep=bytes_per_sep):
556556
self.assertEqual(three_bytes.hex(':', bytes_per_sep), 'b901ef')
557-
for bytes_per_sep in 2**31, -2**31, 2**1000, -2**1000:
557+
for bytes_per_sep in sys.maxsize+1, -sys.maxsize-1, 2**1000, -2**1000:
558558
with self.subTest(bytes_per_sep=bytes_per_sep):
559559
try:
560560
self.assertEqual(three_bytes.hex(':', bytes_per_sep), 'b901ef')

Lib/test/test_memoryview.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -718,10 +718,10 @@ def test_memoryview_hex_separator(self):
718718
self.assertEqual(m2.hex(':', -2), '6564:6362:61')
719719
self.assertEqual(m2.hex(sep=':', bytes_per_sep=2), '65:6463:6261')
720720
self.assertEqual(m2.hex(sep=':', bytes_per_sep=-2), '6564:6362:61')
721-
for bytes_per_sep in 5, -5, 2**31-1, -(2**31-1):
721+
for bytes_per_sep in 5, -5, sys.maxsize, -sys.maxsize:
722722
with self.subTest(bytes_per_sep=bytes_per_sep):
723723
self.assertEqual(m2.hex(':', bytes_per_sep), '6564636261')
724-
for bytes_per_sep in 2**31, -2**31, 2**1000, -2**1000:
724+
for bytes_per_sep in sys.maxsize+1, -sys.maxsize-1, 2**1000, -2**1000:
725725
with self.subTest(bytes_per_sep=bytes_per_sep):
726726
try:
727727
self.assertEqual(m2.hex(':', bytes_per_sep), '6564636261')
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Accepted range for the *bytes_per_sep* argument of :meth:`bytes.hex`,
2+
:meth:`bytearray.hex`, :meth:`memoryview.hex`, and :func:`binascii.b2a_hex`
3+
is now increased, so passing ``sys.maxsize`` and ``-sys.maxsize`` is now
4+
valid.

Modules/binascii.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2010,7 +2010,7 @@ binascii.b2a_hex
20102010
data: Py_buffer
20112011
sep: object = NULL
20122012
An optional single character or byte to separate hex bytes.
2013-
bytes_per_sep: int = 1
2013+
bytes_per_sep: Py_ssize_t = 1
20142014
How many bytes between separators. Positive values count from the
20152015
right, negative values count from the left.
20162016
@@ -2030,8 +2030,8 @@ b'b9_01ef'
20302030

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

20492049
static PyObject *
20502050
binascii_hexlify_impl(PyObject *module, Py_buffer *data, PyObject *sep,
2051-
int bytes_per_sep)
2052-
/*[clinic end generated code: output=d12aa1b001b15199 input=bc317bd4e241f76b]*/
2051+
Py_ssize_t bytes_per_sep)
2052+
/*[clinic end generated code: output=b99b3b39d234a3d4 input=bc317bd4e241f76b]*/
20532053
{
20542054
return _Py_strhex_bytes_with_sep((const char *)data->buf, data->len,
20552055
sep, bytes_per_sep);

Modules/clinic/binascii.c.h

Lines changed: 28 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Objects/bytearrayobject.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2641,7 +2641,7 @@ bytearray.hex
26412641
26422642
sep: object = NULL
26432643
An optional single character or byte to separate hex bytes.
2644-
bytes_per_sep: int = 1
2644+
bytes_per_sep: Py_ssize_t = 1
26452645
How many bytes between separators. Positive values count from the
26462646
right, negative values count from the left.
26472647
@@ -2660,8 +2660,9 @@ Create a string of hexadecimal numbers from a bytearray object.
26602660
[clinic start generated code]*/
26612661

26622662
static PyObject *
2663-
bytearray_hex_impl(PyByteArrayObject *self, PyObject *sep, int bytes_per_sep)
2664-
/*[clinic end generated code: output=29c4e5ef72c565a0 input=7784107de7048873]*/
2663+
bytearray_hex_impl(PyByteArrayObject *self, PyObject *sep,
2664+
Py_ssize_t bytes_per_sep)
2665+
/*[clinic end generated code: output=c9563921aff1262b input=d2b23ef057cfcad5]*/
26652666
{
26662667
char* argbuf = PyByteArray_AS_STRING(self);
26672668
Py_ssize_t arglen = PyByteArray_GET_SIZE(self);

Objects/bytesobject.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2744,7 +2744,7 @@ bytes.hex
27442744
27452745
sep: object = NULL
27462746
An optional single character or byte to separate hex bytes.
2747-
bytes_per_sep: int = 1
2747+
bytes_per_sep: Py_ssize_t = 1
27482748
How many bytes between separators. Positive values count from the
27492749
right, negative values count from the left.
27502750
@@ -2763,8 +2763,8 @@ Create a string of hexadecimal numbers from a bytes object.
27632763
[clinic start generated code]*/
27642764

27652765
static PyObject *
2766-
bytes_hex_impl(PyBytesObject *self, PyObject *sep, int bytes_per_sep)
2767-
/*[clinic end generated code: output=1f134da504064139 input=1a21282b1f1ae595]*/
2766+
bytes_hex_impl(PyBytesObject *self, PyObject *sep, Py_ssize_t bytes_per_sep)
2767+
/*[clinic end generated code: output=588821f02cb9d8f5 input=bd8eceb755d8230f]*/
27682768
{
27692769
const char *argbuf = PyBytes_AS_STRING(self);
27702770
Py_ssize_t arglen = PyBytes_GET_SIZE(self);

Objects/clinic/bytearrayobject.c.h

Lines changed: 15 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Objects/clinic/bytesobject.c.h

Lines changed: 14 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)