diff --git a/Include/internal/pycore_longobject.h b/Include/internal/pycore_longobject.h new file mode 100644 index 00000000000000..62b77516075aa9 --- /dev/null +++ b/Include/internal/pycore_longobject.h @@ -0,0 +1,18 @@ +#ifndef Py_INTERNAL_LONGOBJECT_H +#define Py_INTERNAL_LONGOBJECT_H +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef Py_BUILD_CORE +# error "this header requires Py_BUILD_CORE define" +#endif + +#include "longobject.h" + +PyAPI_FUNC(PyObject *) _PyLong_FromUnsignedChar(unsigned char); + +#ifdef __cplusplus +} +#endif +#endif /* !Py_INTERNAL_LONGOBJECT_H */ diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index 9dd67127b61464..3363694b5840fc 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -2,6 +2,7 @@ #define PY_SSIZE_T_CLEAN #include "Python.h" +#include "pycore_longobject.h" #include "pycore_object.h" #include "pycore_pymem.h" #include "pycore_pystate.h" @@ -387,7 +388,7 @@ bytearray_getitem(PyByteArrayObject *self, Py_ssize_t i) PyErr_SetString(PyExc_IndexError, "bytearray index out of range"); return NULL; } - return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i])); + return _PyLong_FromUnsignedChar((unsigned char)(PyByteArray_AS_STRING(self)[i])); } static PyObject * @@ -406,7 +407,7 @@ bytearray_subscript(PyByteArrayObject *self, PyObject *index) PyErr_SetString(PyExc_IndexError, "bytearray index out of range"); return NULL; } - return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i])); + return _PyLong_FromUnsignedChar((unsigned char)(PyByteArray_AS_STRING(self)[i])); } else if (PySlice_Check(index)) { Py_ssize_t start, stop, step, slicelength, i; @@ -1745,7 +1746,7 @@ bytearray_pop_impl(PyByteArrayObject *self, Py_ssize_t index) if (PyByteArray_Resize((PyObject *)self, n - 1) < 0) return NULL; - return PyLong_FromLong((unsigned char)value); + return _PyLong_FromUnsignedChar((unsigned char)value); } /*[clinic input] @@ -2339,7 +2340,7 @@ bytearrayiter_next(bytesiterobject *it) assert(PyByteArray_Check(seq)); if (it->it_index < PyByteArray_GET_SIZE(seq)) { - item = PyLong_FromLong( + item = _PyLong_FromUnsignedChar( (unsigned char)PyByteArray_AS_STRING(seq)[it->it_index]); if (item != NULL) ++it->it_index; diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c index c4edcca4f76127..d0327282a944f8 100644 --- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -3,6 +3,7 @@ #define PY_SSIZE_T_CLEAN #include "Python.h" +#include "pycore_longobject.h" #include "pycore_object.h" #include "pycore_pymem.h" #include "pycore_pystate.h" @@ -1541,7 +1542,7 @@ bytes_item(PyBytesObject *a, Py_ssize_t i) PyErr_SetString(PyExc_IndexError, "index out of range"); return NULL; } - return PyLong_FromLong((unsigned char)a->ob_sval[i]); + return _PyLong_FromUnsignedChar((unsigned char)a->ob_sval[i]); } static int @@ -1664,7 +1665,7 @@ bytes_subscript(PyBytesObject* self, PyObject* item) "index out of range"); return NULL; } - return PyLong_FromLong((unsigned char)self->ob_sval[i]); + return _PyLong_FromUnsignedChar((unsigned char)self->ob_sval[i]); } else if (PySlice_Check(item)) { Py_ssize_t start, stop, step, slicelength, i; @@ -3091,7 +3092,7 @@ striter_next(striterobject *it) assert(PyBytes_Check(seq)); if (it->it_index < PyBytes_GET_SIZE(seq)) { - item = PyLong_FromLong( + item = _PyLong_FromUnsignedChar( (unsigned char)seq->ob_sval[it->it_index]); if (item != NULL) ++it->it_index; diff --git a/Objects/longobject.c b/Objects/longobject.c index 3978f5c4a16730..4c75503a8740f2 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -402,6 +402,12 @@ PyLong_FromUnsignedLong(unsigned long ival) return (PyObject *)v; } +PyObject * +_PyLong_FromUnsignedChar(unsigned char ival) +{ + return PyLong_FromSize_t(ival); +} + /* Create a new int object from a C double */ PyObject *