8000 Avoid access to PyConfig without holding the GIL when trying to read … · scoder/cython@1ff7673 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1ff7673

Browse files
committed
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 56a0d1b commit 1ff7673

File tree

4 files changed

+30
-14
lines changed

4 files changed

+30
-14
lines changed

Cython/Compiler/Builtin.py

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

478478
builtin_scope.declare_var(
479479
'__debug__', PyrexTypes.c_const_type(PyrexTypes.c_bint_type),
480-
pos=None, cname='(!__pyx_get_Py_OptimizeFlag())', is_cdef=True)
480+
pos=None, cname='__pyx_assertions_enabled()', is_cdef=True)
481481

482482
global type_type, list_type, tuple_type, dict_type, set_type, frozenset_type
483483
global slice_type, bytes_type, str_type, unicode_type, basestring_type, bytearray_type

Cython/Compiler/Nodes.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6978,8 +6978,10 @@ def analyse_expressions(self, env):
69786978
return self
69796979

69806980
def generate_execution_code(self, code):
6981+
code.globalstate.use_utility_code(
6982+
UtilityCode.load_cached("AssertionsEnabled", "Exceptions.c"))
69816983
code.putln("#ifndef CYTHON_WITHOUT_ASSERTIONS")
6982-
code.putln("if (unlikely(!__pyx_get_Py_OptimizeFlag())) {")
6984+
code.putln("if (unlikely(__pyx_assertions_enabled())) {")
69836985
code.mark_pos(self.pos)
69846986
self.condition.generate_evaluation_code(code)
69856987
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
/////////////// ErrOccurredWithGIL.proto ///////////////
1036
static CYTHON_INLINE int __Pyx_ErrOccurredWithGIL(void); /* proto */
1137

Cython/Utility/ModuleSetupCode.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -613,18 +613,6 @@ class __Pyx_FakeReference {
613613

614614
/////////////// PythonCompatibility ///////////////
615615

616-
#if CYTHON_COMPILING_IN_PYPY && PY_VERSION_HEX < 0x02070600 && !defined(Py_OptimizeFlag)
617-
#define __pyx_get_Py_OptimizeFlag() (0)
618-
#elif PY_VERSION_HEX < 0x03080000 || CYTHON_COMPILING_IN_PYPY || defined(Py_LIMITED_API)
619-
#define __pyx_get_Py_OptimizeFlag() (Py_OptimizeFlag)
620-
#elif CYTHON_COMPILING_IN_CPYTHON && PY_VERSION_HEX >= 0x030900A6
621-
// Py3.8+ has PyConfig from PEP 587, but only Py3.9 added read access to it.
622-
// Py_OptimizeFlag is deprecated in Py3.12+
623-
#define __pyx_get_Py_OptimizeFlag() (_PyInterpreterState_GetConfig(__Pyx_PyThreadState_Current->interp)->optimization_level)
624-
#else
625-
#define __pyx_get_Py_OptimizeFlag() (Py_OptimizeFlag)
626-
#endif
627-
628616
#define __PYX_BUILD_PY_SSIZE_T "n"
629617
#define CYTHON_FORMAT_SSIZE_T "z"
630618

0 commit comments

Comments
 (0)
0