This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vstinner
Recipients vstinner
Date 2019-11-07.09:29:31
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1573118972.06.0.487473179974.issue38733@roundup.psfhosted.org>
In-reply-to
Content
bpo-3605 modified PyErr_Occurred() to return NULL if the current Python thread state ("tstate") is NULL:

commit 8e0bdfd1d473ddffaf3501768678f8a970019da8
Author: Jeffrey Yasskin <jyasskin@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.
History
Date User Action Args
2019-11-07 09:29:32vstinnersetrecipients: + vstinner
2019-11-07 09:29:32vstinnersetmessageid: <1573118972.06.0.487473179974.issue38733@roundup.psfhosted.org>
2019-11-07 09:29:32vstinnerlinkissue38733 messages
2019-11-07 09:29:31vstinnercreate