mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
gh-76785: Clean Up Interpreter ID Conversions (gh-117048)
Mostly we unify the two different implementations of the conversion code (from PyObject * to int64_t. We also drop the PyArg_ParseTuple()-style converter function, as well as rename and move PyInterpreterID_LookUp().
This commit is contained in:
parent
e728303532
commit
bbee57fa8c
8 changed files with 143 additions and 178 deletions
|
@ -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"
|
||||
|
||||
|
||||
|
@ -11,6 +10,21 @@ typedef struct interpid {
|
|||
int64_t id;
|
||||
} interpid;
|
||||
|
||||
int64_t
|
||||
_PyInterpreterID_GetID(PyObject *self)
|
||||
{
|
||||
if (!PyObject_TypeCheck(self, &PyInterpreterID_Type)) {
|
||||
PyErr_Format(PyExc_TypeError,
|
||||
"expected an InterpreterID, got %R",
|
||||
self);
|
||||
return -1;
|
||||
|
||||
}
|
||||
int64_t id = ((interpid *)self)->id;
|
||||
assert(id >= 0);
|
||||
return id;
|
||||
}
|
||||
|
||||
static interpid *
|
||||
newinterpid(PyTypeObject *cls, int64_t id, int force)
|
||||
{
|
||||
|
@ -42,43 +56,19 @@ newinterpid(PyTypeObject *cls, int64_t id, int force)
|
|||
return self;
|
||||
}
|
||||
|
||||
static int
|
||||
interp_id_converter(PyObject *arg, void *ptr)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
else {
|
||||
PyErr_Format(PyExc_TypeError,
|
||||
"interpreter ID must be an int, got %.100s",
|
||||
Py_TYPE(arg)->tp_name);
|
||||
return 0;
|
||||
}
|
||||
*(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 = _PyInterpreterState_ObjectToID(idobj);
|
||||
if (id < 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
@ -282,13 +272,3 @@ PyInterpreterState_GetIDObject(PyInterpreterState *interp)
|
|||
}
|
||||
return (PyObject *)newinterpid(&PyInterpreterID_Type, id, 0);
|
||||
}
|
||||
|
||||
PyInterpreterState *
|
||||
PyInterpreterID_LookUp(PyObject *requested_id)
|
||||
{
|
||||
int64_t id;
|
||||
if (!interp_id_converter(requested_id, &id)) {
|
||||
return NULL;
|
||||
}
|
||||
return _PyInterpreterState_LookUpID(id);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue