-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathPyROOTModule.cxx
More file actions
268 lines (220 loc) · 8.88 KB
/
Copy pathPyROOTModule.cxx
File metadata and controls
268 lines (220 loc) · 8.88 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Author: Enric Tejedor CERN 06/2018
// Original PyROOT code by Wim Lavrijsen, LBL
/*************************************************************************
* Copyright (C) 1995-2018, Rene Brun and Fons Rademakers. *
* All rights reserved. *
* *
* For the licensing terms see $ROOTSYS/LICENSE. *
* For the list of contributors see $ROOTSYS/README/CREDITS. *
*************************************************************************/
// Bindings
#include "PyROOTPythonize.h"
#include "RPyROOTApplication.h"
// Cppyy
#include "CPyCppyy/API.h"
// ROOT
#include "TInterpreter.h"
#include "TROOT.h"
#include "TSystem.h"
#include "RConfigure.h"
// Standard
#include <any>
#include <string>
#include <sstream>
#include <utility>
#include <vector>
#include "IOHandler.cxx"
namespace PyROOT {
PyObject *gRootModule = nullptr;
PyObject *RegisterConverterAlias(PyObject * /*self*/, PyObject *args)
{
PyObject *name = nullptr;
PyObject *target = nullptr;
if (!PyArg_ParseTuple(args, "UU:RegisterConverterAlias", &name, &target)) {
return nullptr;
}
const char *nameStr = PyUnicode_AsUTF8AndSize(name, nullptr);
if (!nameStr) {
return nullptr;
}
const char *targetStr = PyUnicode_AsUTF8AndSize(target, nullptr);
if (!targetStr) {
return nullptr;
}
CPyCppyy::RegisterConverterAlias(nameStr, targetStr);
Py_RETURN_NONE;
}
PyObject *RegisterExecutorAlias(PyObject * /*self*/, PyObject *args)
{
PyObject *name = nullptr;
PyObject *target = nullptr;
if (!PyArg_ParseTuple(args, "UU:RegisterExecutorAlias", &name, &target)) {
return nullptr;
}
const char *nameStr = PyUnicode_AsUTF8AndSize(name, nullptr);
if (!nameStr) {
return nullptr;
}
const char *targetStr = PyUnicode_AsUTF8AndSize(target, nullptr);
if (!targetStr) {
return nullptr;
}
CPyCppyy::RegisterExecutorAlias(nameStr, targetStr);
Py_RETURN_NONE;
}
/// \brief A PyObject wrapper to track reference counting of external objects
///
/// This wrapper can be useful in shared ownership scenarios when a C++ object
/// is created on the Python side and there is no easy way to track its ownership
/// on the C++ side. If multiple instances of this class are given the same
/// Python proxy, they will increase/decrease its reference counting following
/// Python rules and ensure proper destruction of the underlying C++ object when
/// no other Python objects are referencing it.
class PyObjRefCounter final {
PyObject *fObject{nullptr};
void Reset(PyObject *object)
{
if (fObject) {
Py_DecRef(fObject);
fObject = nullptr;
}
if (object) {
Py_INCREF(object);
fObject = object;
}
}
public:
PyObjRefCounter(PyObject *object) { Reset(object); }
~PyObjRefCounter() { Reset(nullptr); }
PyObjRefCounter(const PyObjRefCounter &other) { Reset(other.fObject); }
PyObjRefCounter &operator=(const PyObjRefCounter &other)
{
Reset(other.fObject);
return *this;
}
PyObjRefCounter(PyObjRefCounter &&other)
{
fObject = other.fObject;
other.fObject = nullptr;
}
PyObjRefCounter &operator=(PyObjRefCounter &&other)
{
fObject = other.fObject;
other.fObject = nullptr;
return *this;
}
};
PyObject *PyObjRefCounterAsStdAny(PyObject * /*self*/, PyObject *args)
{
PyObject *object = nullptr;
PyArg_ParseTuple(args, "O:PyObjRefCounterAsStdAny", &object);
// The std::any is managed by Python
return CPyCppyy::Instance_FromVoidPtr(new std::any{std::in_place_type<PyObjRefCounter>, object}, "std::any",
/*python_owns=*/true);
}
// Helper function to get the pointer to a buffer (heavily simplified copy of
// CPyCppyy::Utility::GetBuffer).
void GetBuffer(PyObject *pyobject, void *&buf)
{
buf = nullptr;
// Exclude text-like objects (policy decision)
if (PyBytes_Check(pyobject) || PyUnicode_Check(pyobject))
return;
// Fast path: bytearray
if (PyByteArray_CheckExact(pyobject)) {
buf = PyByteArray_AsString(pyobject);
return;
}
// Buffer protocol
if (PyObject_CheckBuffer(pyobject)) {
// Avoid potential issues with empty sequences
if (PySequence_Check(pyobject) && PySequence_Size(pyobject) == 0)
return;
Py_buffer view;
if (PyObject_GetBuffer(pyobject, &view, PyBUF_SIMPLE) == 0) {
if (view.buf && view.len > 0)
buf = view.buf;
PyBuffer_Release(&view);
} else {
PyErr_Clear();
}
}
}
} // namespace PyROOT
// Methods offered by the interface
static PyMethodDef gPyROOTMethods[] = {
{"AddCPPInstancePickling", (PyCFunction)PyROOT::AddCPPInstancePickling, METH_NOARGS,
"Add a custom pickling mechanism for Cppyy Python proxy objects"},
{"GetBranchAttr", (PyCFunction)PyROOT::GetBranchAttr, METH_VARARGS,
"Allow to access branches as tree attributes"},
{"AddTClassDynamicCastPyz", (PyCFunction)PyROOT::AddTClassDynamicCastPyz, METH_VARARGS,
"Cast the void* returned by TClass::DynamicCast to the right type"},
{"BranchPyz", (PyCFunction)PyROOT::BranchPyz, METH_VARARGS,
"Fully enable the use of TTree::Branch from Python"},
{"AddPrettyPrintingPyz", (PyCFunction)PyROOT::AddPrettyPrintingPyz, METH_VARARGS,
"Add pretty printing pythonization"},
{"InitApplication", (PyCFunction)PyROOT::RPyROOTApplication::InitApplication, METH_VARARGS,
"Initialize interactive ROOT use from Python"},
{"InstallGUIEventInputHook", (PyCFunction)PyROOT::RPyROOTApplication::InstallGUIEventInputHook, METH_NOARGS,
"Install an input hook to process GUI events"},
{"_CPPInstance__expand__", (PyCFunction)PyROOT::CPPInstanceExpand, METH_VARARGS,
"Deserialize a pickled object"},
{"JupyROOTExecutor", (PyCFunction)JupyROOTExecutor, METH_VARARGS, "Create JupyROOTExecutor"},
{"JupyROOTDeclarer", (PyCFunction)JupyROOTDeclarer, METH_VARARGS, "Create JupyROOTDeclarer"},
{"JupyROOTExecutorHandler_Clear", (PyCFunction)JupyROOTExecutorHandler_Clear, METH_NOARGS,
"Clear JupyROOTExecutorHandler"},
{"JupyROOTExecutorHandler_Ctor", (PyCFunction)JupyROOTExecutorHandler_Ctor, METH_NOARGS,
"Create JupyROOTExecutorHandler"},
{"JupyROOTExecutorHandler_Poll", (PyCFunction)JupyROOTExecutorHandler_Poll, METH_NOARGS,
"Poll JupyROOTExecutorHandler"},
{"JupyROOTExecutorHandler_EndCapture", (PyCFunction)JupyROOTExecutorHandler_EndCapture, METH_NOARGS,
"End capture JupyROOTExecutorHandler"},
{"JupyROOTExecutorHandler_InitCapture", (PyCFunction)JupyROOTExecutorHandler_InitCapture, METH_NOARGS,
"Init capture JupyROOTExecutorHandler"},
{"JupyROOTExecutorHandler_GetStdout", (PyCFunction)JupyROOTExecutorHandler_GetStdout, METH_NOARGS,
"Get stdout JupyROOTExecutorHandler"},
{"JupyROOTExecutorHandler_GetStderr", (PyCFunction)JupyROOTExecutorHandler_GetStderr, METH_NOARGS,
"Get stderr JupyROOTExecutorHandler"},
{"JupyROOTExecutorHandler_Dtor", (PyCFunction)JupyROOTExecutorHandler_Dtor, METH_NOARGS,
"Destruct JupyROOTExecutorHandler"},
{"CPyCppyyRegisterConverterAlias", (PyCFunction)PyROOT::RegisterConverterAlias, METH_VARARGS,
"Register a custom converter that is a reference to an existing converter"},
{"CPyCppyyRegisterExecutorAlias", (PyCFunction)PyROOT::RegisterExecutorAlias, METH_VARARGS,
"Register a custom executor that is a reference to an existing executor"},
{"PyObjRefCounterAsStdAny", (PyCFunction)PyROOT::PyObjRefCounterAsStdAny, METH_VARARGS,
"Wrap a reference count to any Python object in a std::any for resource management in C++"},
{NULL, NULL, 0, NULL}};
struct module_state {
PyObject *error;
};
#define GETSTATE(m) ((struct module_state *)PyModule_GetState(m))
static int rootmodule_traverse(PyObject *m, visitproc visit, void *arg)
{
Py_VISIT(GETSTATE(m)->error);
return 0;
}
static int rootmodule_clear(PyObject *m)
{
Py_CLEAR(GETSTATE(m)->error);
return 0;
}
static struct PyModuleDef moduledef = {PyModuleDef_HEAD_INIT, "libROOTPythonizations", NULL,
sizeof(struct module_state), gPyROOTMethods, NULL,
rootmodule_traverse, rootmodule_clear, NULL};
/// Initialization of extension module libROOTPythonizations
extern "C" PyObject *PyInit_libROOTPythonizations()
{
using namespace PyROOT;
// setup PyROOT
gRootModule = PyModule_Create(&moduledef);
if (!gRootModule)
return nullptr;
// keep gRootModule, but do not increase its reference count even as it is borrowed,
// or a self-referencing cycle would be created
// Avoid fallback to the GIL when importing this module in a free-threaded
// Python build.
#ifdef Py_GIL_DISABLED
PyUnstable_Module_SetGIL(gRootModule, Py_MOD_GIL_NOT_USED);
#endif
return gRootModule;
}