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.
This commit is contained in:
Serhiy Storchaka 2026-04-01 11:33:30 +03:00 committed by GitHub
parent 42825e6387
commit 473d2a35ce
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 121 additions and 78 deletions

View file

@ -111,9 +111,10 @@ _Py_hexlify_simd(const unsigned char *src, Py_UCS1 *dst, Py_ssize_t len)
#endif /* HAVE_EFFICIENT_BUILTIN_SHUFFLEVECTOR */
static PyObject *_Py_strhex_impl(const char* argbuf, const Py_ssize_t arglen,
PyObject* sep, int bytes_per_sep_group,
const int return_bytes)
static PyObject *
_Py_strhex_impl(const char* argbuf, Py_ssize_t arglen,
PyObject* sep, Py_ssize_t bytes_per_sep_group,
int return_bytes)
{
assert(arglen >= 0);
@ -149,7 +150,7 @@ static PyObject *_Py_strhex_impl(const char* argbuf, const Py_ssize_t arglen,
else {
bytes_per_sep_group = 0;
}
unsigned int abs_bytes_per_sep = _Py_ABS_CAST(unsigned int, bytes_per_sep_group);
size_t abs_bytes_per_sep = _Py_ABS_CAST(size_t, bytes_per_sep_group);
Py_ssize_t resultlen = 0;
if (bytes_per_sep_group && arglen > 0) {
/* How many sep characters we'll be inserting. */
@ -203,7 +204,7 @@ static PyObject *_Py_strhex_impl(const char* argbuf, const Py_ssize_t arglen,
/* The number of complete chunk+sep periods */
Py_ssize_t chunks = (arglen - 1) / abs_bytes_per_sep;
Py_ssize_t chunk;
unsigned int k;
size_t k;
if (bytes_per_sep_group < 0) {
i = j = 0;
@ -251,30 +252,30 @@ static PyObject *_Py_strhex_impl(const char* argbuf, const Py_ssize_t arglen,
return retval;
}
PyObject * _Py_strhex(const char* argbuf, const Py_ssize_t arglen)
PyObject * _Py_strhex(const char* argbuf, Py_ssize_t arglen)
{
return _Py_strhex_impl(argbuf, arglen, NULL, 0, 0);
}
/* Same as above but returns a bytes() instead of str() to avoid the
* need to decode the str() when bytes are needed. */
PyObject* _Py_strhex_bytes(const char* argbuf, const Py_ssize_t arglen)
PyObject* _Py_strhex_bytes(const char* argbuf, Py_ssize_t arglen)
{
return _Py_strhex_impl(argbuf, arglen, NULL, 0, 1);
}
/* These variants include support for a separator between every N bytes: */
PyObject* _Py_strhex_with_sep(const char* argbuf, const Py_ssize_t arglen,
PyObject* sep, const int bytes_per_group)
PyObject* _Py_strhex_with_sep(const char* argbuf, Py_ssize_t arglen,
PyObject* sep, Py_ssize_t bytes_per_group)
{
return _Py_strhex_impl(argbuf, arglen, sep, bytes_per_group, 0);
}
/* Same as above but returns a bytes() instead of str() to avoid the
* need to decode the str() when bytes are needed. */
PyObject* _Py_strhex_bytes_with_sep(const char* argbuf, const Py_ssize_t arglen,
PyObject* sep, const int bytes_per_group)
PyObject* _Py_strhex_bytes_with_sep(const char* argbuf, Py_ssize_t arglen,
PyObject* sep, Py_ssize_t bytes_per_group)
{
return _Py_strhex_impl(argbuf, arglen, sep, bytes_per_group, 1);
}