Logo ROOT  
Reference Guide
PyROOTWrapper.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 "PyROOTWrapper.h"
14 #include "TMemoryRegulator.h"
15 
16 // Cppyy
17 #include "CPyCppyy.h"
18 #include "ProxyWrappers.h"
19 
20 // ROOT
21 #include "TROOT.h"
22 #include "TSystem.h"
23 #include "TClass.h"
24 #include "TInterpreter.h"
25 #include "DllImport.h"
26 
27 namespace PyROOT {
29 }
30 
31 using namespace PyROOT;
32 
33 namespace {
34 
35 static void AddToGlobalScope(const char *label, const char * /* hdr */, TObject *obj, Cppyy::TCppType_t klass)
36 {
37  // Bind the given object with the given class in the global scope with the
38  // given label for its reference.
39  PyModule_AddObject(gRootModule, const_cast<char *>(label), CPyCppyy::BindCppObjectNoCast(obj, klass));
40 }
41 
42 } // unnamed namespace
43 
45 {
46  static TMemoryRegulator m;
47  return m;
48 }
49 
51 {
52  // Initialize and acquire the GIL to allow for threading in ROOT
53  PyEval_InitThreads();
54 
55  // Memory management
56  gROOT->GetListOfCleanups()->Add(&GetMemoryRegulator());
57 
58  // Bind ROOT globals that will be needed in ROOT.py
59  AddToGlobalScope("gROOT", "TROOT.h", gROOT, Cppyy::GetScope(gROOT->IsA()->GetName()));
60  AddToGlobalScope("gSystem", "TSystem.h", gSystem, Cppyy::GetScope(gSystem->IsA()->GetName()));
61  AddToGlobalScope("gInterpreter", "TInterpreter.h", gInterpreter, Cppyy::GetScope(gInterpreter->IsA()->GetName()));
62 }
63 
65 {
66  // Delete all memory-regulated objects
69 }
m
auto * m
Definition: textangle.C:8
PyObject
_object PyObject
Definition: PyMethodBase.h:42
GetMemoryRegulator
static TMemoryRegulator & GetMemoryRegulator()
Definition: PyROOTWrapper.cxx:44
gInterpreter
#define gInterpreter
Definition: TInterpreter.h:560
PyROOT::TMemoryRegulator::ClearProxiedObjects
void ClearProxiedObjects()
Clean up all tracked objects.
Definition: TMemoryRegulator.cxx:90
TClass.h
TROOT.h
PyROOT::Init
void Init()
Definition: PyROOTWrapper.cxx:50
Cppyy::GetScope
RPY_EXPORTED TCppScope_t GetScope(const std::string &scope_name)
Definition: clingwrapper.cxx:497
TSystem.h
ProxyWrappers.h
PyROOT::ClearProxiedObjects
PyObject * ClearProxiedObjects(PyObject *self, PyObject *args)
Definition: PyROOTWrapper.cxx:64
CPyCppyy.h
PyROOT::TMemoryRegulator
Definition: TMemoryRegulator.h:51
PyROOT
Definition: CPPInstancePyz.cxx:25
gSystem
R__EXTERN TSystem * gSystem
Definition: TSystem.h:559
Cppyy::TCppType_t
TCppScope_t TCppType_t
Definition: cpp_cppyy.h:19
TMemoryRegulator.h
TInterpreter.h
TObject
Mother of all ROOT objects.
Definition: TObject.h:37
PyROOT::gRootModule
PyObject * gRootModule
Definition: PyROOTModule.cxx:39
R__EXTERN
#define R__EXTERN
Definition: DllImport.h:27
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
PyROOTWrapper.h
CPyCppyy::BindCppObjectNoCast
PyObject * BindCppObjectNoCast(Cppyy::TCppObject_t object, Cppyy::TCppType_t klass, const unsigned flags=0)
Definition: ProxyWrappers.cxx:799
DllImport.h
Py_RETURN_NONE
#define Py_RETURN_NONE
Definition: CPyCppyy.h:281
gROOT
#define gROOT
Definition: TROOT.h:406