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.28k stars 715 forks source link

AssertionError from assert app._is_running in exception handler #886

Open asmeurer opened 5 years ago

asmeurer commented 5 years ago
Exception in default exception handler
Traceback (most recent call last):
  File "/Users/aaronmeurer/anaconda3/envs/pyflyby3/lib/python3.7/site-packages/prompt_toolkit/eventloop/posix.py", line 154, in _run_task
    t()
  File "/Users/aaronmeurer/anaconda3/envs/pyflyby3/lib/python3.7/site-packages/prompt_toolkit/eventloop/context.py", line 115, in new_func
    return func(*a, **kw)
  File "/Users/aaronmeurer/anaconda3/envs/pyflyby3/lib/python3.7/site-packages/prompt_toolkit/patch_stdout.py", line 102, in write_and_flush_in_loop
    run_in_terminal(write_and_flush, in_executor=False)
  File "/Users/aaronmeurer/anaconda3/envs/pyflyby3/lib/python3.7/site-packages/prompt_toolkit/application/run_in_terminal.py", line 41, in run_in_terminal
    return run_coroutine_in_terminal(async_func, render_cli_done=render_cli_done)
  File "/Users/aaronmeurer/anaconda3/envs/pyflyby3/lib/python3.7/site-packages/prompt_toolkit/application/run_in_terminal.py", line 62, in run_coroutine_in_terminal
    assert app._is_running
AssertionError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/Users/aaronmeurer/anaconda3/envs/pyflyby3/lib/python3.7/site-packages/prompt_toolkit/eventloop/base.py", line 129, in call_exception_handler
    self._exception_handler(context)
  File "/Users/aaronmeurer/anaconda3/envs/pyflyby3/lib/python3.7/site-packages/prompt_toolkit/application/application.py", line 702, in handle_exception
    run_coroutine_in_terminal(print_exception)
  File "/Users/aaronmeurer/anaconda3/envs/pyflyby3/lib/python3.7/site-packages/prompt_toolkit/application/run_in_terminal.py", line 62, in run_coroutine_in_terminal
    assert app._is_running

I am getting this from trying to use pexpect with IPython. If I comment out the assert, it shows WARNING: your terminal doesn't support cursor position requests (CPR).. So I think the assertion is preventing the warning from being displayed correctly. I don't know why the app isn't running, but regardless whether that's a bug on my end or prompt-toolkit's it should not prevent the warning from being displayed.

As a side question, do you know how to properly use prompt-toolkit 2.0 with pexpect? Is there a way to disable the CPR checks entirely? Maybe an environment variable would be useful for this.

jonathanslenders commented 5 years ago

Hi @asmeurer,

This looks like a bug indeed. Are you sure that it happens with the latest prompt_toolkit on the 2.0 branch? I remember doing a fix related to this some time ago.

I don't mind merging a fix that checks an environment variable like PROMPT_TOOLKIT_NO_CPR=1 in prompt_toolkit/input/vt100.py, in the responds_to_cpr method. This could solve it. (Either create PR against master, or against both master and 2.0.)

asmeurer commented 5 years ago

I just checked and it still occurs in the latest 2.0 branch. It's only inside pexpect. I've never seen it in the normal terminal.

asmeurer commented 5 years ago

PR for the environment variable at https://github.com/prompt-toolkit/python-prompt-toolkit/pull/897 and https://github.com/prompt-toolkit/python-prompt-toolkit/pull/898.