Skip to content

Commit

Permalink
Fix IPython inputhook (workaround).
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanslenders committed Nov 13, 2023
1 parent 2e2175a commit a264778
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/prompt_toolkit/application/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,31 @@ def run_in_thread() -> None:
set_exception_handler=set_exception_handler,
handle_sigint=handle_sigint,
)
if inputhook is None:

def _called_from_ipython() -> bool:
try:
return "IPython/terminal/interactiveshell.py" in sys._getframe(3).f_code.co_filename
except BaseException:
return False

if _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)

elif inputhook is None:
# No loop installed. Run like usual.
return asyncio.run(coro)
else:
Expand Down

0 comments on commit a264778

Please sign in to comment.