Skip to content

Add PyThreadState_DisableTracing() #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----------
Expand Down Expand Up @@ -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
^^^^^^^^^^^^^^^^^^
Expand Down Expand Up @@ -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:

Expand Down
41 changes: 41 additions & 0 deletions pythoncapi_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,47 @@ PyThreadState_GetID(PyThreadState *tstate)
}
#endif

// 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)
{
#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.0a0
#if PY_VERSION_HEX < 0x030B00A0 && !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.0a0
#if PY_VERSION_HEX < 0x030B00A0 && !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
Expand Down
18 changes: 13 additions & 5 deletions tests/test_pythoncapi_compat_cext.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#endif

// Ignore reference count checks on PyPy
#if !defined(PYPY_VERSION)
#ifndef PYPY_VERSION
# define CHECK_REFCNT
#endif

Expand Down Expand Up @@ -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))
{
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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},
Expand Down