-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathclasses.cpp
More file actions
140 lines (123 loc) · 4.65 KB
/
Copy pathclasses.cpp
File metadata and controls
140 lines (123 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "classes.hpp"
#include "scorepy/cInstrumenter.hpp"
#include "scorepy/pythonHelpers.hpp"
#include <Python.h>
#include <type_traits>
static_assert(std::is_trivial<scorepy::CInstrumenter>::value,
"Must be trivial or object creation by Python is UB");
static_assert(std::is_standard_layout<scorepy::CInstrumenter>::value,
"Must be standard layout or object creation by Python is UB");
extern "C"
{
/// tp_new implementation that calls object.__new__ with not args to allow ABC classes to work
static PyObject* call_object_new(PyTypeObject* type, PyObject*, PyObject*)
{
scorepy::PyRefObject empty_tuple(PyTuple_New(0), scorepy::adopt_object);
if (!empty_tuple)
{
return nullptr;
}
scorepy::PyRefObject empty_dict(PyDict_New(), scorepy::adopt_object);
if (!empty_dict)
{
return nullptr;
}
return PyBaseObject_Type.tp_new(type, empty_tuple, empty_dict);
}
static void CInstrumenter_dealloc(scorepy::CInstrumenter* self)
{
self->deinit();
Py_TYPE(self)->tp_free(self->to_PyObject());
}
static int CInstrumenter_init(scorepy::CInstrumenter* self, PyObject* args, PyObject* kwds)
{
static const char* kwlist[] = { "interface", nullptr };
const char* interface_cstring;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", const_cast<char**>(kwlist),
&interface_cstring))
{
return -1;
}
const std::string interface_string = interface_cstring;
scorepy::InstrumenterInterface interface;
if (interface_string == "Trace")
{
interface = scorepy::InstrumenterInterface::Trace;
}
else if (interface_string == "Profile")
{
interface = scorepy::InstrumenterInterface::Profile;
}
else
{
PyErr_Format(PyExc_TypeError, "Expected 'Trace' or 'Profile', got '%s'",
interface_cstring);
return -1;
}
self->init(interface);
return 0;
}
static PyObject* CInstrumenter_get_interface(scorepy::CInstrumenter* self, void*)
{
const char* result =
self->interface == scorepy::InstrumenterInterface::Trace ? "Trace" : "Profile";
return PyUnicode_FromString(result);
}
static PyObject* CInstrumenter_enable_instrumenter(scorepy::CInstrumenter* self, PyObject*)
{
self->enable_instrumenter();
Py_RETURN_NONE;
}
static PyObject* CInstrumenter_disable_instrumenter(scorepy::CInstrumenter* self, PyObject*)
{
self->disable_instrumenter();
Py_RETURN_NONE;
}
static PyObject* CInstrumenter_call(scorepy::CInstrumenter* self, PyObject* args,
PyObject* kwds)
{
static const char* kwlist[] = { "frame", "event", "arg", nullptr };
PyFrameObject* frame;
const char* event;
PyObject* arg;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "OsO", const_cast<char**>(kwlist), &frame,
&event, &arg))
{
return nullptr;
}
return (*self)(*frame, event, arg);
}
}
namespace scorepy
{
PyTypeObject& getCInstrumenterType()
{
static PyMethodDef methods[] = {
{ "_enable_instrumenter", scorepy::cast_to_PyFunc(CInstrumenter_enable_instrumenter),
METH_NOARGS, "Enable the instrumenter" },
{ "_disable_instrumenter", scorepy::cast_to_PyFunc(CInstrumenter_disable_instrumenter),
METH_NOARGS, "Disable the instrumenter" },
{ nullptr } /* Sentinel */
};
static PyGetSetDef getseters[] = {
{ "interface", scorepy::cast_to_PyFunc(CInstrumenter_get_interface), nullptr,
"Return the used interface for instrumentation", nullptr },
{ nullptr } /* Sentinel */
};
// Sets the first few fields explicitely and remaining ones to zero
static PyTypeObject type = {
PyVarObject_HEAD_INIT(nullptr, 0) /* header */
"scorep._bindings.CInstrumenter", /* tp_name */
sizeof(CInstrumenter), /* tp_basicsize */
};
type.tp_new = call_object_new;
type.tp_init = scorepy::cast_to_PyFunc(CInstrumenter_init);
type.tp_dealloc = scorepy::cast_to_PyFunc(CInstrumenter_dealloc);
type.tp_methods = methods;
type.tp_call = scorepy::cast_to_PyFunc(CInstrumenter_call);
type.tp_getset = getseters;
type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
type.tp_doc = "Class for the C instrumenter interface of Score-P";
return type;
}
} // namespace scorepy