Skip to content

Commit 262f0ca

Browse files
committed
Revert "[Python] Make linter accept ROOT/__init__.py importing at the beginning"
This reverts commit 52ac3a0, it broke Python on Windows.
1 parent 2afc829 commit 262f0ca

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed

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

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

11-
import atexit
12-
import builtins
11+
import importlib
1312
import os
1413
import sys
15-
import types
16-
from importlib.abc import Loader, MetaPathFinder
17-
from importlib.machinery import ModuleSpec
18-
from typing import Optional, Union
19-
20-
import cppyy
21-
import cppyy.types
22-
23-
from . import _asan # noqa: F401 # imported for side effects for setup specific to AddressSanitizer environments
24-
from ._facade import ROOTFacade
25-
from ._pythonization import _register_pythonizations
2614

2715
# Prevent cppyy's check for extra header directory
2816
os.environ["CPPYY_API_PATH"] = "none"
@@ -35,23 +23,32 @@
3523
# the path of the ROOT library directory (only needed on Windows). For example,
3624
# if the ROOT Python module is in $ROOTSYS/bin/ROOT/__init__.py, the libraries
3725
# are usually in $ROOTSYS/bin.
38-
if "win32" in sys.platform:
39-
root_module_path = os.path.dirname(__file__) # expected to be ${CMAKE_INSTALL_PYTHONDIR}/ROOT
40-
root_install_pythondir = os.path.dirname(root_module_path) # expected to be ${CMAKE_INSTALL_PYTHONDIR}
26+
if 'win32' in sys.platform:
27+
root_module_path = os.path.dirname(__file__) # expected to be ${CMAKE_INSTALL_PYTHONDIR}/ROOT
28+
root_install_pythondir = os.path.dirname(root_module_path) # expected to be ${CMAKE_INSTALL_PYTHONDIR}
4129
os.add_dll_directory(root_install_pythondir)
4230

31+
# Do setup specific to AddressSanitizer environments
32+
from . import _asan
33+
34+
import cppyy
35+
import cppyy.types
36+
4337
# Build cache of commonly used python strings (the cache is python intern, so
4438
# all strings are shared python-wide, not just in PyROOT).
4539
# See: https://docs.python.org/3.2/library/sys.html?highlight=sys.intern#sys.intern
4640
_cached_strings = []
4741
for s in ["Branch", "FitFCN", "ROOT", "SetBranchAddress", "SetFCN", "_TClass__DynamicCast", "__class__"]:
4842
_cached_strings.append(sys.intern(s))
4943

50-
5144
# Trigger the addition of the pythonizations
45+
from ._pythonization import _register_pythonizations
46+
5247
_register_pythonizations()
5348

5449
# Check if we are in the IPython shell
50+
import builtins
51+
5552
_is_ipython = hasattr(builtins, "__IPYTHON__")
5653

5754

@@ -75,6 +72,9 @@ def __getitem__(self, _):
7572
__all__ = _PoisonedDunderAll()
7673

7774
# Configure ROOT facade module
75+
import sys
76+
from ._facade import ROOTFacade
77+
7878
_root_facade = ROOTFacade(sys.modules[__name__], _is_ipython)
7979
sys.modules[__name__] = _root_facade
8080

@@ -84,6 +84,9 @@ def __getitem__(self, _):
8484
# * https://docs.python.org/3/library/importlib.html#module-importlib.abc
8585
#
8686
# * https://python.plainenglish.io/metapathfinders-or-how-to-change-python-import-behavior-a1cf3b5a13ec
87+
from importlib.abc import Loader, MetaPathFinder
88+
from importlib.machinery import ModuleSpec
89+
from importlib.util import spec_from_loader
8790

8891

8992
def _can_be_module(obj) -> bool:
@@ -105,6 +108,10 @@ def _can_be_module(obj) -> bool:
105108
return False
106109

107110

111+
from typing import Optional, Union
112+
import types
113+
114+
108115
def _lookup_root_module(fullname: str) -> Optional[Union[types.ModuleType, cppyy.types.Scope]]:
109116
"""
110117
Recursively looks up attributes of the ROOT facade, using a full module
@@ -153,8 +160,6 @@ class _RootNamespaceFinder(MetaPathFinder):
153160
"""
154161

155162
def find_spec(self, fullname: str, path, target=None) -> ModuleSpec:
156-
from importlib.util import spec_from_loader
157-
158163
if not fullname.startswith("ROOT."):
159164
# This finder only finds ROOT.*
160165
return None
@@ -173,11 +178,11 @@ def find_spec(self, fullname: str, path, target=None) -> ModuleSpec:
173178

174179
ip = get_ipython()
175180
if hasattr(ip, "kernel"):
176-
import JupyROOT # noqa: F401 # imported the side effect of setting up JupyROOT
177-
181+
import JupyROOT
178182
# from . import JsMVA
179183

180184
# Register cleanup
185+
import atexit
181186

182187

183188
def cleanup():
@@ -187,5 +192,4 @@ def cleanup():
187192
facade.__dict__["app"].keep_polling = False
188193
facade.__dict__["app"].process_root_events.join()
189194

190-
191195
atexit.register(cleanup)

0 commit comments

Comments
 (0)