aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/prompt-toolkit/py3/prompt_toolkit/application
diff options
context:
space:
mode:
authorrobot-contrib <robot-contrib@yandex-team.com>2023-12-11 10:59:41 +0300
committerrobot-contrib <robot-contrib@yandex-team.com>2023-12-11 11:40:57 +0300
commit708e84a1342eccd8b69c761dd2916e33503a883a (patch)
treeee90cd4ab26e843c00724b957ef247cc3d1b6f9f /contrib/python/prompt-toolkit/py3/prompt_toolkit/application
parent425d65a76c5bda62894f93d32f1f5e32f7439539 (diff)
downloadydb-708e84a1342eccd8b69c761dd2916e33503a883a.tar.gz
Update contrib/python/prompt-toolkit/py3 to 3.0.41
Diffstat (limited to 'contrib/python/prompt-toolkit/py3/prompt_toolkit/application')
-rw-r--r--contrib/python/prompt-toolkit/py3/prompt_toolkit/application/application.py59
-rw-r--r--contrib/python/prompt-toolkit/py3/prompt_toolkit/application/current.py7
-rw-r--r--contrib/python/prompt-toolkit/py3/prompt_toolkit/application/dummy.py2
3 files changed, 48 insertions, 20 deletions
diff --git a/contrib/python/prompt-toolkit/py3/prompt_toolkit/application/application.py b/contrib/python/prompt-toolkit/py3/prompt_toolkit/application/application.py
index bcc819095c2..726fc0a067d 100644
--- a/contrib/python/prompt-toolkit/py3/prompt_toolkit/application/application.py
+++ b/contrib/python/prompt-toolkit/py3/prompt_toolkit/application/application.py
@@ -40,7 +40,9 @@ from prompt_toolkit.cursor_shapes import AnyCursorShapeConfig, to_cursor_shape_c
from prompt_toolkit.data_structures import Size
from prompt_toolkit.enums import EditingMode
from prompt_toolkit.eventloop import (
+ InputHook,
get_traceback_from_context,
+ new_eventloop_with_inputhook,
run_in_executor_with_context,
)
from prompt_toolkit.eventloop.utils import call_soon_threadsafe
@@ -655,7 +657,7 @@ class Application(Generic[_AppResult]):
# See: https://github.com/prompt-toolkit/python-prompt-toolkit/issues/1553
handle_sigint = False
- async def _run_async(f: "asyncio.Future[_AppResult]") -> _AppResult:
+ async def _run_async(f: asyncio.Future[_AppResult]) -> _AppResult:
context = contextvars.copy_context()
self.context = context
@@ -898,13 +900,12 @@ class Application(Generic[_AppResult]):
set_exception_handler: bool = True,
handle_sigint: bool = True,
in_thread: bool = False,
+ inputhook: InputHook | None = None,
) -> _AppResult:
"""
A blocking 'run' call that waits until the UI is finished.
- This will start the current asyncio event loop. If no loop is set for
- the current thread, then it will create a new loop. If a new loop was
- created, this won't close the new loop (if `in_thread=False`).
+ This will run the application in a fresh asyncio event loop.
:param pre_run: Optional callable, which is called right after the
"reset" of the application.
@@ -937,6 +938,7 @@ class Application(Generic[_AppResult]):
set_exception_handler=set_exception_handler,
# Signal handling only works in the main thread.
handle_sigint=False,
+ inputhook=inputhook,
)
except BaseException as e:
exception = e
@@ -954,17 +956,46 @@ class Application(Generic[_AppResult]):
set_exception_handler=set_exception_handler,
handle_sigint=handle_sigint,
)
- try:
- # See whether a loop was installed already. If so, use that. That's
- # required for the input hooks to work, they are installed using
- # `set_event_loop`.
- loop = asyncio.get_event_loop()
- except RuntimeError:
+
+ def _called_from_ipython() -> bool:
+ try:
+ return (
+ "IPython/terminal/interactiveshell.py"
+ in sys._getframe(3).f_code.co_filename
+ )
+ except BaseException:
+ return False
+
+ if inputhook is not None:
+ # Create new event loop with given input hook and run the app.
+ # In Python 3.12, we can use asyncio.run(loop_factory=...)
+ # For now, use `run_until_complete()`.
+ loop = new_eventloop_with_inputhook(inputhook)
+ result = loop.run_until_complete(coro)
+ loop.run_until_complete(loop.shutdown_asyncgens())
+ loop.close()
+ return result
+
+ elif _called_from_ipython():
+ # workaround to make input hooks work for IPython until
+ # https://github.com/ipython/ipython/pull/14241 is merged.
+ # IPython was setting the input hook by installing an event loop
+ # previously.
+ try:
+ # See whether a loop was installed already. If so, use that.
+ # That's required for the input hooks to work, they are
+ # installed using `set_event_loop`.
+ loop = asyncio.get_event_loop()
+ except RuntimeError:
+ # No loop installed. Run like usual.
+ return asyncio.run(coro)
+ else:
+ # Use existing loop.
+ return loop.run_until_complete(coro)
+
+ else:
# No loop installed. Run like usual.
return asyncio.run(coro)
- else:
- # Use existing loop.
- return loop.run_until_complete(coro)
def _handle_exception(
self, loop: AbstractEventLoop, context: dict[str, Any]
@@ -999,7 +1030,7 @@ class Application(Generic[_AppResult]):
manager. (We will only install the hook if no other custom hook was
set.)
"""
- if sys.version_info >= (3, 7) and sys.breakpointhook == sys.__breakpointhook__:
+ if sys.breakpointhook == sys.__breakpointhook__:
sys.breakpointhook = self._breakpointhook
try:
diff --git a/contrib/python/prompt-toolkit/py3/prompt_toolkit/application/current.py b/contrib/python/prompt-toolkit/py3/prompt_toolkit/application/current.py
index 33828024f41..908141a4767 100644
--- a/contrib/python/prompt-toolkit/py3/prompt_toolkit/application/current.py
+++ b/contrib/python/prompt-toolkit/py3/prompt_toolkit/application/current.py
@@ -1,6 +1,5 @@
from __future__ import annotations
-import sys
from contextlib import contextmanager
from contextvars import ContextVar
from typing import TYPE_CHECKING, Any, Generator
@@ -145,12 +144,8 @@ def create_app_session(
Create a separate AppSession.
This is useful if there can be multiple individual `AppSession`s going on.
- Like in the case of an Telnet/SSH server. This functionality uses
- contextvars and requires at least Python 3.7.
+ Like in the case of an Telnet/SSH server.
"""
- if sys.version_info <= (3, 6):
- raise RuntimeError("Application sessions require Python 3.7.")
-
# If no input/output is specified, fall back to the current input/output,
# whatever that is.
if input is None:
diff --git a/contrib/python/prompt-toolkit/py3/prompt_toolkit/application/dummy.py b/contrib/python/prompt-toolkit/py3/prompt_toolkit/application/dummy.py
index 4107d057806..43819e1e71c 100644
--- a/contrib/python/prompt-toolkit/py3/prompt_toolkit/application/dummy.py
+++ b/contrib/python/prompt-toolkit/py3/prompt_toolkit/application/dummy.py
@@ -2,6 +2,7 @@ from __future__ import annotations
from typing import Callable
+from prompt_toolkit.eventloop import InputHook
from prompt_toolkit.formatted_text import AnyFormattedText
from prompt_toolkit.input import DummyInput
from prompt_toolkit.output import DummyOutput
@@ -28,6 +29,7 @@ class DummyApplication(Application[None]):
set_exception_handler: bool = True,
handle_sigint: bool = True,
in_thread: bool = False,
+ inputhook: InputHook | None = None,
) -> None:
raise NotImplementedError("A DummyApplication is not supposed to run.")