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
27namespace PyROOT {
29}
30
31using namespace PyROOT;
32
33namespace {
34
35static 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}
#define Py_RETURN_NONE
Definition: CPyCppyy.h:281
#define R__EXTERN
Definition: DllImport.h:27
_object PyObject
Definition: PyMethodBase.h:41
static TMemoryRegulator & GetMemoryRegulator()
#define gInterpreter
Definition: TInterpreter.h:556
#define gROOT
Definition: TROOT.h:406
R__EXTERN TSystem * gSystem
Definition: TSystem.h:556
void ClearProxiedObjects()
Clean up all tracked objects.
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
Mother of all ROOT objects.
Definition: TObject.h:37
PyObject * BindCppObjectNoCast(Cppyy::TCppObject_t object, Cppyy::TCppType_t klass, const unsigned flags=0)
TCppScope_t TCppType_t
Definition: cpp_cppyy.h:19
RPY_EXPORTED TCppScope_t GetScope(const std::string &scope_name)
PyObject * ClearProxiedObjects(PyObject *self, PyObject *args)
void Init()
PyObject * gRootModule
auto * m
Definition: textangle.C:8