8000 Avoid deprecated Py_OptimizeFlag in Py3.12 (GH-5343) · cython/cython@639ada4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 639ada4

Browse files
committed
Avoid deprecated Py_OptimizeFlag in Py3.12 (GH-5343)
* Work around the deprecation of Py_OptimizeFlag in Py3.12 by reading the value from the interpreter's current PyConfig. See python/cpython#99872 * Avoid access to PyConfig without holding the GIL when trying to read the old Py_OptimizeFlag. The flag was never meant to be modifiable and thus can be read once at module import time. See python/cpython#99872 (comment)
1 parent 5cef4fa commit 639ada4

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

Cython/Compiler/Builtin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ def init_builtins():
419419

420420
builtin_scope.declare_var(
421421
'__debug__', PyrexTypes.c_const_type(PyrexTypes.c_bint_type),
422-
pos=None, cname='(!Py_OptimizeFlag)', is_cdef=True)
422+
pos=None, cname='__pyx_assertions_enabled()', is_cdef=True)
423423

424424
global list_type, tuple_type, dict_type, set_type, frozenset_type
425425
global bytes_type, str_type, unicode_type, basestring_type, slice_type

Cython/Compiler/Nodes.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6214,8 +6214,10 @@ def analyse_expressions(self, env):
62146214
gil_message = "Raising exception"
62156215

62166216
def generate_execution_code(self, code):
6217+
code.globalstate.use_utility_code(
6218+
UtilityCode.load_cached("AssertionsEnabled", "Exceptions.c"))
62176219
code.putln("#ifndef CYTHON_WITHOUT_ASSERTIONS")
6218-
code.putln("if (unlikely(!Py_OptimizeFlag)) {")
6220+
code.putln("if (unlikely(__pyx_assertions_enabled())) {")
62196221
code.mark_pos(self.pos)
62206222
self.cond.generate_evaluation_code(code)
62216223
code.putln(

Cython/Utility/Exceptions.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,32 @@
66
// __Pyx_GetException()
77

88

9+
/////////////// AssertionsEnabled.init ///////////////
10+
__Pyx_init_assertions_enabled();
11+
12+
/////////////// AssertionsEnabled.proto ///////////////
13+
14+
#define __Pyx_init_assertions_enabled()
15+
16+
#if CYTHON_COMPILING_IN_PYPY && PY_VERSION_HEX < 0x02070600 && !defined(Py_OptimizeFlag)
17+
#define __pyx_assertions_enabled() (1)
18+
#elif PY_VERSION_HEX < 0x03080000 || CYTHON_COMPILING_IN_PYPY || defined(Py_LIMITED_API)
19+
#define __pyx_assertions_enabled() (!Py_OptimizeFlag)
20+
#elif CYTHON_COMPILING_IN_CPYTHON && PY_VERSION_HEX >= 0x030900A6
21+
// Py3.8+ has PyConfig from PEP 587, but only Py3.9 added read access to it.
22+
// Py_OptimizeFlag is deprecated in Py3.12+
23+
static int __pyx_assertions_enabled_flag;
24+
#define __pyx_assertions_enabled() (__pyx_assertions_enabled_flag)
25+
26+
#undef __Pyx_init_assertions_enabled
27+
static void __Pyx_init_assertions_enabled(void) {
28+
__pyx_assertions_enabled_flag = ! _PyInterpreterState_GetConfig(__Pyx_PyThreadState_Current->interp)->optimization_level;
29+
}
30+
#else
31+
#define __pyx_assertions_enabled() (!Py_OptimizeFlag)
32+
#endif
33+
34+
935
/////////////// PyThreadStateGet.proto ///////////////
1036
//@substitute: naming
1137

Cython/Utility/ModuleSetupCode.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -445,10 +445,6 @@ class __Pyx_FakeReference {
445445

446446
/////////////// PythonCompatibility ///////////////
447447

448-
#if CYTHON_COMPILING_IN_PYPY && PY_VERSION_HEX < 0x02070600 && !defined(Py_OptimizeFlag)
449-
#define Py_OptimizeFlag 0
450-
#endif
451-
452448
#define __PYX_BUILD_PY_SSIZE_T "n"
453449
#define CYTHON_FORMAT_SSIZE_T "z"
454450

0 commit comments

Comments
 (0)
0