Skip to content

Commit 530a92c

Browse files
committed
[Python] Revisit try-except blocks in the ROOT facade
There are some `except Exceptions` in the ROOT facade, which are not much better than bare `except` because they catch any meaningful error and turn it into an uninformative "Failed to pythonize the namespace Foo" exception, which makes it harder to develop these namespace pythonizations and is also not helpful to the user. The only exception that we expect is an `ImportError` if a given ROOT component is not available, in which case we should `pass` on the Pythonization. All try-except blocks are revisited accordingly: 1. Remove `try-except` for the VecOps pythonization, because it is available unconditionally, as VecOps is in TMath 2. RDF/DistRDF was already correct, I just added a comment to explain 3. RooFit: this is the case where we had the catch-all, which is corrected 4. TMVA: for some reason, the pythonization was conditional on RDF (probably a copy-paste error or a remnant from when the RBatchGenerator lived in TMVA). This is fixed and a meaningfly try-catch block implemented. 5. UHI: Just like VecOps, this module is unconditionally included and therefore needs to `try-except`.
1 parent 0046874 commit 530a92c

1 file changed

Lines changed: 29 additions & 31 deletions

File tree

  • bindings/pyroot/pythonizations/python/ROOT

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

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -327,12 +327,9 @@ def __version__(self):
327327
@property
328328
def VecOps(self):
329329
ns = self._fallback_getattr("VecOps")
330-
try:
331-
from ._pythonization._rvec import _AsRVec
330+
from ._pythonization._rvec import _AsRVec
332331

333-
ns.AsRVec = _AsRVec
334-
except Exception:
335-
raise Exception("Failed to pythonize the namespace VecOps")
332+
ns.AsRVec = _AsRVec
336333
del type(self).VecOps
337334
return ns
338335

@@ -403,6 +400,7 @@ def MakePandasDataFrame(df):
403400
ns.Experimental.VariationsFor = _variationsfor(ns.Distributed.VariationsFor, ns.Experimental.VariationsFor)
404401
ns.Experimental.FromSpec = _fromspec(ns.Distributed.FromSpec, ns.Experimental.FromSpec)
405402
except ImportError:
403+
# _rdf_namespace submodule not available (expected for dataframe=OFF)
406404
pass
407405

408406
del type(self).RDF
@@ -422,41 +420,44 @@ def RDataFrame(self):
422420

423421
return _rdataframe(local_rdf, ROOT._distrdf.RDataFrame)
424422
except ImportError:
423+
# _distrdf submodule not available (expected for dataframe=OFF)
425424
return local_rdf
426425

427426
# Overload RooFit namespace
428427
@property
429428
def RooFit(self):
430-
from ._pythonization._roofit import pythonize_roofit_namespace
431-
432429
ns = self._fallback_getattr("RooFit")
433430
try:
434-
pythonize_roofit_namespace(ns)
435-
except Exception:
436-
raise Exception("Failed to pythonize the namespace RooFit")
431+
from ._pythonization._roofit import pythonize_roofit_namespace
432+
except ImportError:
433+
# _roofit submodule not available (expected for roofit=OFF)
434+
del type(self).RooFit
435+
return ns
436+
437+
pythonize_roofit_namespace(ns)
438+
437439
del type(self).RooFit
438440
return ns
439441

440442
# Overload TMVA namespace
441443
@property
442444
def TMVA(self):
443-
# This line is needed to import the pythonizations in _tmva directory.
444-
# The comment suppresses linter errors about unused imports.
445-
from ._pythonization import _tmva # noqa: F401
446-
from ._pythonization._tmva._sofie._parser._keras.parser import PyKeras
447-
448445
ns = self._fallback_getattr("TMVA")
449-
setattr(ns.Experimental.SOFIE, "PyKeras", PyKeras)
450-
hasRDF = "dataframe" in self.gROOT.GetConfigFeatures()
451-
if hasRDF:
452-
try:
453-
from ._pythonization._tmva._rtensor import _AsRTensor
454-
from ._pythonization._tmva._tree_inference import SaveXGBoost
455-
456-
ns.Experimental.AsRTensor = _AsRTensor
457-
ns.Experimental.SaveXGBoost = SaveXGBoost
458-
except Exception:
459-
raise Exception("Failed to pythonize the namespace TMVA")
446+
try:
447+
# This line is needed to import the pythonizations in _tmva directory.
448+
# The comment suppresses linter errors about unused imports.
449+
from ._pythonization import _tmva # noqa: F401
450+
from ._pythonization._tmva._rtensor import _AsRTensor
451+
from ._pythonization._tmva._sofie._parser._keras.parser import PyKeras
452+
from ._pythonization._tmva._tree_inference import SaveXGBoost
453+
454+
setattr(ns.Experimental.SOFIE, "PyKeras", PyKeras)
455+
456+
ns.Experimental.AsRTensor = _AsRTensor
457+
ns.Experimental.SaveXGBoost = SaveXGBoost
458+
except ImportError:
459+
# _tmva submodule not available (expected for tmva=OFF)
460+
pass
460461
del type(self).TMVA
461462
return ns
462463

@@ -498,12 +499,9 @@ def uhi(self):
498499
uhi_module = types.ModuleType("uhi")
499500
uhi_module.__file__ = "<module ROOT>"
500501
uhi_module.__package__ = self
501-
try:
502-
from ._pythonization._uhi import _add_module_level_uhi_helpers
502+
from ._pythonization._uhi import _add_module_level_uhi_helpers
503503

504-
_add_module_level_uhi_helpers(uhi_module)
505-
except ImportError:
506-
raise Exception("Failed to pythonize the namespace uhi")
504+
_add_module_level_uhi_helpers(uhi_module)
507505
return uhi_module
508506

509507
@property

0 commit comments

Comments
 (0)