summaryrefslogtreecommitdiffstats
path: root/Python/pystate.c
diff options
context:
space:
mode:
Diffstat (limited to 'Python/pystate.c')
-rw-r--r--Python/pystate.c103
1 files changed, 79 insertions, 24 deletions
diff --git a/Python/pystate.c b/Python/pystate.c
index 5a334e8..5332b8a 100644
--- a/Python/pystate.c
+++ b/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 */