[pypy-commit] cffi py3.8-interp-dict: attempt to use PyInterpreterState_GetDict()
arigo
pypy.commits at gmail.com
Sat Mar 16 03:15:16 EDT 2019
Author: Armin Rigo <arigo at tunes.org>
Branch: py3.8-interp-dict
Changeset: r3245:113fd85cc556
Date: 2019-03-16 08:03 +0100
http://bitbucket.org/cffi/cffi/changeset/113fd85cc556/
Log: attempt to use PyInterpreterState_GetDict()
diff --git a/c/call_python.c b/c/call_python.c
--- a/c/call_python.c
+++ b/c/call_python.c
@@ -1,10 +1,18 @@
#if PY_VERSION_HEX >= 0x03080000
-# define Py_BUILD_CORE
-/* for access to the fields of PyInterpreterState */
-# include "internal/pycore_pystate.h"
-# undef Py_BUILD_CORE
+# define HAVE_PYINTERPSTATE_GETDICT
#endif
+
+static PyObject *_current_interp_key(void)
+{
+ PyInterpreterState *interp = PyThreadState_GET()->interp;
+#ifdef HAVE_PYINTERPSTATE_GETDICT
+ return PyInterpreterState_GetDict(interp); /* shared reference */
+#else
+ return interp->modules;
+#endif
+}
+
static PyObject *_get_interpstate_dict(void)
{
/* Hack around to return a dict that is subinterpreter-local.
@@ -14,7 +22,7 @@
*/
static PyObject *attr_name = NULL;
PyThreadState *tstate;
- PyObject *d, *builtins;
+ PyObject *d, *interpdict;
int err;
tstate = PyThreadState_GET();
@@ -23,8 +31,13 @@
return NULL;
}
- builtins = tstate->interp->builtins;
- if (builtins == NULL) {
+ PyInterpreterState *interp = tstate->interp;
+#ifdef HAVE_PYINTERPSTATE_GETDICT
+ interpdict = PyInterpreterState_GetDict(interp); /* shared reference */
+#else
+ interpdict = interp->builtins;
+#endif
+ if (interpdict == NULL) {
/* subinterpreter was cleared already, or is being cleared right now,
to a point that is too much for us to continue */
return NULL;
@@ -38,13 +51,13 @@
goto error;
}
- d = PyDict_GetItem(builtins, attr_name);
+ d = PyDict_GetItem(interpdict, attr_name);
if (d == NULL) {
d = PyDict_New();
if (d == NULL)
goto error;
- err = PyDict_SetItem(builtins, attr_name, d);
- Py_DECREF(d); /* if successful, there is one ref left in builtins */
+ err = PyDict_SetItem(interpdict, attr_name, d);
+ Py_DECREF(d); /* if successful, there is one ref left in interpdict */
if (err < 0)
goto error;
}
@@ -163,7 +176,7 @@
if (infotuple == NULL)
return 3; /* no ffi.def_extern() from this subinterpreter */
- new1 = PyThreadState_GET()->interp->modules;
+ new1 = _current_interp_key();
Py_INCREF(new1);
Py_INCREF(infotuple);
old1 = (PyObject *)externpy->reserved1;
@@ -252,7 +265,7 @@
}
else {
PyGILState_STATE state = gil_ensure();
- if (externpy->reserved1 != PyThreadState_GET()->interp->modules) {
+ if (externpy->reserved1 != _current_interp_key()) {
/* Update the (reserved1, reserved2) cache. This will fail
if we didn't call @ffi.def_extern() in this particular
subinterpreter. */
More information about the pypy-commit
mailing list