Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-38733: PyErr_Occurred() caller must hold the GIL #17080

Merged
merged 1 commit into from
Nov 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Doc/c-api/exceptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,8 @@ Querying the error indicator
own a reference to the return value, so you do not need to :c:func:`Py_DECREF`
it.

The caller must hold the GIL.

.. note::

Do not compare the return value to a specific exception; use
Expand Down
3 changes: 2 additions & 1 deletion Include/internal/pycore_pyerrors.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ extern "C" {

static inline PyObject* _PyErr_Occurred(PyThreadState *tstate)
{
return tstate == NULL ? NULL : tstate->curexc_type;
assert(tstate != NULL);
return tstate->curexc_type;
}


Expand Down
6 changes: 2 additions & 4 deletions Objects/call.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,10 @@ PyObject*
_Py_CheckFunctionResult(PyThreadState *tstate, PyObject *callable,
PyObject *result, const char *where)
{
int err_occurred = (_PyErr_Occurred(tstate) != NULL);

assert((callable != NULL) ^ (where != NULL));

if (result == NULL) {
if (!err_occurred) {
if (!_PyErr_Occurred(tstate)) {
if (callable)
_PyErr_Format(tstate, PyExc_SystemError,
"%R returned NULL without setting an error",
Expand All @@ -52,7 +50,7 @@ _Py_CheckFunctionResult(PyThreadState *tstate, PyObject *callable,
}
}
else {
if (err_occurred) {
if (_PyErr_Occurred(tstate)) {
Py_DECREF(result);

if (callable) {
Expand Down
5 changes: 3 additions & 2 deletions Objects/obmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2313,12 +2313,13 @@ _PyMem_DebugRawRealloc(void *ctx, void *p, size_t nbytes)
return data;
}

static void
static inline void
_PyMem_DebugCheckGIL(void)
{
if (!PyGILState_Check())
if (!PyGILState_Check()) {
Py_FatalError("Python memory allocator called "
"without holding the GIL");
}
}

static void *
Expand Down
3 changes: 3 additions & 0 deletions Python/errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ PyErr_SetString(PyObject *exception, const char *string)
PyObject* _Py_HOT_FUNCTION
PyErr_Occurred(void)
{
/* The caller must hold the GIL. */
assert(PyGILState_Check());

PyThreadState *tstate = _PyThreadState_GET();
return _PyErr_Occurred(tstate);
}
Expand Down