Skip to content

Commit 74f17f7

Browse files
authored
Support Python 3.11 (#159)
Adds support for Python 3.11, since they changed their C-API (see #151 fixes #159 fixes #158
2 parents b5ef177 + f3584dd commit 74f17f7

5 files changed

Lines changed: 587 additions & 10 deletions

File tree

.github/workflows/unit_tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
runs-on: ubuntu-latest
1212
strategy:
1313
matrix:
14-
python: ["2.7", "3.7", "3.8", "3.9", "3.10", "3.11", 'pypy-2.7', 'pypy-3.6', 'pypy-3.7', 'pypy-3.8']
14+
python: ["2.7", "3.7", "3.8", "3.9", "3.10", "3.11", 'pypy-2.7', 'pypy-3.6', 'pypy-3.7', 'pypy-3.8', 'pypy-3.9']
1515
fail-fast: false
1616

1717
steps:

src/scorepy/cInstrumenter.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "cInstrumenter.hpp"
22
#include "events.hpp"
33
#include "pythonHelpers.hpp"
4+
#include "pythoncapi_compat.h"
45
#include <algorithm>
56
#include <array>
67
#include <cstdint>
@@ -114,7 +115,7 @@ bool CInstrumenter::on_event(PyFrameObject& frame, int what, PyObject*)
114115
{
115116
case PyTrace_CALL:
116117
{
117-
PyCodeObject* code = frame.f_code;
118+
PyCodeObject* code = PyFrame_GetCode(&frame);
118119
bool success = try_region_begin(code);
119120
if (!success)
120121
{
@@ -127,11 +128,12 @@ bool CInstrumenter::on_event(PyFrameObject& frame, int what, PyObject*)
127128
region_begin(name, module_name, file_name, line_number, code);
128129
}
129130
}
131+
Py_DECREF(code);
130132
break;
131133
}
132134
case PyTrace_RETURN:
133135
{
134-
PyCodeObject* code = frame.f_code;
136+
PyCodeObject* code = PyFrame_GetCode(&frame);
135137
bool success = try_region_end(code);
136138
if (!success)
137139
{
@@ -143,6 +145,7 @@ bool CInstrumenter::on_event(PyFrameObject& frame, int what, PyObject*)
143145
region_end(name, module_name, code);
144146
}
145147
}
148+
Py_DECREF(code);
146149
break;
147150
}
148151
}

src/scorepy/pythonHelpers.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,36 @@
11
#include "pythonHelpers.hpp"
22
#include "compat.hpp"
33
#include "pathUtils.hpp"
4+
#include "pythoncapi_compat.h"
45

56
#include <stdlib.h>
67

78
namespace scorepy
89
{
9-
std::string_view get_module_name(const PyFrameObject& frame)
10+
std::string_view get_module_name(PyFrameObject& frame)
1011
{
11-
PyObject* module_name = PyDict_GetItemString(frame.f_globals, "__name__");
12+
PyObject* globals = PyFrame_GetGlobals(&frame);
13+
PyObject* module_name = PyDict_GetItemString(globals, "__name__");
14+
Py_DECREF(globals);
1215
if (module_name)
1316
return compat::get_string_as_utf_8(module_name);
1417

1518
// this is a NUMPY special situation, see NEP-18, and Score-P issue #63
1619
// TODO: Use string_view/C-String to avoid creating 2 std::strings
17-
std::string_view filename = compat::get_string_as_utf_8(frame.f_code->co_filename);
20+
PyCodeObject* code = PyFrame_GetCode(&frame);
21+
std::string_view filename = compat::get_string_as_utf_8(code->co_filename);
22+
Py_DECREF(code);
1823
if ((filename.size() > 0) && (filename == "<__array_function__ internals>"))
1924
return std::string_view("numpy.__array_function__");
2025
else
2126
return std::string_view("unkown");
2227
}
2328

24-
std::string get_file_name(const PyFrameObject& frame)
29+
std::string get_file_name(PyFrameObject& frame)
2530
{
26-
PyObject* filename = frame.f_code->co_filename;
31+
PyCodeObject* code = PyFrame_GetCode(&frame);
32+
PyObject* filename = code->co_filename;
33+
Py_DECREF(code);
2734
if (filename == Py_None)
2835
{
2936
return "None";

src/scorepy/pythonHelpers.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ auto cast_to_PyFunc(TFunc* func) -> detail::ReplaceArgsToPyObject_t<TFunc>*
7373

7474
/// Return the module name the frame belongs to.
7575
/// The pointer is valid for the lifetime of the frame
76-
std::string_view get_module_name(const PyFrameObject& frame);
76+
std::string_view get_module_name(PyFrameObject& frame);
7777
/// Return the file name the frame belongs to
78-
std::string get_file_name(const PyFrameObject& frame);
78+
std::string get_file_name(PyFrameObject& frame);
7979

8080
// Implementation details
8181
namespace detail

0 commit comments

Comments
 (0)