From 80b9d5716f9696daf73160d74600e7cad8a56cca Mon Sep 17 00:00:00 2001 From: Oliver Papst Date: Sat, 23 Mar 2024 14:42:01 +0100 Subject: [PATCH] Support old and new prompt_toolkit versions --- README.md | 2 +- hdtv/cmdline.py | 21 ++++++++++++++++++--- setup.py | 2 +- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a526c32c..eea58739 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ To build and run HDTV, the following dependencies are required: * Python - Tested with 3.8, 3.9, 3.10, and 3.11 - - Packages: numpy scipy matplotlib prompt_toolkit>=3.0.40 uncertainties + - Packages: numpy scipy matplotlib prompt_toolkit>=3.0.14 uncertainties - Packages for development & testing: docutils pytest pytest-cov * [Cern ROOT](https://root.cern/) 6 - Tested with version 6.26 and higher diff --git a/hdtv/cmdline.py b/hdtv/cmdline.py index 1de8ba23..b6871f72 100644 --- a/hdtv/cmdline.py +++ b/hdtv/cmdline.py @@ -34,6 +34,7 @@ from enum import Enum, auto from pwd import getpwuid +import prompt_toolkit from prompt_toolkit.auto_suggest import AutoSuggestFromHistory from prompt_toolkit.completion import Completer, Completion from prompt_toolkit.enums import EditingMode @@ -419,6 +420,12 @@ def __init__(self, command_tree): self.loop = asyncio.new_event_loop() asyncio.set_event_loop(self.loop) + # prompt_toolkit < 3.0.40 brings its own event loop + ptk_version = prompt_toolkit.__version__ + self.fallback_eventloop = ( + ptk_version.startswith("3.0.") and int(ptk_version[4:].split("-")[0]) < 40 + ) + self.command_tree = command_tree self.history = None @@ -434,6 +441,9 @@ def __init__(self, command_tree): self.exit_handlers = [] def StartEventLoop(self): + if self.fallback_eventloop: + return + def _loop(loop): asyncio.set_event_loop(loop) loop.run_forever() @@ -517,12 +527,16 @@ def Exit(self, args=None): def AsyncExit(self): """Asynchronous exit; to be called from another thread""" - self.Exit() + if self.fallback_eventloop: + self.loop.call_soon_threadsafe(self.Exit) + else: + self.Exit() self.loop.call_soon_threadsafe( lambda: self.session.app.exit(style="class:exiting") ) - self.thread.join() + if not self.fallback_eventloop: + self.thread.join() def EOFHandler(self): self.Exit() @@ -747,7 +761,8 @@ def set_vi_mode(vi_mode): if line: self.DoLine(line) - self.loop.call_soon_threadsafe(lambda: self.loop.stop()) + if not self.fallback_eventloop: + self.loop.call_soon_threadsafe(lambda: self.loop.stop()) class HDTVCompleter(Completer): diff --git a/setup.py b/setup.py index 2d660700..11c9a26d 100755 --- a/setup.py +++ b/setup.py @@ -64,7 +64,7 @@ "matplotlib", "numpy", "ipython", - "prompt_toolkit>=3.0.40", + "prompt_toolkit>=3.0.14", "traitlets", "uncertainties", ],