Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

editor_buffer.filename is None: when i try to make execution (F9) work with python 2.7 and 3.8 on a Mac #136

Open
rasimuddinvs opened this issue Aug 17, 2020 · 2 comments

Comments

@rasimuddinvs
Copy link

File "/Users/xxxx/Library/Python/2.7/lib/python/site-packages/prompt_toolkit/key_binding/key_processor.py", line 275, in process_keys
self._process_coroutine.send(key_press)
File "/Users/xxxx/Library/Python/2.7/lib/python/site-packages/prompt_toolkit/key_binding/key_processor.py", line 182, in _process
self._call_handler(matches[-1], key_sequence=buffer[:])
File "/Users/xxxx/Library/Python/2.7/lib/python/site-packages/prompt_toolkit/key_binding/key_processor.py", line 325, in _call_handler
handler.call(event)
File "/Users/xxxx/Library/Python/2.7/lib/python/site-packages/prompt_toolkit/key_binding/key_bindings.py", line 79, in call
return self.handler(event)
File "/Users/xxxx/.pyvimrc", line 67, in save_and_execute_python_file
if editor_buffer.filename is None:

@thuibr
Copy link
Contributor

thuibr commented Oct 15, 2021

To fix this, make the following changes:

New import at the top:

from prompt_toolkit.application import run_in_terminal

Changes to the function that F9 binds to:

    @editor.add_key_binding(Keys.F9)
    def save_and_execute_python_file(event):
        """
        F9: Execute the current Python file.
        """
        # Save buffer first.
        editor_buffer = editor.current_editor_buffer

        if editor_buffer is not None:
            if editor_buffer.location is None:
                editor.show_message("File doesn't have a filename. Please save first.")
                return
            else:
                editor_buffer.write()

        # Now run the Python interpreter. But use
        # `CommandLineInterface.run_in_terminal` to go to the background and
        # not destroy the window layout.
        def execute():
            call(['python3', editor_buffer.location])
            six.moves.input('Press enter to continue...')

        run_in_terminal(execute)

@thuibr
Copy link
Contributor

thuibr commented Oct 15, 2021

See #140

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants