prompt-toolkit / prompt-toolkit/python-prompt-toolkit

input hook is never called

Open
#1,407 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
10.6k
Forks
815
PR merge metrics
No merged PRs in 30d

Description

First of all, a million thanks for prompt-toolkit and ptpython 👍

I am trying to have a custom input hook getting called within a custom ptpython-based shell.
I followed instructions here: https://python-prompt-toolkit.readthedocs.io/en/stable/pages/advanced_topics/input_hooks.html?highlight=input%20hook
But, I must be doing something wrong : the custom hook is never called.

Here is an example to reproduce the problem with latest ptpython (3.0.16) and prompt toolkit (3.0.18):

Python 3.7.10 (default, Feb 26 2021, 18:47:35) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from ptpython.repl import PythonRepl
>>> from prompt_toolkit.eventloop import set_eventloop_with_inputhook
>>> def my_hook(hook_context):
...   breakpoint() #this is just to see if this will be executed
... 
>>> set_eventloop_with_inputhook(my_hook)
<_UnixSelectorEventLoop running=False closed=False debug=False>
>>> pr=PythonRepl()
>>> pr.run()
>>> ... whatever I try, no breakpoint => my_hook is never called ...

Any idea what I am doing wrong ? Or maybe there is a bug here ?

My final goal is to have gevent loop integrated within the ptpython shell. My input hook would execute a gevent select on the file descriptor... (hope this can work).

In the past, I did this with custom event loop (see https://github.com/prompt-toolkit/ptpython/pull/130) but since async io, I feel a bit desperate.

EDIT: gevent integration can be done quite easily in fact, by using aiogevent and calling asyncio.run(pr.run_async()) instead of run().

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the documented input-hook API, especially set_eventloop_with_inputhook, and compare its behavior with PythonRepl.run(). Reproduce the example using PythonRepl and the custom hook, then trace whether the hook is reached during the synchronous run path. Done means the documented hook is called as expected, or the limitation is clearly documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.