From 350872d6a9e443f1c20932b443bc37644d541264 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Fri, 24 Sep 2021 14:27:08 +0200 Subject: [PATCH 1/2] Add PyThreadState_DisableTracing() Add PyThreadState_IsTracing(), PyThreadState_DisableTracing() and PyThreadState_ResetTracing() functions. --- README.rst | 10 ++++++- pythoncapi_compat.h | 41 +++++++++++++++++++++++++++++ tests/test_pythoncapi_compat_cext.c | 18 +++++++++---- 3 files changed, 63 insertions(+), 6 deletions(-) diff --git a/README.rst b/README.rst index c6e6ce8..1206a11 100644 --- a/README.rst +++ b/README.rst @@ -156,7 +156,7 @@ Upgrade Operations pythoncapi_compat.h functions ============================= -Some functions related to frame objects are not available on PyPy. +Some functions related to frame objects and tracing are not available on PyPy. Python 3.10 ----------- @@ -207,6 +207,12 @@ PyThreadState PyInterpreterState* PyThreadState_GetInterpreter(PyThreadState *tstate); // Availability: Python 3.7. Not available on PyPy. uint64_t PyThreadState_GetID(PyThreadState *tstate); + // Not available on PyPy. + int PyThreadState_IsTracing(PyThreadState *tstate); + // Not available on PyPy. + void PyThreadState_DisableTracing(PyThreadState *tstate); + // Not available on PyPy. + void PyThreadState_ResetTracing(PyThreadState *tstate); PyInterpreterState ^^^^^^^^^^^^^^^^^^ @@ -337,6 +343,8 @@ Links Changelog ========= +* 2021-09-24: Add PyThreadState_IsTracing(), PyThreadState_DisableTracing() + and PyThreadState_ResetTracing() functions. * 2021-04-09: Add Py_Is(), Py_IsNone(), Py_IsTrue(), Py_IsFalse() functions. * 2021-04-01: diff --git a/pythoncapi_compat.h b/pythoncapi_compat.h index e1662b9..e6b2e47 100644 --- a/pythoncapi_compat.h +++ b/pythoncapi_compat.h @@ -251,6 +251,47 @@ PyThreadState_GetID(PyThreadState *tstate) } #endif +// bpo-43760 added PyThreadState_IsTracing() to Python 3.11.0a1 +#if PY_VERSION_HEX < 0x030B00A1 && !defined(PYPY_VERSION) +static inline int +PyThreadState_IsTracing(PyThreadState *tstate) +{ +#if PY_VERSION_HEX >= 0X030A00B1 + return tstate->cframe->use_tracing; +#else + return tstate->use_tracing; +#endif +} +#endif + +// bpo-43760 added PyThreadState_DisableTracing() to Python 3.11.0a1 +#if PY_VERSION_HEX < 0x030B00A1 && !defined(PYPY_VERSION) +static inline void +PyThreadState_DisableTracing(PyThreadState *tstate) +{ +#if PY_VERSION_HEX >= 0X030A00B1 + tstate->cframe->use_tracing = 0; +#else + tstate->use_tracing = 0; +#endif +} +#endif + +// bpo-43760 added PyThreadState_ResetTracing() to Python 3.11.0a1 +#if PY_VERSION_HEX < 0x030B00A1 && !defined(PYPY_VERSION) +static inline void +PyThreadState_ResetTracing(PyThreadState *tstate) +{ +#if PY_VERSION_HEX >= 0X030A00B1 + tstate->cframe->use_tracing = (tstate->c_tracefunc != NULL + || tstate->c_profilefunc != NULL); +#else + tstate->use_tracing = (tstate->c_tracefunc != NULL + || tstate->c_profilefunc != NULL); +#endif +} +#endif + // bpo-37194 added PyObject_CallNoArgs() to Python 3.9.0a1 #if PY_VERSION_HEX < 0x030900A1 diff --git a/tests/test_pythoncapi_compat_cext.c b/tests/test_pythoncapi_compat_cext.c index 147bf46..38fcbcb 100644 --- a/tests/test_pythoncapi_compat_cext.c +++ b/tests/test_pythoncapi_compat_cext.c @@ -12,7 +12,7 @@ #endif // Ignore reference count checks on PyPy -#if !defined(PYPY_VERSION) +#ifndef PYPY_VERSION # define CHECK_REFCNT #endif @@ -149,7 +149,7 @@ test_steal_ref(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored)) } -#if !defined(PYPY_VERSION) +#ifndef PYPY_VERSION static PyObject * test_frame(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored)) { @@ -214,7 +214,7 @@ test_thread_state(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored)) PyInterpreterState *interp = PyThreadState_GetInterpreter(tstate); assert(interp != NULL); -#if !defined(PYPY_VERSION) +#ifndef PYPY_VERSION // test PyThreadState_GetFrame() PyFrameObject *frame = PyThreadState_GetFrame(tstate); if (frame != NULL) { @@ -228,6 +228,14 @@ test_thread_state(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored)) assert(id > 0); #endif +#ifndef PYPY_VERSION + // PyThreadState_IsTracing(), PyThreadState_DisableTracing(), + // PyThreadState_ResetTracing() + PyThreadState_DisableTracing(tstate); + assert(PyThreadState_IsTracing(tstate) == 0); + PyThreadState_ResetTracing(tstate); +#endif + Py_RETURN_NONE; } @@ -283,7 +291,7 @@ test_gc(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored)) Py_INCREF(Py_None); PyTuple_SET_ITEM(tuple, 0, Py_None); -#if !defined(PYPY_VERSION) +#ifndef PYPY_VERSION // test PyObject_GC_IsTracked() int tracked = PyObject_GC_IsTracked(tuple); assert(tracked); @@ -394,7 +402,7 @@ static struct PyMethodDef methods[] = { {"test_object", test_object, METH_NOARGS, NULL}, {"test_py_is", test_py_is, METH_NOARGS, NULL}, {"test_steal_ref", test_steal_ref, METH_NOARGS, NULL}, -#if !defined(PYPY_VERSION) +#ifndef PYPY_VERSION {"test_frame", test_frame, METH_NOARGS, NULL}, #endif {"test_thread_state", test_thread_state, METH_NOARGS, NULL}, From 48e7149e473562ebbfb2d77951487426c89b59cf Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Fri, 24 Sep 2021 14:32:55 +0200 Subject: [PATCH 2/2] Fix for Python 3.11.0a0 --- pythoncapi_compat.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pythoncapi_compat.h b/pythoncapi_compat.h index e6b2e47..c793d33 100644 --- a/pythoncapi_compat.h +++ b/pythoncapi_compat.h @@ -251,8 +251,8 @@ PyThreadState_GetID(PyThreadState *tstate) } #endif -// bpo-43760 added PyThreadState_IsTracing() to Python 3.11.0a1 -#if PY_VERSION_HEX < 0x030B00A1 && !defined(PYPY_VERSION) +// bpo-43760 added PyThreadState_IsTracing() to Python 3.11.0a0 +#if PY_VERSION_HEX < 0x030B00A0 && !defined(PYPY_VERSION) static inline int PyThreadState_IsTracing(PyThreadState *tstate) { @@ -264,8 +264,8 @@ PyThreadState_IsTracing(PyThreadState *tstate) } #endif -// bpo-43760 added PyThreadState_DisableTracing() to Python 3.11.0a1 -#if PY_VERSION_HEX < 0x030B00A1 && !defined(PYPY_VERSION) +// bpo-43760 added PyThreadState_DisableTracing() to Python 3.11.0a0 +#if PY_VERSION_HEX < 0x030B00A0 && !defined(PYPY_VERSION) static inline void PyThreadState_DisableTracing(PyThreadState *tstate) { @@ -277,8 +277,8 @@ PyThreadState_DisableTracing(PyThreadState *tstate) } #endif -// bpo-43760 added PyThreadState_ResetTracing() to Python 3.11.0a1 -#if PY_VERSION_HEX < 0x030B00A1 && !defined(PYPY_VERSION) +// bpo-43760 added PyThreadState_ResetTracing() to Python 3.11.0a0 +#if PY_VERSION_HEX < 0x030B00A0 && !defined(PYPY_VERSION) static inline void PyThreadState_ResetTracing(PyThreadState *tstate) {