Skip to content

Commit

Permalink
[PyROOT] Don't add gROOT and other globals manually to PyROOT via C++
Browse files Browse the repository at this point in the history
This is not necessary if we consider that `gROOT` is actually a
preprocessor macro that aliases to `ROOT::GetROOT()`, which we can use
directly in Python via cppyy. Same with the `gInterpreter`.
  • Loading branch information
guitargeek committed Nov 8, 2024
1 parent 7977474 commit 2cb627c
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 37 deletions.
4 changes: 1 addition & 3 deletions bindings/pyroot/pythonizations/python/ROOT/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ def find_spec(self, fullname: str, path, target=None) -> ModuleSpec:
# Register cleanup
import atexit


def cleanup():
# If spawned, stop thread which processes ROOT events
facade = sys.modules[__name__]
Expand All @@ -185,15 +184,14 @@ def cleanup():
facade.__dict__["app"].process_root_events.join()

if "libROOTPythonizations" in sys.modules:
backend = sys.modules["libROOTPythonizations"]

from ROOT import PyConfig

if PyConfig.ShutDown:
# Hard teardown: run part of the gROOT shutdown sequence.
# Running it here ensures that it is done before any ROOT libraries
# are off-loaded, with unspecified order of static object destruction.
backend.gROOT.EndOfProcessCleanups()
facade.gROOT.EndOfProcessCleanups()


atexit.register(cleanup)
10 changes: 4 additions & 6 deletions bindings/pyroot/pythonizations/python/ROOT/_facade.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

import cppyy.ll

from libROOTPythonizations import gROOT

from ._application import PyROOTApplication
from ._numbadeclare import _NumbaDeclareDecorator

Expand All @@ -36,7 +34,7 @@ class _gROOTWrapper(object):

def __init__(self, facade):
self.__dict__["_facade"] = facade
self.__dict__["_gROOT"] = gROOT
self.__dict__["_gROOT"] = cppyy.gbl.ROOT.GetROOT()

def __getattr__(self, name):
if name != "SetBatch" and self._facade.__dict__["gROOT"] != self._gROOT:
Expand Down Expand Up @@ -158,7 +156,7 @@ def _fallback_getattr(self, name):
elif hasattr(cppyy.gbl.ROOT, name):
return getattr(cppyy.gbl.ROOT, name)
else:
res = gROOT.FindObject(name)
res = self.gROOT.FindObject(name)
if res:
return res
raise AttributeError("Failed to get attribute {} from ROOT".format(name))
Expand Down Expand Up @@ -204,7 +202,7 @@ def _register_converters_and_executors(self):

def _finalSetup(self):
# Prevent this method from being re-entered through the gROOT wrapper
self.__dict__["gROOT"] = gROOT
self.__dict__["gROOT"] = cppyy.gbl.ROOT.GetROOT()

# Setup interactive usage from Python
self.__dict__["app"] = PyROOTApplication(self.PyConfig, self._is_ipython)
Expand Down Expand Up @@ -387,7 +385,7 @@ def TMVA(self):
from ._pythonization import _tmva

ns = self._fallback_getattr("TMVA")
hasRDF = "dataframe" in gROOT.GetConfigFeatures()
hasRDF = "dataframe" in self.gROOT.GetConfigFeatures()
if hasRDF:
try:
from ._pythonization._tmva import inject_rbatchgenerator, _AsRTensor, SaveXGBoost
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

from .. import pythonization

from libROOTPythonizations import gROOT

from ._factory import Factory
from ._dataloader import DataLoader
from ._crossvalidation import CrossValidation
Expand Down Expand Up @@ -45,7 +43,7 @@ def inject_rbatchgenerator(ns):

from ._gnn import RModel_GNN, RModel_GraphIndependent

hasRDF = "dataframe" in gROOT.GetConfigFeatures()
hasRDF = "dataframe" in cppyy.gbl.ROOT.GetROOT().GetConfigFeatures()
if hasRDF:
from ._rtensor import get_array_interface, add_array_interface_property, RTensorGetitem, pythonize_rtensor, _AsRTensor

Expand Down
25 changes: 0 additions & 25 deletions bindings/pyroot/pythonizations/src/PyROOTWrapper.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,13 @@
#include "PyROOTWrapper.h"
#include "TMemoryRegulator.h"

// Cppyy
#include "CPyCppyy/API.h"

// ROOT
#include "TROOT.h"
#include "TSystem.h"
#include "TClass.h"
#include "TInterpreter.h"
#include "DllImport.h"

namespace PyROOT {
R__EXTERN PyObject *gRootModule;
}

using namespace PyROOT;

namespace {

static void AddToGlobalScope(const char *label, TObject *obj, const char *classname)
{
// Bind the given object with the given class in the global scope with the
// given label for its reference.
PyModule_AddObject(gRootModule, label, CPyCppyy::Instance_FromVoidPtr(obj, classname));
}

} // unnamed namespace

PyROOT::RegulatorCleanup &GetRegulatorCleanup()
{
// The object is thread-local because it can happen that we call into
Expand All @@ -59,9 +39,4 @@ void PyROOT::Init()

// Memory management
gROOT->GetListOfCleanups()->Add(&GetRegulatorCleanup());

// Bind ROOT globals that will be needed in ROOT.py
AddToGlobalScope("gROOT", gROOT, gROOT->IsA()->GetName());
AddToGlobalScope("gSystem", gSystem, gSystem->IsA()->GetName());
AddToGlobalScope("gInterpreter", gInterpreter, gInterpreter->IsA()->GetName());
}

0 comments on commit 2cb627c

Please sign in to comment.