bpo-35081: Add _PyThreadState_GET() internal macro (GH-10266)

If Py_BUILD_CORE is defined, the PyThreadState_GET() macro access
_PyRuntime which comes from the internal pycore_state.h header.
Public headers must not require internal headers.

Move PyThreadState_GET() and _PyInterpreterState_GET_UNSAFE() from
Include/pystate.h to Include/internal/pycore_state.h, and rename
PyThreadState_GET() to _PyThreadState_GET() there.

The PyThreadState_GET() macro of pystate.h is now redefined when
pycore_state.h is included, to use the fast _PyThreadState_GET().

Changes:

* Add _PyThreadState_GET() macro
* Replace "PyThreadState_GET()->interp" with
  _PyInterpreterState_GET_UNSAFE()
* Replace PyThreadState_GET() with _PyThreadState_GET() in internal C
  files (compiled with Py_BUILD_CORE defined), but keep
  PyThreadState_GET() in the public header files.
* _testcapimodule.c: replace PyThreadState_GET() with
  PyThreadState_Get(); the module is not compiled with Py_BUILD_CORE
  defined.
* pycore_state.h now requires Py_BUILD_CORE to be defined.
This commit is contained in:
Victor Stinner 2018-11-01 01:51:40 +01:00 committed by GitHub
parent 27e2d1f219
commit 50b48572d9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 144 additions and 104 deletions

View file

@ -336,7 +336,7 @@ PyDoc_STRVAR(excepthook_doc,
static PyObject *
sys_exc_info(PyObject *self, PyObject *noargs)
{
_PyErr_StackItem *err_info = _PyErr_GetTopmostException(PyThreadState_GET());
_PyErr_StackItem *err_info = _PyErr_GetTopmostException(_PyThreadState_GET());
return Py_BuildValue(
"(OOO)",
err_info->exc_type != NULL ? err_info->exc_type : Py_None,
@ -565,7 +565,7 @@ function call. See the debugger chapter in the library manual."
static PyObject *
sys_gettrace(PyObject *self, PyObject *args)
{
PyThreadState *tstate = PyThreadState_GET();
PyThreadState *tstate = _PyThreadState_GET();
PyObject *temp = tstate->c_traceobj;
if (temp == NULL)
@ -603,7 +603,7 @@ and return. See the profiler chapter in the library manual."
static PyObject *
sys_getprofile(PyObject *self, PyObject *args)
{
PyThreadState *tstate = PyThreadState_GET();
PyThreadState *tstate = _PyThreadState_GET();
PyObject *temp = tstate->c_profileobj;
if (temp == NULL)
@ -722,7 +722,7 @@ sys_setrecursionlimit(PyObject *self, PyObject *args)
the new low-water mark. Otherwise it may not be possible anymore to
reset the overflowed flag to 0. */
mark = _Py_RecursionLimitLowerWaterMark(new_limit);
tstate = PyThreadState_GET();
tstate = _PyThreadState_GET();
if (tstate->recursion_depth >= mark) {
PyErr_Format(PyExc_RecursionError,
"cannot set the recursion limit to %i at "
@ -1362,7 +1362,7 @@ purposes only."
static PyObject *
sys_getframe(PyObject *self, PyObject *args)
{
PyFrameObject *f = PyThreadState_GET()->frame;
PyFrameObject *f = _PyThreadState_GET()->frame;
int depth = -1;
if (!PyArg_ParseTuple(args, "|i:_getframe", &depth))
@ -1745,7 +1745,7 @@ static int
_PySys_ReadPreInitOptions(void)
{
/* Rerun the add commands with the actual sys module available */
PyThreadState *tstate = PyThreadState_GET();
PyThreadState *tstate = _PyThreadState_GET();
if (tstate == NULL) {
/* Still don't have a thread state, so something is wrong! */
return -1;
@ -1796,7 +1796,7 @@ get_warnoptions(void)
void
PySys_ResetWarnOptions(void)
{
PyThreadState *tstate = PyThreadState_GET();
PyThreadState *tstate = _PyThreadState_GET();
if (tstate == NULL) {
_clear_preinit_entries(&_preinit_warnoptions);
return;
@ -1835,7 +1835,7 @@ PySys_AddWarnOptionUnicode(PyObject *option)
void
PySys_AddWarnOption(const wchar_t *s)
{
PyThreadState *tstate = PyThreadState_GET();
PyThreadState *tstate = _PyThreadState_GET();
if (tstate == NULL) {
_append_preinit_entry(&_preinit_warnoptions, s);
return;
@ -1922,7 +1922,7 @@ error:
void
PySys_AddXOption(const wchar_t *s)
{
PyThreadState *tstate = PyThreadState_GET();
PyThreadState *tstate = _PyThreadState_GET();
if (tstate == NULL) {
_append_preinit_entry(&_preinit_xoptions, s);
return;