|
| 1 | +#include "classes.hpp" |
| 2 | +#include "scorepy/cInstrumenter.hpp" |
| 3 | +#include "scorepy/pythonHelpers.hpp" |
| 4 | +#include <Python.h> |
| 5 | +#include <type_traits> |
| 6 | + |
| 7 | +static_assert(std::is_trivial<scorepy::CInstrumenter>::value, |
| 8 | + "Must be trivial or object creation by Python is UB"); |
| 9 | +static_assert(std::is_standard_layout<scorepy::CInstrumenter>::value, |
| 10 | + "Must be standard layout or object creation by Python is UB"); |
| 11 | + |
| 12 | +extern "C" |
| 13 | +{ |
| 14 | + |
| 15 | + /// tp_new implementation that calls object.__new__ with not args to allow ABC classes to work |
| 16 | + static PyObject* call_object_new(PyTypeObject* type, PyObject*, PyObject*) |
| 17 | + { |
| 18 | + scorepy::PyRefObject empty_tuple(PyTuple_New(0), scorepy::adopt_object); |
| 19 | + if (!empty_tuple) |
| 20 | + return nullptr; |
| 21 | + scorepy::PyRefObject empty_dict(PyDict_New(), scorepy::adopt_object); |
| 22 | + if (!empty_dict) |
| 23 | + return nullptr; |
| 24 | + return PyBaseObject_Type.tp_new(type, empty_tuple, empty_dict); |
| 25 | + } |
| 26 | + |
| 27 | + static int CInstrumenter_init(scorepy::CInstrumenter* self, PyObject* args, PyObject* kwds) |
| 28 | + { |
| 29 | + static const char* kwlist[] = { "tracingOrProfiling", nullptr }; |
| 30 | + int tracingOrProfiling; |
| 31 | + |
| 32 | + if (!PyArg_ParseTupleAndKeywords(args, kwds, "p", const_cast<char**>(kwlist), |
| 33 | + &tracingOrProfiling)) |
| 34 | + return -1; |
| 35 | + |
| 36 | + self->init(tracingOrProfiling != 0); |
| 37 | + return 0; |
| 38 | + } |
| 39 | + |
| 40 | + static PyObject* CInstrumenter_get_tracingOrProfiling(scorepy::CInstrumenter* self, void*) |
| 41 | + { |
| 42 | + scorepy::PyRefObject result(self->tracingOrProfiling ? Py_True : Py_False, |
| 43 | + scorepy::retain_object); |
| 44 | + return result; |
| 45 | + } |
| 46 | + |
| 47 | + static PyObject* CInstrumenter_enable_instrumenter(scorepy::CInstrumenter* self, PyObject*) |
| 48 | + { |
| 49 | + self->enable_instrumenter(); |
| 50 | + Py_RETURN_NONE; |
| 51 | + } |
| 52 | + |
| 53 | + static PyObject* CInstrumenter_disable_instrumenter(scorepy::CInstrumenter* self, PyObject*) |
| 54 | + { |
| 55 | + self->disable_instrumenter(); |
| 56 | + Py_RETURN_NONE; |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +namespace scorepy |
| 61 | +{ |
| 62 | + |
| 63 | +PyTypeObject& getCInstrumenterType() |
| 64 | +{ |
| 65 | + static PyMethodDef methods[] = { |
| 66 | + { "_enable_instrumenter", reinterpret_cast<PyCFunction>(CInstrumenter_enable_instrumenter), |
| 67 | + METH_NOARGS, "Enable the instrumenter" }, |
| 68 | + { "_disable_instrumenter", |
| 69 | + reinterpret_cast<PyCFunction>(CInstrumenter_disable_instrumenter), METH_NOARGS, |
| 70 | + "Disable the instrumenter" }, |
| 71 | + { nullptr } /* Sentinel */ |
| 72 | + }; |
| 73 | + static PyGetSetDef getseters[] = { |
| 74 | + { "tracingOrProfiling", reinterpret_cast<getter>(CInstrumenter_get_tracingOrProfiling), |
| 75 | + nullptr, "Return whether the trace (True) or profile (False) instrumentation is used", |
| 76 | + nullptr }, |
| 77 | + { nullptr } /* Sentinel */ |
| 78 | + }; |
| 79 | + // Sets the first few fields explicitely and remaining ones to zero |
| 80 | + static PyTypeObject type = { |
| 81 | + PyVarObject_HEAD_INIT(nullptr, 0) /* header */ |
| 82 | + "scorep.scorep_bindings.CInstrumenter", /* tp_name */ |
| 83 | + sizeof(CInstrumenter), /* tp_basicsize */ |
| 84 | + }; |
| 85 | + type.tp_new = call_object_new; |
| 86 | + type.tp_init = reinterpret_cast<initproc>(CInstrumenter_init); |
| 87 | + type.tp_methods = methods; |
| 88 | + type.tp_getset = getseters; |
| 89 | + type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE; |
| 90 | + type.tp_doc = "Class for the C instrumenter interface of Score-P"; |
| 91 | + return type; |
| 92 | +} |
| 93 | + |
| 94 | +} // namespace scorepy |
0 commit comments