Skip to content

Commit 473d2a3

Browse files
gh-147944: Increase range of bytes_per_sep (GH-147946)
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 42825e6 commit 473d2a3

File tree

14 files changed

+121
-78
lines changed

14 files changed

+121
-78
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_base64.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,9 @@ def _common_test_wrapcol(self, func, data):
216216
eq(func(data, wrapcol=80), expected)
217217
eq(func(b'', wrapcol=0), func(b''))
218218
eq(func(b'', wrapcol=1), func(b''))
219-
if func is not base64.b16encode:
220-
eq(func(data, wrapcol=sys.maxsize), expected)
219+
eq(func(data, wrapcol=sys.maxsize), expected)
221220
if check_impl_detail():
222-
if func is not base64.b16encode:
223-
eq(func(data, wrapcol=sys.maxsize*2), expected)
221+
eq(func(data, wrapcol=sys.maxsize*2), expected)
224222
with self.assertRaises(OverflowError):
225223
func(data, wrapcol=2**1000)
226224
with self.assertRaises(ValueError):

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
@@ -2067,7 +2067,7 @@ binascii.b2a_hex
20672067
data: Py_buffer
20682068
sep: object = NULL
20692069
An optional single character or byte to separate hex bytes.
2070-
bytes_per_sep: int = 1
2070+
bytes_per_sep: Py_ssize_t = 1
20712071
How many bytes between separators. Positive values count from the
20722072
right, negative values count from the left.
20732073
@@ -2087,8 +2087,8 @@ b'b9_01ef'
20872087

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

21062106
static PyObject *
21072107
binascii_hexlify_impl(PyObject *module, Py_buffer *data, PyObject *sep,
2108-
int bytes_per_sep)
2109-
/*[clinic end generated code: output=d12aa1b001b15199 input=bc317bd4e241f76b]*/
2108+
Py_ssize_t bytes_per_sep)
2109+
/*[clinic end generated code: output=b99b3b39d234a3d4 input=bc317bd4e241f76b]*/
21102110
{
21112111
return _Py_strhex_bytes_with_sep((const char *)data->buf, data->len,
21122112
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
@@ -2640,7 +2640,7 @@ bytearray.hex
26402640
26412641
sep: object = NULL
26422642
An optional single character or byte to separate hex bytes.
2643-
bytes_per_sep: int = 1
2643+
bytes_per_sep: Py_ssize_t = 1
26442644
How many bytes between separators. Positive values count from the
26452645
right, negative values count from the left.
26462646
@@ -2659,8 +2659,9 @@ Create a string of hexadecimal numbers from a bytearray object.
26592659
[clinic start generated code]*/
26602660

26612661
static PyObject *
2662-
bytearray_hex_impl(PyByteArrayObject *self, PyObject *sep, int bytes_per_sep)
2663-
/*[clinic end generated code: output=29c4e5ef72c565a0 input=7784107de7048873]*/
2662+
bytearray_hex_impl(PyByteArrayObject *self, PyObject *sep,
2663+
Py_ssize_t bytes_per_sep)
2664+
/*[clinic end generated code: output=c9563921aff1262b input=d2b23ef057cfcad5]*/
26642665
{
26652666
char* argbuf = PyByteArray_AS_STRING(self);
26662667
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
@@ -2743,7 +2743,7 @@ bytes.hex
27432743
27442744
sep: object = NULL
27452745
An optional single character or byte to separate hex bytes.
2746-
bytes_per_sep: int = 1
2746+
bytes_per_sep: Py_ssize_t = 1
27472747
How many bytes between separators. Positive values count from the
27482748
right, negative values count from the left.
27492749
@@ -2762,8 +2762,8 @@ Create a string of hexadecimal numbers from a bytes object.
27622762
[clinic start generated code]*/
27632763

27642764
static PyObject *
2765-
bytes_hex_impl(PyBytesObject *self, PyObject *sep, int bytes_per_sep)
2766-
/*[clinic end generated code: output=1f134da504064139 input=1a21282b1f1ae595]*/
2765+
bytes_hex_impl(PyBytesObject *self, PyObject *sep, Py_ssize_t bytes_per_sep)
2766+
/*[clinic end generated code: output=588821f02cb9d8f5 input=bd8eceb755d8230f]*/
27672767
{
27682768
const char *argbuf = PyBytes_AS_STRING(self);
27692769
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.

0 commit comments

Comments
 (0)