88namespace scorepy
99{
1010
11+ void CInstrumenter::init (InstrumenterInterface interface)
12+ {
13+ this ->interface = interface;
14+ threading_module = PyImport_ImportModule (" threading" );
15+ if (threading_module)
16+ {
17+ const char * name = (interface == InstrumenterInterface::Trace) ? " settrace" : " setprofile" ;
18+ threading_set_instrumenter = PyObject_GetAttrString (threading_module, name);
19+ }
20+ }
21+
22+ void CInstrumenter::deinit ()
23+ {
24+ Py_CLEAR (threading_module);
25+ Py_CLEAR (threading_set_instrumenter);
26+ }
27+
1128void CInstrumenter::enable_instrumenter ()
1229{
1330 const auto callback = [](PyObject* obj, PyFrameObject* frame, int what, PyObject* arg) -> int {
1431 return from_PyObject (obj)->on_event (*frame, what, arg) ? 0 : -1 ;
1532 };
33+ if (threading_set_instrumenter)
34+ {
35+ PyRefObject result (PyObject_CallFunction (threading_set_instrumenter, " O" , to_PyObject ()),
36+ adopt_object);
37+ }
1638 if (interface == InstrumenterInterface::Trace)
1739 PyEval_SetTrace (callback, to_PyObject ());
1840 else
@@ -25,6 +47,11 @@ void CInstrumenter::disable_instrumenter()
2547 PyEval_SetTrace (nullptr , nullptr );
2648 else
2749 PyEval_SetProfile (nullptr , nullptr );
50+ if (threading_set_instrumenter)
51+ {
52+ PyRefObject result (PyObject_CallFunction (threading_set_instrumenter, " O" , Py_None),
53+ adopt_object);
54+ }
2855}
2956
3057// / Mapping of PyTrace_* to it's string representations
0 commit comments