summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2021-10-13 12:09:13 (GMT)
committerGitHub <noreply@github.com>2021-10-13 12:09:13 (GMT)
commit7cdc2a0f4b785327ad9d55312409a06e554df3d5 (patch)
tree71a2b8fdc68c9fbebfc79e0a3dffafbcad72dd4e /Modules
parent773330773968f211c77abc7b5b525faa7b3c35a2 (diff)
downloadcpython-7cdc2a0f4b785327ad9d55312409a06e554df3d5.zip
cpython-7cdc2a0f4b785327ad9d55312409a06e554df3d5.tar.gz
cpython-7cdc2a0f4b785327ad9d55312409a06e554df3d5.tar.bz2
pycore_pystate.h no longer redefines PyThreadState_GET() (GH-28921)
Redefining the PyThreadState_GET() macro in pycore_pystate.h is useless since it doesn't affect files not including it. Either use _PyThreadState_GET() directly, or don't use pycore_pystate.h internal C API. For example, the _testcapi extension don't use the internal C API, but use the public PyThreadState_Get() function instead. Replace PyThreadState_Get() with _PyThreadState_GET(). The _PyThreadState_GET() macro is more efficient than PyThreadState_Get() and PyThreadState_GET() function calls which call fail with a fatal Python error. posixmodule.c and _ctypes extension now include <windows.h> before pycore header files (like pycore_call.h). _PyTraceback_Add() now uses _PyErr_Fetch()/_PyErr_Restore() instead of PyErr_Fetch()/PyErr_Restore(). The _decimal and _xxsubinterpreters extensions are now built with the Py_BUILD_CORE_MODULE macro defined to get access to the internal C API.
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_asynciomodule.c5
-rw-r--r--Modules/_ctypes/_ctypes.c6
-rw-r--r--Modules/_ctypes/callbacks.c8
-rw-r--r--Modules/_ctypes/cfield.c8
-rw-r--r--Modules/_ctypes/stgdict.c8
-rw-r--r--Modules/_decimal/_decimal.c24
-rw-r--r--Modules/_lsprof.c7
-rw-r--r--Modules/_testinternalcapi.c5
-rw-r--r--Modules/_threadmodule.c2
-rw-r--r--Modules/_xxsubinterpretersmodule.c3
-rw-r--r--Modules/posixmodule.c24
11 files changed, 56 insertions, 44 deletions
diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c
index 56079b0..adc5ff9 100644
--- a/Modules/_asynciomodule.c
+++ b/Modules/_asynciomodule.c
@@ -1,5 +1,6 @@
#include "Python.h"
#include "pycore_pyerrors.h" // _PyErr_ClearExcState()
+#include "pycore_pystate.h" // _PyThreadState_GET()
#include <stddef.h> // offsetof()
@@ -225,7 +226,7 @@ get_running_loop(PyObject **loop)
{
PyObject *rl;
- PyThreadState *ts = PyThreadState_Get();
+ PyThreadState *ts = _PyThreadState_GET();
uint64_t ts_id = PyThreadState_GetID(ts);
if (ts_id == cached_running_holder_tsid && cached_running_holder != NULL) {
// Fast path, check the cache.
@@ -287,7 +288,7 @@ set_running_loop(PyObject *loop)
{
PyObject *ts_dict = NULL;
- PyThreadState *tstate = PyThreadState_Get();
+ PyThreadState *tstate = _PyThreadState_GET();
if (tstate != NULL) {
ts_dict = _PyThreadState_GetDict(tstate); // borrowed
}
diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c
index 2567067..f8940fd 100644
--- a/Modules/_ctypes/_ctypes.c
+++ b/Modules/_ctypes/_ctypes.c
@@ -102,12 +102,16 @@ bytes(cdata)
#define PY_SSIZE_T_CLEAN
#include "Python.h"
+// windows.h must be included before pycore internal headers
+#ifdef MS_WIN32
+# include <windows.h>
+#endif
+
#include "pycore_call.h" // _PyObject_CallNoArgs()
#include "structmember.h" // PyMemberDef
#include <ffi.h>
#ifdef MS_WIN32
-#include <windows.h>
#include <malloc.h>
#ifndef IS_INTRESOURCE
#define IS_INTRESOURCE(x) (((size_t)(x) >> 16) == 0)
diff --git a/Modules/_ctypes/callbacks.c b/Modules/_ctypes/callbacks.c
index 18b0104..c24f04c 100644
--- a/Modules/_ctypes/callbacks.c
+++ b/Modules/_ctypes/callbacks.c
@@ -1,13 +1,15 @@
#include "Python.h"
+// windows.h must be included before pycore internal headers
+#ifdef MS_WIN32
+# include <windows.h>
+#endif
+
#include "pycore_call.h" // _PyObject_CallNoArgs()
#include "frameobject.h"
#include <stdbool.h>
#include <ffi.h>
-#ifdef MS_WIN32
-#include <windows.h>
-#endif
#include "ctypes.h"
/**************************************************************/
diff --git a/Modules/_ctypes/cfield.c b/Modules/_ctypes/cfield.c
index 6788aee..b7585bc 100644
--- a/Modules/_ctypes/cfield.c
+++ b/Modules/_ctypes/cfield.c
@@ -1,11 +1,13 @@
#include "Python.h"
+// windows.h must be included before pycore internal headers
+#ifdef MS_WIN32
+# include <windows.h>
+#endif
+
#include "pycore_bitutils.h" // _Py_bswap32()
#include "pycore_call.h" // _PyObject_CallNoArgs()
#include <ffi.h>
-#ifdef MS_WIN32
-#include <windows.h>
-#endif
#include "ctypes.h"
diff --git a/Modules/_ctypes/stgdict.c b/Modules/_ctypes/stgdict.c
index ea3c58b..43669d7 100644
--- a/Modules/_ctypes/stgdict.c
+++ b/Modules/_ctypes/stgdict.c
@@ -1,9 +1,13 @@
#include "Python.h"
+// windows.h must be included before pycore internal headers
+#ifdef MS_WIN32
+# include <windows.h>
+#endif
+
#include "pycore_call.h" // _PyObject_CallNoArgs()
#include <ffi.h>
#ifdef MS_WIN32
-#include <windows.h>
-#include <malloc.h>
+# include <malloc.h>
#endif
#include "ctypes.h"
diff --git a/Modules/_decimal/_decimal.c b/Modules/_decimal/_decimal.c
index e2979a5..dd876f2 100644
--- a/Modules/_decimal/_decimal.c
+++ b/Modules/_decimal/_decimal.c
@@ -27,6 +27,7 @@
#include <Python.h>
+#include "pycore_pystate.h" // _PyThreadState_GET()
#include "longintrepr.h"
#include "complexobject.h"
#include "mpdecimal.h"
@@ -1511,18 +1512,20 @@ static PyGetSetDef context_getsets [] =
static PyObject *
current_context_from_dict(void)
{
- PyObject *dict;
- PyObject *tl_context;
- PyThreadState *tstate;
+ PyThreadState *tstate = _PyThreadState_GET();
+#ifdef Py_DEBUG
+ // The caller must hold the GIL
+ _Py_EnsureTstateNotNULL(tstate);
+#endif
- dict = PyThreadState_GetDict();
+ PyObject *dict = _PyThreadState_GetDict(tstate);
if (dict == NULL) {
PyErr_SetString(PyExc_RuntimeError,
"cannot get thread state");
return NULL;
}
- tl_context = PyDict_GetItemWithError(dict, tls_context_key);
+ PyObject *tl_context = PyDict_GetItemWithError(dict, tls_context_key);
if (tl_context != NULL) {
/* We already have a thread local context. */
CONTEXT_CHECK(tl_context);
@@ -1548,11 +1551,8 @@ current_context_from_dict(void)
/* Cache the context of the current thread, assuming that it
* will be accessed several times before a thread switch. */
- tstate = PyThreadState_GET();
- if (tstate) {
- cached_context = (PyDecContextObject *)tl_context;
- cached_context->tstate = tstate;
- }
+ cached_context = (PyDecContextObject *)tl_context;
+ cached_context->tstate = tstate;
/* Borrowed reference with refcount==1 */
return tl_context;
@@ -1562,9 +1562,7 @@ current_context_from_dict(void)
static PyObject *
current_context(void)
{
- PyThreadState *tstate;
-
- tstate = PyThreadState_GET();
+ PyThreadState *tstate = _PyThreadState_GET();
if (cached_context && cached_context->tstate == tstate) {
return (PyObject *)cached_context;
}
diff --git a/Modules/_lsprof.c b/Modules/_lsprof.c
index 097d0eff..2e27afc 100644
--- a/Modules/_lsprof.c
+++ b/Modules/_lsprof.c
@@ -1,5 +1,6 @@
#include "Python.h"
#include "pycore_call.h" // _PyObject_CallNoArgs()
+#include "pycore_pystate.h" // _PyThreadState_GET()
#include "rotatingtree.h"
/************************************************************/
@@ -672,7 +673,7 @@ profiler_enable(ProfilerObject *self, PyObject *args, PyObject *kwds)
return NULL;
}
- PyThreadState *tstate = PyThreadState_GET();
+ PyThreadState *tstate = _PyThreadState_GET();
if (_PyEval_SetProfile(tstate, profiler_callback, (PyObject*)self) < 0) {
return NULL;
}
@@ -706,7 +707,7 @@ Stop collecting profiling information.\n\
static PyObject*
profiler_disable(ProfilerObject *self, PyObject* noarg)
{
- PyThreadState *tstate = PyThreadState_GET();
+ PyThreadState *tstate = _PyThreadState_GET();
if (_PyEval_SetProfile(tstate, NULL, NULL) < 0) {
return NULL;
}
@@ -743,7 +744,7 @@ static void
profiler_dealloc(ProfilerObject *op)
{
if (op->flags & POF_ENABLED) {
- PyThreadState *tstate = PyThreadState_GET();
+ PyThreadState *tstate = _PyThreadState_GET();
if (_PyEval_SetProfile(tstate, NULL, NULL) < 0) {
PyErr_WriteUnraisable((PyObject *)op);
}
diff --git a/Modules/_testinternalcapi.c b/Modules/_testinternalcapi.c
index d5616fd..3ba9396 100644
--- a/Modules/_testinternalcapi.c
+++ b/Modules/_testinternalcapi.c
@@ -18,7 +18,8 @@
#include "pycore_hashtable.h" // _Py_hashtable_new()
#include "pycore_initconfig.h" // _Py_GetConfigsAsDict()
#include "pycore_interp.h" // _PyInterpreterState_GetConfigCopy()
-#include "pycore_pyerrors.h" // _Py_UTF8_Edit_Cost()
+#include "pycore_pyerrors.h" // _Py_UTF8_Edit_Cost()
+#include "pycore_pystate.h" // _PyThreadState_GET()
static PyObject *
@@ -31,7 +32,7 @@ get_configs(PyObject *self, PyObject *Py_UNUSED(args))
static PyObject*
get_recursion_depth(PyObject *self, PyObject *Py_UNUSED(args))
{
- PyThreadState *tstate = PyThreadState_Get();
+ PyThreadState *tstate = _PyThreadState_GET();
/* subtract one to ignore the frame of the get_recursion_depth() call */
return PyLong_FromLong(tstate->recursion_depth - 1);
diff --git a/Modules/_threadmodule.c b/Modules/_threadmodule.c
index ff85838..39b116a 100644
--- a/Modules/_threadmodule.c
+++ b/Modules/_threadmodule.c
@@ -1319,7 +1319,7 @@ static PyObject *
thread__set_sentinel(PyObject *module, PyObject *Py_UNUSED(ignored))
{
PyObject *wr;
- PyThreadState *tstate = PyThreadState_Get();
+ PyThreadState *tstate = _PyThreadState_GET();
lockobject *lock;
if (tstate->on_delete_data != NULL) {
diff --git a/Modules/_xxsubinterpretersmodule.c b/Modules/_xxsubinterpretersmodule.c
index 11b55f7..939114e 100644
--- a/Modules/_xxsubinterpretersmodule.c
+++ b/Modules/_xxsubinterpretersmodule.c
@@ -5,6 +5,7 @@
#include "Python.h"
#include "frameobject.h"
#include "pycore_frame.h"
+#include "pycore_pystate.h" // _PyThreadState_GET()
#include "interpreteridobject.h"
@@ -2017,7 +2018,7 @@ interp_create(PyObject *self, PyObject *args, PyObject *kwds)
}
// Create and initialize the new interpreter.
- PyThreadState *save_tstate = PyThreadState_Get();
+ PyThreadState *save_tstate = _PyThreadState_GET();
// XXX Possible GILState issues?
PyThreadState *tstate = _Py_NewInterpreter(isolated);
PyThreadState_Swap(save_tstate);
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index ada1a58..9c174ee 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -10,31 +10,25 @@
#define PY_SSIZE_T_CLEAN
#include "Python.h"
-#include "pycore_call.h" // _PyObject_CallNoArgs()
-#include "pycore_fileutils.h" // _Py_closerange()
-#include "pycore_moduleobject.h" // _PyModule_GetState()
+// Include <windows.h> before pycore internal headers. FSCTL_GET_REPARSE_POINT
+// is not exported by <windows.h> if the WIN32_LEAN_AND_MEAN macro is defined,
+// whereas pycore_condvar.h defines the WIN32_LEAN_AND_MEAN macro.
#ifdef MS_WINDOWS
- /* include <windows.h> early to avoid conflict with pycore_condvar.h:
-
- #define WIN32_LEAN_AND_MEAN
- #include <windows.h>
-
- FSCTL_GET_REPARSE_POINT is not exported with WIN32_LEAN_AND_MEAN. */
# include <windows.h>
# include <pathcch.h>
#endif
-#if !defined(EX_OK) && defined(EXIT_SUCCESS)
-#define EX_OK EXIT_SUCCESS
-#endif
-
#ifdef __VXWORKS__
# include "pycore_bitutils.h" // _Py_popcount32()
#endif
+#include "pycore_call.h" // _PyObject_CallNoArgs()
+#include "pycore_fileutils.h" // _Py_closerange()
+#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "pycore_ceval.h" // _PyEval_ReInitThreads()
#include "pycore_import.h" // _PyImport_ReInitLock()
#include "pycore_initconfig.h" // _PyStatus_EXCEPTION()
#include "pycore_pystate.h" // _PyInterpreterState_GET()
+
#include "structmember.h" // PyMemberDef
#ifndef MS_WINDOWS
# include "posixmodule.h"
@@ -42,6 +36,10 @@
# include "winreparse.h"
#endif
+#if !defined(EX_OK) && defined(EXIT_SUCCESS)
+# define EX_OK EXIT_SUCCESS
+#endif
+
/* On android API level 21, 'AT_EACCESS' is not declared although
* HAVE_FACCESSAT is defined. */
#ifdef __ANDROID__