Skip to content

Fake EOF? #392

Closed
Closed

Description

@jdemeyer

Is there a way for an application to signal to prompt_toolkit that it should pretend an EOF has happened (typically making the application exit)? Something like this works with readline but not with prompt_toolkit:

In [1]: import os; f = open(os.devnull); os.dup2(f.fileno(), 0)

In [2]:      
Traceback (most recent call last):
  File "/usr/local/src/sage-config/local/bin/ipython", line 5, in <module>
    start_ipython()
  File "/usr/local/src/sage-config/local/lib/python2.7/site-packages/IPython/__init__.py", line 119, in start_ipython
    return launch_new_instance(argv=argv, **kwargs)
  File "/usr/local/src/sage-config/local/lib/python2.7/site-packages/traitlets/config/application.py", line 596, in launch_instance
    app.start()
  File "/usr/local/src/sage-config/local/lib/python2.7/site-packages/IPython/terminal/ipapp.py", line 348, in start
    self.shell.mainloop()
  File "/usr/local/src/sage-config/local/lib/python2.7/site-packages/IPython/terminal/interactiveshell.py", line 402, in mainloop
    self.interact()
  File "/usr/local/src/sage-config/local/lib/python2.7/site-packages/IPython/terminal/interactiveshell.py", line 385, in interact
    code = self.prompt_for_code()
  File "/usr/local/src/sage-config/local/lib/python2.7/site-packages/IPython/terminal/interactiveshell.py", line 321, in prompt_for_code
    pre_run=self.pre_prompt, reset_current_buffer=True)
  File "build/bdist.linux-x86_64/egg/prompt_toolkit/interface.py", line 385, in run
    with self.input.raw_mode():
  File "build/bdist.linux-x86_64/egg/prompt_toolkit/input.py", line 69, in raw_mode
    return raw_mode(self.stdin.fileno())
  File "build/bdist.linux-x86_64/egg/prompt_toolkit/terminal/vt100_input.py", line 409, in __init__
    self.attrs_before = termios.tcgetattr(fileno)
error: (25, 'Inappropriate ioctl for device')

My use case is a signal handler forcing the application to exit gracefully.

Activity

  1. jdemeyer commented on Sep 13, 2016

    @jdemeyer
    Author

    cli.exit() is documented as being equivalent to pressing Ctrl-D.

    But that does not work asynchronously. I want a solution which would work while prompt_toolkit is actually waiting for user input.

  2. jonathanslenders commented on Sep 13, 2016

    @jonathanslenders
    Member

    Hi @jdemeyer,
    Thanks for this request. I made a pull request that will probably solve the issue: #393

    However, I wonder why you would ever dup devnull to fd 0.

  3. jdemeyer commented on Sep 13, 2016

    @jdemeyer
    Author

    However, I wonder why you would ever dup devnull to fd 0.

    Because that did the trick for readline :-)

  4. jonathanslenders commented on May 15, 2018

    @jonathanslenders
    Member

    Closing this now. In prompt_toolkit 2.0, calling get_app().exit() should close the current application. This can be done in any key binding, and works asynchronously. Please open again or create a new issue if this still doesn't work.

  5. techtonik commented on Jul 5, 2018

    @techtonik
    Contributor
    from prompt_toolkit.application.current import get_app
    
    @global_kb.add('f10')  # F10
    def quit(event):
        get_app().exit()
    

    This make prompt return None. Not the same as pressing Ctrl+D which raises EOFError.

  6. jonathanslenders commented on Jul 5, 2018

    @jonathanslenders
    Member
    get_app().exit(exception=EOFError)

    should probably do it.

  7. techtonik commented on Jul 5, 2018

    @techtonik
    Contributor

    It does. Thanks. =)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions