88# For the list of contributors see $ROOTSYS/README/CREDITS. #
99################################################################################
1010
11- import importlib
11+ import atexit
12+ import builtins
1213import os
1314import 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
1426
1527# Prevent cppyy's check for extra header directory
1628os .environ ["CPPYY_API_PATH" ] = "none"
2335# the path of the ROOT library directory (only needed on Windows). For example,
2436# if the ROOT Python module is in $ROOTSYS/bin/ROOT/__init__.py, the libraries
2537# are usually in $ROOTSYS/bin.
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}
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}
2941 os .add_dll_directory (root_install_pythondir )
3042
31- # Do setup specific to AddressSanitizer environments
32- from . import _asan
33-
34- import cppyy
35- import cppyy .types
36-
3743# Build cache of commonly used python strings (the cache is python intern, so
3844# all strings are shared python-wide, not just in PyROOT).
3945# See: https://docs.python.org/3.2/library/sys.html?highlight=sys.intern#sys.intern
4046_cached_strings = []
4147for s in ["Branch" , "FitFCN" , "ROOT" , "SetBranchAddress" , "SetFCN" , "_TClass__DynamicCast" , "__class__" ]:
4248 _cached_strings .append (sys .intern (s ))
4349
44- # Trigger the addition of the pythonizations
45- from ._pythonization import _register_pythonizations
4650
51+ # Trigger the addition of the pythonizations
4752_register_pythonizations ()
4853
4954# Check if we are in the IPython shell
50- import builtins
51-
5255_is_ipython = hasattr (builtins , "__IPYTHON__" )
5356
5457
@@ -72,9 +75,6 @@ def __getitem__(self, _):
7275__all__ = _PoisonedDunderAll ()
7376
7477# Configure ROOT facade module
75- import sys
76- from ._facade import ROOTFacade
77-
7878_root_facade = ROOTFacade (sys .modules [__name__ ], _is_ipython )
7979sys .modules [__name__ ] = _root_facade
8080
@@ -84,9 +84,6 @@ 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
9087
9188
9289def _can_be_module (obj ) -> bool :
@@ -108,10 +105,6 @@ def _can_be_module(obj) -> bool:
108105 return False
109106
110107
111- from typing import Optional , Union
112- import types
113-
114-
115108def _lookup_root_module (fullname : str ) -> Optional [Union [types .ModuleType , cppyy .types .Scope ]]:
116109 """
117110 Recursively looks up attributes of the ROOT facade, using a full module
@@ -160,6 +153,8 @@ class _RootNamespaceFinder(MetaPathFinder):
160153 """
161154
162155 def find_spec (self , fullname : str , path , target = None ) -> ModuleSpec :
156+ from importlib .util import spec_from_loader
157+
163158 if not fullname .startswith ("ROOT." ):
164159 # This finder only finds ROOT.*
165160 return None
@@ -178,11 +173,11 @@ def find_spec(self, fullname: str, path, target=None) -> ModuleSpec:
178173
179174 ip = get_ipython ()
180175 if hasattr (ip , "kernel" ):
181- import JupyROOT
176+ import JupyROOT # noqa: F401 # imported the side effect of setting up JupyROOT
177+
182178 # from . import JsMVA
183179
184180# Register cleanup
185- import atexit
186181
187182
188183def cleanup ():
@@ -192,4 +187,5 @@ def cleanup():
192187 facade .__dict__ ["app" ].keep_polling = False
193188 facade .__dict__ ["app" ].process_root_events .join ()
194189
190+
195191atexit .register (cleanup )
0 commit comments