mirror of
https://github.com/python/cpython.git
synced 2025-07-08 03:45:36 +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
103
Python/pystate.c
103
Python/pystate.c
|
@ -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()
|
||||
|
@ -1064,6 +1066,73 @@ _PyInterpreterState_FailIfRunningMain(PyInterpreterState *interp)
|
|||
// accessors
|
||||
//----------
|
||||
|
||||
PyObject *
|
||||
PyUnstable_InterpreterState_GetMainModule(PyInterpreterState *interp)
|
||||
{
|
||||
PyObject *modules = _PyImport_GetModules(interp);
|
||||
if (modules == NULL) {
|
||||
PyErr_SetString(PyExc_RuntimeError, "interpreter not initialized");
|
||||
return NULL;
|
||||
}
|
||||
return PyMapping_GetItemString(modules, "__main__");
|
||||
}
|
||||
|
||||
PyObject *
|
||||
PyInterpreterState_GetDict(PyInterpreterState *interp)
|
||||
{
|
||||
if (interp->dict == NULL) {
|
||||
interp->dict = PyDict_New();
|
||||
if (interp->dict == NULL) {
|
||||
PyErr_Clear();
|
||||
}
|
||||
}
|
||||
/* Returning NULL means no per-interpreter dict is available. */
|
||||
return interp->dict;
|
||||
}
|
||||
|
||||
|
||||
//----------
|
||||
// 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)
|
||||
{
|
||||
|
@ -1142,30 +1211,6 @@ _PyInterpreterState_RequireIDRef(PyInterpreterState *interp, int required)
|
|||
interp->requires_idref = required ? 1 : 0;
|
||||
}
|
||||
|
||||
PyObject *
|
||||
PyUnstable_InterpreterState_GetMainModule(PyInterpreterState *interp)
|
||||
{
|
||||
PyObject *modules = _PyImport_GetModules(interp);
|
||||
if (modules == NULL) {
|
||||
PyErr_SetString(PyExc_RuntimeError, "interpreter not initialized");
|
||||
return NULL;
|
||||
}
|
||||
return PyMapping_GetItemString(modules, "__main__");
|
||||
}
|
||||
|
||||
PyObject *
|
||||
PyInterpreterState_GetDict(PyInterpreterState *interp)
|
||||
{
|
||||
if (interp->dict == NULL) {
|
||||
interp->dict = PyDict_New();
|
||||
if (interp->dict == NULL) {
|
||||
PyErr_Clear();
|
||||
}
|
||||
}
|
||||
/* Returning NULL means no per-interpreter dict is available. */
|
||||
return interp->dict;
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------
|
||||
// look up an interpreter state
|
||||
|
@ -1227,6 +1272,16 @@ _PyInterpreterState_LookUpID(int64_t requested_id)
|
|||
return interp;
|
||||
}
|
||||
|
||||
PyInterpreterState *
|
||||
_PyInterpreterState_LookUpIDObject(PyObject *requested_id)
|
||||
{
|
||||
int64_t id = _PyInterpreterState_ObjectToID(requested_id);
|
||||
if (id < 0) {
|
||||
return NULL;
|
||||
}
|
||||
return _PyInterpreterState_LookUpID(id);
|
||||
}
|
||||
|
||||
|
||||
/********************************/
|
||||
/* the per-thread runtime state */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue