Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Clean up interpreter ID conversions.
  • Loading branch information
ericsnowcurrently committed Mar 19, 2024
commit d299396293cc28f40f14445e56b365253a7cebe8
4 changes: 4 additions & 0 deletions Include/cpython/interpreteridobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ PyAPI_DATA(PyTypeObject) PyInterpreterID_Type;
PyAPI_FUNC(PyObject *) PyInterpreterID_New(int64_t);
PyAPI_FUNC(PyObject *) PyInterpreterState_GetIDObject(PyInterpreterState *);
PyAPI_FUNC(PyInterpreterState *) PyInterpreterID_LookUp(PyObject *);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that in 3.12 this was a private API (leading _) but in 3.13 it lost the _. It looks like you're going back to private. What about the handful of definitions above this line? Are any of them meant to be public?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good question. They needn't be public at this point and it turns out interpreteridobject.h was never added to Python.h, so there shouldn't be a problem moving them. I'll do that here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On second thought, I'm going to remove the InterpreterID type, including this header file, in a separate PR. (I made some changes a few months ago that made the class unnecessary. It can be removed with a couple small tweaks.)


#ifndef Py_BUILD_CORE
extern int64_t _PyInterpreterID_ObjectToID(PyObject *);
#endif
80 changes: 3 additions & 77 deletions Modules/_xxsubinterpretersmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,84 +35,9 @@ _get_current_interp(void)
return PyInterpreterState_Get();
}

static int64_t
pylong_to_interpid(PyObject *idobj)
{
assert(PyLong_CheckExact(idobj));

if (_PyLong_IsNegative((PyLongObject *)idobj)) {
PyErr_Format(PyExc_ValueError,
"interpreter ID must be a non-negative int, got %R",
idobj);
return -1;
}

int overflow;
long long id = PyLong_AsLongLongAndOverflow(idobj, &overflow);
if (id == -1) {
if (!overflow) {
assert(PyErr_Occurred());
return -1;
}
assert(!PyErr_Occurred());
// For now, we don't worry about if LLONG_MAX < INT64_MAX.
goto bad_id;
}
#if LLONG_MAX > INT64_MAX
if (id > INT64_MAX) {
goto bad_id;
}
#endif
return (int64_t)id;

bad_id:
PyErr_Format(PyExc_RuntimeError,
"unrecognized interpreter ID %O", idobj);
return -1;
}

static int64_t
convert_interpid_obj(PyObject *arg)
{
int64_t id = -1;
if (_PyIndex_Check(arg)) {
PyObject *idobj = PyNumber_Long(arg);
if (idobj == NULL) {
return -1;
}
id = pylong_to_interpid(idobj);
Py_DECREF(idobj);
if (id < 0) {
return -1;
}
}
else {
PyErr_Format(PyExc_TypeError,
"interpreter ID must be an int, got %.100s",
Py_TYPE(arg)->tp_name);
return -1;
}
return id;
}

static PyInterpreterState *
look_up_interp(PyObject *arg)
{
int64_t id = convert_interpid_obj(arg);
if (id < 0) {
return NULL;
}
return _PyInterpreterState_LookUpID(id);
}
#define look_up_interp PyInterpreterID_LookUp


static PyObject *
interpid_to_pylong(int64_t id)
{
assert(id < LLONG_MAX);
return PyLong_FromLongLong(id);
}

static PyObject *
get_interpid_obj(PyInterpreterState *interp)
{
Expand All @@ -123,7 +48,8 @@ get_interpid_obj(PyInterpreterState *interp)
if (id < 0) {
return NULL;
}
return interpid_to_pylong(id);
assert(id < LLONG_MAX);
return PyLong_FromLongLong(id);
}

static PyObject *
Expand Down
67 changes: 41 additions & 26 deletions Objects/interpreteridobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,43 +42,58 @@ newinterpid(PyTypeObject *cls, int64_t id, int force)
return self;
}

static int
interp_id_converter(PyObject *arg, void *ptr)
int64_t
_PyInterpreterID_ObjectToID(PyObject *idobj)
{
int64_t id;
if (PyObject_TypeCheck(arg, &PyInterpreterID_Type)) {
id = ((interpid *)arg)->id;
}
else if (_PyIndex_Check(arg)) {
id = PyLong_AsLongLong(arg);
if (id == -1 && PyErr_Occurred()) {
return 0;
}
if (id < 0) {
PyErr_Format(PyExc_ValueError,
"interpreter ID must be a non-negative int, got %R", arg);
return 0;
}
if (PyObject_TypeCheck(idobj, &PyInterpreterID_Type)) {
assert(((interpid *)idobj)->id >= 0);
return ((interpid *)idobj)->id;
}
else {

if (!_PyIndex_Check(idobj)) {
PyErr_Format(PyExc_TypeError,
"interpreter ID must be an int, got %.100s",
Py_TYPE(arg)->tp_name);
return 0;
Py_TYPE(idobj)->tp_name);
return -1;
}

// This may raise OverflowError.
// For now, we don't worry about if LLONG_MAX < INT64_MAX.
long long id = PyLong_AsLongLong(idobj);
if (id == -1 && PyErr_Occurred()) {
return -1;
}

if (id < 0) {
PyErr_Format(PyExc_ValueError,
"interpreter ID must be a non-negative int, got %R",
idobj);
return -1;
}
#if LLONG_MAX > INT64_MAX
else if (id > INT64_MAX) {
PyErr_SetString(PyExc_OverflowError, "int too big to convert");
return -1;
}
#endif
else {
return (int64_t)id;
}
*(int64_t *)ptr = id;
return 1;
}

static PyObject *
interpid_new(PyTypeObject *cls, PyObject *args, PyObject *kwds)
{
static char *kwlist[] = {"id", "force", NULL};
int64_t id;
PyObject *idobj;
int force = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwds,
"O&|$p:InterpreterID.__init__", kwlist,
interp_id_converter, &id, &force)) {
"O|$p:InterpreterID.__init__", kwlist,
&idobj, &force)) {
return NULL;
}
int64_t id = _PyInterpreterID_ObjectToID(idobj);
if (id < 0) {
return NULL;
}

Expand Down Expand Up @@ -286,8 +301,8 @@ PyInterpreterState_GetIDObject(PyInterpreterState *interp)
PyInterpreterState *
PyInterpreterID_LookUp(PyObject *requested_id)
{
int64_t id;
if (!interp_id_converter(requested_id, &id)) {
int64_t id = _PyInterpreterID_ObjectToID(requested_id);
if (id < 0) {
return NULL;
}
return _PyInterpreterState_LookUpID(id);
Expand Down