Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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/API.h"
18
19// ROOT
20#include "TROOT.h"
21#include "TSystem.h"
22#include "TClass.h"
23#include "TInterpreter.h"
24#include "DllImport.h"
25
26namespace PyROOT {
28}
29
30using namespace PyROOT;
31
32namespace {
33
34static void AddToGlobalScope(const char *label, TObject *obj, const char *classname)
35{
36 // Bind the given object with the given class in the global scope with the
37 // given label for its reference.
38 PyModule_AddObject(gRootModule, label, CPyCppyy::Instance_FromVoidPtr(obj, classname));
39}
40
41} // unnamed namespace
42
44{
45 // The object is thread-local because it can happen that we call into
46 // C++ code (from the PyROOT CPython extension, from CPyCppyy or from cling)
47 // from different Python threads. A notable example is within a distributed
48 // RDataFrame application running on Dask.
49 thread_local PyROOT::RegulatorCleanup m;
50 return m;
51}
52
54{
55 // Initialize and acquire the GIL to allow for threading in ROOT
56#if PY_VERSION_HEX < 0x03090000
57 PyEval_InitThreads();
58#endif
59
60 // Memory management
61 gROOT->GetListOfCleanups()->Add(&GetRegulatorCleanup());
62
63 // Bind ROOT globals that will be needed in ROOT.py
64 AddToGlobalScope("gROOT", gROOT, gROOT->IsA()->GetName());
65 AddToGlobalScope("gSystem", gSystem, gSystem->IsA()->GetName());
66 AddToGlobalScope("gInterpreter", gInterpreter, gInterpreter->IsA()->GetName());
67}
68
70{
71 // Delete all memory-regulated objects
74}
#define Py_RETURN_NONE
Definition CPyCppyy.h:268
#define R__EXTERN
Definition DllImport.h:26
_object PyObject
PyROOT::RegulatorCleanup & GetRegulatorCleanup()
#define gInterpreter
#define gROOT
Definition TROOT.h:406
R__EXTERN TSystem * gSystem
Definition TSystem.h:555
A TObject-derived class to inject the memory regulation logic in the ROOT list of cleanups.
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
Mother of all ROOT objects.
Definition TObject.h:41
TClass * IsA() const override
Definition TSystem.h:552
CPYCPPYY_EXTERN PyObject * Instance_FromVoidPtr(void *addr, const std::string &classname, bool python_owns=false)
Definition API.cxx:121
PyObject * ClearProxiedObjects(PyObject *self, PyObject *args)
void Init()
PyObject * gRootModule
TMarker m
Definition textangle.C:8