Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-37837: Add internal _PyLong_FromUnsignedChar() function #15251

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions 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 */
9 changes: 5 additions & 4 deletions Objects/bytearrayobject.c
Expand Up @@ -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"
Expand Down Expand Up @@ -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 *
Expand All @@ -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;
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 4 additions & 3 deletions Objects/bytesobject.c
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions Objects/longobject.c
Expand Up @@ -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 *
Expand Down