Beispiel #1
0
    def run(self) -> None:
        """
        Run the REPL loop.
        """
        if self.terminal_title:
            set_title(self.terminal_title)

        self._add_to_namespace()

        try:
            while True:
                # Pull text from the user.
                try:
                    text = self.read()
                except EOFError:
                    return
                except BaseException as e:
                    # Something went wrong while reading input.
                    # (E.g., a bug in the completer that propagates. Don't
                    # crash the REPL.)
                    traceback.print_exc()
                    continue

                # Run it; display the result (or errors if applicable).
                self.run_and_show_expression(text)
        finally:
            if self.terminal_title:
                clear_title()
            self._remove_from_namespace()
Beispiel #2
0
    async def run_async(self) -> None:
        if self.terminal_title:
            set_title(self.terminal_title)

        while True:
            # Capture the current input_mode in order to restore it after reset,
            # for ViState.reset() sets it to InputMode.INSERT unconditionally and
            # doesn't accept any arguments.
            def pre_run(
                last_input_mode: InputMode = self.app.vi_state.input_mode,
            ) -> None:
                if self.vi_keep_last_used_mode:
                    self.app.vi_state.input_mode = last_input_mode

                if not self.vi_keep_last_used_mode and self.vi_start_in_navigation_mode:
                    self.app.vi_state.input_mode = InputMode.NAVIGATION

            # Run the UI.
            try:
                text = await self.app.run_async(pre_run=pre_run)
            except EOFError:
                return
            except KeyboardInterrupt:
                # Abort - try again.
                self.default_buffer.document = Document()
            else:
                self._process_text(text)

        if self.terminal_title:
            clear_title()
Beispiel #3
0
def cli():
    app = Application(full_screen=True,
                      layout=LAYOUT,
                      style=patched_style(),
                      color_depth=ColorDepth.TRUE_COLOR,
                      key_bindings=KG)
    app.editing_mode = EditingMode.VI
    app.run()
    shortcuts.clear_title()
Beispiel #4
0
    async def run_async(self) -> None:
        """
        Run the REPL loop, but run the blocking parts in an executor, so that
        we don't block the event loop. Both the input and output (which can
        display a pager) will run in a separate thread with their own event
        loop, this way ptpython's own event loop won't interfere with the
        asyncio event loop from where this is called.

        The "eval" however happens in the current thread, which is important.
        (Both for control-C to work, as well as for the code to see the right
        thread in which it was embedded).
        """
        loop = asyncio.get_event_loop()

        if self.terminal_title:
            set_title(self.terminal_title)

        self._add_to_namespace()

        try:
            while True:
                try:
                    # Read.
                    try:
                        text = await loop.run_in_executor(None, self.read)
                    except EOFError:
                        return

                    # Eval.
                    try:
                        result = await self.eval_async(text)
                    except KeyboardInterrupt as e:  # KeyboardInterrupt doesn't inherit from Exception.
                        raise
                    except SystemExit:
                        return
                    except BaseException as e:
                        self._handle_exception(e)
                    else:
                        # Print.
                        if result is not None:
                            await loop.run_in_executor(
                                None, lambda: self.show_result(result))

                        # Loop.
                        self.current_statement_index += 1
                        self.signatures = []

                except KeyboardInterrupt as e:
                    # XXX: This does not yet work properly. In some situations,
                    # `KeyboardInterrupt` exceptions can end up in the event
                    # loop selector.
                    self._handle_keyboard_interrupt(e)
        finally:
            if self.terminal_title:
                clear_title()
            self._remove_from_namespace()
Beispiel #5
0
    def run(self) -> None:
        """
        Run the REPL loop.
        """
        if self.terminal_title:
            set_title(self.terminal_title)

        self._add_to_namespace()

        try:
            while True:
                try:
                    # Read.
                    try:
                        text = self.read()
                    except EOFError:
                        return

                    # Eval.
                    try:
                        result = self.eval(text)
                    except KeyboardInterrupt as e:  # KeyboardInterrupt doesn't inherit from Exception.
                        raise
                    except SystemExit:
                        return
                    except BaseException as e:
                        self._handle_exception(e)
                    else:
                        # Print.
                        if result is not None:
                            self.show_result(result)

                        # Loop.
                        self.current_statement_index += 1
                        self.signatures = []

                except KeyboardInterrupt as e:
                    # Handle all possible `KeyboardInterrupt` errors. This can
                    # happen during the `eval`, but also during the
                    # `show_result` if something takes too long.
                    # (Try/catch is around the whole block, because we want to
                    # prevent that a Control-C keypress terminates the REPL in
                    # any case.)
                    self._handle_keyboard_interrupt(e)
        finally:
            if self.terminal_title:
                clear_title()
            self._remove_from_namespace()
Beispiel #6
0
    async def run_async(self) -> None:
        """
        Run the REPL loop, but run the blocking parts in an executor, so that
        we don't block the event loop. Both the input and output (which can
        display a pager) will run in a separate thread with their own event
        loop, this way ptpython's own event loop won't interfere with the
        asyncio event loop from where this is called.

        The "eval" however happens in the current thread, which is important.
        (Both for control-C to work, as well as for the code to see the right
        thread in which it was embedded).
        """
        loop = asyncio.get_event_loop()

        if self.terminal_title:
            set_title(self.terminal_title)

        self._add_to_namespace()

        try:
            while True:
                try:
                    # Read.
                    try:
                        text = await loop.run_in_executor(None, self.read)
                    except EOFError:
                        return
                    except BaseException:
                        # Something went wrong while reading input.
                        # (E.g., a bug in the completer that propagates. Don't
                        # crash the REPL.)
                        traceback.print_exc()
                        continue

                    # Eval.
                    await self.run_and_show_expression_async(text)

                except KeyboardInterrupt as e:
                    # XXX: This does not yet work properly. In some situations,
                    # `KeyboardInterrupt` exceptions can end up in the event
                    # loop selector.
                    self._handle_keyboard_interrupt(e)
        finally:
            if self.terminal_title:
                clear_title()
            self._remove_from_namespace()
Beispiel #7
0
    async def run_async(self) -> None:
        if self.terminal_title:
            set_title(self.terminal_title)

        while True:
            # Run the UI.
            try:
                text = await self.app.run_async()
            except EOFError:
                return
            except KeyboardInterrupt:
                # Abort - try again.
                self.default_buffer.document = Document()
            else:
                self._process_text(text)

        if self.terminal_title:
            clear_title()
Beispiel #8
0
    def run(self):
        if self.terminal_title:
            set_title(self.terminal_title)

        while True:
            # Run the UI.
            try:
                text = self.app.run(inputhook=inputhook)
            except EOFError:
                return
            except KeyboardInterrupt:
                # Abort - try again.
                self.default_buffer.document = Document()
            else:
                self._process_text(text)

        if self.terminal_title:
            clear_title()
Beispiel #9
0
    def run(self) -> None:
        """
        Run the REPL loop.
        """
        if self.terminal_title:
            set_title(self.terminal_title)

        self._add_to_namespace()

        try:
            while True:
                # Read.
                try:
                    text = self.read()
                except EOFError:
                    return

                # Eval.
                try:
                    result = self.eval(text)
                except KeyboardInterrupt as e:  # KeyboardInterrupt doesn't inherit from Exception.
                    self._handle_keyboard_interrupt(e)
                except SystemExit:
                    return
                except BaseException as e:
                    self._handle_exception(e)
                else:
                    # Print.
                    if result is not None:
                        self.show_result(result)

                    # Loop.
                    self.current_statement_index += 1
                    self.signatures = []

        finally:
            if self.terminal_title:
                clear_title()
            self._remove_from_namespace()
Beispiel #10
0
    def run(self) -> None:
        if self.terminal_title:
            set_title(self.terminal_title)

        def prompt() -> str:
            # In order to make sure that asyncio code written in the
            # interactive shell doesn't interfere with the prompt, we run the
            # prompt in a different event loop.
            # If we don't do this, people could spawn coroutine with a
            # while/true inside which will freeze the prompt.

            try:
                old_loop: Optional[
                    asyncio.AbstractEventLoop] = asyncio.get_event_loop()
            except RuntimeError:
                # This happens when the user used `asyncio.run()`.
                old_loop = None

            asyncio.set_event_loop(self.pt_loop)
            try:
                return self.app.run()  # inputhook=inputhook)
            finally:
                # Restore the original event loop.
                asyncio.set_event_loop(old_loop)

        while True:
            # Run the UI.
            try:
                text = prompt()
            except EOFError:
                return
            except KeyboardInterrupt:
                # Abort - try again.
                self.default_buffer.document = Document()
            else:
                self._process_text(text)

        if self.terminal_title:
            clear_title()
Beispiel #11
0
    def run(self) -> None:
        """
        Run the REPL loop.
        """
        if self.terminal_title:
            set_title(self.terminal_title)

        self._add_to_namespace()

        try:
            while True:
                # Pull text from the user.
                try:
                    text = self.read()
                except EOFError:
                    return

                # Run it; display the result (or errors if applicable).
                self.run_and_show_expression(text)
        finally:
            if self.terminal_title:
                clear_title()
            self._remove_from_namespace()
Beispiel #12
0
def _(_):
    shortcuts.clear_title()
    get_app().exit(result=False)