8000 Make Profile.c use PyThreadState_EnterTracing() (GH-4411) · cython/cython@cbddad2 · GitHub
[go: up one dir, main page]

Skip to content

Commit cbddad2

Browse files
authored
Make Profile.c use PyThreadState_EnterTracing() (GH-4411)
Instead of __Pyx_SetTracing(), Profile.c now uses PyThreadState_EnterTracing() and PyThreadState_LeaveTracing(), which were added to Python 3.11.0a2: python/cpython#28542 When these functions are used, Cython no longer sets directly PyThreadState.cframe.use_tracing.
1 parent f4710b3 commit cbddad2

File tree

1 file changed

+38
-21
lines changed

1 file changed

+38
-21
lines changed

Cython/Utility/Profile.c

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -55,23 +55,48 @@
5555
#define __Pyx_TraceFrameInit(codeobj) \
5656
if (codeobj) $frame_code_cname = (PyCodeObject*) codeobj;
5757

58-
#if PY_VERSION_HEX >= 0x030a00b1
58+
#if PY_VERSION_HEX >= 0x030b00a2
5959
#define __Pyx_IsTracing(tstate, check_tracing, check_funcs) \
6060
(unlikely((tstate)->cframe->use_tracing) && \
6161
(!(check_tracing) || !(tstate)->tracing) && \
6262
(!(check_funcs) || (tstate)->c_profilefunc || (CYTHON_TRACE && (tstate)->c_tracefunc)))
6363

64-
#define __Pyx_SetTracing(tstate, enable) \
65-
(tstate)->cframe->use_tracing = (enable)
64+
#define __Pyx_EnterTracing(tstate) PyThreadState_EnterTracing(tstate)
65+
66+
#define __Pyx_LeaveTracing(tstate) PyThreadState_LeaveTracing(tstate)
67+
68+
#elif PY_VERSION_HEX >= 0x030a00b1
69+
#define __Pyx_IsTracing(tstate, check_tracing, check_funcs) \
70+
(unlikely((tstate)->cframe->use_tracing) && \
71+
(!(check_tracing) || !(tstate)->tracing) && \
72+
(!(check_funcs) || (tstate)->c_profilefunc || (CYTHON_TRACE && (tstate)->c_tracefunc)))
73+
74+
#define __Pyx_EnterTracing(tstate) \
75+
do { tstate->tracing++; tstate->cframe->use_tracing = 0; } while (0)
76+
77+
#define __Pyx_LeaveTracing(tstate) \
78+
do { \
79+
tstate->tracing--; \
80+
tstate->use_tracing = ((CYTHON_TRACE && tstate->c_tracefunc != NULL) \
81+
|| tstate->c_profilefunc != NULL); \
82+
} while (0)
6683

6784
#else
6885
#define __Pyx_IsTracing(tstate, check_tracing, check_funcs) \
6986
(unlikely((tstate)->use_tracing) && \
7087
(!(check_tracing) || !(tstate)->tracing) && \
7188
(!(check_funcs) || (tstate)->c_profilefunc || (CYTHON_TRACE && (tstate)->c_tracefunc)))
7289

73-
#define __Pyx_SetTracing(tstate, enable) \
74-
(tstate)->use_tracing = (enable)
90+
#define __Pyx_EnterTracing(tstate) \
91+
do { tstate->tracing++; tstate->use_tracing = 0; } while (0)
92+
93+
#define __Pyx_LeaveTracing(tstate) \
94+
do { \
95+
tstate->tracing--; \
96+
tstate->cframe->use_tracing = ((CYTHON_TRACE && tstate->c_tracefunc != NULL) \
97+
|| tstate->c_profilefunc != NULL); \
98+
} while (0)
99+
75100
#endif
76101

77102
#ifdef WITH_THREAD
@@ -108,8 +133,7 @@
108133
if (likely(!__Pyx_use_tracing)); else { \
109134
PyThreadState* tstate = __Pyx_PyThreadState_Current; \
110135
if (__Pyx_IsTracing(tstate, 0, 1)) { \
111-
tstate->tracing++; \
112-
__Pyx_SetTracing(tstate, 0); \
136+
__Pyx_EnterTracing(tstate); \
113137
PyObject *exc_info = __Pyx_GetExceptionTuple(tstate); \
114138
if (exc_info) { \
115139
if (CYTHON_TRACE && tstate->c_tracefunc) \
@@ -119,23 +143,20 @@
119143
tstate->c_profileobj, $frame_cname, PyTrace_EXCEPTION, exc_info); \
120144
Py_DECREF(exc_info); \
121145
} \
122-
__Pyx_SetTracing(tstate, 1); \
123-
tstate->tracing--; \
146+
__Pyx_LeaveTracing(tstate); \
124147
} \
125148
}
126149

127150
static void __Pyx_call_return_trace_func(PyThreadState *tstate, PyFrameObject *frame, PyObject *result) {
128151
PyObject *type, *value, *traceback;
129152
__Pyx_ErrFetchInState(tstate, &type, &value, &traceback);
130-
tstate->tracing++;
131-
__Pyx_SetTracing(tstate, 0);
153+
__Pyx_EnterTracing(tstate);
132154
if (CYTHON_TRACE && tstate->c_tracefunc)
133155
tstate->c_tracefunc(tstate->c_traceobj, frame, PyTrace_RETURN, result);
134156
if (tstate->c_profilefunc)
135157
tstate->c_profilefunc(tstate->c_profileobj, frame, PyTrace_RETURN, result);
136158
CYTHON_FRAME_DEL(frame);
137-
__Pyx_SetTracing(tstate, 1);
138-
tstate->tracing--;
159+
__Pyx_LeaveTracing(tstate);
139160
__Pyx_ErrRestoreInState(tstate, type, value, traceback);
140161
}
141162

@@ -190,13 +211,11 @@
190211
PyObject *type, *value, *traceback;
191212
__Pyx_ErrFetchInState(tstate, &type, &value, &traceback);
192213
__Pyx_PyFrame_SetLineNumber(frame, lineno);
193-
tstate->tracing++;
194-
__Pyx_SetTracing(tstate, 0);
214+
__Pyx_EnterTracing(tstate);
195215

196216
ret = tstate->c_tracefunc(tstate->c_traceobj, frame, PyTrace_LINE, NULL);
197217

198-
__Pyx_SetTracing(tstate, 1);
199-
tstate->tracing--;
218+
__Pyx_LeaveTracing(tstate);
200219
if (likely(!ret)) {
201220
__Pyx_ErrRestoreInState(tstate, type, value, traceback);
202221
} else {
@@ -283,8 +302,7 @@ static int __Pyx_TraceSetupAndCall(PyCodeObject** code,
283302
__Pyx_PyFrame_SetLineNumber(*frame, firstlineno);
284303

285304
retval = 1;
286-
tstate->tracing++;
287-
__Pyx_SetTracing(tstate, 0);
305+
__Pyx_EnterTracing(tstate);
288306
__Pyx_ErrFetchInState(tstate, &type, &value, &traceback);
289307

290308
#if CYTHON_TRACE
@@ -294,8 +312,7 @@ static int __Pyx_TraceSetupAndCall(PyCodeObject** code,
294312
#endif
295313
retval = tstate->c_profilefunc(tstate->c_profileobj, *frame, PyTrace_CALL, NULL) == 0;
296314

297-
__Pyx_SetTracing(tstate, (tstate->c_profilefunc || (CYTHON_TRACE && tstate->c_tracefunc)));
298-
tstate->tracing--;
315+
__Pyx_LeaveTracing(tstate);
299316
if (retval) {
300317
__Pyx_ErrRestoreInState(tstate, type, value, traceback);
301318
return __Pyx_IsTracing(tstate, 0, 0) && retval;

0 commit comments

Comments
 (0)
0