11/* InterpreterID object */
22
33#include "Python.h"
4- #include "pycore_abstract.h" // _PyIndex_Check()
5- #include "pycore_interp.h" // _PyInterpreterState_LookUpID()
4+ #include "pycore_interp.h" // _PyInterpreterState_LookUpID()
65#include "interpreteridobject.h"
76
87
@@ -11,6 +10,21 @@ typedef struct interpid {
1110 int64_t id ;
1211} interpid ;
1312
13+ int64_t
14+ _PyInterpreterID_GetID (PyObject * self )
15+ {
16+ if (!PyObject_TypeCheck (self , & PyInterpreterID_Type )) {
17+ PyErr_Format (PyExc_TypeError ,
18+ "expected an InterpreterID, got %R" ,
19+ self );
20+ return -1 ;
21+
22+ }
23+ int64_t id = ((interpid * )self )-> id ;
24+ assert (id >= 0 );
25+ return id ;
26+ }
27+
1428static interpid *
1529newinterpid (PyTypeObject * cls , int64_t id , int force )
1630{
@@ -42,43 +56,19 @@ newinterpid(PyTypeObject *cls, int64_t id, int force)
4256 return self ;
4357}
4458
45- static int
46- interp_id_converter (PyObject * arg , void * ptr )
47- {
48- int64_t id ;
49- if (PyObject_TypeCheck (arg , & PyInterpreterID_Type )) {
50- id = ((interpid * )arg )-> id ;
51- }
52- else if (_PyIndex_Check (arg )) {
53- id = PyLong_AsLongLong (arg );
54- if (id == -1 && PyErr_Occurred ()) {
55- return 0 ;
56- }
57- if (id < 0 ) {
58- PyErr_Format (PyExc_ValueError ,
59- "interpreter ID must be a non-negative int, got %R" , arg );
60- return 0 ;
61- }
62- }
63- else {
64- PyErr_Format (PyExc_TypeError ,
65- "interpreter ID must be an int, got %.100s" ,
66- Py_TYPE (arg )-> tp_name );
67- return 0 ;
68- }
69- * (int64_t * )ptr = id ;
70- return 1 ;
71- }
72-
7359static PyObject *
7460interpid_new (PyTypeObject * cls , PyObject * args , PyObject * kwds )
7561{
7662 static char * kwlist [] = {"id" , "force" , NULL };
77- int64_t id ;
63+ PyObject * idobj ;
7864 int force = 0 ;
7965 if (!PyArg_ParseTupleAndKeywords (args , kwds ,
80- "O&|$p:InterpreterID.__init__" , kwlist ,
81- interp_id_converter , & id , & force )) {
66+ "O|$p:InterpreterID.__init__" , kwlist ,
67+ & idobj , & force )) {
68+ return NULL ;
69+ }
70+ int64_t id = _PyInterpreterState_ObjectToID (idobj );
71+ if (id < 0 ) {
8272 return NULL ;
8373 }
8474
@@ -282,13 +272,3 @@ PyInterpreterState_GetIDObject(PyInterpreterState *interp)
282272 }
283273 return (PyObject * )newinterpid (& PyInterpreterID_Type , id , 0 );
284274}
285-
286- PyInterpreterState *
287- PyInterpreterID_LookUp (PyObject * requested_id )
288- {
289- int64_t id ;
290- if (!interp_id_converter (requested_id , & id )) {
291- return NULL ;
292- }
293- return _PyInterpreterState_LookUpID (id );
294- }
0 commit comments