Skip to content

Commit 7d42f84

Browse files
committed
[PyROOT] Don't import libcppyy directly in PyROOT
Just in case the `libcppyy` gets moved out of the base `site-packages` directory to meet the requirements of Linux distributions like Gentoo. See also #14917.
1 parent 926addd commit 7d42f84

22 files changed

+70
-75
lines changed

bindings/pyroot/pythonizations/python/ROOT/_facade.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,16 @@
44
import os
55
from functools import partial
66

7-
import libcppyy as cppyy_backend
8-
from cppyy import gbl as gbl_namespace
9-
from cppyy import cppdef, include
10-
from ROOT.libROOTPythonizations import gROOT
11-
from cppyy.gbl import gSystem
7+
import cppyy
128
import cppyy.ll
139

10+
from ROOT.libROOTPythonizations import gROOT
11+
1412
from ._application import PyROOTApplication
1513
from ._numbadeclare import _NumbaDeclareDecorator
1614

1715
from ._pythonization import pythonization
1816

19-
2017
class PyROOTConfiguration(object):
2118
"""Class for configuring PyROOT"""
2219

@@ -107,7 +104,7 @@ def __init__(self, module, is_ipython):
107104
"SetOwnership",
108105
]
109106
for name in self._cppyy_exports:
110-
setattr(self, name, getattr(cppyy_backend, name))
107+
setattr(self, name, getattr(cppyy._backend, name))
111108
# For backwards compatibility
112109
self.MakeNullPointer = partial(self.bind_object, 0)
113110
self.BindObject = self.bind_object
@@ -154,10 +151,10 @@ def _fallback_getattr(self, name):
154151
# e.g. ROOT.ROOT.Math as ROOT.Math
155152

156153
# Note that hasattr caches the lookup for getattr
157-
if hasattr(gbl_namespace, name):
158-
return getattr(gbl_namespace, name)
159-
elif hasattr(gbl_namespace.ROOT, name):
160-
return getattr(gbl_namespace.ROOT, name)
154+
if hasattr(cppyy.gbl, name):
155+
return getattr(cppyy.gbl, name)
156+
elif hasattr(cppyy.gbl.ROOT, name):
157+
return getattr(cppyy.gbl.ROOT, name)
161158
else:
162159
res = gROOT.FindObject(name)
163160
if res:
@@ -180,7 +177,7 @@ def _finalSetup(self):
180177

181178
# Redirect lookups to cppyy's global namespace
182179
self.__class__.__getattr__ = self._fallback_getattr
183-
self.__class__.__setattr__ = lambda self, name, val: setattr(gbl_namespace, name, val)
180+
self.__class__.__setattr__ = lambda self, name, val: setattr(cppyy.gbl, name, val)
184181

185182
# Run rootlogon if exists
186183
self._run_rootlogon()
@@ -359,7 +356,7 @@ def TMVA(self):
359356
# Create and overload Numba namespace
360357
@property
361358
def Numba(self):
362-
cppdef("namespace Numba {}")
359+
cppyy.cppdef("namespace Numba {}")
363360
ns = self._fallback_getattr("Numba")
364361
ns.Declare = staticmethod(_NumbaDeclareDecorator)
365362
del type(self).Numba
@@ -380,7 +377,7 @@ def NumbaExt(self):
380377
# Get TPyDispatcher for programming GUI callbacks
381378
@property
382379
def TPyDispatcher(self):
383-
include("ROOT/TPyDispatcher.h")
384-
tpd = gbl_namespace.TPyDispatcher
380+
cppyy.include("ROOT/TPyDispatcher.h")
381+
tpd = cppyy.gbl.TPyDispatcher
385382
type(self).TPyDispatcher = tpd
386383
return tpd

bindings/pyroot/pythonizations/python/ROOT/_pythonization/_cppinstance.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
# For the list of contributors see $ROOTSYS/README/CREDITS. #
99
################################################################################
1010

11-
import libcppyy
12-
from ROOT.libROOTPythonizations import AddCPPInstancePickling
13-
1411
def pythonize_cppinstance():
15-
klass = libcppyy.CPPInstance
12+
import cppyy
13+
from ROOT.libROOTPythonizations import AddCPPInstancePickling
14+
15+
klass = cppyy._backend.CPPInstance
1616

1717
AddCPPInstancePickling(klass)
1818

bindings/pyroot/pythonizations/python/ROOT/_pythonization/_drawables.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010

1111
from . import pythonization
1212
from cppyy.gbl import kCanDelete
13-
from libcppyy import SetOwnership
1413

1514

1615
def _Draw(self, *args):
16+
17+
import ROOT
18+
1719
# Parameters:
1820
# self: Object being drawn
1921
# args: arguments for Draw
@@ -31,11 +33,14 @@ def _Draw(self, *args):
3133
# their constructors. Later, when being drawn, they are appended to
3234
# the list of primitives of gPad.
3335
if self.TestBit(kCanDelete):
34-
SetOwnership(self, False)
36+
ROOT.SetOwnership(self, False)
3537

3638
self.Draw = self._OriginalDraw
3739

3840
def _init(self, *args):
41+
42+
import ROOT
43+
3944
# Parameters:
4045
# self: Object being initialized
4146
# args: arguments for __init__
@@ -47,7 +52,7 @@ def _init(self, *args):
4752
# Therefore, we need to set the ownership here and not in Draw
4853
# (TSlider does not need to be drawn). This is ROOT-10095.
4954
if self.TestBit(kCanDelete):
50-
SetOwnership(self, False)
55+
ROOT.SetOwnership(self, False)
5156
# We have already set the ownership while initializing,
5257
# so we do not need the custom Draw inherited from TPad to
5358
# do it again in case it is executed.

bindings/pyroot/pythonizations/python/ROOT/_pythonization/_rdf_pyz.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
import typing
1212
from .._numbadeclare import _NumbaDeclareDecorator
1313

14-
import libcppyy
15-
1614
class FunctionJitter:
1715
"""
1816
This class allows to jit a python callable with Numba, being able to infer the signature of the function from the types of the RDF columns.
@@ -275,8 +273,10 @@ def _handle_cpp_callables(func, original_template, *args):
275273
three cases above, None otherwise
276274
"""
277275

278-
is_cpp_functor = lambda : isinstance(getattr(func, '__call__', None), libcppyy.CPPOverload)
279-
is_std_function = lambda : isinstance(getattr(func, 'target_type', None), libcppyy.CPPOverload)
276+
import cppyy
277+
278+
is_cpp_functor = lambda : isinstance(getattr(func, '__call__', None), cppyy._backend.CPPOverload)
279+
is_std_function = lambda : isinstance(getattr(func, 'target_type', None), cppyy._backend.CPPOverload)
280280

281281
if is_cpp_functor() or is_std_function():
282282
return original_template[type(func)](*args)

bindings/pyroot/pythonizations/python/ROOT/_pythonization/_roofit/_rooabscollection.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313

1414
from ._utils import _kwargs_to_roocmdargs, cpp_signature
15-
from libcppyy import SetOwnership
1615

1716

1817
class RooAbsCollection(object):
@@ -28,6 +27,9 @@ class RooAbsCollection(object):
2827
"""
2928

3029
def addClone(self, arg, silent=False):
30+
31+
import ROOT
32+
3133
clonedArg = self._addClone(arg, silent)
3234
# There are two overloads of RooAbsCollection::addClone():
3335
#
@@ -42,11 +44,14 @@ def addClone(self, arg, silent=False):
4244
# need to change any ownership flags (in fact, calling
4345
# SetOwnership(None, False) would cause a crash).
4446
if clonedArg is not None:
45-
SetOwnership(clonedArg, False)
47+
ROOT.SetOwnership(clonedArg, False)
4648

4749
def addOwned(self, arg, silent=False):
50+
51+
import ROOT
52+
4853
self._addOwned(arg, silent)
49-
SetOwnership(arg, False)
54+
ROOT.SetOwnership(arg, False)
5055

5156
@cpp_signature(
5257
"RooAbsCollection::printLatex(const RooCmdArg& arg1={}, const RooCmdArg& arg2={},"

bindings/pyroot/pythonizations/python/ROOT/_pythonization/_roofit/_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ def getter(k, v):
2323

2424
# We have to use ROOT here and not cppy.gbl, because the RooFit namespace is pythonized itself.
2525
import ROOT
26-
import libcppyy
26+
import cppyy
2727

2828
func = getattr(ROOT.RooFit, k)
2929

30-
if isinstance(func, libcppyy.CPPOverload):
30+
if isinstance(func, cppyy._backend.CPPOverload):
3131
# Pythonization for functions that don't pass any RooCmdArgs like ShiftToZero() and MoveToBack(). For Eg,
3232
# Default bindings: pdf.plotOn(frame, ROOT.RooFit.MoveToBack())
3333
# With pythonizations: pdf.plotOn(frame, MoveToBack=True)

bindings/pyroot/pythonizations/python/ROOT/_pythonization/_tfile.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@
7373
'''
7474

7575
from . import pythonization
76-
from libcppyy import bind_object
7776

7877

7978
def _TFileConstructor(self, *args):
@@ -90,14 +89,17 @@ def _TFileConstructor(self, *args):
9089

9190

9291
def _TFileOpen(klass, *args):
92+
93+
import ROOT
94+
9395
# Redefinition of ROOT.TFile.Open(str, ...):
9496
# check if the instance of TFile is a C++ nullptr and raise a
9597
# OSError if this is the case.
9698
# Parameters:
9799
# klass: TFile class
98100
# *args: arguments passed to the constructor
99101
f = klass._OriginalOpen(*args)
100-
if f == bind_object(0, klass):
102+
if f == ROOT.bind_object(0, klass):
101103
# args[0] can be either a string or a TFileOpenHandle
102104
raise OSError('Failed to open file {}'.format(str(args[0])))
103105
return f

bindings/pyroot/pythonizations/python/ROOT/_pythonization/_tseqcollection.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
################################################################################
1010

1111
from . import pythonization
12-
from libcppyy import SetOwnership
1312

1413
import sys
1514

@@ -75,6 +74,9 @@ def _remove_at(self, idx):
7574
return self.Remove(lnk)
7675

7776
def _setitem_pyz(self, idx, val):
77+
78+
import ROOT
79+
7880
# Parameters:
7981
# - self: collection where to set item/s
8082
# - idx: index/slice of the item/s
@@ -94,7 +96,7 @@ def _setitem_pyz(self, idx, val):
9496
# Prevent this new Python proxy from owning the C++ object
9597
# Otherwise we get an 'already deleted' error in
9698
# TList::Clear when the application ends
97-
SetOwnership(elem, False)
99+
ROOT.SetOwnership(elem, False)
98100
try:
99101
i = next(it)
100102
self[i] = elem

bindings/pyroot/pythonizations/src/PyROOTModule.cxx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,6 @@ extern "C" PyObject* PyInit_libROOTPythonizations()
9898
// keep gRootModule, but do not increase its reference count even as it is borrowed,
9999
// or a self-referencing cycle would be created
100100

101-
// Make sure libcppyy has been imported
102-
PyImport_ImportModule("libcppyy");
103-
104101
// setup PyROOT
105102
PyROOT::Init();
106103

bindings/pyroot/pythonizations/test/tcollection_iterable.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import unittest
22

33
import ROOT
4-
from libcppyy import SetOwnership
54

65

76
class TCollectionIterable(unittest.TestCase):
@@ -21,7 +20,7 @@ def create_tcollection(self):
2120
for _ in range(self.num_elems):
2221
o = ROOT.TObject()
2322
# Prevent immediate deletion of C++ TObjects
24-
SetOwnership(o, False)
23+
ROOT.SetOwnership(o, False)
2524
c.Add(o)
2625

2726
return c

0 commit comments

Comments
 (0)