[New-bugs-announce] [issue38733] PyErr_Occurred(): tstate must be non-NULL

STINNER Victor report at bugs.python.org
Thu Nov 7 04:29:32 EST 2019


New submission from STINNER Victor <vstinner at python.org>:

bpo-3605 modified PyErr_Occurred() to return NULL if the current Python thread state ("tstate") is NULL:

commit 8e0bdfd1d473ddffaf3501768678f8a970019da8
Author: Jeffrey Yasskin <jyasskin at gmail.com>
Date:   Thu May 13 18:31:05 2010 +0000

    Make PyErr_Occurred return NULL if there is no current thread.  Previously it
    would Py_FatalError, which called PyErr_Occurred, resulting in a semi-infinite
    recursion.
    
    Fixes issue 3605.

This change made PyErr_Occurred() inefficient: PyErr_Occurred() was a simple attribute read (tstate->curexc_type), and now there is an additional if per call.

I recently added _PyErr_Occurred(tstate) which is similar to PyErr_Occurred() but requires to pass tstate explicitly. I expected this function to be as simple as:

static inline PyObject* _PyErr_Occurred(PyThreadState *tstate)
{
    return tstate->curexc_type;
}

but the current implementation is:

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


--

PyErr_Occurred() is currently implemented as:

PyObject* _Py_HOT_FUNCTION
PyErr_Occurred(void)
{
    PyThreadState *tstate = _PyThreadState_GET();
    return _PyErr_Occurred(tstate);
}

_PyThreadState_GET() must only be called with the GIL hold. This macro is designed to be efficient: it doesn't check if tstate is NULL.

_PyThreadState_GET() is only NULL if the thread has no Python thread state yet, or if the GIL is released.

IMHO PyErr_Occurred() must not be called if the GIL is released.

----------
components: Interpreter Core
messages: 356180
nosy: vstinner
priority: normal
severity: normal
status: open
title: PyErr_Occurred(): tstate must be non-NULL
versions: Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue38733>
_______________________________________


More information about the New-bugs-announce mailing list