Skip to content

Commit

Permalink
[PyROOT] Drop support for from ROOT import *
Browse files Browse the repository at this point in the history
Wild card imports like `from ROOT import *` stopped working with Python
3.11 given some changes in the CPython API.

For that reason, upstream `cppyy` also dropped support for lazy lookups,
which was the feature that enabled wildcard imports:
wlav/CPyCppyy@64fd890#diff-6160c0eb004dabeedaeb58d804a7ecd3e563d9379e9e7af39623fd38d0bc6a37R352

This commit suggests to document `from ROOT import *` officially as
unsupported and remove the corresponding code path in the ROOT facade.

Closes #7669.
  • Loading branch information
guitargeek committed Feb 5, 2024
1 parent 591f72e commit dfdbceb
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 22 deletions.
1 change: 1 addition & 0 deletions README/ReleaseNotes/v632/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ The following people have contributed to this new version:
- Some redundant **RooDataSet** constructors are deprecated and will be removed in ROOT 6.34.
Please use the RooDataSet constructors that take RooFit command arguments instead
- ROOT does not longer support Python 2. The minimum required Python version to build ROOT is 3.8.
- Support for wildcard imports like `from ROOT import *` is dropped from PyROOT

## Core Libraries

Expand Down
23 changes: 1 addition & 22 deletions bindings/pyroot/pythonizations/python/ROOT/_facade.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,22 +145,6 @@ def AddressOf(self, obj):
# Create a buffer (LowLevelView) from address
return cppyy.ll.cast[out_type](addr)

def _handle_import_all(self):
# Called if "from ROOT import *" is executed in the app.
# Customises lookup in Python's main module to also
# check in C++'s global namespace

# Get caller module (jump over the facade frames)
num_frame = 2
frame = sys._getframe(num_frame).f_globals["__name__"]
while frame == "ROOT._facade":
num_frame += 1
frame = sys._getframe(num_frame).f_globals["__name__"]
caller = sys.modules[frame]

# Install the hook
cppyy_backend._set_cpp_lazy_lookup(caller.__dict__)

def _fallback_getattr(self, name):
# Try:
# - in the global namespace
Expand All @@ -170,13 +154,8 @@ def _fallback_getattr(self, name):
# The first two attempts allow to lookup
# e.g. ROOT.ROOT.Math as ROOT.Math

if name == "__all__":
self._handle_import_all()
# Make the attributes of the facade be injected in the
# caller module
raise AttributeError()
# Note that hasattr caches the lookup for getattr
elif hasattr(gbl_namespace, name):
if hasattr(gbl_namespace, name):
return getattr(gbl_namespace, name)
elif hasattr(gbl_namespace.ROOT, name):
return getattr(gbl_namespace.ROOT, name)
Expand Down

0 comments on commit dfdbceb

Please sign in to comment.