Skip to content

Commit 9b49da5

Browse files
authored
Merge pull request #268 from vstinner/enter_tracing2
Use PyThreadState_EnterTracing()
2 parents 37cf47a + f82bd1a commit 9b49da5

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

src/greenlet/greenlet.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -702,13 +702,9 @@ g_calltrace(PyObject* tracefunc, PyObject* event, PyGreenlet* origin,
702702
PyThreadState* tstate;
703703
PyErr_Fetch(&exc_type, &exc_val, &exc_tb);
704704
tstate = PyThreadState_GET();
705-
tstate->tracing++;
706-
TSTATE_USE_TRACING(tstate) = 0;
705+
PyThreadState_EnterTracing(tstate);
707706
retval = PyObject_CallFunction(tracefunc, "O(OO)", event, origin, target);
708-
tstate->tracing--;
709-
TSTATE_USE_TRACING(tstate) =
710-
(tstate->tracing <= 0 &&
711-
((tstate->c_tracefunc != NULL) || (tstate->c_profilefunc != NULL)));
707+
PyThreadState_LeaveTracing(tstate);
712708
if (retval == NULL) {
713709
/* In case of exceptions trace function is removed */
714710
GET_THREAD_STATE().state().del_tracefunc();

src/greenlet/greenlet_cpython_compat.hpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@ Python 3.10 beta 1 changed tstate->use_tracing to a nested cframe member.
2020
See https://github.com/python/cpython/pull/25276
2121
We have to save and restore this as well.
2222
*/
23-
# define TSTATE_USE_TRACING(tstate) (tstate->cframe->use_tracing)
2423
# define GREENLET_USE_CFRAME 1
2524
#else
26-
# define TSTATE_USE_TRACING(tstate) (tstate->use_tracing)
2725
# define GREENLET_USE_CFRAME 0
2826
#endif
2927

@@ -95,4 +93,32 @@ PyObject* PyModule_Create(PyModuleDef* m)
9593
}
9694
#endif
9795

96+
// bpo-43760 added PyThreadState_EnterTracing() to Python 3.11.0a2
97+
#if PY_VERSION_HEX < 0x030B00A2 && !defined(PYPY_VERSION)
98+
static inline void PyThreadState_EnterTracing(PyThreadState *tstate)
99+
{
100+
tstate->tracing++;
101+
#if PY_VERSION_HEX >= 0x030A00A1
102+
tstate->cframe->use_tracing = 0;
103+
#else
104+
tstate->use_tracing = 0;
105+
#endif
106+
}
107+
#endif
108+
109+
// bpo-43760 added PyThreadState_LeaveTracing() to Python 3.11.0a2
110+
#if PY_VERSION_HEX < 0x030B00A2 && !defined(PYPY_VERSION)
111+
static inline void PyThreadState_LeaveTracing(PyThreadState *tstate)
112+
{
113+
tstate->tracing--;
114+
int use_tracing = (tstate->c_tracefunc != NULL
115+
|| tstate->c_profilefunc != NULL);
116+
#if PY_VERSION_HEX >= 0x030A00A1
117+
tstate->cframe->use_tracing = use_tracing;
118+
#else
119+
tstate->use_tracing = use_tracing;
120+
#endif
121+
}
122+
#endif
123+
98124
#endif /* GREENLET_CPYTHON_COMPAT_H */

0 commit comments

Comments
 (0)