Skip to content

Commit 19ab5ff

Browse files
godlygeekpablogsal
authored andcommitted
Add Python 3.13 compatibility
- Use the new `PyEval_SetProfileAllThreads` public function, instead of our hacky backport of it. - Handle `_Py_IsFinalizing` being promoted to a public API. - Handle the prototype for `_PyMem_GetCurrentAllocatorName` being removed. Signed-off-by: Matt Wozniski <[email protected]>
1 parent 88aa01a commit 19ab5ff

File tree

5 files changed

+25
-5
lines changed

5 files changed

+25
-5
lines changed

setup.py

+1
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ def build_js_files(self):
318318
"Programming Language :: Python :: 3.10",
319319
"Programming Language :: Python :: 3.11",
320320
"Programming Language :: Python :: 3.12",
321+
"Programming Language :: Python :: 3.13",
321322
"Programming Language :: Python :: Implementation :: CPython",
322323
"Topic :: Software Development :: Debuggers",
323324
],

src/memray/_memray/compat.cpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,19 @@ void
66
setprofileAllThreads(Py_tracefunc func, PyObject* arg)
77
{
88
assert(PyGILState_Check());
9-
9+
#if PY_VERSION_HEX >= 0x030D0000
10+
PyEval_SetProfileAllThreads(func, arg);
11+
#else
1012
PyThreadState* this_tstate = PyThreadState_Get();
1113
PyInterpreterState* interp = threadStateGetInterpreter(this_tstate);
1214
for (PyThreadState* tstate = PyInterpreterState_ThreadHead(interp); tstate != nullptr;
1315
tstate = PyThreadState_Next(tstate))
1416
{
15-
#if PY_VERSION_HEX >= 0x03090000
17+
# if PY_VERSION_HEX >= 0x03090000
1618
if (_PyEval_SetProfile(tstate, func, arg) < 0) {
1719
_PyErr_WriteUnraisableMsg("in PyEval_SetProfileAllThreads", nullptr);
1820
}
19-
#else
21+
# else
2022
// For 3.7 and 3.8, backport _PyEval_SetProfile from 3.9
2123
// https://github.com/python/cpython/blob/v3.9.13/Python/ceval.c#L4738-L4767
2224
PyObject* profileobj = tstate->c_profileobj;
@@ -33,8 +35,9 @@ setprofileAllThreads(Py_tracefunc func, PyObject* arg)
3335

3436
/* Flag that tracing or profiling is turned on */
3537
tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
36-
#endif
38+
# endif
3739
}
40+
#endif
3841
}
3942

4043
} // namespace memray::compat

src/memray/_memray/compat.h

+10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@
77

88
namespace memray::compat {
99

10+
inline int
11+
isPythonFinalizing()
12+
{
13+
#if PY_VERSION_HEX >= 0x030D0000
14+
return Py_IsFinalizing();
15+
#else
16+
return _Py_IsFinalizing();
17+
#endif
18+
}
19+
1020
inline bool
1121
isEntryFrame(PyFrameObject* frame)
1222
{

src/memray/_memray/record_writer.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
#include "records.h"
1212
#include "snapshot.h"
1313

14+
#if PY_VERSION_HEX >= 0x030D0000
15+
// This function still exists in 3.13 but Python.h no longer has its prototype.
16+
extern "C" const char*
17+
_PyMem_GetCurrentAllocatorName();
18+
#endif
19+
1420
namespace memray::tracking_api {
1521

1622
using namespace std::chrono;

src/memray/_memray/tracking_api.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ Tracker::~Tracker()
603603
d_patcher.restore_symbols();
604604
}
605605

606-
if (Py_IsInitialized() && !_Py_IsFinalizing()) {
606+
if (Py_IsInitialized() && !compat::isPythonFinalizing()) {
607607
PyGILState_STATE gstate;
608608
gstate = PyGILState_Ensure();
609609

0 commit comments

Comments
 (0)