Skip to content

Commit d7ada9f

Browse files
committed
Use string/enum as the ctor parameter
1 parent 18ea7fc commit d7ada9f

5 files changed

Lines changed: 39 additions & 23 deletions

File tree

scorep/_instrumenters/scorep_cProfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class ScorepCProfile(scorep._bindings.CInstrumenter, ScorepInstrumenter):
66
def __init__(self, enable_instrumenter):
7-
scorep._bindings.CInstrumenter.__init__(self, tracing_or_profiling=False)
7+
scorep._bindings.CInstrumenter.__init__(self, interface='Profile')
88
ScorepInstrumenter.__init__(self, enable_instrumenter)
99

1010
def _enable_instrumenter(self):

scorep/_instrumenters/scorep_cTrace.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class ScorepCTrace(scorep._bindings.CInstrumenter, ScorepInstrumenter):
66
def __init__(self, enable_instrumenter):
7-
scorep._bindings.CInstrumenter.__init__(self, tracing_or_profiling=True)
7+
scorep._bindings.CInstrumenter.__init__(self, interface='Trace')
88
ScorepInstrumenter.__init__(self, enable_instrumenter)
99

1010
def _enable_instrumenter(self):

src/classes.cpp

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,35 @@ extern "C"
2626

2727
static int CInstrumenter_init(scorepy::CInstrumenter* self, PyObject* args, PyObject* kwds)
2828
{
29-
static const char* kwlist[] = { "tracing_or_profiling", nullptr };
30-
int tracing_or_profiling;
29+
static const char* kwlist[] = { "interface", nullptr };
30+
const char* interface_cstring;
3131

32-
if (!PyArg_ParseTupleAndKeywords(args, kwds, "p", const_cast<char**>(kwlist),
33-
&tracing_or_profiling))
32+
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", const_cast<char**>(kwlist),
33+
&interface_cstring))
3434
return -1;
3535

36-
self->init(tracing_or_profiling != 0);
36+
const std::string interface_string = interface_cstring;
37+
scorepy::InstrumenterInterface interface;
38+
if (interface_string == "Trace")
39+
interface = scorepy::InstrumenterInterface::Trace;
40+
else if (interface_string == "Profile")
41+
interface = scorepy::InstrumenterInterface::Profile;
42+
else
43+
{
44+
PyErr_Format(PyExc_TypeError, "Expected 'Trace' or 'Profile', got '%s'",
45+
interface_cstring);
46+
return -1;
47+
}
48+
49+
self->init(interface);
3750
return 0;
3851
}
3952

40-
static PyObject* CInstrumenter_get_tracingOrProfiling(scorepy::CInstrumenter* self, void*)
53+
static PyObject* CInstrumenter_get_interface(scorepy::CInstrumenter* self, void*)
4154
{
42-
scorepy::PyRefObject result(self->tracing_or_profiling ? Py_True : Py_False,
43-
scorepy::retain_object);
44-
return result;
55+
const char* result =
56+
self->interface == scorepy::InstrumenterInterface::Trace ? "Trace" : "Profile";
57+
return PyUnicode_FromString(result);
4558
}
4659

4760
static PyObject* CInstrumenter_enable_instrumenter(scorepy::CInstrumenter* self, PyObject*)
@@ -85,9 +98,8 @@ PyTypeObject& getCInstrumenterType()
8598
{ nullptr } /* Sentinel */
8699
};
87100
static PyGetSetDef getseters[] = {
88-
{ "tracing_or_profiling", scorepy::cast_to_PyFunc(CInstrumenter_get_tracingOrProfiling),
89-
nullptr, "Return whether the trace (True) or profile (False) instrumentation is used",
90-
nullptr },
101+
{ "interface", scorepy::cast_to_PyFunc(CInstrumenter_get_interface), nullptr,
102+
"Return the used interface for instrumentation", nullptr },
91103
{ nullptr } /* Sentinel */
92104
};
93105
// Sets the first few fields explicitely and remaining ones to zero

src/scorepy/cInstrumenter.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,15 @@ void CInstrumenter::enable_instrumenter()
2121
const auto callback = [](PyObject* obj, PyFrameObject* frame, int what, PyObject* arg) -> int {
2222
return from_PyObject(obj)->on_event(*frame, what, arg) ? 0 : -1;
2323
};
24-
if (tracing_or_profiling)
25-
{
24+
if (interface == InstrumenterInterface::Trace)
2625
PyEval_SetTrace(callback, to_PyObject());
27-
}
2826
else
29-
{
3027
PyEval_SetProfile(callback, to_PyObject());
31-
}
3228
}
3329

3430
void CInstrumenter::disable_instrumenter()
3531
{
36-
if (tracing_or_profiling)
32+
if (interface == InstrumenterInterface::Trace)
3733
PyEval_SetTrace(nullptr, nullptr);
3834
else
3935
PyEval_SetProfile(nullptr, nullptr);

src/scorepy/cInstrumenter.hpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,22 @@
55

66
namespace scorepy
77
{
8+
/// Interface to Python used to implement an instrumenter
9+
/// See sys.settrace/setprofile
10+
enum class InstrumenterInterface
11+
{
12+
Profile,
13+
Trace
14+
};
15+
816
struct CInstrumenter
917
{
1018
PyObject_HEAD;
11-
bool tracing_or_profiling;
19+
InstrumenterInterface interface;
1220

13-
void init(bool tracing_or_profiling)
21+
void init(InstrumenterInterface interface)
1422
{
15-
this->tracing_or_profiling = tracing_or_profiling;
23+
this->interface = interface;
1624
}
1725
void enable_instrumenter();
1826
void disable_instrumenter();

0 commit comments

Comments
 (0)