// 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
}
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):
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')
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')
--- /dev/null
+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.
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.
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);
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);
# include "pycore_gc.h" // PyGC_Head
# include "pycore_runtime.h" // _Py_ID()
#endif
+#include "pycore_abstract.h" // _PyNumber_Index()
#include "pycore_long.h" // _PyLong_Size_t_Converter()
#include "pycore_modsupport.h" // _PyArg_UnpackKeywords()
static PyObject *
binascii_b2a_hex_impl(PyObject *module, Py_buffer *data, PyObject *sep,
- int bytes_per_sep);
+ Py_ssize_t bytes_per_sep);
static PyObject *
binascii_b2a_hex(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Py_buffer data = {NULL, NULL};
PyObject *sep = NULL;
- int bytes_per_sep = 1;
+ Py_ssize_t bytes_per_sep = 1;
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
/*minpos*/ 1, /*maxpos*/ 3, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
goto skip_optional_pos;
}
}
- bytes_per_sep = PyLong_AsInt(args[2]);
- if (bytes_per_sep == -1 && PyErr_Occurred()) {
- goto exit;
+ {
+ Py_ssize_t ival = -1;
+ PyObject *iobj = _PyNumber_Index(args[2]);
+ if (iobj != NULL) {
+ ival = PyLong_AsSsize_t(iobj);
+ Py_DECREF(iobj);
+ }
+ if (ival == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+ bytes_per_sep = ival;
}
skip_optional_pos:
return_value = binascii_b2a_hex_impl(module, &data, sep, bytes_per_sep);
static PyObject *
binascii_hexlify_impl(PyObject *module, Py_buffer *data, PyObject *sep,
- int bytes_per_sep);
+ Py_ssize_t bytes_per_sep);
static PyObject *
binascii_hexlify(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Py_buffer data = {NULL, NULL};
PyObject *sep = NULL;
- int bytes_per_sep = 1;
+ Py_ssize_t bytes_per_sep = 1;
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
/*minpos*/ 1, /*maxpos*/ 3, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
goto skip_optional_pos;
}
}
- bytes_per_sep = PyLong_AsInt(args[2]);
- if (bytes_per_sep == -1 && PyErr_Occurred()) {
- goto exit;
+ {
+ Py_ssize_t ival = -1;
+ PyObject *iobj = _PyNumber_Index(args[2]);
+ if (iobj != NULL) {
+ ival = PyLong_AsSsize_t(iobj);
+ Py_DECREF(iobj);
+ }
+ if (ival == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+ bytes_per_sep = ival;
}
skip_optional_pos:
return_value = binascii_hexlify_impl(module, &data, sep, bytes_per_sep);
return return_value;
}
-/*[clinic end generated code: output=7afd570a9d5a3627 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=197a0f70aa392d39 input=a9049054013a1b77]*/
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.
[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);
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.
[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);
{"hex", _PyCFunction_CAST(bytearray_hex), METH_FASTCALL|METH_KEYWORDS, bytearray_hex__doc__},
static PyObject *
-bytearray_hex_impl(PyByteArrayObject *self, PyObject *sep, int bytes_per_sep);
+bytearray_hex_impl(PyByteArrayObject *self, PyObject *sep,
+ Py_ssize_t bytes_per_sep);
static PyObject *
bytearray_hex(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
PyObject *argsbuf[2];
Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
PyObject *sep = NULL;
- int bytes_per_sep = 1;
+ Py_ssize_t bytes_per_sep = 1;
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
/*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
goto skip_optional_pos;
}
}
- bytes_per_sep = PyLong_AsInt(args[1]);
- if (bytes_per_sep == -1 && PyErr_Occurred()) {
- goto exit;
+ {
+ Py_ssize_t ival = -1;
+ PyObject *iobj = _PyNumber_Index(args[1]);
+ if (iobj != NULL) {
+ ival = PyLong_AsSsize_t(iobj);
+ Py_DECREF(iobj);
+ }
+ if (ival == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+ bytes_per_sep = ival;
}
skip_optional_pos:
Py_BEGIN_CRITICAL_SECTION(self);
{
return bytearray_sizeof_impl((PyByteArrayObject *)self);
}
-/*[clinic end generated code: output=d4976faf6731b8da input=a9049054013a1b77]*/
+/*[clinic end generated code: output=2cacb323147202b9 input=a9049054013a1b77]*/
{"hex", _PyCFunction_CAST(bytes_hex), METH_FASTCALL|METH_KEYWORDS, bytes_hex__doc__},
static PyObject *
-bytes_hex_impl(PyBytesObject *self, PyObject *sep, int bytes_per_sep);
+bytes_hex_impl(PyBytesObject *self, PyObject *sep, Py_ssize_t bytes_per_sep);
static PyObject *
bytes_hex(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
PyObject *argsbuf[2];
Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
PyObject *sep = NULL;
- int bytes_per_sep = 1;
+ Py_ssize_t bytes_per_sep = 1;
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
/*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
goto skip_optional_pos;
}
}
- bytes_per_sep = PyLong_AsInt(args[1]);
- if (bytes_per_sep == -1 && PyErr_Occurred()) {
- goto exit;
+ {
+ Py_ssize_t ival = -1;
+ PyObject *iobj = _PyNumber_Index(args[1]);
+ if (iobj != NULL) {
+ ival = PyLong_AsSsize_t(iobj);
+ Py_DECREF(iobj);
+ }
+ if (ival == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+ bytes_per_sep = ival;
}
skip_optional_pos:
return_value = bytes_hex_impl((PyBytesObject *)self, sep, bytes_per_sep);
exit:
return return_value;
}
-/*[clinic end generated code: output=5675f7008a84ce6d input=a9049054013a1b77]*/
+/*[clinic end generated code: output=b252801ff04a89b3 input=a9049054013a1b77]*/
# include "pycore_gc.h" // PyGC_Head
# include "pycore_runtime.h" // _Py_ID()
#endif
+#include "pycore_abstract.h" // _PyNumber_Index()
#include "pycore_modsupport.h" // _PyArg_UnpackKeywords()
PyDoc_STRVAR(memoryview__doc__,
static PyObject *
memoryview_hex_impl(PyMemoryViewObject *self, PyObject *sep,
- int bytes_per_sep);
+ Py_ssize_t bytes_per_sep);
static PyObject *
memoryview_hex(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
PyObject *argsbuf[2];
Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
PyObject *sep = NULL;
- int bytes_per_sep = 1;
+ Py_ssize_t bytes_per_sep = 1;
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
/*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
goto skip_optional_pos;
}
}
- bytes_per_sep = PyLong_AsInt(args[1]);
- if (bytes_per_sep == -1 && PyErr_Occurred()) {
- goto exit;
+ {
+ Py_ssize_t ival = -1;
+ PyObject *iobj = _PyNumber_Index(args[1]);
+ if (iobj != NULL) {
+ ival = PyLong_AsSsize_t(iobj);
+ Py_DECREF(iobj);
+ }
+ if (ival == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+ bytes_per_sep = ival;
}
skip_optional_pos:
return_value = memoryview_hex_impl((PyMemoryViewObject *)self, sep, bytes_per_sep);
exit:
return return_value;
}
-/*[clinic end generated code: output=154f4c04263ccb24 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=348b6ddb98a1f412 input=a9049054013a1b77]*/
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.
static PyObject *
memoryview_hex_impl(PyMemoryViewObject *self, PyObject *sep,
- int bytes_per_sep)
-/*[clinic end generated code: output=430ca760f94f3ca7 input=539f6a3a5fb56946]*/
+ Py_ssize_t bytes_per_sep)
+/*[clinic end generated code: output=c9bb00c7a8e86056 input=dc48a56ed3b058ae]*/
{
Py_buffer *src = VIEW_ADDR(self);
#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);
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. */
/* 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;
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);
}