Skip to content

Commit 558959f

Browse files
committed
[Python] Use local imports to delay pythonization library loading
This should fix errors on Windows where the `libROOTPythonizations` is attempted to be loaded before the DLL directory is set correctly in `ROOT/__init__.py`.
1 parent 262f0ca commit 558959f

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77
import cppyy
88
import cppyy.ll
99

10-
from ._application import PyROOTApplication
11-
from ._numbadeclare import _NumbaDeclareDecorator
12-
from ._pythonization import pythonization
13-
1410

1511
class PyROOTConfiguration(object):
1612
"""Class for configuring PyROOT"""
@@ -57,6 +53,8 @@ class ROOTFacade(types.ModuleType):
5753
"""Facade class for ROOT module"""
5854

5955
def __init__(self, module, is_ipython):
56+
from ._pythonization import pythonization
57+
6058
types.ModuleType.__init__(self, module.__name__)
6159

6260
self.module = module
@@ -181,6 +179,8 @@ def _register_converters_and_executors(self):
181179
CPyCppyyRegisterExecutorAlias(name, target)
182180

183181
def _finalSetup(self):
182+
from ._application import PyROOTApplication
183+
184184
# Prevent this method from being re-entered through the gROOT wrapper
185185
self.__dict__["gROOT"] = cppyy.gbl.ROOT.GetROOT()
186186

@@ -443,6 +443,8 @@ def TMVA(self):
443443
# Create and overload Numba namespace
444444
@property
445445
def Numba(self):
446+
from ._numbadeclare import _NumbaDeclareDecorator
447+
446448
cppyy.cppdef("namespace Numba {}")
447449
ns = self._fallback_getattr("Numba")
448450
ns.Declare = staticmethod(_NumbaDeclareDecorator)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515
import cppyy
1616

17-
from ._generic import pythonize_generic
18-
1917
# \cond INTERNALS
2018
gbl_namespace = cppyy.gbl
2119
# \endcond
@@ -70,6 +68,7 @@ def pythonization(class_name, ns="::", is_prefix=False):
7068

7169
def passes_filter(class_name):
7270
return any(class_name.startswith(prefix) for prefix in target)
71+
7372
else:
7473

7574
def passes_filter(class_name):
@@ -114,6 +113,7 @@ def cppyy_pythonizor(klass, name):
114113
name (string): name of the class that is the current candidate
115114
to be pythonized.
116115
"""
116+
from ._generic import pythonize_generic
117117

118118
fqn = klass.__cpp_name__
119119

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

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

11-
from ROOT.libROOTPythonizations import AddPrettyPrintingPyz
1211

1312
def _add_getitem_checked(klass):
1413
# Parameters:
@@ -27,25 +26,28 @@ def getitem_checked(o, i):
2726
if i >= 0 and i < len(o):
2827
return o._getitem__unchecked(i)
2928
else:
30-
raise IndexError('index out of range')
29+
raise IndexError("index out of range")
3130

3231
klass._getitem__unchecked = klass.__getitem__
3332
klass.__getitem__ = getitem_checked
3433

34+
3535
# Generic pythonizor for pretty printing that is applied to (almost) all classes
3636
def pythonize_generic(klass, name):
37+
from ROOT.libROOTPythonizations import AddPrettyPrintingPyz
38+
3739
# Parameters:
3840
# klass: class to be pythonized
3941
# name: string containing the name of the class
4042

4143
# Add pretty printing via setting the __str__ special function
4244

4345
# Exclude classes which have the method __str__ already defined in C++
44-
m = getattr(klass, '__str__', None)
45-
has_cpp_str = True if m is not None and type(m).__name__ == 'CPPOverload' else False
46+
m = getattr(klass, "__str__", None)
47+
has_cpp_str = True if m is not None and type(m).__name__ == "CPPOverload" else False
4648

4749
# Exclude std::string with its own pythonization from cppyy
48-
exclude = [ 'std::string' ]
50+
exclude = ["std::string"]
4951

5052
if name not in exclude and not has_cpp_str:
5153
AddPrettyPrintingPyz(klass)

0 commit comments

Comments
 (0)