prompt-toolkit / python-prompt-toolkit

Library for building powerful interactive command line applications in Python
https://python-prompt-toolkit.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
9.1k stars 717 forks source link

Use `get_running_loop` instead of `get_event_loop` #1823

Open auxsvr opened 6 months ago

auxsvr commented 6 months ago

The latter is deprecated and may not always return the running event loop.

Resolves https://github.com/prompt-toolkit/python-prompt-toolkit/issues/1824.

jonathanslenders commented 6 months ago

This is intentional. IPython used to pass the input hook by setting an event loop which we can use in prompt_toolkit.

The if _called_from_ipython(): test that we have there is a workaround to ensure that %gui keeps working in older IPython versions. Merging this would break those IPython versions.

We can remove the elif _called_from_ipython(): block entirely when everyone who's using the newest prompt_toolkit also uses the newest IPython. Or we should check the IPython version in the called_from_ipython() function and return False for the newer IPython versions.

Feel free to create a PR that does that.

auxsvr commented 6 months ago

asyncio.get_event_loop is already deprecated and may not return the running loop, cf. https://github.com/python/cpython/issues/96377#issuecomment-1260006538. According to the discussion there, it should be replaced with asyncio.get_running_loop or the corresponding policy method. If the loop on line 988 is the running loop, this should not break old versions (newer than Python 3.7), if I understand the purpose of this code correctly. Of course, as I am not familiar with the codebase, I could be wrong.

auxsvr commented 6 months ago

If I am wrong, please explain. At the moment, using asyncio.get_running_loop looks like the easiest option to me, preserving backward compatibility.