@@ -35,52 +35,45 @@ newinterpid(PyTypeObject *cls, int64_t id, int force)
3535 return self ;
3636}
3737
38- static PyObject *
39- interpid_new ( PyTypeObject * cls , PyObject * args , PyObject * kwds )
38+ static int
39+ interp_id_converter ( PyObject * arg , void * ptr )
4040{
41- static char * kwlist [] = {"id" , "force" , NULL };
42- PyObject * idobj ;
43- int force = 0 ;
44- if (!PyArg_ParseTupleAndKeywords (args , kwds ,
45- "O|$p:InterpreterID.__init__" , kwlist ,
46- & idobj , & force )) {
47- return NULL ;
48- }
49-
50- // Coerce and check the ID.
5141 int64_t id ;
52- if (PyObject_TypeCheck (idobj , & _PyInterpreterID_Type )) {
53- id = ((interpid * )idobj )-> id ;
42+ if (PyObject_TypeCheck (arg , & _PyInterpreterID_Type )) {
43+ id = ((interpid * )arg )-> id ;
5444 }
55- else {
56- PyObject * pyid ;
57- if (PyIndex_Check (idobj )) {
58- pyid = idobj ;
59- Py_INCREF (pyid );
60- }
61- else if (PyUnicode_Check (idobj )) {
62- pyid = PyNumber_Long (idobj );
63- if (pyid == NULL ) {
64- return NULL ;
65- }
66- }
67- else {
68- PyErr_Format (PyExc_TypeError ,
69- "interpreter ID must be an int, got %.100s" ,
70- idobj -> ob_type -> tp_name );
71- return NULL ;
72- }
73- id = PyLong_AsLongLong (pyid );
74- Py_DECREF (pyid );
45+ else if (PyIndex_Check (arg )) {
46+ id = PyLong_AsLongLong (arg );
7547 if (id == -1 && PyErr_Occurred ()) {
76- return NULL ;
48+ return 0 ;
7749 }
7850 if (id < 0 ) {
7951 PyErr_Format (PyExc_ValueError ,
80- "interpreter ID must be a non-negative int, got %R" , idobj );
81- return NULL ;
52+ "interpreter ID must be a non-negative int, got %R" , arg );
53+ return 0 ;
8254 }
8355 }
56+ else {
57+ PyErr_Format (PyExc_TypeError ,
58+ "interpreter ID must be an int, got %.100s" ,
59+ arg -> ob_type -> tp_name );
60+ return 0 ;
61+ }
62+ * (int64_t * )ptr = id ;
63+ return 1 ;
64+ }
65+
66+ static PyObject *
67+ interpid_new (PyTypeObject * cls , PyObject * args , PyObject * kwds )
68+ {
69+ static char * kwlist [] = {"id" , "force" , NULL };
70+ int64_t id ;
71+ int force = 0 ;
72+ if (!PyArg_ParseTupleAndKeywords (args , kwds ,
73+ "O&|$p:InterpreterID.__init__" , kwlist ,
74+ interp_id_converter , & id , & force )) {
75+ return NULL ;
76+ }
8477
8578 return (PyObject * )newinterpid (cls , id , force );
8679}
@@ -287,19 +280,7 @@ PyInterpreterState *
287280_PyInterpreterID_LookUp (PyObject * requested_id )
288281{
289282 int64_t id ;
290- if (PyObject_TypeCheck (requested_id , & _PyInterpreterID_Type )) {
291- id = ((interpid * )requested_id )-> id ;
292- }
293- else if (PyIndex_Check (requested_id )) {
294- id = PyLong_AsLongLong (requested_id );
295- if (id == -1 && PyErr_Occurred () != NULL ) {
296- return NULL ;
297- }
298- assert (id <= INT64_MAX );
299- }
300- else {
301- PyErr_Format (PyExc_TypeError , "interpreter ID must be an int, got %.100s" ,
302- requested_id -> ob_type -> tp_name );
283+ if (!interp_id_converter (requested_id , & id )) {
303284 return NULL ;
304285 }
305286 return _PyInterpreterState_LookUpID (id );
0 commit comments