From 3ae1d4903d937aad5270a7f80b2e9d2a5030c303 Mon Sep 17 00:00:00 2001 From: Muntasir Mamun Date: Wed, 21 Oct 2026 22:31:08 +0600 Subject: [PATCH] [pyroot] Allow toggling batch mode from True to False in middle of session Fixes #18900 Pythonize TROOT to hook SetBatch so that when batch mode is disabled mid-session (SetBatch(False)), interactive graphics are initialized via app.init_graphics(). Also ensure init_graphics is idempotent to avoid duplicate registrations. --- .../python/ROOT/_application.py | 5 ++- .../python/ROOT/_pythonization/_troot.py | 36 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 bindings/pyroot/pythonizations/python/ROOT/_pythonization/_troot.py diff --git a/bindings/pyroot/pythonizations/python/ROOT/_application.py b/bindings/pyroot/pythonizations/python/ROOT/_application.py index 255636a0bffc2..0f32ddf4ec3f1 100644 --- a/bindings/pyroot/pythonizations/python/ROOT/_application.py +++ b/bindings/pyroot/pythonizations/python/ROOT/_application.py @@ -142,7 +142,10 @@ def displayhook(v): def init_graphics(self, gEnv, gSystem): """Configure ROOT graphics to be used interactively""" - # Note that we only end up in this function if gROOT.IsBatch() is false + if getattr(self, "_graphics_initialized", False): + return + self._graphics_initialized = True + import __main__ if self._is_ipython and "IPython" in sys.modules and sys.modules["IPython"].version_info[0] >= 5: diff --git a/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_troot.py b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_troot.py new file mode 100644 index 0000000000000..fa34e289e77e4 --- /dev/null +++ b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_troot.py @@ -0,0 +1,36 @@ +# Author: PyROOT Team + +################################################################################ +# Copyright (C) 1995-2026, Rene Brun and Fons Rademakers. # +# All rights reserved. # +# # +# For the licensing terms see $ROOTSYS/LICENSE. # +# For the list of contributors see $ROOTSYS/README/CREDITS. # +################################################################################ + +r""" +\pythondoc TROOT + +Pythonizations for TROOT class, enabling dynamic batch mode toggling. +""" + +from . import pythonization + + +def _TROOT_SetBatch(self, batch=True): + """ + Sets the batch mode on ROOT. + If batch mode is turned off in the middle of a session, ensure the + interactive graphics environment is initialized. + """ + import ROOT + + self._cpp_SetBatch(batch) + if not batch and hasattr(ROOT, "app") and ROOT.PyConfig.StartGUIThread: + ROOT.app.init_graphics(ROOT._cppyy.gbl.gEnv, ROOT._cppyy.gbl.gSystem) + + +@pythonization("TROOT") +def pythonize_troot(klass): + klass._cpp_SetBatch = klass.SetBatch + klass.SetBatch = _TROOT_SetBatch