Logo ROOT  
Reference Guide
PyROOTModule.cxx
Go to the documentation of this file.
1 // Author: Enric Tejedor CERN 06/2018
2 // Original PyROOT code by Wim Lavrijsen, LBL
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2018, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 // Bindings
13 #include "PyROOTPythonize.h"
14 #include "PyROOTStrings.h"
15 #include "PyROOTWrapper.h"
16 #include "RPyROOTApplication.h"
17 #include "FacadeHelpers.hxx"
18 
19 // Cppyy
20 #include "CPyCppyy.h"
21 #include "CallContext.h"
22 #include "ProxyWrappers.h"
23 #include "Utility.h"
24 
25 // ROOT
26 #include "TROOT.h"
27 #include "TSystem.h"
28 #include "RConfigure.h"
29 
30 // Standard
31 #include <string>
32 #include <sstream>
33 #include <utility>
34 #include <vector>
35 
36 using namespace CPyCppyy;
37 
38 namespace PyROOT {
40 }
41 
42 // Methods offered by the interface
43 static PyMethodDef gPyROOTMethods[] = {
44  {(char *)"AddDirectoryWritePyz", (PyCFunction)PyROOT::AddDirectoryWritePyz, METH_VARARGS,
45  (char *)"Allow to use seamlessly from Python the templated TDirectory::WriteObject method"},
46  {(char *)"AddCPPInstancePickling", (PyCFunction)PyROOT::AddCPPInstancePickling, METH_VARARGS,
47  (char *)"Add a custom pickling mechanism for Cppyy Python proxy objects"},
48  {(char *)"AddDirectoryGetAttrPyz", (PyCFunction)PyROOT::AddDirectoryGetAttrPyz, METH_VARARGS,
49  (char *)"Attr syntax for TDirectory, TDirectoryFile and TFile"},
50  {(char *)"AddBranchAttrSyntax", (PyCFunction)PyROOT::AddBranchAttrSyntax, METH_VARARGS,
51  (char *)"Allow to access branches as tree attributes"},
52  {(char *)"AddFileOpenPyz", (PyCFunction)PyROOT::AddFileOpenPyz, METH_VARARGS,
53  (char *)"Make TFile::Open a constructor, adjusting for example the reference count"},
54  {(char *)"AddTDirectoryFileGetPyz", (PyCFunction)PyROOT::AddTDirectoryFileGetPyz, METH_VARARGS,
55  (char *)"Get objects inside TDirectoryFile and TFile instantiations"},
56  {(char *)"AddTClassDynamicCastPyz", (PyCFunction)PyROOT::AddTClassDynamicCastPyz, METH_VARARGS,
57  (char *)"Cast the void* returned by TClass::DynamicCast to the right type"},
58  {(char *)"AddTObjectEqNePyz", (PyCFunction)PyROOT::AddTObjectEqNePyz, METH_VARARGS,
59  (char *)"Add equality and inequality comparison operators to TObject"},
60  {(char *)"AddUsingToClass", (PyCFunction)PyROOT::AddUsingToClass, METH_VARARGS,
61  (char *)"Add 'using' overloads for a given method to a class"},
62  {(char *)"SetBranchAddressPyz", (PyCFunction)PyROOT::SetBranchAddressPyz, METH_VARARGS,
63  (char *)"Fully enable the use of TTree::SetBranchAddress from Python"},
64  {(char *)"BranchPyz", (PyCFunction)PyROOT::BranchPyz, METH_VARARGS,
65  (char *)"Fully enable the use of TTree::Branch from Python"},
66  {(char *)"AddSetItemTCAPyz", (PyCFunction)PyROOT::AddSetItemTCAPyz, METH_VARARGS,
67  (char *)"Customize the setting of an item of a TClonesArray"},
68  {(char *)"AddPrettyPrintingPyz", (PyCFunction)PyROOT::AddPrettyPrintingPyz, METH_VARARGS,
69  (char *)"Add pretty printing pythonization"},
70  {(char *)"GetEndianess", (PyCFunction)PyROOT::GetEndianess, METH_NOARGS, (char *)"Get endianess of the system"},
71  {(char *)"GetDataPointer", (PyCFunction)PyROOT::GetDataPointer, METH_VARARGS,
72  (char *)"Get pointer to data of a C++ object"},
73  {(char *)"GetSizeOfType", (PyCFunction)PyROOT::GetSizeOfType, METH_VARARGS, (char *)"Get size of data-type"},
74  {(char *)"AsRVec", (PyCFunction)PyROOT::AsRVec, METH_O, (char *)"Get object with array interface as RVec"},
75 #ifdef R__HAS_DATAFRAME
76  {(char *)"AsRTensor", (PyCFunction)PyROOT::AsRTensor, METH_O, (char *)"Get object with array interface as RTensor"},
77  {(char *)"MakeNumpyDataFrame", (PyCFunction)PyROOT::MakeNumpyDataFrame, METH_O,
78  (char *)"Make RDataFrame from dictionary of numpy arrays"},
79 #endif
80  {(char *)"InitApplication", (PyCFunction)PyROOT::RPyROOTApplication::InitApplication, METH_VARARGS,
81  (char *)"Initialize interactive ROOT use from Python"},
82  {(char *)"InstallGUIEventInputHook", (PyCFunction)PyROOT::RPyROOTApplication::InstallGUIEventInputHook, METH_NOARGS,
83  (char *)"Install an input hook to process GUI events"},
84  {(char *)"_CPPInstance__expand__", (PyCFunction)PyROOT::CPPInstanceExpand, METH_VARARGS,
85  (char *)"Deserialize a pickled object"},
86  {(char *)"ClearProxiedObjects", (PyCFunction)PyROOT::ClearProxiedObjects, METH_NOARGS,
87  (char *)"Clear proxied objects regulated by PyROOT"},
88  {(char *)"CreateBufferFromAddress", (PyCFunction)PyROOT::CreateBufferFromAddress, METH_O,
89  (char *)"Create a LowLevelView object on the received address"},
90  {NULL, NULL, 0, NULL}};
91 
92 #define QuoteIdent(ident) #ident
93 #define QuoteMacro(macro) QuoteIdent(macro)
94 #define LIBROOTPYZ_NAME "libROOTPythonizations" QuoteMacro(PY_MAJOR_VERSION) "_" QuoteMacro(PY_MINOR_VERSION)
95 #define LIBCPPYY_NAME "libcppyy" QuoteMacro(PY_MAJOR_VERSION) "_" QuoteMacro(PY_MINOR_VERSION)
96 
97 #define CONCAT(a, b, c, d) a##b##c##d
98 #define LIBROOTPYZ_INIT_FUNCTION(a, b, c, d) CONCAT(a, b, c, d)
99 
100 #if PY_VERSION_HEX >= 0x03000000
101 struct module_state {
102  PyObject *error;
103 };
104 
105 #define GETSTATE(m) ((struct module_state *)PyModule_GetState(m))
106 
107 static int rootmodule_traverse(PyObject *m, visitproc visit, void *arg)
108 {
109  Py_VISIT(GETSTATE(m)->error);
110  return 0;
111 }
112 
113 static int rootmodule_clear(PyObject *m)
114 {
115  Py_CLEAR(GETSTATE(m)->error);
116  return 0;
117 }
118 
119 static struct PyModuleDef moduledef = {PyModuleDef_HEAD_INIT, LIBROOTPYZ_NAME, NULL,
120  sizeof(struct module_state), gPyROOTMethods, NULL,
121  rootmodule_traverse, rootmodule_clear, NULL};
122 
123 /// Initialization of extension module libROOTPythonizations
124 
125 #define PYROOT_INIT_ERROR return NULL
126 LIBROOTPYZ_INIT_FUNCTION(extern "C" PyObject* PyInit_libROOTPythonizations, PY_MAJOR_VERSION, _, PY_MINOR_VERSION) ()
127 #else // PY_VERSION_HEX >= 0x03000000
128 #define PYROOT_INIT_ERROR return
129 LIBROOTPYZ_INIT_FUNCTION(extern "C" void initlibROOTPythonizations, PY_MAJOR_VERSION, _, PY_MINOR_VERSION) ()
130 #endif
131 {
132  using namespace PyROOT;
133 
134  // load commonly used python strings
137 
138 // setup PyROOT
139 #if PY_VERSION_HEX >= 0x03000000
140  gRootModule = PyModule_Create(&moduledef);
141 #else
142  gRootModule = Py_InitModule(const_cast<char *>(LIBROOTPYZ_NAME), gPyROOTMethods);
143 #endif
144  if (!gRootModule)
146 
147  // keep gRootModule, but do not increase its reference count even as it is borrowed,
148  // or a self-referencing cycle would be created
149 
150  // Make sure libcppyy has been imported
151  PyImport_ImportModule(LIBCPPYY_NAME);
152 
153  // setup PyROOT
154  PyROOT::Init();
155 
156  // signal policy: don't abort interpreter in interactive mode
158 
159  // inject ROOT namespace for convenience
160  PyModule_AddObject(gRootModule, (char *)"ROOT", CreateScopeProxy("ROOT"));
161 
162 #if PY_VERSION_HEX >= 0x03000000
163  Py_INCREF(gRootModule);
164  return gRootModule;
165 #endif
166 }
CPyCppyy
Set of helper functions that are invoked from the pythonizors, on the Python side.
Definition: TPyClassGenerator.cxx:31
m
auto * m
Definition: textangle.C:8
PyROOT::AddTObjectEqNePyz
PyObject * AddTObjectEqNePyz(PyObject *self, PyObject *args)
Add pythonization for equality and inequality operators in TObject.
Definition: TObjectPyz.cxx:57
LIBROOTPYZ_NAME
#define LIBROOTPYZ_NAME
Definition: PyROOTModule.cxx:94
PyROOT::AddSetItemTCAPyz
PyObject * AddSetItemTCAPyz(PyObject *self, PyObject *args)
Customize the setting of an item of a TClonesArray.
Definition: TClonesArrayPyz.cxx:167
PyROOT::RPyROOTApplication::InstallGUIEventInputHook
static PyObject * InstallGUIEventInputHook(PyObject *self, PyObject *args)
Install a method hook for sending events to the GUI.
Definition: RPyROOTApplication.cxx:216
PYROOT_INIT_ERROR
#define PYROOT_INIT_ERROR
Definition: PyROOTModule.cxx:128
PyROOT::AddDirectoryGetAttrPyz
PyObject * AddDirectoryGetAttrPyz(PyObject *self, PyObject *args)
Add attr syntax to TDirectory.
Definition: TDirectoryPyz.cxx:100
PyROOT::GetDataPointer
PyObject * GetDataPointer(PyObject *self, PyObject *args)
Get pointer to the data of an object.
Definition: PyzPythonHelpers.cxx:71
_
#define _(A, B)
Definition: cfortran.h:108
PyObject
_object PyObject
Definition: PyMethodBase.h:42
PyROOT::AddPrettyPrintingPyz
PyObject * AddPrettyPrintingPyz(PyObject *self, PyObject *args)
Add pretty printing pythonization.
Definition: GenericPyz.cxx:54
PyROOT::AddCPPInstancePickling
PyObject * AddCPPInstancePickling(PyObject *self, PyObject *args)
Set reduce attribute for CPPInstance objects.
Definition: CPPInstancePyz.cxx:122
LIBCPPYY_NAME
#define LIBCPPYY_NAME
Definition: PyROOTModule.cxx:95
PyROOT::RPyROOTApplication::InitApplication
static PyObject * InitApplication(PyObject *self, PyObject *args)
Initialize an RPyROOTApplication.
Definition: RPyROOTApplication.cxx:151
Utility.h
PyROOT::SetBranchAddressPyz
PyObject * SetBranchAddressPyz(PyObject *self, PyObject *args)
Add pythonization for TTree::SetBranchAddress.
Definition: TTreePyz.cxx:196
PyROOTPythonize.h
RPyROOTApplication.h
PyROOT::GetSizeOfType
PyObject * GetSizeOfType(PyObject *self, PyObject *args)
Get size of C++ data-type.
Definition: PyzPythonHelpers.cxx:45
PyROOT::CreatePyStrings
bool CreatePyStrings()
Definition: PyROOTStrings.cxx:29
PyROOT::AddDirectoryWritePyz
PyObject * AddDirectoryWritePyz(PyObject *self, PyObject *args)
Add pythonisation of TDirectory::WriteObject.
Definition: TDirectoryPyz.cxx:111
PyROOT::MakeNumpyDataFrame
PyObject * MakeNumpyDataFrame(PyObject *self, PyObject *obj)
Make an RDataFrame from a dictionary of numpy arrays.
Definition: RDataFramePyz.cxx:29
PyROOT::CPPInstanceExpand
PyObject * CPPInstanceExpand(PyObject *self, PyObject *args)
Deserialize pickled objects.
Definition: CPPInstancePyz.cxx:39
FacadeHelpers.hxx
PyROOT::AsRTensor
PyObject * AsRTensor(PyObject *self, PyObject *obj)
Adopt memory of a Python object with array interface using an RTensor.
Definition: RTensorPyz.cxx:28
TROOT.h
PyROOT::AddTDirectoryFileGetPyz
PyObject * AddTDirectoryFileGetPyz(PyObject *self, PyObject *args)
Add pythonisation of TDirectoryFile::Get()
Definition: TDirectoryFilePyz.cxx:69
PyROOT::Init
void Init()
Definition: PyROOTWrapper.cxx:50
PyROOT::CreateBufferFromAddress
PyObject * CreateBufferFromAddress(PyObject *self, PyObject *addr)
Get a buffer starting at a given address.
Definition: FacadeHelpers.cxx:28
TSystem.h
ProxyWrappers.h
PyROOT::ClearProxiedObjects
PyObject * ClearProxiedObjects(PyObject *self, PyObject *args)
Definition: PyROOTWrapper.cxx:64
PyROOT::BranchPyz
PyObject * BranchPyz(PyObject *self, PyObject *args)
Add pythonization for TTree::Branch.
Definition: TTreePyz.cxx:394
CPyCppyy::CreateScopeProxy
PyObject * CreateScopeProxy(Cppyy::TCppScope_t)
Definition: ProxyWrappers.cxx:487
PyROOT::AsRVec
PyObject * AsRVec(PyObject *self, PyObject *obj)
Adopt memory of a Python object with array interface using an RVec.
Definition: RVecPyz.cxx:28
PyROOT::AddBranchAttrSyntax
PyObject * AddBranchAttrSyntax(PyObject *self, PyObject *args)
Allow branches to be accessed as attributes of a tree.
Definition: TTreePyz.cxx:174
CallContext.h
PyROOT::AddFileOpenPyz
PyObject * AddFileOpenPyz(PyObject *self, PyObject *args)
Make TFile::Open equivalent to a constructor.
Definition: TFilePyz.cxx:23
CPyCppyy.h
PyROOT
Definition: CPPInstancePyz.cxx:25
LIBROOTPYZ_INIT_FUNCTION
#define LIBROOTPYZ_INIT_FUNCTION(a, b, c, d)
Definition: PyROOTModule.cxx:98
PyROOT::AddTClassDynamicCastPyz
PyObject * AddTClassDynamicCastPyz(PyObject *self, PyObject *args)
Add pythonization for TClass::DynamicCast.
Definition: TClassPyz.cxx:89
gPyROOTMethods
static PyMethodDef gPyROOTMethods[]
Definition: PyROOTModule.cxx:43
PyROOT::GetEndianess
PyObject * GetEndianess(PyObject *self, PyObject *args)
Get endianess of the system.
Definition: PyzPythonHelpers.cxx:106
PyROOT::gRootModule
PyObject * gRootModule
Definition: PyROOTModule.cxx:39
PyROOT::AddUsingToClass
PyObject * AddUsingToClass(PyObject *self, PyObject *args)
Add base class overloads of a given method to a derived class.
Definition: PyzPythonHelpers.cxx:177
PyROOTStrings.h
PyROOTWrapper.h
CPyCppyy::CallContext::SetGlobalSignalPolicy
static bool SetGlobalSignalPolicy(bool setProtected)
Definition: CallContext.cxx:53
gROOT
#define gROOT
Definition: TROOT.h:406