bpo-38733: PyErr_Occurred() caller must hold the GIL (GH-17080) · python/cpython@d12d0e7 (original) (raw)

5 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -374,6 +374,8 @@ Querying the error indicator
374 374 own a reference to the return value, so you do not need to :c:func:`Py_DECREF`
375 375 it.
376 376
377 + The caller must hold the GIL.
378 +
377 379 .. note::
378 380
379 381 Do not compare the return value to a specific exception; use
Original file line number Diff line number Diff line change
@@ -10,7 +10,8 @@ extern "C" {
10 10
11 11 static inline PyObject* _PyErr_Occurred(PyThreadState *tstate)
12 12 {
13 -return tstate == NULL ? NULL : tstate->curexc_type;
13 +assert(tstate != NULL);
14 +return tstate->curexc_type;
14 15 }
15 16
16 17
Original file line number Diff line number Diff line change
@@ -30,12 +30,10 @@ PyObject*
30 30 _Py_CheckFunctionResult(PyThreadState *tstate, PyObject *callable,
31 31 PyObject *result, const char *where)
32 32 {
33 -int err_occurred = (_PyErr_Occurred(tstate) != NULL);
34 -
35 33 assert((callable != NULL) ^ (where != NULL));
36 34
37 35 if (result == NULL) {
38 -if (!err_occurred) {
36 +if (!_PyErr_Occurred(tstate)) {
39 37 if (callable)
40 38 _PyErr_Format(tstate, PyExc_SystemError,
41 39 "%R returned NULL without setting an error",
@@ -52,7 +50,7 @@ _Py_CheckFunctionResult(PyThreadState *tstate, PyObject *callable,
52 50 }
53 51 }
54 52 else {
55 -if (err_occurred) {
53 +if (_PyErr_Occurred(tstate)) {
56 54 Py_DECREF(result);
57 55
58 56 if (callable) {
Original file line number Diff line number Diff line change
@@ -2313,12 +2313,13 @@ _PyMem_DebugRawRealloc(void *ctx, void *p, size_t nbytes)
2313 2313 return data;
2314 2314 }
2315 2315
2316 -static void
2316 +static inline void
2317 2317 _PyMem_DebugCheckGIL(void)
2318 2318 {
2319 -if (!PyGILState_Check())
2319 +if (!PyGILState_Check()) {
2320 2320 Py_FatalError("Python memory allocator called "
2321 2321 "without holding the GIL");
2322 + }
2322 2323 }
2323 2324
2324 2325 static void *
Original file line number Diff line number Diff line change
@@ -218,6 +218,9 @@ PyErr_SetString(PyObject *exception, const char *string)
218 218 PyObject* _Py_HOT_FUNCTION
219 219 PyErr_Occurred(void)
220 220 {
221 +/* The caller must hold the GIL. */
222 +assert(PyGILState_Check());
223 +
221 224 PyThreadState *tstate = _PyThreadState_GET();
222 225 return _PyErr_Occurred(tstate);
223 226 }