Skip to content

Commit

Permalink
Work around the deprecation of Py_OptimizeFlag in Py3.12 by reading t…
Browse files Browse the repository at this point in the history
…he value from the interpreter's current PyConfig.

See python/cpython#99872
  • Loading branch information
scoder committed Nov 30, 2022
1 parent 6add7fe commit dfcf447
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cython/Compiler/Builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ def init_builtins():

builtin_scope.declare_var(
'__debug__', PyrexTypes.c_const_type(PyrexTypes.c_bint_type),
pos=None, cname='(!Py_OptimizeFlag)', is_cdef=True)
pos=None, cname='(!__pyx_get_Py_OptimizeFlag())', is_cdef=True)

global list_type, tuple_type, dict_type, set_type, frozenset_type
global bytes_type, str_type, unicode_type, basestring_type, slice_type
Expand Down
2 changes: 1 addition & 1 deletion Cython/Compiler/Nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6946,7 +6946,7 @@ def analyse_expressions(self, env):

def generate_execution_code(self, code):
code.putln("#ifndef CYTHON_WITHOUT_ASSERTIONS")
code.putln("if (unlikely(!Py_OptimizeFlag)) {")
code.putln("if (unlikely(!__pyx_get_Py_OptimizeFlag())) {")
code.mark_pos(self.pos)
self.condition.generate_evaluation_code(code)
code.putln(
Expand Down
10 changes: 9 additions & 1 deletion Cython/Utility/ModuleSetupCode.c
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,15 @@ class __Pyx_FakeReference {
/////////////// PythonCompatibility ///////////////

#if CYTHON_COMPILING_IN_PYPY && PY_VERSION_HEX < 0x02070600 && !defined(Py_OptimizeFlag)
#define Py_OptimizeFlag 0
#define __pyx_get_Py_OptimizeFlag() (0)
#elif PY_VERSION_HEX < 0x03080000 || CYTHON_COMPILING_IN_PYPY || defined(Py_LIMITED_API)
#define __pyx_get_Py_OptimizeFlag() (Py_OptimizeFlag)
#elif CYTHON_COMPILING_IN_CPYTHON && PY_VERSION_HEX >= 0x030900A6
// Py3.8+ has PyConfig from PEP 587, but only Py3.9 added read access to it.
// Py_OptimizeFlag is deprecated in Py3.12+
#define __pyx_get_Py_OptimizeFlag() (_PyInterpreterState_GetConfig(__Pyx_PyThreadState_Current->interp)->optimization_level)
#else
#define __pyx_get_Py_OptimizeFlag() (Py_OptimizeFlag)
#endif

#define __PYX_BUILD_PY_SSIZE_T "n"
Expand Down

0 comments on commit dfcf447

Please sign in to comment.