summaryrefslogtreecommitdiffstats
path: root/Objects/interpreteridobject.c
diff options
context:
space:
mode:
authorEric Snow <ericsnowcurrently@gmail.com>2024-03-21 15:56:12 (GMT)
committerGitHub <noreply@github.com>2024-03-21 15:56:12 (GMT)
commitbbee57fa8c318cb26d6c8651254927a1972c9738 (patch)
tree244201e644486b2b5b241e6e40918dd390013027 /Objects/interpreteridobject.c
parente728303532168efab7694c55c82ea19b18bf8385 (diff)
downloadcpython-bbee57fa8c318cb26d6c8651254927a1972c9738.zip
cpython-bbee57fa8c318cb26d6c8651254927a1972c9738.tar.gz
cpython-bbee57fa8c318cb26d6c8651254927a1972c9738.tar.bz2
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().
Diffstat (limited to 'Objects/interpreteridobject.c')
-rw-r--r--Objects/interpreteridobject.c66
1 files changed, 23 insertions, 43 deletions
diff --git a/Objects/interpreteridobject.c b/Objects/interpreteridobject.c
index 16e27b6..4844d6a 100644
--- a/Objects/interpreteridobject.c
+++ b/Objects/interpreteridobject.c
@@ -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);
-}