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
Prev Previous commit
Next Next commit
_PyInterpreterID_ObjectToID() -> _PyInterpreterState_ObjectToID()
  • Loading branch information
ericsnowcurrently committed Mar 20, 2024
commit a10fe2aec4628d03d43980c288741a222675b579
1 change: 0 additions & 1 deletion Include/cpython/interpreteridobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ PyAPI_FUNC(PyInterpreterState *) PyInterpreterID_LookUp(PyObject *);

#ifdef Py_BUILD_CORE
extern int64_t _PyInterpreterID_GetID(PyObject *);
extern int64_t _PyInterpreterID_ObjectToID(PyObject *);
#endif
2 changes: 2 additions & 0 deletions Include/internal/pycore_interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,8 @@ _PyInterpreterState_SetFinalizing(PyInterpreterState *interp, PyThreadState *tst
}


extern int64_t _PyInterpreterState_ObjectToID(PyObject *);

// Export for the _xxinterpchannels module.
PyAPI_FUNC(PyInterpreterState *) _PyInterpreterState_LookUpID(int64_t);

Expand Down
46 changes: 3 additions & 43 deletions Objects/interpreteridobject.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/* InterpreterID object */

#include "Python.h"
#include "pycore_abstract.h" // _PyIndex_Check()
#include "pycore_interp.h" // _PyInterpreterState_LookUpID()
#include "pycore_interp.h" // _PyInterpreterState_LookUpID()
#include "interpreteridobject.h"


Expand Down Expand Up @@ -57,45 +56,6 @@ newinterpid(PyTypeObject *cls, int64_t id, int force)
return self;
}

int64_t
_PyInterpreterID_ObjectToID(PyObject *idobj)
{
if (PyObject_TypeCheck(idobj, &PyInterpreterID_Type)) {
assert(((interpid *)idobj)->id >= 0);
return ((interpid *)idobj)->id;
}

if (!_PyIndex_Check(idobj)) {
PyErr_Format(PyExc_TypeError,
"interpreter ID must be an int, got %.100s",
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;
}
}

static PyObject *
interpid_new(PyTypeObject *cls, PyObject *args, PyObject *kwds)
{
Expand All @@ -107,7 +67,7 @@ interpid_new(PyTypeObject *cls, PyObject *args, PyObject *kwds)
&idobj, &force)) {
return NULL;
}
int64_t id = _PyInterpreterID_ObjectToID(idobj);
int64_t id = _PyInterpreterState_ObjectToID(idobj);
if (id < 0) {
return NULL;
}
Expand Down Expand Up @@ -316,7 +276,7 @@ PyInterpreterState_GetIDObject(PyInterpreterState *interp)
PyInterpreterState *
PyInterpreterID_LookUp(PyObject *requested_id)
{
int64_t id = _PyInterpreterID_ObjectToID(requested_id);
int64_t id = _PyInterpreterState_ObjectToID(requested_id);
if (id < 0) {
return NULL;
}
Expand Down
40 changes: 40 additions & 0 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
/* Thread and interpreter state structures and their interfaces */

#include "Python.h"
#include "interpreteridobject.h" // PyInterpreterID_Type
#include "pycore_abstract.h" // _PyIndex_Check()
#include "pycore_ceval.h"
#include "pycore_code.h" // stats
#include "pycore_critical_section.h" // _PyCriticalSection_Resume()
Expand Down Expand Up @@ -1093,6 +1095,44 @@ PyInterpreterState_GetDict(PyInterpreterState *interp)
// interp ID
//----------

int64_t
_PyInterpreterState_ObjectToID(PyObject *idobj)
{
if (PyObject_TypeCheck(idobj, &PyInterpreterID_Type)) {
return _PyInterpreterID_GetID(idobj);
}

if (!_PyIndex_Check(idobj)) {
PyErr_Format(PyExc_TypeError,
"interpreter ID must be an int, got %.100s",
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
PyInterpreterState_GetID(PyInterpreterState *interp)
{
Expand Down