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.

classification
Title: [C API] Remove _PyErr_OCCURRED() macro
Type: Stage: resolved
Components: C API Versions: Python 3.10
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: vstinner
Priority: normal Keywords: patch

Created on 2021-02-19 13:25 by vstinner, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 24579 merged vstinner, 2021-02-19 13:31
Messages (4)
msg387318 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-02-19 13:25
The private _PyErr_OCCURRED() function was introduced to optimize Objects/setobject.c:

commit 5ba0cbe39221ff8985ce5a4702a3b01a17ae3248
Author: Raymond Hettinger <python@rcn.com>
Date:   Sat Aug 6 18:31:24 2005 +0000

    * set_new() doesn't need to zero the structure a second time after tp_alloc
      has already done the job.
    * Use a macro form of PyErr_Occurred() inside the set_lookkey() function.

But the usage of the macro was removed one month later:

commit 9bda1d6f645bd0f3e76c14f27bbbac919814cd38
Author: Raymond Hettinger <python@rcn.com>
Date:   Fri Sep 16 07:14:21 2005 +0000

    No longer ignore exceptions raised by comparisons during key lookup.
    Inspired by Armin Rigo's suggestion to do the same with dictionaries.

The macro is currently defined as:

#if defined(Py_DEBUG) || defined(Py_LIMITED_API)
#define _PyErr_OCCURRED() PyErr_Occurred()
#else
#define _PyErr_OCCURRED() (PyThreadState_GET()->curexc_type)
#endif

IMO the new _PyErr_Occurred(tstate) internal function is a more reliable way (don't depend on Py_DEBUG and Py_LIMITED_API) to ensure that the code uses the most efficient way to check if an exception was raised.

I cannot find "_PyErr_OCCURRED" in the PyPI top 4000 projects, I checked with INADA-san's tool:
https://github.com/methane/notes/tree/master/2020/wchar-cache
(But I found many C extensiosn using "PyErr_Occurred" which is fine, this one stays ;-) I just wanted to check that my search was working.)

So removing _PyErr_OCCURRED() is unlikely to break PyPI top 4000 projects. If it breaks a third party project: well, we don't provide any backward compatibility warranty on the *private* C API.
msg387319 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-02-19 13:30
> #define _PyErr_OCCURRED() (PyThreadState_GET()->curexc_type)

But this way, this macro access directly the PyThreadState.curexc_type member which goes against the bpo-39947 "[C API] Make the PyThreadState structure opaque (move it to the internal C API)" issue.
msg387320 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-02-19 13:34
_PyErr_Occurred() is defined as:

static inline PyObject* _PyErr_Occurred(PyThreadState *tstate)
{
    assert(tstate != NULL);
    return tstate->curexc_type;
}
msg387322 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-02-19 14:08
New changeset a486054b24658fa623e030ddd4cc0cbfcac54ab0 by Victor Stinner in branch 'master':
bpo-43270: Remove private _PyErr_OCCURRED() macro (GH-24579)
https://github.com/python/cpython/commit/a486054b24658fa623e030ddd4cc0cbfcac54ab0
History
Date User Action Args
2022-04-11 14:59:41adminsetgithub: 87436
2021-02-19 14:09:11vstinnersetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2021-02-19 14:08:56vstinnersetmessages: + msg387322
2021-02-19 13:34:28vstinnersetmessages: + msg387320
2021-02-19 13:31:38vstinnersetkeywords: + patch
stage: patch review
pull_requests: + pull_request23358
2021-02-19 13:30:20vstinnersetmessages: + msg387319
2021-02-19 13:25:53vstinnercreate