Skip to content

Commit 458849c

Browse files
committed
[Python] Only use the ROOT gui event loop in TerminalInteractiveShell
Only the TerminalInteractiveShell will use the input hooks that are registered via terminal.pt_inputhooks, so for other shells the `%gui ROOT` magic command will not do anything anyway. It's better to not run it then, in order to avoid errors about the `ROOT` gui not being available.
1 parent eb582f8 commit 458849c

1 file changed

Lines changed: 12 additions & 6 deletions

File tree

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def _ipython_config():
3535

3636
from IPython import get_ipython
3737
from IPython.terminal import pt_inputhooks
38+
from IPython.terminal.interactiveshell import TerminalInteractiveShell
3839

3940
def inputhook(context):
4041
while not context.input_is_ready():
@@ -44,7 +45,10 @@ def inputhook(context):
4445
pt_inputhooks.register('ROOT', inputhook)
4546

4647
ipy = get_ipython()
47-
if ipy:
48+
49+
# Only the TerminalInteractiveShell will use the input hooks that are
50+
# registered via terminal.pt_inputhooks.
51+
if ipy and isinstance(ipy, TerminalInteractiveShell):
4852
get_ipython().run_line_magic('gui', 'ROOT')
4953

5054
@staticmethod
@@ -75,10 +79,11 @@ def init_graphics(self):
7579

7680
# Note that we only end up in this function if gROOT.IsBatch() is false
7781
import __main__
78-
if self._is_ipython and 'IPython' in sys.modules and sys.modules['IPython'].version_info[0] >= 5:
82+
83+
if self._is_ipython and "IPython" in sys.modules and sys.modules["IPython"].version_info[0] >= 5:
7984
# ipython and notebooks, register our event processing with their hooks
8085
self._ipython_config()
81-
elif sys.flags.interactive == 1 or not hasattr(__main__, '__file__') or gSystem.InheritsFrom('TMacOSXSystem'):
86+
elif sys.flags.interactive == 1 or not hasattr(__main__, "__file__") or gSystem.InheritsFrom("TMacOSXSystem"):
8287
# Python in interactive mode, use the PyOS_InputHook to call our event processing
8388
# - sys.flags.interactive checks for the -i flags passed to python
8489
# - __main__ does not have the attribute __file__ if the Python prompt is started directly
@@ -94,12 +99,13 @@ def _process_root_events(self):
9499
while self.keep_polling:
95100
gSystem.ProcessEvents()
96101
time.sleep(0.01)
102+
97103
import threading
98-
self.keep_polling = True # Used to shut down the thread safely at teardown time
104+
105+
self.keep_polling = True # Used to shut down the thread safely at teardown time
99106
update_thread = threading.Thread(None, _process_root_events, None, (self,))
100-
self.process_root_events = update_thread # The thread is joined at teardown time
107+
self.process_root_events = update_thread # The thread is joined at teardown time
101108
update_thread.daemon = True
102109
update_thread.start()
103110

104111
self._set_display_hook()
105-

0 commit comments

Comments
 (0)