[New-bugs-announce] [issue43270] [C API] Remove _PyErr_OCCURRED() macro

STINNER Victor report at bugs.python.org
Fri Feb 19 08:25:53 EST 2021


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

The private _PyErr_OCCURRED() function was introduced to optimize Objects/setobject.c:

commit 5ba0cbe39221ff8985ce5a4702a3b01a17ae3248
Author: Raymond Hettinger <python at 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 at 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.

----------
components: C API
messages: 387318
nosy: vstinner
priority: normal
severity: normal
status: open
title: [C API] Remove _PyErr_OCCURRED() macro
versions: Python 3.10

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


More information about the New-bugs-announce mailing list