From cf63863ca19ddf16e6789e819989a504ca9448e7 Mon Sep 17 00:00:00 2001 From: Matthias Bussonnier Date: Tue, 19 Sep 2023 09:50:45 +0200 Subject: [PATCH] ENH: Don't show message when enable the current eventloop. Should take care of #14006, at least partially. --- IPython/terminal/interactiveshell.py | 18 +++++++++++++----- IPython/terminal/pt_inputhooks/__init__.py | 3 ++- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/IPython/terminal/interactiveshell.py b/IPython/terminal/interactiveshell.py index 37e0b86981e..9b63159dfa5 100644 --- a/IPython/terminal/interactiveshell.py +++ b/IPython/terminal/interactiveshell.py @@ -4,7 +4,7 @@ import os import sys from warnings import warn -from typing import Union as UnionType +from typing import Union as UnionType, Optional from IPython.core.async_helpers import get_asyncio_loop from IPython.core.interactiveshell import InteractiveShell, InteractiveShellABC @@ -912,8 +912,9 @@ def inputhook(self, context): if self._inputhook is not None: self._inputhook(context) - active_eventloop = None - def enable_gui(self, gui=None): + active_eventloop: Optional[str] = None + + def enable_gui(self, gui: Optional[str] = None) -> None: if self.simple_prompt is True and gui is not None: print( f'Cannot install event loop hook for "{gui}" when running with `--simple-prompt`.' @@ -928,8 +929,15 @@ def enable_gui(self, gui=None): return if self._inputhook is not None and gui is not None: - print( - f"Shell is already running a gui event loop for {self.active_eventloop}. " + newev, newinhook = get_inputhook_name_and_func(gui) + if self._inputhook == newinhook: + # same inputhook, do nothing + self.log.info( + f"Shell is already running the {self.active_eventloop} eventloop. Doing nothing" + ) + return + self.log.warning( + f"Shell is already running a different gui event loop for {self.active_eventloop}. " "Call with no arguments to disable the current loop." ) return diff --git a/IPython/terminal/pt_inputhooks/__init__.py b/IPython/terminal/pt_inputhooks/__init__.py index 9043f15e86b..22a681e249f 100644 --- a/IPython/terminal/pt_inputhooks/__init__.py +++ b/IPython/terminal/pt_inputhooks/__init__.py @@ -1,5 +1,6 @@ import importlib import os +from typing import Tuple, Callable aliases = { 'qt4': 'qt', @@ -119,7 +120,7 @@ def set_qt_api(gui): return qt_env2gui[QT_API] -def get_inputhook_name_and_func(gui): +def get_inputhook_name_and_func(gui: str) -> Tuple[str, Callable]: if gui in registered: return gui, registered[gui]