From b3a77c732dba926300ef59498361fa2ad0598427 Mon Sep 17 00:00:00 2001 From: NightMachinary <36224762+NightMachinary@users.noreply.github.com> Date: Thu, 15 Aug 2019 21:06:54 +0430 Subject: [PATCH 001/220] Fixed a typo --- README.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rst b/README.rst index b953b02d..e65c98ec 100644 --- a/README.rst +++ b/README.rst @@ -121,7 +121,7 @@ Running system commands: Press ``Meta-!`` in Emacs mode or just ``!`` in Vi navigation mode to see the "Shell command" prompt. There you can enter system commands without leaving the REPL. -Selecting text: Press ``Control+Space`` in Emacs mode on ``V`` (major V) in Vi +Selecting text: Press ``Control+Space`` in Emacs mode or ``V`` (major V) in Vi navigation mode. From 0524be1610c383df5f25f1b28097e1722022cf8f Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Mon, 30 Sep 2019 23:10:51 +0100 Subject: [PATCH 002/220] enable universal wheels --- setup.cfg | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 setup.cfg diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 00000000..3c6e79cf --- /dev/null +++ b/setup.cfg @@ -0,0 +1,2 @@ +[bdist_wheel] +universal=1 From 5eb7158114a80efcb50e74ece16572a9b3337f0d Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Mon, 30 Sep 2019 23:11:07 +0100 Subject: [PATCH 003/220] add python3 trove classifier for caniusepython3 --- setup.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/setup.py b/setup.py index 01868e8d..be25c433 100644 --- a/setup.py +++ b/setup.py @@ -21,6 +21,11 @@ 'prompt_toolkit>=2.0.6,<2.1.0', 'pygments', ], + classifiers=[ + 'Programming Language :: Python', + 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 2', + ], entry_points={ 'console_scripts': [ 'ptpython = ptpython.entry_points.run_ptpython:run', From 284b38ff7d767f1bf4772765bb0cc364337398d9 Mon Sep 17 00:00:00 2001 From: "Derek A. Thomas" Date: Wed, 13 Feb 2019 11:08:42 -0800 Subject: [PATCH 004/220] change PythonRepl._process_text to use input text instead of ignoring it --- ptpython/repl.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ptpython/repl.py b/ptpython/repl.py index 9ca8ffaf..72487ad7 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -76,8 +76,7 @@ def run(self): if self.terminal_title: clear_title() - def _process_text(self, text): - line = self.default_buffer.text + def _process_text(self, line): if line and not line.isspace(): try: From 19416eef9d55fd457f9a47a170f2222885b21236 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Tue, 8 Oct 2019 22:53:13 +0200 Subject: [PATCH 005/220] Improved autocompletion: - Added fuzzy completion. - Added dictionary key completion (for keys which are strings). - Highlighting of Python keywords in completion drop down. --- examples/ptpython_config/config.py | 4 + ptpython/completer.py | 125 ++++++++++++++++++++++++++++- ptpython/python_input.py | 25 +++++- ptpython/style.py | 7 ++ setup.py | 2 +- 5 files changed, 157 insertions(+), 6 deletions(-) diff --git a/examples/ptpython_config/config.py b/examples/ptpython_config/config.py index a834112c..28e7c0bf 100644 --- a/examples/ptpython_config/config.py +++ b/examples/ptpython_config/config.py @@ -61,6 +61,10 @@ def configure(repl): # completion menu is shown.) repl.complete_while_typing = True + # Fuzzy and dictionary completion. + self.enable_fuzzy_completion = False + self.enable_dictionary_completion = False + # Vi mode. repl.vi_mode = False diff --git a/ptpython/completer.py b/ptpython/completer.py index 7a63912a..8fa0e314 100644 --- a/ptpython/completer.py +++ b/ptpython/completer.py @@ -6,7 +6,10 @@ from ptpython.utils import get_jedi_script_from_document +import keyword +import ast import re +import six __all__ = ( 'PythonCompleter', @@ -17,11 +20,14 @@ class PythonCompleter(Completer): """ Completer for Python code. """ - def __init__(self, get_globals, get_locals): + def __init__(self, get_globals, get_locals, get_enable_dictionary_completion): super(PythonCompleter, self).__init__() self.get_globals = get_globals self.get_locals = get_locals + self.get_enable_dictionary_completion = get_enable_dictionary_completion + + self.dictionary_completer = DictionaryCompleter(get_globals, get_locals) self._path_completer_cache = None self._path_completer_grammar_cache = None @@ -108,7 +114,16 @@ def get_completions(self, document, complete_event): """ Get Python completions. """ - # Do Path completions + # Do dictionary key completions. + if self.get_enable_dictionary_completion(): + has_dict_completions = False + for c in self.dictionary_completer.get_completions(document, complete_event): + has_dict_completions = True + yield c + if has_dict_completions: + return + + # Do Path completions (if there were no dictionary completions). if complete_event.completion_requested or self._complete_path_while_typing(document): for c in self._path_completer.get_completions(document, complete_event): yield c @@ -162,5 +177,107 @@ def get_completions(self, document, complete_event): pass else: for c in completions: - yield Completion(c.name_with_symbols, len(c.complete) - len(c.name_with_symbols), - display=c.name_with_symbols) + yield Completion( + c.name_with_symbols, len(c.complete) - len(c.name_with_symbols), + display=c.name_with_symbols, + style=_get_style_for_name(c.name_with_symbols)) + + +class DictionaryCompleter(Completer): + """ + Experimental completer for Python dictionary keys. + + Warning: This does an `eval` on the Python object before the open square + bracket, which is potentially dangerous. It doesn't match on + function calls, so it only triggers attribute access. + """ + def __init__(self, get_globals, get_locals): + super(DictionaryCompleter, self).__init__() + + self.get_globals = get_globals + self.get_locals = get_locals + + self.pattern = re.compile( + r''' + # Any expression safe enough to eval while typing. + # No operators, except dot, and only other dict lookups. + # Technically, this can be unsafe of course, if bad code runs + # in `__getattr__` or ``__getitem__``. + ( + # Variable name + [a-zA-Z0-9_]+ + + \s* + + (?: + # Attribute access. + \s* \. \s* [a-zA-Z0-9_]+ \s* + + | + + # Item lookup. + # (We match the square brackets. We don't care about + # matching quotes here in the regex. Nested square + # brackets are not supported.) + \s* \[ [a-zA-Z0-9_'"\s]+ \] \s* + )* + ) + + # Dict loopup to complete (square bracket open + start of + # string). + \[ + \s* ([a-zA-Z0-9_'"]*)$ + ''', + re.VERBOSE + ) + + def get_completions(self, document, complete_event): + match = self.pattern.search(document.text_before_cursor) + if match is not None: + object_var, key = match.groups() + object_var = object_var.strip() + + # Do lookup of `object_var` in the context. + try: + result = eval(object_var, self.get_globals(), self.get_locals()) + except BaseException as e: + return # Many exception, like NameError can be thrown here. + + # If this object is a dictionary, complete the keys. + if isinstance(result, dict): + # Try to evaluate the key. + key_obj = key + for k in [key, key + '"', key + "'"]: + try: + key_obj = ast.literal_eval(k) + except (SyntaxError, ValueError): + continue + else: + break + + for k in result: + if six.text_type(k).startswith(key_obj): + yield Completion( + six.text_type(repr(k)), + - len(key), + display=six.text_type(repr(k)) + ) + +try: + import builtins + _builtin_names = dir(builtins) +except ImportError: # Python 2. + _builtin_names = [] + + +def _get_style_for_name(name): + """ + Return completion style to use for this name. + """ + if name in _builtin_names: + return 'class:completion.builtin' + + if keyword.iskeyword(name): + return 'class:completion.keyword' + + return '' diff --git a/ptpython/python_input.py b/ptpython/python_input.py index 6de5180a..2101657e 100644 --- a/ptpython/python_input.py +++ b/ptpython/python_input.py @@ -25,6 +25,7 @@ from prompt_toolkit.styles import DynamicStyle, SwapLightAndDarkStyleTransformation, ConditionalStyleTransformation, AdjustBrightnessStyleTransformation, merge_style_transformations from prompt_toolkit.utils import is_windows from prompt_toolkit.validation import ConditionalValidator +from prompt_toolkit.completion import FuzzyCompleter from .completer import PythonCompleter from .history_browser import History @@ -151,7 +152,10 @@ def __init__(self, self.get_globals = get_globals or (lambda: {}) self.get_locals = get_locals or self.get_globals - self._completer = _completer or PythonCompleter(self.get_globals, self.get_locals) + self._completer = _completer or FuzzyCompleter( + PythonCompleter(self.get_globals, self.get_locals, + lambda: self.enable_dictionary_completion), + enable_fuzzy=Condition(lambda: self.enable_fuzzy_completion)) self._validator = _validator or PythonValidator(self.get_compiler_flags) self._lexer = _lexer or PygmentsLexer(PythonLexer) @@ -193,6 +197,8 @@ def __init__(self, # with the current input. self.enable_syntax_highlighting = True + self.enable_fuzzy_completion = False + self.enable_dictionary_completion = False self.swap_light_and_dark = False self.highlight_matching_parenthesis = False self.show_sidebar = False # Currently show the sidebar. @@ -433,6 +439,23 @@ def get_values(): 'on': lambda: enable('complete_while_typing') and disable('enable_history_search'), 'off': lambda: disable('complete_while_typing'), }), + Option(title='Enable fuzzy completion', + description="Enable fuzzy completion.", + get_current_value=lambda: ['off', 'on'][self.enable_fuzzy_completion], + get_values=lambda: { + 'on': lambda: enable('enable_fuzzy_completion'), + 'off': lambda: disable('enable_fuzzy_completion'), + }), + Option(title='Dictionary completion', + description='Enable experimental dictionary completion.\n' + 'WARNING: this does "eval" on fragments of\n' + ' your Python input and is\n' + ' potentially unsafe.', + get_current_value=lambda: ['off', 'on'][self.enable_dictionary_completion], + get_values=lambda: { + 'on': lambda: enable('enable_dictionary_completion'), + 'off': lambda: disable('enable_dictionary_completion'), + }), Option(title='History search', description='When pressing the up-arrow, filter the history on input starting ' 'with the current text. (Not compatible with "Complete while typing".)', diff --git a/ptpython/style.py b/ptpython/style.py index 15c5b2ad..7a2cd2a1 100644 --- a/ptpython/style.py +++ b/ptpython/style.py @@ -83,6 +83,13 @@ def generate_style(python_style, ui_style): 'out': '#ff0000', 'out.number': '#ff0000', + # Completions. + 'completion.builtin': '', + 'completion.keyword': 'fg:#008800', + + 'completion.keyword fuzzymatch.inside': 'fg:#008800', + 'completion.keyword fuzzymatch.outside': 'fg:#44aa44', + # Separator between windows. (Used above docstring.) 'separator': '#bbbbbb', diff --git a/setup.py b/setup.py index be25c433..da16fe41 100644 --- a/setup.py +++ b/setup.py @@ -18,7 +18,7 @@ install_requires = [ 'docopt', 'jedi>=0.9.0', - 'prompt_toolkit>=2.0.6,<2.1.0', + 'prompt_toolkit>=2.0.8,<2.1.0', 'pygments', ], classifiers=[ From 6edce3c3d31a5c3f5c071e1a88d62d51a11c42d1 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Wed, 9 Oct 2019 17:05:57 +0100 Subject: [PATCH 006/220] Release 2.0.5 --- CHANGELOG | 16 ++++++++++++++++ setup.py | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index d8fcd0aa..c64a87d1 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,22 @@ CHANGELOG ========= + +2.0.5: 2019-10-09 +----------------- + +New features: +- Added dictionary completer (off by default). +- Added fuzzy completion (off by default). +- Highlight keywords in completion dropdown menu. +- Enable universal wheels. + +Fixes: +- Fixed embedding repl as asyncio coroutine. +- Fixed patching stdout in embedded repl. +- Fixed ResourceWarning in setup.py. + + 2.0.4: 2018-10-30 ----------------- diff --git a/setup.py b/setup.py index da16fe41..cac87b50 100644 --- a/setup.py +++ b/setup.py @@ -10,7 +10,7 @@ setup( name='ptpython', author='Jonathan Slenders', - version='2.0.4', + version='2.0.5', url='https://github.com/jonathanslenders/ptpython', description='Python REPL build on top of prompt_toolkit', long_description=long_description, From 74f7623fb2a93c06bf605dd77fd1c104cac1202e Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Thu, 10 Oct 2019 16:29:26 +0200 Subject: [PATCH 007/220] Fix 'get_enable_dictionary_completion' argument in ptipython. --- ptpython/ipython.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/ptpython/ipython.py b/ptpython/ipython.py index be4bd178..8cc5a36e 100644 --- a/ptpython/ipython.py +++ b/ptpython/ipython.py @@ -90,11 +90,12 @@ def create_ipython_grammar(): """) -def create_completer(get_globals, get_locals, magics_manager, alias_manager): +def create_completer(get_globals, get_locals, magics_manager, alias_manager, + get_enable_dictionary_completion): g = create_ipython_grammar() return GrammarCompleter(g, { - 'python': PythonCompleter(get_globals, get_locals), + 'python': PythonCompleter(get_globals, get_locals, get_enable_dictionary_completion), 'magic': MagicsCompleter(magics_manager), 'alias_name': AliasCompleter(alias_manager), 'pdb_arg': WordCompleter(['on', 'off'], ignore_case=True), @@ -154,7 +155,8 @@ class IPythonInput(PythonInput): def __init__(self, ipython_shell, *a, **kw): kw['_completer'] = create_completer(kw['get_globals'], kw['get_globals'], ipython_shell.magics_manager, - ipython_shell.alias_manager) + ipython_shell.alias_manager, + lambda: self.enable_dictionary_completion) kw['_lexer'] = create_lexer() kw['_validator'] = IPythonValidator( get_compiler_flags=self.get_compiler_flags) From acc75aa5e8e4698ac86ecbee4f35c747897e634e Mon Sep 17 00:00:00 2001 From: Tim Savage Date: Wed, 16 Oct 2019 12:48:29 +1100 Subject: [PATCH 008/220] Fixed variable from self to repl The config entries for "Fuzzy and dictionary completion" was using self instead of repl. --- examples/ptpython_config/config.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/ptpython_config/config.py b/examples/ptpython_config/config.py index 28e7c0bf..6fb0c54f 100644 --- a/examples/ptpython_config/config.py +++ b/examples/ptpython_config/config.py @@ -62,8 +62,8 @@ def configure(repl): repl.complete_while_typing = True # Fuzzy and dictionary completion. - self.enable_fuzzy_completion = False - self.enable_dictionary_completion = False + repl.enable_fuzzy_completion = False + repl.enable_dictionary_completion = False # Vi mode. repl.vi_mode = False From c69f0486ea606c7cb3d5009f68cb73022c6355d2 Mon Sep 17 00:00:00 2001 From: Tim Savage Date: Wed, 16 Oct 2019 12:51:12 +1100 Subject: [PATCH 009/220] Added help for windows users to find config location --- examples/ptpython_config/config.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/ptpython_config/config.py b/examples/ptpython_config/config.py index 6fb0c54f..bd18a563 100644 --- a/examples/ptpython_config/config.py +++ b/examples/ptpython_config/config.py @@ -1,7 +1,9 @@ """ Configuration example for ``ptpython``. -Copy this file to ~/.ptpython/config.py +Copy this file to ~/.ptpython/config.py on windows use +`os.path.expanduser("~/.ptpython/config.py")` to find the +correct location. """ from __future__ import unicode_literals from prompt_toolkit.filters import ViInsertMode From 424aa34645cf69b1adb39d633a569abfe2e7593c Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Mon, 28 Oct 2019 17:53:45 +0100 Subject: [PATCH 010/220] Fixed exec call, for when a filename was passed to ptpython. Pass the namespace explicitly. --- ptpython/entry_points/run_ptpython.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ptpython/entry_points/run_ptpython.py b/ptpython/entry_points/run_ptpython.py index 356d6bd3..b7701b99 100644 --- a/ptpython/entry_points/run_ptpython.py +++ b/ptpython/entry_points/run_ptpython.py @@ -55,7 +55,9 @@ def run(): path = a[''][0] with open(path, 'rb') as f: code = compile(f.read(), path, 'exec') - six.exec_(code) + # NOTE: We have to pass an empty dictionary as namespace. Omitting + # this argument causes imports to not be found. See issue #326. + six.exec_(code, {}) # Run interactive shell. else: From 90c1dad98465d2da013a9ea5e34a29b95af8c687 Mon Sep 17 00:00:00 2001 From: Carl George Date: Sun, 19 Mar 2017 21:59:52 -0500 Subject: [PATCH 011/220] implement XDG Base Directory specification * Use the appdirs module. * Print a warning if the legacy ~/.ptpython directory is detected. * Resolves #63. * Resolves #132. --- README.rst | 2 +- examples/ptpython_config/config.py | 4 +--- ptpython/entry_points/run_ptipython.py | 27 +++++++++++++++++++------- ptpython/entry_points/run_ptpython.py | 27 +++++++++++++++++++------- ptpython/python_input.py | 2 +- ptpython/repl.py | 2 +- setup.py | 1 + 7 files changed, 45 insertions(+), 20 deletions(-) diff --git a/README.rst b/README.rst index e65c98ec..f394054d 100644 --- a/README.rst +++ b/README.rst @@ -128,7 +128,7 @@ navigation mode. Configuration ************* -It is possible to create a ``~/.ptpython/config.py`` file to customize the configuration. +It is possible to create a ``$XDG_CONFIG_HOME/ptpython/config.py`` file to customize the configuration. Have a look at this example to see what is possible: `config.py `_ diff --git a/examples/ptpython_config/config.py b/examples/ptpython_config/config.py index bd18a563..0296727a 100644 --- a/examples/ptpython_config/config.py +++ b/examples/ptpython_config/config.py @@ -1,9 +1,7 @@ """ Configuration example for ``ptpython``. -Copy this file to ~/.ptpython/config.py on windows use -`os.path.expanduser("~/.ptpython/config.py")` to find the -correct location. +Copy this file to $XDG_CONFIG_HOME/ptpython/config.py """ from __future__ import unicode_literals from prompt_toolkit.filters import ViInsertMode diff --git a/ptpython/entry_points/run_ptipython.py b/ptpython/entry_points/run_ptipython.py index a563f52e..a541ddc5 100644 --- a/ptpython/entry_points/run_ptipython.py +++ b/ptpython/entry_points/run_ptipython.py @@ -9,11 +9,12 @@ Options: --vi : Use Vi keybindings instead of Emacs bindings. - --config-dir= : Pass config directory. By default '~/.ptpython/'. + --config-dir= : Pass config directory. By default '$XDG_CONFIG_HOME/ptpython'. -i, --interactive= : Start interactive shell after executing this file. """ -from __future__ import absolute_import, unicode_literals +from __future__ import absolute_import, unicode_literals, print_function +import appdirs import docopt import os import six @@ -24,11 +25,23 @@ def run(user_ns=None): a = docopt.docopt(__doc__) vi_mode = bool(a['--vi']) - config_dir = os.path.expanduser(a['--config-dir'] or '~/.ptpython/') - # Create config directory. - if not os.path.isdir(config_dir): - os.mkdir(config_dir) + config_dir = appdirs.user_config_dir('ptpython', 'Jonathan Slenders') + data_dir = appdirs.user_data_dir('ptpython', 'Jonathan Slenders') + + if a['--config-dir']: + # Override config_dir. + config_dir = os.path.expanduser(a['--config-dir']) + else: + # Warn about the legacy directory. + legacy_dir = os.path.expanduser('~/.ptpython') + if os.path.isdir(legacy_dir): + print('{0} is deprecated, migrate your configuration to {1}'.format(legacy_dir, config_dir)) + + # Create directories. + for d in (config_dir, data_dir): + if not os.path.isdir(d) and not os.path.islink(d): + os.mkdir(d) # If IPython is not available, show message and exit here with error status # code. @@ -89,7 +102,7 @@ def configure(repl): # Run interactive shell. embed(vi_mode=vi_mode, - history_filename=os.path.join(config_dir, 'history'), + history_filename=os.path.join(data_dir, 'history'), configure=configure, user_ns=user_ns, title='IPython REPL (ptipython)') diff --git a/ptpython/entry_points/run_ptpython.py b/ptpython/entry_points/run_ptpython.py index b7701b99..e092b24e 100644 --- a/ptpython/entry_points/run_ptpython.py +++ b/ptpython/entry_points/run_ptpython.py @@ -9,14 +9,15 @@ Options: --vi : Use Vi keybindings instead of Emacs bindings. - --config-dir= : Pass config directory. By default '~/.ptpython/'. + --config-dir= : Pass config directory. By default '$XDG_CONFIG_HOME/ptpython'. -i, --interactive= : Start interactive shell after executing this file. Other environment variables: PYTHONSTARTUP: file executed on interactive startup (no default) """ -from __future__ import absolute_import, unicode_literals +from __future__ import absolute_import, unicode_literals, print_function +import appdirs import docopt import os import six @@ -29,11 +30,23 @@ def run(): a = docopt.docopt(__doc__) vi_mode = bool(a['--vi']) - config_dir = os.path.expanduser(a['--config-dir'] or '~/.ptpython/') - # Create config directory. - if not os.path.isdir(config_dir): - os.mkdir(config_dir) + config_dir = appdirs.user_config_dir('ptpython', 'Jonathan Slenders') + data_dir = appdirs.user_data_dir('ptpython', 'Jonathan Slenders') + + if a['--config-dir']: + # Override config_dir. + config_dir = os.path.expanduser(a['--config-dir']) + else: + # Warn about the legacy directory. + legacy_dir = os.path.expanduser('~/.ptpython') + if os.path.isdir(legacy_dir): + print('{0} is deprecated, migrate your configuration to {1}'.format(legacy_dir, config_dir)) + + # Create directories. + for d in (config_dir, data_dir): + if not os.path.isdir(d) and not os.path.islink(d): + os.mkdir(d) # Startup path startup_paths = [] @@ -71,7 +84,7 @@ def configure(repl): import __main__ embed(vi_mode=vi_mode, - history_filename=os.path.join(config_dir, 'history'), + history_filename=os.path.join(data_dir, 'history'), configure=configure, locals=__main__.__dict__, globals=__main__.__dict__, diff --git a/ptpython/python_input.py b/ptpython/python_input.py index 2101657e..2c855ba9 100644 --- a/ptpython/python_input.py +++ b/ptpython/python_input.py @@ -320,7 +320,7 @@ def get_compiler_flags(self): def add_key_binding(self): """ Shortcut for adding new key bindings. - (Mostly useful for a .ptpython/config.py file, that receives + (Mostly useful for a config.py file, that receives a PythonInput/Repl instance as input.) :: diff --git a/ptpython/repl.py b/ptpython/repl.py index 72487ad7..83cecce1 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -240,7 +240,7 @@ def enable_deprecation_warnings(): module='__main__') -def run_config(repl, config_file='~/.ptpython/config.py'): +def run_config(repl, config_file): """ Execute REPL config file. diff --git a/setup.py b/setup.py index cac87b50..e884f3c4 100644 --- a/setup.py +++ b/setup.py @@ -16,6 +16,7 @@ long_description=long_description, packages=find_packages('.'), install_requires = [ + 'appdirs', 'docopt', 'jedi>=0.9.0', 'prompt_toolkit>=2.0.8,<2.1.0', From b8a7abc402b0854bb9b8e53d8a33bdedbbba5db4 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Tue, 26 Nov 2019 17:32:46 +0100 Subject: [PATCH 012/220] Use prompt_toolkit instead of 'Jonathan Slenders' as appauthor. --- ptpython/entry_points/run_ptipython.py | 4 ++-- ptpython/entry_points/run_ptpython.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ptpython/entry_points/run_ptipython.py b/ptpython/entry_points/run_ptipython.py index a541ddc5..67239ce7 100644 --- a/ptpython/entry_points/run_ptipython.py +++ b/ptpython/entry_points/run_ptipython.py @@ -26,8 +26,8 @@ def run(user_ns=None): vi_mode = bool(a['--vi']) - config_dir = appdirs.user_config_dir('ptpython', 'Jonathan Slenders') - data_dir = appdirs.user_data_dir('ptpython', 'Jonathan Slenders') + config_dir = appdirs.user_config_dir('ptpython', 'prompt_toolkit') + data_dir = appdirs.user_data_dir('ptpython', 'prompt_toolkit') if a['--config-dir']: # Override config_dir. diff --git a/ptpython/entry_points/run_ptpython.py b/ptpython/entry_points/run_ptpython.py index e092b24e..ef9b44a8 100644 --- a/ptpython/entry_points/run_ptpython.py +++ b/ptpython/entry_points/run_ptpython.py @@ -31,8 +31,8 @@ def run(): vi_mode = bool(a['--vi']) - config_dir = appdirs.user_config_dir('ptpython', 'Jonathan Slenders') - data_dir = appdirs.user_data_dir('ptpython', 'Jonathan Slenders') + config_dir = appdirs.user_config_dir('ptpython', 'prompt_toolkit') + data_dir = appdirs.user_data_dir('ptpython', 'prompt_toolkit') if a['--config-dir']: # Override config_dir. From 54709f3b450be11c3078125ad71c6c7368bdc8b5 Mon Sep 17 00:00:00 2001 From: Nasy Date: Tue, 12 Nov 2019 23:39:16 -0500 Subject: [PATCH 013/220] Add Swap light/dark colors to config example --- examples/ptpython_config/config.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/examples/ptpython_config/config.py b/examples/ptpython_config/config.py index 0296727a..c79b01e0 100644 --- a/examples/ptpython_config/config.py +++ b/examples/ptpython_config/config.py @@ -48,6 +48,9 @@ def configure(repl): # When the sidebar is visible, also show the help text. repl.show_sidebar_help = True + # Swap light/dark colors on or off + repl.swap_light_and_dark = False + # Highlight matching parethesis. repl.highlight_matching_parenthesis = True From 392b08b91397cae228ad0fa85bab2068a27b697a Mon Sep 17 00:00:00 2001 From: Tim Gates Date: Mon, 30 Dec 2019 13:44:40 +1100 Subject: [PATCH 014/220] Fix simple typo: registeres -> registers Closes #331 --- ptpython/eventloop.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ptpython/eventloop.py b/ptpython/eventloop.py index 43fe0549..9d16a2df 100644 --- a/ptpython/eventloop.py +++ b/ptpython/eventloop.py @@ -3,7 +3,7 @@ events when it's loaded and while we are waiting for input at the REPL. This way we don't block the UI of for instance ``turtle`` and other Tk libraries. -(Normally Tkinter registeres it's callbacks in ``PyOS_InputHook`` to integrate +(Normally Tkinter registers it's callbacks in ``PyOS_InputHook`` to integrate in readline. ``prompt-toolkit`` doesn't understand that input hook, but this will fix it for Tk.) """ From ac419e1a8662390ddc7383ade36643da5e0c4986 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Tue, 18 Jun 2019 18:47:25 +0200 Subject: [PATCH 015/220] Upgrade to prompt_toolkit 3.0. - Drop Python <3.5 support and prompt_toolkit 2. - Code formatting with black. - Sorted imports with isort. - Added type annotations. - Separate event loop for reading user input. --- .travis.yml | 15 +- examples/asyncio-python-embed.py | 15 +- examples/asyncio-ssh-python-embed.py | 16 +- examples/ptpython_config/config.py | 25 +- examples/python-embed-with-custom-prompt.py | 27 +- examples/python-embed.py | 2 +- examples/python-input.py | 4 +- ptpython/__main__.py | 1 - ptpython/completer.py | 142 ++-- ptpython/contrib/asyncssh_repl.py | 84 +- ptpython/entry_points/run_ptipython.py | 96 +-- ptpython/entry_points/run_ptpython.py | 173 ++-- ptpython/eventloop.py | 16 +- ptpython/filters.py | 24 +- ptpython/history_browser.py | 354 ++++---- ptpython/ipython.py | 176 ++-- ptpython/key_bindings.py | 160 ++-- ptpython/layout.py | 698 +++++++++------ ptpython/prompt_style.py | 61 +- ptpython/python_input.py | 899 ++++++++++++-------- ptpython/repl.py | 271 +++--- ptpython/style.py | 211 ++--- ptpython/utils.py | 76 +- ptpython/validator.py | 20 +- pyproject.toml | 13 + setup.py | 59 +- tests/run_tests.py | 16 +- 27 files changed, 2065 insertions(+), 1589 deletions(-) create mode 100644 pyproject.toml diff --git a/.travis.yml b/.travis.yml index 79a93e91..21611f91 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,18 +4,17 @@ language: python matrix: include: - python: 3.6 - - python: 3.5 - - python: 3.4 - - python: 3.3 - - python: 2.7 - - python: 2.6 - - python: pypy - - python: pypy3 + - python: 3.7 install: - - travis_retry pip install . pytest + - travis_retry pip install . pytest isort black - pip list script: - echo "$TRAVIS_PYTHON_VERSION" - ./tests/run_tests.py + + # Check wheather the imports were sorted correctly. + - isort -c -rc ptpython tests setup.py examples + + - black --check ptpython setup.py examples diff --git a/examples/asyncio-python-embed.py b/examples/asyncio-python-embed.py index fef19b7f..3b796b2a 100755 --- a/examples/asyncio-python-embed.py +++ b/examples/asyncio-python-embed.py @@ -12,10 +12,11 @@ prompt. """ from __future__ import unicode_literals -from ptpython.repl import embed import asyncio +from ptpython.repl import embed + loop = asyncio.get_event_loop() counter = [0] @@ -26,7 +27,7 @@ def print_counter(): Coroutine that prints counters and saves it in a global variable. """ while True: - print('Counter: %i' % counter[0]) + print("Counter: %i" % counter[0]) counter[0] += 1 yield from asyncio.sleep(3) @@ -37,9 +38,13 @@ def interactive_shell(): Coroutine that starts a Python REPL from which we can access the global counter variable. """ - print('You should be able to read and update the "counter[0]" variable from this shell.') + print( + 'You should be able to read and update the "counter[0]" variable from this shell.' + ) try: - yield from embed(globals=globals(), return_asyncio_coroutine=True, patch_stdout=True) + yield from embed( + globals=globals(), return_asyncio_coroutine=True, patch_stdout=True + ) except EOFError: # Stop the loop when quitting the repl. (Ctrl-D press.) loop.stop() @@ -53,5 +58,5 @@ def main(): loop.close() -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/examples/asyncio-ssh-python-embed.py b/examples/asyncio-ssh-python-embed.py index cbd07003..86b56073 100755 --- a/examples/asyncio-ssh-python-embed.py +++ b/examples/asyncio-ssh-python-embed.py @@ -6,9 +6,10 @@ Run this example and then SSH to localhost, port 8222. """ import asyncio -import asyncssh import logging +import asyncssh + from ptpython.contrib.asyncssh_repl import ReplSSHServerSession logging.basicConfig() @@ -19,6 +20,7 @@ class MySSHServer(asyncssh.SSHServer): """ Server without authentication, running `ReplSSHServerSession`. """ + def __init__(self, get_namespace): self.get_namespace = get_namespace @@ -37,22 +39,24 @@ def main(port=8222): loop = asyncio.get_event_loop() # Namespace exposed in the REPL. - environ = {'hello': 'world'} + environ = {"hello": "world"} # Start SSH server. def create_server(): return MySSHServer(lambda: environ) - print('Listening on :%i' % port) + print("Listening on :%i" % port) print('To connect, do "ssh localhost -p %i"' % port) loop.run_until_complete( - asyncssh.create_server(create_server, '', port, - server_host_keys=['/etc/ssh/ssh_host_dsa_key'])) + asyncssh.create_server( + create_server, "", port, server_host_keys=["/etc/ssh/ssh_host_dsa_key"] + ) + ) # Run eventloop. loop.run_forever() -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/examples/ptpython_config/config.py b/examples/ptpython_config/config.py index c79b01e0..ff8b8ac1 100644 --- a/examples/ptpython_config/config.py +++ b/examples/ptpython_config/config.py @@ -4,6 +4,7 @@ Copy this file to $XDG_CONFIG_HOME/ptpython/config.py """ from __future__ import unicode_literals + from prompt_toolkit.filters import ViInsertMode from prompt_toolkit.key_binding.key_processor import KeyPress from prompt_toolkit.keys import Keys @@ -11,9 +12,7 @@ from ptpython.layout import CompletionVisualisation -__all__ = ( - 'configure', -) +__all__ = ("configure",) def configure(repl): @@ -50,7 +49,7 @@ def configure(repl): # Swap light/dark colors on or off repl.swap_light_and_dark = False - + # Highlight matching parethesis. repl.highlight_matching_parenthesis = True @@ -75,7 +74,7 @@ def configure(repl): repl.paste_mode = False # Use the classic prompt. (Display '>>>' instead of 'In [1]'.) - repl.prompt_style = 'classic' # 'classic' or 'ipython' + repl.prompt_style = "classic" # 'classic' or 'ipython' # Don't insert a blank line after the output. repl.insert_blank_line_after_output = False @@ -108,14 +107,14 @@ def configure(repl): repl.enable_input_validation = True # Use this colorscheme for the code. - repl.use_code_colorscheme('pastie') + repl.use_code_colorscheme("pastie") # Set color depth (keep in mind that not all terminals support true color). - #repl.color_depth = 'DEPTH_1_BIT' # Monochrome. - #repl.color_depth = 'DEPTH_4_BIT' # ANSI colors only. - repl.color_depth = 'DEPTH_8_BIT' # The default, 256 colors. - #repl.color_depth = 'DEPTH_24_BIT' # True color. + # repl.color_depth = 'DEPTH_1_BIT' # Monochrome. + # repl.color_depth = 'DEPTH_4_BIT' # ANSI colors only. + repl.color_depth = "DEPTH_8_BIT" # The default, 256 colors. + # repl.color_depth = 'DEPTH_24_BIT' # True color. # Syntax. repl.enable_syntax_highlighting = True @@ -142,7 +141,6 @@ def _(event): event.current_buffer.validate_and_handle() """ - # Typing 'jj' in Vi Insert mode, should send escape. (Go back to navigation # mode.) """ @@ -178,8 +176,7 @@ def _(event): # `ptpython/style.py` for all possible tokens. _custom_ui_colorscheme = { # Blue prompt. - Token.Layout.Prompt: 'bg:#eeeeff #000000 bold', - + Token.Layout.Prompt: "bg:#eeeeff #000000 bold", # Make the status toolbar red. - Token.Toolbar.Status: 'bg:#ff0000 #000000', + Token.Toolbar.Status: "bg:#ff0000 #000000", } diff --git a/examples/python-embed-with-custom-prompt.py b/examples/python-embed-with-custom-prompt.py index 28eca860..bf27e936 100755 --- a/examples/python-embed-with-custom-prompt.py +++ b/examples/python-embed-with-custom-prompt.py @@ -4,10 +4,11 @@ """ from __future__ import unicode_literals -from ptpython.repl import embed -from ptpython.prompt_style import PromptStyle from pygments.token import Token +from ptpython.prompt_style import PromptStyle +from ptpython.repl import embed + def configure(repl): # There are several ways to override the prompt. @@ -18,25 +19,23 @@ def configure(repl): class CustomPrompt(PromptStyle): def in_tokens(self, cli): return [ - (Token.In, 'Input['), - (Token.In.Number, '%s' % repl.current_statement_index), - (Token.In, '] >>: '), + (Token.In, "Input["), + (Token.In.Number, "%s" % repl.current_statement_index), + (Token.In, "] >>: "), ] def in2_tokens(self, cli, width): - return [ - (Token.In, '...: '.rjust(width)), - ] + return [(Token.In, "...: ".rjust(width))] def out_tokens(self, cli): return [ - (Token.Out, 'Result['), - (Token.Out.Number, '%s' % repl.current_statement_index), - (Token.Out, ']: '), + (Token.Out, "Result["), + (Token.Out.Number, "%s" % repl.current_statement_index), + (Token.Out, "]: "), ] - repl.all_prompt_styles['custom'] = CustomPrompt() - repl.prompt_style = 'custom' + repl.all_prompt_styles["custom"] = CustomPrompt() + repl.prompt_style = "custom" # 2. Assign a new callable to `get_input_prompt_tokens`. This will always take effect. ## repl.get_input_prompt_tokens = lambda cli: [(Token.In, '[hello] >>> ')] @@ -52,5 +51,5 @@ def main(): embed(globals(), locals(), configure=configure) -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/examples/python-embed.py b/examples/python-embed.py index 72c1c101..af24456e 100755 --- a/examples/python-embed.py +++ b/examples/python-embed.py @@ -10,5 +10,5 @@ def main(): embed(globals(), locals(), vi_mode=False) -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/examples/python-input.py b/examples/python-input.py index bcfd6fca..1956070d 100755 --- a/examples/python-input.py +++ b/examples/python-input.py @@ -10,8 +10,8 @@ def main(): prompt = PythonInput() text = prompt.app.run() - print('You said: ' + text) + print("You said: " + text) -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/ptpython/__main__.py b/ptpython/__main__.py index 7e4cbabe..83340a7b 100644 --- a/ptpython/__main__.py +++ b/ptpython/__main__.py @@ -1,7 +1,6 @@ """ Make `python -m ptpython` an alias for running `./ptpython`. """ -from __future__ import unicode_literals from .entry_points.run_ptpython import run run() diff --git a/ptpython/completer.py b/ptpython/completer.py index 8fa0e314..2ffaf62e 100644 --- a/ptpython/completer.py +++ b/ptpython/completer.py @@ -1,27 +1,33 @@ -from __future__ import unicode_literals +import ast +import keyword +import re +from typing import TYPE_CHECKING, Iterable -from prompt_toolkit.completion import Completer, Completion, PathCompleter +from prompt_toolkit.completion import ( + CompleteEvent, + Completer, + Completion, + PathCompleter, +) from prompt_toolkit.contrib.regular_languages.compiler import compile as compile_grammar from prompt_toolkit.contrib.regular_languages.completion import GrammarCompleter +from prompt_toolkit.document import Document from ptpython.utils import get_jedi_script_from_document -import keyword -import ast -import re -import six +if TYPE_CHECKING: + from prompt_toolkit.contrib.regular_languages.compiler import _CompiledGrammar -__all__ = ( - 'PythonCompleter', -) +__all__ = ["PythonCompleter"] class PythonCompleter(Completer): """ Completer for Python code. """ + def __init__(self, get_globals, get_locals, get_enable_dictionary_completion): - super(PythonCompleter, self).__init__() + super().__init__() self.get_globals = get_globals self.get_locals = get_locals @@ -33,17 +39,19 @@ def __init__(self, get_globals, get_locals, get_enable_dictionary_completion): self._path_completer_grammar_cache = None @property - def _path_completer(self): + def _path_completer(self) -> GrammarCompleter: if self._path_completer_cache is None: self._path_completer_cache = GrammarCompleter( - self._path_completer_grammar, { - 'var1': PathCompleter(expanduser=True), - 'var2': PathCompleter(expanduser=True), - }) + self._path_completer_grammar, + { + "var1": PathCompleter(expanduser=True), + "var2": PathCompleter(expanduser=True), + }, + ) return self._path_completer_cache @property - def _path_completer_grammar(self): + def _path_completer_grammar(self) -> "_CompiledGrammar": """ Return the grammar for matching paths inside strings inside Python code. @@ -54,15 +62,15 @@ def _path_completer_grammar(self): self._path_completer_grammar_cache = self._create_path_completer_grammar() return self._path_completer_grammar_cache - def _create_path_completer_grammar(self): - def unwrapper(text): - return re.sub(r'\\(.)', r'\1', text) + def _create_path_completer_grammar(self) -> "_CompiledGrammar": + def unwrapper(text: str) -> str: + return re.sub(r"\\(.)", r"\1", text) - def single_quoted_wrapper(text): - return text.replace('\\', '\\\\').replace("'", "\\'") + def single_quoted_wrapper(text: str) -> str: + return text.replace("\\", "\\\\").replace("'", "\\'") - def double_quoted_wrapper(text): - return text.replace('\\', '\\\\').replace('"', '\\"') + def double_quoted_wrapper(text: str) -> str: + return text.replace("\\", "\\\\").replace('"', '\\"') grammar = r""" # Text before the current string. @@ -91,40 +99,45 @@ def double_quoted_wrapper(text): return compile_grammar( grammar, - escape_funcs={ - 'var1': single_quoted_wrapper, - 'var2': double_quoted_wrapper, - }, - unescape_funcs={ - 'var1': unwrapper, - 'var2': unwrapper, - }) - - def _complete_path_while_typing(self, document): + escape_funcs={"var1": single_quoted_wrapper, "var2": double_quoted_wrapper}, + unescape_funcs={"var1": unwrapper, "var2": unwrapper}, + ) + + def _complete_path_while_typing(self, document: Document) -> bool: char_before_cursor = document.char_before_cursor - return document.text and ( - char_before_cursor.isalnum() or char_before_cursor in '/.~') + return bool( + document.text + and (char_before_cursor.isalnum() or char_before_cursor in "/.~") + ) - def _complete_python_while_typing(self, document): + def _complete_python_while_typing(self, document: Document) -> bool: char_before_cursor = document.char_before_cursor - return document.text and ( - char_before_cursor.isalnum() or char_before_cursor in '_.') + return bool( + document.text + and (char_before_cursor.isalnum() or char_before_cursor in "_.") + ) - def get_completions(self, document, complete_event): + def get_completions( + self, document: Document, complete_event: CompleteEvent + ) -> Iterable[Completion]: """ Get Python completions. """ # Do dictionary key completions. if self.get_enable_dictionary_completion(): has_dict_completions = False - for c in self.dictionary_completer.get_completions(document, complete_event): + for c in self.dictionary_completer.get_completions( + document, complete_event + ): has_dict_completions = True yield c if has_dict_completions: return # Do Path completions (if there were no dictionary completions). - if complete_event.completion_requested or self._complete_path_while_typing(document): + if complete_event.completion_requested or self._complete_path_while_typing( + document + ): for c in self._path_completer.get_completions(document, complete_event): yield c @@ -133,8 +146,12 @@ def get_completions(self, document, complete_event): return # Do Jedi Python completions. - if complete_event.completion_requested or self._complete_python_while_typing(document): - script = get_jedi_script_from_document(document, self.get_locals(), self.get_globals()) + if complete_event.completion_requested or self._complete_python_while_typing( + document + ): + script = get_jedi_script_from_document( + document, self.get_locals(), self.get_globals() + ) if script: try: @@ -178,9 +195,11 @@ def get_completions(self, document, complete_event): else: for c in completions: yield Completion( - c.name_with_symbols, len(c.complete) - len(c.name_with_symbols), + c.name_with_symbols, + len(c.complete) - len(c.name_with_symbols), display=c.name_with_symbols, - style=_get_style_for_name(c.name_with_symbols)) + style=_get_style_for_name(c.name_with_symbols), + ) class DictionaryCompleter(Completer): @@ -191,14 +210,15 @@ class DictionaryCompleter(Completer): bracket, which is potentially dangerous. It doesn't match on function calls, so it only triggers attribute access. """ + def __init__(self, get_globals, get_locals): - super(DictionaryCompleter, self).__init__() + super().__init__() self.get_globals = get_globals self.get_locals = get_locals self.pattern = re.compile( - r''' + r""" # Any expression safe enough to eval while typing. # No operators, except dot, and only other dict lookups. # Technically, this can be unsafe of course, if bad code runs @@ -227,11 +247,13 @@ def __init__(self, get_globals, get_locals): # string). \[ \s* ([a-zA-Z0-9_'"]*)$ - ''', - re.VERBOSE + """, + re.VERBOSE, ) - def get_completions(self, document, complete_event): + def get_completions( + self, document: Document, complete_event: CompleteEvent + ) -> Iterable[Completion]: match = self.pattern.search(document.text_before_cursor) if match is not None: object_var, key = match.groups() @@ -240,7 +262,7 @@ def get_completions(self, document, complete_event): # Do lookup of `object_var` in the context. try: result = eval(object_var, self.get_globals(), self.get_locals()) - except BaseException as e: + except BaseException: return # Many exception, like NameError can be thrown here. # If this object is a dictionary, complete the keys. @@ -256,28 +278,26 @@ def get_completions(self, document, complete_event): break for k in result: - if six.text_type(k).startswith(key_obj): - yield Completion( - six.text_type(repr(k)), - - len(key), - display=six.text_type(repr(k)) - ) + if str(k).startswith(key_obj): + yield Completion(str(repr(k)), -len(key), display=str(repr(k))) + try: import builtins + _builtin_names = dir(builtins) except ImportError: # Python 2. _builtin_names = [] -def _get_style_for_name(name): +def _get_style_for_name(name: str) -> str: """ Return completion style to use for this name. """ if name in _builtin_names: - return 'class:completion.builtin' + return "class:completion.builtin" if keyword.iskeyword(name): - return 'class:completion.keyword' + return "class:completion.keyword" - return '' + return "" diff --git a/ptpython/contrib/asyncssh_repl.py b/ptpython/contrib/asyncssh_repl.py index a4df4449..29c63afb 100644 --- a/ptpython/contrib/asyncssh_repl.py +++ b/ptpython/contrib/asyncssh_repl.py @@ -6,22 +6,18 @@ should make sure not to use Python 3-only syntax, because this package should be installable in Python 2 as well! """ -from __future__ import unicode_literals - import asyncio -import asyncssh +from typing import Optional, TextIO, cast -from prompt_toolkit.input import PipeInput -from prompt_toolkit.interface import CommandLineInterface -from prompt_toolkit.layout.screen import Size -from prompt_toolkit.shortcuts import create_asyncio_eventloop -from prompt_toolkit.terminal.vt100_output import Vt100_Output +import asyncssh +from prompt_toolkit.data_structures import Size +from prompt_toolkit.input import create_pipe_input +from prompt_toolkit.output.vt100 import Vt100_Output +from ptpython.python_input import _GetNamespace from ptpython.repl import PythonRepl -__all__ = ( - 'ReplSSHServerSession', -) +__all__ = ["ReplSSHServerSession"] class ReplSSHServerSession(asyncssh.SSHServerSession): @@ -31,51 +27,47 @@ class ReplSSHServerSession(asyncssh.SSHServerSession): :param get_globals: callable that returns the current globals. :param get_locals: (optional) callable that returns the current locals. """ - def __init__(self, get_globals, get_locals=None): - assert callable(get_globals) - assert get_locals is None or callable(get_locals) + def __init__( + self, get_globals: _GetNamespace, get_locals: Optional[_GetNamespace] = None + ) -> None: self._chan = None - def _globals(): + def _globals() -> dict: data = get_globals() - data.setdefault('print', self._print) + data.setdefault("print", self._print) return data - repl = PythonRepl(get_globals=_globals, - get_locals=get_locals or _globals) - - # Disable open-in-editor and system prompt. Because it would run and - # display these commands on the server side, rather than in the SSH - # client. - repl.enable_open_in_editor = False - repl.enable_system_bindings = False - # PipInput object, for sending input in the CLI. # (This is something that we can use in the prompt_toolkit event loop, # but still write date in manually.) - self._input_pipe = PipeInput() + self._input_pipe = create_pipe_input() # Output object. Don't render to the real stdout, but write everything # in the SSH channel. - class Stdout(object): - def write(s, data): + class Stdout: + def write(s, data: str) -> None: if self._chan is not None: - self._chan.write(data.replace('\n', '\r\n')) + data = data.replace("\n", "\r\n") + self._chan.write(data) - def flush(s): + def flush(s) -> None: pass - # Create command line interface. - self.cli = CommandLineInterface( - application=repl.create_application(), - eventloop=create_asyncio_eventloop(), + self.repl = PythonRepl( + get_globals=_globals, + get_locals=get_locals or _globals, input=self._input_pipe, - output=Vt100_Output(Stdout(), self._get_size)) + output=Vt100_Output(cast(TextIO, Stdout()), self._get_size), + ) - self._callbacks = self.cli.create_eventloop_callbacks() + # Disable open-in-editor and system prompt. Because it would run and + # display these commands on the server side, rather than in the SSH + # client. + self.repl.enable_open_in_editor = False + self.repl.enable_system_bindings = False - def _get_size(self): + def _get_size(self) -> Size: """ Callable that returns the current `Size`, required by Vt100_Output. """ @@ -92,22 +84,23 @@ def connection_made(self, chan): self._chan = chan # Run REPL interface. - f = asyncio.ensure_future(self.cli.run_async()) + f = asyncio.ensure_future(self.repl.run_async()) # Close channel when done. - def done(_): + def done(_) -> None: chan.close() self._chan = None + f.add_done_callback(done) - def shell_requested(self): + def shell_requested(self) -> bool: return True def terminal_size_changed(self, width, height, pixwidth, pixheight): """ When the terminal size changes, report back to CLI. """ - self._callbacks.terminal_size_changed() + self.repl.app._on_resize() def data_received(self, data, datatype): """ @@ -115,19 +108,12 @@ def data_received(self, data, datatype): """ self._input_pipe.send(data) - def _print(self, *data, **kw): + def _print(self, *data, sep=" ", end="\n", file=None) -> None: """ - _print(self, *data, sep=' ', end='\n', file=None) - Alternative 'print' function that prints back into the SSH channel. """ # Pop keyword-only arguments. (We cannot use the syntax from the # signature. Otherwise, Python2 will give a syntax error message when # installing.) - sep = kw.pop('sep', ' ') - end = kw.pop('end', '\n') - _ = kw.pop('file', None) - assert not kw, 'Too many keyword-only arguments' - data = sep.join(map(str, data)) self._chan.write(data + end) diff --git a/ptpython/entry_points/run_ptipython.py b/ptpython/entry_points/run_ptipython.py index 67239ce7..e7bcf39a 100644 --- a/ptpython/entry_points/run_ptipython.py +++ b/ptpython/entry_points/run_ptipython.py @@ -1,70 +1,37 @@ #!/usr/bin/env python -""" -ptipython: IPython interactive shell with the `prompt_toolkit` front-end. -Usage: - ptpython [ --vi ] - [ --config-dir= ] [ --interactive= ] - [--] [ ... ] - ptpython -h | --help - -Options: - --vi : Use Vi keybindings instead of Emacs bindings. - --config-dir= : Pass config directory. By default '$XDG_CONFIG_HOME/ptpython'. - -i, --interactive= : Start interactive shell after executing this file. -""" -from __future__ import absolute_import, unicode_literals, print_function - -import appdirs -import docopt import os -import six import sys +from .run_ptpython import create_parser, get_config_and_history_file -def run(user_ns=None): - a = docopt.docopt(__doc__) - - vi_mode = bool(a['--vi']) - - config_dir = appdirs.user_config_dir('ptpython', 'prompt_toolkit') - data_dir = appdirs.user_data_dir('ptpython', 'prompt_toolkit') - if a['--config-dir']: - # Override config_dir. - config_dir = os.path.expanduser(a['--config-dir']) - else: - # Warn about the legacy directory. - legacy_dir = os.path.expanduser('~/.ptpython') - if os.path.isdir(legacy_dir): - print('{0} is deprecated, migrate your configuration to {1}'.format(legacy_dir, config_dir)) +def run(user_ns=None): + a = create_parser().parse_args() - # Create directories. - for d in (config_dir, data_dir): - if not os.path.isdir(d) and not os.path.islink(d): - os.mkdir(d) + config_file, history_file = get_config_and_history_file(a) # If IPython is not available, show message and exit here with error status # code. try: import IPython except ImportError: - print('IPython not found. Please install IPython (pip install ipython).') + print("IPython not found. Please install IPython (pip install ipython).") sys.exit(1) else: from ptpython.ipython import embed from ptpython.repl import run_config, enable_deprecation_warnings # Add the current directory to `sys.path`. - if sys.path[0] != '': - sys.path.insert(0, '') + if sys.path[0] != "": + sys.path.insert(0, "") # When a file has been given, run that, otherwise start the shell. - if a[''] and not a['--interactive']: - sys.argv = a[''] - path = a[''][0] - with open(path, 'rb') as f: - code = compile(f.read(), path, 'exec') - six.exec_(code) + if a.args and not a.interactive: + sys.argv = a.args + path = a.args[0] + with open(path, "rb") as f: + code = compile(f.read(), path, "exec") + exec(code, {}) else: enable_deprecation_warnings() @@ -76,37 +43,38 @@ def run(user_ns=None): # Startup path startup_paths = [] - if 'PYTHONSTARTUP' in os.environ: - startup_paths.append(os.environ['PYTHONSTARTUP']) + if "PYTHONSTARTUP" in os.environ: + startup_paths.append(os.environ["PYTHONSTARTUP"]) # --interactive - if a['--interactive']: - startup_paths.append(a['--interactive']) - sys.argv = [a['--interactive']] + a[''] + if a.interactive: + startup_paths.append(a.args[0]) + sys.argv = a.args # exec scripts from startup paths for path in startup_paths: if os.path.exists(path): - with open(path, 'rb') as f: - code = compile(f.read(), path, 'exec') - six.exec_(code, user_ns, user_ns) + with open(path, "rb") as f: + code = compile(f.read(), path, "exec") + exec(code, user_ns, user_ns) else: - print('File not found: {}\n\n'.format(path)) + print("File not found: {}\n\n".format(path)) sys.exit(1) # Apply config file def configure(repl): - path = os.path.join(config_dir, 'config.py') - if os.path.exists(path): - run_config(repl, path) + if os.path.exists(config_file): + run_config(repl, config_file) # Run interactive shell. - embed(vi_mode=vi_mode, - history_filename=os.path.join(data_dir, 'history'), - configure=configure, - user_ns=user_ns, - title='IPython REPL (ptipython)') + embed( + vi_mode=a.vi, + history_filename=history_file, + configure=configure, + user_ns=user_ns, + title="IPython REPL (ptipython)", + ) -if __name__ == '__main__': +if __name__ == "__main__": run() diff --git a/ptpython/entry_points/run_ptpython.py b/ptpython/entry_points/run_ptpython.py index ef9b44a8..a8710792 100644 --- a/ptpython/entry_points/run_ptpython.py +++ b/ptpython/entry_points/run_ptpython.py @@ -15,81 +15,158 @@ Other environment variables: PYTHONSTARTUP: file executed on interactive startup (no default) """ -from __future__ import absolute_import, unicode_literals, print_function - -import appdirs -import docopt +import argparse import os -import six import sys +from typing import Tuple -from ptpython.repl import embed, enable_deprecation_warnings, run_config - - -def run(): - a = docopt.docopt(__doc__) - - vi_mode = bool(a['--vi']) +import appdirs +from prompt_toolkit.formatted_text import HTML +from prompt_toolkit.shortcuts import print_formatted_text - config_dir = appdirs.user_config_dir('ptpython', 'prompt_toolkit') - data_dir = appdirs.user_data_dir('ptpython', 'prompt_toolkit') +from ptpython.repl import embed, enable_deprecation_warnings, run_config - if a['--config-dir']: - # Override config_dir. - config_dir = os.path.expanduser(a['--config-dir']) - else: - # Warn about the legacy directory. - legacy_dir = os.path.expanduser('~/.ptpython') - if os.path.isdir(legacy_dir): - print('{0} is deprecated, migrate your configuration to {1}'.format(legacy_dir, config_dir)) +__all__ = ["create_parser", "get_config_and_history_file", "run"] + + +class _Parser(argparse.ArgumentParser): + def print_help(self): + super().print_help() + print("Other environment variables:") + print("PYTHONSTARTUP: file executed on interactive startup (no default)") + + +def create_parser() -> _Parser: + parser = _Parser(description="ptpython: Interactive Python shell.") + parser.add_argument("--vi", action="store_true", help="Enable Vi key bindings") + parser.add_argument( + "-i", + "--interactive", + action="store_true", + help="Start interactive shell after executing this file.", + ) + parser.add_argument( + "--config-file", type=str, help="Location of configuration file." + ) + parser.add_argument("--history-file", type=str, help="Location of history file.") + parser.add_argument( + "-V", "--version", action="store_true", help="Print version and exit." + ) + parser.add_argument("args", nargs="*", help="Script and arguments") + return parser + + +def get_config_and_history_file(namespace: argparse.Namespace) -> Tuple[str, str]: + """ + Check which config/history files to use, ensure that the directories for + these files exist, and return the config and history path. + """ + config_dir = appdirs.user_config_dir("ptpython", "prompt_toolkit") + data_dir = appdirs.user_data_dir("ptpython", "prompt_toolkit") # Create directories. for d in (config_dir, data_dir): if not os.path.isdir(d) and not os.path.islink(d): os.mkdir(d) + # Determine config file to be used. + config_file = os.path.join(config_dir, "config.py") + legacy_config_file = os.path.join(os.path.expanduser("~/.ptpython"), "config.py") + + warnings = [] + + # Config file + if namespace.config_file: + # Override config_file. + config_file = os.path.expanduser(namespace.config_file) + + elif os.path.isfile(legacy_config_file): + # Warn about the legacy configuration file. + warnings.append( + HTML( + " ~/.ptpython/config.py is deprecated, move your configuration to %s\n" + ) + % config_file + ) + config_file = legacy_config_file + + # Determine history file to be used. + history_file = os.path.join(data_dir, "history") + legacy_history_file = os.path.join(os.path.expanduser("~/.ptpython"), "history") + + if namespace.history_file: + # Override history_file. + history_file = os.path.expanduser(namespace.history_file) + + elif os.path.isfile(legacy_history_file): + # Warn about the legacy history file. + warnings.append( + HTML( + " ~/.ptpython/history is deprecated, move your history to %s\n" + ) + % history_file + ) + history_file = legacy_history_file + + # Print warnings. + if warnings: + print_formatted_text(HTML("Warning:")) + for w in warnings: + print_formatted_text(w) + + return config_file, history_file + + +def run() -> None: + a = create_parser().parse_args() + + config_file, history_file = get_config_and_history_file(a) + # Startup path startup_paths = [] - if 'PYTHONSTARTUP' in os.environ: - startup_paths.append(os.environ['PYTHONSTARTUP']) + if "PYTHONSTARTUP" in os.environ: + startup_paths.append(os.environ["PYTHONSTARTUP"]) # --interactive - if a['--interactive']: - startup_paths.append(a['--interactive']) - sys.argv = [a['--interactive']] + a[''] + if a.interactive and a.args: + startup_paths.append(a.args[0]) + sys.argv = a.args # Add the current directory to `sys.path`. - if sys.path[0] != '': - sys.path.insert(0, '') + if sys.path[0] != "": + sys.path.insert(0, "") # When a file has been given, run that, otherwise start the shell. - if a[''] and not a['--interactive']: - sys.argv = a[''] - path = a[''][0] - with open(path, 'rb') as f: - code = compile(f.read(), path, 'exec') + if a.args and not a.interactive: + sys.argv = a.args + path = a.args[0] + with open(path, "rb") as f: + code = compile(f.read(), path, "exec") # NOTE: We have to pass an empty dictionary as namespace. Omitting # this argument causes imports to not be found. See issue #326. - six.exec_(code, {}) + exec(code, {}) # Run interactive shell. else: enable_deprecation_warnings() # Apply config file - def configure(repl): - path = os.path.join(config_dir, 'config.py') - if os.path.exists(path): - run_config(repl, path) + def configure(repl) -> None: + if os.path.exists(config_file): + run_config(repl, config_file) import __main__ - embed(vi_mode=vi_mode, - history_filename=os.path.join(data_dir, 'history'), - configure=configure, - locals=__main__.__dict__, - globals=__main__.__dict__, - startup_paths=startup_paths, - title='Python REPL (ptpython)') - -if __name__ == '__main__': + + embed( + vi_mode=a.vi, + history_filename=history_file, + configure=configure, + locals=__main__.__dict__, + globals=__main__.__dict__, + startup_paths=startup_paths, + title="Python REPL (ptpython)", + ) + + +if __name__ == "__main__": run() diff --git a/ptpython/eventloop.py b/ptpython/eventloop.py index 9d16a2df..1e8c46a3 100644 --- a/ptpython/eventloop.py +++ b/ptpython/eventloop.py @@ -10,9 +10,7 @@ import sys import time -__all__ = ( - 'inputhook', -) +__all__ = ["inputhook"] def _inputhook_tk(inputhook_context): @@ -22,7 +20,8 @@ def _inputhook_tk(inputhook_context): """ # Get the current TK application. import _tkinter # Keep this imports inline! - from six.moves import tkinter + import tkinter + root = tkinter._default_root def wait_using_filehandler(): @@ -33,6 +32,7 @@ def wait_using_filehandler(): # Add a handler that sets the stop flag when `prompt-toolkit` has input # to process. stop = [False] + def done(*a): stop[0] = True @@ -52,13 +52,13 @@ def wait_using_polling(): """ while not inputhook_context.input_is_ready(): while root.dooneevent(_tkinter.ALL_EVENTS | _tkinter.DONT_WAIT): - pass + pass # Sleep to make the CPU idle, but not too long, so that the UI # stays responsive. - time.sleep(.01) + time.sleep(0.01) if root is not None: - if hasattr(root, 'createfilehandler'): + if hasattr(root, "createfilehandler"): wait_using_filehandler() else: wait_using_polling() @@ -66,5 +66,5 @@ def wait_using_polling(): def inputhook(inputhook_context): # Only call the real input hook when the 'Tkinter' library was loaded. - if 'Tkinter' in sys.modules or 'tkinter' in sys.modules: + if "Tkinter" in sys.modules or "tkinter" in sys.modules: _inputhook_tk(inputhook_context) diff --git a/ptpython/filters.py b/ptpython/filters.py index 8ddc3c6a..1adac135 100644 --- a/ptpython/filters.py +++ b/ptpython/filters.py @@ -1,38 +1,36 @@ -from __future__ import unicode_literals +from typing import TYPE_CHECKING from prompt_toolkit.filters import Filter -__all__ = ( - 'HasSignature', - 'ShowSidebar', - 'ShowSignature', - 'ShowDocstring', -) +if TYPE_CHECKING: + from .python_input import PythonInput + +__all__ = ["HasSignature", "ShowSidebar", "ShowSignature", "ShowDocstring"] class PythonInputFilter(Filter): - def __init__(self, python_input): + def __init__(self, python_input: "PythonInput") -> None: self.python_input = python_input - def __call__(self): + def __call__(self) -> bool: raise NotImplementedError class HasSignature(PythonInputFilter): - def __call__(self): + def __call__(self) -> bool: return bool(self.python_input.signatures) class ShowSidebar(PythonInputFilter): - def __call__(self): + def __call__(self) -> bool: return self.python_input.show_sidebar class ShowSignature(PythonInputFilter): - def __call__(self): + def __call__(self) -> bool: return self.python_input.show_signature class ShowDocstring(PythonInputFilter): - def __call__(self): + def __call__(self) -> bool: return self.python_input.show_docstring diff --git a/ptpython/history_browser.py b/ptpython/history_browser.py index 3d14067a..6d8ede43 100644 --- a/ptpython/history_browser.py +++ b/ptpython/history_browser.py @@ -4,7 +4,7 @@ `create_history_application` creates an `Application` instance that runs will run as a sub application of the Repl/PythonInput. """ -from __future__ import unicode_literals +from functools import partial from prompt_toolkit.application import Application from prompt_toolkit.application.current import get_app @@ -14,7 +14,17 @@ from prompt_toolkit.filters import Condition, has_focus from prompt_toolkit.formatted_text.utils import fragment_list_to_text from prompt_toolkit.key_binding import KeyBindings -from prompt_toolkit.layout.containers import HSplit, VSplit, Window, FloatContainer, Float, ConditionalContainer, Container, ScrollOffsets, WindowAlign +from prompt_toolkit.layout.containers import ( + ConditionalContainer, + Container, + Float, + FloatContainer, + HSplit, + ScrollOffsets, + VSplit, + Window, + WindowAlign, +) from prompt_toolkit.layout.controls import BufferControl, FormattedTextControl from prompt_toolkit.layout.dimension import Dimension as D from prompt_toolkit.layout.layout import Layout @@ -23,25 +33,16 @@ from prompt_toolkit.lexers import PygmentsLexer from prompt_toolkit.widgets import Frame from prompt_toolkit.widgets.toolbars import ArgToolbar, SearchToolbar +from pygments.lexers import Python3Lexer as PythonLexer from pygments.lexers import RstLexer -from .utils import if_mousedown - from ptpython.layout import get_inputmode_fragments -from functools import partial -import six - -if six.PY2: - from pygments.lexers import PythonLexer -else: - from pygments.lexers import Python3Lexer as PythonLexer +from .utils import if_mousedown HISTORY_COUNT = 2000 -__all__ = ( - 'HistoryLayout', -) +__all__ = ["HistoryLayout", "PythonHistory"] HELP_TEXT = """ This interface is meant to select multiple lines from the @@ -85,107 +86,128 @@ class BORDER: " Box drawing characters. " - HORIZONTAL = '\u2501' - VERTICAL = '\u2503' - TOP_LEFT = '\u250f' - TOP_RIGHT = '\u2513' - BOTTOM_LEFT = '\u2517' - BOTTOM_RIGHT = '\u251b' - LIGHT_VERTICAL = '\u2502' + HORIZONTAL = "\u2501" + VERTICAL = "\u2503" + TOP_LEFT = "\u250f" + TOP_RIGHT = "\u2513" + BOTTOM_LEFT = "\u2517" + BOTTOM_RIGHT = "\u251b" + LIGHT_VERTICAL = "\u2502" -def _create_popup_window(title, body): +def _create_popup_window(title: str, body: Container) -> Frame: """ Return the layout for a pop-up window. It consists of a title bar showing the `title` text, and a body layout. The window is surrounded by borders. """ - assert isinstance(title, six.text_type) - assert isinstance(body, Container) return Frame(body=body, title=title) -class HistoryLayout(object): +class HistoryLayout: """ Create and return a `Container` instance for the history application. """ + def __init__(self, history): search_toolbar = SearchToolbar() self.help_buffer_control = BufferControl( - buffer=history.help_buffer, - lexer=PygmentsLexer(RstLexer)) + buffer=history.help_buffer, lexer=PygmentsLexer(RstLexer) + ) help_window = _create_popup_window( - title='History Help', + title="History Help", body=Window( content=self.help_buffer_control, right_margins=[ScrollbarMargin(display_arrows=True)], - scroll_offsets=ScrollOffsets(top=2, bottom=2))) + scroll_offsets=ScrollOffsets(top=2, bottom=2), + ), + ) self.default_buffer_control = BufferControl( buffer=history.default_buffer, input_processors=[GrayExistingText(history.history_mapping)], - lexer=PygmentsLexer(PythonLexer)) + lexer=PygmentsLexer(PythonLexer), + ) self.history_buffer_control = BufferControl( buffer=history.history_buffer, lexer=PygmentsLexer(PythonLexer), search_buffer_control=search_toolbar.control, - preview_search=True) + preview_search=True, + ) history_window = Window( content=self.history_buffer_control, wrap_lines=False, left_margins=[HistoryMargin(history)], - scroll_offsets=ScrollOffsets(top=2, bottom=2)) - - self.root_container = HSplit([ - # Top title bar. - Window( - content=FormattedTextControl(_get_top_toolbar_fragments), - align=WindowAlign.CENTER, - style='class:status-toolbar'), - FloatContainer( - content=VSplit([ - # Left side: history. - history_window, - # Separator. - Window(width=D.exact(1), - char=BORDER.LIGHT_VERTICAL, - style='class:separator'), - # Right side: result. - Window( - content=self.default_buffer_control, - wrap_lines=False, - left_margins=[ResultMargin(history)], - scroll_offsets=ScrollOffsets(top=2, bottom=2)), - ]), - floats=[ - # Help text as a float. - Float(width=60, top=3, bottom=2, - content=ConditionalContainer( - content=help_window, filter=has_focus(history.help_buffer))), - ] - ), - # Bottom toolbars. - ArgToolbar(), - search_toolbar, - Window( - content=FormattedTextControl( - partial(_get_bottom_toolbar_fragments, history=history)), - style='class:status-toolbar'), - ]) + scroll_offsets=ScrollOffsets(top=2, bottom=2), + ) + + self.root_container = HSplit( + [ + # Top title bar. + Window( + content=FormattedTextControl(_get_top_toolbar_fragments), + align=WindowAlign.CENTER, + style="class:status-toolbar", + ), + FloatContainer( + content=VSplit( + [ + # Left side: history. + history_window, + # Separator. + Window( + width=D.exact(1), + char=BORDER.LIGHT_VERTICAL, + style="class:separator", + ), + # Right side: result. + Window( + content=self.default_buffer_control, + wrap_lines=False, + left_margins=[ResultMargin(history)], + scroll_offsets=ScrollOffsets(top=2, bottom=2), + ), + ] + ), + floats=[ + # Help text as a float. + Float( + width=60, + top=3, + bottom=2, + content=ConditionalContainer( + content=help_window, + filter=has_focus(history.help_buffer), + ), + ) + ], + ), + # Bottom toolbars. + ArgToolbar(), + search_toolbar, + Window( + content=FormattedTextControl( + partial(_get_bottom_toolbar_fragments, history=history) + ), + style="class:status-toolbar", + ), + ] + ) self.layout = Layout(self.root_container, history_window) def _get_top_toolbar_fragments(): - return [('class:status-bar.title', 'History browser - Insert from history')] + return [("class:status-bar.title", "History browser - Insert from history")] def _get_bottom_toolbar_fragments(history): python_input = history.python_input + @if_mousedown def f1(mouse_event): _toggle_help(history) @@ -194,18 +216,21 @@ def f1(mouse_event): def tab(mouse_event): _select_other_window(history) - return [ - ('class:status-toolbar', ' ') ] + get_inputmode_fragments(python_input) + [ - ('class:status-toolbar', ' '), - ('class:status-toolbar.key', '[Space]'), - ('class:status-toolbar', ' Toggle '), - ('class:status-toolbar.key', '[Tab]', tab), - ('class:status-toolbar', ' Focus ', tab), - ('class:status-toolbar.key', '[Enter]'), - ('class:status-toolbar', ' Accept '), - ('class:status-toolbar.key', '[F1]', f1), - ('class:status-toolbar', ' Help ', f1), - ] + return ( + [("class:status-toolbar", " ")] + + get_inputmode_fragments(python_input) + + [ + ("class:status-toolbar", " "), + ("class:status-toolbar.key", "[Space]"), + ("class:status-toolbar", " Toggle "), + ("class:status-toolbar.key", "[Tab]", tab), + ("class:status-toolbar", " Focus ", tab), + ("class:status-toolbar.key", "[Enter]"), + ("class:status-toolbar", " Accept "), + ("class:status-toolbar.key", "[F1]", f1), + ("class:status-toolbar", " Help ", f1), + ] + ) class HistoryMargin(Margin): @@ -213,6 +238,7 @@ class HistoryMargin(Margin): Margin for the history buffer. This displays a green bar for the selected entries. """ + def __init__(self, history): self.history_buffer = history.history_buffer self.history_mapping = history.history_mapping @@ -237,20 +263,20 @@ def create_margin(self, window_render_info, width, height): # Show stars at the start of each entry. # (Visualises multiline entries.) if line_number in lines_starting_new_entries: - char = '*' + char = "*" else: - char = ' ' + char = " " if line_number in selected_lines: - t = 'class:history-line,selected' + t = "class:history-line,selected" else: - t = 'class:history-line' + t = "class:history-line" if line_number == current_lineno: - t = t + ',current' + t = t + ",current" result.append((t, char)) - result.append(('', '\n')) + result.append(("", "\n")) return result @@ -259,6 +285,7 @@ class ResultMargin(Margin): """ The margin to be shown in the result pane. """ + def __init__(self, history): self.history_mapping = history.history_mapping self.history_buffer = history.history_buffer @@ -270,7 +297,9 @@ def create_margin(self, window_render_info, width, height): document = self.history_buffer.document current_lineno = document.cursor_position_row - offset = self.history_mapping.result_line_offset #original_document.cursor_position_row + offset = ( + self.history_mapping.result_line_offset + ) # original_document.cursor_position_row visible_line_to_input_line = window_render_info.visible_line_to_input_line @@ -279,16 +308,19 @@ def create_margin(self, window_render_info, width, height): for y in range(height): line_number = visible_line_to_input_line.get(y) - if (line_number is None or line_number < offset or - line_number >= offset + len(self.history_mapping.selected_lines)): - t = '' + if ( + line_number is None + or line_number < offset + or line_number >= offset + len(self.history_mapping.selected_lines) + ): + t = "" elif line_number == current_lineno: - t = 'class:history-line,selected,current' + t = "class:history-line,selected,current" else: - t = 'class:history-line,selected' + t = "class:history-line,selected" - result.append((t, ' ')) - result.append(('', '\n')) + result.append((t, " ")) + result.append(("", "\n")) return result @@ -300,26 +332,31 @@ class GrayExistingText(Processor): """ Turn the existing input, before and after the inserted code gray. """ + def __init__(self, history_mapping): self.history_mapping = history_mapping - self._lines_before = len(history_mapping.original_document.text_before_cursor.splitlines()) + self._lines_before = len( + history_mapping.original_document.text_before_cursor.splitlines() + ) def apply_transformation(self, transformation_input): lineno = transformation_input.lineno fragments = transformation_input.fragments - if (lineno < self._lines_before or - lineno >= self._lines_before + len(self.history_mapping.selected_lines)): + if lineno < self._lines_before or lineno >= self._lines_before + len( + self.history_mapping.selected_lines + ): text = fragment_list_to_text(fragments) - return Transformation(fragments=[('class:history.existing-input', text)]) + return Transformation(fragments=[("class:history.existing-input", text)]) else: return Transformation(fragments=fragments) -class HistoryMapping(object): +class HistoryMapping: """ Keep a list of all the lines from the history and the selected lines. """ + def __init__(self, history, python_history, original_document): self.history = history self.python_history = python_history @@ -339,10 +376,12 @@ def __init__(self, history, python_history, original_document): history_lines.append(line) if len(history_strings) > HISTORY_COUNT: - history_lines[0] = '# *** History has been truncated to %s lines ***' % HISTORY_COUNT + history_lines[0] = ( + "# *** History has been truncated to %s lines ***" % HISTORY_COUNT + ) self.history_lines = history_lines - self.concatenated_history = '\n'.join(history_lines) + self.concatenated_history = "\n".join(history_lines) # Line offset. if self.original_document.text_before_cursor: @@ -369,7 +408,7 @@ def get_new_document(self, cursor_pos=None): lines.append(self.original_document.text_after_cursor) # Create `Document` with cursor at the right position. - text = '\n'.join(lines) + text = "\n".join(lines) if cursor_pos is not None and cursor_pos > len(text): cursor_pos = len(text) return Document(text, cursor_pos) @@ -377,8 +416,7 @@ def get_new_document(self, cursor_pos=None): def update_default_buffer(self): b = self.history.default_buffer - b.set_document( - self.get_new_document(b.cursor_position), bypass_readonly=True) + b.set_document(self.get_new_document(b.cursor_position), bypass_readonly=True) def _toggle_help(history): @@ -410,7 +448,7 @@ def create_key_bindings(history, python_input, history_mapping): bindings = KeyBindings() handle = bindings.add - @handle(' ', filter=has_focus(history.history_buffer)) + @handle(" ", filter=has_focus(history.history_buffer)) def _(event): """ Space: select/deselect line from history pane. @@ -433,18 +471,21 @@ def _(event): # Update cursor position default_buffer = history.default_buffer - default_lineno = sorted(history_mapping.selected_lines).index(line_no) + \ - history_mapping.result_line_offset - default_buffer.cursor_position = \ - default_buffer.document.translate_row_col_to_index(default_lineno, 0) + default_lineno = ( + sorted(history_mapping.selected_lines).index(line_no) + + history_mapping.result_line_offset + ) + default_buffer.cursor_position = default_buffer.document.translate_row_col_to_index( + default_lineno, 0 + ) # Also move the cursor to the next line. (This way they can hold # space to select a region.) b.cursor_position = b.document.translate_row_col_to_index(line_no + 1, 0) - @handle(' ', filter=has_focus(DEFAULT_BUFFER)) - @handle('delete', filter=has_focus(DEFAULT_BUFFER)) - @handle('c-h', filter=has_focus(DEFAULT_BUFFER)) + @handle(" ", filter=has_focus(DEFAULT_BUFFER)) + @handle("delete", filter=has_focus(DEFAULT_BUFFER)) + @handle("c-h", filter=has_focus(DEFAULT_BUFFER)) def _(event): """ Space: remove line from default pane. @@ -463,50 +504,52 @@ def _(event): history_mapping.update_default_buffer() help_focussed = has_focus(history.help_buffer) - main_buffer_focussed = has_focus(history.history_buffer) | has_focus(history.default_buffer) - - @handle('tab', filter=main_buffer_focussed) - @handle('c-x', filter=main_buffer_focussed, eager=True) - # Eager: ignore the Emacs [Ctrl-X Ctrl-X] binding. - @handle('c-w', filter=main_buffer_focussed) + main_buffer_focussed = has_focus(history.history_buffer) | has_focus( + history.default_buffer + ) + + @handle("tab", filter=main_buffer_focussed) + @handle("c-x", filter=main_buffer_focussed, eager=True) + # Eager: ignore the Emacs [Ctrl-X Ctrl-X] binding. + @handle("c-w", filter=main_buffer_focussed) def _(event): " Select other window. " _select_other_window(history) - @handle('f4') + @handle("f4") def _(event): " Switch between Emacs/Vi mode. " python_input.vi_mode = not python_input.vi_mode - @handle('f1') + @handle("f1") def _(event): " Display/hide help. " _toggle_help(history) - @handle('enter', filter=help_focussed) - @handle('c-c', filter=help_focussed) - @handle('c-g', filter=help_focussed) - @handle('escape', filter=help_focussed) + @handle("enter", filter=help_focussed) + @handle("c-c", filter=help_focussed) + @handle("c-g", filter=help_focussed) + @handle("escape", filter=help_focussed) def _(event): " Leave help. " event.app.layout.focus_previous() - @handle('q', filter=main_buffer_focussed) - @handle('f3', filter=main_buffer_focussed) - @handle('c-c', filter=main_buffer_focussed) - @handle('c-g', filter=main_buffer_focussed) + @handle("q", filter=main_buffer_focussed) + @handle("f3", filter=main_buffer_focussed) + @handle("c-c", filter=main_buffer_focussed) + @handle("c-g", filter=main_buffer_focussed) def _(event): " Cancel and go back. " event.app.exit(result=None) - @handle('enter', filter=main_buffer_focussed) + @handle("enter", filter=main_buffer_focussed) def _(event): " Accept input. " event.app.exit(result=history.default_buffer.text) enable_system_bindings = Condition(lambda: python_input.enable_system_bindings) - @handle('c-z', filter=enable_system_bindings) + @handle("c-z", filter=enable_system_bindings) def _(event): " Suspend to background. " event.app.suspend_to_background() @@ -514,7 +557,7 @@ def _(event): return bindings -class History(object): +class PythonHistory: def __init__(self, python_input, original_document): """ Create an `Application` for the history screen. @@ -530,26 +573,28 @@ def __init__(self, python_input, original_document): document = Document(history_mapping.concatenated_history) document = Document( document.text, - cursor_position=document.cursor_position + document.get_start_of_line_position()) + cursor_position=document.cursor_position + + document.get_start_of_line_position(), + ) self.history_buffer = Buffer( document=document, on_cursor_position_changed=self._history_buffer_pos_changed, accept_handler=( - lambda buff: get_app().exit(result=self.default_buffer.text)), - read_only=True) + lambda buff: get_app().exit(result=self.default_buffer.text) + ), + read_only=True, + ) self.default_buffer = Buffer( name=DEFAULT_BUFFER, document=history_mapping.get_new_document(), on_cursor_position_changed=self._default_buffer_pos_changed, - read_only=True) - - self.help_buffer = Buffer( - document=Document(HELP_TEXT, 0), - read_only=True + read_only=True, ) + self.help_buffer = Buffer(document=Document(HELP_TEXT, 0), read_only=True) + self.history_layout = HistoryLayout(self) self.app = Application( @@ -557,7 +602,7 @@ def __init__(self, python_input, original_document): full_screen=True, style=python_input._current_style, mouse_support=Condition(lambda: python_input.enable_mouse_support), - key_bindings=create_key_bindings(self, python_input, history_mapping) + key_bindings=create_key_bindings(self, python_input, history_mapping), ) def _default_buffer_pos_changed(self, _): @@ -566,8 +611,10 @@ def _default_buffer_pos_changed(self, _): # Only when this buffer has the focus. if self.app.current_buffer == self.default_buffer: try: - line_no = self.default_buffer.document.cursor_position_row - \ - self.history_mapping.result_line_offset + line_no = ( + self.default_buffer.document.cursor_position_row + - self.history_mapping.result_line_offset + ) if line_no < 0: # When the cursor is above the inserted region. raise IndexError @@ -576,8 +623,9 @@ def _default_buffer_pos_changed(self, _): except IndexError: pass else: - self.history_buffer.cursor_position = \ - self.history_buffer.document.translate_row_col_to_index(history_lineno, 0) + self.history_buffer.cursor_position = self.history_buffer.document.translate_row_col_to_index( + history_lineno, 0 + ) def _history_buffer_pos_changed(self, _): """ When the cursor changes in the history buffer. Synchronize. """ @@ -586,9 +634,11 @@ def _history_buffer_pos_changed(self, _): line_no = self.history_buffer.document.cursor_position_row if line_no in self.history_mapping.selected_lines: - default_lineno = sorted(self.history_mapping.selected_lines).index(line_no) + \ - self.history_mapping.result_line_offset - - self.default_buffer.cursor_position = \ - self.default_buffer.document.translate_row_col_to_index(default_lineno, 0) - + default_lineno = ( + sorted(self.history_mapping.selected_lines).index(line_no) + + self.history_mapping.result_line_offset + ) + + self.default_buffer.cursor_position = self.default_buffer.document.translate_row_col_to_index( + default_lineno, 0 + ) diff --git a/ptpython/ipython.py b/ptpython/ipython.py index 8cc5a36e..20f29bdc 100644 --- a/ptpython/ipython.py +++ b/ptpython/ipython.py @@ -8,10 +8,12 @@ offer. """ -from __future__ import unicode_literals, print_function - -from prompt_toolkit.completion import Completion, Completer -from prompt_toolkit.completion import PathCompleter, WordCompleter +from prompt_toolkit.completion import ( + Completer, + Completion, + PathCompleter, + WordCompleter, +) from prompt_toolkit.contrib.completers import SystemCompleter from prompt_toolkit.contrib.regular_languages.compiler import compile from prompt_toolkit.contrib.regular_languages.completion import GrammarCompleter @@ -20,27 +22,25 @@ from prompt_toolkit.formatted_text import PygmentsTokens from prompt_toolkit.lexers import PygmentsLexer, SimpleLexer from prompt_toolkit.styles import Style +from pygments.lexers import BashLexer, PythonLexer -from .python_input import PythonInput, PythonValidator, PythonCompleter -from .style import default_ui_style - -from IPython.terminal.embed import InteractiveShellEmbed as _InteractiveShellEmbed -from IPython.terminal.ipapp import load_default_config from IPython import utils as ipy_utils from IPython.core.inputsplitter import IPythonInputSplitter - -from pygments.lexers import PythonLexer, BashLexer +from IPython.terminal.embed import InteractiveShellEmbed as _InteractiveShellEmbed +from IPython.terminal.ipapp import load_default_config from ptpython.prompt_style import PromptStyle -__all__ = ( - 'embed', -) +from .python_input import PythonCompleter, PythonInput, PythonValidator +from .style import default_ui_style + +__all__ = ["embed"] class IPythonPrompt(PromptStyle): """ Style for IPython >5.0, use the prompt_toolkit tokens directly. """ + def __init__(self, prompts): self.prompts = prompts @@ -68,7 +68,8 @@ def create_ipython_grammar(): """ Return compiled IPython grammar. """ - return compile(r""" + return compile( + r""" \s* ( (?P%)( @@ -87,24 +88,37 @@ def create_ipython_grammar(): (?![%!]) (?P.+) ) \s* - """) + """ + ) -def create_completer(get_globals, get_locals, magics_manager, alias_manager, - get_enable_dictionary_completion): +def create_completer( + get_globals, + get_locals, + magics_manager, + alias_manager, + get_enable_dictionary_completion, +): g = create_ipython_grammar() - return GrammarCompleter(g, { - 'python': PythonCompleter(get_globals, get_locals, get_enable_dictionary_completion), - 'magic': MagicsCompleter(magics_manager), - 'alias_name': AliasCompleter(alias_manager), - 'pdb_arg': WordCompleter(['on', 'off'], ignore_case=True), - 'autocall_arg': WordCompleter(['0', '1', '2'], ignore_case=True), - 'py_filename': PathCompleter(only_directories=False, file_filter=lambda name: name.endswith('.py')), - 'filename': PathCompleter(only_directories=False), - 'directory': PathCompleter(only_directories=True), - 'system': SystemCompleter(), - }) + return GrammarCompleter( + g, + { + "python": PythonCompleter( + get_globals, get_locals, get_enable_dictionary_completion + ), + "magic": MagicsCompleter(magics_manager), + "alias_name": AliasCompleter(alias_manager), + "pdb_arg": WordCompleter(["on", "off"], ignore_case=True), + "autocall_arg": WordCompleter(["0", "1", "2"], ignore_case=True), + "py_filename": PathCompleter( + only_directories=False, file_filter=lambda name: name.endswith(".py") + ), + "filename": PathCompleter(only_directories=False), + "directory": PathCompleter(only_directories=True), + "system": SystemCompleter(), + }, + ) def create_lexer(): @@ -113,12 +127,13 @@ def create_lexer(): return GrammarLexer( g, lexers={ - 'percent': SimpleLexer('class:pygments.operator'), - 'magic': SimpleLexer('class:pygments.keyword'), - 'filename': SimpleLexer('class:pygments.name'), - 'python': PygmentsLexer(PythonLexer), - 'system': PygmentsLexer(BashLexer), - }) + "percent": SimpleLexer("class:pygments.operator"), + "magic": SimpleLexer("class:pygments.keyword"), + "filename": SimpleLexer("class:pygments.name"), + "python": PygmentsLexer(PythonLexer), + "system": PygmentsLexer(BashLexer), + }, + ) class MagicsCompleter(Completer): @@ -128,9 +143,9 @@ def __init__(self, magics_manager): def get_completions(self, document, complete_event): text = document.text_before_cursor.lstrip() - for m in sorted(self.magics_manager.magics['line']): + for m in sorted(self.magics_manager.magics["line"]): if m.startswith(text): - yield Completion('%s' % m, -len(text)) + yield Completion("%s" % m, -len(text)) class AliasCompleter(Completer): @@ -139,48 +154,50 @@ def __init__(self, alias_manager): def get_completions(self, document, complete_event): text = document.text_before_cursor.lstrip() - #aliases = [a for a, _ in self.alias_manager.aliases] + # aliases = [a for a, _ in self.alias_manager.aliases] aliases = self.alias_manager.aliases for a, cmd in sorted(aliases, key=lambda a: a[0]): if a.startswith(text): - yield Completion('%s' % a, -len(text), - display_meta=cmd) + yield Completion("%s" % a, -len(text), display_meta=cmd) class IPythonInput(PythonInput): """ Override our `PythonCommandLineInterface` to add IPython specific stuff. """ + def __init__(self, ipython_shell, *a, **kw): - kw['_completer'] = create_completer(kw['get_globals'], kw['get_globals'], - ipython_shell.magics_manager, - ipython_shell.alias_manager, - lambda: self.enable_dictionary_completion) - kw['_lexer'] = create_lexer() - kw['_validator'] = IPythonValidator( - get_compiler_flags=self.get_compiler_flags) - - super(IPythonInput, self).__init__(*a, **kw) + kw["_completer"] = create_completer( + kw["get_globals"], + kw["get_globals"], + ipython_shell.magics_manager, + ipython_shell.alias_manager, + lambda: self.enable_dictionary_completion, + ) + kw["_lexer"] = create_lexer() + kw["_validator"] = IPythonValidator(get_compiler_flags=self.get_compiler_flags) + + super().__init__(*a, **kw) self.ipython_shell = ipython_shell - self.all_prompt_styles['ipython'] = IPythonPrompt(ipython_shell.prompts) - self.prompt_style = 'ipython' + self.all_prompt_styles["ipython"] = IPythonPrompt(ipython_shell.prompts) + self.prompt_style = "ipython" # UI style for IPython. Add tokens that are used by IPython>5.0 style_dict = {} style_dict.update(default_ui_style) - style_dict.update({ - 'pygments.prompt': '#009900', - 'pygments.prompt-num': '#00ff00 bold', - 'pygments.out-prompt': '#990000', - 'pygments.out-prompt-num': '#ff0000 bold', - }) + style_dict.update( + { + "pygments.prompt": "#009900", + "pygments.prompt-num": "#00ff00 bold", + "pygments.out-prompt": "#990000", + "pygments.out-prompt-num": "#ff0000 bold", + } + ) - self.ui_styles = { - 'default': Style.from_dict(style_dict), - } - self.use_ui_colorscheme('default') + self.ui_styles = {"default": Style.from_dict(style_dict)} + self.use_ui_colorscheme("default") class InteractiveShellEmbed(_InteractiveShellEmbed): @@ -190,31 +207,34 @@ class InteractiveShellEmbed(_InteractiveShellEmbed): :param configure: Callable for configuring the repl. """ + def __init__(self, *a, **kw): - vi_mode = kw.pop('vi_mode', False) - history_filename = kw.pop('history_filename', None) - configure = kw.pop('configure', None) - title = kw.pop('title', None) + vi_mode = kw.pop("vi_mode", False) + history_filename = kw.pop("history_filename", None) + configure = kw.pop("configure", None) + title = kw.pop("title", None) # Don't ask IPython to confirm for exit. We have our own exit prompt. self.confirm_exit = False - super(InteractiveShellEmbed, self).__init__(*a, **kw) + super().__init__(*a, **kw) def get_globals(): return self.user_ns python_input = IPythonInput( self, - get_globals=get_globals, vi_mode=vi_mode, - history_filename=history_filename) + get_globals=get_globals, + vi_mode=vi_mode, + history_filename=history_filename, + ) if title: python_input.terminal_title = title if configure: configure(python_input) - python_input.prompt_style = 'ipython' # Don't take from config. + python_input.prompt_style = "ipython" # Don't take from config. self.python_input = python_input @@ -223,7 +243,7 @@ def prompt_for_code(self): return self.python_input.app.run() except KeyboardInterrupt: self.python_input.default_buffer.document = Document() - return '' + return "" def initialize_extensions(shell, extensions): @@ -240,8 +260,10 @@ def initialize_extensions(shell, extensions): shell.extension_manager.load_extension(ext) except: ipy_utils.warn.warn( - "Error in loading extension: %s" % ext + - "\nCheck your config files in %s" % ipy_utils.path.get_ipython_dir()) + "Error in loading extension: %s" % ext + + "\nCheck your config files in %s" + % ipy_utils.path.get_ipython_dir() + ) shell.showtraceback() @@ -249,13 +271,13 @@ def embed(**kwargs): """ Copied from `IPython/terminal/embed.py`, but using our `InteractiveShellEmbed` instead. """ - config = kwargs.get('config') - header = kwargs.pop('header', u'') - compile_flags = kwargs.pop('compile_flags', None) + config = kwargs.get("config") + header = kwargs.pop("header", "") + compile_flags = kwargs.pop("compile_flags", None) if config is None: config = load_default_config() config.InteractiveShellEmbed = config.TerminalInteractiveShell - kwargs['config'] = config + kwargs["config"] = config shell = InteractiveShellEmbed.instance(**kwargs) - initialize_extensions(shell, config['InteractiveShellApp']['extensions']) + initialize_extensions(shell, config["InteractiveShellApp"]["extensions"]) shell(header=header, stack_depth=2, compile_flags=compile_flags) diff --git a/ptpython/key_bindings.py b/ptpython/key_bindings.py index 001f59b9..1740caf7 100644 --- a/ptpython/key_bindings.py +++ b/ptpython/key_bindings.py @@ -1,18 +1,24 @@ -from __future__ import unicode_literals - +from prompt_toolkit.application import get_app from prompt_toolkit.document import Document from prompt_toolkit.enums import DEFAULT_BUFFER -from prompt_toolkit.filters import has_selection, has_focus, Condition, vi_insert_mode, emacs_insert_mode, emacs_mode +from prompt_toolkit.filters import ( + Condition, + emacs_insert_mode, + emacs_mode, + has_focus, + has_selection, + vi_insert_mode, +) from prompt_toolkit.key_binding import KeyBindings from prompt_toolkit.keys import Keys -from prompt_toolkit.application import get_app + from .utils import document_is_multiline_python -__all__ = ( - 'load_python_bindings', - 'load_sidebar_bindings', - 'load_confirm_exit_bindings', -) +__all__ = [ + "load_python_bindings", + "load_sidebar_bindings", + "load_confirm_exit_bindings", +] @Condition @@ -40,14 +46,14 @@ def load_python_bindings(python_input): sidebar_visible = Condition(lambda: python_input.show_sidebar) handle = bindings.add - @handle('c-l') + @handle("c-l") def _(event): """ Clear whole screen and render again -- also when the sidebar is visible. """ event.app.renderer.clear() - @handle('c-z') + @handle("c-z") def _(event): """ Suspend. @@ -55,7 +61,7 @@ def _(event): if python_input.enable_system_bindings: event.app.suspend_to_background() - @handle('f2') + @handle("f2") def _(event): """ Show/hide sidebar. @@ -66,42 +72,49 @@ def _(event): else: event.app.layout.focus_last() - @handle('f3') + @handle("f3") def _(event): """ Select from the history. """ python_input.enter_history() - @handle('f4') + @handle("f4") def _(event): """ Toggle between Vi and Emacs mode. """ python_input.vi_mode = not python_input.vi_mode - @handle('f6') + @handle("f6") def _(event): """ Enable/Disable paste mode. """ python_input.paste_mode = not python_input.paste_mode - @handle('tab', filter= ~sidebar_visible & ~has_selection & tab_should_insert_whitespace) + @handle( + "tab", filter=~sidebar_visible & ~has_selection & tab_should_insert_whitespace + ) def _(event): """ When tab should insert whitespace, do that instead of completion. """ - event.app.current_buffer.insert_text(' ') + event.app.current_buffer.insert_text(" ") @Condition def is_multiline(): return document_is_multiline_python(python_input.default_buffer.document) - @handle('enter', filter= ~sidebar_visible & ~has_selection & - (vi_insert_mode | emacs_insert_mode) & - has_focus(DEFAULT_BUFFER) & ~is_multiline) - @handle(Keys.Escape, Keys.Enter, filter= ~sidebar_visible & emacs_mode) + @handle( + "enter", + filter=~sidebar_visible + & ~has_selection + & (vi_insert_mode | emacs_insert_mode) + & has_focus(DEFAULT_BUFFER) + & ~is_multiline, + ) + @handle(Keys.Escape, Keys.Enter, filter=~sidebar_visible & emacs_mode) def _(event): """ Accept input (for single line input). @@ -112,14 +125,19 @@ def _(event): # When the cursor is at the end, and we have an empty line: # drop the empty lines, but return the value. b.document = Document( - text=b.text.rstrip(), - cursor_position=len(b.text.rstrip())) + text=b.text.rstrip(), cursor_position=len(b.text.rstrip()) + ) b.validate_and_handle() - @handle('enter', filter= ~sidebar_visible & ~has_selection & - (vi_insert_mode | emacs_insert_mode) & - has_focus(DEFAULT_BUFFER) & is_multiline) + @handle( + "enter", + filter=~sidebar_visible + & ~has_selection + & (vi_insert_mode | emacs_insert_mode) + & has_focus(DEFAULT_BUFFER) + & is_multiline, + ) def _(event): """ Behaviour of the Enter key. @@ -134,30 +152,36 @@ def at_the_end(b): """ we consider the cursor at the end when there is no text after the cursor, or only whitespace. """ text = b.document.text_after_cursor - return text == '' or (text.isspace() and not '\n' in text) + return text == "" or (text.isspace() and not "\n" in text) if python_input.paste_mode: # In paste mode, always insert text. - b.insert_text('\n') + b.insert_text("\n") - elif at_the_end(b) and b.document.text.replace(' ', '').endswith( - '\n' * (empty_lines_required - 1)): + elif at_the_end(b) and b.document.text.replace(" ", "").endswith( + "\n" * (empty_lines_required - 1) + ): # When the cursor is at the end, and we have an empty line: # drop the empty lines, but return the value. if b.validate(): b.document = Document( - text=b.text.rstrip(), - cursor_position=len(b.text.rstrip())) + text=b.text.rstrip(), cursor_position=len(b.text.rstrip()) + ) b.validate_and_handle() else: auto_newline(b) - @handle('c-d', filter=~sidebar_visible & - has_focus(python_input.default_buffer) & - Condition(lambda: - # The current buffer is empty. - not get_app().current_buffer.text)) + @handle( + "c-d", + filter=~sidebar_visible + & has_focus(python_input.default_buffer) + & Condition( + lambda: + # The current buffer is empty. + not get_app().current_buffer.text + ), + ) def _(event): """ Override Control-D exit, to ask for confirmation. @@ -167,10 +191,10 @@ def _(event): else: event.app.exit(exception=EOFError) - @handle('c-c', filter=has_focus(python_input.default_buffer)) + @handle("c-c", filter=has_focus(python_input.default_buffer)) def _(event): " Abort when Control-C has been pressed. " - event.app.exit(exception=KeyboardInterrupt, style='class:aborting') + event.app.exit(exception=KeyboardInterrupt, style="class:aborting") return bindings @@ -184,42 +208,44 @@ def load_sidebar_bindings(python_input): handle = bindings.add sidebar_visible = Condition(lambda: python_input.show_sidebar) - @handle('up', filter=sidebar_visible) - @handle('c-p', filter=sidebar_visible) - @handle('k', filter=sidebar_visible) + @handle("up", filter=sidebar_visible) + @handle("c-p", filter=sidebar_visible) + @handle("k", filter=sidebar_visible) def _(event): " Go to previous option. " python_input.selected_option_index = ( - (python_input.selected_option_index - 1) % python_input.option_count) + python_input.selected_option_index - 1 + ) % python_input.option_count - @handle('down', filter=sidebar_visible) - @handle('c-n', filter=sidebar_visible) - @handle('j', filter=sidebar_visible) + @handle("down", filter=sidebar_visible) + @handle("c-n", filter=sidebar_visible) + @handle("j", filter=sidebar_visible) def _(event): " Go to next option. " python_input.selected_option_index = ( - (python_input.selected_option_index + 1) % python_input.option_count) + python_input.selected_option_index + 1 + ) % python_input.option_count - @handle('right', filter=sidebar_visible) - @handle('l', filter=sidebar_visible) - @handle(' ', filter=sidebar_visible) + @handle("right", filter=sidebar_visible) + @handle("l", filter=sidebar_visible) + @handle(" ", filter=sidebar_visible) def _(event): " Select next value for current option. " option = python_input.selected_option option.activate_next() - @handle('left', filter=sidebar_visible) - @handle('h', filter=sidebar_visible) + @handle("left", filter=sidebar_visible) + @handle("h", filter=sidebar_visible) def _(event): " Select previous value for current option. " option = python_input.selected_option option.activate_previous() - @handle('c-c', filter=sidebar_visible) - @handle('c-d', filter=sidebar_visible) - @handle('c-d', filter=sidebar_visible) - @handle('enter', filter=sidebar_visible) - @handle('escape', filter=sidebar_visible) + @handle("c-c", filter=sidebar_visible) + @handle("c-d", filter=sidebar_visible) + @handle("c-d", filter=sidebar_visible) + @handle("enter", filter=sidebar_visible) + @handle("escape", filter=sidebar_visible) def _(event): " Hide sidebar. " python_input.show_sidebar = False @@ -237,15 +263,15 @@ def load_confirm_exit_bindings(python_input): handle = bindings.add confirmation_visible = Condition(lambda: python_input.show_exit_confirmation) - @handle('y', filter=confirmation_visible) - @handle('Y', filter=confirmation_visible) - @handle('enter', filter=confirmation_visible) - @handle('c-d', filter=confirmation_visible) + @handle("y", filter=confirmation_visible) + @handle("Y", filter=confirmation_visible) + @handle("enter", filter=confirmation_visible) + @handle("c-d", filter=confirmation_visible) def _(event): """ Really quit. """ - event.app.exit(exception=EOFError, style='class:exiting') + event.app.exit(exception=EOFError, style="class:exiting") @handle(Keys.Any, filter=confirmation_visible) def _(event): @@ -265,14 +291,14 @@ def auto_newline(buffer): if buffer.document.current_line_after_cursor: # When we are in the middle of a line. Always insert a newline. - insert_text('\n') + insert_text("\n") else: # Go to new line, but also add indentation. current_line = buffer.document.current_line_before_cursor.rstrip() - insert_text('\n') + insert_text("\n") # Unident if the last line ends with 'pass', remove four spaces. - unindent = current_line.rstrip().endswith(' pass') + unindent = current_line.rstrip().endswith(" pass") # Copy whitespace from current line current_line2 = current_line[4:] if unindent else current_line @@ -284,6 +310,6 @@ def auto_newline(buffer): break # If the last line ends with a colon, add four extra spaces. - if current_line[-1:] == ':': + if current_line[-1:] == ":": for x in range(4): - insert_text(' ') + insert_text(" ") diff --git a/ptpython/layout.py b/ptpython/layout.py index 3cc230f0..7b68b2d4 100644 --- a/ptpython/layout.py +++ b/ptpython/layout.py @@ -1,120 +1,147 @@ """ Creation of the `Layout` instance for the Python input/REPL. """ -from __future__ import unicode_literals +import platform +import sys +from enum import Enum +from typing import TYPE_CHECKING, Optional from prompt_toolkit.application import get_app from prompt_toolkit.enums import DEFAULT_BUFFER, SEARCH_BUFFER -from prompt_toolkit.filters import is_done, has_completions, renderer_height_is_known, has_focus, Condition +from prompt_toolkit.filters import ( + Condition, + has_completions, + has_focus, + is_done, + renderer_height_is_known, +) from prompt_toolkit.formatted_text import fragment_list_width, to_formatted_text +from prompt_toolkit.formatted_text.base import StyleAndTextTuples from prompt_toolkit.key_binding.vi_state import InputMode -from prompt_toolkit.layout.containers import Window, HSplit, VSplit, FloatContainer, Float, ConditionalContainer, ScrollOffsets +from prompt_toolkit.layout.containers import ( + ConditionalContainer, + Container, + Float, + FloatContainer, + HSplit, + ScrollOffsets, + VSplit, + Window, +) from prompt_toolkit.layout.controls import BufferControl, FormattedTextControl -from prompt_toolkit.layout.dimension import Dimension +from prompt_toolkit.layout.dimension import AnyDimension, Dimension from prompt_toolkit.layout.layout import Layout from prompt_toolkit.layout.margins import PromptMargin from prompt_toolkit.layout.menus import CompletionsMenu, MultiColumnCompletionsMenu -from prompt_toolkit.layout.processors import ConditionalProcessor, AppendAutoSuggestion, HighlightIncrementalSearchProcessor, HighlightSelectionProcessor, HighlightMatchingBracketProcessor, Processor, Transformation +from prompt_toolkit.layout.processors import ( + AppendAutoSuggestion, + ConditionalProcessor, + DisplayMultipleCursors, + HighlightIncrementalSearchProcessor, + HighlightMatchingBracketProcessor, + HighlightSelectionProcessor, +) from prompt_toolkit.lexers import SimpleLexer +from prompt_toolkit.mouse_events import MouseEvent from prompt_toolkit.selection import SelectionType -from prompt_toolkit.widgets.toolbars import CompletionsToolbar, ArgToolbar, SearchToolbar, ValidationToolbar, SystemToolbar - -from .filters import HasSignature, ShowSidebar, ShowSignature, ShowDocstring -from .utils import if_mousedown - -from pygments.lexers import PythonLexer - -import platform -import sys - -__all__ = ( - 'PtPythonLayout', - 'CompletionVisualisation', +from prompt_toolkit.widgets.toolbars import ( + ArgToolbar, + CompletionsToolbar, + SearchToolbar, + SystemToolbar, + ValidationToolbar, ) +from pygments.lexers import PythonLexer +from .filters import HasSignature, ShowDocstring, ShowSidebar, ShowSignature +from .utils import if_mousedown -# DisplayMultipleCursors: Only for prompt_toolkit>=1.0.8 -try: - from prompt_toolkit.layout.processors import DisplayMultipleCursors -except ImportError: - class DisplayMultipleCursors(Processor): - " Dummy. " - def __init__(self, *a): - pass +if TYPE_CHECKING: + from .python_input import PythonInput, OptionCategory - def apply_transformation(self, document, lineno, - source_to_display, tokens): - return Transformation(tokens) +__all__ = ["PtPythonLayout", "CompletionVisualisation"] -class CompletionVisualisation: +class CompletionVisualisation(Enum): " Visualisation method for the completions. " - NONE = 'none' - POP_UP = 'pop-up' - MULTI_COLUMN = 'multi-column' - TOOLBAR = 'toolbar' + NONE = "none" + POP_UP = "pop-up" + MULTI_COLUMN = "multi-column" + TOOLBAR = "toolbar" -def show_completions_toolbar(python_input): - return Condition(lambda: python_input.completion_visualisation == CompletionVisualisation.TOOLBAR) +def show_completions_toolbar(python_input: "PythonInput") -> Condition: + return Condition( + lambda: python_input.completion_visualisation == CompletionVisualisation.TOOLBAR + ) -def show_completions_menu(python_input): - return Condition(lambda: python_input.completion_visualisation == CompletionVisualisation.POP_UP) +def show_completions_menu(python_input: "PythonInput") -> Condition: + return Condition( + lambda: python_input.completion_visualisation == CompletionVisualisation.POP_UP + ) -def show_multi_column_completions_menu(python_input): - return Condition(lambda: python_input.completion_visualisation == CompletionVisualisation.MULTI_COLUMN) +def show_multi_column_completions_menu(python_input: "PythonInput") -> Condition: + return Condition( + lambda: python_input.completion_visualisation + == CompletionVisualisation.MULTI_COLUMN + ) -def python_sidebar(python_input): +def python_sidebar(python_input: "PythonInput") -> Window: """ Create the `Layout` for the sidebar with the configurable options. """ - def get_text_fragments(): - tokens = [] - def append_category(category): - tokens.extend([ - ('class:sidebar', ' '), - ('class:sidebar.title', ' %-36s' % category.title), - ('class:sidebar', '\n'), - ]) + def get_text_fragments() -> StyleAndTextTuples: + tokens: StyleAndTextTuples = [] - def append(index, label, status): + def append_category(category: "OptionCategory") -> None: + tokens.extend( + [ + ("class:sidebar", " "), + ("class:sidebar.title", " %-36s" % category.title), + ("class:sidebar", "\n"), + ] + ) + + def append(index: int, label: str, status: str) -> None: selected = index == python_input.selected_option_index @if_mousedown - def select_item(mouse_event): + def select_item(mouse_event: MouseEvent) -> None: python_input.selected_option_index = index @if_mousedown - def goto_next(mouse_event): + def goto_next(mouse_event: MouseEvent) -> None: " Select item and go to next value. " python_input.selected_option_index = index option = python_input.selected_option option.activate_next() - sel = ',selected' if selected else '' + sel = ",selected" if selected else "" - tokens.append(('class:sidebar' + sel, ' >' if selected else ' ')) - tokens.append(('class:sidebar.label' + sel, '%-24s' % label, select_item)) - tokens.append(('class:sidebar.status' + sel, ' ', select_item)) - tokens.append(('class:sidebar.status' + sel, '%s' % status, goto_next)) + tokens.append(("class:sidebar" + sel, " >" if selected else " ")) + tokens.append(("class:sidebar.label" + sel, "%-24s" % label, select_item)) + tokens.append(("class:sidebar.status" + sel, " ", select_item)) + tokens.append(("class:sidebar.status" + sel, "%s" % status, goto_next)) if selected: - tokens.append(('[SetCursorPosition]', '')) + tokens.append(("[SetCursorPosition]", "")) - tokens.append(('class:sidebar.status' + sel, ' ' * (13 - len(status)), goto_next)) - tokens.append(('class:sidebar', '<' if selected else '')) - tokens.append(('class:sidebar', '\n')) + tokens.append( + ("class:sidebar.status" + sel, " " * (13 - len(status)), goto_next) + ) + tokens.append(("class:sidebar", "<" if selected else "")) + tokens.append(("class:sidebar", "\n")) i = 0 for category in python_input.options: append_category(category) for option in category.options: - append(i, option.title, '%s' % option.get_current_value()) + append(i, option.title, "%s" % option.get_current_value()) i += 1 tokens.pop() # Remove last newline. @@ -130,45 +157,44 @@ def move_cursor_up(self): return Window( Control(get_text_fragments), - style='class:sidebar', + style="class:sidebar", width=Dimension.exact(43), height=Dimension(min=3), - scroll_offsets=ScrollOffsets(top=1, bottom=1)) + scroll_offsets=ScrollOffsets(top=1, bottom=1), + ) def python_sidebar_navigation(python_input): """ Create the `Layout` showing the navigation information for the sidebar. """ - def get_text_fragments(): - tokens = [] + def get_text_fragments(): # Show navigation info. - tokens.extend([ - ('class:sidebar', ' '), - ('class:sidebar.key', '[Arrows]'), - ('class:sidebar', ' '), - ('class:sidebar.description', 'Navigate'), - ('class:sidebar', ' '), - ('class:sidebar.key', '[Enter]'), - ('class:sidebar', ' '), - ('class:sidebar.description', 'Hide menu'), - ]) - - return tokens + return [ + ("class:sidebar", " "), + ("class:sidebar.key", "[Arrows]"), + ("class:sidebar", " "), + ("class:sidebar.description", "Navigate"), + ("class:sidebar", " "), + ("class:sidebar.key", "[Enter]"), + ("class:sidebar", " "), + ("class:sidebar.description", "Hide menu"), + ] return Window( FormattedTextControl(get_text_fragments), - style='class:sidebar', + style="class:sidebar", width=Dimension.exact(43), - height=Dimension.exact(1)) + height=Dimension.exact(1), + ) def python_sidebar_help(python_input): """ Create the `Layout` for the help text for the current item in the sidebar. """ - token = 'class:sidebar.helptext' + token = "class:sidebar.helptext" def get_current_description(): """ @@ -180,33 +206,35 @@ def get_current_description(): if i == python_input.selected_option_index: return option.description i += 1 - return '' + return "" def get_help_text(): return [(token, get_current_description())] return ConditionalContainer( content=Window( - FormattedTextControl(get_help_text), - style=token, - height=Dimension(min=3)), - filter=ShowSidebar(python_input) & - Condition(lambda: python_input.show_sidebar_help) & ~is_done) + FormattedTextControl(get_help_text), style=token, height=Dimension(min=3) + ), + filter=ShowSidebar(python_input) + & Condition(lambda: python_input.show_sidebar_help) + & ~is_done, + ) def signature_toolbar(python_input): """ Return the `Layout` for the signature. """ + def get_text_fragments(): result = [] append = result.append - Signature = 'class:signature-toolbar' + Signature = "class:signature-toolbar" if python_input.signatures: sig = python_input.signatures[0] # Always take the first one. - append((Signature, ' ')) + append((Signature, " ")) try: append((Signature, sig.full_name)) except IndexError: @@ -214,7 +242,7 @@ def get_text_fragments(): # See also: https://github.com/davidhalter/jedi/issues/490 return [] - append((Signature + ',operator', '(')) + append((Signature + ",operator", "(")) try: enumerated_params = enumerate(sig.params) @@ -228,39 +256,45 @@ def get_text_fragments(): # and sig has no 'index' attribute. # See: https://github.com/jonathanslenders/ptpython/issues/47 # https://github.com/davidhalter/jedi/issues/598 - description = (p.description if p else '*') #or '*' - sig_index = getattr(sig, 'index', 0) + description = p.description if p else "*" # or '*' + sig_index = getattr(sig, "index", 0) if i == sig_index: # Note: we use `_Param.description` instead of # `_Param.name`, that way we also get the '*' before args. - append((Signature + ',current-name', str(description))) + append((Signature + ",current-name", str(description))) else: append((Signature, str(description))) - append((Signature + ',operator', ', ')) + append((Signature + ",operator", ", ")) if sig.params: # Pop last comma result.pop() - append((Signature + ',operator', ')')) - append((Signature, ' ')) + append((Signature + ",operator", ")")) + append((Signature, " ")) return result return ConditionalContainer( content=Window( - FormattedTextControl(get_text_fragments), - height=Dimension.exact(1)), + FormattedTextControl(get_text_fragments), height=Dimension.exact(1) + ), filter= - # Show only when there is a signature - HasSignature(python_input) & - # And there are no completions to be shown. (would cover signature pop-up.) - ~(has_completions & (show_completions_menu(python_input) | - show_multi_column_completions_menu(python_input))) - # Signature needs to be shown. - & ShowSignature(python_input) & - # Not done yet. - ~is_done) + # Show only when there is a signature + HasSignature(python_input) & + # And there are no completions to be shown. (would cover signature pop-up.) + ~( + has_completions + & ( + show_completions_menu(python_input) + | show_multi_column_completions_menu(python_input) + ) + ) + # Signature needs to be shown. + & ShowSignature(python_input) & + # Not done yet. + ~is_done, + ) class PythonPromptMargin(PromptMargin): @@ -268,6 +302,7 @@ class PythonPromptMargin(PromptMargin): Create margin that displays the prompt. It shows something like "In [1]:". """ + def __init__(self, python_input): self.python_input = python_input @@ -279,211 +314,254 @@ def get_prompt(): def get_continuation(width, line_number, is_soft_wrap): if python_input.show_line_numbers and not is_soft_wrap: - text = ('%i ' % (line_number + 1)).rjust(width) - return [('class:line-number', text)] + text = ("%i " % (line_number + 1)).rjust(width) + return [("class:line-number", text)] else: return get_prompt_style().in2_prompt(width) - super(PythonPromptMargin, self).__init__(get_prompt, get_continuation) + super().__init__(get_prompt, get_continuation) -def status_bar(python_input): +def status_bar(python_input: "PythonInput") -> Container: """ Create the `Layout` for the status bar. """ - TB = 'class:status-toolbar' + TB = "class:status-toolbar" @if_mousedown - def toggle_paste_mode(mouse_event): + def toggle_paste_mode(mouse_event: MouseEvent) -> None: python_input.paste_mode = not python_input.paste_mode @if_mousedown - def enter_history(mouse_event): + def enter_history(mouse_event: MouseEvent) -> None: python_input.enter_history() - def get_text_fragments(): + def get_text_fragments() -> StyleAndTextTuples: python_buffer = python_input.default_buffer - result = [] + result: StyleAndTextTuples = [] append = result.append - append((TB, ' ')) + append((TB, " ")) result.extend(get_inputmode_fragments(python_input)) - append((TB, ' ')) + append((TB, " ")) # Position in history. - append((TB, '%i/%i ' % (python_buffer.working_index + 1, - len(python_buffer._working_lines)))) + append( + ( + TB, + "%i/%i " + % (python_buffer.working_index + 1, len(python_buffer._working_lines)), + ) + ) # Shortcuts. app = get_app() - if not python_input.vi_mode and app.current_buffer == python_input.search_buffer: - append((TB, '[Ctrl-G] Cancel search [Enter] Go to this position.')) + if ( + not python_input.vi_mode + and app.current_buffer == python_input.search_buffer + ): + append((TB, "[Ctrl-G] Cancel search [Enter] Go to this position.")) elif bool(app.current_buffer.selection_state) and not python_input.vi_mode: # Emacs cut/copy keys. - append((TB, '[Ctrl-W] Cut [Meta-W] Copy [Ctrl-Y] Paste [Ctrl-G] Cancel')) + append((TB, "[Ctrl-W] Cut [Meta-W] Copy [Ctrl-Y] Paste [Ctrl-G] Cancel")) else: - result.extend([ - (TB + ' class:key', '[F3]', enter_history), - (TB, ' History ', enter_history), - (TB + ' class:key', '[F6]', toggle_paste_mode), - (TB, ' ', toggle_paste_mode), - ]) + result.extend( + [ + (TB + " class:key", "[F3]", enter_history), + (TB, " History ", enter_history), + (TB + " class:key", "[F6]", toggle_paste_mode), + (TB, " ", toggle_paste_mode), + ] + ) if python_input.paste_mode: - append((TB + ' class:paste-mode-on', 'Paste mode (on)', toggle_paste_mode)) + append( + (TB + " class:paste-mode-on", "Paste mode (on)", toggle_paste_mode) + ) else: - append((TB, 'Paste mode', toggle_paste_mode)) + append((TB, "Paste mode", toggle_paste_mode)) return result return ConditionalContainer( - content=Window(content=FormattedTextControl(get_text_fragments), style=TB), - filter=~is_done & renderer_height_is_known & - Condition(lambda: python_input.show_status_bar and - not python_input.show_exit_confirmation)) + content=Window(content=FormattedTextControl(get_text_fragments), style=TB), + filter=~is_done + & renderer_height_is_known + & Condition( + lambda: python_input.show_status_bar + and not python_input.show_exit_confirmation + ), + ) -def get_inputmode_fragments(python_input): +def get_inputmode_fragments(python_input: "PythonInput") -> StyleAndTextTuples: """ Return current input mode as a list of (token, text) tuples for use in a toolbar. """ app = get_app() + @if_mousedown - def toggle_vi_mode(mouse_event): + def toggle_vi_mode(mouse_event: MouseEvent) -> None: python_input.vi_mode = not python_input.vi_mode - token = 'class:status-toolbar' - input_mode_t = 'class:status-toolbar.input-mode' + token = "class:status-toolbar" + input_mode_t = "class:status-toolbar.input-mode" mode = app.vi_state.input_mode - result = [] + result: StyleAndTextTuples = [] append = result.append - append((input_mode_t, '[F4] ', toggle_vi_mode)) + append((input_mode_t, "[F4] ", toggle_vi_mode)) # InputMode if python_input.vi_mode: recording_register = app.vi_state.recording_register if recording_register: - append((token, ' ')) - append((token + ' class:record', 'RECORD({})'.format(recording_register))) - append((token, ' - ')) + append((token, " ")) + append((token + " class:record", "RECORD({})".format(recording_register))) + append((token, " - ")) - if bool(app.current_buffer.selection_state): + if app.current_buffer.selection_state is not None: if app.current_buffer.selection_state.type == SelectionType.LINES: - append((input_mode_t, 'Vi (VISUAL LINE)', toggle_vi_mode)) + append((input_mode_t, "Vi (VISUAL LINE)", toggle_vi_mode)) elif app.current_buffer.selection_state.type == SelectionType.CHARACTERS: - append((input_mode_t, 'Vi (VISUAL)', toggle_vi_mode)) - append((token, ' ')) - elif app.current_buffer.selection_state.type == 'BLOCK': - append((input_mode_t, 'Vi (VISUAL BLOCK)', toggle_vi_mode)) - append((token, ' ')) - elif mode in (InputMode.INSERT, 'vi-insert-multiple'): - append((input_mode_t, 'Vi (INSERT)', toggle_vi_mode)) - append((token, ' ')) + append((input_mode_t, "Vi (VISUAL)", toggle_vi_mode)) + append((token, " ")) + elif app.current_buffer.selection_state.type == "BLOCK": + append((input_mode_t, "Vi (VISUAL BLOCK)", toggle_vi_mode)) + append((token, " ")) + elif mode in (InputMode.INSERT, "vi-insert-multiple"): + append((input_mode_t, "Vi (INSERT)", toggle_vi_mode)) + append((token, " ")) elif mode == InputMode.NAVIGATION: - append((input_mode_t, 'Vi (NAV)', toggle_vi_mode)) - append((token, ' ')) + append((input_mode_t, "Vi (NAV)", toggle_vi_mode)) + append((token, " ")) elif mode == InputMode.REPLACE: - append((input_mode_t, 'Vi (REPLACE)', toggle_vi_mode)) - append((token, ' ')) + append((input_mode_t, "Vi (REPLACE)", toggle_vi_mode)) + append((token, " ")) else: if app.emacs_state.is_recording: - append((token, ' ')) - append((token + ' class:record', 'RECORD')) - append((token, ' - ')) + append((token, " ")) + append((token + " class:record", "RECORD")) + append((token, " - ")) - append((input_mode_t, 'Emacs', toggle_vi_mode)) - append((token, ' ')) + append((input_mode_t, "Emacs", toggle_vi_mode)) + append((token, " ")) return result -def show_sidebar_button_info(python_input): +def show_sidebar_button_info(python_input: "PythonInput") -> Container: """ Create `Layout` for the information in the right-bottom corner. (The right part of the status bar.) """ + @if_mousedown - def toggle_sidebar(mouse_event): + def toggle_sidebar(mouse_event: MouseEvent) -> None: " Click handler for the menu. " python_input.show_sidebar = not python_input.show_sidebar version = sys.version_info - tokens = [ - ('class:status-toolbar.key', '[F2]', toggle_sidebar), - ('class:status-toolbar', ' Menu', toggle_sidebar), - ('class:status-toolbar', ' - '), - ('class:status-toolbar.python-version', '%s %i.%i.%i' % (platform.python_implementation(), - version[0], version[1], version[2])), - ('class:status-toolbar', ' '), + tokens: StyleAndTextTuples = [ + ("class:status-toolbar.key", "[F2]", toggle_sidebar), + ("class:status-toolbar", " Menu", toggle_sidebar), + ("class:status-toolbar", " - "), + ( + "class:status-toolbar.python-version", + "%s %i.%i.%i" + % (platform.python_implementation(), version[0], version[1], version[2]), + ), + ("class:status-toolbar", " "), ] width = fragment_list_width(tokens) - def get_text_fragments(): + def get_text_fragments() -> StyleAndTextTuples: # Python version return tokens return ConditionalContainer( content=Window( FormattedTextControl(get_text_fragments), - style='class:status-toolbar', + style="class:status-toolbar", height=Dimension.exact(1), - width=Dimension.exact(width)), - filter=~is_done & renderer_height_is_known & - Condition(lambda: python_input.show_status_bar and - not python_input.show_exit_confirmation)) - - -def exit_confirmation(python_input, style='class:exit-confirmation'): + width=Dimension.exact(width), + ), + filter=~is_done + & renderer_height_is_known + & Condition( + lambda: python_input.show_status_bar + and not python_input.show_exit_confirmation + ), + ) + + +def exit_confirmation( + python_input: "PythonInput", style="class:exit-confirmation" +) -> Container: """ Create `Layout` for the exit message. """ + def get_text_fragments(): # Show "Do you really want to exit?" return [ - (style, '\n %s ([y]/n)' % python_input.exit_message), - ('[SetCursorPosition]', ''), - (style, ' \n'), + (style, "\n %s ([y]/n)" % python_input.exit_message), + ("[SetCursorPosition]", ""), + (style, " \n"), ] visible = ~is_done & Condition(lambda: python_input.show_exit_confirmation) return ConditionalContainer( - content=Window(FormattedTextControl(get_text_fragments), style=style), # , has_focus=visible)), - filter=visible) + content=Window( + FormattedTextControl(get_text_fragments), style=style + ), # , has_focus=visible)), + filter=visible, + ) -def meta_enter_message(python_input): +def meta_enter_message(python_input: "PythonInput") -> Container: """ Create the `Layout` for the 'Meta+Enter` message. """ - def get_text_fragments(): - return [('class:accept-message', ' [Meta+Enter] Execute ')] - def extra_condition(): + def get_text_fragments() -> StyleAndTextTuples: + return [("class:accept-message", " [Meta+Enter] Execute ")] + + @Condition + def extra_condition() -> bool: " Only show when... " b = python_input.default_buffer return ( - python_input.show_meta_enter_message and - (not b.document.is_cursor_at_the_end or - python_input.accept_input_on_enter is None) and - '\n' in b.text) + python_input.show_meta_enter_message + and ( + not b.document.is_cursor_at_the_end + or python_input.accept_input_on_enter is None + ) + and "\n" in b.text + ) - visible = ~is_done & has_focus(DEFAULT_BUFFER) & Condition(extra_condition) + visible = ~is_done & has_focus(DEFAULT_BUFFER) & extra_condition return ConditionalContainer( - content=Window(FormattedTextControl(get_text_fragments)), - filter=visible) - - -class PtPythonLayout(object): - def __init__(self, python_input, lexer=PythonLexer, extra_body=None, - extra_toolbars=None, extra_buffer_processors=None, - input_buffer_height=None): + content=Window(FormattedTextControl(get_text_fragments)), filter=visible + ) + + +class PtPythonLayout: + def __init__( + self, + python_input: "PythonInput", + lexer=PythonLexer, + extra_body=None, + extra_toolbars=None, + extra_buffer_processors=None, + input_buffer_height: Optional[AnyDimension] = None, + ): D = Dimension extra_body = [extra_body] if extra_body else [] extra_toolbars = extra_toolbars or [] @@ -514,21 +592,26 @@ def menu_position(): input_processors=[ ConditionalProcessor( processor=HighlightIncrementalSearchProcessor(), - filter=has_focus(SEARCH_BUFFER) | has_focus(search_toolbar.control), + filter=has_focus(SEARCH_BUFFER) + | has_focus(search_toolbar.control), ), HighlightSelectionProcessor(), DisplayMultipleCursors(), # Show matching parentheses, but only while editing. ConditionalProcessor( - processor=HighlightMatchingBracketProcessor(chars='[](){}'), - filter=has_focus(DEFAULT_BUFFER) & ~is_done & - Condition(lambda: python_input.highlight_matching_parenthesis)), + processor=HighlightMatchingBracketProcessor(chars="[](){}"), + filter=has_focus(DEFAULT_BUFFER) + & ~is_done + & Condition( + lambda: python_input.highlight_matching_parenthesis + ), + ), ConditionalProcessor( - processor=AppendAutoSuggestion(), - filter=~is_done) - ] + extra_buffer_processors, + processor=AppendAutoSuggestion(), filter=~is_done + ), + ] + + extra_buffer_processors, menu_position=menu_position, - # Make sure that we always see the result of an reverse-i-search: preview_search=True, ), @@ -538,85 +621,134 @@ def menu_position(): # which is a float. scroll_offsets=ScrollOffsets(bottom=1, left=4, right=4), # As long as we're editing, prefer a minimal height of 6. - height=(lambda: ( - None if get_app().is_done or python_input.show_exit_confirmation - else input_buffer_height)), + height=( + lambda: ( + None + if get_app().is_done or python_input.show_exit_confirmation + else input_buffer_height + ) + ), wrap_lines=Condition(lambda: python_input.wrap_lines), ) sidebar = python_sidebar(python_input) - root_container = HSplit([ - VSplit([ - HSplit([ - FloatContainer( - content=HSplit( - [create_python_input_window()] + extra_body + root_container = HSplit( + [ + VSplit( + [ + HSplit( + [ + FloatContainer( + content=HSplit( + [create_python_input_window()] + extra_body + ), + floats=[ + Float( + xcursor=True, + ycursor=True, + content=ConditionalContainer( + content=CompletionsMenu( + scroll_offset=( + lambda: python_input.completion_menu_scroll_offset + ), + max_height=12, + ), + filter=show_completions_menu( + python_input + ), + ), + ), + Float( + xcursor=True, + ycursor=True, + content=ConditionalContainer( + content=MultiColumnCompletionsMenu(), + filter=show_multi_column_completions_menu( + python_input + ), + ), + ), + Float( + xcursor=True, + ycursor=True, + content=signature_toolbar(python_input), + ), + Float( + left=2, + bottom=1, + content=exit_confirmation(python_input), + ), + Float( + bottom=0, + right=0, + height=1, + content=meta_enter_message(python_input), + hide_when_covering_content=True, + ), + Float( + bottom=1, + left=1, + right=0, + content=python_sidebar_help(python_input), + ), + ], + ), + ArgToolbar(), + search_toolbar, + SystemToolbar(), + ValidationToolbar(), + ConditionalContainer( + content=CompletionsToolbar(), + filter=show_completions_toolbar(python_input) + & ~is_done, + ), + # Docstring region. + ConditionalContainer( + content=Window( + height=D.exact(1), + char="\u2500", + style="class:separator", + ), + filter=HasSignature(python_input) + & ShowDocstring(python_input) + & ~is_done, + ), + ConditionalContainer( + content=Window( + BufferControl( + buffer=python_input.docstring_buffer, + lexer=SimpleLexer(style="class:docstring"), + # lexer=PythonLexer, + ), + height=D(max=12), + ), + filter=HasSignature(python_input) + & ShowDocstring(python_input) + & ~is_done, + ), + ] ), - floats=[ - Float(xcursor=True, - ycursor=True, - content=ConditionalContainer( - content=CompletionsMenu( - scroll_offset=( - lambda: python_input.completion_menu_scroll_offset), - max_height=12), - filter=show_completions_menu(python_input))), - Float(xcursor=True, - ycursor=True, - content=ConditionalContainer( - content=MultiColumnCompletionsMenu(), - filter=show_multi_column_completions_menu(python_input))), - Float(xcursor=True, - ycursor=True, - content=signature_toolbar(python_input)), - Float(left=2, - bottom=1, - content=exit_confirmation(python_input)), - Float(bottom=0, right=0, height=1, - content=meta_enter_message(python_input), - hide_when_covering_content=True), - Float(bottom=1, left=1, right=0, content=python_sidebar_help(python_input)), - ]), - ArgToolbar(), - search_toolbar, - SystemToolbar(), - ValidationToolbar(), - ConditionalContainer( - content=CompletionsToolbar(), - filter=show_completions_toolbar(python_input)), - - # Docstring region. - ConditionalContainer( - content=Window( - height=D.exact(1), - char='\u2500', - style='class:separator'), - filter=HasSignature(python_input) & ShowDocstring(python_input) & ~is_done), - ConditionalContainer( - content=Window( - BufferControl( - buffer=python_input.docstring_buffer, - lexer=SimpleLexer(style='class:docstring'), - #lexer=PythonLexer, + ConditionalContainer( + content=HSplit( + [ + sidebar, + Window(style="class:sidebar,separator", height=1), + python_sidebar_navigation(python_input), + ] ), - height=D(max=12)), - filter=HasSignature(python_input) & ShowDocstring(python_input) & ~is_done), - ]), - ConditionalContainer( - content=HSplit([ - sidebar, - Window(style='class:sidebar,separator', height=1), - python_sidebar_navigation(python_input), - ]), - filter=ShowSidebar(python_input) & ~is_done) - ]), - ] + extra_toolbars + [ - VSplit([ - status_bar(python_input), - show_sidebar_button_info(python_input), - ]) - ]) + filter=ShowSidebar(python_input) & ~is_done, + ), + ] + ) + ] + + extra_toolbars + + [ + VSplit( + [status_bar(python_input), show_sidebar_button_info(python_input)] + ) + ] + ) self.layout = Layout(root_container) self.sidebar = sidebar diff --git a/ptpython/prompt_style.py b/ptpython/prompt_style.py index 58514afe..d5e6ca8c 100644 --- a/ptpython/prompt_style.py +++ b/ptpython/prompt_style.py @@ -1,25 +1,26 @@ -from __future__ import unicode_literals from abc import ABCMeta, abstractmethod -from six import with_metaclass +from typing import TYPE_CHECKING -__all__ = ( - 'PromptStyle', - 'IPythonPrompt', - 'ClassicPrompt', -) +from prompt_toolkit.formatted_text import StyleAndTextTuples +if TYPE_CHECKING: + from .python_input import PythonInput -class PromptStyle(with_metaclass(ABCMeta, object)): +__all__ = ["PromptStyle", "IPythonPrompt", "ClassicPrompt"] + + +class PromptStyle(metaclass=ABCMeta): """ Base class for all prompts. """ + @abstractmethod - def in_prompt(self): + def in_prompt(self) -> StyleAndTextTuples: " Return the input tokens. " return [] @abstractmethod - def in2_prompt(self, width): + def in2_prompt(self, width: int) -> StyleAndTextTuples: """ Tokens for every following input line. @@ -29,7 +30,7 @@ def in2_prompt(self, width): return [] @abstractmethod - def out_prompt(self): + def out_prompt(self) -> StyleAndTextTuples: " Return the output tokens. " return [] @@ -38,27 +39,26 @@ class IPythonPrompt(PromptStyle): """ A prompt resembling the IPython prompt. """ - def __init__(self, python_input): + + def __init__(self, python_input: "PythonInput") -> None: self.python_input = python_input - def in_prompt(self): + def in_prompt(self) -> StyleAndTextTuples: return [ - ('class:in', 'In ['), - ('class:in.number', '%s' % self.python_input.current_statement_index), - ('class:in', ']: '), + ("class:in", "In ["), + ("class:in.number", "%s" % self.python_input.current_statement_index), + ("class:in", "]: "), ] - def in2_prompt(self, width): - return [ - ('class:in', '...: '.rjust(width)), - ] + def in2_prompt(self, width: int) -> StyleAndTextTuples: + return [("class:in", "...: ".rjust(width))] - def out_prompt(self): + def out_prompt(self) -> StyleAndTextTuples: return [ - ('class:out', 'Out['), - ('class:out.number', '%s' % self.python_input.current_statement_index), - ('class:out', ']:'), - ('', ' '), + ("class:out", "Out["), + ("class:out.number", "%s" % self.python_input.current_statement_index), + ("class:out", "]:"), + ("", " "), ] @@ -66,11 +66,12 @@ class ClassicPrompt(PromptStyle): """ The classic Python prompt. """ - def in_prompt(self): - return [('class:prompt', '>>> ')] - def in2_prompt(self, width): - return [('class:prompt.dots', '...')] + def in_prompt(self) -> StyleAndTextTuples: + return [("class:prompt", ">>> ")] + + def in2_prompt(self, width: int) -> StyleAndTextTuples: + return [("class:prompt.dots", "...")] - def out_prompt(self): + def out_prompt(self) -> StyleAndTextTuples: return [] diff --git a/ptpython/python_input.py b/ptpython/python_input.py index 2c855ba9..c4bbbd0c 100644 --- a/ptpython/python_input.py +++ b/ptpython/python_input.py @@ -2,66 +2,79 @@ Application for reading Python input. This can be used for creation of Python REPLs. """ -from __future__ import unicode_literals +import __future__ + +from asyncio import get_event_loop +from functools import partial +from typing import Any, Callable, Dict, Generic, List, Optional, TypeVar from prompt_toolkit.application import Application, get_app -from prompt_toolkit.application.run_in_terminal import run_coroutine_in_terminal -from prompt_toolkit.auto_suggest import AutoSuggestFromHistory, ConditionalAutoSuggest, ThreadedAutoSuggest +from prompt_toolkit.auto_suggest import ( + AutoSuggestFromHistory, + ConditionalAutoSuggest, + ThreadedAutoSuggest, +) from prompt_toolkit.buffer import Buffer -from prompt_toolkit.key_binding.bindings.auto_suggest import load_auto_suggest_bindings -from prompt_toolkit.key_binding.bindings.open_in_editor import load_open_in_editor_bindings -from prompt_toolkit.completion import ThreadedCompleter +from prompt_toolkit.completion import Completer, FuzzyCompleter, ThreadedCompleter from prompt_toolkit.document import Document from prompt_toolkit.enums import DEFAULT_BUFFER, EditingMode -from prompt_toolkit.eventloop.defaults import get_event_loop from prompt_toolkit.filters import Condition -from prompt_toolkit.history import FileHistory, InMemoryHistory, ThreadedHistory -from prompt_toolkit.input.defaults import create_input -from prompt_toolkit.key_binding import merge_key_bindings, ConditionalKeyBindings, KeyBindings +from prompt_toolkit.history import ( + FileHistory, + History, + InMemoryHistory, + ThreadedHistory, +) +from prompt_toolkit.input import Input +from prompt_toolkit.key_binding import ( + ConditionalKeyBindings, + KeyBindings, + merge_key_bindings, +) +from prompt_toolkit.key_binding.bindings.auto_suggest import load_auto_suggest_bindings +from prompt_toolkit.key_binding.bindings.open_in_editor import ( + load_open_in_editor_bindings, +) from prompt_toolkit.key_binding.vi_state import InputMode -from prompt_toolkit.lexers import PygmentsLexer, DynamicLexer, SimpleLexer -from prompt_toolkit.output import ColorDepth -from prompt_toolkit.output.defaults import create_output -from prompt_toolkit.styles import DynamicStyle, SwapLightAndDarkStyleTransformation, ConditionalStyleTransformation, AdjustBrightnessStyleTransformation, merge_style_transformations +from prompt_toolkit.lexers import DynamicLexer, Lexer, PygmentsLexer, SimpleLexer +from prompt_toolkit.output import ColorDepth, Output +from prompt_toolkit.styles import ( + AdjustBrightnessStyleTransformation, + BaseStyle, + ConditionalStyleTransformation, + DynamicStyle, + SwapLightAndDarkStyleTransformation, + merge_style_transformations, +) from prompt_toolkit.utils import is_windows -from prompt_toolkit.validation import ConditionalValidator -from prompt_toolkit.completion import FuzzyCompleter +from prompt_toolkit.validation import ConditionalValidator, Validator +from pygments.lexers import Python3Lexer as PythonLexer from .completer import PythonCompleter -from .history_browser import History -from .key_bindings import load_python_bindings, load_sidebar_bindings, load_confirm_exit_bindings -from .layout import PtPythonLayout, CompletionVisualisation -from .prompt_style import IPythonPrompt, ClassicPrompt -from .style import get_all_code_styles, get_all_ui_styles, generate_style +from .history_browser import PythonHistory +from .key_bindings import ( + load_confirm_exit_bindings, + load_python_bindings, + load_sidebar_bindings, +) +from .layout import CompletionVisualisation, PtPythonLayout +from .prompt_style import ClassicPrompt, IPythonPrompt, PromptStyle +from .style import generate_style, get_all_code_styles, get_all_ui_styles from .utils import get_jedi_script_from_document from .validator import PythonValidator -from functools import partial +__all__ = ["PythonInput"] -import sys -import six -import __future__ - -if six.PY2: - from pygments.lexers import PythonLexer -else: - from pygments.lexers import Python3Lexer as PythonLexer +_T = TypeVar("_T") -__all__ = ( - 'PythonInput', -) - - -class OptionCategory(object): - def __init__(self, title, options): - assert isinstance(title, six.text_type) - assert isinstance(options, list) +class OptionCategory: + def __init__(self, title: str, options: List["Option"]) -> None: self.title = title self.options = options -class Option(object): +class Option(Generic[_T]): """ Ptpython configuration option that can be shown and modified from the sidebar. @@ -72,22 +85,26 @@ class Option(object): possible values to callbacks that activate these value. :param get_current_value: Callable that returns the current, active value. """ - def __init__(self, title, description, get_current_value, get_values): - assert isinstance(title, six.text_type) - assert isinstance(description, six.text_type) - assert callable(get_current_value) - assert callable(get_values) + def __init__( + self, + title: str, + description: str, + get_current_value: Callable[[], _T], + # We accept `object` as return type for the select functions, because + # often they return an unused boolean. Maybe this can be improved. + get_values: Callable[[], Dict[_T, Callable[[], object]]], + ) -> None: self.title = title self.description = description self.get_current_value = get_current_value self.get_values = get_values @property - def values(self): + def values(self) -> Dict[_T, Callable[[], object]]: return self.get_values() - def activate_next(self, _previous=False): + def activate_next(self, _previous: bool = False) -> None: """ Activate next value. """ @@ -110,7 +127,7 @@ def activate_next(self, _previous=False): next_option = options[index % len(options)] self.values[next_option]() - def activate_previous(self): + def activate_previous(self) -> None: """ Activate previous value. """ @@ -118,14 +135,17 @@ def activate_previous(self): COLOR_DEPTHS = { - ColorDepth.DEPTH_1_BIT: 'Monochrome', - ColorDepth.DEPTH_4_BIT: 'ANSI Colors', - ColorDepth.DEPTH_8_BIT: '256 colors', - ColorDepth.DEPTH_24_BIT: 'True color', + ColorDepth.DEPTH_1_BIT: "Monochrome", + ColorDepth.DEPTH_4_BIT: "ANSI Colors", + ColorDepth.DEPTH_8_BIT: "256 colors", + ColorDepth.DEPTH_24_BIT: "True color", } +_Namespace = Dict[str, Any] +_GetNamespace = Callable[[], _Namespace] -class PythonInput(object): + +class PythonInput: """ Prompt for reading Python input. @@ -134,31 +154,43 @@ class PythonInput(object): python_input = PythonInput(...) python_code = python_input.app.run() """ - def __init__(self, - get_globals=None, get_locals=None, history_filename=None, - vi_mode=False, - - input=None, - output=None, - color_depth=None, - # For internal use. - extra_key_bindings=None, - _completer=None, _validator=None, - _lexer=None, _extra_buffer_processors=None, - _extra_layout_body=None, _extra_toolbars=None, - _input_buffer_height=None): - - self.get_globals = get_globals or (lambda: {}) - self.get_locals = get_locals or self.get_globals + def __init__( + self, + get_globals: Optional[_GetNamespace] = None, + get_locals: Optional[_GetNamespace] = None, + history_filename: Optional[str] = None, + vi_mode: bool = False, + color_depth: Optional[ColorDepth] = None, + # Input/output. + input: Optional[Input] = None, + output: Optional[Output] = None, + # For internal use. + extra_key_bindings: Optional[KeyBindings] = None, + _completer: Optional[Completer] = None, + _validator: Optional[Validator] = None, + _lexer: Optional[Lexer] = None, + _extra_buffer_processors=None, + _extra_layout_body=None, + _extra_toolbars=None, + _input_buffer_height=None, + ) -> None: + + self.get_globals: _GetNamespace = get_globals or (lambda: {}) + self.get_locals: _GetNamespace = get_locals or self.get_globals self._completer = _completer or FuzzyCompleter( - PythonCompleter(self.get_globals, self.get_locals, - lambda: self.enable_dictionary_completion), - enable_fuzzy=Condition(lambda: self.enable_fuzzy_completion)) + PythonCompleter( + self.get_globals, + self.get_locals, + lambda: self.enable_dictionary_completion, + ), + enable_fuzzy=Condition(lambda: self.enable_fuzzy_completion), + ) self._validator = _validator or PythonValidator(self.get_compiler_flags) self._lexer = _lexer or PygmentsLexer(PythonLexer) + self.history: History if history_filename: self.history = ThreadedHistory(FileHistory(history_filename)) else: @@ -172,128 +204,142 @@ def __init__(self, self.extra_key_bindings = extra_key_bindings or KeyBindings() # Settings. - self.show_signature = False - self.show_docstring = False - self.show_meta_enter_message = True - self.completion_visualisation = CompletionVisualisation.MULTI_COLUMN - self.completion_menu_scroll_offset = 1 - - self.show_line_numbers = False - self.show_status_bar = True - self.wrap_lines = True - self.complete_while_typing = True - self.paste_mode = False # When True, don't insert whitespace after newline. - self.confirm_exit = True # Ask for confirmation when Control-D is pressed. - self.accept_input_on_enter = 2 # Accept when pressing Enter 'n' times. - # 'None' means that meta-enter is always required. - self.enable_open_in_editor = True - self.enable_system_bindings = True - self.enable_input_validation = True - self.enable_auto_suggest = False - self.enable_mouse_support = False - self.enable_history_search = False # When True, like readline, going - # back in history will filter the - # history on the records starting - # with the current input. - - self.enable_syntax_highlighting = True - self.enable_fuzzy_completion = False - self.enable_dictionary_completion = False - self.swap_light_and_dark = False - self.highlight_matching_parenthesis = False - self.show_sidebar = False # Currently show the sidebar. - self.show_sidebar_help = True # When the sidebar is visible, also show the help text. - self.show_exit_confirmation = False # Currently show 'Do you really want to exit?' - self.terminal_title = None # The title to be displayed in the terminal. (None or string.) - self.exit_message = 'Do you really want to exit?' - self.insert_blank_line_after_output = True # (For the REPL.) + self.show_signature: bool = False + self.show_docstring: bool = False + self.show_meta_enter_message: bool = True + self.completion_visualisation: CompletionVisualisation = CompletionVisualisation.MULTI_COLUMN + self.completion_menu_scroll_offset: int = 1 + + self.show_line_numbers: bool = False + self.show_status_bar: bool = True + self.wrap_lines: bool = True + self.complete_while_typing: bool = True + self.paste_mode: bool = False # When True, don't insert whitespace after newline. + self.confirm_exit: bool = True # Ask for confirmation when Control-D is pressed. + self.accept_input_on_enter: int = 2 # Accept when pressing Enter 'n' times. + # 'None' means that meta-enter is always required. + self.enable_open_in_editor: bool = True + self.enable_system_bindings: bool = True + self.enable_input_validation: bool = True + self.enable_auto_suggest: bool = False + self.enable_mouse_support: bool = False + self.enable_history_search: bool = False # When True, like readline, going + # back in history will filter the + # history on the records starting + # with the current input. + + self.enable_syntax_highlighting: bool = True + self.enable_fuzzy_completion: bool = False + self.enable_dictionary_completion: bool = False + self.swap_light_and_dark: bool = False + self.highlight_matching_parenthesis: bool = False + self.show_sidebar: bool = False # Currently show the sidebar. + + # When the sidebar is visible, also show the help text. + self.show_sidebar_help: bool = True + + # Currently show 'Do you really want to exit?' + self.show_exit_confirmation: bool = False + + # The title to be displayed in the terminal. (None or string.) + self.terminal_title: Optional[str] = None + + self.exit_message: str = "Do you really want to exit?" + self.insert_blank_line_after_output: bool = True # (For the REPL.) # The buffers. self.default_buffer = self._create_buffer() - self.search_buffer = Buffer() - self.docstring_buffer = Buffer(read_only=True) + self.search_buffer: Buffer = Buffer() + self.docstring_buffer: Buffer = Buffer(read_only=True) # Tokens to be shown at the prompt. - self.prompt_style = 'classic' # The currently active style. + self.prompt_style: str = "classic" # The currently active style. - self.all_prompt_styles = { # Styles selectable from the menu. - 'ipython': IPythonPrompt(self), - 'classic': ClassicPrompt(), + # Styles selectable from the menu. + self.all_prompt_styles: Dict[str, PromptStyle] = { + "ipython": IPythonPrompt(self), + "classic": ClassicPrompt(), } - self.get_input_prompt = lambda: \ - self.all_prompt_styles[self.prompt_style].in_prompt() + self.get_input_prompt = lambda: self.all_prompt_styles[ + self.prompt_style + ].in_prompt() - self.get_output_prompt = lambda: \ - self.all_prompt_styles[self.prompt_style].out_prompt() + self.get_output_prompt = lambda: self.all_prompt_styles[ + self.prompt_style + ].out_prompt() #: Load styles. - self.code_styles = get_all_code_styles() + self.code_styles: Dict[str, BaseStyle] = get_all_code_styles() self.ui_styles = get_all_ui_styles() - self._current_code_style_name = 'default' - self._current_ui_style_name = 'default' + self._current_code_style_name: str = "default" + self._current_ui_style_name: str = "default" if is_windows(): - self._current_code_style_name = 'win32' + self._current_code_style_name = "win32" self._current_style = self._generate_style() - self.color_depth = color_depth or ColorDepth.default() + self.color_depth: ColorDepth = color_depth or ColorDepth.default() - self.max_brightness = 1.0 - self.min_brightness = 0.0 + self.max_brightness: float = 1.0 + self.min_brightness: float = 0.0 # Options to be configurable from the sidebar. self.options = self._create_options() - self.selected_option_index = 0 + self.selected_option_index: int = 0 #: Incremeting integer counting the current statement. - self.current_statement_index = 1 + self.current_statement_index: int = 1 # Code signatures. (This is set asynchronously after a timeout.) - self.signatures = [] + self.signatures: List[Any] = [] # Boolean indicating whether we have a signatures thread running. # (Never run more than one at the same time.) - self._get_signatures_thread_running = False - - self.output = output or create_output() - self.input = input or create_input(sys.stdin) - - self.style_transformation = merge_style_transformations([ - ConditionalStyleTransformation( - SwapLightAndDarkStyleTransformation(), - filter=Condition(lambda: self.swap_light_and_dark)), - AdjustBrightnessStyleTransformation( - lambda: self.min_brightness, - lambda: self.max_brightness), - ]) + self._get_signatures_thread_running: bool = False + + self.style_transformation = merge_style_transformations( + [ + ConditionalStyleTransformation( + SwapLightAndDarkStyleTransformation(), + filter=Condition(lambda: self.swap_light_and_dark), + ), + AdjustBrightnessStyleTransformation( + lambda: self.min_brightness, lambda: self.max_brightness + ), + ] + ) self.ptpython_layout = PtPythonLayout( self, lexer=DynamicLexer( - lambda: self._lexer if self.enable_syntax_highlighting else SimpleLexer()), + lambda: self._lexer + if self.enable_syntax_highlighting + else SimpleLexer() + ), input_buffer_height=self._input_buffer_height, extra_buffer_processors=self._extra_buffer_processors, extra_body=self._extra_layout_body, - extra_toolbars=self._extra_toolbars) + extra_toolbars=self._extra_toolbars, + ) self.app = self._create_application() if vi_mode: self.app.editing_mode = EditingMode.VI - def _accept_handler(self, buff): + def _accept_handler(self, buff: Buffer) -> bool: app = get_app() app.exit(result=buff.text) app.pre_run_callables.append(buff.reset) return True # Keep text, we call 'reset' later on. @property - def option_count(self): + def option_count(self) -> int: " Return the total amount of options. (In all categories together.) " return sum(len(category.options) for category in self.options) @property - def selected_option(self): + def selected_option(self) -> Option: " Return the currently selected option. " i = 0 for category in self.options: @@ -303,7 +349,9 @@ def selected_option(self): else: i += 1 - def get_compiler_flags(self): + raise ValueError("Nothing selected") + + def get_compiler_flags(self) -> int: """ Give the current compiler flags by looking for _Feature instances in the globals. @@ -317,7 +365,7 @@ def get_compiler_flags(self): return flags @property - def add_key_binding(self): + def add_key_binding(self) -> Callable[[_T], _T]: """ Shortcut for adding new key bindings. (Mostly useful for a config.py file, that receives @@ -329,20 +377,19 @@ def add_key_binding(self): def handler(event): ... """ + def add_binding_decorator(*k, **kw): return self.extra_key_bindings.add(*k, **kw) + return add_binding_decorator - def install_code_colorscheme(self, name, style_dict): + def install_code_colorscheme(self, name: str, style: BaseStyle) -> None: """ Install a new code color scheme. """ - assert isinstance(name, six.text_type) - assert isinstance(style_dict, dict) + self.code_styles[name] = style - self.code_styles[name] = style_dict - - def use_code_colorscheme(self, name): + def use_code_colorscheme(self, name: str) -> None: """ Apply new colorscheme. (By name.) """ @@ -351,16 +398,13 @@ def use_code_colorscheme(self, name): self._current_code_style_name = name self._current_style = self._generate_style() - def install_ui_colorscheme(self, name, style_dict): + def install_ui_colorscheme(self, name: str, style: BaseStyle) -> None: """ Install a new UI color scheme. """ - assert isinstance(name, six.text_type) - assert isinstance(style_dict, dict) - - self.ui_styles[name] = style_dict + self.ui_styles[name] = style - def use_ui_colorscheme(self, name): + def use_ui_colorscheme(self, name: str) -> None: """ Apply new colorscheme. (By name.) """ @@ -369,43 +413,48 @@ def use_ui_colorscheme(self, name): self._current_ui_style_name = name self._current_style = self._generate_style() - def _use_color_depth(self, depth): + def _use_color_depth(self, depth: ColorDepth) -> None: self.color_depth = depth - def _set_min_brightness(self, value): + def _set_min_brightness(self, value: float) -> None: self.min_brightness = value self.max_brightness = max(self.max_brightness, value) - def _set_max_brightness(self, value): + def _set_max_brightness(self, value: float) -> None: self.max_brightness = value self.min_brightness = min(self.min_brightness, value) - def _generate_style(self): + def _generate_style(self) -> BaseStyle: """ Create new Style instance. (We don't want to do this on every key press, because each time the renderer receives a new style class, he will redraw everything.) """ - return generate_style(self.code_styles[self._current_code_style_name], - self.ui_styles[self._current_ui_style_name]) + return generate_style( + self.code_styles[self._current_code_style_name], + self.ui_styles[self._current_ui_style_name], + ) - def _create_options(self): + def _create_options(self) -> List[OptionCategory]: """ Create a list of `Option` instances for the options sidebar. """ - def enable(attribute, value=True): + + def enable(attribute: str, value: Any = True) -> bool: setattr(self, attribute, value) # Return `True`, to be able to chain this in the lambdas below. return True - def disable(attribute): + def disable(attribute: str) -> bool: setattr(self, attribute, False) return True - def simple_option(title, description, field_name, values=None): + def simple_option( + title: str, description: str, field_name: str, values: Optional[List] = None + ) -> Option: " Create Simple on/of option. " - values = values or ['off', 'on'] + values = values or ["off", "on"] def get_current_value(): return values[bool(getattr(self, field_name))] @@ -416,195 +465,300 @@ def get_values(): values[0]: lambda: disable(field_name), } - return Option(title=title, description=description, - get_values=get_values, - get_current_value=get_current_value) + return Option( + title=title, + description=description, + get_values=get_values, + get_current_value=get_current_value, + ) brightness_values = [1.0 / 20 * value for value in range(0, 21)] return [ - OptionCategory('Input', [ - simple_option(title='Editing mode', - description='Vi or emacs key bindings.', - field_name='vi_mode', - values=[EditingMode.EMACS, EditingMode.VI]), - simple_option(title='Paste mode', - description="When enabled, don't indent automatically.", - field_name='paste_mode'), - Option(title='Complete while typing', - description="Generate autocompletions automatically while typing. " - 'Don\'t require pressing TAB. (Not compatible with "History search".)', - get_current_value=lambda: ['off', 'on'][self.complete_while_typing], - get_values=lambda: { - 'on': lambda: enable('complete_while_typing') and disable('enable_history_search'), - 'off': lambda: disable('complete_while_typing'), - }), - Option(title='Enable fuzzy completion', - description="Enable fuzzy completion.", - get_current_value=lambda: ['off', 'on'][self.enable_fuzzy_completion], - get_values=lambda: { - 'on': lambda: enable('enable_fuzzy_completion'), - 'off': lambda: disable('enable_fuzzy_completion'), - }), - Option(title='Dictionary completion', - description='Enable experimental dictionary completion.\n' - 'WARNING: this does "eval" on fragments of\n' - ' your Python input and is\n' - ' potentially unsafe.', - get_current_value=lambda: ['off', 'on'][self.enable_dictionary_completion], - get_values=lambda: { - 'on': lambda: enable('enable_dictionary_completion'), - 'off': lambda: disable('enable_dictionary_completion'), - }), - Option(title='History search', - description='When pressing the up-arrow, filter the history on input starting ' - 'with the current text. (Not compatible with "Complete while typing".)', - get_current_value=lambda: ['off', 'on'][self.enable_history_search], - get_values=lambda: { - 'on': lambda: enable('enable_history_search') and disable('complete_while_typing'), - 'off': lambda: disable('enable_history_search'), - }), - simple_option(title='Mouse support', - description='Respond to mouse clicks and scrolling for positioning the cursor, ' - 'selecting text and scrolling through windows.', - field_name='enable_mouse_support'), - simple_option(title='Confirm on exit', - description='Require confirmation when exiting.', - field_name='confirm_exit'), - simple_option(title='Input validation', - description='In case of syntax errors, move the cursor to the error ' - 'instead of showing a traceback of a SyntaxError.', - field_name='enable_input_validation'), - simple_option(title='Auto suggestion', - description='Auto suggest inputs by looking at the history. ' - 'Pressing right arrow or Ctrl-E will complete the entry.', - field_name='enable_auto_suggest'), - Option(title='Accept input on enter', - description='Amount of ENTER presses required to execute input when the cursor ' - 'is at the end of the input. (Note that META+ENTER will always execute.)', - get_current_value=lambda: str(self.accept_input_on_enter or 'meta-enter'), - get_values=lambda: { - '2': lambda: enable('accept_input_on_enter', 2), - '3': lambda: enable('accept_input_on_enter', 3), - '4': lambda: enable('accept_input_on_enter', 4), - 'meta-enter': lambda: enable('accept_input_on_enter', None), - }), - ]), - OptionCategory('Display', [ - Option(title='Completions', - description='Visualisation to use for displaying the completions. (Multiple columns, one column, a toolbar or nothing.)', - get_current_value=lambda: self.completion_visualisation, - get_values=lambda: { - CompletionVisualisation.NONE: lambda: enable('completion_visualisation', CompletionVisualisation.NONE), - CompletionVisualisation.POP_UP: lambda: enable('completion_visualisation', CompletionVisualisation.POP_UP), - CompletionVisualisation.MULTI_COLUMN: lambda: enable('completion_visualisation', CompletionVisualisation.MULTI_COLUMN), - CompletionVisualisation.TOOLBAR: lambda: enable('completion_visualisation', CompletionVisualisation.TOOLBAR), - }), - Option(title='Prompt', - description="Visualisation of the prompt. ('>>>' or 'In [1]:')", - get_current_value=lambda: self.prompt_style, - get_values=lambda: dict((s, partial(enable, 'prompt_style', s)) for s in self.all_prompt_styles)), - simple_option(title='Blank line after output', - description='Insert a blank line after the output.', - field_name='insert_blank_line_after_output'), - simple_option(title='Show signature', - description='Display function signatures.', - field_name='show_signature'), - simple_option(title='Show docstring', - description='Display function docstrings.', - field_name='show_docstring'), - simple_option(title='Show line numbers', - description='Show line numbers when the input consists of multiple lines.', - field_name='show_line_numbers'), - simple_option(title='Show Meta+Enter message', - description='Show the [Meta+Enter] message when this key combination is required to execute commands. ' + - '(This is the case when a simple [Enter] key press will insert a newline.', - field_name='show_meta_enter_message'), - simple_option(title='Wrap lines', - description='Wrap lines instead of scrolling horizontally.', - field_name='wrap_lines'), - simple_option(title='Show status bar', - description='Show the status bar at the bottom of the terminal.', - field_name='show_status_bar'), - simple_option(title='Show sidebar help', - description='When the sidebar is visible, also show this help text.', - field_name='show_sidebar_help'), - simple_option(title='Highlight parenthesis', - description='Highlight matching parenthesis, when the cursor is on or right after one.', - field_name='highlight_matching_parenthesis'), - ]), - OptionCategory('Colors', [ - simple_option(title='Syntax highlighting', - description='Use colors for syntax highligthing', - field_name='enable_syntax_highlighting'), - simple_option(title='Swap light/dark colors', - description='Swap light and dark colors.', - field_name='swap_light_and_dark'), - Option(title='Code', - description='Color scheme to use for the Python code.', - get_current_value=lambda: self._current_code_style_name, - get_values=lambda: dict( - (name, partial(self.use_code_colorscheme, name)) for name in self.code_styles) - ), - Option(title='User interface', - description='Color scheme to use for the user interface.', - get_current_value=lambda: self._current_ui_style_name, - get_values=lambda: dict( - (name, partial(self.use_ui_colorscheme, name)) for name in self.ui_styles) - ), - Option(title='Color depth', - description='Monochrome (1 bit), 16 ANSI colors (4 bit),\n256 colors (8 bit), or 24 bit.', - get_current_value=lambda: COLOR_DEPTHS[self.color_depth], - get_values=lambda: dict( - (name, partial(self._use_color_depth, depth)) for depth, name in COLOR_DEPTHS.items()) - ), - Option(title='Min brightness', - description='Minimum brightness for the color scheme (default=0.0).', - get_current_value=lambda: '%.2f' % self.min_brightness, - get_values=lambda: dict( - ('%.2f' % value, partial(self._set_min_brightness, value)) - for value in brightness_values) - ), - Option(title='Max brightness', - description='Maximum brightness for the color scheme (default=1.0).', - get_current_value=lambda: '%.2f' % self.max_brightness, - get_values=lambda: dict( - ('%.2f' % value, partial(self._set_max_brightness, value)) - for value in brightness_values) - ), - ]), + OptionCategory( + "Input", + [ + Option( + title="Editing mode", + description="Vi or emacs key bindings.", + get_current_value=lambda: ["Emacs", "Vi"][self.vi_mode], + get_values=lambda: { + "Emacs": lambda: disable("vi_mode"), + "Vi": lambda: enable("vi_mode"), + }, + ), + simple_option( + title="Paste mode", + description="When enabled, don't indent automatically.", + field_name="paste_mode", + ), + Option( + title="Complete while typing", + description="Generate autocompletions automatically while typing. " + 'Don\'t require pressing TAB. (Not compatible with "History search".)', + get_current_value=lambda: ["off", "on"][ + self.complete_while_typing + ], + get_values=lambda: { + "on": lambda: enable("complete_while_typing") + and disable("enable_history_search"), + "off": lambda: disable("complete_while_typing"), + }, + ), + Option( + title="Enable fuzzy completion", + description="Enable fuzzy completion.", + get_current_value=lambda: ["off", "on"][ + self.enable_fuzzy_completion + ], + get_values=lambda: { + "on": lambda: enable("enable_fuzzy_completion"), + "off": lambda: disable("enable_fuzzy_completion"), + }, + ), + Option( + title="Dictionary completion", + description="Enable experimental dictionary completion.\n" + 'WARNING: this does "eval" on fragments of\n' + " your Python input and is\n" + " potentially unsafe.", + get_current_value=lambda: ["off", "on"][ + self.enable_dictionary_completion + ], + get_values=lambda: { + "on": lambda: enable("enable_dictionary_completion"), + "off": lambda: disable("enable_dictionary_completion"), + }, + ), + Option( + title="History search", + description="When pressing the up-arrow, filter the history on input starting " + 'with the current text. (Not compatible with "Complete while typing".)', + get_current_value=lambda: ["off", "on"][ + self.enable_history_search + ], + get_values=lambda: { + "on": lambda: enable("enable_history_search") + and disable("complete_while_typing"), + "off": lambda: disable("enable_history_search"), + }, + ), + simple_option( + title="Mouse support", + description="Respond to mouse clicks and scrolling for positioning the cursor, " + "selecting text and scrolling through windows.", + field_name="enable_mouse_support", + ), + simple_option( + title="Confirm on exit", + description="Require confirmation when exiting.", + field_name="confirm_exit", + ), + simple_option( + title="Input validation", + description="In case of syntax errors, move the cursor to the error " + "instead of showing a traceback of a SyntaxError.", + field_name="enable_input_validation", + ), + simple_option( + title="Auto suggestion", + description="Auto suggest inputs by looking at the history. " + "Pressing right arrow or Ctrl-E will complete the entry.", + field_name="enable_auto_suggest", + ), + Option( + title="Accept input on enter", + description="Amount of ENTER presses required to execute input when the cursor " + "is at the end of the input. (Note that META+ENTER will always execute.)", + get_current_value=lambda: str( + self.accept_input_on_enter or "meta-enter" + ), + get_values=lambda: { + "2": lambda: enable("accept_input_on_enter", 2), + "3": lambda: enable("accept_input_on_enter", 3), + "4": lambda: enable("accept_input_on_enter", 4), + "meta-enter": lambda: enable("accept_input_on_enter", None), + }, + ), + ], + ), + OptionCategory( + "Display", + [ + Option( + title="Completions", + description="Visualisation to use for displaying the completions. (Multiple columns, one column, a toolbar or nothing.)", + get_current_value=lambda: self.completion_visualisation.value, + get_values=lambda: { + CompletionVisualisation.NONE.value: lambda: enable( + "completion_visualisation", CompletionVisualisation.NONE + ), + CompletionVisualisation.POP_UP.value: lambda: enable( + "completion_visualisation", + CompletionVisualisation.POP_UP, + ), + CompletionVisualisation.MULTI_COLUMN.value: lambda: enable( + "completion_visualisation", + CompletionVisualisation.MULTI_COLUMN, + ), + CompletionVisualisation.TOOLBAR.value: lambda: enable( + "completion_visualisation", + CompletionVisualisation.TOOLBAR, + ), + }, + ), + Option( + title="Prompt", + description="Visualisation of the prompt. ('>>>' or 'In [1]:')", + get_current_value=lambda: self.prompt_style, + get_values=lambda: dict( + (s, partial(enable, "prompt_style", s)) + for s in self.all_prompt_styles + ), + ), + simple_option( + title="Blank line after output", + description="Insert a blank line after the output.", + field_name="insert_blank_line_after_output", + ), + simple_option( + title="Show signature", + description="Display function signatures.", + field_name="show_signature", + ), + simple_option( + title="Show docstring", + description="Display function docstrings.", + field_name="show_docstring", + ), + simple_option( + title="Show line numbers", + description="Show line numbers when the input consists of multiple lines.", + field_name="show_line_numbers", + ), + simple_option( + title="Show Meta+Enter message", + description="Show the [Meta+Enter] message when this key combination is required to execute commands. " + + "(This is the case when a simple [Enter] key press will insert a newline.", + field_name="show_meta_enter_message", + ), + simple_option( + title="Wrap lines", + description="Wrap lines instead of scrolling horizontally.", + field_name="wrap_lines", + ), + simple_option( + title="Show status bar", + description="Show the status bar at the bottom of the terminal.", + field_name="show_status_bar", + ), + simple_option( + title="Show sidebar help", + description="When the sidebar is visible, also show this help text.", + field_name="show_sidebar_help", + ), + simple_option( + title="Highlight parenthesis", + description="Highlight matching parenthesis, when the cursor is on or right after one.", + field_name="highlight_matching_parenthesis", + ), + ], + ), + OptionCategory( + "Colors", + [ + simple_option( + title="Syntax highlighting", + description="Use colors for syntax highligthing", + field_name="enable_syntax_highlighting", + ), + simple_option( + title="Swap light/dark colors", + description="Swap light and dark colors.", + field_name="swap_light_and_dark", + ), + Option( + title="Code", + description="Color scheme to use for the Python code.", + get_current_value=lambda: self._current_code_style_name, + get_values=lambda: { + name: partial(self.use_code_colorscheme, name) + for name in self.code_styles + }, + ), + Option( + title="User interface", + description="Color scheme to use for the user interface.", + get_current_value=lambda: self._current_ui_style_name, + get_values=lambda: dict( + (name, partial(self.use_ui_colorscheme, name)) + for name in self.ui_styles + ), + ), + Option( + title="Color depth", + description="Monochrome (1 bit), 16 ANSI colors (4 bit),\n256 colors (8 bit), or 24 bit.", + get_current_value=lambda: COLOR_DEPTHS[self.color_depth], + get_values=lambda: { + name: partial(self._use_color_depth, depth) + for depth, name in COLOR_DEPTHS.items() + }, + ), + Option( + title="Min brightness", + description="Minimum brightness for the color scheme (default=0.0).", + get_current_value=lambda: "%.2f" % self.min_brightness, + get_values=lambda: { + "%.2f" % value: partial(self._set_min_brightness, value) + for value in brightness_values + }, + ), + Option( + title="Max brightness", + description="Maximum brightness for the color scheme (default=1.0).", + get_current_value=lambda: "%.2f" % self.max_brightness, + get_values=lambda: { + "%.2f" % value: partial(self._set_max_brightness, value) + for value in brightness_values + }, + ), + ], + ), ] - def _create_application(self): + def _create_application(self) -> Application: """ Create an `Application` instance. """ return Application( - input=self.input, - output=self.output, layout=self.ptpython_layout.layout, - key_bindings=merge_key_bindings([ - load_python_bindings(self), - load_auto_suggest_bindings(), - load_sidebar_bindings(self), - load_confirm_exit_bindings(self), - ConditionalKeyBindings( - load_open_in_editor_bindings(), - Condition(lambda: self.enable_open_in_editor)), - # Extra key bindings should not be active when the sidebar is visible. - ConditionalKeyBindings( - self.extra_key_bindings, - Condition(lambda: not self.show_sidebar)) - ]), + key_bindings=merge_key_bindings( + [ + load_python_bindings(self), + load_auto_suggest_bindings(), + load_sidebar_bindings(self), + load_confirm_exit_bindings(self), + ConditionalKeyBindings( + load_open_in_editor_bindings(), + Condition(lambda: self.enable_open_in_editor), + ), + # Extra key bindings should not be active when the sidebar is visible. + ConditionalKeyBindings( + self.extra_key_bindings, + Condition(lambda: not self.show_sidebar), + ), + ] + ), color_depth=lambda: self.color_depth, paste_mode=Condition(lambda: self.paste_mode), mouse_support=Condition(lambda: self.enable_mouse_support), style=DynamicStyle(lambda: self._current_style), style_transformation=self.style_transformation, include_default_pygments_style=False, - reverse_vi_search_direction=True) + reverse_vi_search_direction=True, + ) - def _create_buffer(self): + def _create_buffer(self) -> Buffer: """ Create the `Buffer` for the Python input. """ @@ -612,45 +766,46 @@ def _create_buffer(self): name=DEFAULT_BUFFER, complete_while_typing=Condition(lambda: self.complete_while_typing), enable_history_search=Condition(lambda: self.enable_history_search), - tempfile_suffix='.py', + tempfile_suffix=".py", history=self.history, completer=ThreadedCompleter(self._completer), validator=ConditionalValidator( - self._validator, - Condition(lambda: self.enable_input_validation)), + self._validator, Condition(lambda: self.enable_input_validation) + ), auto_suggest=ConditionalAutoSuggest( ThreadedAutoSuggest(AutoSuggestFromHistory()), - Condition(lambda: self.enable_auto_suggest)), + Condition(lambda: self.enable_auto_suggest), + ), accept_handler=self._accept_handler, - on_text_changed=self._on_input_timeout) + on_text_changed=self._on_input_timeout, + ) return python_buffer @property - def editing_mode(self): + def editing_mode(self) -> EditingMode: return self.app.editing_mode @editing_mode.setter - def editing_mode(self, value): + def editing_mode(self, value: EditingMode) -> None: self.app.editing_mode = value @property - def vi_mode(self): + def vi_mode(self) -> bool: return self.editing_mode == EditingMode.VI @vi_mode.setter - def vi_mode(self, value): + def vi_mode(self, value: bool) -> None: if value: self.editing_mode = EditingMode.VI else: self.editing_mode = EditingMode.EMACS - def _on_input_timeout(self, buff): + def _on_input_timeout(self, buff: Buffer, loop=None) -> None: """ When there is no input activity, in another thread, get the signature of the current code. """ - assert isinstance(buff, Buffer) app = self.app # Never run multiple get-signature threads. @@ -660,8 +815,12 @@ def _on_input_timeout(self, buff): document = buff.document + loop = loop or get_event_loop() + def run(): - script = get_jedi_script_from_document(document, self.get_locals(), self.get_globals()) + script = get_jedi_script_from_document( + document, self.get_locals(), self.get_globals() + ) # Show signatures in help text. if script: @@ -700,37 +859,41 @@ def run(): # Set docstring in docstring buffer. if signatures: string = signatures[0].docstring() - if not isinstance(string, six.text_type): - string = string.decode('utf-8') + if not isinstance(string, str): + string = string.decode("utf-8") self.docstring_buffer.reset( - document=Document(string, cursor_position=0)) + document=Document(string, cursor_position=0) + ) else: self.docstring_buffer.reset() app.invalidate() else: - self._on_input_timeout(buff) + self._on_input_timeout(buff, loop=loop) - get_event_loop().run_in_executor(run) + loop.run_in_executor(None, run) - def on_reset(self): + def on_reset(self) -> None: self.signatures = [] - def enter_history(self): + def enter_history(self) -> None: """ Display the history. """ app = get_app() app.vi_state.input_mode = InputMode.NAVIGATION - def done(f): - result = f.result() - if result is not None: - self.default_buffer.text = result + history = PythonHistory(self, self.default_buffer.document) + + from prompt_toolkit.application import in_terminal + import asyncio - app.vi_state.input_mode = InputMode.INSERT + async def do_in_terminal() -> None: + async with in_terminal(): + result = await history.app.run_async() + if result is not None: + self.default_buffer.text = result - history = History(self, self.default_buffer.document) + app.vi_state.input_mode = InputMode.INSERT - future = run_coroutine_in_terminal(history.app.run_async) - future.add_done_callback(done) + asyncio.ensure_future(do_in_terminal()) diff --git a/ptpython/repl.py b/ptpython/repl.py index 83cecce1..4b8edf2a 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -7,64 +7,80 @@ embed(globals(), locals(), vi_mode=False) """ -from __future__ import unicode_literals - -from pygments.lexers import PythonTracebackLexer, PythonLexer -from pygments.token import Token +import asyncio +import builtins +import os +import sys +import traceback +import warnings +from typing import Any, Callable, ContextManager, Dict, Optional from prompt_toolkit.document import Document -from prompt_toolkit.eventloop.defaults import use_asyncio_event_loop -from prompt_toolkit.formatted_text import merge_formatted_text, FormattedText +from prompt_toolkit.formatted_text import ( + FormattedText, + PygmentsTokens, + merge_formatted_text, +) from prompt_toolkit.formatted_text.utils import fragment_list_width -from prompt_toolkit.utils import DummyContext -from prompt_toolkit.shortcuts import set_title, clear_title -from prompt_toolkit.shortcuts import print_formatted_text -from prompt_toolkit.formatted_text import PygmentsTokens from prompt_toolkit.patch_stdout import patch_stdout as patch_stdout_context +from prompt_toolkit.shortcuts import clear_title, print_formatted_text, set_title +from prompt_toolkit.utils import DummyContext +from pygments.lexers import PythonLexer, PythonTracebackLexer +from pygments.token import Token -from .python_input import PythonInput from .eventloop import inputhook +from .python_input import PythonInput -import os -import six -import sys -import traceback -import warnings - -__all__ = ( - 'PythonRepl', - 'enable_deprecation_warnings', - 'run_config', - 'embed', -) +__all__ = ["PythonRepl", "enable_deprecation_warnings", "run_config", "embed"] class PythonRepl(PythonInput): - def __init__(self, *a, **kw): - self._startup_paths = kw.pop('startup_paths', None) - super(PythonRepl, self).__init__(*a, **kw) + def __init__(self, *a, **kw) -> None: + self._startup_paths = kw.pop("startup_paths", None) + super().__init__(*a, **kw) self._load_start_paths() + self.pt_loop = asyncio.new_event_loop() - def _load_start_paths(self): + def _load_start_paths(self) -> None: " Start the Read-Eval-Print Loop. " if self._startup_paths: for path in self._startup_paths: if os.path.exists(path): - with open(path, 'rb') as f: - code = compile(f.read(), path, 'exec') - six.exec_(code, self.get_globals(), self.get_locals()) + with open(path, "rb") as f: + code = compile(f.read(), path, "exec") + exec(code, self.get_globals(), self.get_locals()) else: output = self.app.output - output.write('WARNING | File not found: {}\n\n'.format(path)) + output.write("WARNING | File not found: {}\n\n".format(path)) - def run(self): + 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 = self.app.run(inputhook=inputhook) + text = prompt() except EOFError: return except KeyboardInterrupt: @@ -76,7 +92,12 @@ def run(self): if self.terminal_title: clear_title() - def _process_text(self, line): + async def run_async(self) -> None: + while True: + text = await self.app.run_async() + self._process_text(text) + + def _process_text(self, line: str) -> None: if line and not line.isspace(): try: @@ -88,12 +109,12 @@ def _process_text(self, line): self._handle_exception(e) if self.insert_blank_line_after_output: - self.app.output.write('\n') + self.app.output.write("\n") self.current_statement_index += 1 self.signatures = [] - def _execute(self, line): + def _execute(self, line: str) -> None: """ Evaluate the line and print the result. """ @@ -101,70 +122,81 @@ def _execute(self, line): # WORKAROUND: Due to a bug in Jedi, the current directory is removed # from sys.path. See: https://github.com/davidhalter/jedi/issues/1148 - if '' not in sys.path: - sys.path.insert(0, '') + if "" not in sys.path: + sys.path.insert(0, "") - def compile_with_flags(code, mode): + def compile_with_flags(code: str, mode: str): " Compile code with the right compiler flags. " - return compile(code, '', mode, - flags=self.get_compiler_flags(), - dont_inherit=True) - - if line.lstrip().startswith('\x1a'): + return compile( + code, + "", + mode, + flags=self.get_compiler_flags(), + dont_inherit=True, + ) + + if line.lstrip().startswith("\x1a"): # When the input starts with Ctrl-Z, quit the REPL. self.app.exit() - elif line.lstrip().startswith('!'): + elif line.lstrip().startswith("!"): # Run as shell command os.system(line[1:]) else: # Try eval first try: - code = compile_with_flags(line, 'eval') + code = compile_with_flags(line, "eval") result = eval(code, self.get_globals(), self.get_locals()) - locals = self.get_locals() - locals['_'] = locals['_%i' % self.current_statement_index] = result + locals: Dict[str, Any] = self.get_locals() + locals["_"] = locals["_%i" % self.current_statement_index] = result if result is not None: out_prompt = self.get_output_prompt() try: - result_str = '%r\n' % (result, ) + result_str = "%r\n" % (result,) except UnicodeDecodeError: # In Python 2: `__repr__` should return a bytestring, # so to put it in a unicode context could raise an # exception that the 'ascii' codec can't decode certain # characters. Decode as utf-8 in that case. - result_str = '%s\n' % repr(result).decode('utf-8') + result_str = "%s\n" % repr(result).decode( # type: ignore + "utf-8" + ) # Align every line to the first one. - line_sep = '\n' + ' ' * fragment_list_width(out_prompt) - result_str = line_sep.join(result_str.splitlines()) + '\n' + line_sep = "\n" + " " * fragment_list_width(out_prompt) + result_str = line_sep.join(result_str.splitlines()) + "\n" # Write output tokens. if self.enable_syntax_highlighting: - formatted_output = merge_formatted_text([ - out_prompt, - PygmentsTokens(list(_lex_python_result(result_str))), - ]) + formatted_output = merge_formatted_text( + [ + out_prompt, + PygmentsTokens(list(_lex_python_result(result_str))), + ] + ) else: formatted_output = FormattedText( - out_prompt + [('', result_str)]) + out_prompt + [("", result_str)] + ) print_formatted_text( - formatted_output, style=self._current_style, + formatted_output, + style=self._current_style, style_transformation=self.style_transformation, - include_default_pygments_style=False) + include_default_pygments_style=False, + ) # If not a valid `eval` expression, run using `exec` instead. except SyntaxError: - code = compile_with_flags(line, 'exec') - six.exec_(code, self.get_globals(), self.get_locals()) + code = compile_with_flags(line, "exec") + exec(code, self.get_globals(), self.get_locals()) output.flush() - def _handle_exception(self, e): + def _handle_exception(self, e: Exception) -> None: output = self.app.output # Instead of just calling ``traceback.format_exc``, we take the @@ -174,10 +206,10 @@ def _handle_exception(self, e): # Required for pdb.post_mortem() to work. sys.last_type, sys.last_value, sys.last_traceback = t, v, tb - tblist = traceback.extract_tb(tb) + tblist = list(traceback.extract_tb(tb)) for line_nr, tb_tuple in enumerate(tblist): - if tb_tuple[0] == '': + if tb_tuple[0] == "": tblist = tblist[line_nr:] break @@ -186,33 +218,30 @@ def _handle_exception(self, e): l.insert(0, "Traceback (most recent call last):\n") l.extend(traceback.format_exception_only(t, v)) - # For Python2: `format_list` and `format_exception_only` return - # non-unicode strings. Ensure that everything is unicode. - if six.PY2: - l = [i.decode('utf-8') if isinstance(i, six.binary_type) else i for i in l] - - tb = ''.join(l) + tb_str = "".join(l) # Format exception and write to output. # (We use the default style. Most other styles result # in unreadable colors for the traceback.) if self.enable_syntax_highlighting: - tokens = list(_lex_python_traceback(tb)) + tokens = list(_lex_python_traceback(tb_str)) else: - tokens = [(Token, tb)] + tokens = [(Token, tb_str)] print_formatted_text( - PygmentsTokens(tokens), style=self._current_style, + PygmentsTokens(tokens), + style=self._current_style, style_transformation=self.style_transformation, - include_default_pygments_style=False) + include_default_pygments_style=False, + ) - output.write('%s\n' % e) + output.write("%s\n" % e) output.flush() - def _handle_keyboard_interrupt(self, e): + def _handle_keyboard_interrupt(self, e: KeyboardInterrupt) -> None: output = self.app.output - output.write('\rKeyboardInterrupt\n\n') + output.write("\rKeyboardInterrupt\n\n") output.flush() @@ -228,7 +257,7 @@ def _lex_python_result(tb): return lexer.get_tokens(tb) -def enable_deprecation_warnings(): +def enable_deprecation_warnings() -> None: """ Show deprecation warnings, when they are triggered directly by actions in the REPL. This is recommended to call, before calling `embed`. @@ -236,53 +265,57 @@ def enable_deprecation_warnings(): e.g. This will show an error message when the user imports the 'sha' library on Python 2.7. """ - warnings.filterwarnings('default', category=DeprecationWarning, - module='__main__') + warnings.filterwarnings("default", category=DeprecationWarning, module="__main__") -def run_config(repl, config_file): +def run_config(repl: PythonInput, config_file: str) -> None: """ Execute REPL config file. :param repl: `PythonInput` instance. :param config_file: Path of the configuration file. """ - assert isinstance(repl, PythonInput) - assert isinstance(config_file, six.text_type) - # Expand tildes. config_file = os.path.expanduser(config_file) - def enter_to_continue(): - six.moves.input('\nPress ENTER to continue...') + def enter_to_continue() -> None: + input("\nPress ENTER to continue...") # Check whether this file exists. if not os.path.exists(config_file): - print('Impossible to read %r' % config_file) + print("Impossible to read %r" % config_file) enter_to_continue() return # Run the config file in an empty namespace. try: - namespace = {} + namespace: Dict[str, Any] = {} - with open(config_file, 'rb') as f: - code = compile(f.read(), config_file, 'exec') - six.exec_(code, namespace, namespace) + with open(config_file, "rb") as f: + code = compile(f.read(), config_file, "exec") + exec(code, namespace, namespace) # Now we should have a 'configure' method in this namespace. We call this # method with the repl as an argument. - if 'configure' in namespace: - namespace['configure'](repl) + if "configure" in namespace: + namespace["configure"](repl) except Exception: - traceback.print_exc() - enter_to_continue() + traceback.print_exc() + enter_to_continue() -def embed(globals=None, locals=None, configure=None, - vi_mode=False, history_filename=None, title=None, - startup_paths=None, patch_stdout=False, return_asyncio_coroutine=False): +def embed( + globals=None, + locals=None, + configure: Optional[Callable] = None, + vi_mode: bool = False, + history_filename: Optional[str] = None, + title: Optional[str] = None, + startup_paths=None, + patch_stdout: bool = False, + return_asyncio_coroutine: bool = False, +) -> None: """ Call this to embed Python shell at the current point in your program. It's similar to `IPython.embed` and `bpython.embed`. :: @@ -295,15 +328,13 @@ def embed(globals=None, locals=None, configure=None, argument, to trigger configuration. :param title: Title to be displayed in the terminal titlebar. (None or string.) """ - assert configure is None or callable(configure) - # Default globals/locals if globals is None: globals = { - '__name__': '__main__', - '__package__': None, - '__doc__': None, - '__builtins__': six.moves.builtins, + "__name__": "__main__", + "__package__": None, + "__doc__": None, + "__builtins__": builtins, } locals = locals or globals @@ -314,13 +345,14 @@ def get_globals(): def get_locals(): return locals - # Create eventloop. - if return_asyncio_coroutine: - use_asyncio_event_loop() - # Create REPL. - repl = PythonRepl(get_globals=get_globals, get_locals=get_locals, vi_mode=vi_mode, - history_filename=history_filename, startup_paths=startup_paths) + repl = PythonRepl( + get_globals=get_globals, + get_locals=get_locals, + vi_mode=vi_mode, + history_filename=history_filename, + startup_paths=startup_paths, + ) if title: repl.terminal_title = title @@ -328,22 +360,15 @@ def get_locals(): if configure: configure(repl) - app = repl.app - # Start repl. - patch_context = patch_stdout_context() if patch_stdout else DummyContext() + patch_context: ContextManager = patch_stdout_context() if patch_stdout else DummyContext() - if return_asyncio_coroutine: # XXX - def coroutine(): + if return_asyncio_coroutine: + + async def coroutine(): with patch_context: - while True: - iterator = iter(app.run_async().to_asyncio_future()) - try: - while True: - yield next(iterator) - except StopIteration as exc: - text = exc.args[0] - repl._process_text(text) + await repl.run_async() + return coroutine() else: with patch_context: diff --git a/ptpython/style.py b/ptpython/style.py index 7a2cd2a1..a084c076 100644 --- a/ptpython/style.py +++ b/ptpython/style.py @@ -1,174 +1,151 @@ -from __future__ import unicode_literals +from typing import Dict -from prompt_toolkit.styles import Style, merge_styles +from prompt_toolkit.styles import BaseStyle, Style, merge_styles from prompt_toolkit.styles.pygments import style_from_pygments_cls -from prompt_toolkit.utils import is_windows, is_conemu_ansi, is_windows_vt100_supported -from pygments.styles import get_style_by_name, get_all_styles +from prompt_toolkit.utils import is_conemu_ansi, is_windows, is_windows_vt100_supported +from pygments.styles import get_all_styles, get_style_by_name -__all__ = ( - 'get_all_code_styles', - 'get_all_ui_styles', - 'generate_style', -) +__all__ = ["get_all_code_styles", "get_all_ui_styles", "generate_style"] -def get_all_code_styles(): +def get_all_code_styles() -> Dict[str, BaseStyle]: """ Return a mapping from style names to their classes. """ - result = dict((name, style_from_pygments_cls(get_style_by_name(name))) for name in get_all_styles()) - result['win32'] = Style.from_dict(win32_code_style) + result: Dict[str, BaseStyle] = { + name: style_from_pygments_cls(get_style_by_name(name)) + for name in get_all_styles() + } + result["win32"] = Style.from_dict(win32_code_style) return result -def get_all_ui_styles(): +def get_all_ui_styles() -> Dict[str, BaseStyle]: """ Return a dict mapping {ui_style_name -> style_dict}. """ return { - 'default': Style.from_dict(default_ui_style), - 'blue': Style.from_dict(blue_ui_style), + "default": Style.from_dict(default_ui_style), + "blue": Style.from_dict(blue_ui_style), } -def generate_style(python_style, ui_style): +def generate_style(python_style: BaseStyle, ui_style: BaseStyle) -> BaseStyle: """ Generate Pygments Style class from two dictionaries containing style rules. """ - return merge_styles([ - python_style, - ui_style - ]) + return merge_styles([python_style, ui_style]) # Code style for Windows consoles. They support only 16 colors, # so we choose a combination that displays nicely. win32_code_style = { - 'pygments.comment': "#00ff00", - 'pygments.keyword': '#44ff44', - 'pygments.number': '', - 'pygments.operator': '', - 'pygments.string': '#ff44ff', - - 'pygments.name': '', - 'pygments.name.decorator': '#ff4444', - 'pygments.name.class': '#ff4444', - 'pygments.name.function': '#ff4444', - 'pygments.name.builtin': '#ff4444', - - 'pygments.name.attribute': '', - 'pygments.name.constant': '', - 'pygments.name.entity': '', - 'pygments.name.exception': '', - 'pygments.name.label': '', - 'pygments.name.namespace': '', - 'pygments.name.tag': '', - 'pygments.name.variable': '', + "pygments.comment": "#00ff00", + "pygments.keyword": "#44ff44", + "pygments.number": "", + "pygments.operator": "", + "pygments.string": "#ff44ff", + "pygments.name": "", + "pygments.name.decorator": "#ff4444", + "pygments.name.class": "#ff4444", + "pygments.name.function": "#ff4444", + "pygments.name.builtin": "#ff4444", + "pygments.name.attribute": "", + "pygments.name.constant": "", + "pygments.name.entity": "", + "pygments.name.exception": "", + "pygments.name.label": "", + "pygments.name.namespace": "", + "pygments.name.tag": "", + "pygments.name.variable": "", } default_ui_style = { - 'control-character': 'ansiblue', - + "control-character": "ansiblue", # Classic prompt. - 'prompt': 'bold', - 'prompt.dots': 'noinherit', - + "prompt": "bold", + "prompt.dots": "noinherit", # (IPython <5.0) Prompt: "In [1]:" - 'in': 'bold #008800', - 'in.number': '', - + "in": "bold #008800", + "in.number": "", # Return value. - 'out': '#ff0000', - 'out.number': '#ff0000', - + "out": "#ff0000", + "out.number": "#ff0000", # Completions. - 'completion.builtin': '', - 'completion.keyword': 'fg:#008800', - - 'completion.keyword fuzzymatch.inside': 'fg:#008800', - 'completion.keyword fuzzymatch.outside': 'fg:#44aa44', - + "completion.builtin": "", + "completion.keyword": "fg:#008800", + "completion.keyword fuzzymatch.inside": "fg:#008800", + "completion.keyword fuzzymatch.outside": "fg:#44aa44", # Separator between windows. (Used above docstring.) - 'separator': '#bbbbbb', - + "separator": "#bbbbbb", # System toolbar - 'system-toolbar': '#22aaaa noinherit', - + "system-toolbar": "#22aaaa noinherit", # "arg" toolbar. - 'arg-toolbar': '#22aaaa noinherit', - 'arg-toolbar.text': 'noinherit', - + "arg-toolbar": "#22aaaa noinherit", + "arg-toolbar.text": "noinherit", # Signature toolbar. - 'signature-toolbar': 'bg:#44bbbb #000000', - 'signature-toolbar.currentname': 'bg:#008888 #ffffff bold', - 'signature-toolbar.operator': '#000000 bold', - - 'docstring': '#888888', - + "signature-toolbar": "bg:#44bbbb #000000", + "signature-toolbar.currentname": "bg:#008888 #ffffff bold", + "signature-toolbar.operator": "#000000 bold", + "docstring": "#888888", # Validation toolbar. - 'validation-toolbar': 'bg:#440000 #aaaaaa', - + "validation-toolbar": "bg:#440000 #aaaaaa", # Status toolbar. - 'status-toolbar': 'bg:#222222 #aaaaaa', - 'status-toolbar.title': 'underline', - 'status-toolbar.inputmode': 'bg:#222222 #ffffaa', - 'status-toolbar.key': 'bg:#000000 #888888', - 'status-toolbar.pastemodeon': 'bg:#aa4444 #ffffff', - 'status-toolbar.pythonversion': 'bg:#222222 #ffffff bold', - 'status-toolbar paste-mode-on': 'bg:#aa4444 #ffffff', - 'record': 'bg:#884444 white', - 'status-toolbar.input-mode': '#ffff44', - + "status-toolbar": "bg:#222222 #aaaaaa", + "status-toolbar.title": "underline", + "status-toolbar.inputmode": "bg:#222222 #ffffaa", + "status-toolbar.key": "bg:#000000 #888888", + "status-toolbar.pastemodeon": "bg:#aa4444 #ffffff", + "status-toolbar.pythonversion": "bg:#222222 #ffffff bold", + "status-toolbar paste-mode-on": "bg:#aa4444 #ffffff", + "record": "bg:#884444 white", + "status-toolbar.input-mode": "#ffff44", # The options sidebar. - 'sidebar': 'bg:#bbbbbb #000000', - 'sidebar.title': 'bg:#668866 #ffffff', - 'sidebar.label': 'bg:#bbbbbb #222222', - 'sidebar.status': 'bg:#dddddd #000011', - 'sidebar.label selected': 'bg:#222222 #eeeeee', - 'sidebar.status selected': 'bg:#444444 #ffffff bold', - - 'sidebar.separator': 'underline', - 'sidebar.key': 'bg:#bbddbb #000000 bold', - 'sidebar.key.description': 'bg:#bbbbbb #000000', - 'sidebar.helptext': 'bg:#fdf6e3 #000011', - -# # Styling for the history layout. -# history.line: '', -# history.line.selected: 'bg:#008800 #000000', -# history.line.current: 'bg:#ffffff #000000', -# history.line.selected.current: 'bg:#88ff88 #000000', -# history.existinginput: '#888888', - + "sidebar": "bg:#bbbbbb #000000", + "sidebar.title": "bg:#668866 #ffffff", + "sidebar.label": "bg:#bbbbbb #222222", + "sidebar.status": "bg:#dddddd #000011", + "sidebar.label selected": "bg:#222222 #eeeeee", + "sidebar.status selected": "bg:#444444 #ffffff bold", + "sidebar.separator": "underline", + "sidebar.key": "bg:#bbddbb #000000 bold", + "sidebar.key.description": "bg:#bbbbbb #000000", + "sidebar.helptext": "bg:#fdf6e3 #000011", + # # Styling for the history layout. + # history.line: '', + # history.line.selected: 'bg:#008800 #000000', + # history.line.current: 'bg:#ffffff #000000', + # history.line.selected.current: 'bg:#88ff88 #000000', + # history.existinginput: '#888888', # Help Window. - 'window-border': '#aaaaaa', - 'window-title': 'bg:#bbbbbb #000000', - + "window-border": "#aaaaaa", + "window-title": "bg:#bbbbbb #000000", # Meta-enter message. - 'accept-message': 'bg:#ffff88 #444444', - + "accept-message": "bg:#ffff88 #444444", # Exit confirmation. - 'exit-confirmation': 'bg:#884444 #ffffff', + "exit-confirmation": "bg:#884444 #ffffff", } # Some changes to get a bit more contrast on Windows consoles. # (They only support 16 colors.) if is_windows() and not is_conemu_ansi() and not is_windows_vt100_supported(): - default_ui_style.update({ - 'sidebar.title': 'bg:#00ff00 #ffffff', - 'exitconfirmation': 'bg:#ff4444 #ffffff', - 'toolbar.validation': 'bg:#ff4444 #ffffff', - - 'menu.completions.completion': 'bg:#ffffff #000000', - 'menu.completions.completion.current': 'bg:#aaaaaa #000000', - }) + default_ui_style.update( + { + "sidebar.title": "bg:#00ff00 #ffffff", + "exitconfirmation": "bg:#ff4444 #ffffff", + "toolbar.validation": "bg:#ff4444 #ffffff", + "menu.completions.completion": "bg:#ffffff #000000", + "menu.completions.completion.current": "bg:#aaaaaa #000000", + } + ) blue_ui_style = {} blue_ui_style.update(default_ui_style) -#blue_ui_style.update({ +# blue_ui_style.update({ # # Line numbers. # Token.LineNumber: '#aa6666', # @@ -192,4 +169,4 @@ def generate_style(python_style, ui_style): # Token.Menu.Completions.Meta.Current: 'bg:#00aaaa #000000', # Token.Menu.Completions.ProgressBar: 'bg:#aaaaaa', # Token.Menu.Completions.ProgressButton: 'bg:#000000', -#}) +# }) diff --git a/ptpython/utils.py b/ptpython/utils.py index 2cdf2491..130da34f 100644 --- a/ptpython/utils.py +++ b/ptpython/utils.py @@ -1,19 +1,19 @@ """ For internal use only. """ -from __future__ import unicode_literals - -from prompt_toolkit.mouse_events import MouseEventType import re +from typing import Callable, TypeVar, cast + +from prompt_toolkit.mouse_events import MouseEvent, MouseEventType -__all__ = ( - 'has_unclosed_brackets', - 'get_jedi_script_from_document', - 'document_is_multiline_python', -) +__all__ = [ + "has_unclosed_brackets", + "get_jedi_script_from_document", + "document_is_multiline_python", +] -def has_unclosed_brackets(text): +def has_unclosed_brackets(text: str) -> bool: """ Starting at the end of the string. If we find an opening bracket for which we didn't had a closing one yet, return True. @@ -21,17 +21,19 @@ def has_unclosed_brackets(text): stack = [] # Ignore braces inside strings - text = re.sub(r'''('[^']*'|"[^"]*")''', '', text) # XXX: handle escaped quotes.! + text = re.sub(r"""('[^']*'|"[^"]*")""", "", text) # XXX: handle escaped quotes.! for c in reversed(text): - if c in '])}': + if c in "])}": stack.append(c) - elif c in '[({': + elif c in "[({": if stack: - if ((c == '[' and stack[-1] == ']') or - (c == '{' and stack[-1] == '}') or - (c == '(' and stack[-1] == ')')): + if ( + (c == "[" and stack[-1] == "]") + or (c == "{" and stack[-1] == "}") + or (c == "(" and stack[-1] == ")") + ): stack.pop() else: # Opening bracket for which we didn't had a closing one. @@ -42,15 +44,17 @@ def has_unclosed_brackets(text): def get_jedi_script_from_document(document, locals, globals): import jedi # We keep this import in-line, to improve start-up time. - # Importing Jedi is 'slow'. + + # Importing Jedi is 'slow'. try: return jedi.Interpreter( document.text, column=document.cursor_position_col, line=document.cursor_position_row + 1, - path='input-text', - namespaces=[locals, globals]) + path="input-text", + namespaces=[locals, globals], + ) except ValueError: # Invalid cursor position. # ValueError('`column` parameter is not in a valid range.') @@ -70,14 +74,15 @@ def get_jedi_script_from_document(document, locals, globals): return None -_multiline_string_delims = re.compile('''[']{3}|["]{3}''') +_multiline_string_delims = re.compile("""[']{3}|["]{3}""") def document_is_multiline_python(document): """ Determine whether this is a multiline Python document. """ - def ends_in_multiline_string(): + + def ends_in_multiline_string() -> bool: """ ``True`` if we're inside a multiline string at the end of the text. """ @@ -90,28 +95,35 @@ def ends_in_multiline_string(): opening = None return bool(opening) - if '\n' in document.text or ends_in_multiline_string(): + if "\n" in document.text or ends_in_multiline_string(): return True - def line_ends_with_colon(): - return document.current_line.rstrip()[-1:] == ':' + def line_ends_with_colon() -> bool: + return document.current_line.rstrip()[-1:] == ":" # If we just typed a colon, or still have open brackets, always insert a real newline. - if line_ends_with_colon() or \ - (document.is_cursor_at_the_end and - has_unclosed_brackets(document.text_before_cursor)) or \ - document.text.startswith('@'): + if ( + line_ends_with_colon() + or ( + document.is_cursor_at_the_end + and has_unclosed_brackets(document.text_before_cursor) + ) + or document.text.startswith("@") + ): return True # If the character before the cursor is a backslash (line continuation # char), insert a new line. - elif document.text_before_cursor[-1:] == '\\': + elif document.text_before_cursor[-1:] == "\\": return True return False -def if_mousedown(handler): +_T = TypeVar("_T", bound=Callable[[MouseEvent], None]) + + +def if_mousedown(handler: _T) -> _T: """ Decorator for mouse handlers. Only handle event when the user pressed mouse down. @@ -119,9 +131,11 @@ def if_mousedown(handler): (When applied to a token list. Scroll events will bubble up and are handled by the Window.) """ - def handle_if_mouse_down(mouse_event): + + def handle_if_mouse_down(mouse_event: MouseEvent): if mouse_event.event_type == MouseEventType.MOUSE_DOWN: return handler(mouse_event) else: return NotImplemented - return handle_if_mouse_down + + return cast(_T, handle_if_mouse_down) diff --git a/ptpython/validator.py b/ptpython/validator.py index 80cc3fb1..b7880bf6 100644 --- a/ptpython/validator.py +++ b/ptpython/validator.py @@ -1,10 +1,7 @@ -from __future__ import unicode_literals +from prompt_toolkit.validation import ValidationError, Validator -from prompt_toolkit.validation import Validator, ValidationError +__all__ = ["PythonValidator"] -__all__ = ( - 'PythonValidator', -) class PythonValidator(Validator): """ @@ -13,6 +10,7 @@ class PythonValidator(Validator): :param get_compiler_flags: Callable that returns the currently active compiler flags. """ + def __init__(self, get_compiler_flags=None): self.get_compiler_flags = get_compiler_flags @@ -22,7 +20,7 @@ def validate(self, document): """ # When the input starts with Ctrl-Z, always accept. This means EOF in a # Python REPL. - if document.text.startswith('\x1a'): + if document.text.startswith("\x1a"): return try: @@ -31,17 +29,19 @@ def validate(self, document): else: flags = 0 - compile(document.text, '', 'exec', flags=flags, dont_inherit=True) + compile(document.text, "", "exec", flags=flags, dont_inherit=True) except SyntaxError as e: # Note, the 'or 1' for offset is required because Python 2.7 # gives `None` as offset in case of '4=4' as input. (Looks like # fixed in Python 3.) - index = document.translate_row_col_to_index(e.lineno - 1, (e.offset or 1) - 1) - raise ValidationError(index, 'Syntax Error') + index = document.translate_row_col_to_index( + e.lineno - 1, (e.offset or 1) - 1 + ) + raise ValidationError(index, "Syntax Error") except TypeError as e: # e.g. "compile() expected string without null bytes" raise ValidationError(0, str(e)) except ValueError as e: # In Python 2, compiling "\x9" (an invalid escape sequence) raises # ValueError instead of SyntaxError. - raise ValidationError(0, 'Syntax Error: %s' % e) + raise ValidationError(0, "Syntax Error: %s" % e) diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 00000000..b356239f --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,13 @@ +[tool.black] +target-version = ['py36'] + + +[tool.isort] +# isort configuration that is compatible with Black. +multi_line_output = 3 +include_trailing_comma = true +known_first_party = "ptpython" +known_third_party = "prompt_toolkit,pygments,asyncssh" +force_grid_wrap = 0 +use_parentheses = true +line_length = 88 diff --git a/setup.py b/setup.py index e884f3c4..b652877a 100644 --- a/setup.py +++ b/setup.py @@ -1,43 +1,48 @@ #!/usr/bin/env python import os import sys -from setuptools import setup, find_packages -with open(os.path.join(os.path.dirname(__file__), 'README.rst')) as f: +from setuptools import find_packages, setup + +with open(os.path.join(os.path.dirname(__file__), "README.rst")) as f: long_description = f.read() setup( - name='ptpython', - author='Jonathan Slenders', - version='2.0.5', - url='https://github.com/jonathanslenders/ptpython', - description='Python REPL build on top of prompt_toolkit', + name="ptpython", + author="Jonathan Slenders", + version="2.0.5", + url="https://github.com/prompt-toolkit/ptpython", + description="Python REPL build on top of prompt_toolkit", long_description=long_description, - packages=find_packages('.'), - install_requires = [ - 'appdirs', - 'docopt', - 'jedi>=0.9.0', - 'prompt_toolkit>=2.0.8,<2.1.0', - 'pygments', + packages=find_packages("."), + install_requires=[ + "appdirs", + "jedi>=0.9.0", + "prompt_toolkit>=3.0.0,<3.1.0", + "pygments", ], + python_requires=">=3.6", classifiers=[ - 'Programming Language :: Python', - 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 2', + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.6", + "Programming Language :: Python :: 3.7", + "Programming Language :: Python :: 3.8", + "Programming Language :: Python :: 3 :: Only", + "Programming Language :: Python", ], entry_points={ - 'console_scripts': [ - 'ptpython = ptpython.entry_points.run_ptpython:run', - 'ptipython = ptpython.entry_points.run_ptipython:run', - 'ptpython%s = ptpython.entry_points.run_ptpython:run' % sys.version_info[0], - 'ptpython%s.%s = ptpython.entry_points.run_ptpython:run' % sys.version_info[:2], - 'ptipython%s = ptpython.entry_points.run_ptipython:run' % sys.version_info[0], - 'ptipython%s.%s = ptpython.entry_points.run_ptipython:run' % sys.version_info[:2], + "console_scripts": [ + "ptpython = ptpython.entry_points.run_ptpython:run", + "ptipython = ptpython.entry_points.run_ptipython:run", + "ptpython%s = ptpython.entry_points.run_ptpython:run" % sys.version_info[0], + "ptpython%s.%s = ptpython.entry_points.run_ptpython:run" + % sys.version_info[:2], + "ptipython%s = ptpython.entry_points.run_ptipython:run" + % sys.version_info[0], + "ptipython%s.%s = ptpython.entry_points.run_ptipython:run" + % sys.version_info[:2], ] }, - extras_require={ - 'ptipython': ['ipython'] # For ptipython, we need to have IPython - } + extras_require={"ptipython": ["ipython"]}, # For ptipython, we need to have IPython ) diff --git a/tests/run_tests.py b/tests/run_tests.py index a23fddec..2f945163 100755 --- a/tests/run_tests.py +++ b/tests/run_tests.py @@ -1,17 +1,9 @@ #!/usr/bin/env python -from __future__ import unicode_literals - import unittest - -# For now there are no tests here. -# However this is sufficient for Travis to do at least a syntax check. -# That way we are at least sure to restrict to the Python 2.6 syntax. - import ptpython.completer -import ptpython.filters -#import ptpython.ipython import ptpython.eventloop +import ptpython.filters import ptpython.history_browser import ptpython.key_bindings import ptpython.layout @@ -21,6 +13,10 @@ import ptpython.utils import ptpython.validator +# For now there are no tests here. +# However this is sufficient for Travis to do at least a syntax check. +# That way we are at least sure to restrict to the Python 2.6 syntax. + -if __name__ == '__main__': +if __name__ == "__main__": unittest.main() From 6569e2451b1cdb60ee6737b091ae153a6e93f2cb Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Mon, 27 Jan 2020 22:41:54 +0100 Subject: [PATCH 016/220] Create parent directories for configuration. --- ptpython/entry_points/run_ptpython.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ptpython/entry_points/run_ptpython.py b/ptpython/entry_points/run_ptpython.py index a8710792..204a94aa 100644 --- a/ptpython/entry_points/run_ptpython.py +++ b/ptpython/entry_points/run_ptpython.py @@ -17,6 +17,7 @@ """ import argparse import os +import pathlib import sys from typing import Tuple @@ -66,8 +67,7 @@ def get_config_and_history_file(namespace: argparse.Namespace) -> Tuple[str, str # Create directories. for d in (config_dir, data_dir): - if not os.path.isdir(d) and not os.path.islink(d): - os.mkdir(d) + pathlib.Path(d).mkdir(parents=True, exist_ok=True) # Determine config file to be used. config_file = os.path.join(config_dir, "config.py") From 27f5bcd8493df726277a1562ff52b1c8ff5b5285 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Wed, 29 Jan 2020 22:26:41 +0100 Subject: [PATCH 017/220] Release 3.0.0 --- CHANGELOG | 9 +++++++++ README.rst | 3 +++ setup.py | 2 +- 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index c64a87d1..6dfbcc61 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,15 @@ CHANGELOG ========= +3.0.0: 2020-01-29 +----------------- + +Upgrade to prompt_toolkit 3.0. +Requires at least Python 3.6. + +New features: +- Uses XDG base directory specification. + 2.0.5: 2019-10-09 ----------------- diff --git a/README.rst b/README.rst index f394054d..da60c1e6 100644 --- a/README.rst +++ b/README.rst @@ -15,6 +15,9 @@ Ptpython is an advanced Python REPL. It should work on all Python versions from 2.6 up to 3.7 and work cross platform (Linux, BSD, OS X and Windows). +Note: this version of ptpython requires at least Python 3.6. Install ptpython +2.0.5 for older Python versions. + Installation ************ diff --git a/setup.py b/setup.py index b652877a..8f4eec58 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ setup( name="ptpython", author="Jonathan Slenders", - version="2.0.5", + version="3.0.0", url="https://github.com/prompt-toolkit/ptpython", description="Python REPL build on top of prompt_toolkit", long_description=long_description, From d21c480ac1f7e9c22dae429f6be73f53601eb455 Mon Sep 17 00:00:00 2001 From: Mikaeil Orfanian Date: Mon, 17 Feb 2020 15:46:59 +0100 Subject: [PATCH 018/220] Retain backwards compatibility Some other libraries (e.g. django-extensions) rely on the previous signature of this function. This change makes the function signature more flexible so current and older usages don't break. --- ptpython/repl.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ptpython/repl.py b/ptpython/repl.py index 4b8edf2a..e3f04e5c 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -268,7 +268,7 @@ def enable_deprecation_warnings() -> None: warnings.filterwarnings("default", category=DeprecationWarning, module="__main__") -def run_config(repl: PythonInput, config_file: str) -> None: +def run_config(repl: PythonInput, config_file: str="~/.ptpython/config.py") -> None: """ Execute REPL config file. From fb0f7cb650b4ab23a99354efed0ba446b46d8dd5 Mon Sep 17 00:00:00 2001 From: Mikaeil Orfanian Date: Mon, 17 Feb 2020 15:54:59 +0100 Subject: [PATCH 019/220] Blacken --- ptpython/repl.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ptpython/repl.py b/ptpython/repl.py index e3f04e5c..06062dc3 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -268,7 +268,7 @@ def enable_deprecation_warnings() -> None: warnings.filterwarnings("default", category=DeprecationWarning, module="__main__") -def run_config(repl: PythonInput, config_file: str="~/.ptpython/config.py") -> None: +def run_config(repl: PythonInput, config_file: str = "~/.ptpython/config.py") -> None: """ Execute REPL config file. From c6952a8c81ecd197fffe2864fba83f8d5feb5972 Mon Sep 17 00:00:00 2001 From: sblondon Date: Mon, 3 Feb 2020 15:43:33 +0100 Subject: [PATCH 020/220] Emacs commands in lowercases Hello, the commands for Emacs uses lowercases for 'x' and 'e' so this PR changes the cases to fit this. It's like the Emacs documentation (par example https://www.gnu.org/software/emacs/manual/html_node/emacs/Keys.html#Keys). --- examples/ptpython_config/config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/ptpython_config/config.py b/examples/ptpython_config/config.py index ff8b8ac1..3a1175ca 100644 --- a/examples/ptpython_config/config.py +++ b/examples/ptpython_config/config.py @@ -91,7 +91,7 @@ def configure(repl): # based on the history.) repl.enable_auto_suggest = False - # Enable open-in-editor. Pressing C-X C-E in emacs mode or 'v' in + # Enable open-in-editor. Pressing C-x C-e in emacs mode or 'v' in # Vi navigation mode will open the input in the current editor. repl.enable_open_in_editor = True From 311c26feca034d4b99728bff20c267bbcd757c53 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Sun, 23 Feb 2020 05:56:50 +0100 Subject: [PATCH 021/220] Fix input mode in status bar for block selection. --- ptpython/layout.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ptpython/layout.py b/ptpython/layout.py index 7b68b2d4..bf783c6a 100644 --- a/ptpython/layout.py +++ b/ptpython/layout.py @@ -429,7 +429,7 @@ def toggle_vi_mode(mouse_event: MouseEvent) -> None: elif app.current_buffer.selection_state.type == SelectionType.CHARACTERS: append((input_mode_t, "Vi (VISUAL)", toggle_vi_mode)) append((token, " ")) - elif app.current_buffer.selection_state.type == "BLOCK": + elif app.current_buffer.selection_state.type == SelectionType.BLOCK: append((input_mode_t, "Vi (VISUAL BLOCK)", toggle_vi_mode)) append((token, " ")) elif mode in (InputMode.INSERT, "vi-insert-multiple"): From ee551a37fcc9f02fb9b29070460ab26364adaad9 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Sun, 23 Feb 2020 05:58:56 +0100 Subject: [PATCH 022/220] Release 3.0.1 --- CHANGELOG | 8 ++++++++ setup.py | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 6dfbcc61..a90d86e1 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,14 @@ CHANGELOG ========= +3.0.1: 2020-02-24 +----------------- + +- Fix backwards-compatibility of the `run_config` function. (used by + django-extensions). +- Fix input mode in status bar for block selection. + + 3.0.0: 2020-01-29 ----------------- diff --git a/setup.py b/setup.py index 8f4eec58..8fbc277c 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ setup( name="ptpython", author="Jonathan Slenders", - version="3.0.0", + version="3.0.1", url="https://github.com/prompt-toolkit/ptpython", description="Python REPL build on top of prompt_toolkit", long_description=long_description, From 7e204a094596bd436a10700ddea42991afa27408 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Wed, 8 Apr 2020 16:44:22 +0200 Subject: [PATCH 023/220] Improved dictionary completion. - Also complete list indexes. - Also complete attributes after doing a dictionary lookup. - Also complete on iterators in a for-loop. --- ptpython/completer.py | 225 +++++++++++++++++++++++++++++++++------ ptpython/python_input.py | 2 +- 2 files changed, 193 insertions(+), 34 deletions(-) diff --git a/ptpython/completer.py b/ptpython/completer.py index 2ffaf62e..46995ba7 100644 --- a/ptpython/completer.py +++ b/ptpython/completer.py @@ -1,7 +1,7 @@ import ast import keyword import re -from typing import TYPE_CHECKING, Iterable +from typing import TYPE_CHECKING, Any, Dict, Iterable from prompt_toolkit.completion import ( CompleteEvent, @@ -129,7 +129,10 @@ def get_completions( for c in self.dictionary_completer.get_completions( document, complete_event ): - has_dict_completions = True + if c.text not in '[.': + # If we get the [ or . completion, still include the other + # completions. + has_dict_completions = True yield c if has_dict_completions: return @@ -206,8 +209,8 @@ class DictionaryCompleter(Completer): """ Experimental completer for Python dictionary keys. - Warning: This does an `eval` on the Python object before the open square - bracket, which is potentially dangerous. It doesn't match on + Warning: This does an `eval` and `repr` on some Python expressions before + the cursor, which is potentially dangerous. It doesn't match on function calls, so it only triggers attribute access. """ @@ -217,31 +220,58 @@ def __init__(self, get_globals, get_locals): self.get_globals = get_globals self.get_locals = get_locals - self.pattern = re.compile( - r""" - # Any expression safe enough to eval while typing. - # No operators, except dot, and only other dict lookups. - # Technically, this can be unsafe of course, if bad code runs - # in `__getattr__` or ``__getitem__``. - ( - # Variable name - [a-zA-Z0-9_]+ - - \s* + # Pattern for expressions that are "safe" to eval for auto-completion. + # These are expressions that contain only attribute and index lookups. + expression = r""" + # Any expression safe enough to eval while typing. + # No operators, except dot, and only other dict lookups. + # Technically, this can be unsafe of course, if bad code runs + # in `__getattr__` or ``__getitem__``. + ( + # Variable name + [a-zA-Z0-9_]+ + + \s* + + (?: + # Attribute access. + \s* \. \s* [a-zA-Z0-9_]+ \s* + + | + + # Item lookup. + # (We match the square brackets. We don't care about + # matching quotes here in the regex. Nested square brackets + # are not supported.) + \s* \[ [a-zA-Z0-9_'"\s]+ \] \s* + )* + ) + """ - (?: - # Attribute access. - \s* \. \s* [a-zA-Z0-9_]+ \s* + # Pattern for recognizing for-loops, so that we can provide + # autocompletion on the iterator of the for-loop. (According to the + # first item of the collection we're iterating over.) + self.for_loop_pattern = re.compile( + rf""" + for \s+ ([a-zA-Z0-9_]+) \s+ in \s+ {expression} \s* : + """, + re.VERBOSE, + ) - | + # Pattern for matching a simple expression (for completing [ or . + # operators). + self.expression_pattern = re.compile( + rf""" + {expression} + $ + """, + re.VERBOSE, + ) - # Item lookup. - # (We match the square brackets. We don't care about - # matching quotes here in the regex. Nested square - # brackets are not supported.) - \s* \[ [a-zA-Z0-9_'"\s]+ \] \s* - )* - ) + # Pattern for matching item lookups. + self.item_lookup_pattern = re.compile( + rf""" + {expression} # Dict loopup to complete (square bracket open + start of # string). @@ -251,19 +281,97 @@ def __init__(self, get_globals, get_locals): re.VERBOSE, ) + # Pattern for matching attribute lookups. + self.attribute_lookup_pattern = re.compile( + rf""" + {expression} + + # Attribute loopup to complete (dot + varname). + \. + \s* ([a-zA-Z0-9_]*)$ + """, + re.VERBOSE, + ) + + def _lookup(self, expression: str, temp_locals: Dict[str, Any]) -> object: + """ + Do lookup of `object_var` in the context. + `temp_locals` is a dictionary, used for the locals. + """ + try: + return eval(expression.strip(), self.get_globals(), temp_locals) + except BaseException: + return # Many exception, like NameError can be thrown here. + def get_completions( self, document: Document, complete_event: CompleteEvent ) -> Iterable[Completion]: - match = self.pattern.search(document.text_before_cursor) + + # First, find all for-loops, and assing the first item of the + # collections they're iterating to the iterator variable, so that we + # can provide code completion on the iterators. + temp_locals = self.get_locals().copy() + + for match in self.for_loop_pattern.finditer(document.text_before_cursor): + varname, expression = match.groups() + expression_val = self._lookup(expression, temp_locals) + + # We do this only for lists and tuples. Calling `next()` on any + # collection would create undesired side effects. + if isinstance(expression_val, (list, tuple)) and expression_val: + temp_locals[varname] = expression_val[0] + + # Get all completions. + yield from self._get_expression_completions( + document, complete_event, temp_locals + ) + yield from self._get_item_lookup_completions( + document, complete_event, temp_locals + ) + yield from self._get_attribute_completions( + document, complete_event, temp_locals + ) + + def _do_repr(self, obj: object) -> str: + try: + return str(repr(obj)) + except BaseException: + raise ReprFailedError + + def _get_expression_completions( + self, + document: Document, + complete_event: CompleteEvent, + temp_locals: Dict[str, Any], + ) -> Iterable[Completion]: + """ + Complete the [ or . operator after an object. + """ + match = self.expression_pattern.search(document.text_before_cursor) + if match is not None: + object_var = match.groups()[0] + result = self._lookup(object_var, temp_locals) + + if isinstance(result, (list, tuple, dict)): + yield Completion("[", 0) + elif result: + yield Completion(".", 0) + + def _get_item_lookup_completions( + self, + document: Document, + complete_event: CompleteEvent, + temp_locals: Dict[str, Any], + ) -> Iterable[Completion]: + """ + Complete dictionary keys. + """ + match = self.item_lookup_pattern.search(document.text_before_cursor) if match is not None: object_var, key = match.groups() - object_var = object_var.strip() # Do lookup of `object_var` in the context. - try: - result = eval(object_var, self.get_globals(), self.get_locals()) - except BaseException: - return # Many exception, like NameError can be thrown here. + result = self._lookup(object_var, temp_locals) # If this object is a dictionary, complete the keys. if isinstance(result, dict): @@ -279,7 +387,58 @@ def get_completions( for k in result: if str(k).startswith(key_obj): - yield Completion(str(repr(k)), -len(key), display=str(repr(k))) + try: + k_repr = self._do_repr(k) + yield Completion( + k_repr + "]", + -len(key), + display=f"[{k_repr}]", + display_meta=self._do_repr(result[k]), + ) + except ReprFailedError: + pass + + # Complete list/tuple index keys. + elif isinstance(result, (list, tuple)): + if not key or key.isdigit(): + for k in range(min(len(result), 1000)): + if str(k).startswith(key): + try: + k_repr = self._do_repr(k) + yield Completion( + k_repr + "]", + -len(key), + display=f"[{k_repr}]", + display_meta=self._do_repr(result[k]), + ) + except ReprFailedError: + pass + + def _get_attribute_completions( + self, + document: Document, + complete_event: CompleteEvent, + temp_locals: Dict[str, Any], + ) -> Iterable[Completion]: + """ + Complete attribute names. + """ + match = self.attribute_lookup_pattern.search(document.text_before_cursor) + if match is not None: + object_var, attr_name = match.groups() + + # Do lookup of `object_var` in the context. + result = self._lookup(object_var, temp_locals) + + for name in dir(result): + if name.startswith(attr_name): + yield Completion( + name, -len(attr_name), + ) + + +class ReprFailedError(Exception): + " Raised when the repr() call in `DictionaryCompleter` fails. " try: diff --git a/ptpython/python_input.py b/ptpython/python_input.py index c4bbbd0c..462e9b0c 100644 --- a/ptpython/python_input.py +++ b/ptpython/python_input.py @@ -518,7 +518,7 @@ def get_values(): ), Option( title="Dictionary completion", - description="Enable experimental dictionary completion.\n" + description="Enable experimental dictionary/list completion.\n" 'WARNING: this does "eval" on fragments of\n' " your Python input and is\n" " potentially unsafe.", From 6bf312f0b118bcb2bc0fe58f12af0906ea4af4d7 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Tue, 14 Apr 2020 16:48:48 +0200 Subject: [PATCH 024/220] Added 'title' option to ptpython. --- examples/ptpython_config/config.py | 6 ++++++ ptpython/layout.py | 3 +++ ptpython/python_input.py | 2 ++ 3 files changed, 11 insertions(+) diff --git a/examples/ptpython_config/config.py b/examples/ptpython_config/config.py index 3a1175ca..9c7241f8 100644 --- a/examples/ptpython_config/config.py +++ b/examples/ptpython_config/config.py @@ -171,6 +171,12 @@ def _(event): b.insert_text(' ') """ + # Add a custom title to the status bar. This is useful when ptpython is + # embedded in other applications. + """ + repl.title = "My custom prompt." + """ + # Custom colorscheme for the UI. See `ptpython/layout.py` and # `ptpython/style.py` for all possible tokens. diff --git a/ptpython/layout.py b/ptpython/layout.py index bf783c6a..09f2c8ed 100644 --- a/ptpython/layout.py +++ b/ptpython/layout.py @@ -413,6 +413,9 @@ def toggle_vi_mode(mouse_event: MouseEvent) -> None: result: StyleAndTextTuples = [] append = result.append + if python_input.title: + result.extend(to_formatted_text(python_input.title)) + append((input_mode_t, "[F4] ", toggle_vi_mode)) # InputMode diff --git a/ptpython/python_input.py b/ptpython/python_input.py index 462e9b0c..eaf818d4 100644 --- a/ptpython/python_input.py +++ b/ptpython/python_input.py @@ -15,6 +15,7 @@ ThreadedAutoSuggest, ) from prompt_toolkit.buffer import Buffer +from prompt_toolkit.formatted_text import AnyFormattedText from prompt_toolkit.completion import Completer, FuzzyCompleter, ThreadedCompleter from prompt_toolkit.document import Document from prompt_toolkit.enums import DEFAULT_BUFFER, EditingMode @@ -204,6 +205,7 @@ def __init__( self.extra_key_bindings = extra_key_bindings or KeyBindings() # Settings. + self.title: AnyFormattedText = '' self.show_signature: bool = False self.show_docstring: bool = False self.show_meta_enter_message: bool = True From 75dceaa086b6659eab446143b0d995b50e82d9b2 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Tue, 14 Apr 2020 16:49:26 +0200 Subject: [PATCH 025/220] Added a few more type annotations. --- ptpython/completer.py | 2 +- ptpython/layout.py | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/ptpython/completer.py b/ptpython/completer.py index 46995ba7..65d88a46 100644 --- a/ptpython/completer.py +++ b/ptpython/completer.py @@ -301,7 +301,7 @@ def _lookup(self, expression: str, temp_locals: Dict[str, Any]) -> object: try: return eval(expression.strip(), self.get_globals(), temp_locals) except BaseException: - return # Many exception, like NameError can be thrown here. + return None # Many exception, like NameError can be thrown here. def get_completions( self, document: Document, complete_event: CompleteEvent diff --git a/ptpython/layout.py b/ptpython/layout.py index 09f2c8ed..5e114879 100644 --- a/ptpython/layout.py +++ b/ptpython/layout.py @@ -303,13 +303,13 @@ class PythonPromptMargin(PromptMargin): It shows something like "In [1]:". """ - def __init__(self, python_input): + def __init__(self, python_input) -> None: self.python_input = python_input def get_prompt_style(): return python_input.all_prompt_styles[python_input.prompt_style] - def get_prompt(): + def get_prompt() -> StyleAndTextTuples: return to_formatted_text(get_prompt_style().in_prompt()) def get_continuation(width, line_number, is_soft_wrap): @@ -508,7 +508,7 @@ def exit_confirmation( Create `Layout` for the exit message. """ - def get_text_fragments(): + def get_text_fragments() -> StyleAndTextTuples: # Show "Do you really want to exit?" return [ (style, "\n %s ([y]/n)" % python_input.exit_message), @@ -564,7 +564,7 @@ def __init__( extra_toolbars=None, extra_buffer_processors=None, input_buffer_height: Optional[AnyDimension] = None, - ): + ) -> None: D = Dimension extra_body = [extra_body] if extra_body else [] extra_toolbars = extra_toolbars or [] From 7346416270bc9d954958618dad61cd6116a760ea Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Tue, 14 Apr 2020 16:49:49 +0200 Subject: [PATCH 026/220] Fixed python-embed-with-custom-prompt example. --- examples/python-embed-with-custom-prompt.py | 46 +++++++-------------- ptpython/completer.py | 2 +- ptpython/prompt_style.py | 20 ++++----- ptpython/python_input.py | 2 +- ptpython/repl.py | 4 +- 5 files changed, 30 insertions(+), 44 deletions(-) diff --git a/examples/python-embed-with-custom-prompt.py b/examples/python-embed-with-custom-prompt.py index bf27e936..85fd97c9 100755 --- a/examples/python-embed-with-custom-prompt.py +++ b/examples/python-embed-with-custom-prompt.py @@ -5,46 +5,32 @@ from __future__ import unicode_literals from pygments.token import Token +from prompt_toolkit.formatted_text import HTML from ptpython.prompt_style import PromptStyle from ptpython.repl import embed def configure(repl): - # There are several ways to override the prompt. - - # 1. Probably, the best is to add a new PromptStyle to `all_prompt_styles` - # and activate it. This way, the other styles are still selectable from - # the menu. + # Probably, the best is to add a new PromptStyle to `all_prompt_styles` and + # activate it. This way, the other styles are still selectable from the + # menu. class CustomPrompt(PromptStyle): - def in_tokens(self, cli): - return [ - (Token.In, "Input["), - (Token.In.Number, "%s" % repl.current_statement_index), - (Token.In, "] >>: "), - ] - - def in2_tokens(self, cli, width): - return [(Token.In, "...: ".rjust(width))] - - def out_tokens(self, cli): - return [ - (Token.Out, "Result["), - (Token.Out.Number, "%s" % repl.current_statement_index), - (Token.Out, "]: "), - ] - - repl.all_prompt_styles["custom"] = CustomPrompt() - repl.prompt_style = "custom" + def in_prompt(self): + return HTML("Input[%s]: ") % ( + repl.current_statement_index, + ) - # 2. Assign a new callable to `get_input_prompt_tokens`. This will always take effect. - ## repl.get_input_prompt_tokens = lambda cli: [(Token.In, '[hello] >>> ')] + def in2_prompt(self, width): + return "...: ".rjust(width) - # 3. Also replace `get_input_prompt_tokens`, but still call the original. This inserts - # a prefix. + def out_prompt(self): + return HTML("Result[%s]: ") % ( + repl.current_statement_index, + ) - ## original = repl.get_input_prompt_tokens - ## repl.get_input_prompt_tokens = lambda cli: [(Token.In, '[prefix]')] + original(cli) + repl.all_prompt_styles["custom"] = CustomPrompt() + repl.prompt_style = "custom" def main(): diff --git a/ptpython/completer.py b/ptpython/completer.py index 65d88a46..d8ec87b9 100644 --- a/ptpython/completer.py +++ b/ptpython/completer.py @@ -129,7 +129,7 @@ def get_completions( for c in self.dictionary_completer.get_completions( document, complete_event ): - if c.text not in '[.': + if c.text not in "[.": # If we get the [ or . completion, still include the other # completions. has_dict_completions = True diff --git a/ptpython/prompt_style.py b/ptpython/prompt_style.py index d5e6ca8c..24e5f883 100644 --- a/ptpython/prompt_style.py +++ b/ptpython/prompt_style.py @@ -1,7 +1,7 @@ from abc import ABCMeta, abstractmethod from typing import TYPE_CHECKING -from prompt_toolkit.formatted_text import StyleAndTextTuples +from prompt_toolkit.formatted_text import AnyFormattedText if TYPE_CHECKING: from .python_input import PythonInput @@ -15,12 +15,12 @@ class PromptStyle(metaclass=ABCMeta): """ @abstractmethod - def in_prompt(self) -> StyleAndTextTuples: + def in_prompt(self) -> AnyFormattedText: " Return the input tokens. " return [] @abstractmethod - def in2_prompt(self, width: int) -> StyleAndTextTuples: + def in2_prompt(self, width: int) -> AnyFormattedText: """ Tokens for every following input line. @@ -30,7 +30,7 @@ def in2_prompt(self, width: int) -> StyleAndTextTuples: return [] @abstractmethod - def out_prompt(self) -> StyleAndTextTuples: + def out_prompt(self) -> AnyFormattedText: " Return the output tokens. " return [] @@ -43,17 +43,17 @@ class IPythonPrompt(PromptStyle): def __init__(self, python_input: "PythonInput") -> None: self.python_input = python_input - def in_prompt(self) -> StyleAndTextTuples: + def in_prompt(self) -> AnyFormattedText: return [ ("class:in", "In ["), ("class:in.number", "%s" % self.python_input.current_statement_index), ("class:in", "]: "), ] - def in2_prompt(self, width: int) -> StyleAndTextTuples: + def in2_prompt(self, width: int) -> AnyFormattedText: return [("class:in", "...: ".rjust(width))] - def out_prompt(self) -> StyleAndTextTuples: + def out_prompt(self) -> AnyFormattedText: return [ ("class:out", "Out["), ("class:out.number", "%s" % self.python_input.current_statement_index), @@ -67,11 +67,11 @@ class ClassicPrompt(PromptStyle): The classic Python prompt. """ - def in_prompt(self) -> StyleAndTextTuples: + def in_prompt(self) -> AnyFormattedText: return [("class:prompt", ">>> ")] - def in2_prompt(self, width: int) -> StyleAndTextTuples: + def in2_prompt(self, width: int) -> AnyFormattedText: return [("class:prompt.dots", "...")] - def out_prompt(self) -> StyleAndTextTuples: + def out_prompt(self) -> AnyFormattedText: return [] diff --git a/ptpython/python_input.py b/ptpython/python_input.py index eaf818d4..0fc2f85a 100644 --- a/ptpython/python_input.py +++ b/ptpython/python_input.py @@ -205,7 +205,7 @@ def __init__( self.extra_key_bindings = extra_key_bindings or KeyBindings() # Settings. - self.title: AnyFormattedText = '' + self.title: AnyFormattedText = "" self.show_signature: bool = False self.show_docstring: bool = False self.show_meta_enter_message: bool = True diff --git a/ptpython/repl.py b/ptpython/repl.py index 06062dc3..69c53e32 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -21,7 +21,7 @@ PygmentsTokens, merge_formatted_text, ) -from prompt_toolkit.formatted_text.utils import fragment_list_width +from prompt_toolkit.formatted_text import fragment_list_width, to_formatted_text from prompt_toolkit.patch_stdout import patch_stdout as patch_stdout_context from prompt_toolkit.shortcuts import clear_title, print_formatted_text, set_title from prompt_toolkit.utils import DummyContext @@ -152,7 +152,7 @@ def compile_with_flags(code: str, mode: str): locals["_"] = locals["_%i" % self.current_statement_index] = result if result is not None: - out_prompt = self.get_output_prompt() + out_prompt = to_formatted_text(self.get_output_prompt()) try: result_str = "%r\n" % (result,) From d8b4eae00f2c3fd3888ce78709331df2dd2e1ab3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Grignard?= Date: Fri, 20 Mar 2020 09:08:33 +0100 Subject: [PATCH 027/220] fix: custom REPL input/output --- ptpython/python_input.py | 10 ++++++++-- ptpython/repl.py | 2 ++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/ptpython/python_input.py b/ptpython/python_input.py index 0fc2f85a..c14f9393 100644 --- a/ptpython/python_input.py +++ b/ptpython/python_input.py @@ -324,7 +324,7 @@ def __init__( extra_toolbars=self._extra_toolbars, ) - self.app = self._create_application() + self.app = self._create_application(input, output) if vi_mode: self.app.editing_mode = EditingMode.VI @@ -728,7 +728,11 @@ def get_values(): ), ] - def _create_application(self) -> Application: + def _create_application( + self, + input: Optional[Input], + output: Optional[Output] + ) -> Application: """ Create an `Application` instance. """ @@ -758,6 +762,8 @@ def _create_application(self) -> Application: style_transformation=self.style_transformation, include_default_pygments_style=False, reverse_vi_search_direction=True, + input=input, + output=output, ) def _create_buffer(self) -> Buffer: diff --git a/ptpython/repl.py b/ptpython/repl.py index 69c53e32..c7e71663 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -187,6 +187,7 @@ def compile_with_flags(code: str, mode: str): style=self._current_style, style_transformation=self.style_transformation, include_default_pygments_style=False, + output=output, ) # If not a valid `eval` expression, run using `exec` instead. @@ -233,6 +234,7 @@ def _handle_exception(self, e: Exception) -> None: style=self._current_style, style_transformation=self.style_transformation, include_default_pygments_style=False, + output=output, ) output.write("%s\n" % e) From fdb9e018412cde643cf86c183396adbec3078aed Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Tue, 14 Apr 2020 16:59:54 +0200 Subject: [PATCH 028/220] Run Mypy in CI. --- .travis.yml | 3 +++ mypy.ini | 6 ++++++ ptpython/contrib/asyncssh_repl.py | 4 ++-- 3 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 mypy.ini diff --git a/.travis.yml b/.travis.yml index 21611f91..6b1b8d65 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,3 +18,6 @@ script: - isort -c -rc ptpython tests setup.py examples - black --check ptpython setup.py examples + + # Type checking + - mypy ptpython diff --git a/mypy.ini b/mypy.ini new file mode 100644 index 00000000..5a7ef2eb --- /dev/null +++ b/mypy.ini @@ -0,0 +1,6 @@ +[mypy] +ignore_missing_imports = True +no_implicit_optional = True +platform = win32 +strict_equality = True +strict_optional = True diff --git a/ptpython/contrib/asyncssh_repl.py b/ptpython/contrib/asyncssh_repl.py index 29c63afb..4c36217d 100644 --- a/ptpython/contrib/asyncssh_repl.py +++ b/ptpython/contrib/asyncssh_repl.py @@ -7,7 +7,7 @@ package should be installable in Python 2 as well! """ import asyncio -from typing import Optional, TextIO, cast +from typing import Any, Optional, TextIO, cast import asyncssh from prompt_toolkit.data_structures import Size @@ -31,7 +31,7 @@ class ReplSSHServerSession(asyncssh.SSHServerSession): def __init__( self, get_globals: _GetNamespace, get_locals: Optional[_GetNamespace] = None ) -> None: - self._chan = None + self._chan: Any = None def _globals() -> dict: data = get_globals() From c1aaf400d2747653012df36e1acdef6a47f3cbb8 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Tue, 14 Apr 2020 17:50:01 +0200 Subject: [PATCH 029/220] Fixed sorting of imports. --- examples/python-embed-with-custom-prompt.py | 2 +- ptpython/python_input.py | 2 +- ptpython/repl.py | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/examples/python-embed-with-custom-prompt.py b/examples/python-embed-with-custom-prompt.py index 85fd97c9..05417282 100755 --- a/examples/python-embed-with-custom-prompt.py +++ b/examples/python-embed-with-custom-prompt.py @@ -4,8 +4,8 @@ """ from __future__ import unicode_literals -from pygments.token import Token from prompt_toolkit.formatted_text import HTML +from pygments.token import Token from ptpython.prompt_style import PromptStyle from ptpython.repl import embed diff --git a/ptpython/python_input.py b/ptpython/python_input.py index c14f9393..6140826a 100644 --- a/ptpython/python_input.py +++ b/ptpython/python_input.py @@ -15,11 +15,11 @@ ThreadedAutoSuggest, ) from prompt_toolkit.buffer import Buffer -from prompt_toolkit.formatted_text import AnyFormattedText from prompt_toolkit.completion import Completer, FuzzyCompleter, ThreadedCompleter from prompt_toolkit.document import Document from prompt_toolkit.enums import DEFAULT_BUFFER, EditingMode from prompt_toolkit.filters import Condition +from prompt_toolkit.formatted_text import AnyFormattedText from prompt_toolkit.history import ( FileHistory, History, diff --git a/ptpython/repl.py b/ptpython/repl.py index c7e71663..897af693 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -19,9 +19,10 @@ from prompt_toolkit.formatted_text import ( FormattedText, PygmentsTokens, + fragment_list_width, merge_formatted_text, + to_formatted_text, ) -from prompt_toolkit.formatted_text import fragment_list_width, to_formatted_text from prompt_toolkit.patch_stdout import patch_stdout as patch_stdout_context from prompt_toolkit.shortcuts import clear_title, print_formatted_text, set_title from prompt_toolkit.utils import DummyContext From 469ef08b1c9b2bbc124c8d0cf03f98abdd9da349 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Tue, 14 Apr 2020 17:51:29 +0200 Subject: [PATCH 030/220] Added mypy to 'pip install' in .travis.yml. --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 6b1b8d65..7061cb5d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,7 +7,7 @@ matrix: - python: 3.7 install: - - travis_retry pip install . pytest isort black + - travis_retry pip install . pytest isort black mypy - pip list script: From 954be776bd1acc20d060ea297ea15e1e0b4a4d21 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Tue, 14 Apr 2020 22:29:02 +0200 Subject: [PATCH 031/220] Fixed code formatting. --- ptpython/python_input.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/ptpython/python_input.py b/ptpython/python_input.py index 6140826a..20eb5d90 100644 --- a/ptpython/python_input.py +++ b/ptpython/python_input.py @@ -729,9 +729,7 @@ def get_values(): ] def _create_application( - self, - input: Optional[Input], - output: Optional[Output] + self, input: Optional[Input], output: Optional[Output] ) -> Application: """ Create an `Application` instance. From ee18cb77675521474334fb4e0ac1e8bc5bb0adbf Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Tue, 14 Apr 2020 22:32:10 +0200 Subject: [PATCH 032/220] Added badges to README. --- README.rst | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/README.rst b/README.rst index da60c1e6..38e34ce5 100644 --- a/README.rst +++ b/README.rst @@ -1,6 +1,8 @@ ptpython ======== +|Build Status| |PyPI| |License| + *A better Python REPL* :: @@ -205,9 +207,12 @@ Special thanks to - `wcwidth `_: Determine columns needed for a wide characters. - `prompt_toolkit `_ for the interface. -.. |Build Status| image:: https://api.travis-ci.org/jonathanslenders/ptpython.svg?branch=master - :target: https://travis-ci.org/jonathanslenders/ptpython# +.. |Build Status| image:: https://api.travis-ci.org/prompt-toolkit/ptpython.svg?branch=master + :target: https://travis-ci.org/prompt-toolkit/ptpython# + +.. |License| image:: https://img.shields.io/github/license/prompt-toolkit/ptpython.svg + :target: https://github.com/prompt-toolkit/ptpython/blob/master/LICENSE -.. |PyPI| image:: https://pypip.in/version/prompt-toolkit/badge.svg - :target: https://pypi.python.org/pypi/prompt-toolkit/ +.. |PyPI| image:: https://pypip.in/version/ptpython/badge.svg + :target: https://pypi.python.org/pypi/ptpython/ :alt: Latest Version From 448b65555ec28deefb5c2700042c71e505faa685 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Tue, 14 Apr 2020 22:36:01 +0200 Subject: [PATCH 033/220] Fixed badges location in README. --- README.rst | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.rst b/README.rst index 38e34ce5..481238a3 100644 --- a/README.rst +++ b/README.rst @@ -11,8 +11,6 @@ ptpython .. image :: https://github.com/jonathanslenders/ptpython/raw/master/docs/images/example1.png -|Build Status| - Ptpython is an advanced Python REPL. It should work on all Python versions from 2.6 up to 3.7 and work cross platform (Linux, BSD, OS X and Windows). From 89017ba158ed1d95319233fa5aedf3931c3b8b77 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Tue, 14 Apr 2020 22:47:44 +0200 Subject: [PATCH 034/220] Release 3.0.2 --- CHANGELOG | 12 ++++++++++++ setup.py | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index a90d86e1..2c385c6f 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,18 @@ CHANGELOG ========= +3.0.2: 2020-04-14 +----------------- + +New features: +- Improved custom dictionary completion: + * Also complete list indexes. + * Also complete attributes after doing a dictionary lookup. + * Also complete iterators in a for-loop. +- Added a 'title' option, so that applications embedding ptpython can set a + title in the status bar. + + 3.0.1: 2020-02-24 ----------------- diff --git a/setup.py b/setup.py index 8fbc277c..4a59ad80 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ setup( name="ptpython", author="Jonathan Slenders", - version="3.0.1", + version="3.0.2", url="https://github.com/prompt-toolkit/ptpython", description="Python REPL build on top of prompt_toolkit", long_description=long_description, From 493c48e280ecf62099f089d411587b473781cfe9 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Thu, 9 Jul 2020 17:54:42 +0200 Subject: [PATCH 035/220] Sort attribute names for DictionaryCompleter and move underscored attributes to the end. --- ptpython/completer.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/ptpython/completer.py b/ptpython/completer.py index d8ec87b9..00ba8258 100644 --- a/ptpython/completer.py +++ b/ptpython/completer.py @@ -1,7 +1,7 @@ import ast import keyword import re -from typing import TYPE_CHECKING, Any, Dict, Iterable +from typing import TYPE_CHECKING, Any, Dict, Iterable, List from prompt_toolkit.completion import ( CompleteEvent, @@ -430,12 +430,29 @@ def _get_attribute_completions( # Do lookup of `object_var` in the context. result = self._lookup(object_var, temp_locals) - for name in dir(result): + names = self._sort_attribute_names(dir(result)) + + for name in names: if name.startswith(attr_name): yield Completion( name, -len(attr_name), ) + def _sort_attribute_names(self, names: List[str]) -> List[str]: + """ + Sort attribute names alphabetically, but move the double underscore and + underscore names to the end. + """ + + def sort_key(name: str): + if name.startswith("__"): + return (2, name) # Double underscore comes latest. + if name.startswith("_"): + return (1, name) # Single underscore before that. + return (0, name) # Other names first. + + return sorted(names, key=sort_key) + class ReprFailedError(Exception): " Raised when the repr() call in `DictionaryCompleter` fails. " From 82e61370682c46fe39fd81f194072429340bb240 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Fri, 10 Jul 2020 15:58:32 +0200 Subject: [PATCH 036/220] Isort fixes. --- ptpython/entry_points/run_ptipython.py | 2 +- ptpython/eventloop.py | 3 ++- ptpython/ipython.py | 8 ++++---- ptpython/layout.py | 2 +- ptpython/python_input.py | 3 ++- 5 files changed, 10 insertions(+), 8 deletions(-) diff --git a/ptpython/entry_points/run_ptipython.py b/ptpython/entry_points/run_ptipython.py index e7bcf39a..650633ec 100644 --- a/ptpython/entry_points/run_ptipython.py +++ b/ptpython/entry_points/run_ptipython.py @@ -19,7 +19,7 @@ def run(user_ns=None): sys.exit(1) else: from ptpython.ipython import embed - from ptpython.repl import run_config, enable_deprecation_warnings + from ptpython.repl import enable_deprecation_warnings, run_config # Add the current directory to `sys.path`. if sys.path[0] != "": diff --git a/ptpython/eventloop.py b/ptpython/eventloop.py index 1e8c46a3..c841972d 100644 --- a/ptpython/eventloop.py +++ b/ptpython/eventloop.py @@ -19,9 +19,10 @@ def _inputhook_tk(inputhook_context): Run the Tk eventloop until prompt-toolkit needs to process the next input. """ # Get the current TK application. - import _tkinter # Keep this imports inline! import tkinter + import _tkinter # Keep this imports inline! + root = tkinter._default_root def wait_using_filehandler(): diff --git a/ptpython/ipython.py b/ptpython/ipython.py index 20f29bdc..169aa2db 100644 --- a/ptpython/ipython.py +++ b/ptpython/ipython.py @@ -8,6 +8,10 @@ offer. """ +from IPython import utils as ipy_utils +from IPython.core.inputsplitter import IPythonInputSplitter +from IPython.terminal.embed import InteractiveShellEmbed as _InteractiveShellEmbed +from IPython.terminal.ipapp import load_default_config from prompt_toolkit.completion import ( Completer, Completion, @@ -24,10 +28,6 @@ from prompt_toolkit.styles import Style from pygments.lexers import BashLexer, PythonLexer -from IPython import utils as ipy_utils -from IPython.core.inputsplitter import IPythonInputSplitter -from IPython.terminal.embed import InteractiveShellEmbed as _InteractiveShellEmbed -from IPython.terminal.ipapp import load_default_config from ptpython.prompt_style import PromptStyle from .python_input import PythonCompleter, PythonInput, PythonValidator diff --git a/ptpython/layout.py b/ptpython/layout.py index 5e114879..3940e7a1 100644 --- a/ptpython/layout.py +++ b/ptpython/layout.py @@ -57,7 +57,7 @@ from .utils import if_mousedown if TYPE_CHECKING: - from .python_input import PythonInput, OptionCategory + from .python_input import OptionCategory, PythonInput __all__ = ["PtPythonLayout", "CompletionVisualisation"] diff --git a/ptpython/python_input.py b/ptpython/python_input.py index 20eb5d90..7c57cf1e 100644 --- a/ptpython/python_input.py +++ b/ptpython/python_input.py @@ -891,9 +891,10 @@ def enter_history(self) -> None: history = PythonHistory(self, self.default_buffer.document) - from prompt_toolkit.application import in_terminal import asyncio + from prompt_toolkit.application import in_terminal + async def do_in_terminal() -> None: async with in_terminal(): result = await history.app.run_async() From 356dc481dadac56f636747daedeb46d0bb0f9321 Mon Sep 17 00:00:00 2001 From: Linus Pithan Date: Mon, 22 Jun 2020 11:31:28 +0200 Subject: [PATCH 037/220] get rid of 'Unhandled exception in event loop' caused by `get_compiler_flags` --- ptpython/python_input.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/ptpython/python_input.py b/ptpython/python_input.py index 7c57cf1e..bddbb2ef 100644 --- a/ptpython/python_input.py +++ b/ptpython/python_input.py @@ -361,8 +361,14 @@ def get_compiler_flags(self) -> int: flags = 0 for value in self.get_globals().values(): - if isinstance(value, __future__._Feature): - flags |= value.compiler_flag + try: + if isinstance(value, __future__._Feature): + f = value.compiler_flag + flags |= f + except BaseException: + # get_compiler_flags should never raise to not run into an + # `Unhandled exception in event loop` + pass return flags From 58e3cadf50914b630d99c0c48ccec65fa7488d04 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Fri, 10 Jul 2020 16:08:00 +0200 Subject: [PATCH 038/220] Added comment to 'get_compiler_flags' error handling. --- ptpython/python_input.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ptpython/python_input.py b/ptpython/python_input.py index bddbb2ef..e41b921f 100644 --- a/ptpython/python_input.py +++ b/ptpython/python_input.py @@ -368,6 +368,10 @@ def get_compiler_flags(self) -> int: except BaseException: # get_compiler_flags should never raise to not run into an # `Unhandled exception in event loop` + + # See: https://github.com/prompt-toolkit/ptpython/issues/351 + # An exception can be raised when some objects in the globals + # raise an exception in a custom `__getattribute__`. pass return flags From 458d26b377714d05570ea66742756535676bc6f8 Mon Sep 17 00:00:00 2001 From: Vincent Michel Date: Sat, 23 May 2020 11:45:29 +0200 Subject: [PATCH 039/220] Improve PythonRepl.run_async() method --- ptpython/repl.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/ptpython/repl.py b/ptpython/repl.py index 897af693..8633890e 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -94,9 +94,23 @@ def prompt() -> str: clear_title() async def run_async(self) -> None: + if self.terminal_title: + set_title(self.terminal_title) + while True: - text = await self.app.run_async() - self._process_text(text) + # 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() def _process_text(self, line: str) -> None: From f1ad66298972088ba4e4b72c847b7688167f14cc Mon Sep 17 00:00:00 2001 From: Vincent Michel Date: Sat, 23 May 2020 11:58:04 +0200 Subject: [PATCH 040/220] Remove duplicated code in PythonRepl.run() --- ptpython/repl.py | 55 ++++++++++++++++-------------------------------- 1 file changed, 18 insertions(+), 37 deletions(-) diff --git a/ptpython/repl.py b/ptpython/repl.py index 8633890e..237f6fb7 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -55,43 +55,24 @@ def _load_start_paths(self) -> None: output.write("WARNING | File not found: {}\n\n".format(path)) 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() + # 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.pt_loop.run_until_complete(self.run_async()) + finally: + # Restore the original event loop. + asyncio.set_event_loop(old_loop) async def run_async(self) -> None: if self.terminal_title: From 1fe521a7023181cdebeaec283fca498ace41a690 Mon Sep 17 00:00:00 2001 From: Julian Berman Date: Fri, 8 May 2020 08:00:21 -0400 Subject: [PATCH 041/220] Fix ptpython --version. Previously it'd just start the interpreter and not do anything. --- ptpython/entry_points/run_ptpython.py | 10 +++++++++- setup.py | 1 + 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/ptpython/entry_points/run_ptpython.py b/ptpython/entry_points/run_ptpython.py index 204a94aa..d2c382ff 100644 --- a/ptpython/entry_points/run_ptpython.py +++ b/ptpython/entry_points/run_ptpython.py @@ -21,6 +21,11 @@ import sys from typing import Tuple +try: + from importlib import metadata +except ImportError: + import importlib_metadata as metadata + import appdirs from prompt_toolkit.formatted_text import HTML from prompt_toolkit.shortcuts import print_formatted_text @@ -51,7 +56,10 @@ def create_parser() -> _Parser: ) parser.add_argument("--history-file", type=str, help="Location of history file.") parser.add_argument( - "-V", "--version", action="store_true", help="Print version and exit." + "-V", + "--version", + action="version", + version=metadata.version("ptpython"), ) parser.add_argument("args", nargs="*", help="Script and arguments") return parser diff --git a/setup.py b/setup.py index 4a59ad80..288870dd 100644 --- a/setup.py +++ b/setup.py @@ -18,6 +18,7 @@ packages=find_packages("."), install_requires=[ "appdirs", + "importlib_metadata;python_version<'3.8'", "jedi>=0.9.0", "prompt_toolkit>=3.0.0,<3.1.0", "pygments", From f5e7fb4e5515bbd8c5017dd649c14bc70c8baf64 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Fri, 10 Jul 2020 16:15:58 +0200 Subject: [PATCH 042/220] Fixed custom style in example config. --- examples/ptpython_config/config.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/ptpython_config/config.py b/examples/ptpython_config/config.py index 9c7241f8..aa0bb635 100644 --- a/examples/ptpython_config/config.py +++ b/examples/ptpython_config/config.py @@ -8,7 +8,7 @@ from prompt_toolkit.filters import ViInsertMode from prompt_toolkit.key_binding.key_processor import KeyPress from prompt_toolkit.keys import Keys -from pygments.token import Token +from prompt_toolkit.styles import Style from ptpython.layout import CompletionVisualisation @@ -121,7 +121,7 @@ def configure(repl): # Install custom colorscheme named 'my-colorscheme' and use it. """ - repl.install_ui_colorscheme('my-colorscheme', _custom_ui_colorscheme) + repl.install_ui_colorscheme('my-colorscheme', Style.from_dict(_custom_ui_colorscheme)) repl.use_ui_colorscheme('my-colorscheme') """ @@ -182,7 +182,7 @@ def _(event): # `ptpython/style.py` for all possible tokens. _custom_ui_colorscheme = { # Blue prompt. - Token.Layout.Prompt: "bg:#eeeeff #000000 bold", + "prompt": "bg:#eeeeff #000000 bold", # Make the status toolbar red. - Token.Toolbar.Status: "bg:#ff0000 #000000", + "status-toolbar": "bg:#ff0000 #000000", } From 5d46a58b5cdbf996b2243634793ddf1bfd0561c9 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Fri, 10 Jul 2020 16:44:59 +0200 Subject: [PATCH 043/220] Type:ignore for ImportError. --- ptpython/entry_points/run_ptpython.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ptpython/entry_points/run_ptpython.py b/ptpython/entry_points/run_ptpython.py index d2c382ff..aeb5c26d 100644 --- a/ptpython/entry_points/run_ptpython.py +++ b/ptpython/entry_points/run_ptpython.py @@ -24,7 +24,7 @@ try: from importlib import metadata except ImportError: - import importlib_metadata as metadata + import importlib_metadata as metadata # type: ignore import appdirs from prompt_toolkit.formatted_text import HTML @@ -59,7 +59,7 @@ def create_parser() -> _Parser: "-V", "--version", action="version", - version=metadata.version("ptpython"), + version=metadata.version("ptpython"), # type: ignore ) parser.add_argument("args", nargs="*", help="Script and arguments") return parser From a94449e5095ee93d7327bada4fb0478ad3ea6911 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Fri, 10 Jul 2020 16:47:42 +0200 Subject: [PATCH 044/220] Release 3.0.3 --- CHANGELOG | 11 +++++++++++ setup.py | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 2c385c6f..9a6d6447 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,17 @@ CHANGELOG ========= +3.0.3: 2020-07-10 +----------------- + +Fixes: +- Sort attribute names for `DictionaryCompleter` and move underscored + attributes to the end. +- Handle unhandled exceptions in `get_compiler_flags`. +- Improved `run_async` code. +- Fix --version parameter. + + 3.0.2: 2020-04-14 ----------------- diff --git a/setup.py b/setup.py index 288870dd..b6d42497 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ setup( name="ptpython", author="Jonathan Slenders", - version="3.0.2", + version="3.0.3", url="https://github.com/prompt-toolkit/ptpython", description="Python REPL build on top of prompt_toolkit", long_description=long_description, From d4ddea30779db1c2f3fa02c5302c5a3397f67ab5 Mon Sep 17 00:00:00 2001 From: Nasy Date: Mon, 27 Jul 2020 12:24:26 -0400 Subject: [PATCH 045/220] Replace IPython.utils.warn with warnings.warn (#370) * Replace IPython.utils.warn with warnings.warn IPython.utils.warn was removed. * Fixed isort --- ptpython/ipython.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ptpython/ipython.py b/ptpython/ipython.py index 169aa2db..2e8d1195 100644 --- a/ptpython/ipython.py +++ b/ptpython/ipython.py @@ -8,6 +8,8 @@ offer. """ +from warnings import warn + from IPython import utils as ipy_utils from IPython.core.inputsplitter import IPythonInputSplitter from IPython.terminal.embed import InteractiveShellEmbed as _InteractiveShellEmbed @@ -259,7 +261,7 @@ def initialize_extensions(shell, extensions): try: shell.extension_manager.load_extension(ext) except: - ipy_utils.warn.warn( + warn( "Error in loading extension: %s" % ext + "\nCheck your config files in %s" % ipy_utils.path.get_ipython_dir() From 6f7d953a165ad7bcedf6af1bd8cfe7658efe7818 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Fri, 31 Jul 2020 11:33:31 +0200 Subject: [PATCH 046/220] Show full syntax error in Validator. --- ptpython/validator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ptpython/validator.py b/ptpython/validator.py index b7880bf6..8e98e878 100644 --- a/ptpython/validator.py +++ b/ptpython/validator.py @@ -37,7 +37,7 @@ def validate(self, document): index = document.translate_row_col_to_index( e.lineno - 1, (e.offset or 1) - 1 ) - raise ValidationError(index, "Syntax Error") + raise ValidationError(index, f"Syntax Error: {e}") except TypeError as e: # e.g. "compile() expected string without null bytes" raise ValidationError(0, str(e)) From 85c4fc1c08795da7147da3ec3a007409a2fc70a2 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Fri, 31 Jul 2020 11:45:01 +0200 Subject: [PATCH 047/220] Allow leading whitespace before single line expressions. --- ptpython/repl.py | 5 +++++ ptpython/validator.py | 11 +++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/ptpython/repl.py b/ptpython/repl.py index 237f6fb7..5b8af92f 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -131,6 +131,11 @@ def compile_with_flags(code: str, mode: str): dont_inherit=True, ) + # If the input is single line, remove leading whitespace. + # (This doesn't have to be a syntax error.) + if len(line.splitlines()) == 1: + line = line.strip() + if line.lstrip().startswith("\x1a"): # When the input starts with Ctrl-Z, quit the REPL. self.app.exit() diff --git a/ptpython/validator.py b/ptpython/validator.py index 8e98e878..b63bedcb 100644 --- a/ptpython/validator.py +++ b/ptpython/validator.py @@ -18,9 +18,16 @@ def validate(self, document): """ Check input for Python syntax errors. """ + text = document.text + + # If the input is single line, remove leading whitespace. + # (This doesn't have to be a syntax error.) + if len(text.splitlines()) == 1: + text = text.strip() + # When the input starts with Ctrl-Z, always accept. This means EOF in a # Python REPL. - if document.text.startswith("\x1a"): + if text.startswith("\x1a"): return try: @@ -29,7 +36,7 @@ def validate(self, document): else: flags = 0 - compile(document.text, "", "exec", flags=flags, dont_inherit=True) + compile(text, "", "exec", flags=flags, dont_inherit=True) except SyntaxError as e: # Note, the 'or 1' for offset is required because Python 2.7 # gives `None` as offset in case of '4=4' as input. (Looks like From 49f0e0562499f4b884bbd62905dc0f4bea94d6c9 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Fri, 31 Jul 2020 11:45:26 +0200 Subject: [PATCH 048/220] Bugfix in dictionary completion: don't recognize numbers as variable names. --- ptpython/completer.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/ptpython/completer.py b/ptpython/completer.py index 00ba8258..c62e2cb3 100644 --- a/ptpython/completer.py +++ b/ptpython/completer.py @@ -222,20 +222,22 @@ def __init__(self, get_globals, get_locals): # Pattern for expressions that are "safe" to eval for auto-completion. # These are expressions that contain only attribute and index lookups. - expression = r""" + varname = r"[a-zA-Z_][a-zA-Z0-9_]*" + + expression = rf""" # Any expression safe enough to eval while typing. # No operators, except dot, and only other dict lookups. # Technically, this can be unsafe of course, if bad code runs # in `__getattr__` or ``__getitem__``. ( # Variable name - [a-zA-Z0-9_]+ + {varname} \s* (?: # Attribute access. - \s* \. \s* [a-zA-Z0-9_]+ \s* + \s* \. \s* {varname} \s* | From c33d05889d35eb506ddbf7e3b1779bda7f3f08a1 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Fri, 31 Jul 2020 11:46:07 +0200 Subject: [PATCH 049/220] Completed type annotation for embed() call. --- ptpython/repl.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ptpython/repl.py b/ptpython/repl.py index 5b8af92f..cbfb33b5 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -311,7 +311,7 @@ def enter_to_continue() -> None: def embed( globals=None, locals=None, - configure: Optional[Callable] = None, + configure: Optional[Callable[[PythonRepl], None]] = None, vi_mode: bool = False, history_filename: Optional[str] = None, title: Optional[str] = None, From a8519bbb2120ec06bcee941e623f91a534845501 Mon Sep 17 00:00:00 2001 From: NotAFile Date: Tue, 4 Aug 2020 16:27:16 +0200 Subject: [PATCH 050/220] Document Embedding with IPython Support --- README.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.rst b/README.rst index 481238a3..aa0c8eaa 100644 --- a/README.rst +++ b/README.rst @@ -147,6 +147,13 @@ ipython``) .. image :: https://github.com/jonathanslenders/ptpython/raw/master/docs/images/ipython.png +This is also available for embedding: + +.. code:: python + + from ptpython.ipython.repl import embed + embed(globals(), locals()) + Django support ************** From 77a5bea3c3f234357aca064170e8be98255af0e6 Mon Sep 17 00:00:00 2001 From: Anton Alekseev Date: Wed, 5 Feb 2020 01:31:30 +0300 Subject: [PATCH 051/220] Add config options related to Vi input mode Setting `vi_start_in_nav_mode` to `True` enables `NAVIGATION` mode on startup. The issue is that due to the current behaviour of `ViState.reset()` input mode gets resetted back to `INSERT` on the every iteration of the main loop. In order to at one hand to provide the user with desired behaviour and on the other hand doesn't introduce breaking changes the other option `vi_keep_last_used_mode` was introduced which sets `input_mode` to the state observed before reset. `vi_keep_last_used_mode` can be useful even with `vi_start_in_nav_mode` set to `False` in the case the user prefer to start in `INSERT` mode but still wants to maintain the last mode he was in. In the case of `vi_keep_last_used_mode` set to `False` and `vi_start_in_nav_mode` to `True` `NAVIGATION` mode is set on every iteration the same way `INSERT` was set before this commit. Fixes #258. Commit rebased and modified by Jonathan Slenders. --- examples/ptpython_config/config.py | 6 ++++++ ptpython/python_input.py | 6 ++++++ ptpython/repl.py | 20 +++++++++++++++++++- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/examples/ptpython_config/config.py b/examples/ptpython_config/config.py index aa0bb635..2a4ffd94 100644 --- a/examples/ptpython_config/config.py +++ b/examples/ptpython_config/config.py @@ -119,6 +119,12 @@ def configure(repl): # Syntax. repl.enable_syntax_highlighting = True + # Get into Vi navigation mode at startup + repl.vi_start_in_nav_mode = False + + # Preserve last used Vi input mode between main loop iterations + repl.vi_keep_last_used_mode = False + # Install custom colorscheme named 'my-colorscheme' and use it. """ repl.install_ui_colorscheme('my-colorscheme', Style.from_dict(_custom_ui_colorscheme)) diff --git a/ptpython/python_input.py b/ptpython/python_input.py index e41b921f..3794020a 100644 --- a/ptpython/python_input.py +++ b/ptpython/python_input.py @@ -300,6 +300,12 @@ def __init__( # (Never run more than one at the same time.) self._get_signatures_thread_running: bool = False + # Get into Vi navigation mode at startup + self.vi_start_in_nav_mode: bool = False + + # Preserve last used Vi input mode between main loop iterations + self.vi_keep_last_used_mode: bool = False + self.style_transformation = merge_style_transformations( [ ConditionalStyleTransformation( diff --git a/ptpython/repl.py b/ptpython/repl.py index cbfb33b5..44c077cb 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -23,6 +23,8 @@ merge_formatted_text, to_formatted_text, ) +from prompt_toolkit.formatted_text.utils import fragment_list_width +from prompt_toolkit.key_binding.vi_state import InputMode from prompt_toolkit.patch_stdout import patch_stdout as patch_stdout_context from prompt_toolkit.shortcuts import clear_title, print_formatted_text, set_title from prompt_toolkit.utils import DummyContext @@ -79,9 +81,21 @@ async def run_async(self) -> None: 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_nav_mode: + self.app.vi_state.input_mode = InputMode.NAVIGATION + # Run the UI. try: - text = await self.app.run_async() + text = await self.app.run_async(pre_run=pre_run) except EOFError: return except KeyboardInterrupt: @@ -363,6 +377,10 @@ def get_locals(): if configure: configure(repl) + # Set Vi input mode + if repl.vi_start_in_nav_mode: + repl.app.vi_state.input_mode = InputMode.NAVIGATION + # Start repl. patch_context: ContextManager = patch_stdout_context() if patch_stdout else DummyContext() From e42c8d69718d0dda7e3b1563e5a8ee0aa49581b8 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Thu, 6 Aug 2020 12:03:15 +0200 Subject: [PATCH 052/220] Renamed repl.vi_start_in_nav_mode to repl.vi_start_in_navigation_mode. --- examples/ptpython_config/config.py | 2 +- ptpython/python_input.py | 2 +- ptpython/repl.py | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/ptpython_config/config.py b/examples/ptpython_config/config.py index 2a4ffd94..1a009018 100644 --- a/examples/ptpython_config/config.py +++ b/examples/ptpython_config/config.py @@ -120,7 +120,7 @@ def configure(repl): repl.enable_syntax_highlighting = True # Get into Vi navigation mode at startup - repl.vi_start_in_nav_mode = False + repl.vi_start_in_navigation_mode = False # Preserve last used Vi input mode between main loop iterations repl.vi_keep_last_used_mode = False diff --git a/ptpython/python_input.py b/ptpython/python_input.py index 3794020a..18b9ef69 100644 --- a/ptpython/python_input.py +++ b/ptpython/python_input.py @@ -301,7 +301,7 @@ def __init__( self._get_signatures_thread_running: bool = False # Get into Vi navigation mode at startup - self.vi_start_in_nav_mode: bool = False + self.vi_start_in_navigation_mode: bool = False # Preserve last used Vi input mode between main loop iterations self.vi_keep_last_used_mode: bool = False diff --git a/ptpython/repl.py b/ptpython/repl.py index 44c077cb..d4f4ad83 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -90,7 +90,7 @@ def pre_run( 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_nav_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. @@ -378,7 +378,7 @@ def get_locals(): configure(repl) # Set Vi input mode - if repl.vi_start_in_nav_mode: + if repl.vi_start_in_navigation_mode: repl.app.vi_state.input_mode = InputMode.NAVIGATION # Start repl. From 594f0e69cde7a855c80589b41633559768ef9ebd Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Thu, 6 Aug 2020 12:09:30 +0200 Subject: [PATCH 053/220] Fix: no need to handle vi_start_in_navigation_mode in the embed() call. --- ptpython/repl.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/ptpython/repl.py b/ptpython/repl.py index d4f4ad83..ba95a3d5 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -377,10 +377,6 @@ def get_locals(): if configure: configure(repl) - # Set Vi input mode - if repl.vi_start_in_navigation_mode: - repl.app.vi_state.input_mode = InputMode.NAVIGATION - # Start repl. patch_context: ContextManager = patch_stdout_context() if patch_stdout else DummyContext() From 7b185fc7870de4bfb8366afa40687ef66589f404 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Thu, 6 Aug 2020 12:44:35 +0200 Subject: [PATCH 054/220] Fix exit confirmation. Before, the exit confirmation was not focused. Which meant that key bindings of the main buffer were still active. If we are in Vi mode, that meant that there was a key binding for the ("y", "y") already, which caused the handling of "y" to be delayed (it was not marked as eager). This fix will focus the exit confirmation and avoid further interference of buffer key bindings. --- ptpython/key_bindings.py | 6 ++++++ ptpython/layout.py | 11 ++++++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/ptpython/key_bindings.py b/ptpython/key_bindings.py index 1740caf7..d5171cc9 100644 --- a/ptpython/key_bindings.py +++ b/ptpython/key_bindings.py @@ -187,7 +187,12 @@ def _(event): Override Control-D exit, to ask for confirmation. """ if python_input.confirm_exit: + # Show exit confirmation and focus it (focusing is important for + # making sure the default buffer key bindings are not active). python_input.show_exit_confirmation = True + python_input.app.layout.focus( + python_input.ptpython_layout.exit_confirmation + ) else: event.app.exit(exception=EOFError) @@ -279,6 +284,7 @@ def _(event): Cancel exit. """ python_input.show_exit_confirmation = False + python_input.app.layout.focus_previous() return bindings diff --git a/ptpython/layout.py b/ptpython/layout.py index 3940e7a1..d50a3a53 100644 --- a/ptpython/layout.py +++ b/ptpython/layout.py @@ -501,7 +501,7 @@ def get_text_fragments() -> StyleAndTextTuples: ) -def exit_confirmation( +def create_exit_confirmation( python_input: "PythonInput", style="class:exit-confirmation" ) -> Container: """ @@ -511,7 +511,7 @@ def exit_confirmation( def get_text_fragments() -> StyleAndTextTuples: # Show "Do you really want to exit?" return [ - (style, "\n %s ([y]/n)" % python_input.exit_message), + (style, "\n %s ([y]/n) " % python_input.exit_message), ("[SetCursorPosition]", ""), (style, " \n"), ] @@ -520,8 +520,8 @@ def get_text_fragments() -> StyleAndTextTuples: return ConditionalContainer( content=Window( - FormattedTextControl(get_text_fragments), style=style - ), # , has_focus=visible)), + FormattedTextControl(get_text_fragments, focusable=True), style=style + ), filter=visible, ) @@ -635,6 +635,7 @@ def menu_position(): ) sidebar = python_sidebar(python_input) + self.exit_confirmation = create_exit_confirmation(python_input) root_container = HSplit( [ @@ -680,7 +681,7 @@ def menu_position(): Float( left=2, bottom=1, - content=exit_confirmation(python_input), + content=self.exit_confirmation, ), Float( bottom=0, From c1353d2e15d13b3b0f86faf14efea347a9f56c73 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Thu, 6 Aug 2020 11:29:13 +0200 Subject: [PATCH 055/220] Improved dictionary completion. Handle strings as dictionary keys that contain spaces. --- ptpython/completer.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ptpython/completer.py b/ptpython/completer.py index c62e2cb3..1ff7bcc8 100644 --- a/ptpython/completer.py +++ b/ptpython/completer.py @@ -242,10 +242,10 @@ def __init__(self, get_globals, get_locals): | # Item lookup. - # (We match the square brackets. We don't care about - # matching quotes here in the regex. Nested square brackets - # are not supported.) - \s* \[ [a-zA-Z0-9_'"\s]+ \] \s* + # (We match the square brackets. The key can be anything. + # We don't care about matching quotes here in the regex. + # Nested square brackets are not supported.) + \s* \[ [^\[\]]+ \] \s* )* ) """ @@ -278,7 +278,7 @@ def __init__(self, get_globals, get_locals): # Dict loopup to complete (square bracket open + start of # string). \[ - \s* ([a-zA-Z0-9_'"]*)$ + \s* ([^\[\]]*)$ """, re.VERBOSE, ) From c786ca40176515464cf567ee49534e7e4cdd4e41 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Mon, 10 Aug 2020 12:06:13 +0200 Subject: [PATCH 056/220] Release 3.0.4 --- CHANGELOG | 14 ++++++++++++++ setup.py | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 9a6d6447..9615da16 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,20 @@ CHANGELOG ========= +3.0.4: 2020-08-10 +----------------- + +New features: +- Allow leading whitespace before single line expressions. +- Show full syntax error in validator. +- Added `vi_start_in_navigation_mode` and `vi_keep_last_used_mode` options. + +Fixes: +- Improved dictionary completion: handle keys that contain spaces and don't + recognize numbers as variable names. +- Fix in exit confirmation. + + 3.0.3: 2020-07-10 ----------------- diff --git a/setup.py b/setup.py index b6d42497..c590ffa4 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ setup( name="ptpython", author="Jonathan Slenders", - version="3.0.3", + version="3.0.4", url="https://github.com/prompt-toolkit/ptpython", description="Python REPL build on top of prompt_toolkit", long_description=long_description, From c05c4a6cbb2c4f70e224cbfd854546ba0367e56e Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Mon, 10 Aug 2020 12:11:42 +0200 Subject: [PATCH 057/220] Added ssh-and-telnet-embed.py example. Thanks to Vincent Michel. --- examples/ssh-and-telnet-embed.py | 50 ++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100755 examples/ssh-and-telnet-embed.py diff --git a/examples/ssh-and-telnet-embed.py b/examples/ssh-and-telnet-embed.py new file mode 100755 index 00000000..541b885c --- /dev/null +++ b/examples/ssh-and-telnet-embed.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python +""" +Serve a ptpython console using both telnet and ssh. + +Thanks to Vincent Michel for this! +https://gist.github.com/vxgmichel/7685685b3e5ead04ada4a3ba75a48eef +""" + +import pathlib +import asyncio + +import asyncssh + +from ptpython.repl import embed + +from prompt_toolkit import print_formatted_text +from prompt_toolkit.contrib.telnet.server import TelnetServer +from prompt_toolkit.contrib.ssh.server import PromptToolkitSSHServer + + +def ensure_key(filename="ssh_host_key"): + path = pathlib.Path(filename) + if not path.exists(): + rsa_key = asyncssh.generate_private_key("ssh-rsa") + path.write_bytes(rsa_key.export_private_key()) + return str(path) + + +async def interact(connection=None): + global_dict = {**globals(), "print": print_formatted_text} + await embed(return_asyncio_coroutine=True, globals=global_dict) + + +async def main(ssh_port=8022, telnet_port=8023): + ssh_server = PromptToolkitSSHServer(interact=interact) + await asyncssh.create_server( + lambda: ssh_server, "", ssh_port, server_host_keys=[ensure_key()] + ) + print(f"Running ssh server on port {ssh_port}...") + + telnet_server = TelnetServer(interact=interact, port=telnet_port) + telnet_server.start() + print(f"Running telnet server on port {telnet_port}...") + + while True: + await asyncio.sleep(60) + + +if __name__ == "__main__": + asyncio.run(main()) From 85dab9a26ee84d02ecef20d295d4ace60364c31e Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Mon, 10 Aug 2020 12:12:45 +0200 Subject: [PATCH 058/220] Removed old 'from __future__ import unicode_literals' statements. --- examples/asyncio-python-embed.py | 2 -- examples/python-embed-with-custom-prompt.py | 2 -- examples/python-embed.py | 2 -- examples/python-input.py | 2 -- 4 files changed, 8 deletions(-) diff --git a/examples/asyncio-python-embed.py b/examples/asyncio-python-embed.py index 3b796b2a..4dbbbcdd 100755 --- a/examples/asyncio-python-embed.py +++ b/examples/asyncio-python-embed.py @@ -11,8 +11,6 @@ to stdout, it won't break the input line, but instead writes nicely above the prompt. """ -from __future__ import unicode_literals - import asyncio from ptpython.repl import embed diff --git a/examples/python-embed-with-custom-prompt.py b/examples/python-embed-with-custom-prompt.py index 05417282..f9f68cc2 100755 --- a/examples/python-embed-with-custom-prompt.py +++ b/examples/python-embed-with-custom-prompt.py @@ -2,8 +2,6 @@ """ Example of embedding a Python REPL, and setting a custom prompt. """ -from __future__ import unicode_literals - from prompt_toolkit.formatted_text import HTML from pygments.token import Token diff --git a/examples/python-embed.py b/examples/python-embed.py index af24456e..ac2cd06f 100755 --- a/examples/python-embed.py +++ b/examples/python-embed.py @@ -1,8 +1,6 @@ #!/usr/bin/env python """ """ -from __future__ import unicode_literals - from ptpython.repl import embed diff --git a/examples/python-input.py b/examples/python-input.py index 1956070d..567c2ee6 100755 --- a/examples/python-input.py +++ b/examples/python-input.py @@ -1,8 +1,6 @@ #!/usr/bin/env python """ """ -from __future__ import unicode_literals - from ptpython.python_input import PythonInput From 44397e82eeb07dda298fdbf5c25f693e3dc8176b Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Mon, 10 Aug 2020 12:17:35 +0200 Subject: [PATCH 059/220] Fix in dictionary completion. Handle bug when numeric keys are used. --- ptpython/completer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ptpython/completer.py b/ptpython/completer.py index 1ff7bcc8..9f36aab3 100644 --- a/ptpython/completer.py +++ b/ptpython/completer.py @@ -388,7 +388,7 @@ def _get_item_lookup_completions( break for k in result: - if str(k).startswith(key_obj): + if str(k).startswith(str(key_obj)): try: k_repr = self._do_repr(k) yield Completion( From 8f7b8e1ff1f8d7f92b7c9e7f9a492312fdc2df4d Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Mon, 10 Aug 2020 12:19:42 +0200 Subject: [PATCH 060/220] Release 3.0.5 --- CHANGELOG | 7 +++++++ setup.py | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 9615da16..d6220bda 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,13 @@ CHANGELOG ========= +3.0.5: 2020-08-10 +----------------- + +Fixes: +- Handle bug in dictionary completion when numeric keys are used. + + 3.0.4: 2020-08-10 ----------------- diff --git a/setup.py b/setup.py index c590ffa4..e2bf89ba 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ setup( name="ptpython", author="Jonathan Slenders", - version="3.0.4", + version="3.0.5", url="https://github.com/prompt-toolkit/ptpython", description="Python REPL build on top of prompt_toolkit", long_description=long_description, From 1c95fd72f3e92835962aaa6be8caaeafc5768f9d Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Wed, 23 Sep 2020 19:33:32 +0200 Subject: [PATCH 061/220] Added support for __pt_repr__ methods. For objects that expose this method, we'll use that for printing the result. Further, if __repr__ doesn't return a valid Python string, we won't apply syntax highlighting, because it's often wrong. --- README.rst | 18 ++++++++ ptpython/repl.py | 103 +++++++++++++++++++++++++++------------------- ptpython/utils.py | 23 ++++++++++- 3 files changed, 101 insertions(+), 43 deletions(-) diff --git a/README.rst b/README.rst index aa0c8eaa..ef8f569b 100644 --- a/README.rst +++ b/README.rst @@ -50,6 +50,24 @@ Features [2] If the terminal supports it (most terminals do), this allows pasting without going into paste mode. It will keep the indentation. +__pt_repr__: A nicer repr with colors +************************************* + +When classes implement a ``__pt_repr__`` method, this will be used instead of +``__repr__`` for printing. Any `prompt_toolkit "formatted text" +`_ +can be returned from here. In order to avoid writing a ``__repr__`` as well, +the ``ptpython.utils.ptrepr_to_repr`` decorator can be applied. For instance: + +.. code:: python + + from ptpython.utils import ptrepr_to_repr + from prompt_toolkit.formatted_text import HTML + + @ptrepr_to_repr + class MyClass: + def __pt_repr__(self): + return HTML('Hello world!') More screenshots **************** diff --git a/ptpython/repl.py b/ptpython/repl.py index ba95a3d5..9be7d05e 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -19,11 +19,16 @@ from prompt_toolkit.formatted_text import ( FormattedText, PygmentsTokens, + StyleAndTextTuples, fragment_list_width, merge_formatted_text, to_formatted_text, ) -from prompt_toolkit.formatted_text.utils import fragment_list_width +from prompt_toolkit.formatted_text.utils import ( + fragment_list_to_text, + fragment_list_width, + split_lines, +) from prompt_toolkit.key_binding.vi_state import InputMode from prompt_toolkit.patch_stdout import patch_stdout as patch_stdout_context from prompt_toolkit.shortcuts import clear_title, print_formatted_text, set_title @@ -128,8 +133,6 @@ def _execute(self, line: str) -> None: """ Evaluate the line and print the result. """ - output = self.app.output - # WORKAROUND: Due to a bug in Jedi, the current directory is removed # from sys.path. See: https://github.com/davidhalter/jedi/issues/1148 if "" not in sys.path: @@ -167,50 +170,66 @@ def compile_with_flags(code: str, mode: str): locals["_"] = locals["_%i" % self.current_statement_index] = result if result is not None: - out_prompt = to_formatted_text(self.get_output_prompt()) - - try: - result_str = "%r\n" % (result,) - except UnicodeDecodeError: - # In Python 2: `__repr__` should return a bytestring, - # so to put it in a unicode context could raise an - # exception that the 'ascii' codec can't decode certain - # characters. Decode as utf-8 in that case. - result_str = "%s\n" % repr(result).decode( # type: ignore - "utf-8" - ) - - # Align every line to the first one. - line_sep = "\n" + " " * fragment_list_width(out_prompt) - result_str = line_sep.join(result_str.splitlines()) + "\n" - - # Write output tokens. - if self.enable_syntax_highlighting: - formatted_output = merge_formatted_text( - [ - out_prompt, - PygmentsTokens(list(_lex_python_result(result_str))), - ] - ) - else: - formatted_output = FormattedText( - out_prompt + [("", result_str)] - ) - - print_formatted_text( - formatted_output, - style=self._current_style, - style_transformation=self.style_transformation, - include_default_pygments_style=False, - output=output, - ) - + self.show_result(result) # If not a valid `eval` expression, run using `exec` instead. except SyntaxError: code = compile_with_flags(line, "exec") exec(code, self.get_globals(), self.get_locals()) - output.flush() + def show_result(self, result: object) -> None: + """ + Show __repr__ for an `eval` result. + """ + out_prompt = to_formatted_text(self.get_output_prompt()) + result_repr = to_formatted_text("%r\n" % (result,)) + + # If __pt_repr__ is present, take this. This can return + # prompt_toolkit formatted text. + if hasattr(result, "__pt_repr__"): + try: + result_repr = to_formatted_text(getattr(result, "__pt_repr__")()) + if isinstance(result_repr, list): + result_repr = FormattedText(result_repr) + except: + pass + + # If we have a string so far, and it's valid Python code, + # use the Pygments lexer. + if isinstance(result, str): + try: + compile(result, "", "eval") + except SyntaxError: + pass + else: + result = PygmentsTokens(list(_lex_python_result(result))) + + # Align every line to the prompt. + line_sep = "\n" + " " * fragment_list_width(out_prompt) + indented_repr: StyleAndTextTuples = [] + + for fragment in split_lines(result_repr): + indented_repr.extend(fragment) + indented_repr.append(("", line_sep)) + if indented_repr: + indented_repr.pop() + indented_repr.append(("", "\n")) + + # Write output tokens. + if self.enable_syntax_highlighting: + formatted_output = merge_formatted_text([out_prompt, indented_repr]) + else: + formatted_output = FormattedText( + out_prompt + [("", fragment_list_to_text(result_repr))] + ) + + print_formatted_text( + formatted_output, + style=self._current_style, + style_transformation=self.style_transformation, + include_default_pygments_style=False, + output=self.app.output, + ) + self.app.output.flush() def _handle_exception(self, e: Exception) -> None: output = self.app.output diff --git a/ptpython/utils.py b/ptpython/utils.py index 130da34f..1642914e 100644 --- a/ptpython/utils.py +++ b/ptpython/utils.py @@ -2,8 +2,10 @@ For internal use only. """ import re -from typing import Callable, TypeVar, cast +from typing import Callable, Type, TypeVar, cast +from prompt_toolkit.formatted_text import to_formatted_text +from prompt_toolkit.formatted_text.utils import fragment_list_to_text from prompt_toolkit.mouse_events import MouseEvent, MouseEventType __all__ = [ @@ -139,3 +141,22 @@ def handle_if_mouse_down(mouse_event: MouseEvent): return NotImplemented return cast(_T, handle_if_mouse_down) + + +_T_type = TypeVar("_T_type", bound=Type) + + +def ptrepr_to_repr(cls: _T_type) -> _T_type: + """ + Generate a normal `__repr__` method for classes that have a `__pt_repr__`. + """ + if not hasattr(cls, "__pt_repr__"): + raise TypeError( + "@ptrepr_to_repr can only be applied to classes that have a `__pt_repr__` method." + ) + + def __repr__(self) -> str: + return fragment_list_to_text(to_formatted_text(cls.__pt_repr__(self))) + + cls.__repr__ = __repr__ # type:ignore + return cls From 4a81398d20fceccf04d439d679ea3fd625aed598 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Wed, 23 Sep 2020 19:35:00 +0200 Subject: [PATCH 062/220] Some changes because of a new Black release. --- examples/ssh-and-telnet-embed.py | 9 ++++----- ptpython/completer.py | 4 +--- ptpython/history_browser.py | 20 ++++++++++++-------- ptpython/key_bindings.py | 4 ++-- ptpython/python_input.py | 12 +++++++++--- ptpython/repl.py | 4 +++- 6 files changed, 31 insertions(+), 22 deletions(-) diff --git a/examples/ssh-and-telnet-embed.py b/examples/ssh-and-telnet-embed.py index 541b885c..378784ce 100755 --- a/examples/ssh-and-telnet-embed.py +++ b/examples/ssh-and-telnet-embed.py @@ -6,16 +6,15 @@ https://gist.github.com/vxgmichel/7685685b3e5ead04ada4a3ba75a48eef """ -import pathlib import asyncio +import pathlib import asyncssh - -from ptpython.repl import embed - from prompt_toolkit import print_formatted_text -from prompt_toolkit.contrib.telnet.server import TelnetServer from prompt_toolkit.contrib.ssh.server import PromptToolkitSSHServer +from prompt_toolkit.contrib.telnet.server import TelnetServer + +from ptpython.repl import embed def ensure_key(filename="ssh_host_key"): diff --git a/ptpython/completer.py b/ptpython/completer.py index 9f36aab3..9912d743 100644 --- a/ptpython/completer.py +++ b/ptpython/completer.py @@ -436,9 +436,7 @@ def _get_attribute_completions( for name in names: if name.startswith(attr_name): - yield Completion( - name, -len(attr_name), - ) + yield Completion(name, -len(attr_name)) def _sort_attribute_names(self, names: List[str]) -> List[str]: """ diff --git a/ptpython/history_browser.py b/ptpython/history_browser.py index 6d8ede43..798a280f 100644 --- a/ptpython/history_browser.py +++ b/ptpython/history_browser.py @@ -475,8 +475,8 @@ def _(event): sorted(history_mapping.selected_lines).index(line_no) + history_mapping.result_line_offset ) - default_buffer.cursor_position = default_buffer.document.translate_row_col_to_index( - default_lineno, 0 + default_buffer.cursor_position = ( + default_buffer.document.translate_row_col_to_index(default_lineno, 0) ) # Also move the cursor to the next line. (This way they can hold @@ -606,8 +606,8 @@ def __init__(self, python_input, original_document): ) def _default_buffer_pos_changed(self, _): - """ When the cursor changes in the default buffer. Synchronize with - history buffer. """ + """When the cursor changes in the default buffer. Synchronize with + history buffer.""" # Only when this buffer has the focus. if self.app.current_buffer == self.default_buffer: try: @@ -623,8 +623,10 @@ def _default_buffer_pos_changed(self, _): except IndexError: pass else: - self.history_buffer.cursor_position = self.history_buffer.document.translate_row_col_to_index( - history_lineno, 0 + self.history_buffer.cursor_position = ( + self.history_buffer.document.translate_row_col_to_index( + history_lineno, 0 + ) ) def _history_buffer_pos_changed(self, _): @@ -639,6 +641,8 @@ def _history_buffer_pos_changed(self, _): + self.history_mapping.result_line_offset ) - self.default_buffer.cursor_position = self.default_buffer.document.translate_row_col_to_index( - default_lineno, 0 + self.default_buffer.cursor_position = ( + self.default_buffer.document.translate_row_col_to_index( + default_lineno, 0 + ) ) diff --git a/ptpython/key_bindings.py b/ptpython/key_bindings.py index d5171cc9..b01762e6 100644 --- a/ptpython/key_bindings.py +++ b/ptpython/key_bindings.py @@ -149,8 +149,8 @@ def _(event): empty_lines_required = python_input.accept_input_on_enter or 10000 def at_the_end(b): - """ we consider the cursor at the end when there is no text after - the cursor, or only whitespace. """ + """we consider the cursor at the end when there is no text after + the cursor, or only whitespace.""" text = b.document.text_after_cursor return text == "" or (text.isspace() and not "\n" in text) diff --git a/ptpython/python_input.py b/ptpython/python_input.py index 18b9ef69..5447d198 100644 --- a/ptpython/python_input.py +++ b/ptpython/python_input.py @@ -209,15 +209,21 @@ def __init__( self.show_signature: bool = False self.show_docstring: bool = False self.show_meta_enter_message: bool = True - self.completion_visualisation: CompletionVisualisation = CompletionVisualisation.MULTI_COLUMN + self.completion_visualisation: CompletionVisualisation = ( + CompletionVisualisation.MULTI_COLUMN + ) self.completion_menu_scroll_offset: int = 1 self.show_line_numbers: bool = False self.show_status_bar: bool = True self.wrap_lines: bool = True self.complete_while_typing: bool = True - self.paste_mode: bool = False # When True, don't insert whitespace after newline. - self.confirm_exit: bool = True # Ask for confirmation when Control-D is pressed. + self.paste_mode: bool = ( + False # When True, don't insert whitespace after newline. + ) + self.confirm_exit: bool = ( + True # Ask for confirmation when Control-D is pressed. + ) self.accept_input_on_enter: int = 2 # Accept when pressing Enter 'n' times. # 'None' means that meta-enter is always required. self.enable_open_in_editor: bool = True diff --git a/ptpython/repl.py b/ptpython/repl.py index 9be7d05e..27d2c60b 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -397,7 +397,9 @@ def get_locals(): configure(repl) # Start repl. - patch_context: ContextManager = patch_stdout_context() if patch_stdout else DummyContext() + patch_context: ContextManager = ( + patch_stdout_context() if patch_stdout else DummyContext() + ) if return_asyncio_coroutine: From 7425ce32197a08b4c897f5316644396e9dbc9996 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Wed, 23 Sep 2020 19:38:24 +0200 Subject: [PATCH 063/220] Added py.typed file. --- ptpython/py.typed | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 ptpython/py.typed diff --git a/ptpython/py.typed b/ptpython/py.typed new file mode 100644 index 00000000..e69de29b From f9d72f08a754042d8943f381b529f4fb0764adec Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Wed, 23 Sep 2020 19:54:21 +0200 Subject: [PATCH 064/220] Release 3.0.6 --- CHANGELOG | 11 +++++++++++ setup.py | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index d6220bda..a1c5c1e5 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,17 @@ CHANGELOG ========= +3.0.6: 2020-09-23 +----------------- + +New features: +- (Experimental) support for `__pt_repr__` methods. If objects implement this + method, this will be used to print the result in the REPL instead of the + normal `__repr__`. +- Added py.typed file, to enable type checking for applications that are + embedding ptpython. + + 3.0.5: 2020-08-10 ----------------- diff --git a/setup.py b/setup.py index e2bf89ba..10a70f12 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ setup( name="ptpython", author="Jonathan Slenders", - version="3.0.5", + version="3.0.6", url="https://github.com/prompt-toolkit/ptpython", description="Python REPL build on top of prompt_toolkit", long_description=long_description, From 0f57868a8a7b3b12a26ca586e7a104c63e0e03b1 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Wed, 23 Sep 2020 20:25:59 +0200 Subject: [PATCH 065/220] Run readme_renderer in Travis. --- .travis.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 7061cb5d..e622b352 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,7 +7,7 @@ matrix: - python: 3.7 install: - - travis_retry pip install . pytest isort black mypy + - travis_retry pip install . pytest isort black mypy readme_renderer - pip list script: @@ -21,3 +21,6 @@ script: # Type checking - mypy ptpython + + # Ensure that the README renders correctly (required for uploading to PyPI). + - python -m readme_renderer README.rst > /dev/null From 54849cb9c30c66e4f15ca8d69867545e8c1a048c Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Fri, 25 Sep 2020 16:27:13 +0200 Subject: [PATCH 066/220] Added 'insert_blank_line_after_input' configuration option and fixed a few __pt_repr__ formatting issues. --- ptpython/python_input.py | 6 +++++ ptpython/repl.py | 47 +++++++++++++++++++++++----------------- 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/ptpython/python_input.py b/ptpython/python_input.py index 5447d198..5c08c1b4 100644 --- a/ptpython/python_input.py +++ b/ptpython/python_input.py @@ -254,6 +254,7 @@ def __init__( self.exit_message: str = "Do you really want to exit?" self.insert_blank_line_after_output: bool = True # (For the REPL.) + self.insert_blank_line_after_input: bool = False # (For the REPL.) # The buffers. self.default_buffer = self._create_buffer() @@ -640,6 +641,11 @@ def get_values(): for s in self.all_prompt_styles ), ), + simple_option( + title="Blank line after input", + description="Insert a blank line after the input.", + field_name="insert_blank_line_after_input", + ), simple_option( title="Blank line after output", description="Insert a blank line after the output.", diff --git a/ptpython/repl.py b/ptpython/repl.py index 27d2c60b..95b1004e 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -115,6 +115,9 @@ def pre_run( def _process_text(self, line: str) -> None: if line and not line.isspace(): + if self.insert_blank_line_after_input: + self.app.output.write("\n") + try: # Eval and print. self._execute(line) @@ -181,45 +184,49 @@ def show_result(self, result: object) -> None: Show __repr__ for an `eval` result. """ out_prompt = to_formatted_text(self.get_output_prompt()) - result_repr = to_formatted_text("%r\n" % (result,)) + + # If the repr is valid Python code, use the Pygments lexer. + result_repr = repr(result) + try: + compile(result_repr, "", "eval") + except SyntaxError: + formatted_result_repr = to_formatted_text(result_repr) + else: + formatted_result_repr = to_formatted_text( + PygmentsTokens(list(_lex_python_result(result_repr))) + ) # If __pt_repr__ is present, take this. This can return # prompt_toolkit formatted text. if hasattr(result, "__pt_repr__"): try: - result_repr = to_formatted_text(getattr(result, "__pt_repr__")()) - if isinstance(result_repr, list): - result_repr = FormattedText(result_repr) + formatted_result_repr = to_formatted_text( + getattr(result, "__pt_repr__")() + ) + if isinstance(formatted_result_repr, list): + formatted_result_repr = FormattedText(formatted_result_repr) except: pass - # If we have a string so far, and it's valid Python code, - # use the Pygments lexer. - if isinstance(result, str): - try: - compile(result, "", "eval") - except SyntaxError: - pass - else: - result = PygmentsTokens(list(_lex_python_result(result))) - # Align every line to the prompt. line_sep = "\n" + " " * fragment_list_width(out_prompt) indented_repr: StyleAndTextTuples = [] - for fragment in split_lines(result_repr): + lines = list(split_lines(formatted_result_repr)) + + for i, fragment in enumerate(lines): indented_repr.extend(fragment) - indented_repr.append(("", line_sep)) - if indented_repr: - indented_repr.pop() - indented_repr.append(("", "\n")) + + # Add indentation separator between lines, not after the last line. + if i != len(lines) - 1: + indented_repr.append(("", line_sep)) # Write output tokens. if self.enable_syntax_highlighting: formatted_output = merge_formatted_text([out_prompt, indented_repr]) else: formatted_output = FormattedText( - out_prompt + [("", fragment_list_to_text(result_repr))] + out_prompt + [("", fragment_list_to_text(formatted_result_repr))] ) print_formatted_text( From 9f7819ea4a0df5d7da633a63f1e387c218d216a0 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Fri, 25 Sep 2020 16:25:39 +0200 Subject: [PATCH 067/220] Abbreviate completian meta information for dictionary completer if multiline or too long. --- ptpython/completer.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/ptpython/completer.py b/ptpython/completer.py index 9912d743..e4b43fc0 100644 --- a/ptpython/completer.py +++ b/ptpython/completer.py @@ -368,6 +368,16 @@ def _get_item_lookup_completions( """ Complete dictionary keys. """ + + def abbr_meta(text: str) -> str: + " Abbreviate meta text, make sure it fits on one line. " + # Take first line, if multiple lines. + if len(text) > 20: + text = text[:20] + "..." + if "\n" in text: + text = text.split("\n", 1)[0] + "..." + return text + match = self.item_lookup_pattern.search(document.text_before_cursor) if match is not None: object_var, key = match.groups() @@ -395,7 +405,7 @@ def _get_item_lookup_completions( k_repr + "]", -len(key), display=f"[{k_repr}]", - display_meta=self._do_repr(result[k]), + display_meta=abbr_meta(self._do_repr(result[k])), ) except ReprFailedError: pass @@ -411,7 +421,7 @@ def _get_item_lookup_completions( k_repr + "]", -len(key), display=f"[{k_repr}]", - display_meta=self._do_repr(result[k]), + display_meta=abbr_meta(self._do_repr(result[k])), ) except ReprFailedError: pass From a395a25f3307c7ece9c1fffe7c833f04556648b2 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Fri, 25 Sep 2020 17:16:42 +0200 Subject: [PATCH 068/220] Added option for hiding/showing private completions. --- ptpython/completer.py | 79 ++++++++++++++++++++++++++++++++++------ ptpython/layout.py | 5 ++- ptpython/python_input.py | 46 +++++++++++++++++++---- 3 files changed, 111 insertions(+), 19 deletions(-) diff --git a/ptpython/completer.py b/ptpython/completer.py index e4b43fc0..535d2e2e 100644 --- a/ptpython/completer.py +++ b/ptpython/completer.py @@ -1,7 +1,8 @@ import ast import keyword import re -from typing import TYPE_CHECKING, Any, Dict, Iterable, List +from enum import Enum +from typing import TYPE_CHECKING, Any, Callable, Dict, Iterable, List, Optional from prompt_toolkit.completion import ( CompleteEvent, @@ -12,13 +13,24 @@ from prompt_toolkit.contrib.regular_languages.compiler import compile as compile_grammar from prompt_toolkit.contrib.regular_languages.completion import GrammarCompleter from prompt_toolkit.document import Document +from prompt_toolkit.formatted_text import fragment_list_to_text, to_formatted_text from ptpython.utils import get_jedi_script_from_document if TYPE_CHECKING: from prompt_toolkit.contrib.regular_languages.compiler import _CompiledGrammar -__all__ = ["PythonCompleter"] +__all__ = ["PythonCompleter", "CompletePrivateAttributes", "HidePrivateCompleter"] + + +class CompletePrivateAttributes(Enum): + """ + Should we display private attributes in the completion pop-up? + """ + + NEVER = "NEVER" + IF_NO_PUBLIC = "IF_NO_PUBLIC" + ALWAYS = "ALWAYS" class PythonCompleter(Completer): @@ -26,7 +38,9 @@ class PythonCompleter(Completer): Completer for Python code. """ - def __init__(self, get_globals, get_locals, get_enable_dictionary_completion): + def __init__( + self, get_globals, get_locals, get_enable_dictionary_completion + ) -> None: super().__init__() self.get_globals = get_globals @@ -35,8 +49,8 @@ def __init__(self, get_globals, get_locals, get_enable_dictionary_completion): self.dictionary_completer = DictionaryCompleter(get_globals, get_locals) - self._path_completer_cache = None - self._path_completer_grammar_cache = None + self._path_completer_cache: Optional[GrammarCompleter] = None + self._path_completer_grammar_cache: Optional["_CompiledGrammar"] = None @property def _path_completer(self) -> GrammarCompleter: @@ -158,7 +172,7 @@ def get_completions( if script: try: - completions = script.completions() + jedi_completions = script.completions() except TypeError: # Issue #9: bad syntax causes completions() to fail in jedi. # https://github.com/jonathanslenders/python-prompt-toolkit/issues/9 @@ -196,12 +210,12 @@ def get_completions( # Supress all other Jedi exceptions. pass else: - for c in completions: + for jc in jedi_completions: yield Completion( - c.name_with_symbols, - len(c.complete) - len(c.name_with_symbols), - display=c.name_with_symbols, - style=_get_style_for_name(c.name_with_symbols), + jc.name_with_symbols, + len(jc.complete) - len(jc.name_with_symbols), + display=jc.name_with_symbols, + style=_get_style_for_name(jc.name_with_symbols), ) @@ -464,6 +478,49 @@ def sort_key(name: str): return sorted(names, key=sort_key) +class HidePrivateCompleter(Completer): + """ + Wrapper around completer that hides private fields, deponding on whether or + not public fields are shown. + + (The reason this is implemented as a `Completer` wrapper is because this + way it works also with `FuzzyCompleter`.) + """ + + def __init__( + self, + completer: Completer, + complete_private_attributes: Callable[[], CompletePrivateAttributes], + ) -> None: + self.completer = completer + self.complete_private_attributes = complete_private_attributes + + def get_completions( + self, document: Document, complete_event: CompleteEvent + ) -> Iterable[Completion]: + + completions = list(self.completer.get_completions(document, complete_event)) + complete_private_attributes = self.complete_private_attributes() + hide_private = False + + def is_private(completion: Completion) -> bool: + text = fragment_list_to_text(to_formatted_text(completion.display)) + return text.startswith("_") + + if complete_private_attributes == CompletePrivateAttributes.NEVER: + hide_private = True + + elif complete_private_attributes == CompletePrivateAttributes.IF_NO_PUBLIC: + hide_private = any(not is_private(completion) for completion in completions) + + if hide_private: + completions = [ + completion for completion in completions if not is_private(completion) + ] + + return completions + + class ReprFailedError(Exception): " Raised when the repr() call in `DictionaryCompleter` fails. " diff --git a/ptpython/layout.py b/ptpython/layout.py index d50a3a53..b06b95d3 100644 --- a/ptpython/layout.py +++ b/ptpython/layout.py @@ -213,7 +213,10 @@ def get_help_text(): return ConditionalContainer( content=Window( - FormattedTextControl(get_help_text), style=token, height=Dimension(min=3) + FormattedTextControl(get_help_text), + style=token, + height=Dimension(min=3), + wrap_lines=True, ), filter=ShowSidebar(python_input) & Condition(lambda: python_input.show_sidebar_help) diff --git a/ptpython/python_input.py b/ptpython/python_input.py index 5c08c1b4..c119e391 100644 --- a/ptpython/python_input.py +++ b/ptpython/python_input.py @@ -51,7 +51,7 @@ from prompt_toolkit.validation import ConditionalValidator, Validator from pygments.lexers import Python3Lexer as PythonLexer -from .completer import PythonCompleter +from .completer import CompletePrivateAttributes, HidePrivateCompleter, PythonCompleter from .history_browser import PythonHistory from .key_bindings import ( load_confirm_exit_bindings, @@ -180,13 +180,17 @@ def __init__( self.get_globals: _GetNamespace = get_globals or (lambda: {}) self.get_locals: _GetNamespace = get_locals or self.get_globals - self._completer = _completer or FuzzyCompleter( - PythonCompleter( - self.get_globals, - self.get_locals, - lambda: self.enable_dictionary_completion, + self._completer = HidePrivateCompleter( + _completer + or FuzzyCompleter( + PythonCompleter( + self.get_globals, + self.get_locals, + lambda: self.enable_dictionary_completion, + ), + enable_fuzzy=Condition(lambda: self.enable_fuzzy_completion), ), - enable_fuzzy=Condition(lambda: self.enable_fuzzy_completion), + lambda: self.complete_private_attributes, ) self._validator = _validator or PythonValidator(self.get_compiler_flags) self._lexer = _lexer or PygmentsLexer(PythonLexer) @@ -239,6 +243,9 @@ def __init__( self.enable_syntax_highlighting: bool = True self.enable_fuzzy_completion: bool = False self.enable_dictionary_completion: bool = False + self.complete_private_attributes: CompletePrivateAttributes = ( + CompletePrivateAttributes.ALWAYS + ) self.swap_light_and_dark: bool = False self.highlight_matching_parenthesis: bool = False self.show_sidebar: bool = False # Currently show the sidebar. @@ -530,6 +537,31 @@ def get_values(): "off": lambda: disable("complete_while_typing"), }, ), + Option( + title="Complete private attrs", + description="Show or hide private attributes in the completions. " + "'If no public' means: show private attributes only if no public " + "matches are found or if an underscore was typed.", + get_current_value=lambda: { + CompletePrivateAttributes.NEVER: "Never", + CompletePrivateAttributes.ALWAYS: "Always", + CompletePrivateAttributes.IF_NO_PUBLIC: "If no public", + }[self.complete_private_attributes], + get_values=lambda: { + "Never": lambda: enable( + "complete_private_attributes", + CompletePrivateAttributes.NEVER, + ), + "Always": lambda: enable( + "complete_private_attributes", + CompletePrivateAttributes.ALWAYS, + ), + "If no public": lambda: enable( + "complete_private_attributes", + CompletePrivateAttributes.IF_NO_PUBLIC, + ), + }, + ), Option( title="Enable fuzzy completion", description="Enable fuzzy completion.", From 86e1571f8fda623cd49aa42841d7303c94fb95f7 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Fri, 25 Sep 2020 17:58:28 +0200 Subject: [PATCH 069/220] Release 3.0.7 --- CHANGELOG | 13 +++++++++++++ setup.py | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index a1c5c1e5..b37222d5 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,19 @@ CHANGELOG ========= +3.0.7: 2020-09-25 +----------------- + +New features: +- Option to show/hide private attributes during a completion +- Added `insert_blank_line_after_input` option similar to + `insert_blank_line_after_output`. + +Fixes: +- Fixed some formatting issues of `__pt_repr__`. +- Abbreviate completion meta information for dictionary completer if needed. + + 3.0.6: 2020-09-23 ----------------- diff --git a/setup.py b/setup.py index 10a70f12..9b71711e 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ setup( name="ptpython", author="Jonathan Slenders", - version="3.0.6", + version="3.0.7", url="https://github.com/prompt-toolkit/ptpython", description="Python REPL build on top of prompt_toolkit", long_description=long_description, From 544a8f800d5fe4569330e67d39fb6cc74dde3f45 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Wed, 28 Oct 2020 18:21:44 +0100 Subject: [PATCH 070/220] Stop using deprecated Jedi functions. Use Script.get_signatures() instead of Script.call_signatures() to get Jedi signatures, and Script.complete() instead of Script.completions(). --- ptpython/completer.py | 5 ++++- ptpython/python_input.py | 2 +- ptpython/utils.py | 2 -- setup.py | 2 +- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/ptpython/completer.py b/ptpython/completer.py index 535d2e2e..73900da6 100644 --- a/ptpython/completer.py +++ b/ptpython/completer.py @@ -172,7 +172,10 @@ def get_completions( if script: try: - jedi_completions = script.completions() + jedi_completions = script.complete( + column=document.cursor_position_col, + line=document.cursor_position_row + 1, + ) except TypeError: # Issue #9: bad syntax causes completions() to fail in jedi. # https://github.com/jonathanslenders/python-prompt-toolkit/issues/9 diff --git a/ptpython/python_input.py b/ptpython/python_input.py index c119e391..efe0bdd5 100644 --- a/ptpython/python_input.py +++ b/ptpython/python_input.py @@ -891,7 +891,7 @@ def run(): # Show signatures in help text. if script: try: - signatures = script.call_signatures() + signatures = script.get_signatures() except ValueError: # e.g. in case of an invalid \\x escape. signatures = [] diff --git a/ptpython/utils.py b/ptpython/utils.py index 1642914e..3658085a 100644 --- a/ptpython/utils.py +++ b/ptpython/utils.py @@ -52,8 +52,6 @@ def get_jedi_script_from_document(document, locals, globals): try: return jedi.Interpreter( document.text, - column=document.cursor_position_col, - line=document.cursor_position_row + 1, path="input-text", namespaces=[locals, globals], ) diff --git a/setup.py b/setup.py index 9b71711e..6d3e93fc 100644 --- a/setup.py +++ b/setup.py @@ -19,7 +19,7 @@ install_requires=[ "appdirs", "importlib_metadata;python_version<'3.8'", - "jedi>=0.9.0", + "jedi>=0.16.0", "prompt_toolkit>=3.0.0,<3.1.0", "pygments", ], From f91f19b3c8c4ef6eb29722da0b2c63e64a86eb2c Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Thu, 29 Oct 2020 15:23:13 +0100 Subject: [PATCH 071/220] Fix typing error: 'sorted' needs a sortable type (was a TypeVar without bound). --- ptpython/python_input.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/ptpython/python_input.py b/ptpython/python_input.py index efe0bdd5..16837db8 100644 --- a/ptpython/python_input.py +++ b/ptpython/python_input.py @@ -6,7 +6,7 @@ from asyncio import get_event_loop from functools import partial -from typing import Any, Callable, Dict, Generic, List, Optional, TypeVar +from typing import TYPE_CHECKING, Any, Callable, Dict, Generic, List, Optional, TypeVar from prompt_toolkit.application import Application, get_app from prompt_toolkit.auto_suggest import ( @@ -66,7 +66,18 @@ __all__ = ["PythonInput"] -_T = TypeVar("_T") + +if TYPE_CHECKING: + from typing_extensions import Protocol + + class _SupportsLessThan(Protocol): + # Taken from typeshed. _T is used by "sorted", which needs anything + # sortable. + def __lt__(self, __other: Any) -> bool: + ... + + +_T = TypeVar("_T", bound="_SupportsLessThan") class OptionCategory: From 626a1b5f621d1c47a0e367ac0f5fd1b0d5a7841c Mon Sep 17 00:00:00 2001 From: Mikhail Terekhov Date: Mon, 5 Oct 2020 11:53:23 -0400 Subject: [PATCH 072/220] Update asyncio-python-embed.py Fix deprecation warning in Python 3.8. --- examples/asyncio-python-embed.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/examples/asyncio-python-embed.py b/examples/asyncio-python-embed.py index 4dbbbcdd..e1075a22 100755 --- a/examples/asyncio-python-embed.py +++ b/examples/asyncio-python-embed.py @@ -19,19 +19,17 @@ counter = [0] -@asyncio.coroutine -def print_counter(): +async def print_counter(): """ Coroutine that prints counters and saves it in a global variable. """ while True: print("Counter: %i" % counter[0]) counter[0] += 1 - yield from asyncio.sleep(3) + await asyncio.sleep(3) -@asyncio.coroutine -def interactive_shell(): +async def interactive_shell(): """ Coroutine that starts a Python REPL from which we can access the global counter variable. @@ -40,7 +38,7 @@ def interactive_shell(): 'You should be able to read and update the "counter[0]" variable from this shell.' ) try: - yield from embed( + await embed( globals=globals(), return_asyncio_coroutine=True, patch_stdout=True ) except EOFError: From be38c35d480c9f9ebdb9c0fd978bd6971dc290d9 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Thu, 19 Nov 2020 17:50:49 +0100 Subject: [PATCH 073/220] Show completion suffixes (like '(' for functions). --- ptpython/completer.py | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/ptpython/completer.py b/ptpython/completer.py index 73900da6..8261c224 100644 --- a/ptpython/completer.py +++ b/ptpython/completer.py @@ -1,4 +1,5 @@ import ast +import inspect import keyword import re from enum import Enum @@ -214,10 +215,16 @@ def get_completions( pass else: for jc in jedi_completions: + if jc.type == "function": + suffix = "()" + else: + suffix = "" + yield Completion( jc.name_with_symbols, len(jc.complete) - len(jc.name_with_symbols), - display=jc.name_with_symbols, + display=jc.name_with_symbols + suffix, + display_meta=jc.type, style=_get_style_for_name(jc.name_with_symbols), ) @@ -461,9 +468,24 @@ def _get_attribute_completions( names = self._sort_attribute_names(dir(result)) + def get_suffix(name: str) -> str: + try: + obj = getattr(result, name, None) + if inspect.isfunction(obj): + return "()" + + if isinstance(obj, dict): + return "{}" + if isinstance(obj, (list, tuple)): + return "[]" + except: + pass + return "" + for name in names: if name.startswith(attr_name): - yield Completion(name, -len(attr_name)) + suffix = get_suffix(name) + yield Completion(name, -len(attr_name), display=name + suffix) def _sort_attribute_names(self, names: List[str]) -> List[str]: """ From 2d26324fc56ef380e397b63550104a70735155a4 Mon Sep 17 00:00:00 2001 From: Julian Berman Date: Sat, 21 Nov 2020 11:55:24 -0500 Subject: [PATCH 074/220] Regenerate the docstring / helpstring, which looks outdated. --- ptpython/entry_points/run_ptpython.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/ptpython/entry_points/run_ptpython.py b/ptpython/entry_points/run_ptpython.py index aeb5c26d..53e0289e 100644 --- a/ptpython/entry_points/run_ptpython.py +++ b/ptpython/entry_points/run_ptpython.py @@ -1,17 +1,19 @@ #!/usr/bin/env python """ ptpython: Interactive Python shell. -Usage: - ptpython [ --vi ] - [ --config-dir= ] [ --interactive= ] - [--] [ ... ] - ptpython -h | --help - -Options: - --vi : Use Vi keybindings instead of Emacs bindings. - --config-dir= : Pass config directory. By default '$XDG_CONFIG_HOME/ptpython'. - -i, --interactive= : Start interactive shell after executing this file. +positional arguments: + args Script and arguments + +optional arguments: + -h, --help show this help message and exit + --vi Enable Vi key bindings + -i, --interactive Start interactive shell after executing this file. + --config-file CONFIG_FILE + Location of configuration file. + --history-file HISTORY_FILE + Location of history file. + -V, --version show program's version number and exit Other environment variables: PYTHONSTARTUP: file executed on interactive startup (no default) """ From 703133915af1f9fdbb478cf0667fa93442a669dc Mon Sep 17 00:00:00 2001 From: Julian Berman Date: Sat, 21 Nov 2020 12:03:35 -0500 Subject: [PATCH 075/220] Add PTPYTHON_CONFIG_HOME for explicitly setting a config dir. In particular allows macOS users to follow the Linux convention instead of the macOS one, of putting config back in ~/.config. Closes: #346 --- README.rst | 7 ++++++- ptpython/entry_points/run_ptpython.py | 23 ++++++++++++++++++----- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/README.rst b/README.rst index ef8f569b..3d6b1d9c 100644 --- a/README.rst +++ b/README.rst @@ -149,7 +149,12 @@ navigation mode. Configuration ************* -It is possible to create a ``$XDG_CONFIG_HOME/ptpython/config.py`` file to customize the configuration. +It is possible to create a ``config.py`` file to customize configuration. +ptpython will look in an appropriate platform-specific directory via `appdirs +`. See the ``appdirs`` documentation for the +precise location for your platform. A ``PTPYTHON_CONFIG_HOME`` environment +variable, if set, can also be used to explicitly override where configuration +is looked for. Have a look at this example to see what is possible: `config.py `_ diff --git a/ptpython/entry_points/run_ptpython.py b/ptpython/entry_points/run_ptpython.py index 53e0289e..47407c37 100644 --- a/ptpython/entry_points/run_ptpython.py +++ b/ptpython/entry_points/run_ptpython.py @@ -14,13 +14,16 @@ --history-file HISTORY_FILE Location of history file. -V, --version show program's version number and exit -Other environment variables: -PYTHONSTARTUP: file executed on interactive startup (no default) + +environment variables: + PTPYTHON_CONFIG_HOME: a configuration directory to use + PYTHONSTARTUP: file executed on interactive startup (no default) """ import argparse import os import pathlib import sys +from textwrap import dedent from typing import Tuple try: @@ -40,8 +43,15 @@ class _Parser(argparse.ArgumentParser): def print_help(self): super().print_help() - print("Other environment variables:") - print("PYTHONSTARTUP: file executed on interactive startup (no default)") + print( + dedent( + """ + environment variables: + PTPYTHON_CONFIG_HOME: a configuration directory to use + PYTHONSTARTUP: file executed on interactive startup (no default) + """, + ).rstrip(), + ) def create_parser() -> _Parser: @@ -72,7 +82,10 @@ def get_config_and_history_file(namespace: argparse.Namespace) -> Tuple[str, str Check which config/history files to use, ensure that the directories for these files exist, and return the config and history path. """ - config_dir = appdirs.user_config_dir("ptpython", "prompt_toolkit") + config_dir = os.environ.get( + "PTPYTHON_CONFIG_HOME", + appdirs.user_config_dir("ptpython", "prompt_toolkit"), + ) data_dir = appdirs.user_data_dir("ptpython", "prompt_toolkit") # Create directories. From 0409350b77f898223182851a27e8d89bbc54f3b5 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Mon, 7 Dec 2020 17:08:27 +0100 Subject: [PATCH 076/220] Some cleanup to the config file. --- README.rst | 1 - examples/ptpython_config/config.py | 44 ++++++++++++++++-------------- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/README.rst b/README.rst index 3d6b1d9c..0cf7f3c0 100644 --- a/README.rst +++ b/README.rst @@ -231,7 +231,6 @@ Special thanks to - `Pygments `_: Syntax highlighter. - `Jedi `_: Autocompletion library. -- `Docopt `_: Command-line interface description language. - `wcwidth `_: Determine columns needed for a wide characters. - `prompt_toolkit `_ for the interface. diff --git a/examples/ptpython_config/config.py b/examples/ptpython_config/config.py index 1a009018..8532f938 100644 --- a/examples/ptpython_config/config.py +++ b/examples/ptpython_config/config.py @@ -2,9 +2,8 @@ Configuration example for ``ptpython``. Copy this file to $XDG_CONFIG_HOME/ptpython/config.py +On Linux, this is: ~/.config/ptpython/config.py """ -from __future__ import unicode_literals - from prompt_toolkit.filters import ViInsertMode from prompt_toolkit.key_binding.key_processor import KeyPress from prompt_toolkit.keys import Keys @@ -12,7 +11,7 @@ from ptpython.layout import CompletionVisualisation -__all__ = ("configure",) +__all__ = ["configure"] def configure(repl): @@ -107,14 +106,19 @@ def configure(repl): repl.enable_input_validation = True # Use this colorscheme for the code. - repl.use_code_colorscheme("pastie") + repl.use_code_colorscheme("default") + # repl.use_code_colorscheme("pastie") # Set color depth (keep in mind that not all terminals support true color). - # repl.color_depth = 'DEPTH_1_BIT' # Monochrome. - # repl.color_depth = 'DEPTH_4_BIT' # ANSI colors only. + # repl.color_depth = "DEPTH_1_BIT" # Monochrome. + # repl.color_depth = "DEPTH_4_BIT" # ANSI colors only. repl.color_depth = "DEPTH_8_BIT" # The default, 256 colors. - # repl.color_depth = 'DEPTH_24_BIT' # True color. + # repl.color_depth = "DEPTH_24_BIT" # True color. + + # Min/max brightness + repl.min_brightness = 0.0 # Increase for dark terminal backgrounds. + repl.max_brightness = 1.0 # Decrease for light terminal backgrounds. # Syntax. repl.enable_syntax_highlighting = True @@ -127,22 +131,22 @@ def configure(repl): # Install custom colorscheme named 'my-colorscheme' and use it. """ - repl.install_ui_colorscheme('my-colorscheme', Style.from_dict(_custom_ui_colorscheme)) - repl.use_ui_colorscheme('my-colorscheme') + repl.install_ui_colorscheme("my-colorscheme", Style.from_dict(_custom_ui_colorscheme)) + repl.use_ui_colorscheme("my-colorscheme") """ # Add custom key binding for PDB. """ - @repl.add_key_binding(Keys.ControlB) + @repl.add_key_binding("c-b") def _(event): - ' Pressing Control-B will insert "pdb.set_trace()" ' - event.cli.current_buffer.insert_text('\nimport pdb; pdb.set_trace()\n') + " Pressing Control-B will insert "pdb.set_trace()" " + event.cli.current_buffer.insert_text("\nimport pdb; pdb.set_trace()\n") """ # Typing ControlE twice should also execute the current command. # (Alternative for Meta-Enter.) """ - @repl.add_key_binding(Keys.ControlE, Keys.ControlE) + @repl.add_key_binding("c-e", "c-e") def _(event): event.current_buffer.validate_and_handle() """ @@ -150,22 +154,22 @@ def _(event): # Typing 'jj' in Vi Insert mode, should send escape. (Go back to navigation # mode.) """ - @repl.add_key_binding('j', 'j', filter=ViInsertMode()) + @repl.add_key_binding("j", "j", filter=ViInsertMode()) def _(event): " Map 'jj' to Escape. " - event.cli.key_processor.feed(KeyPress(Keys.Escape)) + event.cli.key_processor.feed(KeyPress("escape")) """ # Custom key binding for some simple autocorrection while typing. """ corrections = { - 'impotr': 'import', - 'pritn': 'print', + "impotr": "import", + "pritn": "print", } - @repl.add_key_binding(' ') + @repl.add_key_binding(" ") def _(event): - ' When a space is pressed. Check & correct word before cursor. ' + " When a space is pressed. Check & correct word before cursor. " b = event.cli.current_buffer w = b.document.get_word_before_cursor() @@ -174,7 +178,7 @@ def _(event): b.delete_before_cursor(count=len(w)) b.insert_text(corrections[w]) - b.insert_text(' ') + b.insert_text(" ") """ # Add a custom title to the status bar. This is useful when ptpython is From 5da4e370da2e7664e11c77ec1f19a9d20d21cafd Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Mon, 7 Dec 2020 17:18:50 +0100 Subject: [PATCH 077/220] Fix dictionary completion on Pandas objects. This should fix the following error: File ".../ptpython/completer.py", line 373, in _get_expression_completions elif result: File ".../pandas/core/generic.py", line 1330, in __nonzero__ f"The truth value of a {type(self).__name__} is ambiguous. " --- ptpython/completer.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ptpython/completer.py b/ptpython/completer.py index 8261c224..a5bf2d2d 100644 --- a/ptpython/completer.py +++ b/ptpython/completer.py @@ -380,7 +380,9 @@ def _get_expression_completions( if isinstance(result, (list, tuple, dict)): yield Completion("[", 0) - elif result: + elif result is not None: + # Note: Don't call `if result` here. That can fail for types + # that have custom truthness checks. yield Completion(".", 0) def _get_item_lookup_completions( From ca041ea71b66578bb117a70d2ca8bf2e8026a6a0 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Mon, 7 Dec 2020 17:41:03 +0100 Subject: [PATCH 078/220] Added --light-bg and --dark-bg CLI flags. --- ptpython/entry_points/run_ptpython.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/ptpython/entry_points/run_ptpython.py b/ptpython/entry_points/run_ptpython.py index 47407c37..f23e69e2 100644 --- a/ptpython/entry_points/run_ptpython.py +++ b/ptpython/entry_points/run_ptpython.py @@ -9,6 +9,8 @@ -h, --help show this help message and exit --vi Enable Vi key bindings -i, --interactive Start interactive shell after executing this file. + --light-bg Run on a light background (use dark colors for text). + --dark-bg Run on a dark background (use light colors for text). --config-file CONFIG_FILE Location of configuration file. --history-file HISTORY_FILE @@ -63,6 +65,16 @@ def create_parser() -> _Parser: action="store_true", help="Start interactive shell after executing this file.", ) + parser.add_argument( + "--light-bg", + action="store_true", + help="Run on a light background (use dark colors for text).", + ), + parser.add_argument( + "--dark-bg", + action="store_true", + help="Run on a dark background (use light colors for text).", + ), parser.add_argument( "--config-file", type=str, help="Location of configuration file." ) @@ -83,8 +95,7 @@ def get_config_and_history_file(namespace: argparse.Namespace) -> Tuple[str, str these files exist, and return the config and history path. """ config_dir = os.environ.get( - "PTPYTHON_CONFIG_HOME", - appdirs.user_config_dir("ptpython", "prompt_toolkit"), + "PTPYTHON_CONFIG_HOME", appdirs.user_config_dir("ptpython", "prompt_toolkit"), ) data_dir = appdirs.user_data_dir("ptpython", "prompt_toolkit") @@ -178,6 +189,14 @@ def configure(repl) -> None: if os.path.exists(config_file): run_config(repl, config_file) + # Adjust colors if dark/light background flag has been given. + if a.light_bg: + repl.min_brightness = 0.0 + repl.max_brightness = 0.60 + elif a.dark_bg: + repl.min_brightness = 0.60 + repl.max_brightness = 1.0 + import __main__ embed( From 86497891634275b7771c24cd7172c04e9bb94a0e Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Thu, 17 Dec 2020 17:17:36 +0100 Subject: [PATCH 079/220] Added option for output formatting and pager for displaying big outputs. --- examples/asyncio-python-embed.py | 4 +- ptpython/entry_points/run_ptpython.py | 14 +-- ptpython/python_input.py | 15 +++ ptpython/repl.py | 147 ++++++++++++++++++++++---- setup.py | 1 + 5 files changed, 154 insertions(+), 27 deletions(-) diff --git a/examples/asyncio-python-embed.py b/examples/asyncio-python-embed.py index e1075a22..05f52f1d 100755 --- a/examples/asyncio-python-embed.py +++ b/examples/asyncio-python-embed.py @@ -38,9 +38,7 @@ async def interactive_shell(): 'You should be able to read and update the "counter[0]" variable from this shell.' ) try: - await embed( - globals=globals(), return_asyncio_coroutine=True, patch_stdout=True - ) + await embed(globals=globals(), return_asyncio_coroutine=True, patch_stdout=True) except EOFError: # Stop the loop when quitting the repl. (Ctrl-D press.) loop.stop() diff --git a/ptpython/entry_points/run_ptpython.py b/ptpython/entry_points/run_ptpython.py index f23e69e2..e1255905 100644 --- a/ptpython/entry_points/run_ptpython.py +++ b/ptpython/entry_points/run_ptpython.py @@ -28,17 +28,18 @@ from textwrap import dedent from typing import Tuple -try: - from importlib import metadata -except ImportError: - import importlib_metadata as metadata # type: ignore - import appdirs from prompt_toolkit.formatted_text import HTML from prompt_toolkit.shortcuts import print_formatted_text from ptpython.repl import embed, enable_deprecation_warnings, run_config +try: + from importlib import metadata +except ImportError: + import importlib_metadata as metadata # type: ignore + + __all__ = ["create_parser", "get_config_and_history_file", "run"] @@ -95,7 +96,8 @@ def get_config_and_history_file(namespace: argparse.Namespace) -> Tuple[str, str these files exist, and return the config and history path. """ config_dir = os.environ.get( - "PTPYTHON_CONFIG_HOME", appdirs.user_config_dir("ptpython", "prompt_toolkit"), + "PTPYTHON_CONFIG_HOME", + appdirs.user_config_dir("ptpython", "prompt_toolkit"), ) data_dir = appdirs.user_data_dir("ptpython", "prompt_toolkit") diff --git a/ptpython/python_input.py b/ptpython/python_input.py index 16837db8..508c42d4 100644 --- a/ptpython/python_input.py +++ b/ptpython/python_input.py @@ -261,6 +261,10 @@ def __init__( self.highlight_matching_parenthesis: bool = False self.show_sidebar: bool = False # Currently show the sidebar. + # Pager. + self.enable_output_formatting: bool = False + self.use_pager_for_big_outputs: bool = False + # When the sidebar is visible, also show the help text. self.show_sidebar_help: bool = True @@ -735,6 +739,17 @@ def get_values(): description="Highlight matching parenthesis, when the cursor is on or right after one.", field_name="highlight_matching_parenthesis", ), + simple_option( + title="Reformat output (black)", + description="Reformat outputs using Black, if possible (experimental).", + field_name="enable_output_formatting", + ), + simple_option( + title="Pager for big outputs", + description="Use a pager for displaying outputs that don't " + "fit on the screen.", + field_name="use_pager_for_big_outputs", + ), ], ), OptionCategory( diff --git a/ptpython/repl.py b/ptpython/repl.py index 95b1004e..fe869384 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -15,8 +15,10 @@ import warnings from typing import Any, Callable, ContextManager, Dict, Optional +import black from prompt_toolkit.document import Document from prompt_toolkit.formatted_text import ( + HTML, FormattedText, PygmentsTokens, StyleAndTextTuples, @@ -24,19 +26,20 @@ merge_formatted_text, to_formatted_text, ) -from prompt_toolkit.formatted_text.utils import ( - fragment_list_to_text, - fragment_list_width, - split_lines, -) +from prompt_toolkit.formatted_text.utils import fragment_list_to_text, split_lines +from prompt_toolkit.key_binding import KeyBindings, KeyPressEvent from prompt_toolkit.key_binding.vi_state import InputMode from prompt_toolkit.patch_stdout import patch_stdout as patch_stdout_context -from prompt_toolkit.shortcuts import clear_title, print_formatted_text, set_title -from prompt_toolkit.utils import DummyContext +from prompt_toolkit.shortcuts import ( + PromptSession, + clear_title, + print_formatted_text, + set_title, +) +from prompt_toolkit.utils import DummyContext, get_cwidth from pygments.lexers import PythonLexer, PythonTracebackLexer from pygments.token import Token -from .eventloop import inputhook from .python_input import PythonInput __all__ = ["PythonRepl", "enable_deprecation_warnings", "run_config", "embed"] @@ -107,12 +110,12 @@ def pre_run( # Abort - try again. self.default_buffer.document = Document() else: - self._process_text(text) + await self._process_text(text) if self.terminal_title: clear_title() - def _process_text(self, line: str) -> None: + async def _process_text(self, line: str) -> None: if line and not line.isspace(): if self.insert_blank_line_after_input: @@ -120,7 +123,7 @@ def _process_text(self, line: str) -> None: try: # Eval and print. - self._execute(line) + await self._execute(line) except KeyboardInterrupt as e: # KeyboardInterrupt doesn't inherit from Exception. self._handle_keyboard_interrupt(e) except Exception as e: @@ -132,7 +135,7 @@ def _process_text(self, line: str) -> None: self.current_statement_index += 1 self.signatures = [] - def _execute(self, line: str) -> None: + async def _execute(self, line: str) -> None: """ Evaluate the line and print the result. """ @@ -173,13 +176,13 @@ def compile_with_flags(code: str, mode: str): locals["_"] = locals["_%i" % self.current_statement_index] = result if result is not None: - self.show_result(result) + await self.show_result(result) # If not a valid `eval` expression, run using `exec` instead. except SyntaxError: code = compile_with_flags(line, "exec") exec(code, self.get_globals(), self.get_locals()) - def show_result(self, result: object) -> None: + async def show_result(self, result: object) -> None: """ Show __repr__ for an `eval` result. """ @@ -192,12 +195,19 @@ def show_result(self, result: object) -> None: except SyntaxError: formatted_result_repr = to_formatted_text(result_repr) else: + # Syntactically correct. Format with black and syntax highlight. + if self.enable_output_formatting: + result_repr = black.format_str( + result_repr, + mode=black.FileMode(line_length=self.app.output.get_size().columns), + ) + formatted_result_repr = to_formatted_text( PygmentsTokens(list(_lex_python_result(result_repr))) ) - # If __pt_repr__ is present, take this. This can return - # prompt_toolkit formatted text. + # If __pt_repr__ is present, take this. This can return prompt_toolkit + # formatted text. if hasattr(result, "__pt_repr__"): try: formatted_result_repr = to_formatted_text( @@ -229,14 +239,81 @@ def show_result(self, result: object) -> None: out_prompt + [("", fragment_list_to_text(formatted_result_repr))] ) + if self.use_pager_for_big_outputs: + await self._print_paginated_formatted_text( + to_formatted_text(formatted_output) + ) + else: + self.print_formatted_text(to_formatted_text(formatted_output)) + + self.app.output.flush() + + def print_formatted_text(self, formatted_text: StyleAndTextTuples) -> None: print_formatted_text( - formatted_output, + FormattedText(formatted_text), style=self._current_style, style_transformation=self.style_transformation, include_default_pygments_style=False, output=self.app.output, ) - self.app.output.flush() + + async def _print_paginated_formatted_text( + self, formatted_text: StyleAndTextTuples + ) -> None: + """ + Print formatted text, using --MORE-- style pagination. + (Avoid filling up the terminal's scrollback buffer.) + """ + continue_prompt = create_continue_prompt() + size = self.app.output.get_size() + + # Page buffer. + rows_in_buffer = 0 + columns_in_buffer = 0 + page: StyleAndTextTuples = [] + + def flush_page() -> None: + nonlocal page, columns_in_buffer, rows_in_buffer + self.print_formatted_text(page) + page = [] + columns_in_buffer = 0 + rows_in_buffer = 0 + + # Loop over lines. Show --MORE-- prompt when page is filled. + for line in split_lines(formatted_text): + for style, text, *_ in line: + for c in text: + width = get_cwidth(c) + + # (Soft) wrap line if it doesn't fit. + if columns_in_buffer + width > size.columns: + # Show pager first if we get too many lines after + # wrapping. + if rows_in_buffer + 1 >= size.rows - 1: + flush_page() + do_continue = await continue_prompt.prompt_async() + if not do_continue: + print("...") + return + + rows_in_buffer += 1 + columns_in_buffer = 0 + + columns_in_buffer += width + page.append((style, c)) + + if rows_in_buffer + 1 >= size.rows - 1: + flush_page() + do_continue = await continue_prompt.prompt_async() + if not do_continue: + print("...") + return + else: + page.append(("", "\n")) + rows_in_buffer += 1 + columns_in_buffer = 0 + + flush_page() def _handle_exception(self, e: Exception) -> None: output = self.app.output @@ -418,3 +495,37 @@ async def coroutine(): else: with patch_context: repl.run() + + +def create_continue_prompt() -> PromptSession[bool]: + """ + Create a "continue" prompt for paginated output. + """ + bindings = KeyBindings() + + @bindings.add("y") + @bindings.add("Y") + @bindings.add("enter") + @bindings.add("space") + def yes(event: KeyPressEvent) -> None: + event.app.exit(result=True) + + @bindings.add("n") + @bindings.add("N") + @bindings.add("q") + @bindings.add("c-c") + @bindings.add("escape", eager=True) + def no(event: KeyPressEvent) -> None: + event.app.exit(result=False) + + @bindings.add("") + def _(event: KeyPressEvent) -> None: + " Disallow inserting other text. " + pass + + session: PromptSession[bool] = PromptSession( + HTML(" -- MORE --"), + key_bindings=bindings, + erase_when_done=True, + ) + return session diff --git a/setup.py b/setup.py index 6d3e93fc..d75704f7 100644 --- a/setup.py +++ b/setup.py @@ -22,6 +22,7 @@ "jedi>=0.16.0", "prompt_toolkit>=3.0.0,<3.1.0", "pygments", + "black", ], python_requires=">=3.6", classifiers=[ From bc78c9e7861a69ee48bda61c0e6daf0bec07b3bc Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Tue, 5 Jan 2021 10:42:53 +0100 Subject: [PATCH 080/220] Improved the pager prompt. --- ptpython/layout.py | 4 +- ptpython/python_input.py | 6 +-- ptpython/repl.py | 82 ++++++++++++++++++++++++++++++---------- ptpython/style.py | 2 + 4 files changed, 69 insertions(+), 25 deletions(-) diff --git a/ptpython/layout.py b/ptpython/layout.py index b06b95d3..4ad70d36 100644 --- a/ptpython/layout.py +++ b/ptpython/layout.py @@ -371,9 +371,9 @@ def get_text_fragments() -> StyleAndTextTuples: else: result.extend( [ - (TB + " class:key", "[F3]", enter_history), + (TB + " class:status-toolbar.key", "[F3]", enter_history), (TB, " History ", enter_history), - (TB + " class:key", "[F6]", toggle_paste_mode), + (TB + " class:status-toolbar.key", "[F6]", toggle_paste_mode), (TB, " ", toggle_paste_mode), ] ) diff --git a/ptpython/python_input.py b/ptpython/python_input.py index 508c42d4..1b6b8f36 100644 --- a/ptpython/python_input.py +++ b/ptpython/python_input.py @@ -263,7 +263,7 @@ def __init__( # Pager. self.enable_output_formatting: bool = False - self.use_pager_for_big_outputs: bool = False + self.enable_pager: bool = False # When the sidebar is visible, also show the help text. self.show_sidebar_help: bool = True @@ -745,10 +745,10 @@ def get_values(): field_name="enable_output_formatting", ), simple_option( - title="Pager for big outputs", + title="Enable pager for output", description="Use a pager for displaying outputs that don't " "fit on the screen.", - field_name="use_pager_for_big_outputs", + field_name="enable_pager", ), ], ), diff --git a/ptpython/repl.py b/ptpython/repl.py index fe869384..de1b92a4 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -13,6 +13,7 @@ import sys import traceback import warnings +from enum import Enum from typing import Any, Callable, ContextManager, Dict, Optional import black @@ -36,6 +37,7 @@ print_formatted_text, set_title, ) +from prompt_toolkit.styles import BaseStyle from prompt_toolkit.utils import DummyContext, get_cwidth from pygments.lexers import PythonLexer, PythonTracebackLexer from pygments.token import Token @@ -239,7 +241,7 @@ async def show_result(self, result: object) -> None: out_prompt + [("", fragment_list_to_text(formatted_result_repr))] ) - if self.use_pager_for_big_outputs: + if self.enable_pager: await self._print_paginated_formatted_text( to_formatted_text(formatted_output) ) @@ -264,9 +266,14 @@ async def _print_paginated_formatted_text( Print formatted text, using --MORE-- style pagination. (Avoid filling up the terminal's scrollback buffer.) """ - continue_prompt = create_continue_prompt() + pager_prompt = self.create_pager_prompt() size = self.app.output.get_size() + abort = False + + # Max number of lines allowed in the buffer before painting. + max_rows = size.rows - 1 + # Page buffer. rows_in_buffer = 0 columns_in_buffer = 0 @@ -279,6 +286,20 @@ def flush_page() -> None: columns_in_buffer = 0 rows_in_buffer = 0 + async def show_pager() -> None: + nonlocal abort, max_rows + + continue_result = await pager_prompt.prompt_async() + if continue_result == PagerResult.ABORT: + print("...") + abort = True + + elif continue_result == PagerResult.NEXT_LINE: + max_rows = 1 + + elif continue_result == PagerResult.NEXT_PAGE: + max_rows = size.rows - 1 + # Loop over lines. Show --MORE-- prompt when page is filled. for line in split_lines(formatted_text): for style, text, *_ in line: @@ -289,11 +310,10 @@ def flush_page() -> None: if columns_in_buffer + width > size.columns: # Show pager first if we get too many lines after # wrapping. - if rows_in_buffer + 1 >= size.rows - 1: + if rows_in_buffer + 1 >= max_rows: flush_page() - do_continue = await continue_prompt.prompt_async() - if not do_continue: - print("...") + await show_pager() + if abort: return rows_in_buffer += 1 @@ -302,11 +322,10 @@ def flush_page() -> None: columns_in_buffer += width page.append((style, c)) - if rows_in_buffer + 1 >= size.rows - 1: + if rows_in_buffer + 1 >= max_rows: flush_page() - do_continue = await continue_prompt.prompt_async() - if not do_continue: - print("...") + await show_pager() + if abort: return else: page.append(("", "\n")) @@ -315,6 +334,12 @@ def flush_page() -> None: flush_page() + def create_pager_prompt(self) -> PromptSession["PagerResult"]: + """ + Create pager --MORE-- prompt. + """ + return create_pager_prompt(self._current_style) + def _handle_exception(self, e: Exception) -> None: output = self.app.output @@ -497,35 +522,52 @@ async def coroutine(): repl.run() -def create_continue_prompt() -> PromptSession[bool]: +class PagerResult(Enum): + ABORT = "ABORT" + NEXT_LINE = "NEXT_LINE" + NEXT_PAGE = "NEXT_PAGE" + + +def create_pager_prompt(style: BaseStyle) -> PromptSession[PagerResult]: """ Create a "continue" prompt for paginated output. """ bindings = KeyBindings() - @bindings.add("y") - @bindings.add("Y") @bindings.add("enter") + @bindings.add("down") + def next_line(event: KeyPressEvent) -> None: + event.app.exit(result=PagerResult.NEXT_LINE) + @bindings.add("space") - def yes(event: KeyPressEvent) -> None: - event.app.exit(result=True) + def next_page(event: KeyPressEvent) -> None: + event.app.exit(result=PagerResult.NEXT_PAGE) - @bindings.add("n") - @bindings.add("N") @bindings.add("q") @bindings.add("c-c") + @bindings.add("c-d") @bindings.add("escape", eager=True) def no(event: KeyPressEvent) -> None: - event.app.exit(result=False) + event.app.exit(result=PagerResult.ABORT) @bindings.add("") def _(event: KeyPressEvent) -> None: " Disallow inserting other text. " pass - session: PromptSession[bool] = PromptSession( - HTML(" -- MORE --"), + style + + session: PromptSession[PagerResult] = PromptSession( + HTML( + "" + " -- MORE -- " + "[Enter] Scroll " + "[Space] Next page " + "[q] Quit " + ": " + ), key_bindings=bindings, erase_when_done=True, + style=style, ) return session diff --git a/ptpython/style.py b/ptpython/style.py index a084c076..b16be697 100644 --- a/ptpython/style.py +++ b/ptpython/style.py @@ -97,10 +97,12 @@ def generate_style(python_style: BaseStyle, ui_style: BaseStyle) -> BaseStyle: "status-toolbar.title": "underline", "status-toolbar.inputmode": "bg:#222222 #ffffaa", "status-toolbar.key": "bg:#000000 #888888", + "status-toolbar key": "bg:#000000 #888888", "status-toolbar.pastemodeon": "bg:#aa4444 #ffffff", "status-toolbar.pythonversion": "bg:#222222 #ffffff bold", "status-toolbar paste-mode-on": "bg:#aa4444 #ffffff", "record": "bg:#884444 white", + "status-toolbar more": "#ffff44", "status-toolbar.input-mode": "#ffff44", # The options sidebar. "sidebar": "bg:#bbbbbb #000000", From acb03f33f9746f5c8135af3ced264114be3de56d Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Tue, 5 Jan 2021 10:53:02 +0100 Subject: [PATCH 081/220] Release 3.0.8 --- CHANGELOG | 16 ++++++++++++++++ setup.py | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index b37222d5..7558f901 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,22 @@ CHANGELOG ========= +3.0.8: 2020-01-05 +----------------- + +New features: +- Optional output formatting using Black. +- Optional pager for displaying outputs that don't fit on the screen. +- Added --light-bg and --dark-bg flags to automatically optimize the brightness + of the colors according to the terminal background. +- Addd `PTPYTHON_CONFIG_HOME` for explicitely setting the config directory. +- Show completion suffixes (like '(' for functions). + +Fixes: +- Fix dictionary completion on Pandas objects. +- Stop using deprecated Jedi functions. + + 3.0.7: 2020-09-25 ----------------- diff --git a/setup.py b/setup.py index d75704f7..dd551eef 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ setup( name="ptpython", author="Jonathan Slenders", - version="3.0.7", + version="3.0.8", url="https://github.com/prompt-toolkit/ptpython", description="Python REPL build on top of prompt_toolkit", long_description=long_description, From cb03427b7dc0e980c27eee2d88cb4a854df03a7f Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Thu, 7 Jan 2021 16:19:41 +0100 Subject: [PATCH 082/220] Allow replacing the completer -> Use DynamicCompleter. --- ptpython/python_input.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/ptpython/python_input.py b/ptpython/python_input.py index 1b6b8f36..fd735d19 100644 --- a/ptpython/python_input.py +++ b/ptpython/python_input.py @@ -15,7 +15,12 @@ ThreadedAutoSuggest, ) from prompt_toolkit.buffer import Buffer -from prompt_toolkit.completion import Completer, FuzzyCompleter, ThreadedCompleter +from prompt_toolkit.completion import ( + Completer, + DynamicCompleter, + FuzzyCompleter, + ThreadedCompleter, +) from prompt_toolkit.document import Document from prompt_toolkit.enums import DEFAULT_BUFFER, EditingMode from prompt_toolkit.filters import Condition @@ -191,14 +196,15 @@ def __init__( self.get_globals: _GetNamespace = get_globals or (lambda: {}) self.get_locals: _GetNamespace = get_locals or self.get_globals + self.completer = _completer or PythonCompleter( + self.get_globals, + self.get_locals, + lambda: self.enable_dictionary_completion, + ) + self._completer = HidePrivateCompleter( - _completer - or FuzzyCompleter( - PythonCompleter( - self.get_globals, - self.get_locals, - lambda: self.enable_dictionary_completion, - ), + FuzzyCompleter( + DynamicCompleter(lambda: self.completer), enable_fuzzy=Condition(lambda: self.enable_fuzzy_completion), ), lambda: self.complete_private_attributes, From e9eabede316b6df293aa42df3d689016d2fc62ae Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Thu, 7 Jan 2021 16:20:01 +0100 Subject: [PATCH 083/220] Set REPL title in pager. --- ptpython/repl.py | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/ptpython/repl.py b/ptpython/repl.py index de1b92a4..332dd6ed 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -20,6 +20,7 @@ from prompt_toolkit.document import Document from prompt_toolkit.formatted_text import ( HTML, + AnyFormattedText, FormattedText, PygmentsTokens, StyleAndTextTuples, @@ -338,7 +339,7 @@ def create_pager_prompt(self) -> PromptSession["PagerResult"]: """ Create pager --MORE-- prompt. """ - return create_pager_prompt(self._current_style) + return create_pager_prompt(self._current_style, self.title) def _handle_exception(self, e: Exception) -> None: output = self.app.output @@ -528,7 +529,9 @@ class PagerResult(Enum): NEXT_PAGE = "NEXT_PAGE" -def create_pager_prompt(style: BaseStyle) -> PromptSession[PagerResult]: +def create_pager_prompt( + style: BaseStyle, title: AnyFormattedText = "" +) -> PromptSession[PagerResult]: """ Create a "continue" prompt for paginated output. """ @@ -558,13 +561,18 @@ def _(event: KeyPressEvent) -> None: style session: PromptSession[PagerResult] = PromptSession( - HTML( - "" - " -- MORE -- " - "[Enter] Scroll " - "[Space] Next page " - "[q] Quit " - ": " + merge_formatted_text( + [ + title, + HTML( + "" + " -- MORE -- " + "[Enter] Scroll " + "[Space] Next page " + "[q] Quit " + ": " + ), + ] ), key_bindings=bindings, erase_when_done=True, From 0bbb369940fa0d4ee2d09f0c19b9a43f7012e142 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Fri, 8 Jan 2021 12:39:24 +0100 Subject: [PATCH 084/220] Release 3.0.9 --- CHANGELOG | 9 +++++++++ setup.py | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 7558f901..80c918f2 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,15 @@ CHANGELOG ========= +3.0.9: 2020-01-10 +----------------- + +New features: +- Allow replacing `PythonInput.completer` at runtime (useful for tools build on + top of ptpython). +- Show REPL title in pager. + + 3.0.8: 2020-01-05 ----------------- diff --git a/setup.py b/setup.py index dd551eef..109b0dea 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ setup( name="ptpython", author="Jonathan Slenders", - version="3.0.8", + version="3.0.9", url="https://github.com/prompt-toolkit/ptpython", description="Python REPL build on top of prompt_toolkit", long_description=long_description, From fe0ef13b852c1ea83f9c57d8c888a9a77377e4f0 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Fri, 8 Jan 2021 12:42:16 +0100 Subject: [PATCH 085/220] Removed unused import in example. --- examples/python-embed-with-custom-prompt.py | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/python-embed-with-custom-prompt.py b/examples/python-embed-with-custom-prompt.py index f9f68cc2..968aedc5 100755 --- a/examples/python-embed-with-custom-prompt.py +++ b/examples/python-embed-with-custom-prompt.py @@ -3,7 +3,6 @@ Example of embedding a Python REPL, and setting a custom prompt. """ from prompt_toolkit.formatted_text import HTML -from pygments.token import Token from ptpython.prompt_style import PromptStyle from ptpython.repl import embed From 6abf0050fe1e61d8c9f02c3d7e79d9559f9ee2e7 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Tue, 12 Jan 2021 16:48:45 +0100 Subject: [PATCH 086/220] Do dictionary completion on Sequence and Mapping objects (from collections.abc). --- ptpython/completer.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/ptpython/completer.py b/ptpython/completer.py index a5bf2d2d..da45023d 100644 --- a/ptpython/completer.py +++ b/ptpython/completer.py @@ -1,4 +1,5 @@ import ast +import collections.abc as collections_abc import inspect import keyword import re @@ -378,7 +379,10 @@ def _get_expression_completions( object_var = match.groups()[0] result = self._lookup(object_var, temp_locals) - if isinstance(result, (list, tuple, dict)): + if isinstance( + result, + (list, tuple, dict, collections_abc.Mapping, collections_abc.Sequence), + ): yield Completion("[", 0) elif result is not None: # Note: Don't call `if result` here. That can fail for types @@ -412,7 +416,7 @@ def abbr_meta(text: str) -> str: result = self._lookup(object_var, temp_locals) # If this object is a dictionary, complete the keys. - if isinstance(result, dict): + if isinstance(result, (dict, collections_abc.Mapping)): # Try to evaluate the key. key_obj = key for k in [key, key + '"', key + "'"]: @@ -437,7 +441,7 @@ def abbr_meta(text: str) -> str: pass # Complete list/tuple index keys. - elif isinstance(result, (list, tuple)): + elif isinstance(result, (list, tuple, collections_abc.Sequence)): if not key or key.isdigit(): for k in range(min(len(result), 1000)): if str(k).startswith(key): From 52a0da9e32f520908b905afee2a93175292b9a75 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Wed, 13 Jan 2021 10:33:09 +0100 Subject: [PATCH 087/220] Release 3.0.10 --- CHANGELOG | 9 +++++++++ setup.py | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 80c918f2..3ad6b2dd 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,15 @@ CHANGELOG ========= +3.0.10: 2020-01-13 +------------------ + +Fixes: +- Do dictionary completion on Sequence and Mapping objects (from + collections.abc). Note that dictionary completion is still turned off by + default. + + 3.0.9: 2020-01-10 ----------------- diff --git a/setup.py b/setup.py index 109b0dea..3388e91b 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ setup( name="ptpython", author="Jonathan Slenders", - version="3.0.9", + version="3.0.10", url="https://github.com/prompt-toolkit/ptpython", description="Python REPL build on top of prompt_toolkit", long_description=long_description, From 036346364e02614db66e0967038e264290c00c07 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Wed, 20 Jan 2021 10:50:45 +0100 Subject: [PATCH 088/220] Fix additional line ending after output. Use Pygments get_tokens_unprocessed. --- ptpython/repl.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ptpython/repl.py b/ptpython/repl.py index 332dd6ed..d34f6f93 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -400,7 +400,11 @@ def _lex_python_traceback(tb): def _lex_python_result(tb): " Return token list for Python string. " lexer = PythonLexer() - return lexer.get_tokens(tb) + # Use `get_tokens_unprocessed`, so that we get exactly the same string, + # without line endings appended. `print_formatted_text` already appends a + # line ending, and otherwise we'll have two line endings. + tokens = lexer.get_tokens_unprocessed(tb) + return [(tokentype, value) for index, tokentype, value in tokens] def enable_deprecation_warnings() -> None: From f0526c07a1f947f0ad6254f00eb7a9b894f1098d Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Wed, 20 Jan 2021 11:21:51 +0100 Subject: [PATCH 089/220] Improved system prompt. - Fix: accept !-style inputs in the validator again. - Added syntax highlighting for system prompt. - Added autocompletion for the system prompt. --- ptpython/completer.py | 19 +++++++++++++++---- ptpython/lexer.py | 28 ++++++++++++++++++++++++++++ ptpython/python_input.py | 6 +++--- ptpython/validator.py | 5 +++++ 4 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 ptpython/lexer.py diff --git a/ptpython/completer.py b/ptpython/completer.py index da45023d..aee280f4 100644 --- a/ptpython/completer.py +++ b/ptpython/completer.py @@ -12,6 +12,7 @@ Completion, PathCompleter, ) +from prompt_toolkit.contrib.completers.system import SystemCompleter from prompt_toolkit.contrib.regular_languages.compiler import compile as compile_grammar from prompt_toolkit.contrib.regular_languages.completion import GrammarCompleter from prompt_toolkit.document import Document @@ -49,7 +50,8 @@ def __init__( self.get_locals = get_locals self.get_enable_dictionary_completion = get_enable_dictionary_completion - self.dictionary_completer = DictionaryCompleter(get_globals, get_locals) + self._system_completer = SystemCompleter() + self._dictionary_completer = DictionaryCompleter(get_globals, get_locals) self._path_completer_cache: Optional[GrammarCompleter] = None self._path_completer_grammar_cache: Optional["_CompiledGrammar"] = None @@ -139,10 +141,20 @@ def get_completions( """ Get Python completions. """ + # If the input starts with an exclamation mark. Use the system completer. + if document.text.lstrip().startswith("!"): + yield from self._system_completer.get_completions( + Document( + text=document.text[1:], cursor_position=document.cursor_position - 1 + ), + complete_event, + ) + return + # Do dictionary key completions. if self.get_enable_dictionary_completion(): has_dict_completions = False - for c in self.dictionary_completer.get_completions( + for c in self._dictionary_completer.get_completions( document, complete_event ): if c.text not in "[.": @@ -157,8 +169,7 @@ def get_completions( if complete_event.completion_requested or self._complete_path_while_typing( document ): - for c in self._path_completer.get_completions(document, complete_event): - yield c + yield from self._path_completer.get_completions(document, complete_event) # If we are inside a string, Don't do Jedi completion. if self._path_completer_grammar.match(document.text_before_cursor): diff --git a/ptpython/lexer.py b/ptpython/lexer.py new file mode 100644 index 00000000..62e470f8 --- /dev/null +++ b/ptpython/lexer.py @@ -0,0 +1,28 @@ +from typing import Callable, Optional + +from prompt_toolkit.document import Document +from prompt_toolkit.formatted_text import StyleAndTextTuples +from prompt_toolkit.lexers import Lexer, PygmentsLexer +from pygments.lexers import BashLexer +from pygments.lexers import Python3Lexer as PythonLexer + +__all__ = ["PtpythonLexer"] + + +class PtpythonLexer(Lexer): + """ + Lexer for ptpython input. + + If the input starts with an exclamation mark, use a Bash lexer, otherwise, + use a Python 3 lexer. + """ + + def __init__(self, python_lexer: Optional[Lexer] = None) -> None: + self.python_lexer = python_lexer or PygmentsLexer(PythonLexer) + self.system_lexer = PygmentsLexer(BashLexer) + + def lex_document(self, document: Document) -> Callable[[int], StyleAndTextTuples]: + if document.text.startswith("!"): + return self.system_lexer.lex_document(document) + + return self.python_lexer.lex_document(document) diff --git a/ptpython/python_input.py b/ptpython/python_input.py index fd735d19..125b2d03 100644 --- a/ptpython/python_input.py +++ b/ptpython/python_input.py @@ -42,7 +42,7 @@ load_open_in_editor_bindings, ) from prompt_toolkit.key_binding.vi_state import InputMode -from prompt_toolkit.lexers import DynamicLexer, Lexer, PygmentsLexer, SimpleLexer +from prompt_toolkit.lexers import DynamicLexer, Lexer, SimpleLexer from prompt_toolkit.output import ColorDepth, Output from prompt_toolkit.styles import ( AdjustBrightnessStyleTransformation, @@ -54,7 +54,6 @@ ) from prompt_toolkit.utils import is_windows from prompt_toolkit.validation import ConditionalValidator, Validator -from pygments.lexers import Python3Lexer as PythonLexer from .completer import CompletePrivateAttributes, HidePrivateCompleter, PythonCompleter from .history_browser import PythonHistory @@ -64,6 +63,7 @@ load_sidebar_bindings, ) from .layout import CompletionVisualisation, PtPythonLayout +from .lexer import PtpythonLexer from .prompt_style import ClassicPrompt, IPythonPrompt, PromptStyle from .style import generate_style, get_all_code_styles, get_all_ui_styles from .utils import get_jedi_script_from_document @@ -210,7 +210,7 @@ def __init__( lambda: self.complete_private_attributes, ) self._validator = _validator or PythonValidator(self.get_compiler_flags) - self._lexer = _lexer or PygmentsLexer(PythonLexer) + self._lexer = PtpythonLexer(_lexer) self.history: History if history_filename: diff --git a/ptpython/validator.py b/ptpython/validator.py index b63bedcb..a027ecb1 100644 --- a/ptpython/validator.py +++ b/ptpython/validator.py @@ -30,6 +30,11 @@ def validate(self, document): if text.startswith("\x1a"): return + # When the input starts with an exclamation mark. Accept as shell + # command. + if text.lstrip().startswith("!"): + return + try: if self.get_compiler_flags: flags = self.get_compiler_flags() From 40be8c54fd102a5db3295ac0fa4997d6a9ec2905 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Tue, 19 Jan 2021 18:31:47 +0100 Subject: [PATCH 090/220] Refactoring of event loop usage. - The ptpython input UI will now run in a separate thread. This makes it possible to properly embed ptpython in an asyncio application, without having to deal with nested event loops (which asyncio does not support). - The "eval" part doesn't anymore take place within a ptpython coroutine, so it can spawn its own loop if needed. This also fixes `asyncio.run()` usage in the REPL, which was broken before. - Add support for top-level await. Special thanks to both Stephen.Y and baldulin for the original prototype implementations of top-level await support. --- docs/concurrency-challenges.rst | 36 +++++ ptpython/python_input.py | 65 ++++++++ ptpython/repl.py | 261 +++++++++++++++++++++----------- 3 files changed, 270 insertions(+), 92 deletions(-) create mode 100644 docs/concurrency-challenges.rst diff --git a/docs/concurrency-challenges.rst b/docs/concurrency-challenges.rst new file mode 100644 index 00000000..1a94d491 --- /dev/null +++ b/docs/concurrency-challenges.rst @@ -0,0 +1,36 @@ + +Concurrency-related challenges regarding embedding of ptpython in asyncio code +============================================================================== + +Things we want to be possible +----------------------------- + +- embed blocking ptpython in non-asyncio code. +- embed blocking ptpython in asyncio code (the event loop will block). +- embed awaitable ptpython in asyncio code (the loop will continue). +- react to resize events (SIGWINCH). +- support top-level await. +- Be able to patch_stdout, so that logging messages from another thread will be + printed above the prompt. +- It should be possible to handle `KeyboardInterrupt` during evaluation of an + expression. (This only works if the "eval" happens in the main thread.) +- The "eval" should happen in the same thread in which embed() was used. + +- create asyncio background tasks and have them run in the ptpython event loop. +- create asyncio background tasks and have ptpython run in a separate, isolated loop. + +Limitations of asyncio/python +----------------------------- + +- Spawning a new event loop in an existing event loop (from in a coroutine) is + not allowed. We can however spawn the event loop in a separate thread, and + wait for that thread to finish. + +- We can't listen to SIGWINCH signals, but prompt_toolkit's terminal size + polling solves that. + +- For patch_stdout to work correctly, we have to know what prompt_toolkit + application is running on the terminal, and tell that application to print + the output and redraw itself. + +- Handling of `KeyboardInterrupt`. diff --git a/ptpython/python_input.py b/ptpython/python_input.py index 125b2d03..fb0cc6a3 100644 --- a/ptpython/python_input.py +++ b/ptpython/python_input.py @@ -4,6 +4,7 @@ """ import __future__ +import threading from asyncio import get_event_loop from functools import partial from typing import TYPE_CHECKING, Any, Callable, Dict, Generic, List, Optional, TypeVar @@ -996,3 +997,67 @@ async def do_in_terminal() -> None: app.vi_state.input_mode = InputMode.INSERT asyncio.ensure_future(do_in_terminal()) + + def read(self) -> str: + """ + Read the input. + + This will run the Python input user interface in another thread, wait + for input to be accepted and return that. By running the UI in another + thread, we avoid issues regarding possibly nested event loops. + + This can raise EOFError, when Control-D is pressed. + """ + # 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. + result: str = "" + exception: Optional[BaseException] = None + + def in_thread() -> None: + nonlocal result, exception + try: + while True: + try: + result = self.app.run(pre_run=pre_run) + + if result.lstrip().startswith("\x1a"): + # When the input starts with Ctrl-Z, quit the REPL. + # (Important for Windows users.) + raise EOFError + + # If the input is single line, remove leading whitespace. + # (This doesn't have to be a syntax error.) + if len(result.splitlines()) == 1: + result = result.strip() + + if result and not result.isspace(): + return + except KeyboardInterrupt: + # Abort - try again. + self.default_buffer.document = Document() + except BaseException as e: + exception = e + return + + finally: + if self.insert_blank_line_after_input: + self.app.output.write("\n") + + thread = threading.Thread(target=in_thread) + thread.start() + thread.join() + + if exception is not None: + raise exception + return result diff --git a/ptpython/repl.py b/ptpython/repl.py index d34f6f93..3f88fe18 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -11,13 +11,15 @@ import builtins import os import sys +import threading import traceback +import types import warnings +from dis import COMPILER_FLAG_NAMES from enum import Enum from typing import Any, Callable, ContextManager, Dict, Optional import black -from prompt_toolkit.document import Document from prompt_toolkit.formatted_text import ( HTML, AnyFormattedText, @@ -30,7 +32,6 @@ ) from prompt_toolkit.formatted_text.utils import fragment_list_to_text, split_lines from prompt_toolkit.key_binding import KeyBindings, KeyPressEvent -from prompt_toolkit.key_binding.vi_state import InputMode from prompt_toolkit.patch_stdout import patch_stdout as patch_stdout_context from prompt_toolkit.shortcuts import ( PromptSession, @@ -45,15 +46,39 @@ from .python_input import PythonInput +try: + from ast import PyCF_ALLOW_TOP_LEVEL_AWAIT +except ImportError: + PyCF_ALLOW_TOP_LEVEL_AWAIT = 0 + __all__ = ["PythonRepl", "enable_deprecation_warnings", "run_config", "embed"] +def _get_coroutine_flag() -> Optional[int]: + for k, v in COMPILER_FLAG_NAMES.items(): + if v == "COROUTINE": + return k + + # Flag not found. + return None + + +COROUTINE_FLAG: Optional[int] = _get_coroutine_flag() + + +def _has_coroutine_flag(code: types.CodeType) -> bool: + if COROUTINE_FLAG is None: + # Not supported on this Python version. + return False + + return bool(code.co_flags & COROUTINE_FLAG) + + class PythonRepl(PythonInput): def __init__(self, *a, **kw) -> None: self._startup_paths = kw.pop("startup_paths", None) super().__init__(*a, **kw) self._load_start_paths() - self.pt_loop = asyncio.new_event_loop() def _load_start_paths(self) -> None: " Start the Read-Eval-Print Loop. " @@ -68,77 +93,82 @@ def _load_start_paths(self) -> None: output.write("WARNING | File not found: {}\n\n".format(path)) def run(self) -> None: - # 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.pt_loop.run_until_complete(self.run_async()) - finally: - # Restore the original event loop. - asyncio.set_event_loop(old_loop) - - async def run_async(self) -> None: + """ + Run the REPL loop. + """ 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. + # Read. try: - text = await self.app.run_async(pre_run=pre_run) + text = self.read() except EOFError: return - except KeyboardInterrupt: - # Abort - try again. - self.default_buffer.document = Document() + + # Eval. + try: + result = self.eval(text) + except KeyboardInterrupt as e: # KeyboardInterrupt doesn't inherit from Exception. + self._handle_keyboard_interrupt(e) + except BaseException as e: + self._handle_exception(e) else: - await self._process_text(text) + # Print. + if result is not None: + self.show_result(result) + + # Loop. + self.current_statement_index += 1 + self.signatures = [] if self.terminal_title: clear_title() - async def _process_text(self, line: str) -> None: + 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) - if line and not line.isspace(): - if self.insert_blank_line_after_input: - self.app.output.write("\n") + while True: + # Read. + try: + text = await loop.run_in_executor(None, self.read) + except EOFError: + return + # Eval. try: - # Eval and print. - await self._execute(line) + result = await self.eval_async(text) except KeyboardInterrupt as e: # KeyboardInterrupt doesn't inherit from Exception. self._handle_keyboard_interrupt(e) - except Exception as e: + 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)) - if self.insert_blank_line_after_output: - self.app.output.write("\n") + # Loop. + self.current_statement_index += 1 + self.signatures = [] - self.current_statement_index += 1 - self.signatures = [] + if self.terminal_title: + clear_title() - async def _execute(self, line: str) -> None: + def eval(self, line: str) -> object: """ Evaluate the line and print the result. """ @@ -147,45 +177,79 @@ async def _execute(self, line: str) -> None: if "" not in sys.path: sys.path.insert(0, "") - def compile_with_flags(code: str, mode: str): - " Compile code with the right compiler flags. " - return compile( - code, - "", - mode, - flags=self.get_compiler_flags(), - dont_inherit=True, - ) + if line.lstrip().startswith("!"): + # Run as shell command + os.system(line[1:]) + else: + # Try eval first + try: + code = self._compile_with_flags(line, "eval") + except SyntaxError: + # If not a valid `eval` expression, run using `exec` instead. + code = self._compile_with_flags(line, "exec") + exec(code, self.get_globals(), self.get_locals()) + else: + # No syntax errors for eval. Do eval. + result = eval(code, self.get_globals(), self.get_locals()) + + if _has_coroutine_flag(code): + result = asyncio.get_event_loop().run_until_complete(result) - # If the input is single line, remove leading whitespace. - # (This doesn't have to be a syntax error.) - if len(line.splitlines()) == 1: - line = line.strip() + self._store_eval_result(result) + return result - if line.lstrip().startswith("\x1a"): - # When the input starts with Ctrl-Z, quit the REPL. - self.app.exit() + return None + + async def eval_async(self, line: str) -> object: + """ + Evaluate the line and print the result. + """ + # WORKAROUND: Due to a bug in Jedi, the current directory is removed + # from sys.path. See: https://github.com/davidhalter/jedi/issues/1148 + if "" not in sys.path: + sys.path.insert(0, "") - elif line.lstrip().startswith("!"): + if line.lstrip().startswith("!"): # Run as shell command os.system(line[1:]) else: # Try eval first try: - code = compile_with_flags(line, "eval") + code = self._compile_with_flags(line, "eval") + except SyntaxError: + # If not a valid `eval` expression, run using `exec` instead. + code = self._compile_with_flags(line, "exec") + exec(code, self.get_globals(), self.get_locals()) + else: + # No syntax errors for eval. Do eval. result = eval(code, self.get_globals(), self.get_locals()) - locals: Dict[str, Any] = self.get_locals() - locals["_"] = locals["_%i" % self.current_statement_index] = result + if _has_coroutine_flag(code): + result = await result - if result is not None: - await self.show_result(result) - # If not a valid `eval` expression, run using `exec` instead. - except SyntaxError: - code = compile_with_flags(line, "exec") - exec(code, self.get_globals(), self.get_locals()) + self._store_eval_result(result) + return result + + return None - async def show_result(self, result: object) -> None: + def _store_eval_result(self, result: object) -> None: + locals: Dict[str, Any] = self.get_locals() + locals["_"] = locals["_%i" % self.current_statement_index] = result + + def get_compiler_flags(self) -> int: + return super().get_compiler_flags() | PyCF_ALLOW_TOP_LEVEL_AWAIT + + def _compile_with_flags(self, code: str, mode: str): + " Compile code with the right compiler flags. " + return compile( + code, + "", + mode, + flags=self.get_compiler_flags(), + dont_inherit=True, + ) + + def show_result(self, result: object) -> None: """ Show __repr__ for an `eval` result. """ @@ -243,14 +307,15 @@ async def show_result(self, result: object) -> None: ) if self.enable_pager: - await self._print_paginated_formatted_text( - to_formatted_text(formatted_output) - ) + self.print_paginated_formatted_text(to_formatted_text(formatted_output)) else: self.print_formatted_text(to_formatted_text(formatted_output)) self.app.output.flush() + if self.insert_blank_line_after_output: + self.app.output.write("\n") + def print_formatted_text(self, formatted_text: StyleAndTextTuples) -> None: print_formatted_text( FormattedText(formatted_text), @@ -260,7 +325,7 @@ def print_formatted_text(self, formatted_text: StyleAndTextTuples) -> None: output=self.app.output, ) - async def _print_paginated_formatted_text( + def print_paginated_formatted_text( self, formatted_text: StyleAndTextTuples ) -> None: """ @@ -287,18 +352,30 @@ def flush_page() -> None: columns_in_buffer = 0 rows_in_buffer = 0 - async def show_pager() -> None: + def show_pager() -> None: nonlocal abort, max_rows - continue_result = await pager_prompt.prompt_async() - if continue_result == PagerResult.ABORT: + # Run pager prompt in another thread. + # Same as for the input. This prevents issues with nested event + # loops. + pager_result = None + + def in_thread() -> None: + nonlocal pager_result + pager_result = pager_prompt.prompt() + + th = threading.Thread(target=in_thread) + th.start() + th.join() + + if pager_result == PagerResult.ABORT: print("...") abort = True - elif continue_result == PagerResult.NEXT_LINE: + elif pager_result == PagerResult.NEXT_LINE: max_rows = 1 - elif continue_result == PagerResult.NEXT_PAGE: + elif pager_result == PagerResult.NEXT_PAGE: max_rows = size.rows - 1 # Loop over lines. Show --MORE-- prompt when page is filled. @@ -313,7 +390,7 @@ async def show_pager() -> None: # wrapping. if rows_in_buffer + 1 >= max_rows: flush_page() - await show_pager() + show_pager() if abort: return @@ -325,7 +402,7 @@ async def show_pager() -> None: if rows_in_buffer + 1 >= max_rows: flush_page() - await show_pager() + show_pager() if abort: return else: @@ -341,7 +418,7 @@ def create_pager_prompt(self) -> PromptSession["PagerResult"]: """ return create_pager_prompt(self._current_style, self.title) - def _handle_exception(self, e: Exception) -> None: + def _handle_exception(self, e: BaseException) -> None: output = self.app.output # Instead of just calling ``traceback.format_exc``, we take the From c794c120ae7034a75c1d6f14e09f3ec662eef954 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Wed, 20 Jan 2021 17:56:41 +0100 Subject: [PATCH 091/220] Don't run PYTHONSTARTUP when -i flag was given. --- ptpython/entry_points/run_ptpython.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ptpython/entry_points/run_ptpython.py b/ptpython/entry_points/run_ptpython.py index e1255905..0b3dbdb9 100644 --- a/ptpython/entry_points/run_ptpython.py +++ b/ptpython/entry_points/run_ptpython.py @@ -165,7 +165,8 @@ def run() -> None: # --interactive if a.interactive and a.args: - startup_paths.append(a.args[0]) + # Note that we shouldn't run PYTHONSTARTUP when -i is given. + startup_paths = [a.args[0]] sys.argv = a.args # Add the current directory to `sys.path`. From 4a74eb5621a018c340160a13d47b4e786c0ec19b Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Thu, 21 Jan 2021 15:44:35 +0100 Subject: [PATCH 092/220] Require prompt_toolkit 3.0.11 for the latest ptpython. --- setup.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 3388e91b..f4ccfed0 100644 --- a/setup.py +++ b/setup.py @@ -20,7 +20,10 @@ "appdirs", "importlib_metadata;python_version<'3.8'", "jedi>=0.16.0", - "prompt_toolkit>=3.0.0,<3.1.0", + # Use prompt_toolkit 3.0.11, because ptpython now runs the UI in the + # background thread, and we need the terminal size polling that was + # introduced here. + "prompt_toolkit>=3.0.11,<3.1.0", "pygments", "black", ], From cf422acc58eaaf9c8fdb435b7587adc1331c93ca Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Thu, 21 Jan 2021 15:45:20 +0100 Subject: [PATCH 093/220] Move 'black' import inline. --- ptpython/repl.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ptpython/repl.py b/ptpython/repl.py index 3f88fe18..84b015b6 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -19,7 +19,6 @@ from enum import Enum from typing import Any, Callable, ContextManager, Dict, Optional -import black from prompt_toolkit.formatted_text import ( HTML, AnyFormattedText, @@ -264,6 +263,9 @@ def show_result(self, result: object) -> None: else: # Syntactically correct. Format with black and syntax highlight. if self.enable_output_formatting: + # Inline import. Slightly speed up start-up time if black is + # not used. + import black result_repr = black.format_str( result_repr, mode=black.FileMode(line_length=self.app.output.get_size().columns), From 72f2ed7d4fc55bf7ae5568e7c2e634893cb74ac6 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Thu, 21 Jan 2021 17:27:14 +0100 Subject: [PATCH 094/220] Extended the concurrency-challenges documentation. --- docs/concurrency-challenges.rst | 87 +++++++++++++++++++++++++++------ 1 file changed, 71 insertions(+), 16 deletions(-) diff --git a/docs/concurrency-challenges.rst b/docs/concurrency-challenges.rst index 1a94d491..b56d9698 100644 --- a/docs/concurrency-challenges.rst +++ b/docs/concurrency-challenges.rst @@ -5,32 +5,87 @@ Concurrency-related challenges regarding embedding of ptpython in asyncio code Things we want to be possible ----------------------------- -- embed blocking ptpython in non-asyncio code. -- embed blocking ptpython in asyncio code (the event loop will block). -- embed awaitable ptpython in asyncio code (the loop will continue). -- react to resize events (SIGWINCH). -- support top-level await. +- Embed blocking ptpython in non-asyncio code (the normal use case). +- Embed blocking ptpython in asyncio code (the event loop will block). +- Embed awaitable ptpython in asyncio code (the loop will continue). +- React to resize events (SIGWINCH). +- Support top-level await. - Be able to patch_stdout, so that logging messages from another thread will be printed above the prompt. - It should be possible to handle `KeyboardInterrupt` during evaluation of an - expression. (This only works if the "eval" happens in the main thread.) -- The "eval" should happen in the same thread in which embed() was used. + expression. +- The "eval" should happen in the same thread from where embed() was called. -- create asyncio background tasks and have them run in the ptpython event loop. -- create asyncio background tasks and have ptpython run in a separate, isolated loop. Limitations of asyncio/python ----------------------------- -- Spawning a new event loop in an existing event loop (from in a coroutine) is - not allowed. We can however spawn the event loop in a separate thread, and - wait for that thread to finish. +- We can only listen to SIGWINCH signal (resize) events in the main thread. -- We can't listen to SIGWINCH signals, but prompt_toolkit's terminal size - polling solves that. +- Usage of Control-C for triggering a `KeyboardInterrupt` only works for code + running in the main thread. (And only if the terminal was not set in raw + input mode). + +- Spawning a new event loop from within a coroutine, that's being executed in + an existing event loop is not allowed in asyncio. We can however spawn any + event loop in a separate thread, and wait for that thread to finish. - For patch_stdout to work correctly, we have to know what prompt_toolkit - application is running on the terminal, and tell that application to print + application is running on the terminal, then tell that application to print the output and redraw itself. -- Handling of `KeyboardInterrupt`. + +Additional challenges for IPython +--------------------------------- + +IPython supports integration of 3rd party event loops (for various GUI +toolkits). These event loops are supposed to continue running while we are +prompting for input. In an asyncio environment, it means that there are +situations where we have to juggle three event loops: + +- The asyncio loop in which the code was embedded. +- The asyncio loop from the prompt. +- The 3rd party GUI loop. + +Approach taken in ptpython 3.0.11 +--------------------------------- + +For ptpython, the most reliable solution is to to run the prompt_toolkit input +prompt in a separate background thread. This way it can use its own asyncio +event loop without ever having to interfere with whatever runs in the main +thread. + +Then, depending on how we embed, we do the following: +When a normal blocking embed is used: + * We start the UI thread for the input, and do a blocking wait on + `thread.join()` here. + * The "eval" happens in the main thread. + * The "print" happens also in the main thread. Unless a pager is shown, + which is also a prompt_toolkit application, then the pager itself is runs + also in another thread, similar to the way we do the input. + +When an awaitable embed is used, for embedding in a coroutine, but having the +event loop continue: + * We run the input method from the blocking embed in an asyncio executor + and do an `await loop.run_in_excecutor(...)`. + * The "eval" happens again in the main thread. + * "print" is also similar, except that the pager code (if used) runs in an + executor too. + +This means that the prompt_toolkit application code will always run in a +different thread. It means it won't be able to respond to SIGWINCH (window +resize events), but prompt_toolkit's 3.0.11 has now terminal size polling which +solves this. + +Control-C key presses won't interrupt the main thread while we wait for input, +because the prompt_toolkit application turns the terminal in raw mode, while +it's reading, which means that it will receive control-c key presses as raw +data in its own thread. + +Top-level await works in most situations as expected. +- If a blocking embed is used. We execute ``loop.run_until_complete(code)``. + This assumes that the blocking embed is not used in a coroutine of a running + event loop, otherwise, this will attempt to start a nested event loop, which + asyncio does not support. In that case we will get an exception. +- If an awaitable embed is used. We literally execute ``await code``. This will + integrate nicely in the current event loop. From 8ab2e167df76dad64293a1b73b1b5c1f974d4b60 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Thu, 21 Jan 2021 15:45:56 +0100 Subject: [PATCH 095/220] Release 3.0.11 --- CHANGELOG | 23 +++++++++++++++++++++++ setup.py | 2 +- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 3ad6b2dd..daba760f 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,29 @@ CHANGELOG ========= +3.0.11: 2020-01-20 +------------------ + +New features: +- Add support for top-level await. +- Refactoring of event loop usage: + + * The ptpython input UI will now run in a separate thread. This makes it + possible to properly embed ptpython in an asyncio application, without + having to deal with nested event loops (which asyncio does not support). + + * The "eval" part doesn't anymore take place within a ptpython coroutine, so + it can spawn its own loop if needed. This also fixes `asyncio.run()` usage + in the REPL, which was broken before. + +- Added syntax highlighting and autocompletion for !-style system commands. + +Fixes: +- Remove unexpected additional line after output. +- Fix system prompt. Accept !-style inputs again. +- Don't execute PYTHONSTARTUP when -i flag was given. + + 3.0.10: 2020-01-13 ------------------ diff --git a/setup.py b/setup.py index f4ccfed0..af20ec6a 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ setup( name="ptpython", author="Jonathan Slenders", - version="3.0.10", + version="3.0.11", url="https://github.com/prompt-toolkit/ptpython", description="Python REPL build on top of prompt_toolkit", long_description=long_description, From dcc43f1ea74e9107d0aa99020f02f8ee751821d2 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Fri, 22 Jan 2021 16:44:47 +0100 Subject: [PATCH 096/220] Update README. We support up to Python 3.9 --- README.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 0cf7f3c0..ae12f4d7 100644 --- a/README.rst +++ b/README.rst @@ -12,7 +12,7 @@ ptpython .. image :: https://github.com/jonathanslenders/ptpython/raw/master/docs/images/example1.png Ptpython is an advanced Python REPL. It should work on all -Python versions from 2.6 up to 3.7 and work cross platform (Linux, +Python versions from 2.6 up to 3.9 and work cross platform (Linux, BSD, OS X and Windows). Note: this version of ptpython requires at least Python 3.6. Install ptpython From 742c6d7c77fe03a42554304131083213b6103d63 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Fri, 22 Jan 2021 16:38:25 +0100 Subject: [PATCH 097/220] Properly handle SystemExit. --- ptpython/repl.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ptpython/repl.py b/ptpython/repl.py index 84b015b6..963b041d 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -110,6 +110,8 @@ def run(self) -> None: 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: @@ -153,6 +155,8 @@ async def run_async(self) -> None: result = await self.eval_async(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: From 7e49c40371e443bbf9b9c82f1cae2a663ab736ea Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Fri, 22 Jan 2021 16:43:35 +0100 Subject: [PATCH 098/220] Properly handle exceptions when trying to access __pt_repr__. --- ptpython/repl.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/ptpython/repl.py b/ptpython/repl.py index 963b041d..98978119 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -281,15 +281,17 @@ def show_result(self, result: object) -> None: # If __pt_repr__ is present, take this. This can return prompt_toolkit # formatted text. - if hasattr(result, "__pt_repr__"): - try: + try: + if hasattr(result, "__pt_repr__"): formatted_result_repr = to_formatted_text( getattr(result, "__pt_repr__")() ) if isinstance(formatted_result_repr, list): formatted_result_repr = FormattedText(formatted_result_repr) - except: - pass + except: + # For bad code, `__getattr__` can raise something that's not an + # `AttributeError`. This happens already when calling `hasattr()`. + pass # Align every line to the prompt. line_sep = "\n" + " " * fragment_list_width(out_prompt) From 5ccf10a03307a4e534f6530eaa72ed4966147df4 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Fri, 22 Jan 2021 16:54:47 +0100 Subject: [PATCH 099/220] Expose 'embed' function at the top-level of ptpython. --- ptpython/__init__.py | 3 +++ ptpython/repl.py | 3 +++ 2 files changed, 6 insertions(+) diff --git a/ptpython/__init__.py b/ptpython/__init__.py index e69de29b..4908eba8 100644 --- a/ptpython/__init__.py +++ b/ptpython/__init__.py @@ -0,0 +1,3 @@ +from .repl import embed + +__all__ = ["embed"] diff --git a/ptpython/repl.py b/ptpython/repl.py index 98978119..70e347eb 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -270,6 +270,7 @@ def show_result(self, result: object) -> None: # Inline import. Slightly speed up start-up time if black is # not used. import black + result_repr = black.format_str( result_repr, mode=black.FileMode(line_length=self.app.output.get_size().columns), @@ -562,6 +563,8 @@ def embed( :param configure: Callable that will be called with the `PythonRepl` as a first argument, to trigger configuration. :param title: Title to be displayed in the terminal titlebar. (None or string.) + :param patch_stdout: When true, patch `sys.stdout` so that background + threads that are printing will print nicely above the prompt. """ # Default globals/locals if globals is None: From 2cc7802610d158c9e8514460f13e5015030a02e5 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Sat, 23 Jan 2021 19:59:35 +0100 Subject: [PATCH 100/220] Ignore typing error regarding PyCF_ALLOW_TOP_LEVEL_AWAIT (not known for older Python versions). --- ptpython/repl.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ptpython/repl.py b/ptpython/repl.py index 70e347eb..f90a9c36 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -46,7 +46,7 @@ from .python_input import PythonInput try: - from ast import PyCF_ALLOW_TOP_LEVEL_AWAIT + from ast import PyCF_ALLOW_TOP_LEVEL_AWAIT # type: ignore except ImportError: PyCF_ALLOW_TOP_LEVEL_AWAIT = 0 From 0d0509c840e93826b42a6ae6509cb9e893b369b2 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Fri, 22 Jan 2021 17:43:22 +0100 Subject: [PATCH 101/220] Expose a get_ptpython function in the global namespace. --- ptpython/repl.py | 128 +++++++++++++++++++++++++++++------------------ 1 file changed, 79 insertions(+), 49 deletions(-) diff --git a/ptpython/repl.py b/ptpython/repl.py index f90a9c36..2883e770 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -98,33 +98,38 @@ def run(self) -> None: if self.terminal_title: set_title(self.terminal_title) - while True: - # Read. - try: - text = self.read() - except EOFError: - return + self._add_to_namespace() - # 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) + try: + while True: + # Read. + try: + text = self.read() + except EOFError: + return - # Loop. - self.current_statement_index += 1 - self.signatures = [] + # 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) - if self.terminal_title: - clear_title() + # Loop. + self.current_statement_index += 1 + self.signatures = [] + + finally: + if self.terminal_title: + clear_title() + self._remove_from_namespace() async def run_async(self) -> None: """ @@ -143,33 +148,39 @@ async def run_async(self) -> None: if self.terminal_title: set_title(self.terminal_title) - while True: - # 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. - self._handle_keyboard_interrupt(e) - 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)) + self._add_to_namespace() - # Loop. - self.current_statement_index += 1 - self.signatures = [] + try: + while True: + # Read. + try: + text = await loop.run_in_executor(None, self.read) + except EOFError: + return - if self.terminal_title: - clear_title() + # Eval. + try: + result = await self.eval_async(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: + await loop.run_in_executor( + None, lambda: self.show_result(result) + ) + + # Loop. + self.current_statement_index += 1 + self.signatures = [] + finally: + if self.terminal_title: + clear_title() + self._remove_from_namespace() def eval(self, line: str) -> object: """ @@ -476,6 +487,25 @@ def _handle_keyboard_interrupt(self, e: KeyboardInterrupt) -> None: output.write("\rKeyboardInterrupt\n\n") output.flush() + def _add_to_namespace(self) -> None: + """ + Add ptpython built-ins to global namespace. + """ + globals = self.get_globals() + + # Add a 'get_ptpython', similar to 'get_ipython' + def get_ptpython() -> PythonInput: + return self + + globals["get_ptpython"] = get_ptpython + + def _remove_from_namespace(self) -> None: + """ + Remove added symbols from the globals. + """ + globals = self.get_globals() + del globals["get_ptpython"] + def _lex_python_traceback(tb): " Return token list for traceback string. " From 06554f9863b52b0db392725be98c19a1ef82bb3c Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Sun, 24 Jan 2021 10:53:38 +0100 Subject: [PATCH 102/220] Release 3.0.12 --- CHANGELOG | 14 ++++++++++++++ setup.py | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index daba760f..ee90fcb5 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,20 @@ CHANGELOG ========= +3.0.12: 2020-01-24 +------------------ + +New features: +- Expose a `get_ptpython` function in the global namespace, to get programmatic + access to the REPL. +- Expose `embed()` at the top level of the package. Make it possible to do + `from ptpython import embed`. + +Fixes: +- Properly handle exceptions when trying to access `__pt_repr__`. +- Properly handle `SystemExit`. + + 3.0.11: 2020-01-20 ------------------ diff --git a/setup.py b/setup.py index af20ec6a..0b0da6ce 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ setup( name="ptpython", author="Jonathan Slenders", - version="3.0.11", + version="3.0.12", url="https://github.com/prompt-toolkit/ptpython", description="Python REPL build on top of prompt_toolkit", long_description=long_description, From 3dac89a804473d041906cb1649d1b1429675b46a Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Mon, 25 Jan 2021 16:44:29 +0100 Subject: [PATCH 103/220] Added Github actions test.yaml file and removed .travis.yml. --- .github/workflows/test.yaml | 38 +++++++++++++++++++++++++++++++++++++ .travis.yml | 26 ------------------------- 2 files changed, 38 insertions(+), 26 deletions(-) create mode 100644 .github/workflows/test.yaml delete mode 100644 .travis.yml diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 00000000..00ed1b00 --- /dev/null +++ b/.github/workflows/test.yaml @@ -0,0 +1,38 @@ +name: test + +on: + push: # any branch + pull_request: + branches: [master] + +jobs: + test-ubuntu: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: [3.6, 3.7, 3.8, 3.9] + + steps: + - uses: actions/checkout@v2 + - name: Setup Python ${{ matrix.python-version }} + uses: actions/setup-python@v1 + with: + python-version: ${{ matrix.python-version }} + - name: Install Dependencies + run: | + sudo apt remove python3-pip + python -m pip install --upgrade pip + python -m pip install . black isort mypy pytest readme_renderer + pip list + - name: Type Checker + run: | + mypy ptpython + isort -c --profile black ptpython examples setup.py + black --check ptpython examples setup.py + - name: Run Tests + run: | + ./tests/run_tests.py + - name: Validate README.md + # Ensure that the README renders correctly (required for uploading to PyPI). + run: | + python -m readme_renderer README.rst > /dev/null diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index e622b352..00000000 --- a/.travis.yml +++ /dev/null @@ -1,26 +0,0 @@ -sudo: false -language: python - -matrix: - include: - - python: 3.6 - - python: 3.7 - -install: - - travis_retry pip install . pytest isort black mypy readme_renderer - - pip list - -script: - - echo "$TRAVIS_PYTHON_VERSION" - - ./tests/run_tests.py - - # Check wheather the imports were sorted correctly. - - isort -c -rc ptpython tests setup.py examples - - - black --check ptpython setup.py examples - - # Type checking - - mypy ptpython - - # Ensure that the README renders correctly (required for uploading to PyPI). - - python -m readme_renderer README.rst > /dev/null From 24756f48e0d6a32ab67a73b35d32e99f14f43b7c Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Mon, 25 Jan 2021 14:43:39 +0100 Subject: [PATCH 104/220] Remove extra line ending in paginated output. When the "Enable pager for output" option is used, an extra line ending was printed. --- ptpython/repl.py | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/ptpython/repl.py b/ptpython/repl.py index 2883e770..e8ca3a0e 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -336,17 +336,22 @@ def show_result(self, result: object) -> None: if self.insert_blank_line_after_output: self.app.output.write("\n") - def print_formatted_text(self, formatted_text: StyleAndTextTuples) -> None: + def print_formatted_text( + self, formatted_text: StyleAndTextTuples, end: str = "\n" + ) -> None: print_formatted_text( FormattedText(formatted_text), style=self._current_style, style_transformation=self.style_transformation, include_default_pygments_style=False, output=self.app.output, + end=end, ) def print_paginated_formatted_text( - self, formatted_text: StyleAndTextTuples + self, + formatted_text: StyleAndTextTuples, + end: str = "\n", ) -> None: """ Print formatted text, using --MORE-- style pagination. @@ -367,7 +372,7 @@ def print_paginated_formatted_text( def flush_page() -> None: nonlocal page, columns_in_buffer, rows_in_buffer - self.print_formatted_text(page) + self.print_formatted_text(page, end="") page = [] columns_in_buffer = 0 rows_in_buffer = 0 @@ -399,7 +404,11 @@ def in_thread() -> None: max_rows = size.rows - 1 # Loop over lines. Show --MORE-- prompt when page is filled. - for line in split_lines(formatted_text): + + formatted_text = formatted_text + [("", end)] + lines = list(split_lines(formatted_text)) + + for lineno, line in enumerate(lines): for style, text, *_ in line: for c in text: width = get_cwidth(c) @@ -426,9 +435,13 @@ def in_thread() -> None: if abort: return else: - page.append(("", "\n")) - rows_in_buffer += 1 - columns_in_buffer = 0 + # Add line ending between lines (if `end="\n"` was given, one + # more empty line is added in `split_lines` automatically to + # take care of the final line ending). + if lineno != len(lines) - 1: + page.append(("", "\n")) + rows_in_buffer += 1 + columns_in_buffer = 0 flush_page() From fcc90bb139d52f222b0cb9d237fe3849737103b2 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Mon, 25 Jan 2021 17:40:29 +0100 Subject: [PATCH 105/220] Improve handling of indented code. - Allow multiline input to be indented as a whole. (We will unindent before executing.) - Use `TabsProcessor` to properly visualize tabs that were pasted (in bracketed paste) instead of `^I`. --- ptpython/layout.py | 2 ++ ptpython/python_input.py | 10 +++++----- ptpython/utils.py | 40 +++++++++++++++++++++++++++++++++++++++- ptpython/validator.py | 10 ++++------ 4 files changed, 50 insertions(+), 12 deletions(-) diff --git a/ptpython/layout.py b/ptpython/layout.py index 4ad70d36..3cf3c77d 100644 --- a/ptpython/layout.py +++ b/ptpython/layout.py @@ -40,6 +40,7 @@ HighlightIncrementalSearchProcessor, HighlightMatchingBracketProcessor, HighlightSelectionProcessor, + TabsProcessor, ) from prompt_toolkit.lexers import SimpleLexer from prompt_toolkit.mouse_events import MouseEvent @@ -603,6 +604,7 @@ def menu_position(): ), HighlightSelectionProcessor(), DisplayMultipleCursors(), + TabsProcessor(), # Show matching parentheses, but only while editing. ConditionalProcessor( processor=HighlightMatchingBracketProcessor(chars="[](){}"), diff --git a/ptpython/python_input.py b/ptpython/python_input.py index fb0cc6a3..c84c80f9 100644 --- a/ptpython/python_input.py +++ b/ptpython/python_input.py @@ -67,7 +67,7 @@ from .lexer import PtpythonLexer from .prompt_style import ClassicPrompt, IPythonPrompt, PromptStyle from .style import generate_style, get_all_code_styles, get_all_ui_styles -from .utils import get_jedi_script_from_document +from .utils import get_jedi_script_from_document, unindent_code from .validator import PythonValidator __all__ = ["PythonInput"] @@ -1036,10 +1036,10 @@ def in_thread() -> None: # (Important for Windows users.) raise EOFError - # If the input is single line, remove leading whitespace. - # (This doesn't have to be a syntax error.) - if len(result.splitlines()) == 1: - result = result.strip() + # Remove leading whitespace. + # (Users can add extra indentation, which happens for + # instance because of copy/pasting code.) + result = unindent_code(result) if result and not result.isspace(): return diff --git a/ptpython/utils.py b/ptpython/utils.py index 3658085a..2fb24a41 100644 --- a/ptpython/utils.py +++ b/ptpython/utils.py @@ -2,7 +2,7 @@ For internal use only. """ import re -from typing import Callable, Type, TypeVar, cast +from typing import Callable, Iterable, Type, TypeVar, cast from prompt_toolkit.formatted_text import to_formatted_text from prompt_toolkit.formatted_text.utils import fragment_list_to_text @@ -12,6 +12,7 @@ "has_unclosed_brackets", "get_jedi_script_from_document", "document_is_multiline_python", + "unindent_code", ] @@ -158,3 +159,40 @@ def __repr__(self) -> str: cls.__repr__ = __repr__ # type:ignore return cls + + +def unindent_code(text: str) -> str: + """ + Remove common leading whitespace when all lines are indented. + """ + lines = text.splitlines(keepends=True) + + # Look for common prefix. + common_prefix = _common_whitespace_prefix(lines) + + # Remove indentation. + lines = [line[len(common_prefix) :] for line in lines] + + return "".join(lines) + + +def _common_whitespace_prefix(strings: Iterable[str]) -> str: + """ + Return common prefix for a list of lines. + This will ignore lines that contain whitespace only. + """ + # Ignore empty lines and lines that have whitespace only. + strings = [s for s in strings if not s.isspace() and not len(s) == 0] + + if not strings: + return "" + + else: + s1 = min(strings) + s2 = max(strings) + + for i, c in enumerate(s1): + if c != s2[i] or c not in " \t": + return s1[:i] + + return s1 diff --git a/ptpython/validator.py b/ptpython/validator.py index a027ecb1..0f6a4eaf 100644 --- a/ptpython/validator.py +++ b/ptpython/validator.py @@ -1,5 +1,7 @@ from prompt_toolkit.validation import ValidationError, Validator +from .utils import unindent_code + __all__ = ["PythonValidator"] @@ -18,12 +20,7 @@ def validate(self, document): """ Check input for Python syntax errors. """ - text = document.text - - # If the input is single line, remove leading whitespace. - # (This doesn't have to be a syntax error.) - if len(text.splitlines()) == 1: - text = text.strip() + text = unindent_code(document.text) # When the input starts with Ctrl-Z, always accept. This means EOF in a # Python REPL. @@ -46,6 +43,7 @@ def validate(self, document): # Note, the 'or 1' for offset is required because Python 2.7 # gives `None` as offset in case of '4=4' as input. (Looks like # fixed in Python 3.) + # TODO: This is not correct if indentation was removed. index = document.translate_row_col_to_index( e.lineno - 1, (e.offset or 1) - 1 ) From 325072295bec594b8de273efe2daecdcb08f7e6a Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Mon, 25 Jan 2021 18:27:03 +0100 Subject: [PATCH 106/220] Added 'print_all' option in pager. --- ptpython/repl.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/ptpython/repl.py b/ptpython/repl.py index e8ca3a0e..9e22e2f6 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -361,6 +361,7 @@ def print_paginated_formatted_text( size = self.app.output.get_size() abort = False + print_all = False # Max number of lines allowed in the buffer before painting. max_rows = size.rows - 1 @@ -378,7 +379,7 @@ def flush_page() -> None: rows_in_buffer = 0 def show_pager() -> None: - nonlocal abort, max_rows + nonlocal abort, max_rows, print_all # Run pager prompt in another thread. # Same as for the input. This prevents issues with nested event @@ -403,6 +404,9 @@ def in_thread() -> None: elif pager_result == PagerResult.NEXT_PAGE: max_rows = size.rows - 1 + elif pager_result == PagerResult.PRINT_ALL: + print_all = True + # Loop over lines. Show --MORE-- prompt when page is filled. formatted_text = formatted_text + [("", end)] @@ -417,7 +421,7 @@ def in_thread() -> None: if columns_in_buffer + width > size.columns: # Show pager first if we get too many lines after # wrapping. - if rows_in_buffer + 1 >= max_rows: + if rows_in_buffer + 1 >= max_rows and not print_all: flush_page() show_pager() if abort: @@ -429,7 +433,7 @@ def in_thread() -> None: columns_in_buffer += width page.append((style, c)) - if rows_in_buffer + 1 >= max_rows: + if rows_in_buffer + 1 >= max_rows and not print_all: flush_page() show_pager() if abort: @@ -662,6 +666,7 @@ class PagerResult(Enum): ABORT = "ABORT" NEXT_LINE = "NEXT_LINE" NEXT_PAGE = "NEXT_PAGE" + PRINT_ALL = "PRINT_ALL" def create_pager_prompt( @@ -681,6 +686,10 @@ def next_line(event: KeyPressEvent) -> None: def next_page(event: KeyPressEvent) -> None: event.app.exit(result=PagerResult.NEXT_PAGE) + @bindings.add("a") + def print_all(event: KeyPressEvent) -> None: + event.app.exit(result=PagerResult.PRINT_ALL) + @bindings.add("q") @bindings.add("c-c") @bindings.add("c-d") @@ -704,6 +713,7 @@ def _(event: KeyPressEvent) -> None: " -- MORE -- " "[Enter] Scroll " "[Space] Next page " + "[a] Print all " "[q] Quit " ": " ), From 7f127c5be94d682c6ffda2a17e7923c3973930d9 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Mon, 25 Jan 2021 18:26:26 +0100 Subject: [PATCH 107/220] Fix line ending bug in pager. --- ptpython/repl.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ptpython/repl.py b/ptpython/repl.py index 9e22e2f6..0006a111 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -422,6 +422,7 @@ def in_thread() -> None: # Show pager first if we get too many lines after # wrapping. if rows_in_buffer + 1 >= max_rows and not print_all: + page.append(("", "\n")) flush_page() show_pager() if abort: @@ -434,6 +435,7 @@ def in_thread() -> None: page.append((style, c)) if rows_in_buffer + 1 >= max_rows and not print_all: + page.append(("", "\n")) flush_page() show_pager() if abort: From 93d45e060e775699102311d55dc1b58fae8ce1cd Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Tue, 26 Jan 2021 09:28:27 +0100 Subject: [PATCH 108/220] Added ptpython-in-other-thread.py example. --- .../test-cases/ptpython-in-other-thread.py | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 examples/test-cases/ptpython-in-other-thread.py diff --git a/examples/test-cases/ptpython-in-other-thread.py b/examples/test-cases/ptpython-in-other-thread.py new file mode 100644 index 00000000..7c788464 --- /dev/null +++ b/examples/test-cases/ptpython-in-other-thread.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python +""" +Example of running ptpython in another thread. + +(For testing whether it's working fine if it's not embedded in the main +thread.) +""" +import threading + +from ptpython.repl import embed + + +def in_thread(): + embed(globals(), locals(), vi_mode=False) + + +def main(): + th = threading.Thread(target=in_thread) + th.start() + th.join() + + +if __name__ == "__main__": + main() From 9a3d6c1b2ab8247797b5cf5dea46c87767817c23 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Tue, 26 Jan 2021 10:13:10 +0100 Subject: [PATCH 109/220] Release 3.0.13 --- CHANGELOG | 15 +++++++++++++++ setup.py | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index ee90fcb5..5b704461 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,21 @@ CHANGELOG ========= +3.0.13: 2020-01-26 +------------------ + +New features: +- Added 'print all' option to pager. +- Improve handling of indented code: + * Allow multiline input to be indented as a whole (we will unindent before + executing). + * Correctly visualize tabs (instead of ^I, which happens when pasted in + bracketed paste). + +Fixes: +- Fix line ending bug in pager. + + 3.0.12: 2020-01-24 ------------------ diff --git a/setup.py b/setup.py index 0b0da6ce..09643f49 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ setup( name="ptpython", author="Jonathan Slenders", - version="3.0.12", + version="3.0.13", url="https://github.com/prompt-toolkit/ptpython", description="Python REPL build on top of prompt_toolkit", long_description=long_description, From 7ea2e5bc23301ea1790b2ec19b0554dee78c9a39 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Wed, 27 Jan 2021 10:49:53 +0100 Subject: [PATCH 110/220] Handle exceptions raised when repr() is called. --- ptpython/repl.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/ptpython/repl.py b/ptpython/repl.py index 0006a111..301b3207 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -270,7 +270,13 @@ def show_result(self, result: object) -> None: out_prompt = to_formatted_text(self.get_output_prompt()) # If the repr is valid Python code, use the Pygments lexer. - result_repr = repr(result) + try: + result_repr = repr(result) + except BaseException as e: + # Calling repr failed. + self._handle_exception(e) + return + try: compile(result_repr, "", "eval") except SyntaxError: From 5af2dac65fdf5d73b7730437952158c4b0dfb996 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Tue, 9 Feb 2021 15:09:26 +0100 Subject: [PATCH 111/220] Fix leakage of exc_info from eval to exec call. `exec()` was always executed in the `except SyntaxError` block of the try around `eval()`, and because of this ``sys.exc_info()`` would not see the right exception if called as a statement. See: https://github.com/prompt-toolkit/ptpython/issues/435 Thanks to Peter Holloway for the proposed fix. --- ptpython/repl.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/ptpython/repl.py b/ptpython/repl.py index 301b3207..1253504d 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -199,9 +199,7 @@ def eval(self, line: str) -> object: try: code = self._compile_with_flags(line, "eval") except SyntaxError: - # If not a valid `eval` expression, run using `exec` instead. - code = self._compile_with_flags(line, "exec") - exec(code, self.get_globals(), self.get_locals()) + pass else: # No syntax errors for eval. Do eval. result = eval(code, self.get_globals(), self.get_locals()) @@ -212,6 +210,13 @@ def eval(self, line: str) -> object: self._store_eval_result(result) return result + # If not a valid `eval` expression, run using `exec` instead. + # Note that we shouldn't run this in the `except SyntaxError` block + # above, then `sys.exc_info()` would not report the right error. + # See issue: https://github.com/prompt-toolkit/ptpython/issues/435 + code = self._compile_with_flags(line, "exec") + exec(code, self.get_globals(), self.get_locals()) + return None async def eval_async(self, line: str) -> object: @@ -231,9 +236,7 @@ async def eval_async(self, line: str) -> object: try: code = self._compile_with_flags(line, "eval") except SyntaxError: - # If not a valid `eval` expression, run using `exec` instead. - code = self._compile_with_flags(line, "exec") - exec(code, self.get_globals(), self.get_locals()) + pass else: # No syntax errors for eval. Do eval. result = eval(code, self.get_globals(), self.get_locals()) @@ -244,6 +247,10 @@ async def eval_async(self, line: str) -> object: self._store_eval_result(result) return result + # If not a valid `eval` expression, run using `exec` instead. + code = self._compile_with_flags(line, "exec") + exec(code, self.get_globals(), self.get_locals()) + return None def _store_eval_result(self, result: object) -> None: From 08b7417d3fdec0ddca4aa45eb899942d639bd8a3 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Tue, 9 Feb 2021 14:37:12 +0100 Subject: [PATCH 112/220] Fix handling of `KeyboardInterrupt` in REPL during evaluation of __repr__. This fixes the issue that if calling `__repr__`, `__pt_repr__` or formatting the output using "Black" takes too long and the uses presses control-C, that we don't terminate the REPL by mistake. --- ptpython/repl.py | 114 ++++++++++++++++++++++++++++------------------- 1 file changed, 69 insertions(+), 45 deletions(-) diff --git a/ptpython/repl.py b/ptpython/repl.py index 1253504d..ae7b1d0d 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -102,30 +102,39 @@ def run(self) -> None: 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. + # 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) - 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() @@ -152,31 +161,38 @@ async def run_async(self) -> None: try: while True: - # 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. + # 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) - 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 = [] finally: if self.terminal_title: clear_title() @@ -273,12 +289,18 @@ def _compile_with_flags(self, code: str, mode: str): def show_result(self, result: object) -> None: """ Show __repr__ for an `eval` result. + + Note: this can raise `KeyboardInterrupt` if either calling `__repr__`, + `__pt_repr__` or formatting the output with "Black" takes to long + and the user presses Control-C. """ out_prompt = to_formatted_text(self.get_output_prompt()) # If the repr is valid Python code, use the Pygments lexer. try: result_repr = repr(result) + except KeyboardInterrupt: + raise # Don't catch here. except BaseException as e: # Calling repr failed. self._handle_exception(e) @@ -313,6 +335,8 @@ def show_result(self, result: object) -> None: ) if isinstance(formatted_result_repr, list): formatted_result_repr = FormattedText(formatted_result_repr) + except KeyboardInterrupt: + raise # Don't catch here. except: # For bad code, `__getattr__` can raise something that's not an # `AttributeError`. This happens already when calling `hasattr()`. From fd97a4254e936dcc191fe57bf08c298b62a5b085 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Thu, 28 Jan 2021 12:16:08 +0100 Subject: [PATCH 113/220] Fix style for signature toolbar. --- ptpython/style.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ptpython/style.py b/ptpython/style.py index b16be697..23e51c7e 100644 --- a/ptpython/style.py +++ b/ptpython/style.py @@ -87,8 +87,8 @@ def generate_style(python_style: BaseStyle, ui_style: BaseStyle) -> BaseStyle: "arg-toolbar.text": "noinherit", # Signature toolbar. "signature-toolbar": "bg:#44bbbb #000000", - "signature-toolbar.currentname": "bg:#008888 #ffffff bold", - "signature-toolbar.operator": "#000000 bold", + "signature-toolbar current-name": "bg:#008888 #ffffff bold", + "signature-toolbar operator": "#000000 bold", "docstring": "#888888", # Validation toolbar. "validation-toolbar": "bg:#440000 #aaaaaa", From 1b528cf5c1b355ba3bd040b0086957dd5072b567 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Thu, 28 Jan 2021 12:17:14 +0100 Subject: [PATCH 114/220] Allow display of signature and completion drop down together. --- ptpython/layout.py | 60 +++++++++++++++++++--------------------------- setup.py | 7 +++--- 2 files changed, 27 insertions(+), 40 deletions(-) diff --git a/ptpython/layout.py b/ptpython/layout.py index 3cf3c77d..4c76dbd4 100644 --- a/ptpython/layout.py +++ b/ptpython/layout.py @@ -230,8 +230,8 @@ def signature_toolbar(python_input): Return the `Layout` for the signature. """ - def get_text_fragments(): - result = [] + def get_text_fragments() -> StyleAndTextTuples: + result: StyleAndTextTuples = [] append = result.append Signature = "class:signature-toolbar" @@ -260,7 +260,7 @@ def get_text_fragments(): # and sig has no 'index' attribute. # See: https://github.com/jonathanslenders/ptpython/issues/47 # https://github.com/davidhalter/jedi/issues/598 - description = p.description if p else "*" # or '*' + description = p.description if p else "*" sig_index = getattr(sig, "index", 0) if i == sig_index: @@ -286,16 +286,8 @@ def get_text_fragments(): filter= # Show only when there is a signature HasSignature(python_input) & - # And there are no completions to be shown. (would cover signature pop-up.) - ~( - has_completions - & ( - show_completions_menu(python_input) - | show_multi_column_completions_menu(python_input) - ) - ) # Signature needs to be shown. - & ShowSignature(python_input) & + ShowSignature(python_input) & # Not done yet. ~is_done, ) @@ -656,33 +648,29 @@ def menu_position(): Float( xcursor=True, ycursor=True, - content=ConditionalContainer( - content=CompletionsMenu( - scroll_offset=( - lambda: python_input.completion_menu_scroll_offset + content=HSplit( + [ + signature_toolbar(python_input), + ConditionalContainer( + content=CompletionsMenu( + scroll_offset=( + lambda: python_input.completion_menu_scroll_offset + ), + max_height=12, + ), + filter=show_completions_menu( + python_input + ), ), - max_height=12, - ), - filter=show_completions_menu( - python_input - ), - ), - ), - Float( - xcursor=True, - ycursor=True, - content=ConditionalContainer( - content=MultiColumnCompletionsMenu(), - filter=show_multi_column_completions_menu( - python_input - ), + ConditionalContainer( + content=MultiColumnCompletionsMenu(), + filter=show_multi_column_completions_menu( + python_input + ), + ), + ] ), ), - Float( - xcursor=True, - ycursor=True, - content=signature_toolbar(python_input), - ), Float( left=2, bottom=1, diff --git a/setup.py b/setup.py index 09643f49..a803ef1a 100644 --- a/setup.py +++ b/setup.py @@ -20,10 +20,9 @@ "appdirs", "importlib_metadata;python_version<'3.8'", "jedi>=0.16.0", - # Use prompt_toolkit 3.0.11, because ptpython now runs the UI in the - # background thread, and we need the terminal size polling that was - # introduced here. - "prompt_toolkit>=3.0.11,<3.1.0", + # Use prompt_toolkit 3.0.12, because of dont_extend_width bugfix when + # signature and completion dropdown are displayed together. + "prompt_toolkit>=3.0.12,<3.1.0", "pygments", "black", ], From 107aba8dea556207389227c6789043e50c65ff95 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Thu, 28 Jan 2021 12:18:32 +0100 Subject: [PATCH 115/220] Cleanup of completion code. --- ptpython/completer.py | 205 +++++++++++++++++++++++++----------------- ptpython/style.py | 1 + 2 files changed, 125 insertions(+), 81 deletions(-) diff --git a/ptpython/completer.py b/ptpython/completer.py index aee280f4..af4d1c74 100644 --- a/ptpython/completer.py +++ b/ptpython/completer.py @@ -51,6 +51,7 @@ def __init__( self.get_enable_dictionary_completion = get_enable_dictionary_completion self._system_completer = SystemCompleter() + self._jedi_completer = JediCompleter(get_globals, get_locals) self._dictionary_completer = DictionaryCompleter(get_globals, get_locals) self._path_completer_cache: Optional[GrammarCompleter] = None @@ -129,10 +130,14 @@ def _complete_path_while_typing(self, document: Document) -> bool: ) def _complete_python_while_typing(self, document: Document) -> bool: - char_before_cursor = document.char_before_cursor + """ + When `complete_while_typing` is set, only return completions when this + returns `True`. + """ + text = document.text_before_cursor.rstrip() + char_before_cursor = text[-1:] return bool( - document.text - and (char_before_cursor.isalnum() or char_before_cursor in "_.") + text and (char_before_cursor.isalnum() or char_before_cursor in "_.(,") ) def get_completions( @@ -151,94 +156,127 @@ def get_completions( ) return - # Do dictionary key completions. - if self.get_enable_dictionary_completion(): - has_dict_completions = False - for c in self._dictionary_completer.get_completions( - document, complete_event - ): - if c.text not in "[.": - # If we get the [ or . completion, still include the other - # completions. - has_dict_completions = True - yield c - if has_dict_completions: - return - # Do Path completions (if there were no dictionary completions). if complete_event.completion_requested or self._complete_path_while_typing( document ): yield from self._path_completer.get_completions(document, complete_event) - # If we are inside a string, Don't do Jedi completion. - if self._path_completer_grammar.match(document.text_before_cursor): - return - - # Do Jedi Python completions. if complete_event.completion_requested or self._complete_python_while_typing( document ): - script = get_jedi_script_from_document( - document, self.get_locals(), self.get_globals() - ) + # If we are inside a string, Don't do Python completion. + if self._path_completer_grammar.match(document.text_before_cursor): + return - if script: - try: - jedi_completions = script.complete( - column=document.cursor_position_col, - line=document.cursor_position_row + 1, + # Do dictionary key completions. + if self.get_enable_dictionary_completion(): + has_dict_completions = False + for c in self._dictionary_completer.get_completions( + document, complete_event + ): + if c.text not in "[.": + # If we get the [ or . completion, still include the other + # completions. + has_dict_completions = True + yield c + if has_dict_completions: + return + + # Do Jedi Python completions. + yield from self._jedi_completer.get_completions(document, complete_event) + + +class JediCompleter(Completer): + """ + Autocompleter that uses the Jedi library. + """ + + def __init__(self, get_globals, get_locals) -> None: + super().__init__() + + self.get_globals = get_globals + self.get_locals = get_locals + + def get_completions( + self, document: Document, complete_event: CompleteEvent + ) -> Iterable[Completion]: + script = get_jedi_script_from_document( + document, self.get_locals(), self.get_globals() + ) + + if script: + try: + jedi_completions = script.complete( + column=document.cursor_position_col, + line=document.cursor_position_row + 1, + ) + except TypeError: + # Issue #9: bad syntax causes completions() to fail in jedi. + # https://github.com/jonathanslenders/python-prompt-toolkit/issues/9 + pass + except UnicodeDecodeError: + # Issue #43: UnicodeDecodeError on OpenBSD + # https://github.com/jonathanslenders/python-prompt-toolkit/issues/43 + pass + except AttributeError: + # Jedi issue #513: https://github.com/davidhalter/jedi/issues/513 + pass + except ValueError: + # Jedi issue: "ValueError: invalid \x escape" + pass + except KeyError: + # Jedi issue: "KeyError: u'a_lambda'." + # https://github.com/jonathanslenders/ptpython/issues/89 + pass + except IOError: + # Jedi issue: "IOError: No such file or directory." + # https://github.com/jonathanslenders/ptpython/issues/71 + pass + except AssertionError: + # In jedi.parser.__init__.py: 227, in remove_last_newline, + # the assertion "newline.value.endswith('\n')" can fail. + pass + except SystemError: + # In jedi.api.helpers.py: 144, in get_stack_at_position + # raise SystemError("This really shouldn't happen. There's a bug in Jedi.") + pass + except NotImplementedError: + # See: https://github.com/jonathanslenders/ptpython/issues/223 + pass + except Exception: + # Supress all other Jedi exceptions. + pass + else: + # Move function parameters to the top. + jedi_completions = sorted( + jedi_completions, + key=lambda jc: ( + # Params first. + jc.type != "param", + # Private at the end. + jc.name.startswith("_"), + # Then sort by name. + jc.name_with_symbols.lower(), + ), + ) + + for jc in jedi_completions: + if jc.type == "function": + suffix = "()" + else: + suffix = "" + + if jc.type == "param": + suffix = "..." + + yield Completion( + jc.name_with_symbols, + len(jc.complete) - len(jc.name_with_symbols), + display=jc.name_with_symbols + suffix, + display_meta=jc.type, + style=_get_style_for_jedi_completion(jc), ) - except TypeError: - # Issue #9: bad syntax causes completions() to fail in jedi. - # https://github.com/jonathanslenders/python-prompt-toolkit/issues/9 - pass - except UnicodeDecodeError: - # Issue #43: UnicodeDecodeError on OpenBSD - # https://github.com/jonathanslenders/python-prompt-toolkit/issues/43 - pass - except AttributeError: - # Jedi issue #513: https://github.com/davidhalter/jedi/issues/513 - pass - except ValueError: - # Jedi issue: "ValueError: invalid \x escape" - pass - except KeyError: - # Jedi issue: "KeyError: u'a_lambda'." - # https://github.com/jonathanslenders/ptpython/issues/89 - pass - except IOError: - # Jedi issue: "IOError: No such file or directory." - # https://github.com/jonathanslenders/ptpython/issues/71 - pass - except AssertionError: - # In jedi.parser.__init__.py: 227, in remove_last_newline, - # the assertion "newline.value.endswith('\n')" can fail. - pass - except SystemError: - # In jedi.api.helpers.py: 144, in get_stack_at_position - # raise SystemError("This really shouldn't happen. There's a bug in Jedi.") - pass - except NotImplementedError: - # See: https://github.com/jonathanslenders/ptpython/issues/223 - pass - except Exception: - # Supress all other Jedi exceptions. - pass - else: - for jc in jedi_completions: - if jc.type == "function": - suffix = "()" - else: - suffix = "" - - yield Completion( - jc.name_with_symbols, - len(jc.complete) - len(jc.name_with_symbols), - display=jc.name_with_symbols + suffix, - display_meta=jc.type, - style=_get_style_for_name(jc.name_with_symbols), - ) class DictionaryCompleter(Completer): @@ -575,10 +613,15 @@ class ReprFailedError(Exception): _builtin_names = [] -def _get_style_for_name(name: str) -> str: +def _get_style_for_jedi_completion(jedi_completion) -> str: """ Return completion style to use for this name. """ + name = jedi_completion.name_with_symbols + + if jedi_completion.type == "param": + return "class:completion.param" + if name in _builtin_names: return "class:completion.builtin" diff --git a/ptpython/style.py b/ptpython/style.py index 23e51c7e..4b54d0cd 100644 --- a/ptpython/style.py +++ b/ptpython/style.py @@ -75,6 +75,7 @@ def generate_style(python_style: BaseStyle, ui_style: BaseStyle) -> BaseStyle: "out.number": "#ff0000", # Completions. "completion.builtin": "", + "completion.param": "#006666 italic", "completion.keyword": "fg:#008800", "completion.keyword fuzzymatch.inside": "fg:#008800", "completion.keyword fuzzymatch.outside": "fg:#44aa44", From 2238d412952d43dd9a2dfbe30a42b02a1a115c28 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Thu, 28 Jan 2021 12:27:32 +0100 Subject: [PATCH 116/220] Improve signature pop-up. --- ptpython/layout.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ptpython/layout.py b/ptpython/layout.py index 4c76dbd4..3496b60f 100644 --- a/ptpython/layout.py +++ b/ptpython/layout.py @@ -240,7 +240,7 @@ def get_text_fragments() -> StyleAndTextTuples: append((Signature, " ")) try: - append((Signature, sig.full_name)) + append((Signature, sig.name)) except IndexError: # Workaround for #37: https://github.com/jonathanslenders/python-prompt-toolkit/issues/37 # See also: https://github.com/davidhalter/jedi/issues/490 @@ -260,7 +260,7 @@ def get_text_fragments() -> StyleAndTextTuples: # and sig has no 'index' attribute. # See: https://github.com/jonathanslenders/ptpython/issues/47 # https://github.com/davidhalter/jedi/issues/598 - description = p.description if p else "*" + description = p.to_string() if p else "*" sig_index = getattr(sig, "index", 0) if i == sig_index: From bf991c67a3b9a46c7fbd73085ca58d79f99ab995 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Thu, 28 Jan 2021 13:59:36 +0100 Subject: [PATCH 117/220] Better signature abstractions. Retrieve signatures without Jedi when Jedi fails. --- ptpython/completer.py | 24 +++- ptpython/layout.py | 43 ++++--- ptpython/python_input.py | 48 +++----- ptpython/signatures.py | 238 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 296 insertions(+), 57 deletions(-) create mode 100644 ptpython/signatures.py diff --git a/ptpython/completer.py b/ptpython/completer.py index af4d1c74..68774be4 100644 --- a/ptpython/completer.py +++ b/ptpython/completer.py @@ -157,6 +157,7 @@ def get_completions( return # Do Path completions (if there were no dictionary completions). + # TODO: not if we have dictionary completions... if complete_event.completion_requested or self._complete_path_while_typing( document ): @@ -383,7 +384,7 @@ def get_completions( self, document: Document, complete_event: CompleteEvent ) -> Iterable[Completion]: - # First, find all for-loops, and assing the first item of the + # First, find all for-loops, and assign the first item of the # collections they're iterating to the iterator variable, so that we # can provide code completion on the iterators. temp_locals = self.get_locals().copy() @@ -414,6 +415,17 @@ def _do_repr(self, obj: object) -> str: except BaseException: raise ReprFailedError + def eval_expression(self, document: Document, locals: Dict[str, Any]) -> object: + """ + Evaluate + """ + match = self.expression_pattern.search(document.text_before_cursor) + if match is not None: + object_var = match.groups()[0] + return self._lookup(object_var, locals) + + return None + def _get_expression_completions( self, document: Document, @@ -423,17 +435,17 @@ def _get_expression_completions( """ Complete the [ or . operator after an object. """ - match = self.expression_pattern.search(document.text_before_cursor) - if match is not None: - object_var = match.groups()[0] - result = self._lookup(object_var, temp_locals) + result = self.eval_expression(document, temp_locals) + + if result is not None: if isinstance( result, (list, tuple, dict, collections_abc.Mapping, collections_abc.Sequence), ): yield Completion("[", 0) - elif result is not None: + + else: # Note: Don't call `if result` here. That can fail for types # that have custom truthness checks. yield Completion(".", 0) diff --git a/ptpython/layout.py b/ptpython/layout.py index 3496b60f..b12010ce 100644 --- a/ptpython/layout.py +++ b/ptpython/layout.py @@ -4,13 +4,13 @@ import platform import sys from enum import Enum +from inspect import _ParameterKind as ParameterKind from typing import TYPE_CHECKING, Optional from prompt_toolkit.application import get_app from prompt_toolkit.enums import DEFAULT_BUFFER, SEARCH_BUFFER from prompt_toolkit.filters import ( Condition, - has_completions, has_focus, is_done, renderer_height_is_known, @@ -248,30 +248,41 @@ def get_text_fragments() -> StyleAndTextTuples: append((Signature + ",operator", "(")) - try: - enumerated_params = enumerate(sig.params) - except AttributeError: - # Workaround for #136: https://github.com/jonathanslenders/ptpython/issues/136 - # AttributeError: 'Lambda' object has no attribute 'get_subscope_by_name' - return [] + got_positional_only = False + got_keyword_only = False + + for i, p in enumerate(sig.parameters): + # Detect transition between positional-only and not positional-only. + if p.kind == ParameterKind.POSITIONAL_ONLY: + got_positional_only = True + if got_positional_only and p.kind != ParameterKind.POSITIONAL_ONLY: + got_positional_only = False + append((Signature, "/")) + append((Signature + ",operator", ", ")) - for i, p in enumerated_params: - # Workaround for #47: 'p' is None when we hit the '*' in the signature. - # and sig has no 'index' attribute. - # See: https://github.com/jonathanslenders/ptpython/issues/47 - # https://github.com/davidhalter/jedi/issues/598 - description = p.to_string() if p else "*" + if not got_keyword_only and p.kind == ParameterKind.KEYWORD_ONLY: + got_keyword_only = True + append((Signature, "*")) + append((Signature + ",operator", ", ")) + + description = p.name sig_index = getattr(sig, "index", 0) if i == sig_index: # Note: we use `_Param.description` instead of # `_Param.name`, that way we also get the '*' before args. - append((Signature + ",current-name", str(description))) + append((Signature + ",current-name", description)) else: append((Signature, str(description))) + + if p.default: + # NOTE: For the jedi-based completion, the default is + # currently still part of the name. + append((Signature, f"={p.default}")) + append((Signature + ",operator", ", ")) - if sig.params: + if sig.parameters: # Pop last comma result.pop() @@ -577,7 +588,7 @@ def menu_position(): """ b = python_input.default_buffer - if b.complete_state is None and python_input.signatures: + if python_input.signatures: row, col = python_input.signatures[0].bracket_start index = b.document.translate_row_col_to_index(row - 1, col) return index diff --git a/ptpython/python_input.py b/ptpython/python_input.py index c84c80f9..8d5da502 100644 --- a/ptpython/python_input.py +++ b/ptpython/python_input.py @@ -66,8 +66,9 @@ from .layout import CompletionVisualisation, PtPythonLayout from .lexer import PtpythonLexer from .prompt_style import ClassicPrompt, IPythonPrompt, PromptStyle +from .signatures import Signature, get_signatures_using_eval, get_signatures_using_jedi from .style import generate_style, get_all_code_styles, get_all_ui_styles -from .utils import get_jedi_script_from_document, unindent_code +from .utils import unindent_code from .validator import PythonValidator __all__ = ["PythonInput"] @@ -260,7 +261,7 @@ def __init__( self.enable_syntax_highlighting: bool = True self.enable_fuzzy_completion: bool = False - self.enable_dictionary_completion: bool = False + self.enable_dictionary_completion: bool = False # Also eval-based completion. self.complete_private_attributes: CompletePrivateAttributes = ( CompletePrivateAttributes.ALWAYS ) @@ -330,7 +331,7 @@ def __init__( self.current_statement_index: int = 1 # Code signatures. (This is set asynchronously after a timeout.) - self.signatures: List[Any] = [] + self.signatures: List[Signature] = [] # Boolean indicating whether we have a signatures thread running. # (Never run more than one at the same time.) @@ -917,36 +918,16 @@ def _on_input_timeout(self, buff: Buffer, loop=None) -> None: loop = loop or get_event_loop() def run(): - script = get_jedi_script_from_document( + # First, get signatures from Jedi. If we didn't found any and if + # "dictionary completion" (eval-based completion) is enabled, then + # get signatures using eval. + signatures = get_signatures_using_jedi( document, self.get_locals(), self.get_globals() ) - - # Show signatures in help text. - if script: - try: - signatures = script.get_signatures() - except ValueError: - # e.g. in case of an invalid \\x escape. - signatures = [] - except Exception: - # Sometimes we still get an exception (TypeError), because - # of probably bugs in jedi. We can silence them. - # See: https://github.com/davidhalter/jedi/issues/492 - signatures = [] - else: - # Try to access the params attribute just once. For Jedi - # signatures containing the keyword-only argument star, - # this will crash when retrieving it the first time with - # AttributeError. Every following time it works. - # See: https://github.com/jonathanslenders/ptpython/issues/47 - # https://github.com/davidhalter/jedi/issues/598 - try: - if signatures: - signatures[0].params - except AttributeError: - pass - else: - signatures = [] + if not signatures and self.enable_dictionary_completion: + signatures = get_signatures_using_eval( + document, self.get_locals(), self.get_globals() + ) self._get_signatures_thread_running = False @@ -957,11 +938,8 @@ def run(): # Set docstring in docstring buffer. if signatures: - string = signatures[0].docstring() - if not isinstance(string, str): - string = string.decode("utf-8") self.docstring_buffer.reset( - document=Document(string, cursor_position=0) + document=Document(signatures[0].docstring, cursor_position=0) ) else: self.docstring_buffer.reset() diff --git a/ptpython/signatures.py b/ptpython/signatures.py new file mode 100644 index 00000000..39cdba29 --- /dev/null +++ b/ptpython/signatures.py @@ -0,0 +1,238 @@ +""" +Helpers for retrieving the function signature of the function call that we are +editing. + +Either with the Jedi library, or using `inspect.signature` if Jedi fails and we +can use `eval()` to evaluate the function object. +""" +import inspect +from inspect import Signature as InspectSignature +from inspect import _ParameterKind as ParameterKind +from typing import Any, Dict, List, Optional, Sequence, Tuple + +from prompt_toolkit.document import Document + +from .completer import DictionaryCompleter +from .utils import get_jedi_script_from_document + +__all__ = ["Signature", "get_signatures_using_jedi", "get_signatures_using_eval"] + + +class Parameter: + def __init__( + self, + name: str, + annotation: Optional[str], + default: Optional[str], + kind: ParameterKind, + ) -> None: + self.name = name + self.kind = kind + + self.annotation = annotation + self.default = default + + def __repr__(self) -> str: + return f"Parameter(name={self.name!r})" + + +class Signature: + """ + Signature definition used wrap around both Jedi signatures and + python-inspect signatures. + + :param index: Parameter index of the current cursor position. + :param bracket_start: (line, column) tuple for the open bracket that starts + the function call. + """ + + def __init__( + self, + name: str, + docstring: str, + parameters: Sequence[Parameter], + index: Optional[int] = None, + returns: str = "", + bracket_start: Tuple[int, int] = (0, 0), + ) -> None: + self.name = name + self.docstring = docstring + self.parameters = parameters + self.index = index + self.returns = returns + self.bracket_start = bracket_start + + @classmethod + def from_inspect_signature( + cls, + name: str, + docstring: str, + signature: InspectSignature, + index: int, + ) -> "Signature": + parameters = [] + for p in signature.parameters.values(): + parameters.append( + Parameter( + name=p.name, + annotation=p.annotation.__name__, + default=repr(p.default) + if p.default is not inspect.Parameter.empty + else None, + kind=p.kind, + ) + ) + + return cls( + name=name, + docstring=docstring, + parameters=parameters, + index=index, + returns="", + ) + + @classmethod + def from_jedi_signature(cls, signature) -> "Signature": + parameters = [] + + for p in signature.params: + if p is None: + # We just hit the "*". + continue + + parameters.append( + Parameter( + name=p.to_string(), # p.name, + annotation=None, # p.infer_annotation() + default=None, # p.infer_default() + kind=p.kind, + ) + ) + + docstring = signature.docstring() + if not isinstance(docstring, str): + docstring = docstring.decode("utf-8") + + return cls( + name=signature.name, + docstring=docstring, + parameters=parameters, + index=signature.index, + returns="", + bracket_start=signature.bracket_start, + ) + + def __repr__(self) -> str: + return f"Signature({self.name!r}, parameters={self.parameters!r})" + + +def get_signatures_using_jedi( + document: Document, locals: Dict[str, Any], globals: Dict[str, Any] +) -> List[Signature]: + script = get_jedi_script_from_document(document, locals, globals) + + # Show signatures in help text. + if not script: + return [] + + try: + signatures = script.get_signatures() + except ValueError: + # e.g. in case of an invalid \\x escape. + signatures = [] + except Exception: + # Sometimes we still get an exception (TypeError), because + # of probably bugs in jedi. We can silence them. + # See: https://github.com/davidhalter/jedi/issues/492 + signatures = [] + else: + # Try to access the params attribute just once. For Jedi + # signatures containing the keyword-only argument star, + # this will crash when retrieving it the first time with + # AttributeError. Every following time it works. + # See: https://github.com/jonathanslenders/ptpython/issues/47 + # https://github.com/davidhalter/jedi/issues/598 + try: + if signatures: + signatures[0].params + except AttributeError: + pass + + return [Signature.from_jedi_signature(sig) for sig in signatures] + + +def get_signatures_using_eval( + document: Document, locals: Dict[str, Any], globals: Dict[str, Any] +) -> List[Signature]: + """ + Look for the signature of the function before the cursor position without + use of Jedi. This uses a similar approach as the `DictionaryCompleter` of + running `eval()` over the detected function name. + """ + # Look for open parenthesis, before cursor position. + text = document.text_before_cursor + pos = document.cursor_position - 1 + + paren_mapping = {")": "(", "}": "{", "]": "["} + paren_stack = [ + ")" + ] # Start stack with closing ')'. We are going to look for the matching open ')'. + comma_count = 0 # Number of comma's between start of function call and cursor pos. + found_start = False # Found something. + + while pos >= 0: + char = document.text[pos] + if char in ")]}": + paren_stack.append(char) + elif char in "([{": + if not paren_stack: + # Open paren, while no closing paren was found. Mouse cursor is + # positioned in nested parentheses. Not at the "top-level" of a + # function call. + break + if paren_mapping[paren_stack[-1]] != char: + # Unmatching parentheses: syntax error? + break + + paren_stack.pop() + + if len(paren_stack) == 0: + found_start = True + break + + elif char == "," and len(paren_stack) == 1: + comma_count += 1 + + pos -= 1 + + if not found_start: + return [] + + # We found the start of the function call. Now look for the object before + # this position on which we can do an 'eval' to retrieve the function + # object. + obj = DictionaryCompleter(lambda: globals, lambda: locals).eval_expression( + Document(document.text, cursor_position=pos), locals + ) + if obj is None: + return [] + + try: + name = obj.__name__ # type:ignore + except Exception: + name = obj.__class__.__name__ + + try: + signature = inspect.signature(obj) # type: ignore + except TypeError: + return [] # Not a callable object. + except ValueError: + return [] # No signature found, like for build-ins like "print". + + try: + doc = obj.__doc__ or "" + except: + doc = "" + + # TODO: `index` is not yet correct when dealing with keyword-only arguments. + return [Signature.from_inspect_signature(name, doc, signature, index=comma_count)] From 3a389b702e5b6d08016d1cd05987802c981aabe3 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Fri, 5 Feb 2021 17:24:11 +0100 Subject: [PATCH 118/220] Hide signature when sidebar is visible. --- ptpython/layout.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ptpython/layout.py b/ptpython/layout.py index b12010ce..7c3739ff 100644 --- a/ptpython/layout.py +++ b/ptpython/layout.py @@ -299,6 +299,8 @@ def get_text_fragments() -> StyleAndTextTuples: HasSignature(python_input) & # Signature needs to be shown. ShowSignature(python_input) & + # And no sidebar is visible. + ~ShowSidebar(python_input) & # Not done yet. ~is_done, ) From b0e761966237867ccfb392efaba3e0d41183bc85 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Wed, 10 Feb 2021 18:51:44 +0100 Subject: [PATCH 119/220] Release 3.0.14 --- CHANGELOG | 19 +++++++++++++++++++ setup.py | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 5b704461..abca054c 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,25 @@ CHANGELOG ========= +3.0.14: 2020-02-10 +------------------ + +New features: +- Display of signature and completion drop down together. +- If `DictionaryCompleter` is enabled, also retrieve signatures when Jedi + fails, using the same logic. +- List function parameters first and private attributes at the end in the + completion menu. +- Cleanup of the completion code. + +Fixes: +- Handle exceptions raised when `repr()` is called. +- Fix leakage of `exc_info` from eval to exec call. +- Fix handling of `KeyboardInterrupt` in REPL during evaluation of `__repr__`. +- Fix style for signature toolbar. +- Hide signature when sidebar is visible. + + 3.0.13: 2020-01-26 ------------------ diff --git a/setup.py b/setup.py index a803ef1a..d17bdb4d 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ setup( name="ptpython", author="Jonathan Slenders", - version="3.0.13", + version="3.0.14", url="https://github.com/prompt-toolkit/ptpython", description="Python REPL build on top of prompt_toolkit", long_description=long_description, From 869e4fa08a6b90d1d104b0729bf188cef834fe1e Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Thu, 11 Feb 2021 10:45:03 +0100 Subject: [PATCH 120/220] Fix AttributeError in when retrieving signatures that contain type annotations. --- ptpython/layout.py | 5 ++--- ptpython/signatures.py | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/ptpython/layout.py b/ptpython/layout.py index 7c3739ff..6482cbd0 100644 --- a/ptpython/layout.py +++ b/ptpython/layout.py @@ -265,15 +265,14 @@ def get_text_fragments() -> StyleAndTextTuples: append((Signature, "*")) append((Signature + ",operator", ", ")) - description = p.name sig_index = getattr(sig, "index", 0) if i == sig_index: # Note: we use `_Param.description` instead of # `_Param.name`, that way we also get the '*' before args. - append((Signature + ",current-name", description)) + append((Signature + ",current-name", p.description)) else: - append((Signature, str(description))) + append((Signature, p.description)) if p.default: # NOTE: For the jedi-based completion, the default is diff --git a/ptpython/signatures.py b/ptpython/signatures.py index 39cdba29..228b99b2 100644 --- a/ptpython/signatures.py +++ b/ptpython/signatures.py @@ -35,6 +35,18 @@ def __init__( def __repr__(self) -> str: return f"Parameter(name={self.name!r})" + @property + def description(self) -> str: + """ + Name + annotation. + """ + description = self.name + + if self.annotation is not None: + description += f": {self.annotation}" + + return description + class Signature: """ @@ -71,11 +83,27 @@ def from_inspect_signature( index: int, ) -> "Signature": parameters = [] + + def get_annotation_name(annotation: object) -> str: + """ + Get annotation as string from inspect signature. + """ + try: + # In case the annotation is a class like "int", "float", ... + return str(annotation.__name__) # type: ignore + except AttributeError: + pass # No attribute `__name__`, e.g., in case of `List[int]`. + + annotation = str(annotation) + if annotation.startswith("typing."): + annotation = annotation[len("typing:") :] + return annotation + for p in signature.parameters.values(): parameters.append( Parameter( name=p.name, - annotation=p.annotation.__name__, + annotation=get_annotation_name(p.annotation), default=repr(p.default) if p.default is not inspect.Parameter.empty else None, @@ -102,7 +130,7 @@ def from_jedi_signature(cls, signature) -> "Signature": parameters.append( Parameter( - name=p.to_string(), # p.name, + name=p.to_string(), # p.name, (`to_string()` already includes the annotation). annotation=None, # p.infer_annotation() default=None, # p.infer_default() kind=p.kind, From 1246deb1517a4e0d3d4fa599d87cd11b8f910a21 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Thu, 11 Feb 2021 12:06:19 +0100 Subject: [PATCH 121/220] Only delete 'word' instead of 'WORD' before cursor when control-w is pressed. (Stopping at any punctiation character is more logical when editing Python code.) --- ptpython/key_bindings.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ptpython/key_bindings.py b/ptpython/key_bindings.py index b01762e6..86317f90 100644 --- a/ptpython/key_bindings.py +++ b/ptpython/key_bindings.py @@ -10,6 +10,7 @@ vi_insert_mode, ) from prompt_toolkit.key_binding import KeyBindings +from prompt_toolkit.key_binding.bindings.named_commands import get_by_name from prompt_toolkit.keys import Keys from .utils import document_is_multiline_python @@ -61,6 +62,10 @@ def _(event): if python_input.enable_system_bindings: event.app.suspend_to_background() + # Delete word before cursor, but use all Python symbols as separators + # (WORD=False). + handle("c-w")(get_by_name("backward-kill-word")) + @handle("f2") def _(event): """ From 7f619ed7d5774c44193781b7f029fd64b5855475 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Thu, 11 Feb 2021 12:02:26 +0100 Subject: [PATCH 122/220] Several fixes to the completion code. - Give dictionary completions priority over path completions. - Always call non-fuzzy completer after fuzzy completer to prevent that some completions were missed out if the fuzzy completer doesn't find them. --- ptpython/completer.py | 45 +++++++++++++++++++++++----------------- ptpython/python_input.py | 19 ++++++++++++++--- setup.py | 5 ++--- 3 files changed, 44 insertions(+), 25 deletions(-) diff --git a/ptpython/completer.py b/ptpython/completer.py index 68774be4..9f7e10bc 100644 --- a/ptpython/completer.py +++ b/ptpython/completer.py @@ -42,13 +42,16 @@ class PythonCompleter(Completer): """ def __init__( - self, get_globals, get_locals, get_enable_dictionary_completion + self, + get_globals: Callable[[], dict], + get_locals: Callable[[], dict], + enable_dictionary_completion: Callable[[], bool], ) -> None: super().__init__() self.get_globals = get_globals self.get_locals = get_locals - self.get_enable_dictionary_completion = get_enable_dictionary_completion + self.enable_dictionary_completion = enable_dictionary_completion self._system_completer = SystemCompleter() self._jedi_completer = JediCompleter(get_globals, get_locals) @@ -134,10 +137,10 @@ def _complete_python_while_typing(self, document: Document) -> bool: When `complete_while_typing` is set, only return completions when this returns `True`. """ - text = document.text_before_cursor.rstrip() + text = document.text_before_cursor # .rstrip() char_before_cursor = text[-1:] return bool( - text and (char_before_cursor.isalnum() or char_before_cursor in "_.(,") + text and (char_before_cursor.isalnum() or char_before_cursor in "_.([,") ) def get_completions( @@ -156,22 +159,11 @@ def get_completions( ) return - # Do Path completions (if there were no dictionary completions). - # TODO: not if we have dictionary completions... - if complete_event.completion_requested or self._complete_path_while_typing( - document - ): - yield from self._path_completer.get_completions(document, complete_event) - + # Do dictionary key completions. if complete_event.completion_requested or self._complete_python_while_typing( document ): - # If we are inside a string, Don't do Python completion. - if self._path_completer_grammar.match(document.text_before_cursor): - return - - # Do dictionary key completions. - if self.get_enable_dictionary_completion(): + if self.enable_dictionary_completion(): has_dict_completions = False for c in self._dictionary_completer.get_completions( document, complete_event @@ -184,8 +176,23 @@ def get_completions( if has_dict_completions: return - # Do Jedi Python completions. - yield from self._jedi_completer.get_completions(document, complete_event) + # Do Path completions (if there were no dictionary completions). + if complete_event.completion_requested or self._complete_path_while_typing( + document + ): + yield from self._path_completer.get_completions(document, complete_event) + + # Do Jedi completions. + if complete_event.completion_requested or self._complete_python_while_typing( + document + ): + # If we are inside a string, Don't do Jedi completion. + if not self._path_completer_grammar.match(document.text_before_cursor): + + # Do Jedi Python completions. + yield from self._jedi_completer.get_completions( + document, complete_event + ) class JediCompleter(Completer): diff --git a/ptpython/python_input.py b/ptpython/python_input.py index 8d5da502..e63cdf1d 100644 --- a/ptpython/python_input.py +++ b/ptpython/python_input.py @@ -18,9 +18,11 @@ from prompt_toolkit.buffer import Buffer from prompt_toolkit.completion import ( Completer, + ConditionalCompleter, DynamicCompleter, FuzzyCompleter, ThreadedCompleter, + merge_completers, ) from prompt_toolkit.document import Document from prompt_toolkit.enums import DEFAULT_BUFFER, EditingMode @@ -205,9 +207,20 @@ def __init__( ) self._completer = HidePrivateCompleter( - FuzzyCompleter( - DynamicCompleter(lambda: self.completer), - enable_fuzzy=Condition(lambda: self.enable_fuzzy_completion), + # If fuzzy is enabled, first do fuzzy completion, but always add + # the non-fuzzy completions, if somehow the fuzzy completer didn't + # find them. (Due to the way the cursor position is moved in the + # fuzzy completer, some completions will not always be found by the + # fuzzy completer, but will be found with the normal completer.) + merge_completers( + [ + ConditionalCompleter( + FuzzyCompleter(DynamicCompleter(lambda: self.completer)), + Condition(lambda: self.enable_fuzzy_completion), + ), + DynamicCompleter(lambda: self.completer), + ], + deduplicate=True, ), lambda: self.complete_private_attributes, ) diff --git a/setup.py b/setup.py index d17bdb4d..57fe2030 100644 --- a/setup.py +++ b/setup.py @@ -20,9 +20,8 @@ "appdirs", "importlib_metadata;python_version<'3.8'", "jedi>=0.16.0", - # Use prompt_toolkit 3.0.12, because of dont_extend_width bugfix when - # signature and completion dropdown are displayed together. - "prompt_toolkit>=3.0.12,<3.1.0", + # Use prompt_toolkit 3.0.16, because of the `DeduplicateCompleter`. + "prompt_toolkit>=3.0.16,<3.1.0", "pygments", "black", ], From 02a7b835623cb6f8c0aa65584fbe786fff2ed10a Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Thu, 11 Feb 2021 12:30:54 +0100 Subject: [PATCH 123/220] Release 3.0.15 --- CHANGELOG | 10 ++++++++++ setup.py | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index abca054c..6d48ade2 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,16 @@ CHANGELOG ========= +3.0.15: 2020-02-11 +------------------ + +New features: +- When pressing control-w, only delete characters until a punctuation. + +Fixes: +- Fix `AttributeError` during retrieval of signatures with type annotations. + + 3.0.14: 2020-02-10 ------------------ diff --git a/setup.py b/setup.py index 57fe2030..b4a4b683 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ setup( name="ptpython", author="Jonathan Slenders", - version="3.0.14", + version="3.0.15", url="https://github.com/prompt-toolkit/ptpython", description="Python REPL build on top of prompt_toolkit", long_description=long_description, From caff15a461b64dee36c22608d36170830cafd5f3 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Thu, 11 Feb 2021 12:34:06 +0100 Subject: [PATCH 124/220] Release 3.0.16 --- CHANGELOG | 12 ++++++++++++ setup.py | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 6d48ade2..67ac0a85 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,18 @@ CHANGELOG ========= +3.0.16: 2020-02-11 +------------------ + +(Commit 7f619e was missing in previous release.) + +Fixes: +- Several fixes to the completion code: + * Give dictionary completions priority over path completions. + * Always call non-fuzzy completer after fuzzy completer to prevent that some + completions were missed out if the fuzzy completer doesn't find them. + + 3.0.15: 2020-02-11 ------------------ diff --git a/setup.py b/setup.py index b4a4b683..dbbe55b9 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ setup( name="ptpython", author="Jonathan Slenders", - version="3.0.15", + version="3.0.16", url="https://github.com/prompt-toolkit/ptpython", description="Python REPL build on top of prompt_toolkit", long_description=long_description, From b222d03f3c83f106003f23527dc55f0eaf514776 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Fri, 19 Mar 2021 12:48:24 +0100 Subject: [PATCH 125/220] Fix leaking file descriptors. --- ptpython/python_input.py | 60 ++++++++++++++-------------------------- ptpython/repl.py | 10 +------ setup.py | 4 +-- 3 files changed, 24 insertions(+), 50 deletions(-) diff --git a/ptpython/python_input.py b/ptpython/python_input.py index e63cdf1d..d5f03738 100644 --- a/ptpython/python_input.py +++ b/ptpython/python_input.py @@ -1012,43 +1012,25 @@ def pre_run( self.app.vi_state.input_mode = InputMode.NAVIGATION # Run the UI. - result: str = "" - exception: Optional[BaseException] = None - - def in_thread() -> None: - nonlocal result, exception + while True: try: - while True: - try: - result = self.app.run(pre_run=pre_run) - - if result.lstrip().startswith("\x1a"): - # When the input starts with Ctrl-Z, quit the REPL. - # (Important for Windows users.) - raise EOFError - - # Remove leading whitespace. - # (Users can add extra indentation, which happens for - # instance because of copy/pasting code.) - result = unindent_code(result) - - if result and not result.isspace(): - return - except KeyboardInterrupt: - # Abort - try again. - self.default_buffer.document = Document() - except BaseException as e: - exception = e - return - - finally: - if self.insert_blank_line_after_input: - self.app.output.write("\n") - - thread = threading.Thread(target=in_thread) - thread.start() - thread.join() - - if exception is not None: - raise exception - return result + result = self.app.run(pre_run=pre_run, in_thread=True) + + if result.lstrip().startswith("\x1a"): + # When the input starts with Ctrl-Z, quit the REPL. + # (Important for Windows users.) + raise EOFError + + # Remove leading whitespace. + # (Users can add extra indentation, which happens for + # instance because of copy/pasting code.) + result = unindent_code(result) + + if result and not result.isspace(): + if self.insert_blank_line_after_input: + self.app.output.write("\n") + + return result + except KeyboardInterrupt: + # Abort - try again. + self.default_buffer.document = Document() diff --git a/ptpython/repl.py b/ptpython/repl.py index ae7b1d0d..f960f6f6 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -421,15 +421,7 @@ def show_pager() -> None: # Run pager prompt in another thread. # Same as for the input. This prevents issues with nested event # loops. - pager_result = None - - def in_thread() -> None: - nonlocal pager_result - pager_result = pager_prompt.prompt() - - th = threading.Thread(target=in_thread) - th.start() - th.join() + pager_result = pager_prompt.prompt(in_thread=True) if pager_result == PagerResult.ABORT: print("...") diff --git a/setup.py b/setup.py index dbbe55b9..c5f40f35 100644 --- a/setup.py +++ b/setup.py @@ -20,8 +20,8 @@ "appdirs", "importlib_metadata;python_version<'3.8'", "jedi>=0.16.0", - # Use prompt_toolkit 3.0.16, because of the `DeduplicateCompleter`. - "prompt_toolkit>=3.0.16,<3.1.0", + # Use prompt_toolkit 3.0.18, because of the `in_thread` option. + "prompt_toolkit>=3.0.18,<3.1.0", "pygments", "black", ], From 7d116e84909d13832935ab4132eabfe316d26b74 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Mon, 22 Mar 2021 15:43:47 +0100 Subject: [PATCH 126/220] Fix race condition during retrieval of signatures. `_on_input_timeout` was called recursively from within another thread, while it was not thread safe. --- ptpython/python_input.py | 64 +++++++++++++++++++++++----------------- ptpython/repl.py | 5 ++-- 2 files changed, 39 insertions(+), 30 deletions(-) diff --git a/ptpython/python_input.py b/ptpython/python_input.py index d5f03738..2f6a5b9f 100644 --- a/ptpython/python_input.py +++ b/ptpython/python_input.py @@ -4,7 +4,6 @@ """ import __future__ -import threading from asyncio import get_event_loop from functools import partial from typing import TYPE_CHECKING, Any, Callable, Dict, Generic, List, Optional, TypeVar @@ -914,23 +913,13 @@ def vi_mode(self, value: bool) -> None: else: self.editing_mode = EditingMode.EMACS - def _on_input_timeout(self, buff: Buffer, loop=None) -> None: + def _on_input_timeout(self, buff: Buffer) -> None: """ When there is no input activity, in another thread, get the signature of the current code. """ - app = self.app - - # Never run multiple get-signature threads. - if self._get_signatures_thread_running: - return - self._get_signatures_thread_running = True - - document = buff.document - - loop = loop or get_event_loop() - def run(): + def get_signatures_in_executor(document: Document) -> List[Signature]: # First, get signatures from Jedi. If we didn't found any and if # "dictionary completion" (eval-based completion) is enabled, then # get signatures using eval. @@ -942,26 +931,47 @@ def run(): document, self.get_locals(), self.get_globals() ) - self._get_signatures_thread_running = False + return signatures + + app = self.app + + async def on_timeout_task() -> None: + loop = get_event_loop() - # Set signatures and redraw if the text didn't change in the - # meantime. Otherwise request new signatures. - if buff.text == document.text: - self.signatures = signatures + # Never run multiple get-signature threads. + if self._get_signatures_thread_running: + return + self._get_signatures_thread_running = True - # Set docstring in docstring buffer. - if signatures: - self.docstring_buffer.reset( - document=Document(signatures[0].docstring, cursor_position=0) + try: + while True: + document = buff.document + signatures = await loop.run_in_executor( + None, get_signatures_in_executor, document ) - else: - self.docstring_buffer.reset() - app.invalidate() + # If the text didn't change in the meantime, take these + # signatures. Otherwise, try again. + if buff.text == document.text: + break + finally: + self._get_signatures_thread_running = False + + # Set signatures and redraw. + self.signatures = signatures + + # Set docstring in docstring buffer. + if signatures: + self.docstring_buffer.reset( + document=Document(signatures[0].docstring, cursor_position=0) + ) else: - self._on_input_timeout(buff, loop=loop) + self.docstring_buffer.reset() + + app.invalidate() - loop.run_in_executor(None, run) + if app.is_running: + app.create_background_task(on_timeout_task()) def on_reset(self) -> None: self.signatures = [] diff --git a/ptpython/repl.py b/ptpython/repl.py index f960f6f6..af73cb69 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -11,7 +11,6 @@ import builtins import os import sys -import threading import traceback import types import warnings @@ -112,7 +111,7 @@ def run(self) -> None: # Eval. try: result = self.eval(text) - except KeyboardInterrupt as e: # KeyboardInterrupt doesn't inherit from Exception. + except KeyboardInterrupt: # KeyboardInterrupt doesn't inherit from Exception. raise except SystemExit: return @@ -171,7 +170,7 @@ async def run_async(self) -> None: # Eval. try: result = await self.eval_async(text) - except KeyboardInterrupt as e: # KeyboardInterrupt doesn't inherit from Exception. + except KeyboardInterrupt: # KeyboardInterrupt doesn't inherit from Exception. raise except SystemExit: return From 321d9e177d42de782790740927a4e7a45c3cb7b3 Mon Sep 17 00:00:00 2001 From: AnthonyDiGirolamo Date: Sun, 18 Apr 2021 19:14:49 -0700 Subject: [PATCH 127/220] Support using ptpython as a library. This change allows another fullscreen prompt_toolkit application to create it's own ptpython based repl embedded in a window. It separates the print and format output parts and run functionality into se - Create _format_result_output and _format_exception_output functions to separate the format result from the print to stdout. - Move the eval parts of run() and run_async() into their own functions: run_and_show_expression() and run_and_show_expression_async(). --- ptpython/layout.py | 4 +- ptpython/python_input.py | 27 ++++++-- ptpython/repl.py | 137 +++++++++++++++++++++++---------------- 3 files changed, 105 insertions(+), 63 deletions(-) diff --git a/ptpython/layout.py b/ptpython/layout.py index 6482cbd0..e7b3f554 100644 --- a/ptpython/layout.py +++ b/ptpython/layout.py @@ -646,7 +646,7 @@ def menu_position(): sidebar = python_sidebar(python_input) self.exit_confirmation = create_exit_confirmation(python_input) - root_container = HSplit( + self.root_container = HSplit( [ VSplit( [ @@ -759,5 +759,5 @@ def menu_position(): ] ) - self.layout = Layout(root_container) + self.layout = Layout(self.root_container) self.sidebar = sidebar diff --git a/ptpython/python_input.py b/ptpython/python_input.py index 2f6a5b9f..fce0242b 100644 --- a/ptpython/python_input.py +++ b/ptpython/python_input.py @@ -173,6 +173,11 @@ class PythonInput: python_input = PythonInput(...) python_code = python_input.app.run() + + :param create_app: When `False`, don't create and manage a prompt_toolkit + application. The default is `True` and should only be set + to false if PythonInput is being embedded in a separate + prompt_toolkit application. """ def __init__( @@ -187,6 +192,7 @@ def __init__( output: Optional[Output] = None, # For internal use. extra_key_bindings: Optional[KeyBindings] = None, + create_app = True, _completer: Optional[Completer] = None, _validator: Optional[Validator] = None, _lexer: Optional[Lexer] = None, @@ -379,10 +385,17 @@ def __init__( extra_toolbars=self._extra_toolbars, ) - self.app = self._create_application(input, output) + # Create an app if requested. If not, the global get_app() is returned + # for self.app via property getter. + if create_app: + self._app = self._create_application(input, output) + # Setting vi_mode will not work unless the prompt_toolkit + # application has been created. + if vi_mode: + self.app.editing_mode = EditingMode.VI + else: + self._app = None - if vi_mode: - self.app.editing_mode = EditingMode.VI def _accept_handler(self, buff: Buffer) -> bool: app = get_app() @@ -913,6 +926,12 @@ def vi_mode(self, value: bool) -> None: else: self.editing_mode = EditingMode.EMACS + @property + def app(self) -> Application: + if self._app is None: + return get_app() + return self._app + def _on_input_timeout(self, buff: Buffer) -> None: """ When there is no input activity, @@ -980,7 +999,7 @@ def enter_history(self) -> None: """ Display the history. """ - app = get_app() + app = self.app app.vi_state.input_mode = InputMode.NAVIGATION history = PythonHistory(self, self.default_buffer.document) diff --git a/ptpython/repl.py b/ptpython/repl.py index af73cb69..7d05e710 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -90,6 +90,35 @@ def _load_start_paths(self) -> None: output = self.app.output output.write("WARNING | File not found: {}\n\n".format(path)) + def run_and_show_expression(self, expression): + try: + # Eval. + try: + result = self.eval(expression) + except KeyboardInterrupt: # 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) + def run(self) -> None: """ Run the REPL loop. @@ -101,44 +130,43 @@ def run(self) -> None: try: while True: + # Pull text from the user. try: - # Read. - try: - text = self.read() - except EOFError: - return - - # Eval. - try: - result = self.eval(text) - except KeyboardInterrupt: # 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 = [] + text = self.read() + except EOFError: + return - 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) + # 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() + async def run_and_show_expression_async(self, text): + loop = asyncio.get_event_loop() + + try: + result = await self.eval_async(text) + except KeyboardInterrupt: # 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 = [] + # Return the result for future consumers. + return result + async def run_async(self) -> None: """ Run the REPL loop, but run the blocking parts in an executor, so that @@ -168,24 +196,7 @@ async def run_async(self) -> None: return # Eval. - try: - result = await self.eval_async(text) - except KeyboardInterrupt: # 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 = [] + await self.run_and_show_expression_async(text) except KeyboardInterrupt as e: # XXX: This does not yet work properly. In some situations, @@ -285,9 +296,9 @@ def _compile_with_flags(self, code: str, mode: str): dont_inherit=True, ) - def show_result(self, result: object) -> None: + def _format_result_output(self, result: object) -> AnyFormattedText: """ - Show __repr__ for an `eval` result. + Format __repr__ for an `eval` result. Note: this can raise `KeyboardInterrupt` if either calling `__repr__`, `__pt_repr__` or formatting the output with "Black" takes to long @@ -303,7 +314,7 @@ def show_result(self, result: object) -> None: except BaseException as e: # Calling repr failed. self._handle_exception(e) - return + return None try: compile(result_repr, "", "eval") @@ -362,10 +373,18 @@ def show_result(self, result: object) -> None: out_prompt + [("", fragment_list_to_text(formatted_result_repr))] ) + return to_formatted_text(formatted_output) + + def show_result(self, result: object) -> None: + """ + Show __repr__ for an `eval` result and print to ouptut. + """ + formatted_text_output = self._format_result_output(result) + if self.enable_pager: - self.print_paginated_formatted_text(to_formatted_text(formatted_output)) + self.print_paginated_formatted_text(formatted_text_output) else: - self.print_formatted_text(to_formatted_text(formatted_output)) + self.print_formatted_text(formatted_text_output) self.app.output.flush() @@ -485,9 +504,7 @@ def create_pager_prompt(self) -> PromptSession["PagerResult"]: """ return create_pager_prompt(self._current_style, self.title) - def _handle_exception(self, e: BaseException) -> None: - output = self.app.output - + def _format_exception_output(self, e: BaseException) -> AnyFormattedText: # Instead of just calling ``traceback.format_exc``, we take the # traceback and skip the bottom calls of this framework. t, v, tb = sys.exc_info() @@ -516,6 +533,12 @@ def _handle_exception(self, e: BaseException) -> None: tokens = list(_lex_python_traceback(tb_str)) else: tokens = [(Token, tb_str)] + return tokens + + def _handle_exception(self, e: BaseException) -> None: + output = self.app.output + + tokens = self._format_exception_output(e) print_formatted_text( PygmentsTokens(tokens), From b436e79c09467098712dbc4da4d260317a13620e Mon Sep 17 00:00:00 2001 From: Roee Nizan Date: Thu, 11 Mar 2021 20:25:18 +0200 Subject: [PATCH 128/220] Feature: read ipython config files --- ptpython/ipython.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/ptpython/ipython.py b/ptpython/ipython.py index 2e8d1195..91633340 100644 --- a/ptpython/ipython.py +++ b/ptpython/ipython.py @@ -282,4 +282,21 @@ def embed(**kwargs): kwargs["config"] = config shell = InteractiveShellEmbed.instance(**kwargs) initialize_extensions(shell, config["InteractiveShellApp"]["extensions"]) + run_startup_scripts(shell) shell(header=header, stack_depth=2, compile_flags=compile_flags) + + +def run_startup_scripts(shell): + """ + Contributed by linyuxu: + https://github.com/prompt-toolkit/ptpython/issues/126#issue-161242480 + """ + import glob + import os + + startup_dir = shell.profile_dir.startup_dir + startup_files = [] + startup_files += glob.glob(os.path.join(startup_dir, "*.py")) + startup_files += glob.glob(os.path.join(startup_dir, "*.ipy")) + for file in startup_files: + shell.run_cell(open(file).read()) From 588f9d368193f1ec8e448e541c3f091b62230e8e Mon Sep 17 00:00:00 2001 From: Curiosity <53520949+sisrfeng@users.noreply.github.com> Date: Thu, 28 Jan 2021 15:43:03 +0800 Subject: [PATCH 129/220] Update repl.py use ~/.config/ptpython/config.py instead of ~/.ptpython/config.py --- ptpython/repl.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ptpython/repl.py b/ptpython/repl.py index 7d05e710..2c186515 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -604,7 +604,7 @@ def enable_deprecation_warnings() -> None: warnings.filterwarnings("default", category=DeprecationWarning, module="__main__") -def run_config(repl: PythonInput, config_file: str = "~/.ptpython/config.py") -> None: +def run_config(repl: PythonInput, config_file: str = "~/.config/ptpython/config.py") -> None: """ Execute REPL config file. From 8d1ee2163a6b5e32d065a2289c5eceb0e515326b Mon Sep 17 00:00:00 2001 From: jhylands Date: Wed, 21 Apr 2021 16:58:38 +0100 Subject: [PATCH 130/220] Fixed config example, vi jj remap --- examples/ptpython_config/config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/ptpython_config/config.py b/examples/ptpython_config/config.py index 8532f938..24275728 100644 --- a/examples/ptpython_config/config.py +++ b/examples/ptpython_config/config.py @@ -157,7 +157,7 @@ def _(event): @repl.add_key_binding("j", "j", filter=ViInsertMode()) def _(event): " Map 'jj' to Escape. " - event.cli.key_processor.feed(KeyPress("escape")) + event.cli.key_processor.feed(KeyPress(Keys("escape"))) """ # Custom key binding for some simple autocorrection while typing. From 8f36d931e2ea29d58ad69981abb7f0c04d840bbf Mon Sep 17 00:00:00 2001 From: Andrew Zhou <0az@afzhou.com> Date: Wed, 3 Mar 2021 14:07:00 -0600 Subject: [PATCH 131/220] Fix incorrect __main__ on script execution (#444) --- ptpython/entry_points/run_ptipython.py | 2 +- ptpython/entry_points/run_ptpython.py | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/ptpython/entry_points/run_ptipython.py b/ptpython/entry_points/run_ptipython.py index 650633ec..1a489d3f 100644 --- a/ptpython/entry_points/run_ptipython.py +++ b/ptpython/entry_points/run_ptipython.py @@ -31,7 +31,7 @@ def run(user_ns=None): path = a.args[0] with open(path, "rb") as f: code = compile(f.read(), path, "exec") - exec(code, {}) + exec(code, {'__name__': '__main__', '__file__': path}) else: enable_deprecation_warnings() diff --git a/ptpython/entry_points/run_ptpython.py b/ptpython/entry_points/run_ptpython.py index 0b3dbdb9..84a9aee1 100644 --- a/ptpython/entry_points/run_ptpython.py +++ b/ptpython/entry_points/run_ptpython.py @@ -179,9 +179,11 @@ def run() -> None: path = a.args[0] with open(path, "rb") as f: code = compile(f.read(), path, "exec") - # NOTE: We have to pass an empty dictionary as namespace. Omitting - # this argument causes imports to not be found. See issue #326. - exec(code, {}) + # NOTE: We have to pass a dict as namespace. Omitting this argument + # causes imports to not be found. See issue #326. + # However, an empty dict sets __name__ to 'builtins', which + # breaks `if __name__ == '__main__'` checks. See issue #444. + exec(code, {'__name__': '__main__', '__file__': path}) # Run interactive shell. else: From b85716354e34ce32424e3aa17f6afd342d88e513 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Mon, 22 Mar 2021 16:08:40 +0100 Subject: [PATCH 132/220] Release 3.0.17 --- CHANGELOG | 9 +++++++++ setup.py | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 67ac0a85..8f946b4b 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,15 @@ CHANGELOG ========= +3.0.17: 2020-03-22 +------------------ + +Fixes: +- Fix leaking file descriptors due to not closing the asyncio event loop after + reading input in a thread. +- Fix race condition during retrieval of signatures. + + 3.0.16: 2020-02-11 ------------------ diff --git a/setup.py b/setup.py index c5f40f35..3f735073 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ setup( name="ptpython", author="Jonathan Slenders", - version="3.0.16", + version="3.0.17", url="https://github.com/prompt-toolkit/ptpython", description="Python REPL build on top of prompt_toolkit", long_description=long_description, From 513b9f41120f49aad75dbac00ef01ce17746b07c Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Mon, 24 May 2021 14:18:32 +0200 Subject: [PATCH 133/220] Code formatting (latest black). --- ptpython/completer.py | 4 ++-- ptpython/entry_points/run_ptipython.py | 2 +- ptpython/entry_points/run_ptpython.py | 2 +- ptpython/history_browser.py | 22 +++++++++++----------- ptpython/key_bindings.py | 12 ++++++------ ptpython/layout.py | 8 ++++---- ptpython/prompt_style.py | 4 ++-- ptpython/python_input.py | 9 ++++----- ptpython/repl.py | 18 +++++++++--------- 9 files changed, 40 insertions(+), 41 deletions(-) diff --git a/ptpython/completer.py b/ptpython/completer.py index 9f7e10bc..285398c2 100644 --- a/ptpython/completer.py +++ b/ptpython/completer.py @@ -468,7 +468,7 @@ def _get_item_lookup_completions( """ def abbr_meta(text: str) -> str: - " Abbreviate meta text, make sure it fits on one line. " + "Abbreviate meta text, make sure it fits on one line." # Take first line, if multiple lines. if len(text) > 20: text = text[:20] + "..." @@ -621,7 +621,7 @@ def is_private(completion: Completion) -> bool: class ReprFailedError(Exception): - " Raised when the repr() call in `DictionaryCompleter` fails. " + "Raised when the repr() call in `DictionaryCompleter` fails." try: diff --git a/ptpython/entry_points/run_ptipython.py b/ptpython/entry_points/run_ptipython.py index 1a489d3f..21d70637 100644 --- a/ptpython/entry_points/run_ptipython.py +++ b/ptpython/entry_points/run_ptipython.py @@ -31,7 +31,7 @@ def run(user_ns=None): path = a.args[0] with open(path, "rb") as f: code = compile(f.read(), path, "exec") - exec(code, {'__name__': '__main__', '__file__': path}) + exec(code, {"__name__": "__main__", "__file__": path}) else: enable_deprecation_warnings() diff --git a/ptpython/entry_points/run_ptpython.py b/ptpython/entry_points/run_ptpython.py index 84a9aee1..5ebe2b95 100644 --- a/ptpython/entry_points/run_ptpython.py +++ b/ptpython/entry_points/run_ptpython.py @@ -183,7 +183,7 @@ def run() -> None: # causes imports to not be found. See issue #326. # However, an empty dict sets __name__ to 'builtins', which # breaks `if __name__ == '__main__'` checks. See issue #444. - exec(code, {'__name__': '__main__', '__file__': path}) + exec(code, {"__name__": "__main__", "__file__": path}) # Run interactive shell. else: diff --git a/ptpython/history_browser.py b/ptpython/history_browser.py index 798a280f..b7fe0865 100644 --- a/ptpython/history_browser.py +++ b/ptpython/history_browser.py @@ -85,7 +85,7 @@ class BORDER: - " Box drawing characters. " + "Box drawing characters." HORIZONTAL = "\u2501" VERTICAL = "\u2503" TOP_LEFT = "\u250f" @@ -420,7 +420,7 @@ def update_default_buffer(self): def _toggle_help(history): - " Display/hide help. " + "Display/hide help." help_buffer_control = history.history_layout.help_buffer_control if history.app.layout.current_control == help_buffer_control: @@ -430,7 +430,7 @@ def _toggle_help(history): def _select_other_window(history): - " Toggle focus between left/right window. " + "Toggle focus between left/right window." current_buffer = history.app.current_buffer layout = history.history_layout.layout @@ -513,17 +513,17 @@ def _(event): # Eager: ignore the Emacs [Ctrl-X Ctrl-X] binding. @handle("c-w", filter=main_buffer_focussed) def _(event): - " Select other window. " + "Select other window." _select_other_window(history) @handle("f4") def _(event): - " Switch between Emacs/Vi mode. " + "Switch between Emacs/Vi mode." python_input.vi_mode = not python_input.vi_mode @handle("f1") def _(event): - " Display/hide help. " + "Display/hide help." _toggle_help(history) @handle("enter", filter=help_focussed) @@ -531,7 +531,7 @@ def _(event): @handle("c-g", filter=help_focussed) @handle("escape", filter=help_focussed) def _(event): - " Leave help. " + "Leave help." event.app.layout.focus_previous() @handle("q", filter=main_buffer_focussed) @@ -539,19 +539,19 @@ def _(event): @handle("c-c", filter=main_buffer_focussed) @handle("c-g", filter=main_buffer_focussed) def _(event): - " Cancel and go back. " + "Cancel and go back." event.app.exit(result=None) @handle("enter", filter=main_buffer_focussed) def _(event): - " Accept input. " + "Accept input." event.app.exit(result=history.default_buffer.text) enable_system_bindings = Condition(lambda: python_input.enable_system_bindings) @handle("c-z", filter=enable_system_bindings) def _(event): - " Suspend to background. " + "Suspend to background." event.app.suspend_to_background() return bindings @@ -630,7 +630,7 @@ def _default_buffer_pos_changed(self, _): ) def _history_buffer_pos_changed(self, _): - """ When the cursor changes in the history buffer. Synchronize. """ + """When the cursor changes in the history buffer. Synchronize.""" # Only when this buffer has the focus. if self.app.current_buffer == self.history_buffer: line_no = self.history_buffer.document.cursor_position_row diff --git a/ptpython/key_bindings.py b/ptpython/key_bindings.py index 86317f90..ae23a3df 100644 --- a/ptpython/key_bindings.py +++ b/ptpython/key_bindings.py @@ -203,7 +203,7 @@ def _(event): @handle("c-c", filter=has_focus(python_input.default_buffer)) def _(event): - " Abort when Control-C has been pressed. " + "Abort when Control-C has been pressed." event.app.exit(exception=KeyboardInterrupt, style="class:aborting") return bindings @@ -222,7 +222,7 @@ def load_sidebar_bindings(python_input): @handle("c-p", filter=sidebar_visible) @handle("k", filter=sidebar_visible) def _(event): - " Go to previous option. " + "Go to previous option." python_input.selected_option_index = ( python_input.selected_option_index - 1 ) % python_input.option_count @@ -231,7 +231,7 @@ def _(event): @handle("c-n", filter=sidebar_visible) @handle("j", filter=sidebar_visible) def _(event): - " Go to next option. " + "Go to next option." python_input.selected_option_index = ( python_input.selected_option_index + 1 ) % python_input.option_count @@ -240,14 +240,14 @@ def _(event): @handle("l", filter=sidebar_visible) @handle(" ", filter=sidebar_visible) def _(event): - " Select next value for current option. " + "Select next value for current option." option = python_input.selected_option option.activate_next() @handle("left", filter=sidebar_visible) @handle("h", filter=sidebar_visible) def _(event): - " Select previous value for current option. " + "Select previous value for current option." option = python_input.selected_option option.activate_previous() @@ -257,7 +257,7 @@ def _(event): @handle("enter", filter=sidebar_visible) @handle("escape", filter=sidebar_visible) def _(event): - " Hide sidebar. " + "Hide sidebar." python_input.show_sidebar = False event.app.layout.focus_last() diff --git a/ptpython/layout.py b/ptpython/layout.py index e7b3f554..dc6b19bb 100644 --- a/ptpython/layout.py +++ b/ptpython/layout.py @@ -64,7 +64,7 @@ class CompletionVisualisation(Enum): - " Visualisation method for the completions. " + "Visualisation method for the completions." NONE = "none" POP_UP = "pop-up" MULTI_COLUMN = "multi-column" @@ -116,7 +116,7 @@ def select_item(mouse_event: MouseEvent) -> None: @if_mousedown def goto_next(mouse_event: MouseEvent) -> None: - " Select item and go to next value. " + "Select item and go to next value." python_input.selected_option_index = index option = python_input.selected_option option.activate_next() @@ -472,7 +472,7 @@ def show_sidebar_button_info(python_input: "PythonInput") -> Container: @if_mousedown def toggle_sidebar(mouse_event: MouseEvent) -> None: - " Click handler for the menu. " + "Click handler for the menu." python_input.show_sidebar = not python_input.show_sidebar version = sys.version_info @@ -544,7 +544,7 @@ def get_text_fragments() -> StyleAndTextTuples: @Condition def extra_condition() -> bool: - " Only show when... " + "Only show when..." b = python_input.default_buffer return ( diff --git a/ptpython/prompt_style.py b/ptpython/prompt_style.py index 24e5f883..e7334af2 100644 --- a/ptpython/prompt_style.py +++ b/ptpython/prompt_style.py @@ -16,7 +16,7 @@ class PromptStyle(metaclass=ABCMeta): @abstractmethod def in_prompt(self) -> AnyFormattedText: - " Return the input tokens. " + "Return the input tokens." return [] @abstractmethod @@ -31,7 +31,7 @@ def in2_prompt(self, width: int) -> AnyFormattedText: @abstractmethod def out_prompt(self) -> AnyFormattedText: - " Return the output tokens. " + "Return the output tokens." return [] diff --git a/ptpython/python_input.py b/ptpython/python_input.py index fce0242b..2b75d6e5 100644 --- a/ptpython/python_input.py +++ b/ptpython/python_input.py @@ -192,7 +192,7 @@ def __init__( output: Optional[Output] = None, # For internal use. extra_key_bindings: Optional[KeyBindings] = None, - create_app = True, + create_app=True, _completer: Optional[Completer] = None, _validator: Optional[Validator] = None, _lexer: Optional[Lexer] = None, @@ -396,7 +396,6 @@ def __init__( else: self._app = None - def _accept_handler(self, buff: Buffer) -> bool: app = get_app() app.exit(result=buff.text) @@ -405,12 +404,12 @@ def _accept_handler(self, buff: Buffer) -> bool: @property def option_count(self) -> int: - " Return the total amount of options. (In all categories together.) " + "Return the total amount of options. (In all categories together.)" return sum(len(category.options) for category in self.options) @property def selected_option(self) -> Option: - " Return the currently selected option. " + "Return the currently selected option." i = 0 for category in self.options: for o in category.options: @@ -533,7 +532,7 @@ def disable(attribute: str) -> bool: def simple_option( title: str, description: str, field_name: str, values: Optional[List] = None ) -> Option: - " Create Simple on/of option. " + "Create Simple on/of option." values = values or ["off", "on"] def get_current_value(): diff --git a/ptpython/repl.py b/ptpython/repl.py index 2c186515..c0026fbf 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -79,7 +79,7 @@ def __init__(self, *a, **kw) -> None: self._load_start_paths() def _load_start_paths(self) -> None: - " Start the Read-Eval-Print Loop. " + "Start the Read-Eval-Print Loop." if self._startup_paths: for path in self._startup_paths: if os.path.exists(path): @@ -157,9 +157,7 @@ async def run_and_show_expression_async(self, text): else: # Print. if result is not None: - await loop.run_in_executor( - None, lambda: self.show_result(result) - ) + await loop.run_in_executor(None, lambda: self.show_result(result)) # Loop. self.current_statement_index += 1 @@ -287,7 +285,7 @@ def get_compiler_flags(self) -> int: return super().get_compiler_flags() | PyCF_ALLOW_TOP_LEVEL_AWAIT def _compile_with_flags(self, code: str, mode: str): - " Compile code with the right compiler flags. " + "Compile code with the right compiler flags." return compile( code, "", @@ -578,13 +576,13 @@ def _remove_from_namespace(self) -> None: def _lex_python_traceback(tb): - " Return token list for traceback string. " + "Return token list for traceback string." lexer = PythonTracebackLexer() return lexer.get_tokens(tb) def _lex_python_result(tb): - " Return token list for Python string. " + "Return token list for Python string." lexer = PythonLexer() # Use `get_tokens_unprocessed`, so that we get exactly the same string, # without line endings appended. `print_formatted_text` already appends a @@ -604,7 +602,9 @@ def enable_deprecation_warnings() -> None: warnings.filterwarnings("default", category=DeprecationWarning, module="__main__") -def run_config(repl: PythonInput, config_file: str = "~/.config/ptpython/config.py") -> None: +def run_config( + repl: PythonInput, config_file: str = "~/.config/ptpython/config.py" +) -> None: """ Execute REPL config file. @@ -752,7 +752,7 @@ def no(event: KeyPressEvent) -> None: @bindings.add("") def _(event: KeyPressEvent) -> None: - " Disallow inserting other text. " + "Disallow inserting other text." pass style From 71c74fe8bf826aa156cce35fca01fba9a8ff6d5c Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Mon, 24 May 2021 14:25:25 +0200 Subject: [PATCH 134/220] Fixed several typing issues. --- ptpython/python_input.py | 2 +- ptpython/repl.py | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/ptpython/python_input.py b/ptpython/python_input.py index 2b75d6e5..1785f523 100644 --- a/ptpython/python_input.py +++ b/ptpython/python_input.py @@ -388,7 +388,7 @@ def __init__( # Create an app if requested. If not, the global get_app() is returned # for self.app via property getter. if create_app: - self._app = self._create_application(input, output) + self._app: Optional[Application] = self._create_application(input, output) # Setting vi_mode will not work unless the prompt_toolkit # application has been created. if vi_mode: diff --git a/ptpython/repl.py b/ptpython/repl.py index c0026fbf..64c9dc14 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -294,7 +294,7 @@ def _compile_with_flags(self, code: str, mode: str): dont_inherit=True, ) - def _format_result_output(self, result: object) -> AnyFormattedText: + def _format_result_output(self, result: object) -> StyleAndTextTuples: """ Format __repr__ for an `eval` result. @@ -312,7 +312,7 @@ def _format_result_output(self, result: object) -> AnyFormattedText: except BaseException as e: # Calling repr failed. self._handle_exception(e) - return None + return [] try: compile(result_repr, "", "eval") @@ -502,7 +502,7 @@ def create_pager_prompt(self) -> PromptSession["PagerResult"]: """ return create_pager_prompt(self._current_style, self.title) - def _format_exception_output(self, e: BaseException) -> AnyFormattedText: + def _format_exception_output(self, e: BaseException) -> PygmentsTokens: # Instead of just calling ``traceback.format_exc``, we take the # traceback and skip the bottom calls of this framework. t, v, tb = sys.exc_info() @@ -531,7 +531,7 @@ def _format_exception_output(self, e: BaseException) -> AnyFormattedText: tokens = list(_lex_python_traceback(tb_str)) else: tokens = [(Token, tb_str)] - return tokens + return PygmentsTokens(tokens) def _handle_exception(self, e: BaseException) -> None: output = self.app.output @@ -539,7 +539,7 @@ def _handle_exception(self, e: BaseException) -> None: tokens = self._format_exception_output(e) print_formatted_text( - PygmentsTokens(tokens), + tokens, style=self._current_style, style_transformation=self.style_transformation, include_default_pygments_style=False, From b74af76490ee6cba674f916b51e0495729988fb6 Mon Sep 17 00:00:00 2001 From: stonebig Date: Sun, 30 May 2021 18:12:52 +0200 Subject: [PATCH 135/220] Make Black optional Make Black optional --- ptpython/repl.py | 17 +++++++++++------ setup.py | 6 ++++-- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/ptpython/repl.py b/ptpython/repl.py index 64c9dc14..b158b93c 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -323,12 +323,17 @@ def _format_result_output(self, result: object) -> StyleAndTextTuples: if self.enable_output_formatting: # Inline import. Slightly speed up start-up time if black is # not used. - import black - - result_repr = black.format_str( - result_repr, - mode=black.FileMode(line_length=self.app.output.get_size().columns), - ) + try: + import black + except ImportError: + pass # no Black package in your installation + else: + result_repr = black.format_str( + result_repr, + mode=black.FileMode( + line_length=self.app.output.get_size().columns + ), + ) formatted_result_repr = to_formatted_text( PygmentsTokens(list(_lex_python_result(result_repr))) diff --git a/setup.py b/setup.py index 3f735073..40b23cf1 100644 --- a/setup.py +++ b/setup.py @@ -23,7 +23,6 @@ # Use prompt_toolkit 3.0.18, because of the `in_thread` option. "prompt_toolkit>=3.0.18,<3.1.0", "pygments", - "black", ], python_requires=">=3.6", classifiers=[ @@ -47,5 +46,8 @@ % sys.version_info[:2], ] }, - extras_require={"ptipython": ["ipython"]}, # For ptipython, we need to have IPython + extras_require={ + "ptipython": ["ipython"], # For ptipython, we need to have IPython + "all": ["black"], # Black not always possible on PyPy + }, ) From 4b49c5bc8841d854c35b6a5586718f05d84ae5cf Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Sat, 26 Jun 2021 21:43:25 +0200 Subject: [PATCH 136/220] Release 3.0.18 --- CHANGELOG | 7 +++++++ setup.py | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 8f946b4b..d3f64ac1 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,13 @@ CHANGELOG ========= +3.0.18: 2020-06-26 +------------------ + +Fixes: +- Made "black" an optional dependency. + + 3.0.17: 2020-03-22 ------------------ diff --git a/setup.py b/setup.py index 40b23cf1..ad17cfad 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ setup( name="ptpython", author="Jonathan Slenders", - version="3.0.17", + version="3.0.18", url="https://github.com/prompt-toolkit/ptpython", description="Python REPL build on top of prompt_toolkit", long_description=long_description, From 78c5a0df8d37c24d97c933a7f820afdd179bab28 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Thu, 8 Jul 2021 16:32:43 +0200 Subject: [PATCH 137/220] Fix handling of SystemExit --- ptpython/repl.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ptpython/repl.py b/ptpython/repl.py index b158b93c..e6647c9f 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -98,7 +98,7 @@ def run_and_show_expression(self, expression): except KeyboardInterrupt: # KeyboardInterrupt doesn't inherit from Exception. raise except SystemExit: - return + raise except BaseException as e: self._handle_exception(e) else: From b6d9bc7a18a030a57892008d6f819d3900918e2d Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Thu, 8 Jul 2021 16:38:06 +0200 Subject: [PATCH 138/220] Fix for black integration. Use black.Mode instead of black.FileMode. --- .github/workflows/test.yaml | 1 + ptpython/repl.py | 4 +--- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 00ed1b00..0368ba7b 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -23,6 +23,7 @@ jobs: sudo apt remove python3-pip python -m pip install --upgrade pip python -m pip install . black isort mypy pytest readme_renderer + python -m pip install . types-dataclasses # Needed for Python 3.6 pip list - name: Type Checker run: | diff --git a/ptpython/repl.py b/ptpython/repl.py index e6647c9f..455e5f38 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -330,9 +330,7 @@ def _format_result_output(self, result: object) -> StyleAndTextTuples: else: result_repr = black.format_str( result_repr, - mode=black.FileMode( - line_length=self.app.output.get_size().columns - ), + mode=black.Mode(line_length=self.app.output.get_size().columns), ) formatted_result_repr = to_formatted_text( From 70dd3bd8d0785bcd76d6da82355a1a1aaa7e33d1 Mon Sep 17 00:00:00 2001 From: baldulin Date: Fri, 11 Jun 2021 20:53:12 +0200 Subject: [PATCH 139/220] Enable use of await in assignment expressions This tries to fix #447 and some other bugs concerning expressions, like for instance `for` loops. Which cannot contain awaitables otherwise. --- ptpython/repl.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/ptpython/repl.py b/ptpython/repl.py index 455e5f38..b3411cb8 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -271,9 +271,14 @@ async def eval_async(self, line: str) -> object: self._store_eval_result(result) return result - # If not a valid `eval` expression, run using `exec` instead. + # If not a valid `eval` expression, compile as `exec` expression + # but still run with eval to get an awaitable in case of a + # awaitable expression. code = self._compile_with_flags(line, "exec") - exec(code, self.get_globals(), self.get_locals()) + result = eval(code, self.get_globals(), self.get_locals()) + + if _has_coroutine_flag(code): + result = await result return None From 3bf39985671de1ed37584fcea37c6fa850e37fc7 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Thu, 8 Jul 2021 17:16:24 +0200 Subject: [PATCH 140/220] Fix last commit: allow await in assignment expressions when the REPL itself is not running async. --- ptpython/repl.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ptpython/repl.py b/ptpython/repl.py index b3411cb8..220c673f 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -239,7 +239,10 @@ def eval(self, line: str) -> object: # above, then `sys.exc_info()` would not report the right error. # See issue: https://github.com/prompt-toolkit/ptpython/issues/435 code = self._compile_with_flags(line, "exec") - exec(code, self.get_globals(), self.get_locals()) + result = eval(code, self.get_globals(), self.get_locals()) + + if _has_coroutine_flag(code): + result = asyncio.get_event_loop().run_until_complete(result) return None From 52705a77d31dc0914386c10422e89303aa5ca0c5 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Thu, 8 Jul 2021 17:25:08 +0200 Subject: [PATCH 141/220] Release 3.0.19 --- CHANGELOG | 9 +++++++++ setup.py | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index d3f64ac1..6a1eb218 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,15 @@ CHANGELOG ========= +3.0.19: 2020-07-08 +------------------ + +Fixes: +- Fix handling of `SystemExit` (fixes "ValueError: I/O operation on closed + file"). +- Allow usage of `await` in assignment expressions or for-loops. + + 3.0.18: 2020-06-26 ------------------ diff --git a/setup.py b/setup.py index ad17cfad..faab112d 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ setup( name="ptpython", author="Jonathan Slenders", - version="3.0.18", + version="3.0.19", url="https://github.com/prompt-toolkit/ptpython", description="Python REPL build on top of prompt_toolkit", long_description=long_description, From ae608c27427af160ec5c06a30851cb339e24b0bb Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Fri, 6 Aug 2021 15:56:26 +0200 Subject: [PATCH 142/220] Don't crash when trying to complete broken mappings. Show the traceback when something goes wrong while reading input in the REPL due to completer bugs or other bugs. Don't crash the REPL. --- ptpython/completer.py | 8 ++++++++ ptpython/repl.py | 12 ++++++++++++ 2 files changed, 20 insertions(+) diff --git a/ptpython/completer.py b/ptpython/completer.py index 285398c2..d235a024 100644 --- a/ptpython/completer.py +++ b/ptpython/completer.py @@ -505,6 +505,10 @@ def abbr_meta(text: str) -> str: display=f"[{k_repr}]", display_meta=abbr_meta(self._do_repr(result[k])), ) + except KeyError: + # `result[k]` lookup failed. Trying to complete + # broken object. + pass except ReprFailedError: pass @@ -521,6 +525,10 @@ def abbr_meta(text: str) -> str: display=f"[{k_repr}]", display_meta=abbr_meta(self._do_repr(result[k])), ) + except KeyError: + # `result[k]` lookup failed. Trying to complete + # broken object. + pass except ReprFailedError: pass diff --git a/ptpython/repl.py b/ptpython/repl.py index 220c673f..d451a61f 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -135,6 +135,12 @@ def run(self) -> None: 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) @@ -192,6 +198,12 @@ async def run_async(self) -> None: 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) From 667805397637edd66d82d891fed0819178f987fd Mon Sep 17 00:00:00 2001 From: jlamelas Date: Mon, 13 Sep 2021 17:55:47 +0200 Subject: [PATCH 143/220] Raising Import Error if Mode not in black - older versions don't have Mode. Adding formating --- ptpython/repl.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ptpython/repl.py b/ptpython/repl.py index d451a61f..b55b5d56 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -345,6 +345,9 @@ def _format_result_output(self, result: object) -> StyleAndTextTuples: # not used. try: import black + + if not hasattr(black, "Mode"): + raise ImportError except ImportError: pass # no Black package in your installation else: From e9df9075c20451b3e2852104f5a6f672de51485b Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Tue, 14 Sep 2021 12:11:25 +0200 Subject: [PATCH 144/220] Show parentheses after the completions for methods when using the `DictionaryCompleter`. --- ptpython/completer.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ptpython/completer.py b/ptpython/completer.py index d235a024..51a4086b 100644 --- a/ptpython/completer.py +++ b/ptpython/completer.py @@ -553,9 +553,8 @@ def _get_attribute_completions( def get_suffix(name: str) -> str: try: obj = getattr(result, name, None) - if inspect.isfunction(obj): + if inspect.isfunction(obj) or inspect.ismethod(obj): return "()" - if isinstance(obj, dict): return "{}" if isinstance(obj, (list, tuple)): From 2ba2174f361fc3f5cf000fe59c4e64bf3d9ddead Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Tue, 14 Sep 2021 12:21:03 +0200 Subject: [PATCH 145/220] Fix dates in changelog (2020 -> 2021). --- CHANGELOG | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 6a1eb218..d561685c 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,7 +1,8 @@ CHANGELOG ========= -3.0.19: 2020-07-08 + +3.0.19: 2021-07-08 ------------------ Fixes: @@ -10,14 +11,14 @@ Fixes: - Allow usage of `await` in assignment expressions or for-loops. -3.0.18: 2020-06-26 +3.0.18: 2021-06-26 ------------------ Fixes: - Made "black" an optional dependency. -3.0.17: 2020-03-22 +3.0.17: 2021-03-22 ------------------ Fixes: @@ -26,7 +27,7 @@ Fixes: - Fix race condition during retrieval of signatures. -3.0.16: 2020-02-11 +3.0.16: 2021-02-11 ------------------ (Commit 7f619e was missing in previous release.) @@ -38,7 +39,7 @@ Fixes: completions were missed out if the fuzzy completer doesn't find them. -3.0.15: 2020-02-11 +3.0.15: 2021-02-11 ------------------ New features: @@ -48,7 +49,7 @@ Fixes: - Fix `AttributeError` during retrieval of signatures with type annotations. -3.0.14: 2020-02-10 +3.0.14: 2021-02-10 ------------------ New features: @@ -67,7 +68,7 @@ Fixes: - Hide signature when sidebar is visible. -3.0.13: 2020-01-26 +3.0.13: 2021-01-26 ------------------ New features: @@ -82,7 +83,7 @@ Fixes: - Fix line ending bug in pager. -3.0.12: 2020-01-24 +3.0.12: 2021-01-24 ------------------ New features: @@ -96,7 +97,7 @@ Fixes: - Properly handle `SystemExit`. -3.0.11: 2020-01-20 +3.0.11: 2021-01-20 ------------------ New features: @@ -119,7 +120,7 @@ Fixes: - Don't execute PYTHONSTARTUP when -i flag was given. -3.0.10: 2020-01-13 +3.0.10: 2021-01-13 ------------------ Fixes: @@ -128,7 +129,7 @@ Fixes: default. -3.0.9: 2020-01-10 +3.0.9: 2021-01-10 ----------------- New features: @@ -137,7 +138,7 @@ New features: - Show REPL title in pager. -3.0.8: 2020-01-05 +3.0.8: 2021-01-05 ----------------- New features: From d8b5c90d20cc1e8d88837aa7163289950c231f74 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Tue, 14 Sep 2021 12:21:48 +0200 Subject: [PATCH 146/220] Release 3.0.20 --- CHANGELOG | 11 +++++++++++ setup.py | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index d561685c..69a95e7d 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,17 @@ CHANGELOG ========= +3.0.20: 2021-09-14 +------------------ + +New features: +- For `DictionaryCompleter`: show parentheses after methods. + +Fixes: +- Don't crash when trying to complete broken mappings in `DictionaryCompleter`. +- Don't crash when an older version of `black` is installed that is not + compatible. + 3.0.19: 2021-07-08 ------------------ diff --git a/setup.py b/setup.py index faab112d..72a0e8b2 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ setup( name="ptpython", author="Jonathan Slenders", - version="3.0.19", + version="3.0.20", url="https://github.com/prompt-toolkit/ptpython", description="Python REPL build on top of prompt_toolkit", long_description=long_description, From 5449dc4ed494aacd9627ac4a97e24ee2113724f2 Mon Sep 17 00:00:00 2001 From: Rik Date: Fri, 22 Oct 2021 11:39:35 +0200 Subject: [PATCH 147/220] Added docs to example config about code colorscheme usage --- examples/ptpython_config/config.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/examples/ptpython_config/config.py b/examples/ptpython_config/config.py index 24275728..bf9d05fe 100644 --- a/examples/ptpython_config/config.py +++ b/examples/ptpython_config/config.py @@ -106,8 +106,13 @@ def configure(repl): repl.enable_input_validation = True # Use this colorscheme for the code. + # Ptpython uses Pygments for code styling, so you can choose from Pygments' + # color schemes. See: + # https://pygments.org/docs/styles/ + # https://pygments.org/demo/ repl.use_code_colorscheme("default") - # repl.use_code_colorscheme("pastie") + # A colorscheme that looks good on dark backgrounds is 'native': + # repl.use_code_colorscheme("native") # Set color depth (keep in mind that not all terminals support true color). From 52490b3235f62ee0d2c68a3b4eea83715a50e8af Mon Sep 17 00:00:00 2001 From: Jack Desert Date: Thu, 19 Aug 2021 09:35:52 -0500 Subject: [PATCH 148/220] Demonstrate Help Menu in README --- README.rst | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index ae12f4d7..1edc5403 100644 --- a/README.rst +++ b/README.rst @@ -50,6 +50,40 @@ Features [2] If the terminal supports it (most terminals do), this allows pasting without going into paste mode. It will keep the indentation. +Command Line Options +******************** + +The help menu shows basic command-line options. + +:: + + $ ptpython --help + usage: ptpython [-h] [--vi] [-i] [--light-bg] [--dark-bg] [--config-file CONFIG_FILE] + [--history-file HISTORY_FILE] [-V] + [args ...] + + ptpython: Interactive Python shell. + + positional arguments: + args Script and arguments + + optional arguments: + -h, --help show this help message and exit + --vi Enable Vi key bindings + -i, --interactive Start interactive shell after executing this file. + --light-bg Run on a light background (use dark colors for text). + --dark-bg Run on a dark background (use light colors for text). + --config-file CONFIG_FILE + Location of configuration file. + --history-file HISTORY_FILE + Location of history file. + -V, --version show program's version number and exit + + environment variables: + PTPYTHON_CONFIG_HOME: a configuration directory to use + PYTHONSTARTUP: file executed on interactive startup (no default) + + __pt_repr__: A nicer repr with colors ************************************* @@ -211,7 +245,7 @@ FAQ **Q**: The ``Meta``-key doesn't work. -**A**: For some terminals you have to enable the Alt-key to act as meta key, but you +**A**: For some terminals you have to enable the Alt-key to act as meta key, but you can also type ``Escape`` before any key instead. From d31915d4bc97e72415d63681b9d480375da829cc Mon Sep 17 00:00:00 2001 From: Jack Desert Date: Thu, 19 Aug 2021 09:56:21 -0500 Subject: [PATCH 149/220] Alerting Users that Config is Not Used when Embedding --- README.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.rst b/README.rst index 1edc5403..15464ba4 100644 --- a/README.rst +++ b/README.rst @@ -143,6 +143,8 @@ like this: else: sys.exit(embed(globals(), locals())) +Note config file support currently only works when invoking `ptpython` directly. +That it, the config file will be ignored when embedding ptpython in an application. Multiline editing ***************** @@ -193,6 +195,9 @@ is looked for. Have a look at this example to see what is possible: `config.py `_ +Note config file support currently only works when invoking `ptpython` directly. +That it, the config file will be ignored when embedding ptpython in an application. + IPython support *************** From 0af5c10cf19462f27e43914b8dbed81e5b0f53ae Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Mon, 20 Jun 2022 22:46:36 +0200 Subject: [PATCH 150/220] Added py.typed to package_data in setup.py --- setup.py | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.py b/setup.py index 72a0e8b2..a8214f27 100644 --- a/setup.py +++ b/setup.py @@ -16,6 +16,7 @@ description="Python REPL build on top of prompt_toolkit", long_description=long_description, packages=find_packages("."), + package_data={"ptpython": ["py.typed"]}, install_requires=[ "appdirs", "importlib_metadata;python_version<'3.8'", From 042ecc3199b89819a071048fbd27e551ae25e114 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Fri, 28 Oct 2022 20:18:02 +0000 Subject: [PATCH 151/220] Improve DictionaryCompleter performance for slow mappings. The performance was bad when we had a huge custom mapping with an expensive `__getitem__`. --- ptpython/completer.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ptpython/completer.py b/ptpython/completer.py index 51a4086b..22698f8e 100644 --- a/ptpython/completer.py +++ b/ptpython/completer.py @@ -495,7 +495,7 @@ def abbr_meta(text: str) -> str: else: break - for k in result: + for k, v in result.items(): if str(k).startswith(str(key_obj)): try: k_repr = self._do_repr(k) @@ -503,7 +503,7 @@ def abbr_meta(text: str) -> str: k_repr + "]", -len(key), display=f"[{k_repr}]", - display_meta=abbr_meta(self._do_repr(result[k])), + display_meta=abbr_meta(self._do_repr(v)), ) except KeyError: # `result[k]` lookup failed. Trying to complete From 05d4aed170babf345e2daed90fff812349044ce4 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Fri, 28 Oct 2022 21:36:16 +0000 Subject: [PATCH 152/220] Added more precise types in various places. --- ptpython/completer.py | 25 ++++-- ptpython/entry_points/run_ptpython.py | 12 +-- ptpython/eventloop.py | 14 +-- ptpython/history_browser.py | 122 +++++++++++++++++--------- ptpython/ipython.py | 26 ++++-- ptpython/key_bindings.py | 61 +++++++------ ptpython/layout.py | 64 ++++++++------ ptpython/python_input.py | 52 +++++++---- ptpython/repl.py | 15 ++-- ptpython/signatures.py | 9 +- ptpython/utils.py | 36 ++++++-- ptpython/validator.py | 9 +- setup.cfg | 39 ++++++++ 13 files changed, 324 insertions(+), 160 deletions(-) diff --git a/ptpython/completer.py b/ptpython/completer.py index 22698f8e..2b6795d4 100644 --- a/ptpython/completer.py +++ b/ptpython/completer.py @@ -4,7 +4,7 @@ import keyword import re from enum import Enum -from typing import TYPE_CHECKING, Any, Callable, Dict, Iterable, List, Optional +from typing import TYPE_CHECKING, Any, Callable, Dict, Iterable, List, Optional, Tuple from prompt_toolkit.completion import ( CompleteEvent, @@ -21,6 +21,7 @@ from ptpython.utils import get_jedi_script_from_document if TYPE_CHECKING: + import jedi.api.classes from prompt_toolkit.contrib.regular_languages.compiler import _CompiledGrammar __all__ = ["PythonCompleter", "CompletePrivateAttributes", "HidePrivateCompleter"] @@ -43,8 +44,8 @@ class PythonCompleter(Completer): def __init__( self, - get_globals: Callable[[], dict], - get_locals: Callable[[], dict], + get_globals: Callable[[], Dict[str, Any]], + get_locals: Callable[[], Dict[str, Any]], enable_dictionary_completion: Callable[[], bool], ) -> None: super().__init__() @@ -200,7 +201,11 @@ class JediCompleter(Completer): Autocompleter that uses the Jedi library. """ - def __init__(self, get_globals, get_locals) -> None: + def __init__( + self, + get_globals: Callable[[], Dict[str, Any]], + get_locals: Callable[[], Dict[str, Any]], + ) -> None: super().__init__() self.get_globals = get_globals @@ -296,7 +301,11 @@ class DictionaryCompleter(Completer): function calls, so it only triggers attribute access. """ - def __init__(self, get_globals, get_locals): + def __init__( + self, + get_globals: Callable[[], Dict[str, Any]], + get_locals: Callable[[], Dict[str, Any]], + ) -> None: super().__init__() self.get_globals = get_globals @@ -574,7 +583,7 @@ def _sort_attribute_names(self, names: List[str]) -> List[str]: underscore names to the end. """ - def sort_key(name: str): + def sort_key(name: str) -> Tuple[int, str]: if name.startswith("__"): return (2, name) # Double underscore comes latest. if name.startswith("_"): @@ -639,7 +648,9 @@ class ReprFailedError(Exception): _builtin_names = [] -def _get_style_for_jedi_completion(jedi_completion) -> str: +def _get_style_for_jedi_completion( + jedi_completion: "jedi.api.classes.Completion", +) -> str: """ Return completion style to use for this name. """ diff --git a/ptpython/entry_points/run_ptpython.py b/ptpython/entry_points/run_ptpython.py index 5ebe2b95..edffa44d 100644 --- a/ptpython/entry_points/run_ptpython.py +++ b/ptpython/entry_points/run_ptpython.py @@ -26,16 +26,16 @@ import pathlib import sys from textwrap import dedent -from typing import Tuple +from typing import IO, Optional, Tuple import appdirs from prompt_toolkit.formatted_text import HTML from prompt_toolkit.shortcuts import print_formatted_text -from ptpython.repl import embed, enable_deprecation_warnings, run_config +from ptpython.repl import PythonRepl, embed, enable_deprecation_warnings, run_config try: - from importlib import metadata + from importlib import metadata # type: ignore except ImportError: import importlib_metadata as metadata # type: ignore @@ -44,7 +44,7 @@ class _Parser(argparse.ArgumentParser): - def print_help(self): + def print_help(self, file: Optional[IO[str]] = None) -> None: super().print_help() print( dedent( @@ -84,7 +84,7 @@ def create_parser() -> _Parser: "-V", "--version", action="version", - version=metadata.version("ptpython"), # type: ignore + version=metadata.version("ptpython"), ) parser.add_argument("args", nargs="*", help="Script and arguments") return parser @@ -190,7 +190,7 @@ def run() -> None: enable_deprecation_warnings() # Apply config file - def configure(repl) -> None: + def configure(repl: PythonRepl) -> None: if os.path.exists(config_file): run_config(repl, config_file) diff --git a/ptpython/eventloop.py b/ptpython/eventloop.py index c841972d..63dd7408 100644 --- a/ptpython/eventloop.py +++ b/ptpython/eventloop.py @@ -10,10 +10,12 @@ import sys import time +from prompt_toolkit.eventloop import InputHookContext + __all__ = ["inputhook"] -def _inputhook_tk(inputhook_context): +def _inputhook_tk(inputhook_context: InputHookContext) -> None: """ Inputhook for Tk. Run the Tk eventloop until prompt-toolkit needs to process the next input. @@ -23,9 +25,9 @@ def _inputhook_tk(inputhook_context): import _tkinter # Keep this imports inline! - root = tkinter._default_root + root = tkinter._default_root # type: ignore - def wait_using_filehandler(): + def wait_using_filehandler() -> None: """ Run the TK eventloop until the file handler that we got from the inputhook becomes readable. @@ -34,7 +36,7 @@ def wait_using_filehandler(): # to process. stop = [False] - def done(*a): + def done(*a: object) -> None: stop[0] = True root.createfilehandler(inputhook_context.fileno(), _tkinter.READABLE, done) @@ -46,7 +48,7 @@ def done(*a): root.deletefilehandler(inputhook_context.fileno()) - def wait_using_polling(): + def wait_using_polling() -> None: """ Windows TK doesn't support 'createfilehandler'. So, run the TK eventloop and poll until input is ready. @@ -65,7 +67,7 @@ def wait_using_polling(): wait_using_polling() -def inputhook(inputhook_context): +def inputhook(inputhook_context: InputHookContext) -> None: # Only call the real input hook when the 'Tkinter' library was loaded. if "Tkinter" in sys.modules or "tkinter" in sys.modules: _inputhook_tk(inputhook_context) diff --git a/ptpython/history_browser.py b/ptpython/history_browser.py index b7fe0865..08725ee0 100644 --- a/ptpython/history_browser.py +++ b/ptpython/history_browser.py @@ -5,6 +5,7 @@ run as a sub application of the Repl/PythonInput. """ from functools import partial +from typing import TYPE_CHECKING, Callable, List, Optional, Set from prompt_toolkit.application import Application from prompt_toolkit.application.current import get_app @@ -12,8 +13,11 @@ from prompt_toolkit.document import Document from prompt_toolkit.enums import DEFAULT_BUFFER from prompt_toolkit.filters import Condition, has_focus +from prompt_toolkit.formatted_text.base import StyleAndTextTuples from prompt_toolkit.formatted_text.utils import fragment_list_to_text +from prompt_toolkit.history import History from prompt_toolkit.key_binding import KeyBindings +from prompt_toolkit.key_binding.key_processor import KeyPressEvent from prompt_toolkit.layout.containers import ( ConditionalContainer, Container, @@ -24,13 +28,23 @@ VSplit, Window, WindowAlign, + WindowRenderInfo, +) +from prompt_toolkit.layout.controls import ( + BufferControl, + FormattedTextControl, + UIContent, ) -from prompt_toolkit.layout.controls import BufferControl, FormattedTextControl from prompt_toolkit.layout.dimension import Dimension as D from prompt_toolkit.layout.layout import Layout from prompt_toolkit.layout.margins import Margin, ScrollbarMargin -from prompt_toolkit.layout.processors import Processor, Transformation +from prompt_toolkit.layout.processors import ( + Processor, + Transformation, + TransformationInput, +) from prompt_toolkit.lexers import PygmentsLexer +from prompt_toolkit.mouse_events import MouseEvent from prompt_toolkit.widgets import Frame from prompt_toolkit.widgets.toolbars import ArgToolbar, SearchToolbar from pygments.lexers import Python3Lexer as PythonLexer @@ -40,10 +54,15 @@ from .utils import if_mousedown +if TYPE_CHECKING: + from .python_input import PythonInput + HISTORY_COUNT = 2000 __all__ = ["HistoryLayout", "PythonHistory"] +E = KeyPressEvent + HELP_TEXT = """ This interface is meant to select multiple lines from the history and execute them together. @@ -109,7 +128,7 @@ class HistoryLayout: application. """ - def __init__(self, history): + def __init__(self, history: "PythonHistory") -> None: search_toolbar = SearchToolbar() self.help_buffer_control = BufferControl( @@ -201,19 +220,19 @@ def __init__(self, history): self.layout = Layout(self.root_container, history_window) -def _get_top_toolbar_fragments(): +def _get_top_toolbar_fragments() -> StyleAndTextTuples: return [("class:status-bar.title", "History browser - Insert from history")] -def _get_bottom_toolbar_fragments(history): +def _get_bottom_toolbar_fragments(history: "PythonHistory") -> StyleAndTextTuples: python_input = history.python_input @if_mousedown - def f1(mouse_event): + def f1(mouse_event: MouseEvent) -> None: _toggle_help(history) @if_mousedown - def tab(mouse_event): + def tab(mouse_event: MouseEvent) -> None: _select_other_window(history) return ( @@ -239,14 +258,16 @@ class HistoryMargin(Margin): This displays a green bar for the selected entries. """ - def __init__(self, history): + def __init__(self, history: "PythonHistory") -> None: self.history_buffer = history.history_buffer self.history_mapping = history.history_mapping - def get_width(self, ui_content): + def get_width(self, get_ui_content: Callable[[], UIContent]) -> int: return 2 - def create_margin(self, window_render_info, width, height): + def create_margin( + self, window_render_info: WindowRenderInfo, width: int, height: int + ) -> StyleAndTextTuples: document = self.history_buffer.document lines_starting_new_entries = self.history_mapping.lines_starting_new_entries @@ -255,7 +276,7 @@ def create_margin(self, window_render_info, width, height): current_lineno = document.cursor_position_row visible_line_to_input_line = window_render_info.visible_line_to_input_line - result = [] + result: StyleAndTextTuples = [] for y in range(height): line_number = visible_line_to_input_line.get(y) @@ -286,14 +307,16 @@ class ResultMargin(Margin): The margin to be shown in the result pane. """ - def __init__(self, history): + def __init__(self, history: "PythonHistory") -> None: self.history_mapping = history.history_mapping self.history_buffer = history.history_buffer - def get_width(self, ui_content): + def get_width(self, get_ui_content: Callable[[], UIContent]) -> int: return 2 - def create_margin(self, window_render_info, width, height): + def create_margin( + self, window_render_info: WindowRenderInfo, width: int, height: int + ) -> StyleAndTextTuples: document = self.history_buffer.document current_lineno = document.cursor_position_row @@ -303,7 +326,7 @@ def create_margin(self, window_render_info, width, height): visible_line_to_input_line = window_render_info.visible_line_to_input_line - result = [] + result: StyleAndTextTuples = [] for y in range(height): line_number = visible_line_to_input_line.get(y) @@ -324,7 +347,7 @@ def create_margin(self, window_render_info, width, height): return result - def invalidation_hash(self, document): + def invalidation_hash(self, document: Document) -> int: return document.cursor_position_row @@ -333,13 +356,15 @@ class GrayExistingText(Processor): Turn the existing input, before and after the inserted code gray. """ - def __init__(self, history_mapping): + def __init__(self, history_mapping: "HistoryMapping") -> None: self.history_mapping = history_mapping self._lines_before = len( history_mapping.original_document.text_before_cursor.splitlines() ) - def apply_transformation(self, transformation_input): + def apply_transformation( + self, transformation_input: TransformationInput + ) -> Transformation: lineno = transformation_input.lineno fragments = transformation_input.fragments @@ -357,17 +382,22 @@ class HistoryMapping: Keep a list of all the lines from the history and the selected lines. """ - def __init__(self, history, python_history, original_document): + def __init__( + self, + history: "PythonHistory", + python_history: History, + original_document: Document, + ) -> None: self.history = history self.python_history = python_history self.original_document = original_document self.lines_starting_new_entries = set() - self.selected_lines = set() + self.selected_lines: Set[int] = set() # Process history. history_strings = python_history.get_strings() - history_lines = [] + history_lines: List[str] = [] for entry_nr, entry in list(enumerate(history_strings))[-HISTORY_COUNT:]: self.lines_starting_new_entries.add(len(history_lines)) @@ -389,7 +419,7 @@ def __init__(self, history, python_history, original_document): else: self.result_line_offset = 0 - def get_new_document(self, cursor_pos=None): + def get_new_document(self, cursor_pos: Optional[int] = None) -> Document: """ Create a `Document` instance that contains the resulting text. """ @@ -413,13 +443,13 @@ def get_new_document(self, cursor_pos=None): cursor_pos = len(text) return Document(text, cursor_pos) - def update_default_buffer(self): + def update_default_buffer(self) -> None: b = self.history.default_buffer b.set_document(self.get_new_document(b.cursor_position), bypass_readonly=True) -def _toggle_help(history): +def _toggle_help(history: "PythonHistory") -> None: "Display/hide help." help_buffer_control = history.history_layout.help_buffer_control @@ -429,7 +459,7 @@ def _toggle_help(history): history.app.layout.current_control = help_buffer_control -def _select_other_window(history): +def _select_other_window(history: "PythonHistory") -> None: "Toggle focus between left/right window." current_buffer = history.app.current_buffer layout = history.history_layout.layout @@ -441,7 +471,11 @@ def _select_other_window(history): layout.current_control = history.history_layout.history_buffer_control -def create_key_bindings(history, python_input, history_mapping): +def create_key_bindings( + history: "PythonHistory", + python_input: "PythonInput", + history_mapping: HistoryMapping, +) -> KeyBindings: """ Key bindings. """ @@ -449,7 +483,7 @@ def create_key_bindings(history, python_input, history_mapping): handle = bindings.add @handle(" ", filter=has_focus(history.history_buffer)) - def _(event): + def _(event: E) -> None: """ Space: select/deselect line from history pane. """ @@ -486,7 +520,7 @@ def _(event): @handle(" ", filter=has_focus(DEFAULT_BUFFER)) @handle("delete", filter=has_focus(DEFAULT_BUFFER)) @handle("c-h", filter=has_focus(DEFAULT_BUFFER)) - def _(event): + def _(event: E) -> None: """ Space: remove line from default pane. """ @@ -512,17 +546,17 @@ def _(event): @handle("c-x", filter=main_buffer_focussed, eager=True) # Eager: ignore the Emacs [Ctrl-X Ctrl-X] binding. @handle("c-w", filter=main_buffer_focussed) - def _(event): + def _(event: E) -> None: "Select other window." _select_other_window(history) @handle("f4") - def _(event): + def _(event: E) -> None: "Switch between Emacs/Vi mode." python_input.vi_mode = not python_input.vi_mode @handle("f1") - def _(event): + def _(event: E) -> None: "Display/hide help." _toggle_help(history) @@ -530,7 +564,7 @@ def _(event): @handle("c-c", filter=help_focussed) @handle("c-g", filter=help_focussed) @handle("escape", filter=help_focussed) - def _(event): + def _(event: E) -> None: "Leave help." event.app.layout.focus_previous() @@ -538,19 +572,19 @@ def _(event): @handle("f3", filter=main_buffer_focussed) @handle("c-c", filter=main_buffer_focussed) @handle("c-g", filter=main_buffer_focussed) - def _(event): + def _(event: E) -> None: "Cancel and go back." event.app.exit(result=None) @handle("enter", filter=main_buffer_focussed) - def _(event): + def _(event: E) -> None: "Accept input." event.app.exit(result=history.default_buffer.text) enable_system_bindings = Condition(lambda: python_input.enable_system_bindings) @handle("c-z", filter=enable_system_bindings) - def _(event): + def _(event: E) -> None: "Suspend to background." event.app.suspend_to_background() @@ -558,7 +592,9 @@ def _(event): class PythonHistory: - def __init__(self, python_input, original_document): + def __init__( + self, python_input: "PythonInput", original_document: Document + ) -> None: """ Create an `Application` for the history screen. This has to be run as a sub application of `python_input`. @@ -577,12 +613,14 @@ def __init__(self, python_input, original_document): + document.get_start_of_line_position(), ) + def accept_handler(buffer: Buffer) -> bool: + get_app().exit(result=self.default_buffer.text) + return False + self.history_buffer = Buffer( document=document, on_cursor_position_changed=self._history_buffer_pos_changed, - accept_handler=( - lambda buff: get_app().exit(result=self.default_buffer.text) - ), + accept_handler=accept_handler, read_only=True, ) @@ -597,7 +635,7 @@ def __init__(self, python_input, original_document): self.history_layout = HistoryLayout(self) - self.app = Application( + self.app: Application[str] = Application( layout=self.history_layout.layout, full_screen=True, style=python_input._current_style, @@ -605,7 +643,7 @@ def __init__(self, python_input, original_document): key_bindings=create_key_bindings(self, python_input, history_mapping), ) - def _default_buffer_pos_changed(self, _): + def _default_buffer_pos_changed(self, _: Buffer) -> None: """When the cursor changes in the default buffer. Synchronize with history buffer.""" # Only when this buffer has the focus. @@ -629,7 +667,7 @@ def _default_buffer_pos_changed(self, _): ) ) - def _history_buffer_pos_changed(self, _): + def _history_buffer_pos_changed(self, _: Buffer) -> None: """When the cursor changes in the history buffer. Synchronize.""" # Only when this buffer has the focus. if self.app.current_buffer == self.history_buffer: diff --git a/ptpython/ipython.py b/ptpython/ipython.py index 91633340..9eafa995 100644 --- a/ptpython/ipython.py +++ b/ptpython/ipython.py @@ -8,6 +8,7 @@ offer. """ +from typing import Iterable from warnings import warn from IPython import utils as ipy_utils @@ -15,6 +16,7 @@ from IPython.terminal.embed import InteractiveShellEmbed as _InteractiveShellEmbed from IPython.terminal.ipapp import load_default_config from prompt_toolkit.completion import ( + CompleteEvent, Completer, Completion, PathCompleter, @@ -25,15 +27,17 @@ from prompt_toolkit.contrib.regular_languages.completion import GrammarCompleter from prompt_toolkit.contrib.regular_languages.lexer import GrammarLexer from prompt_toolkit.document import Document -from prompt_toolkit.formatted_text import PygmentsTokens +from prompt_toolkit.formatted_text import AnyFormattedText, PygmentsTokens from prompt_toolkit.lexers import PygmentsLexer, SimpleLexer from prompt_toolkit.styles import Style from pygments.lexers import BashLexer, PythonLexer from ptpython.prompt_style import PromptStyle -from .python_input import PythonCompleter, PythonInput, PythonValidator +from .completer import PythonCompleter +from .python_input import PythonInput from .style import default_ui_style +from .validator import PythonValidator __all__ = ["embed"] @@ -46,13 +50,13 @@ class IPythonPrompt(PromptStyle): def __init__(self, prompts): self.prompts = prompts - def in_prompt(self): + def in_prompt(self) -> AnyFormattedText: return PygmentsTokens(self.prompts.in_prompt_tokens()) - def in2_prompt(self, width): + def in2_prompt(self, width: int) -> AnyFormattedText: return PygmentsTokens(self.prompts.continuation_prompt_tokens()) - def out_prompt(self): + def out_prompt(self) -> AnyFormattedText: return [] @@ -61,7 +65,7 @@ def __init__(self, *args, **kwargs): super(IPythonValidator, self).__init__(*args, **kwargs) self.isp = IPythonInputSplitter() - def validate(self, document): + def validate(self, document: Document) -> None: document = Document(text=self.isp.transform_cell(document.text)) super(IPythonValidator, self).validate(document) @@ -142,7 +146,9 @@ class MagicsCompleter(Completer): def __init__(self, magics_manager): self.magics_manager = magics_manager - def get_completions(self, document, complete_event): + def get_completions( + self, document: Document, complete_event: CompleteEvent + ) -> Iterable[Completion]: text = document.text_before_cursor.lstrip() for m in sorted(self.magics_manager.magics["line"]): @@ -154,7 +160,9 @@ class AliasCompleter(Completer): def __init__(self, alias_manager): self.alias_manager = alias_manager - def get_completions(self, document, complete_event): + def get_completions( + self, document: Document, complete_event: CompleteEvent + ) -> Iterable[Completion]: text = document.text_before_cursor.lstrip() # aliases = [a for a, _ in self.alias_manager.aliases] aliases = self.alias_manager.aliases @@ -240,7 +248,7 @@ def get_globals(): self.python_input = python_input - def prompt_for_code(self): + def prompt_for_code(self) -> str: try: return self.python_input.app.run() except KeyboardInterrupt: diff --git a/ptpython/key_bindings.py b/ptpython/key_bindings.py index ae23a3df..147a321d 100644 --- a/ptpython/key_bindings.py +++ b/ptpython/key_bindings.py @@ -1,4 +1,7 @@ +from typing import TYPE_CHECKING + from prompt_toolkit.application import get_app +from prompt_toolkit.buffer import Buffer from prompt_toolkit.document import Document from prompt_toolkit.enums import DEFAULT_BUFFER from prompt_toolkit.filters import ( @@ -11,19 +14,25 @@ ) from prompt_toolkit.key_binding import KeyBindings from prompt_toolkit.key_binding.bindings.named_commands import get_by_name +from prompt_toolkit.key_binding.key_processor import KeyPressEvent from prompt_toolkit.keys import Keys from .utils import document_is_multiline_python +if TYPE_CHECKING: + from .python_input import PythonInput + __all__ = [ "load_python_bindings", "load_sidebar_bindings", "load_confirm_exit_bindings", ] +E = KeyPressEvent + @Condition -def tab_should_insert_whitespace(): +def tab_should_insert_whitespace() -> bool: """ When the 'tab' key is pressed with only whitespace character before the cursor, do autocompletion. Otherwise, insert indentation. @@ -38,7 +47,7 @@ def tab_should_insert_whitespace(): return bool(b.text and (not before_cursor or before_cursor.isspace())) -def load_python_bindings(python_input): +def load_python_bindings(python_input: "PythonInput") -> KeyBindings: """ Custom key bindings. """ @@ -48,14 +57,14 @@ def load_python_bindings(python_input): handle = bindings.add @handle("c-l") - def _(event): + def _(event: E) -> None: """ Clear whole screen and render again -- also when the sidebar is visible. """ event.app.renderer.clear() @handle("c-z") - def _(event): + def _(event: E) -> None: """ Suspend. """ @@ -67,7 +76,7 @@ def _(event): handle("c-w")(get_by_name("backward-kill-word")) @handle("f2") - def _(event): + def _(event: E) -> None: """ Show/hide sidebar. """ @@ -78,21 +87,21 @@ def _(event): event.app.layout.focus_last() @handle("f3") - def _(event): + def _(event: E) -> None: """ Select from the history. """ python_input.enter_history() @handle("f4") - def _(event): + def _(event: E) -> None: """ Toggle between Vi and Emacs mode. """ python_input.vi_mode = not python_input.vi_mode @handle("f6") - def _(event): + def _(event: E) -> None: """ Enable/Disable paste mode. """ @@ -101,14 +110,14 @@ def _(event): @handle( "tab", filter=~sidebar_visible & ~has_selection & tab_should_insert_whitespace ) - def _(event): + def _(event: E) -> None: """ When tab should insert whitespace, do that instead of completion. """ event.app.current_buffer.insert_text(" ") @Condition - def is_multiline(): + def is_multiline() -> bool: return document_is_multiline_python(python_input.default_buffer.document) @handle( @@ -120,7 +129,7 @@ def is_multiline(): & ~is_multiline, ) @handle(Keys.Escape, Keys.Enter, filter=~sidebar_visible & emacs_mode) - def _(event): + def _(event: E) -> None: """ Accept input (for single line input). """ @@ -143,7 +152,7 @@ def _(event): & has_focus(DEFAULT_BUFFER) & is_multiline, ) - def _(event): + def _(event: E) -> None: """ Behaviour of the Enter key. @@ -153,11 +162,11 @@ def _(event): b = event.current_buffer empty_lines_required = python_input.accept_input_on_enter or 10000 - def at_the_end(b): + def at_the_end(b: Buffer) -> bool: """we consider the cursor at the end when there is no text after the cursor, or only whitespace.""" text = b.document.text_after_cursor - return text == "" or (text.isspace() and not "\n" in text) + return text == "" or (text.isspace() and "\n" not in text) if python_input.paste_mode: # In paste mode, always insert text. @@ -187,7 +196,7 @@ def at_the_end(b): not get_app().current_buffer.text ), ) - def _(event): + def _(event: E) -> None: """ Override Control-D exit, to ask for confirmation. """ @@ -202,14 +211,14 @@ def _(event): event.app.exit(exception=EOFError) @handle("c-c", filter=has_focus(python_input.default_buffer)) - def _(event): + def _(event: E) -> None: "Abort when Control-C has been pressed." event.app.exit(exception=KeyboardInterrupt, style="class:aborting") return bindings -def load_sidebar_bindings(python_input): +def load_sidebar_bindings(python_input: "PythonInput") -> KeyBindings: """ Load bindings for the navigation in the sidebar. """ @@ -221,7 +230,7 @@ def load_sidebar_bindings(python_input): @handle("up", filter=sidebar_visible) @handle("c-p", filter=sidebar_visible) @handle("k", filter=sidebar_visible) - def _(event): + def _(event: E) -> None: "Go to previous option." python_input.selected_option_index = ( python_input.selected_option_index - 1 @@ -230,7 +239,7 @@ def _(event): @handle("down", filter=sidebar_visible) @handle("c-n", filter=sidebar_visible) @handle("j", filter=sidebar_visible) - def _(event): + def _(event: E) -> None: "Go to next option." python_input.selected_option_index = ( python_input.selected_option_index + 1 @@ -239,14 +248,14 @@ def _(event): @handle("right", filter=sidebar_visible) @handle("l", filter=sidebar_visible) @handle(" ", filter=sidebar_visible) - def _(event): + def _(event: E) -> None: "Select next value for current option." option = python_input.selected_option option.activate_next() @handle("left", filter=sidebar_visible) @handle("h", filter=sidebar_visible) - def _(event): + def _(event: E) -> None: "Select previous value for current option." option = python_input.selected_option option.activate_previous() @@ -256,7 +265,7 @@ def _(event): @handle("c-d", filter=sidebar_visible) @handle("enter", filter=sidebar_visible) @handle("escape", filter=sidebar_visible) - def _(event): + def _(event: E) -> None: "Hide sidebar." python_input.show_sidebar = False event.app.layout.focus_last() @@ -264,7 +273,7 @@ def _(event): return bindings -def load_confirm_exit_bindings(python_input): +def load_confirm_exit_bindings(python_input: "PythonInput") -> KeyBindings: """ Handle yes/no key presses when the exit confirmation is shown. """ @@ -277,14 +286,14 @@ def load_confirm_exit_bindings(python_input): @handle("Y", filter=confirmation_visible) @handle("enter", filter=confirmation_visible) @handle("c-d", filter=confirmation_visible) - def _(event): + def _(event: E) -> None: """ Really quit. """ event.app.exit(exception=EOFError, style="class:exiting") @handle(Keys.Any, filter=confirmation_visible) - def _(event): + def _(event: E) -> None: """ Cancel exit. """ @@ -294,7 +303,7 @@ def _(event): return bindings -def auto_newline(buffer): +def auto_newline(buffer: Buffer) -> None: r""" Insert \n at the cursor position. Also add necessary padding. """ diff --git a/ptpython/layout.py b/ptpython/layout.py index dc6b19bb..365f381b 100644 --- a/ptpython/layout.py +++ b/ptpython/layout.py @@ -5,7 +5,7 @@ import sys from enum import Enum from inspect import _ParameterKind as ParameterKind -from typing import TYPE_CHECKING, Optional +from typing import TYPE_CHECKING, Any, List, Optional, Type from prompt_toolkit.application import get_app from prompt_toolkit.enums import DEFAULT_BUFFER, SEARCH_BUFFER @@ -15,10 +15,15 @@ is_done, renderer_height_is_known, ) -from prompt_toolkit.formatted_text import fragment_list_width, to_formatted_text +from prompt_toolkit.formatted_text import ( + AnyFormattedText, + fragment_list_width, + to_formatted_text, +) from prompt_toolkit.formatted_text.base import StyleAndTextTuples from prompt_toolkit.key_binding.vi_state import InputMode from prompt_toolkit.layout.containers import ( + AnyContainer, ConditionalContainer, Container, Float, @@ -40,9 +45,10 @@ HighlightIncrementalSearchProcessor, HighlightMatchingBracketProcessor, HighlightSelectionProcessor, + Processor, TabsProcessor, ) -from prompt_toolkit.lexers import SimpleLexer +from prompt_toolkit.lexers import Lexer, SimpleLexer from prompt_toolkit.mouse_events import MouseEvent from prompt_toolkit.selection import SelectionType from prompt_toolkit.widgets.toolbars import ( @@ -55,6 +61,7 @@ from pygments.lexers import PythonLexer from .filters import HasSignature, ShowDocstring, ShowSidebar, ShowSignature +from .prompt_style import PromptStyle from .utils import if_mousedown if TYPE_CHECKING: @@ -98,7 +105,7 @@ def python_sidebar(python_input: "PythonInput") -> Window: def get_text_fragments() -> StyleAndTextTuples: tokens: StyleAndTextTuples = [] - def append_category(category: "OptionCategory") -> None: + def append_category(category: "OptionCategory[Any]") -> None: tokens.extend( [ ("class:sidebar", " "), @@ -150,10 +157,10 @@ def goto_next(mouse_event: MouseEvent) -> None: return tokens class Control(FormattedTextControl): - def move_cursor_down(self): + def move_cursor_down(self) -> None: python_input.selected_option_index += 1 - def move_cursor_up(self): + def move_cursor_up(self) -> None: python_input.selected_option_index -= 1 return Window( @@ -165,12 +172,12 @@ def move_cursor_up(self): ) -def python_sidebar_navigation(python_input): +def python_sidebar_navigation(python_input: "PythonInput") -> Window: """ Create the `Layout` showing the navigation information for the sidebar. """ - def get_text_fragments(): + def get_text_fragments() -> StyleAndTextTuples: # Show navigation info. return [ ("class:sidebar", " "), @@ -191,13 +198,13 @@ def get_text_fragments(): ) -def python_sidebar_help(python_input): +def python_sidebar_help(python_input: "PythonInput") -> Container: """ Create the `Layout` for the help text for the current item in the sidebar. """ token = "class:sidebar.helptext" - def get_current_description(): + def get_current_description() -> str: """ Return the description of the selected option. """ @@ -209,7 +216,7 @@ def get_current_description(): i += 1 return "" - def get_help_text(): + def get_help_text() -> StyleAndTextTuples: return [(token, get_current_description())] return ConditionalContainer( @@ -225,7 +232,7 @@ def get_help_text(): ) -def signature_toolbar(python_input): +def signature_toolbar(python_input: "PythonInput") -> Container: """ Return the `Layout` for the signature. """ @@ -311,21 +318,23 @@ class PythonPromptMargin(PromptMargin): It shows something like "In [1]:". """ - def __init__(self, python_input) -> None: + def __init__(self, python_input: "PythonInput") -> None: self.python_input = python_input - def get_prompt_style(): + def get_prompt_style() -> PromptStyle: return python_input.all_prompt_styles[python_input.prompt_style] def get_prompt() -> StyleAndTextTuples: return to_formatted_text(get_prompt_style().in_prompt()) - def get_continuation(width, line_number, is_soft_wrap): + def get_continuation( + width: int, line_number: int, is_soft_wrap: bool + ) -> StyleAndTextTuples: if python_input.show_line_numbers and not is_soft_wrap: text = ("%i " % (line_number + 1)).rjust(width) return [("class:line-number", text)] else: - return get_prompt_style().in2_prompt(width) + return to_formatted_text(get_prompt_style().in2_prompt(width)) super().__init__(get_prompt, get_continuation) @@ -510,7 +519,7 @@ def get_text_fragments() -> StyleAndTextTuples: def create_exit_confirmation( - python_input: "PythonInput", style="class:exit-confirmation" + python_input: "PythonInput", style: str = "class:exit-confirmation" ) -> Container: """ Create `Layout` for the exit message. @@ -567,22 +576,22 @@ class PtPythonLayout: def __init__( self, python_input: "PythonInput", - lexer=PythonLexer, - extra_body=None, - extra_toolbars=None, - extra_buffer_processors=None, + lexer: Lexer, + extra_body: Optional[AnyContainer] = None, + extra_toolbars: Optional[List[AnyContainer]] = None, + extra_buffer_processors: Optional[List[Processor]] = None, input_buffer_height: Optional[AnyDimension] = None, ) -> None: D = Dimension - extra_body = [extra_body] if extra_body else [] + extra_body_list: List[AnyContainer] = [extra_body] if extra_body else [] extra_toolbars = extra_toolbars or [] - extra_buffer_processors = extra_buffer_processors or [] + input_buffer_height = input_buffer_height or D(min=6) search_toolbar = SearchToolbar(python_input.search_buffer) - def create_python_input_window(): - def menu_position(): + def create_python_input_window() -> Window: + def menu_position() -> Optional[int]: """ When there is no autocompletion menu to be shown, and we have a signature, set the pop-up position at `bracket_start`. @@ -593,6 +602,7 @@ def menu_position(): row, col = python_input.signatures[0].bracket_start index = b.document.translate_row_col_to_index(row - 1, col) return index + return None return Window( BufferControl( @@ -622,7 +632,7 @@ def menu_position(): processor=AppendAutoSuggestion(), filter=~is_done ), ] - + extra_buffer_processors, + + (extra_buffer_processors or []), menu_position=menu_position, # Make sure that we always see the result of an reverse-i-search: preview_search=True, @@ -654,7 +664,7 @@ def menu_position(): [ FloatContainer( content=HSplit( - [create_python_input_window()] + extra_body + [create_python_input_window()] + extra_body_list ), floats=[ Float( diff --git a/ptpython/python_input.py b/ptpython/python_input.py index 1785f523..c5611179 100644 --- a/ptpython/python_input.py +++ b/ptpython/python_input.py @@ -6,7 +6,18 @@ from asyncio import get_event_loop from functools import partial -from typing import TYPE_CHECKING, Any, Callable, Dict, Generic, List, Optional, TypeVar +from typing import ( + TYPE_CHECKING, + Any, + Callable, + Dict, + Generic, + List, + Mapping, + Optional, + Tuple, + TypeVar, +) from prompt_toolkit.application import Application, get_app from prompt_toolkit.auto_suggest import ( @@ -44,6 +55,7 @@ load_open_in_editor_bindings, ) from prompt_toolkit.key_binding.vi_state import InputMode +from prompt_toolkit.layout.containers import AnyContainer from prompt_toolkit.lexers import DynamicLexer, Lexer, SimpleLexer from prompt_toolkit.output import ColorDepth, Output from prompt_toolkit.styles import ( @@ -88,8 +100,8 @@ def __lt__(self, __other: Any) -> bool: _T = TypeVar("_T", bound="_SupportsLessThan") -class OptionCategory: - def __init__(self, title: str, options: List["Option"]) -> None: +class OptionCategory(Generic[_T]): + def __init__(self, title: str, options: List["Option[_T]"]) -> None: self.title = title self.options = options @@ -113,7 +125,7 @@ def __init__( get_current_value: Callable[[], _T], # We accept `object` as return type for the select functions, because # often they return an unused boolean. Maybe this can be improved. - get_values: Callable[[], Dict[_T, Callable[[], object]]], + get_values: Callable[[], Mapping[_T, Callable[[], object]]], ) -> None: self.title = title self.description = description @@ -121,7 +133,7 @@ def __init__( self.get_values = get_values @property - def values(self) -> Dict[_T, Callable[[], object]]: + def values(self) -> Mapping[_T, Callable[[], object]]: return self.get_values() def activate_next(self, _previous: bool = False) -> None: @@ -192,12 +204,12 @@ def __init__( output: Optional[Output] = None, # For internal use. extra_key_bindings: Optional[KeyBindings] = None, - create_app=True, + create_app: bool = True, _completer: Optional[Completer] = None, _validator: Optional[Validator] = None, _lexer: Optional[Lexer] = None, _extra_buffer_processors=None, - _extra_layout_body=None, + _extra_layout_body: Optional[AnyContainer] = None, _extra_toolbars=None, _input_buffer_height=None, ) -> None: @@ -239,7 +251,7 @@ def __init__( self.history = InMemoryHistory() self._input_buffer_height = _input_buffer_height - self._extra_layout_body = _extra_layout_body or [] + self._extra_layout_body = _extra_layout_body self._extra_toolbars = _extra_toolbars or [] self._extra_buffer_processors = _extra_buffer_processors or [] @@ -388,7 +400,9 @@ def __init__( # Create an app if requested. If not, the global get_app() is returned # for self.app via property getter. if create_app: - self._app: Optional[Application] = self._create_application(input, output) + self._app: Optional[Application[str]] = self._create_application( + input, output + ) # Setting vi_mode will not work unless the prompt_toolkit # application has been created. if vi_mode: @@ -408,7 +422,7 @@ def option_count(self) -> int: return sum(len(category.options) for category in self.options) @property - def selected_option(self) -> Option: + def selected_option(self) -> Option[Any]: "Return the currently selected option." i = 0 for category in self.options: @@ -514,7 +528,7 @@ def _generate_style(self) -> BaseStyle: self.ui_styles[self._current_ui_style_name], ) - def _create_options(self) -> List[OptionCategory]: + def _create_options(self) -> List[OptionCategory[Any]]: """ Create a list of `Option` instances for the options sidebar. """ @@ -530,15 +544,17 @@ def disable(attribute: str) -> bool: return True def simple_option( - title: str, description: str, field_name: str, values: Optional[List] = None - ) -> Option: + title: str, + description: str, + field_name: str, + values: Tuple[str, str] = ("off", "on"), + ) -> Option[str]: "Create Simple on/of option." - values = values or ["off", "on"] - def get_current_value(): + def get_current_value() -> str: return values[bool(getattr(self, field_name))] - def get_values(): + def get_values() -> Dict[str, Callable[[], bool]]: return { values[1]: lambda: enable(field_name), values[0]: lambda: disable(field_name), @@ -848,7 +864,7 @@ def get_values(): def _create_application( self, input: Optional[Input], output: Optional[Output] - ) -> Application: + ) -> Application[str]: """ Create an `Application` instance. """ @@ -926,7 +942,7 @@ def vi_mode(self, value: bool) -> None: self.editing_mode = EditingMode.EMACS @property - def app(self) -> Application: + def app(self) -> Application[str]: if self._app is None: return get_app() return self._app diff --git a/ptpython/repl.py b/ptpython/repl.py index b55b5d56..3c729c0f 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -44,6 +44,7 @@ from .python_input import PythonInput +PyCF_ALLOW_TOP_LEVEL_AWAIT: int try: from ast import PyCF_ALLOW_TOP_LEVEL_AWAIT # type: ignore except ImportError: @@ -90,7 +91,7 @@ def _load_start_paths(self) -> None: output = self.app.output output.write("WARNING | File not found: {}\n\n".format(path)) - def run_and_show_expression(self, expression): + def run_and_show_expression(self, expression: str) -> None: try: # Eval. try: @@ -135,7 +136,7 @@ def run(self) -> None: text = self.read() except EOFError: return - except BaseException as e: + except BaseException: # Something went wrong while reading input. # (E.g., a bug in the completer that propagates. Don't # crash the REPL.) @@ -149,7 +150,7 @@ def run(self) -> None: clear_title() self._remove_from_namespace() - async def run_and_show_expression_async(self, text): + async def run_and_show_expression_async(self, text: str): loop = asyncio.get_event_loop() try: @@ -349,7 +350,7 @@ def _format_result_output(self, result: object) -> StyleAndTextTuples: if not hasattr(black, "Mode"): raise ImportError except ImportError: - pass # no Black package in your installation + pass # no Black package in your installation else: result_repr = black.format_str( result_repr, @@ -725,17 +726,17 @@ def get_locals(): configure(repl) # Start repl. - patch_context: ContextManager = ( + patch_context: ContextManager[None] = ( patch_stdout_context() if patch_stdout else DummyContext() ) if return_asyncio_coroutine: - async def coroutine(): + async def coroutine() -> None: with patch_context: await repl.run_async() - return coroutine() + return coroutine() # type: ignore else: with patch_context: repl.run() diff --git a/ptpython/signatures.py b/ptpython/signatures.py index 228b99b2..e836d33e 100644 --- a/ptpython/signatures.py +++ b/ptpython/signatures.py @@ -8,13 +8,16 @@ import inspect from inspect import Signature as InspectSignature from inspect import _ParameterKind as ParameterKind -from typing import Any, Dict, List, Optional, Sequence, Tuple +from typing import TYPE_CHECKING, Any, Dict, List, Optional, Sequence, Tuple from prompt_toolkit.document import Document from .completer import DictionaryCompleter from .utils import get_jedi_script_from_document +if TYPE_CHECKING: + import jedi.api.classes + __all__ = ["Signature", "get_signatures_using_jedi", "get_signatures_using_eval"] @@ -120,7 +123,9 @@ def get_annotation_name(annotation: object) -> str: ) @classmethod - def from_jedi_signature(cls, signature) -> "Signature": + def from_jedi_signature( + cls, signature: "jedi.api.classes.Signature" + ) -> "Signature": parameters = [] for p in signature.params: diff --git a/ptpython/utils.py b/ptpython/utils.py index 2fb24a41..ef96ca4b 100644 --- a/ptpython/utils.py +++ b/ptpython/utils.py @@ -2,12 +2,31 @@ For internal use only. """ import re -from typing import Callable, Iterable, Type, TypeVar, cast - +from typing import ( + TYPE_CHECKING, + Any, + Callable, + Dict, + Iterable, + Optional, + Type, + TypeVar, + cast, +) + +from prompt_toolkit.document import Document from prompt_toolkit.formatted_text import to_formatted_text from prompt_toolkit.formatted_text.utils import fragment_list_to_text from prompt_toolkit.mouse_events import MouseEvent, MouseEventType +if TYPE_CHECKING: + from jedi import Interpreter + + # See: prompt_toolkit/key_binding/key_bindings.py + # Annotating these return types as `object` is what works best, because + # `NotImplemented` is typed `Any`. + NotImplementedOrNone = object + __all__ = [ "has_unclosed_brackets", "get_jedi_script_from_document", @@ -45,7 +64,9 @@ def has_unclosed_brackets(text: str) -> bool: return False -def get_jedi_script_from_document(document, locals, globals): +def get_jedi_script_from_document( + document: Document, locals: Dict[str, Any], globals: Dict[str, Any] +) -> "Interpreter": import jedi # We keep this import in-line, to improve start-up time. # Importing Jedi is 'slow'. @@ -78,7 +99,7 @@ def get_jedi_script_from_document(document, locals, globals): _multiline_string_delims = re.compile("""[']{3}|["]{3}""") -def document_is_multiline_python(document): +def document_is_multiline_python(document: Document) -> bool: """ Determine whether this is a multiline Python document. """ @@ -133,7 +154,7 @@ def if_mousedown(handler: _T) -> _T: by the Window.) """ - def handle_if_mouse_down(mouse_event: MouseEvent): + def handle_if_mouse_down(mouse_event: MouseEvent) -> "NotImplementedOrNone": if mouse_event.event_type == MouseEventType.MOUSE_DOWN: return handler(mouse_event) else: @@ -142,7 +163,7 @@ def handle_if_mouse_down(mouse_event: MouseEvent): return cast(_T, handle_if_mouse_down) -_T_type = TypeVar("_T_type", bound=Type) +_T_type = TypeVar("_T_type", bound=type) def ptrepr_to_repr(cls: _T_type) -> _T_type: @@ -154,7 +175,8 @@ def ptrepr_to_repr(cls: _T_type) -> _T_type: "@ptrepr_to_repr can only be applied to classes that have a `__pt_repr__` method." ) - def __repr__(self) -> str: + def __repr__(self: object) -> str: + assert hasattr(cls, "__pt_repr__") return fragment_list_to_text(to_formatted_text(cls.__pt_repr__(self))) cls.__repr__ = __repr__ # type:ignore diff --git a/ptpython/validator.py b/ptpython/validator.py index 0f6a4eaf..ffac5839 100644 --- a/ptpython/validator.py +++ b/ptpython/validator.py @@ -1,3 +1,6 @@ +from typing import Callable, Optional + +from prompt_toolkit.document import Document from prompt_toolkit.validation import ValidationError, Validator from .utils import unindent_code @@ -13,10 +16,10 @@ class PythonValidator(Validator): active compiler flags. """ - def __init__(self, get_compiler_flags=None): + def __init__(self, get_compiler_flags: Optional[Callable[[], int]] = None) -> None: self.get_compiler_flags = get_compiler_flags - def validate(self, document): + def validate(self, document: Document) -> None: """ Check input for Python syntax errors. """ @@ -45,7 +48,7 @@ def validate(self, document): # fixed in Python 3.) # TODO: This is not correct if indentation was removed. index = document.translate_row_col_to_index( - e.lineno - 1, (e.offset or 1) - 1 + (e.lineno or 1) - 1, (e.offset or 1) - 1 ) raise ValidationError(index, f"Syntax Error: {e}") except TypeError as e: diff --git a/setup.cfg b/setup.cfg index 3c6e79cf..80dfec6a 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,2 +1,41 @@ [bdist_wheel] universal=1 + +[flake8] +exclude=__init__.py +max_line_length=150 +ignore= + E114, + E116, + E117, + E121, + E122, + E123, + E125, + E126, + E127, + E128, + E131, + E171, + E203, + E211, + E221, + E227, + E231, + E241, + E251, + E301, + E402, + E501, + E701, + E702, + E704, + E731, + E741, + F401, + F403, + F405, + F811, + W503, + W504, + E722 From 1b7652d3f2fd35ea96789df60063516503811e68 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Tue, 22 Nov 2022 09:10:12 +0000 Subject: [PATCH 153/220] Call Filter super() in PythonInputFilter. --- ptpython/filters.py | 1 + 1 file changed, 1 insertion(+) diff --git a/ptpython/filters.py b/ptpython/filters.py index 1adac135..be85edf7 100644 --- a/ptpython/filters.py +++ b/ptpython/filters.py @@ -10,6 +10,7 @@ class PythonInputFilter(Filter): def __init__(self, python_input: "PythonInput") -> None: + super().__init__() self.python_input = python_input def __call__(self) -> bool: From d387b8e559803402105fe25e2c6d41f1beb583c8 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Tue, 22 Nov 2022 09:11:22 +0000 Subject: [PATCH 154/220] Update test.yaml workflow. Test on 3.10 and skip mypy on 3.6. --- .github/workflows/test.yaml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 0368ba7b..ef806cff 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -10,7 +10,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: [3.6, 3.7, 3.8, 3.9] + python-version: [3.6, 3.7, 3.8, 3.9, "3.10"] steps: - uses: actions/checkout@v2 @@ -30,6 +30,7 @@ jobs: mypy ptpython isort -c --profile black ptpython examples setup.py black --check ptpython examples setup.py + if: matrix.python-version != '3.6' - name: Run Tests run: | ./tests/run_tests.py From 8bbdc53179085371d4f88380ecb6273e86e6630d Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Fri, 25 Nov 2022 13:56:44 +0000 Subject: [PATCH 155/220] Make ptipython respect more config changes See: https://github.com/prompt-toolkit/ptpython/pull/110 --- ptpython/ipython.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/ptpython/ipython.py b/ptpython/ipython.py index 9eafa995..db2a2049 100644 --- a/ptpython/ipython.py +++ b/ptpython/ipython.py @@ -277,6 +277,25 @@ def initialize_extensions(shell, extensions): shell.showtraceback() +def run_exec_lines(shell, exec_lines): + """ + Partial copy of run_exec_lines code from IPython.core.shellapp . + """ + try: + iter(exec_lines) + except TypeError: + pass + else: + try: + for line in exec_lines: + try: + shell.run_cell(line, store_history=False) + except: + shell.showtraceback() + except: + shell.showtraceback() + + def embed(**kwargs): """ Copied from `IPython/terminal/embed.py`, but using our `InteractiveShellEmbed` instead. @@ -290,6 +309,7 @@ def embed(**kwargs): kwargs["config"] = config shell = InteractiveShellEmbed.instance(**kwargs) initialize_extensions(shell, config["InteractiveShellApp"]["extensions"]) + run_exec_lines(shell, config["InteractiveShellApp"]["exec_lines"]) run_startup_scripts(shell) shell(header=header, stack_depth=2, compile_flags=compile_flags) From 100f4ae839e94dec1170523700e569058ca36aac Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Fri, 25 Nov 2022 14:50:21 +0000 Subject: [PATCH 156/220] Release 3.0.21 --- CHANGELOG | 16 ++++++++++++++++ setup.py | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 69a95e7d..ebc39c9c 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,22 @@ CHANGELOG ========= +3.0.21: 2022-11-25 +------------------ + +New features: +- Make ptipython respect more config changes. + (See: https://github.com/prompt-toolkit/ptpython/pull/110 ) +- Improved performance of `DictionaryCompleter` for slow mappings. + +Fixes: +- Call `super()` in `PythonInputFilter`. This will prevent potentially breakage + with an upcoming prompt_toolkit change. + (See: https://github.com/prompt-toolkit/python-prompt-toolkit/pull/1690 ) +- Improved type annotations. +- Added `py.typed` to the `package_data`. + + 3.0.20: 2021-09-14 ------------------ diff --git a/setup.py b/setup.py index a8214f27..274be8ee 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ setup( name="ptpython", author="Jonathan Slenders", - version="3.0.20", + version="3.0.21", url="https://github.com/prompt-toolkit/ptpython", description="Python REPL build on top of prompt_toolkit", long_description=long_description, From b7205ac5657e0edf8a5877a1381a03beb66b9193 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Tue, 6 Dec 2022 20:37:12 +0000 Subject: [PATCH 157/220] Improve rendering performance when there are many completions. (Make computing the "meta" text for the completion menu lazy.) --- ptpython/completer.py | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/ptpython/completer.py b/ptpython/completer.py index 2b6795d4..9252106e 100644 --- a/ptpython/completer.py +++ b/ptpython/completer.py @@ -476,14 +476,22 @@ def _get_item_lookup_completions( Complete dictionary keys. """ - def abbr_meta(text: str) -> str: + def meta_repr(value: object) -> Callable[[], str]: "Abbreviate meta text, make sure it fits on one line." - # Take first line, if multiple lines. - if len(text) > 20: - text = text[:20] + "..." - if "\n" in text: - text = text.split("\n", 1)[0] + "..." - return text + # We return a function, so that it gets computed when it's needed. + # When there are many completions, that improves the performance + # quite a bit (for the multi-column completion menu, we only need + # to display one meta text). + def get_value_repr() -> str: + text = self._do_repr(value) + + # Take first line, if multiple lines. + if "\n" in text: + text = text.split("\n", 1)[0] + "..." + + return text + + return get_value_repr match = self.item_lookup_pattern.search(document.text_before_cursor) if match is not None: @@ -512,12 +520,8 @@ def abbr_meta(text: str) -> str: k_repr + "]", -len(key), display=f"[{k_repr}]", - display_meta=abbr_meta(self._do_repr(v)), + display_meta=meta_repr(v), ) - except KeyError: - # `result[k]` lookup failed. Trying to complete - # broken object. - pass except ReprFailedError: pass @@ -532,7 +536,7 @@ def abbr_meta(text: str) -> str: k_repr + "]", -len(key), display=f"[{k_repr}]", - display_meta=abbr_meta(self._do_repr(result[k])), + display_meta=meta_repr(result[k]), ) except KeyError: # `result[k]` lookup failed. Trying to complete From d34704775faa5cd0926cfce9a4dcf3c26d0a178a Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Tue, 6 Dec 2022 20:56:40 +0000 Subject: [PATCH 158/220] Remove Python 3.6 from GitHub workflow (not supported anymore). --- .github/workflows/test.yaml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index ef806cff..7ec86626 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -10,7 +10,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: [3.6, 3.7, 3.8, 3.9, "3.10"] + python-version: [3.7, 3.8, 3.9, "3.10"] steps: - uses: actions/checkout@v2 @@ -23,14 +23,12 @@ jobs: sudo apt remove python3-pip python -m pip install --upgrade pip python -m pip install . black isort mypy pytest readme_renderer - python -m pip install . types-dataclasses # Needed for Python 3.6 pip list - name: Type Checker run: | mypy ptpython isort -c --profile black ptpython examples setup.py black --check ptpython examples setup.py - if: matrix.python-version != '3.6' - name: Run Tests run: | ./tests/run_tests.py From b6fbf018ce252cb36dd296f5c93cdeb633c7acf1 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Tue, 6 Dec 2022 22:18:45 +0000 Subject: [PATCH 159/220] Release 3.0.22 --- CHANGELOG | 7 +++++++ setup.py | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index ebc39c9c..916a5422 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,13 @@ CHANGELOG ========= +3.0.22: 2022-12-06 +------------------ + +New features: +- Improve rendering performance when there are many completions. + + 3.0.21: 2022-11-25 ------------------ diff --git a/setup.py b/setup.py index 274be8ee..2725dac4 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ setup( name="ptpython", author="Jonathan Slenders", - version="3.0.21", + version="3.0.22", url="https://github.com/prompt-toolkit/ptpython", description="Python REPL build on top of prompt_toolkit", long_description=long_description, From 7a6b54026611d5ae9f6730cc476dceb79911654d Mon Sep 17 00:00:00 2001 From: Jongwook Choi Date: Wed, 7 Dec 2022 18:31:16 -0500 Subject: [PATCH 160/220] Fix documentation to correct ptpython.ipython import Fixes #506 --- README.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 15464ba4..2db3f695 100644 --- a/README.rst +++ b/README.rst @@ -213,7 +213,7 @@ This is also available for embedding: .. code:: python - from ptpython.ipython.repl import embed + from ptpython.ipython import embed embed(globals(), locals()) From af89ce2e82b09132daa3f6a62961e98d1105fbb3 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Tue, 21 Feb 2023 14:51:53 +0000 Subject: [PATCH 161/220] Fix code formatting (new Black version). --- ptpython/completer.py | 5 +---- ptpython/python_input.py | 2 +- ptpython/repl.py | 3 ++- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/ptpython/completer.py b/ptpython/completer.py index 9252106e..95383aaf 100644 --- a/ptpython/completer.py +++ b/ptpython/completer.py @@ -189,7 +189,6 @@ def get_completions( ): # If we are inside a string, Don't do Jedi completion. if not self._path_completer_grammar.match(document.text_before_cursor): - # Do Jedi Python completions. yield from self._jedi_completer.get_completions( document, complete_event @@ -399,7 +398,6 @@ def _lookup(self, expression: str, temp_locals: Dict[str, Any]) -> object: def get_completions( self, document: Document, complete_event: CompleteEvent ) -> Iterable[Completion]: - # First, find all for-loops, and assign the first item of the # collections they're iterating to the iterator variable, so that we # can provide code completion on the iterators. @@ -454,7 +452,6 @@ def _get_expression_completions( result = self.eval_expression(document, temp_locals) if result is not None: - if isinstance( result, (list, tuple, dict, collections_abc.Mapping, collections_abc.Sequence), @@ -478,6 +475,7 @@ def _get_item_lookup_completions( def meta_repr(value: object) -> Callable[[], str]: "Abbreviate meta text, make sure it fits on one line." + # We return a function, so that it gets computed when it's needed. # When there are many completions, that improves the performance # quite a bit (for the multi-column completion menu, we only need @@ -617,7 +615,6 @@ def __init__( def get_completions( self, document: Document, complete_event: CompleteEvent ) -> Iterable[Completion]: - completions = list(self.completer.get_completions(document, complete_event)) complete_private_attributes = self.complete_private_attributes() hide_private = False diff --git a/ptpython/python_input.py b/ptpython/python_input.py index c5611179..1a766c46 100644 --- a/ptpython/python_input.py +++ b/ptpython/python_input.py @@ -213,7 +213,6 @@ def __init__( _extra_toolbars=None, _input_buffer_height=None, ) -> None: - self.get_globals: _GetNamespace = get_globals or (lambda: {}) self.get_locals: _GetNamespace = get_locals or self.get_globals @@ -1043,6 +1042,7 @@ def read(self) -> str: This can raise EOFError, when Control-D is pressed. """ + # 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. diff --git a/ptpython/repl.py b/ptpython/repl.py index 3c729c0f..604d2b4a 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -96,7 +96,8 @@ def run_and_show_expression(self, expression: str) -> None: # Eval. try: result = self.eval(expression) - except KeyboardInterrupt: # KeyboardInterrupt doesn't inherit from Exception. + except KeyboardInterrupt: + # KeyboardInterrupt doesn't inherit from Exception. raise except SystemExit: raise From 1720189d9870a1059eacf7499b042c5f1ee5cf8d Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Tue, 21 Feb 2023 13:57:28 +0000 Subject: [PATCH 162/220] Don't print exception twice in exception handler. The exception formatting itself already prints the exception message. Printing the exception again leads to lots of duplicated output if the exception contains a long multiline message. --- ptpython/repl.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/ptpython/repl.py b/ptpython/repl.py index 604d2b4a..342852ff 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -573,8 +573,6 @@ def _handle_exception(self, e: BaseException) -> None: include_default_pygments_style=False, output=output, ) - - output.write("%s\n" % e) output.flush() def _handle_keyboard_interrupt(self, e: KeyboardInterrupt) -> None: From ec697aa4983085bbfa0cd0bfa78722f6cd1ff5b9 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Tue, 21 Feb 2023 16:33:17 +0000 Subject: [PATCH 163/220] Add Python 3.11 to GitHub workflow. --- .github/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 7ec86626..31837db3 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -10,7 +10,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: [3.7, 3.8, 3.9, "3.10"] + python-version: [3.7, 3.8, 3.9, "3.10", "3.11"] steps: - uses: actions/checkout@v2 From 3f24501e6e52a10669acf9c37d4dbdee24a00266 Mon Sep 17 00:00:00 2001 From: Itay R <0xItx@users.noreply.github.com> Date: Wed, 22 Feb 2023 12:20:28 +0200 Subject: [PATCH 164/220] Add macOS path to config.py's docstring (#501) --- examples/ptpython_config/config.py | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/ptpython_config/config.py b/examples/ptpython_config/config.py index bf9d05fe..2b51dfc4 100644 --- a/examples/ptpython_config/config.py +++ b/examples/ptpython_config/config.py @@ -3,6 +3,7 @@ Copy this file to $XDG_CONFIG_HOME/ptpython/config.py On Linux, this is: ~/.config/ptpython/config.py +On macOS, this is: ~/Library/Application Support/ptpython/config.py """ from prompt_toolkit.filters import ViInsertMode from prompt_toolkit.key_binding.key_processor import KeyPress From be972abf8d37f0cdc5553a945ef2d22bda4341b1 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Tue, 21 Feb 2023 16:46:09 +0000 Subject: [PATCH 165/220] Drop Python 3.6. Now that prompt_toolkit itself dropped Python 3.6 support, we can drop Python 3.6 too. --- ptpython/__init__.py | 2 + ptpython/__main__.py | 2 + ptpython/completer.py | 40 ++++++++-------- ptpython/contrib/asyncssh_repl.py | 4 +- ptpython/entry_points/run_ptipython.py | 4 +- ptpython/entry_points/run_ptpython.py | 6 ++- ptpython/eventloop.py | 2 + ptpython/filters.py | 4 +- ptpython/history_browser.py | 32 ++++++------- ptpython/ipython.py | 6 ++- ptpython/key_bindings.py | 8 ++-- ptpython/layout.py | 46 +++++++++--------- ptpython/lexer.py | 4 +- ptpython/prompt_style.py | 4 +- ptpython/python_input.py | 65 ++++++++++++++------------ ptpython/repl.py | 20 ++++---- ptpython/signatures.py | 24 +++++----- ptpython/style.py | 8 ++-- ptpython/utils.py | 8 ++-- ptpython/validator.py | 4 +- setup.py | 4 +- tests/run_tests.py | 2 + 22 files changed, 169 insertions(+), 130 deletions(-) diff --git a/ptpython/__init__.py b/ptpython/__init__.py index 4908eba8..63c6233d 100644 --- a/ptpython/__init__.py +++ b/ptpython/__init__.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from .repl import embed __all__ = ["embed"] diff --git a/ptpython/__main__.py b/ptpython/__main__.py index 83340a7b..c0062613 100644 --- a/ptpython/__main__.py +++ b/ptpython/__main__.py @@ -1,6 +1,8 @@ """ Make `python -m ptpython` an alias for running `./ptpython`. """ +from __future__ import annotations + from .entry_points.run_ptpython import run run() diff --git a/ptpython/completer.py b/ptpython/completer.py index 95383aaf..f610916e 100644 --- a/ptpython/completer.py +++ b/ptpython/completer.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import ast import collections.abc as collections_abc import inspect @@ -44,8 +46,8 @@ class PythonCompleter(Completer): def __init__( self, - get_globals: Callable[[], Dict[str, Any]], - get_locals: Callable[[], Dict[str, Any]], + get_globals: Callable[[], dict[str, Any]], + get_locals: Callable[[], dict[str, Any]], enable_dictionary_completion: Callable[[], bool], ) -> None: super().__init__() @@ -58,8 +60,8 @@ def __init__( self._jedi_completer = JediCompleter(get_globals, get_locals) self._dictionary_completer = DictionaryCompleter(get_globals, get_locals) - self._path_completer_cache: Optional[GrammarCompleter] = None - self._path_completer_grammar_cache: Optional["_CompiledGrammar"] = None + self._path_completer_cache: GrammarCompleter | None = None + self._path_completer_grammar_cache: _CompiledGrammar | None = None @property def _path_completer(self) -> GrammarCompleter: @@ -74,7 +76,7 @@ def _path_completer(self) -> GrammarCompleter: return self._path_completer_cache @property - def _path_completer_grammar(self) -> "_CompiledGrammar": + def _path_completer_grammar(self) -> _CompiledGrammar: """ Return the grammar for matching paths inside strings inside Python code. @@ -85,7 +87,7 @@ def _path_completer_grammar(self) -> "_CompiledGrammar": self._path_completer_grammar_cache = self._create_path_completer_grammar() return self._path_completer_grammar_cache - def _create_path_completer_grammar(self) -> "_CompiledGrammar": + def _create_path_completer_grammar(self) -> _CompiledGrammar: def unwrapper(text: str) -> str: return re.sub(r"\\(.)", r"\1", text) @@ -202,8 +204,8 @@ class JediCompleter(Completer): def __init__( self, - get_globals: Callable[[], Dict[str, Any]], - get_locals: Callable[[], Dict[str, Any]], + get_globals: Callable[[], dict[str, Any]], + get_locals: Callable[[], dict[str, Any]], ) -> None: super().__init__() @@ -241,7 +243,7 @@ def get_completions( # Jedi issue: "KeyError: u'a_lambda'." # https://github.com/jonathanslenders/ptpython/issues/89 pass - except IOError: + except OSError: # Jedi issue: "IOError: No such file or directory." # https://github.com/jonathanslenders/ptpython/issues/71 pass @@ -302,8 +304,8 @@ class DictionaryCompleter(Completer): def __init__( self, - get_globals: Callable[[], Dict[str, Any]], - get_locals: Callable[[], Dict[str, Any]], + get_globals: Callable[[], dict[str, Any]], + get_locals: Callable[[], dict[str, Any]], ) -> None: super().__init__() @@ -385,7 +387,7 @@ def __init__( re.VERBOSE, ) - def _lookup(self, expression: str, temp_locals: Dict[str, Any]) -> object: + def _lookup(self, expression: str, temp_locals: dict[str, Any]) -> object: """ Do lookup of `object_var` in the context. `temp_locals` is a dictionary, used for the locals. @@ -429,7 +431,7 @@ def _do_repr(self, obj: object) -> str: except BaseException: raise ReprFailedError - def eval_expression(self, document: Document, locals: Dict[str, Any]) -> object: + def eval_expression(self, document: Document, locals: dict[str, Any]) -> object: """ Evaluate """ @@ -444,7 +446,7 @@ def _get_expression_completions( self, document: Document, complete_event: CompleteEvent, - temp_locals: Dict[str, Any], + temp_locals: dict[str, Any], ) -> Iterable[Completion]: """ Complete the [ or . operator after an object. @@ -467,7 +469,7 @@ def _get_item_lookup_completions( self, document: Document, complete_event: CompleteEvent, - temp_locals: Dict[str, Any], + temp_locals: dict[str, Any], ) -> Iterable[Completion]: """ Complete dictionary keys. @@ -547,7 +549,7 @@ def _get_attribute_completions( self, document: Document, complete_event: CompleteEvent, - temp_locals: Dict[str, Any], + temp_locals: dict[str, Any], ) -> Iterable[Completion]: """ Complete attribute names. @@ -579,13 +581,13 @@ def get_suffix(name: str) -> str: suffix = get_suffix(name) yield Completion(name, -len(attr_name), display=name + suffix) - def _sort_attribute_names(self, names: List[str]) -> List[str]: + def _sort_attribute_names(self, names: list[str]) -> list[str]: """ Sort attribute names alphabetically, but move the double underscore and underscore names to the end. """ - def sort_key(name: str) -> Tuple[int, str]: + def sort_key(name: str) -> tuple[int, str]: if name.startswith("__"): return (2, name) # Double underscore comes latest. if name.startswith("_"): @@ -650,7 +652,7 @@ class ReprFailedError(Exception): def _get_style_for_jedi_completion( - jedi_completion: "jedi.api.classes.Completion", + jedi_completion: jedi.api.classes.Completion, ) -> str: """ Return completion style to use for this name. diff --git a/ptpython/contrib/asyncssh_repl.py b/ptpython/contrib/asyncssh_repl.py index 4c36217d..0347aded 100644 --- a/ptpython/contrib/asyncssh_repl.py +++ b/ptpython/contrib/asyncssh_repl.py @@ -6,6 +6,8 @@ should make sure not to use Python 3-only syntax, because this package should be installable in Python 2 as well! """ +from __future__ import annotations + import asyncio from typing import Any, Optional, TextIO, cast @@ -29,7 +31,7 @@ class ReplSSHServerSession(asyncssh.SSHServerSession): """ def __init__( - self, get_globals: _GetNamespace, get_locals: Optional[_GetNamespace] = None + self, get_globals: _GetNamespace, get_locals: _GetNamespace | None = None ) -> None: self._chan: Any = None diff --git a/ptpython/entry_points/run_ptipython.py b/ptpython/entry_points/run_ptipython.py index 21d70637..b660a0ac 100644 --- a/ptpython/entry_points/run_ptipython.py +++ b/ptpython/entry_points/run_ptipython.py @@ -1,4 +1,6 @@ #!/usr/bin/env python +from __future__ import annotations + import os import sys @@ -58,7 +60,7 @@ def run(user_ns=None): code = compile(f.read(), path, "exec") exec(code, user_ns, user_ns) else: - print("File not found: {}\n\n".format(path)) + print(f"File not found: {path}\n\n") sys.exit(1) # Apply config file diff --git a/ptpython/entry_points/run_ptpython.py b/ptpython/entry_points/run_ptpython.py index edffa44d..1b4074d4 100644 --- a/ptpython/entry_points/run_ptpython.py +++ b/ptpython/entry_points/run_ptpython.py @@ -21,6 +21,8 @@ PTPYTHON_CONFIG_HOME: a configuration directory to use PYTHONSTARTUP: file executed on interactive startup (no default) """ +from __future__ import annotations + import argparse import os import pathlib @@ -44,7 +46,7 @@ class _Parser(argparse.ArgumentParser): - def print_help(self, file: Optional[IO[str]] = None) -> None: + def print_help(self, file: IO[str] | None = None) -> None: super().print_help() print( dedent( @@ -90,7 +92,7 @@ def create_parser() -> _Parser: return parser -def get_config_and_history_file(namespace: argparse.Namespace) -> Tuple[str, str]: +def get_config_and_history_file(namespace: argparse.Namespace) -> tuple[str, str]: """ Check which config/history files to use, ensure that the directories for these files exist, and return the config and history path. diff --git a/ptpython/eventloop.py b/ptpython/eventloop.py index 63dd7408..14ab64be 100644 --- a/ptpython/eventloop.py +++ b/ptpython/eventloop.py @@ -7,6 +7,8 @@ in readline. ``prompt-toolkit`` doesn't understand that input hook, but this will fix it for Tk.) """ +from __future__ import annotations + import sys import time diff --git a/ptpython/filters.py b/ptpython/filters.py index be85edf7..a2079fd3 100644 --- a/ptpython/filters.py +++ b/ptpython/filters.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from typing import TYPE_CHECKING from prompt_toolkit.filters import Filter @@ -9,7 +11,7 @@ class PythonInputFilter(Filter): - def __init__(self, python_input: "PythonInput") -> None: + def __init__(self, python_input: PythonInput) -> None: super().__init__() self.python_input = python_input diff --git a/ptpython/history_browser.py b/ptpython/history_browser.py index 08725ee0..81cc63ae 100644 --- a/ptpython/history_browser.py +++ b/ptpython/history_browser.py @@ -4,6 +4,8 @@ `create_history_application` creates an `Application` instance that runs will run as a sub application of the Repl/PythonInput. """ +from __future__ import annotations + from functools import partial from typing import TYPE_CHECKING, Callable, List, Optional, Set @@ -128,7 +130,7 @@ class HistoryLayout: application. """ - def __init__(self, history: "PythonHistory") -> None: + def __init__(self, history: PythonHistory) -> None: search_toolbar = SearchToolbar() self.help_buffer_control = BufferControl( @@ -224,7 +226,7 @@ def _get_top_toolbar_fragments() -> StyleAndTextTuples: return [("class:status-bar.title", "History browser - Insert from history")] -def _get_bottom_toolbar_fragments(history: "PythonHistory") -> StyleAndTextTuples: +def _get_bottom_toolbar_fragments(history: PythonHistory) -> StyleAndTextTuples: python_input = history.python_input @if_mousedown @@ -258,7 +260,7 @@ class HistoryMargin(Margin): This displays a green bar for the selected entries. """ - def __init__(self, history: "PythonHistory") -> None: + def __init__(self, history: PythonHistory) -> None: self.history_buffer = history.history_buffer self.history_mapping = history.history_mapping @@ -307,7 +309,7 @@ class ResultMargin(Margin): The margin to be shown in the result pane. """ - def __init__(self, history: "PythonHistory") -> None: + def __init__(self, history: PythonHistory) -> None: self.history_mapping = history.history_mapping self.history_buffer = history.history_buffer @@ -356,7 +358,7 @@ class GrayExistingText(Processor): Turn the existing input, before and after the inserted code gray. """ - def __init__(self, history_mapping: "HistoryMapping") -> None: + def __init__(self, history_mapping: HistoryMapping) -> None: self.history_mapping = history_mapping self._lines_before = len( history_mapping.original_document.text_before_cursor.splitlines() @@ -384,7 +386,7 @@ class HistoryMapping: def __init__( self, - history: "PythonHistory", + history: PythonHistory, python_history: History, original_document: Document, ) -> None: @@ -393,11 +395,11 @@ def __init__( self.original_document = original_document self.lines_starting_new_entries = set() - self.selected_lines: Set[int] = set() + self.selected_lines: set[int] = set() # Process history. history_strings = python_history.get_strings() - history_lines: List[str] = [] + history_lines: list[str] = [] for entry_nr, entry in list(enumerate(history_strings))[-HISTORY_COUNT:]: self.lines_starting_new_entries.add(len(history_lines)) @@ -419,7 +421,7 @@ def __init__( else: self.result_line_offset = 0 - def get_new_document(self, cursor_pos: Optional[int] = None) -> Document: + def get_new_document(self, cursor_pos: int | None = None) -> Document: """ Create a `Document` instance that contains the resulting text. """ @@ -449,7 +451,7 @@ def update_default_buffer(self) -> None: b.set_document(self.get_new_document(b.cursor_position), bypass_readonly=True) -def _toggle_help(history: "PythonHistory") -> None: +def _toggle_help(history: PythonHistory) -> None: "Display/hide help." help_buffer_control = history.history_layout.help_buffer_control @@ -459,7 +461,7 @@ def _toggle_help(history: "PythonHistory") -> None: history.app.layout.current_control = help_buffer_control -def _select_other_window(history: "PythonHistory") -> None: +def _select_other_window(history: PythonHistory) -> None: "Toggle focus between left/right window." current_buffer = history.app.current_buffer layout = history.history_layout.layout @@ -472,8 +474,8 @@ def _select_other_window(history: "PythonHistory") -> None: def create_key_bindings( - history: "PythonHistory", - python_input: "PythonInput", + history: PythonHistory, + python_input: PythonInput, history_mapping: HistoryMapping, ) -> KeyBindings: """ @@ -592,9 +594,7 @@ def _(event: E) -> None: class PythonHistory: - def __init__( - self, python_input: "PythonInput", original_document: Document - ) -> None: + def __init__(self, python_input: PythonInput, original_document: Document) -> None: """ Create an `Application` for the history screen. This has to be run as a sub application of `python_input`. diff --git a/ptpython/ipython.py b/ptpython/ipython.py index db2a2049..fb4b5ed9 100644 --- a/ptpython/ipython.py +++ b/ptpython/ipython.py @@ -8,6 +8,8 @@ offer. """ +from __future__ import annotations + from typing import Iterable from warnings import warn @@ -62,12 +64,12 @@ def out_prompt(self) -> AnyFormattedText: class IPythonValidator(PythonValidator): def __init__(self, *args, **kwargs): - super(IPythonValidator, self).__init__(*args, **kwargs) + super().__init__(*args, **kwargs) self.isp = IPythonInputSplitter() def validate(self, document: Document) -> None: document = Document(text=self.isp.transform_cell(document.text)) - super(IPythonValidator, self).validate(document) + super().validate(document) def create_ipython_grammar(): diff --git a/ptpython/key_bindings.py b/ptpython/key_bindings.py index 147a321d..6b4c1862 100644 --- a/ptpython/key_bindings.py +++ b/ptpython/key_bindings.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from typing import TYPE_CHECKING from prompt_toolkit.application import get_app @@ -47,7 +49,7 @@ def tab_should_insert_whitespace() -> bool: return bool(b.text and (not before_cursor or before_cursor.isspace())) -def load_python_bindings(python_input: "PythonInput") -> KeyBindings: +def load_python_bindings(python_input: PythonInput) -> KeyBindings: """ Custom key bindings. """ @@ -218,7 +220,7 @@ def _(event: E) -> None: return bindings -def load_sidebar_bindings(python_input: "PythonInput") -> KeyBindings: +def load_sidebar_bindings(python_input: PythonInput) -> KeyBindings: """ Load bindings for the navigation in the sidebar. """ @@ -273,7 +275,7 @@ def _(event: E) -> None: return bindings -def load_confirm_exit_bindings(python_input: "PythonInput") -> KeyBindings: +def load_confirm_exit_bindings(python_input: PythonInput) -> KeyBindings: """ Handle yes/no key presses when the exit confirmation is shown. """ diff --git a/ptpython/layout.py b/ptpython/layout.py index 365f381b..2c6395ce 100644 --- a/ptpython/layout.py +++ b/ptpython/layout.py @@ -1,6 +1,8 @@ """ Creation of the `Layout` instance for the Python input/REPL. """ +from __future__ import annotations + import platform import sys from enum import Enum @@ -78,26 +80,26 @@ class CompletionVisualisation(Enum): TOOLBAR = "toolbar" -def show_completions_toolbar(python_input: "PythonInput") -> Condition: +def show_completions_toolbar(python_input: PythonInput) -> Condition: return Condition( lambda: python_input.completion_visualisation == CompletionVisualisation.TOOLBAR ) -def show_completions_menu(python_input: "PythonInput") -> Condition: +def show_completions_menu(python_input: PythonInput) -> Condition: return Condition( lambda: python_input.completion_visualisation == CompletionVisualisation.POP_UP ) -def show_multi_column_completions_menu(python_input: "PythonInput") -> Condition: +def show_multi_column_completions_menu(python_input: PythonInput) -> Condition: return Condition( lambda: python_input.completion_visualisation == CompletionVisualisation.MULTI_COLUMN ) -def python_sidebar(python_input: "PythonInput") -> Window: +def python_sidebar(python_input: PythonInput) -> Window: """ Create the `Layout` for the sidebar with the configurable options. """ @@ -105,7 +107,7 @@ def python_sidebar(python_input: "PythonInput") -> Window: def get_text_fragments() -> StyleAndTextTuples: tokens: StyleAndTextTuples = [] - def append_category(category: "OptionCategory[Any]") -> None: + def append_category(category: OptionCategory[Any]) -> None: tokens.extend( [ ("class:sidebar", " "), @@ -172,7 +174,7 @@ def move_cursor_up(self) -> None: ) -def python_sidebar_navigation(python_input: "PythonInput") -> Window: +def python_sidebar_navigation(python_input: PythonInput) -> Window: """ Create the `Layout` showing the navigation information for the sidebar. """ @@ -198,7 +200,7 @@ def get_text_fragments() -> StyleAndTextTuples: ) -def python_sidebar_help(python_input: "PythonInput") -> Container: +def python_sidebar_help(python_input: PythonInput) -> Container: """ Create the `Layout` for the help text for the current item in the sidebar. """ @@ -232,7 +234,7 @@ def get_help_text() -> StyleAndTextTuples: ) -def signature_toolbar(python_input: "PythonInput") -> Container: +def signature_toolbar(python_input: PythonInput) -> Container: """ Return the `Layout` for the signature. """ @@ -318,7 +320,7 @@ class PythonPromptMargin(PromptMargin): It shows something like "In [1]:". """ - def __init__(self, python_input: "PythonInput") -> None: + def __init__(self, python_input: PythonInput) -> None: self.python_input = python_input def get_prompt_style() -> PromptStyle: @@ -339,7 +341,7 @@ def get_continuation( super().__init__(get_prompt, get_continuation) -def status_bar(python_input: "PythonInput") -> Container: +def status_bar(python_input: PythonInput) -> Container: """ Create the `Layout` for the status bar. """ @@ -412,7 +414,7 @@ def get_text_fragments() -> StyleAndTextTuples: ) -def get_inputmode_fragments(python_input: "PythonInput") -> StyleAndTextTuples: +def get_inputmode_fragments(python_input: PythonInput) -> StyleAndTextTuples: """ Return current input mode as a list of (token, text) tuples for use in a toolbar. @@ -440,7 +442,7 @@ def toggle_vi_mode(mouse_event: MouseEvent) -> None: recording_register = app.vi_state.recording_register if recording_register: append((token, " ")) - append((token + " class:record", "RECORD({})".format(recording_register))) + append((token + " class:record", f"RECORD({recording_register})")) append((token, " - ")) if app.current_buffer.selection_state is not None: @@ -473,7 +475,7 @@ def toggle_vi_mode(mouse_event: MouseEvent) -> None: return result -def show_sidebar_button_info(python_input: "PythonInput") -> Container: +def show_sidebar_button_info(python_input: PythonInput) -> Container: """ Create `Layout` for the information in the right-bottom corner. (The right part of the status bar.) @@ -519,7 +521,7 @@ def get_text_fragments() -> StyleAndTextTuples: def create_exit_confirmation( - python_input: "PythonInput", style: str = "class:exit-confirmation" + python_input: PythonInput, style: str = "class:exit-confirmation" ) -> Container: """ Create `Layout` for the exit message. @@ -543,7 +545,7 @@ def get_text_fragments() -> StyleAndTextTuples: ) -def meta_enter_message(python_input: "PythonInput") -> Container: +def meta_enter_message(python_input: PythonInput) -> Container: """ Create the `Layout` for the 'Meta+Enter` message. """ @@ -575,15 +577,15 @@ def extra_condition() -> bool: class PtPythonLayout: def __init__( self, - python_input: "PythonInput", + python_input: PythonInput, lexer: Lexer, - extra_body: Optional[AnyContainer] = None, - extra_toolbars: Optional[List[AnyContainer]] = None, - extra_buffer_processors: Optional[List[Processor]] = None, - input_buffer_height: Optional[AnyDimension] = None, + extra_body: AnyContainer | None = None, + extra_toolbars: list[AnyContainer] | None = None, + extra_buffer_processors: list[Processor] | None = None, + input_buffer_height: AnyDimension | None = None, ) -> None: D = Dimension - extra_body_list: List[AnyContainer] = [extra_body] if extra_body else [] + extra_body_list: list[AnyContainer] = [extra_body] if extra_body else [] extra_toolbars = extra_toolbars or [] input_buffer_height = input_buffer_height or D(min=6) @@ -591,7 +593,7 @@ def __init__( search_toolbar = SearchToolbar(python_input.search_buffer) def create_python_input_window() -> Window: - def menu_position() -> Optional[int]: + def menu_position() -> int | None: """ When there is no autocompletion menu to be shown, and we have a signature, set the pop-up position at `bracket_start`. diff --git a/ptpython/lexer.py b/ptpython/lexer.py index 62e470f8..81924c9d 100644 --- a/ptpython/lexer.py +++ b/ptpython/lexer.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from typing import Callable, Optional from prompt_toolkit.document import Document @@ -17,7 +19,7 @@ class PtpythonLexer(Lexer): use a Python 3 lexer. """ - def __init__(self, python_lexer: Optional[Lexer] = None) -> None: + def __init__(self, python_lexer: Lexer | None = None) -> None: self.python_lexer = python_lexer or PygmentsLexer(PythonLexer) self.system_lexer = PygmentsLexer(BashLexer) diff --git a/ptpython/prompt_style.py b/ptpython/prompt_style.py index e7334af2..96b738f7 100644 --- a/ptpython/prompt_style.py +++ b/ptpython/prompt_style.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from abc import ABCMeta, abstractmethod from typing import TYPE_CHECKING @@ -40,7 +42,7 @@ class IPythonPrompt(PromptStyle): A prompt resembling the IPython prompt. """ - def __init__(self, python_input: "PythonInput") -> None: + def __init__(self, python_input: PythonInput) -> None: self.python_input = python_input def in_prompt(self) -> AnyFormattedText: diff --git a/ptpython/python_input.py b/ptpython/python_input.py index 1a766c46..e8170f2b 100644 --- a/ptpython/python_input.py +++ b/ptpython/python_input.py @@ -2,7 +2,7 @@ Application for reading Python input. This can be used for creation of Python REPLs. """ -import __future__ +from __future__ import annotations from asyncio import get_event_loop from functools import partial @@ -84,6 +84,11 @@ from .utils import unindent_code from .validator import PythonValidator +# Isort introduces a SyntaxError, if we'd write `import __future__`. +# https://github.com/PyCQA/isort/issues/2100 +__future__ = __import__("__future__") + + __all__ = ["PythonInput"] @@ -101,7 +106,7 @@ def __lt__(self, __other: Any) -> bool: class OptionCategory(Generic[_T]): - def __init__(self, title: str, options: List["Option[_T]"]) -> None: + def __init__(self, title: str, options: list[Option[_T]]) -> None: self.title = title self.options = options @@ -194,22 +199,22 @@ class PythonInput: def __init__( self, - get_globals: Optional[_GetNamespace] = None, - get_locals: Optional[_GetNamespace] = None, - history_filename: Optional[str] = None, + get_globals: _GetNamespace | None = None, + get_locals: _GetNamespace | None = None, + history_filename: str | None = None, vi_mode: bool = False, - color_depth: Optional[ColorDepth] = None, + color_depth: ColorDepth | None = None, # Input/output. - input: Optional[Input] = None, - output: Optional[Output] = None, + input: Input | None = None, + output: Output | None = None, # For internal use. - extra_key_bindings: Optional[KeyBindings] = None, + extra_key_bindings: KeyBindings | None = None, create_app: bool = True, - _completer: Optional[Completer] = None, - _validator: Optional[Validator] = None, - _lexer: Optional[Lexer] = None, + _completer: Completer | None = None, + _validator: Validator | None = None, + _lexer: Lexer | None = None, _extra_buffer_processors=None, - _extra_layout_body: Optional[AnyContainer] = None, + _extra_layout_body: AnyContainer | None = None, _extra_toolbars=None, _input_buffer_height=None, ) -> None: @@ -309,7 +314,7 @@ def __init__( self.show_exit_confirmation: bool = False # The title to be displayed in the terminal. (None or string.) - self.terminal_title: Optional[str] = None + self.terminal_title: str | None = None self.exit_message: str = "Do you really want to exit?" self.insert_blank_line_after_output: bool = True # (For the REPL.) @@ -324,7 +329,7 @@ def __init__( self.prompt_style: str = "classic" # The currently active style. # Styles selectable from the menu. - self.all_prompt_styles: Dict[str, PromptStyle] = { + self.all_prompt_styles: dict[str, PromptStyle] = { "ipython": IPythonPrompt(self), "classic": ClassicPrompt(), } @@ -338,7 +343,7 @@ def __init__( ].out_prompt() #: Load styles. - self.code_styles: Dict[str, BaseStyle] = get_all_code_styles() + self.code_styles: dict[str, BaseStyle] = get_all_code_styles() self.ui_styles = get_all_ui_styles() self._current_code_style_name: str = "default" self._current_ui_style_name: str = "default" @@ -360,7 +365,7 @@ def __init__( self.current_statement_index: int = 1 # Code signatures. (This is set asynchronously after a timeout.) - self.signatures: List[Signature] = [] + self.signatures: list[Signature] = [] # Boolean indicating whether we have a signatures thread running. # (Never run more than one at the same time.) @@ -399,9 +404,7 @@ def __init__( # Create an app if requested. If not, the global get_app() is returned # for self.app via property getter. if create_app: - self._app: Optional[Application[str]] = self._create_application( - input, output - ) + self._app: Application[str] | None = self._create_application(input, output) # Setting vi_mode will not work unless the prompt_toolkit # application has been created. if vi_mode: @@ -527,7 +530,7 @@ def _generate_style(self) -> BaseStyle: self.ui_styles[self._current_ui_style_name], ) - def _create_options(self) -> List[OptionCategory[Any]]: + def _create_options(self) -> list[OptionCategory[Any]]: """ Create a list of `Option` instances for the options sidebar. """ @@ -546,14 +549,14 @@ def simple_option( title: str, description: str, field_name: str, - values: Tuple[str, str] = ("off", "on"), + values: tuple[str, str] = ("off", "on"), ) -> Option[str]: "Create Simple on/of option." def get_current_value() -> str: return values[bool(getattr(self, field_name))] - def get_values() -> Dict[str, Callable[[], bool]]: + def get_values() -> dict[str, Callable[[], bool]]: return { values[1]: lambda: enable(field_name), values[0]: lambda: disable(field_name), @@ -730,10 +733,10 @@ def get_values() -> Dict[str, Callable[[], bool]]: title="Prompt", description="Visualisation of the prompt. ('>>>' or 'In [1]:')", get_current_value=lambda: self.prompt_style, - get_values=lambda: dict( - (s, partial(enable, "prompt_style", s)) + get_values=lambda: { + s: partial(enable, "prompt_style", s) for s in self.all_prompt_styles - ), + }, ), simple_option( title="Blank line after input", @@ -825,10 +828,10 @@ def get_values() -> Dict[str, Callable[[], bool]]: title="User interface", description="Color scheme to use for the user interface.", get_current_value=lambda: self._current_ui_style_name, - get_values=lambda: dict( - (name, partial(self.use_ui_colorscheme, name)) + get_values=lambda: { + name: partial(self.use_ui_colorscheme, name) for name in self.ui_styles - ), + }, ), Option( title="Color depth", @@ -862,7 +865,7 @@ def get_values() -> Dict[str, Callable[[], bool]]: ] def _create_application( - self, input: Optional[Input], output: Optional[Output] + self, input: Input | None, output: Output | None ) -> Application[str]: """ Create an `Application` instance. @@ -952,7 +955,7 @@ def _on_input_timeout(self, buff: Buffer) -> None: in another thread, get the signature of the current code. """ - def get_signatures_in_executor(document: Document) -> List[Signature]: + def get_signatures_in_executor(document: Document) -> list[Signature]: # First, get signatures from Jedi. If we didn't found any and if # "dictionary completion" (eval-based completion) is enabled, then # get signatures using eval. diff --git a/ptpython/repl.py b/ptpython/repl.py index 342852ff..a3dd788e 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -7,6 +7,8 @@ embed(globals(), locals(), vi_mode=False) """ +from __future__ import annotations + import asyncio import builtins import os @@ -53,7 +55,7 @@ __all__ = ["PythonRepl", "enable_deprecation_warnings", "run_config", "embed"] -def _get_coroutine_flag() -> Optional[int]: +def _get_coroutine_flag() -> int | None: for k, v in COMPILER_FLAG_NAMES.items(): if v == "COROUTINE": return k @@ -62,7 +64,7 @@ def _get_coroutine_flag() -> Optional[int]: return None -COROUTINE_FLAG: Optional[int] = _get_coroutine_flag() +COROUTINE_FLAG: int | None = _get_coroutine_flag() def _has_coroutine_flag(code: types.CodeType) -> bool: @@ -89,7 +91,7 @@ def _load_start_paths(self) -> None: exec(code, self.get_globals(), self.get_locals()) else: output = self.app.output - output.write("WARNING | File not found: {}\n\n".format(path)) + output.write(f"WARNING | File not found: {path}\n\n") def run_and_show_expression(self, expression: str) -> None: try: @@ -300,7 +302,7 @@ async def eval_async(self, line: str) -> object: return None def _store_eval_result(self, result: object) -> None: - locals: Dict[str, Any] = self.get_locals() + locals: dict[str, Any] = self.get_locals() locals["_"] = locals["_%i" % self.current_statement_index] = result def get_compiler_flags(self) -> int: @@ -524,7 +526,7 @@ def show_pager() -> None: flush_page() - def create_pager_prompt(self) -> PromptSession["PagerResult"]: + def create_pager_prompt(self) -> PromptSession[PagerResult]: """ Create pager --MORE-- prompt. """ @@ -651,7 +653,7 @@ def enter_to_continue() -> None: # Run the config file in an empty namespace. try: - namespace: Dict[str, Any] = {} + namespace: dict[str, Any] = {} with open(config_file, "rb") as f: code = compile(f.read(), config_file, "exec") @@ -670,10 +672,10 @@ def enter_to_continue() -> None: def embed( globals=None, locals=None, - configure: Optional[Callable[[PythonRepl], None]] = None, + configure: Callable[[PythonRepl], None] | None = None, vi_mode: bool = False, - history_filename: Optional[str] = None, - title: Optional[str] = None, + history_filename: str | None = None, + title: str | None = None, startup_paths=None, patch_stdout: bool = False, return_asyncio_coroutine: bool = False, diff --git a/ptpython/signatures.py b/ptpython/signatures.py index e836d33e..5a6f286a 100644 --- a/ptpython/signatures.py +++ b/ptpython/signatures.py @@ -5,6 +5,8 @@ Either with the Jedi library, or using `inspect.signature` if Jedi fails and we can use `eval()` to evaluate the function object. """ +from __future__ import annotations + import inspect from inspect import Signature as InspectSignature from inspect import _ParameterKind as ParameterKind @@ -25,8 +27,8 @@ class Parameter: def __init__( self, name: str, - annotation: Optional[str], - default: Optional[str], + annotation: str | None, + default: str | None, kind: ParameterKind, ) -> None: self.name = name @@ -66,9 +68,9 @@ def __init__( name: str, docstring: str, parameters: Sequence[Parameter], - index: Optional[int] = None, + index: int | None = None, returns: str = "", - bracket_start: Tuple[int, int] = (0, 0), + bracket_start: tuple[int, int] = (0, 0), ) -> None: self.name = name self.docstring = docstring @@ -84,7 +86,7 @@ def from_inspect_signature( docstring: str, signature: InspectSignature, index: int, - ) -> "Signature": + ) -> Signature: parameters = [] def get_annotation_name(annotation: object) -> str: @@ -123,9 +125,7 @@ def get_annotation_name(annotation: object) -> str: ) @classmethod - def from_jedi_signature( - cls, signature: "jedi.api.classes.Signature" - ) -> "Signature": + def from_jedi_signature(cls, signature: jedi.api.classes.Signature) -> Signature: parameters = [] for p in signature.params: @@ -160,8 +160,8 @@ def __repr__(self) -> str: def get_signatures_using_jedi( - document: Document, locals: Dict[str, Any], globals: Dict[str, Any] -) -> List[Signature]: + document: Document, locals: dict[str, Any], globals: dict[str, Any] +) -> list[Signature]: script = get_jedi_script_from_document(document, locals, globals) # Show signatures in help text. @@ -195,8 +195,8 @@ def get_signatures_using_jedi( def get_signatures_using_eval( - document: Document, locals: Dict[str, Any], globals: Dict[str, Any] -) -> List[Signature]: + document: Document, locals: dict[str, Any], globals: dict[str, Any] +) -> list[Signature]: """ Look for the signature of the function before the cursor position without use of Jedi. This uses a similar approach as the `DictionaryCompleter` of diff --git a/ptpython/style.py b/ptpython/style.py index 4b54d0cd..199d5abf 100644 --- a/ptpython/style.py +++ b/ptpython/style.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from typing import Dict from prompt_toolkit.styles import BaseStyle, Style, merge_styles @@ -8,11 +10,11 @@ __all__ = ["get_all_code_styles", "get_all_ui_styles", "generate_style"] -def get_all_code_styles() -> Dict[str, BaseStyle]: +def get_all_code_styles() -> dict[str, BaseStyle]: """ Return a mapping from style names to their classes. """ - result: Dict[str, BaseStyle] = { + result: dict[str, BaseStyle] = { name: style_from_pygments_cls(get_style_by_name(name)) for name in get_all_styles() } @@ -20,7 +22,7 @@ def get_all_code_styles() -> Dict[str, BaseStyle]: return result -def get_all_ui_styles() -> Dict[str, BaseStyle]: +def get_all_ui_styles() -> dict[str, BaseStyle]: """ Return a dict mapping {ui_style_name -> style_dict}. """ diff --git a/ptpython/utils.py b/ptpython/utils.py index ef96ca4b..53488997 100644 --- a/ptpython/utils.py +++ b/ptpython/utils.py @@ -1,6 +1,8 @@ """ For internal use only. """ +from __future__ import annotations + import re from typing import ( TYPE_CHECKING, @@ -65,8 +67,8 @@ def has_unclosed_brackets(text: str) -> bool: def get_jedi_script_from_document( - document: Document, locals: Dict[str, Any], globals: Dict[str, Any] -) -> "Interpreter": + document: Document, locals: dict[str, Any], globals: dict[str, Any] +) -> Interpreter: import jedi # We keep this import in-line, to improve start-up time. # Importing Jedi is 'slow'. @@ -154,7 +156,7 @@ def if_mousedown(handler: _T) -> _T: by the Window.) """ - def handle_if_mouse_down(mouse_event: MouseEvent) -> "NotImplementedOrNone": + def handle_if_mouse_down(mouse_event: MouseEvent) -> NotImplementedOrNone: if mouse_event.event_type == MouseEventType.MOUSE_DOWN: return handler(mouse_event) else: diff --git a/ptpython/validator.py b/ptpython/validator.py index ffac5839..3b36d273 100644 --- a/ptpython/validator.py +++ b/ptpython/validator.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from typing import Callable, Optional from prompt_toolkit.document import Document @@ -16,7 +18,7 @@ class PythonValidator(Validator): active compiler flags. """ - def __init__(self, get_compiler_flags: Optional[Callable[[], int]] = None) -> None: + def __init__(self, get_compiler_flags: Callable[[], int] | None = None) -> None: self.get_compiler_flags = get_compiler_flags def validate(self, document: Document) -> None: diff --git a/setup.py b/setup.py index 2725dac4..c4087f9c 100644 --- a/setup.py +++ b/setup.py @@ -25,10 +25,10 @@ "prompt_toolkit>=3.0.18,<3.1.0", "pygments", ], - python_requires=">=3.6", + python_requires=">=3.7", classifiers=[ "Programming Language :: Python :: 3", - "Programming Language :: Python :: 3.6", + "Programming Language :: Python :: 3.7", "Programming Language :: Python :: 3.7", "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3 :: Only", diff --git a/tests/run_tests.py b/tests/run_tests.py index 2f945163..0de37430 100755 --- a/tests/run_tests.py +++ b/tests/run_tests.py @@ -1,4 +1,6 @@ #!/usr/bin/env python +from __future__ import annotations + import unittest import ptpython.completer From 2d4b0b0d04973e49cf0fe35a71e62c4ca486eed1 Mon Sep 17 00:00:00 2001 From: Tim Gates Date: Sun, 17 Jul 2022 08:46:58 +1000 Subject: [PATCH 166/220] docs: Fix a few typos There are small typos in: - docs/concurrency-challenges.rst - examples/ptpython_config/config.py - ptpython/completer.py - ptpython/history_browser.py - ptpython/key_bindings.py - ptpython/repl.py Fixes: - Should read `returns` rather than `retuns`. - Should read `parentheses` rather than `parethesis`. - Should read `output` rather than `ouptut`. - Should read `navigation` rather than `navigaton`. - Should read `executor` rather than `excecutor`. - Should read `depending` rather than `deponding`. Signed-off-by: Tim Gates --- docs/concurrency-challenges.rst | 2 +- examples/ptpython_config/config.py | 2 +- ptpython/completer.py | 2 +- ptpython/history_browser.py | 2 +- ptpython/key_bindings.py | 2 +- ptpython/repl.py | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/concurrency-challenges.rst b/docs/concurrency-challenges.rst index b56d9698..0ff9c6c3 100644 --- a/docs/concurrency-challenges.rst +++ b/docs/concurrency-challenges.rst @@ -67,7 +67,7 @@ When a normal blocking embed is used: When an awaitable embed is used, for embedding in a coroutine, but having the event loop continue: * We run the input method from the blocking embed in an asyncio executor - and do an `await loop.run_in_excecutor(...)`. + and do an `await loop.run_in_executor(...)`. * The "eval" happens again in the main thread. * "print" is also similar, except that the pager code (if used) runs in an executor too. diff --git a/examples/ptpython_config/config.py b/examples/ptpython_config/config.py index 2b51dfc4..2f3f49dd 100644 --- a/examples/ptpython_config/config.py +++ b/examples/ptpython_config/config.py @@ -50,7 +50,7 @@ def configure(repl): # Swap light/dark colors on or off repl.swap_light_and_dark = False - # Highlight matching parethesis. + # Highlight matching parentheses. repl.highlight_matching_parenthesis = True # Line wrapping. (Instead of horizontal scrolling.) diff --git a/ptpython/completer.py b/ptpython/completer.py index f610916e..3c5dd32f 100644 --- a/ptpython/completer.py +++ b/ptpython/completer.py @@ -599,7 +599,7 @@ def sort_key(name: str) -> tuple[int, str]: class HidePrivateCompleter(Completer): """ - Wrapper around completer that hides private fields, deponding on whether or + Wrapper around completer that hides private fields, depending on whether or not public fields are shown. (The reason this is implemented as a `Completer` wrapper is because this diff --git a/ptpython/history_browser.py b/ptpython/history_browser.py index 81cc63ae..eea81c2e 100644 --- a/ptpython/history_browser.py +++ b/ptpython/history_browser.py @@ -599,7 +599,7 @@ def __init__(self, python_input: PythonInput, original_document: Document) -> No Create an `Application` for the history screen. This has to be run as a sub application of `python_input`. - When this application runs and returns, it retuns the selected lines. + When this application runs and returns, it returns the selected lines. """ self.python_input = python_input diff --git a/ptpython/key_bindings.py b/ptpython/key_bindings.py index 6b4c1862..d7bb575e 100644 --- a/ptpython/key_bindings.py +++ b/ptpython/key_bindings.py @@ -159,7 +159,7 @@ def _(event: E) -> None: Behaviour of the Enter key. Auto indent after newline/Enter. - (When not in Vi navigaton mode, and when multiline is enabled.) + (When not in Vi navigation mode, and when multiline is enabled.) """ b = event.current_buffer empty_lines_required = python_input.accept_input_on_enter or 10000 diff --git a/ptpython/repl.py b/ptpython/repl.py index a3dd788e..02a5075d 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -405,7 +405,7 @@ def _format_result_output(self, result: object) -> StyleAndTextTuples: def show_result(self, result: object) -> None: """ - Show __repr__ for an `eval` result and print to ouptut. + Show __repr__ for an `eval` result and print to output. """ formatted_text_output = self._format_result_output(result) From ea6b2c51db96e260b2ce32574938bd844f7a01ce Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Wed, 22 Feb 2023 10:35:43 +0000 Subject: [PATCH 167/220] Fix completer suffix for mappings/sequences. --- ptpython/completer.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ptpython/completer.py b/ptpython/completer.py index 3c5dd32f..f28d2b16 100644 --- a/ptpython/completer.py +++ b/ptpython/completer.py @@ -568,9 +568,9 @@ def get_suffix(name: str) -> str: obj = getattr(result, name, None) if inspect.isfunction(obj) or inspect.ismethod(obj): return "()" - if isinstance(obj, dict): + if isinstance(obj, collections_abc.Mapping): return "{}" - if isinstance(obj, (list, tuple)): + if isinstance(obj, collections_abc.Sequence): return "[]" except: pass From ee047a2701fcd269592a626e947bf9625db5eb6d Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Wed, 22 Feb 2023 10:36:12 +0000 Subject: [PATCH 168/220] Add cursor shape support. --- ptpython/layout.py | 2 +- ptpython/python_input.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/ptpython/layout.py b/ptpython/layout.py index 2c6395ce..d15e52e2 100644 --- a/ptpython/layout.py +++ b/ptpython/layout.py @@ -151,7 +151,7 @@ def goto_next(mouse_event: MouseEvent) -> None: append_category(category) for option in category.options: - append(i, option.title, "%s" % option.get_current_value()) + append(i, option.title, "%s" % (option.get_current_value(),)) i += 1 tokens.pop() # Remove last newline. diff --git a/ptpython/python_input.py b/ptpython/python_input.py index e8170f2b..da19076b 100644 --- a/ptpython/python_input.py +++ b/ptpython/python_input.py @@ -34,6 +34,12 @@ ThreadedCompleter, merge_completers, ) +from prompt_toolkit.cursor_shapes import ( + AnyCursorShapeConfig, + CursorShape, + DynamicCursorShapeConfig, + ModalCursorShapeConfig, +) from prompt_toolkit.document import Document from prompt_toolkit.enums import DEFAULT_BUFFER, EditingMode from prompt_toolkit.filters import Condition @@ -325,6 +331,18 @@ def __init__( self.search_buffer: Buffer = Buffer() self.docstring_buffer: Buffer = Buffer(read_only=True) + # Cursor shapes. + self.cursor_shape_config = "Block" + self.all_cursor_shape_configs: Dict[str, AnyCursorShapeConfig] = { + "Block": CursorShape.BLOCK, + "Underline": CursorShape.UNDERLINE, + "Beam": CursorShape.BEAM, + "Modal (vi)": ModalCursorShapeConfig(), + "Blink block": CursorShape.BLINKING_BLOCK, + "Blink under": CursorShape.BLINKING_UNDERLINE, + "Blink beam": CursorShape.BLINKING_BEAM, + } + # Tokens to be shown at the prompt. self.prompt_style: str = "classic" # The currently active style. @@ -584,6 +602,16 @@ def get_values() -> dict[str, Callable[[], bool]]: "Vi": lambda: enable("vi_mode"), }, ), + Option( + title="Cursor shape", + description="Change the cursor style, possibly according " + "to the Vi input mode.", + get_current_value=lambda: self.cursor_shape_config, + get_values=lambda: dict( + (s, partial(enable, "cursor_shape_config", s)) + for s in self.all_cursor_shape_configs + ), + ), simple_option( title="Paste mode", description="When enabled, don't indent automatically.", @@ -896,6 +924,9 @@ def _create_application( style_transformation=self.style_transformation, include_default_pygments_style=False, reverse_vi_search_direction=True, + cursor=DynamicCursorShapeConfig( + lambda: self.all_cursor_shape_configs[self.cursor_shape_config] + ), input=input, output=output, ) From f8399dd5a13d4bb8e5cd98365f2435cdfaf628a8 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Wed, 22 Feb 2023 10:52:04 +0000 Subject: [PATCH 169/220] Set minimum prompt_toolkit version to 3.0.28, because of cursor shape support. --- setup.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index c4087f9c..ce5be98d 100644 --- a/setup.py +++ b/setup.py @@ -21,8 +21,8 @@ "appdirs", "importlib_metadata;python_version<'3.8'", "jedi>=0.16.0", - # Use prompt_toolkit 3.0.18, because of the `in_thread` option. - "prompt_toolkit>=3.0.18,<3.1.0", + # Use prompt_toolkit 3.0.28, because of cursor shape support. + "prompt_toolkit>=3.0.28,<3.1.0", "pygments", ], python_requires=">=3.7", From 44f0c6e57d616d41de458daccbf36e8d8eb5fb3d Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Wed, 22 Feb 2023 10:52:15 +0000 Subject: [PATCH 170/220] Release 3.0.23 --- CHANGELOG | 11 +++++++++++ setup.py | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 916a5422..645ca60b 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,17 @@ CHANGELOG ========= +3.0.23: 2023-02-22 +------------------ + +Fixes: +- Don't print exception messages twice for unhandled exceptions. +- Added cursor shape support. + +Breaking changes: +- Drop Python 3.6 support. + + 3.0.22: 2022-12-06 ------------------ diff --git a/setup.py b/setup.py index ce5be98d..18d2911a 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ setup( name="ptpython", author="Jonathan Slenders", - version="3.0.22", + version="3.0.23", url="https://github.com/prompt-toolkit/ptpython", description="Python REPL build on top of prompt_toolkit", long_description=long_description, From 91d2c3589310452a0f79f2fa1a4a6847fc095481 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Wed, 12 Apr 2023 15:07:11 +0000 Subject: [PATCH 171/220] Fix various typos. --- CHANGELOG | 2 +- ptpython/completer.py | 6 +++--- ptpython/python_input.py | 4 ++-- ptpython/utils.py | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 645ca60b..e753cfd9 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -191,7 +191,7 @@ New features: - Optional pager for displaying outputs that don't fit on the screen. - Added --light-bg and --dark-bg flags to automatically optimize the brightness of the colors according to the terminal background. -- Addd `PTPYTHON_CONFIG_HOME` for explicitely setting the config directory. +- Add `PTPYTHON_CONFIG_HOME` for explicitly setting the config directory. - Show completion suffixes (like '(' for functions). Fixes: diff --git a/ptpython/completer.py b/ptpython/completer.py index f28d2b16..85a96d7c 100644 --- a/ptpython/completer.py +++ b/ptpython/completer.py @@ -259,7 +259,7 @@ def get_completions( # See: https://github.com/jonathanslenders/ptpython/issues/223 pass except Exception: - # Supress all other Jedi exceptions. + # Suppress all other Jedi exceptions. pass else: # Move function parameters to the top. @@ -367,7 +367,7 @@ def __init__( rf""" {expression} - # Dict loopup to complete (square bracket open + start of + # Dict lookup to complete (square bracket open + start of # string). \[ \s* ([^\[\]]*)$ @@ -380,7 +380,7 @@ def __init__( rf""" {expression} - # Attribute loopup to complete (dot + varname). + # Attribute lookup to complete (dot + varname). \. \s* ([a-zA-Z0-9_]*)$ """, diff --git a/ptpython/python_input.py b/ptpython/python_input.py index da19076b..0c7fef6f 100644 --- a/ptpython/python_input.py +++ b/ptpython/python_input.py @@ -379,7 +379,7 @@ def __init__( self.options = self._create_options() self.selected_option_index: int = 0 - #: Incremeting integer counting the current statement. + #: Incrementing integer counting the current statement. self.current_statement_index: int = 1 # Code signatures. (This is set asynchronously after a timeout.) @@ -835,7 +835,7 @@ def get_values() -> dict[str, Callable[[], bool]]: [ simple_option( title="Syntax highlighting", - description="Use colors for syntax highligthing", + description="Use colors for syntax highlighting", field_name="enable_syntax_highlighting", ), simple_option( diff --git a/ptpython/utils.py b/ptpython/utils.py index 53488997..d973d726 100644 --- a/ptpython/utils.py +++ b/ptpython/utils.py @@ -91,7 +91,7 @@ def get_jedi_script_from_document( # Workaround Jedi issue #514: for https://github.com/davidhalter/jedi/issues/514 return None except KeyError: - # Workaroud for a crash when the input is "u'", the start of a unicode string. + # Workaround for a crash when the input is "u'", the start of a unicode string. return None except Exception: # Workaround for: https://github.com/jonathanslenders/ptpython/issues/91 From 6c2d650649e5003d9ee01c01df508c11c6b28e9b Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Fri, 3 Nov 2023 13:07:14 +0000 Subject: [PATCH 172/220] Use ruff for linting and formatting. - Removed unused typing imports. - Renamed ambiguous variable. - Fix dict literal usage. - Ruff formatting. - Removed unnecessary trailing commas. --- .github/workflows/test.yaml | 6 ++-- ptpython/completer.py | 2 +- ptpython/contrib/asyncssh_repl.py | 2 +- ptpython/entry_points/run_ptpython.py | 6 ++-- ptpython/history_browser.py | 3 +- ptpython/layout.py | 22 ++++++------- ptpython/lexer.py | 2 +- ptpython/python_input.py | 21 +++--------- ptpython/repl.py | 12 +++---- ptpython/signatures.py | 3 +- ptpython/style.py | 2 -- ptpython/utils.py | 12 +------ ptpython/validator.py | 2 +- pyproject.toml | 47 +++++++++++++++++++-------- setup.py | 10 +++--- 15 files changed, 75 insertions(+), 77 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 31837db3..9a50f3bc 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -22,13 +22,13 @@ jobs: run: | sudo apt remove python3-pip python -m pip install --upgrade pip - python -m pip install . black isort mypy pytest readme_renderer + python -m pip install . ruff mypy pytest readme_renderer pip list - name: Type Checker run: | mypy ptpython - isort -c --profile black ptpython examples setup.py - black --check ptpython examples setup.py + ruff . + ruff format --check . - name: Run Tests run: | ./tests/run_tests.py diff --git a/ptpython/completer.py b/ptpython/completer.py index 85a96d7c..91d66474 100644 --- a/ptpython/completer.py +++ b/ptpython/completer.py @@ -6,7 +6,7 @@ import keyword import re from enum import Enum -from typing import TYPE_CHECKING, Any, Callable, Dict, Iterable, List, Optional, Tuple +from typing import TYPE_CHECKING, Any, Callable, Iterable from prompt_toolkit.completion import ( CompleteEvent, diff --git a/ptpython/contrib/asyncssh_repl.py b/ptpython/contrib/asyncssh_repl.py index 0347aded..051519de 100644 --- a/ptpython/contrib/asyncssh_repl.py +++ b/ptpython/contrib/asyncssh_repl.py @@ -9,7 +9,7 @@ from __future__ import annotations import asyncio -from typing import Any, Optional, TextIO, cast +from typing import Any, TextIO, cast import asyncssh from prompt_toolkit.data_structures import Size diff --git a/ptpython/entry_points/run_ptpython.py b/ptpython/entry_points/run_ptpython.py index 1b4074d4..c0b4078b 100644 --- a/ptpython/entry_points/run_ptpython.py +++ b/ptpython/entry_points/run_ptpython.py @@ -28,7 +28,7 @@ import pathlib import sys from textwrap import dedent -from typing import IO, Optional, Tuple +from typing import IO import appdirs from prompt_toolkit.formatted_text import HTML @@ -72,12 +72,12 @@ def create_parser() -> _Parser: "--light-bg", action="store_true", help="Run on a light background (use dark colors for text).", - ), + ) parser.add_argument( "--dark-bg", action="store_true", help="Run on a dark background (use light colors for text).", - ), + ) parser.add_argument( "--config-file", type=str, help="Location of configuration file." ) diff --git a/ptpython/history_browser.py b/ptpython/history_browser.py index eea81c2e..b667be12 100644 --- a/ptpython/history_browser.py +++ b/ptpython/history_browser.py @@ -7,7 +7,7 @@ from __future__ import annotations from functools import partial -from typing import TYPE_CHECKING, Callable, List, Optional, Set +from typing import TYPE_CHECKING, Callable from prompt_toolkit.application import Application from prompt_toolkit.application.current import get_app @@ -107,6 +107,7 @@ class BORDER: "Box drawing characters." + HORIZONTAL = "\u2501" VERTICAL = "\u2503" TOP_LEFT = "\u250f" diff --git a/ptpython/layout.py b/ptpython/layout.py index d15e52e2..2c1ec15f 100644 --- a/ptpython/layout.py +++ b/ptpython/layout.py @@ -7,7 +7,7 @@ import sys from enum import Enum from inspect import _ParameterKind as ParameterKind -from typing import TYPE_CHECKING, Any, List, Optional, Type +from typing import TYPE_CHECKING, Any from prompt_toolkit.application import get_app from prompt_toolkit.enums import DEFAULT_BUFFER, SEARCH_BUFFER @@ -17,11 +17,7 @@ is_done, renderer_height_is_known, ) -from prompt_toolkit.formatted_text import ( - AnyFormattedText, - fragment_list_width, - to_formatted_text, -) +from prompt_toolkit.formatted_text import fragment_list_width, to_formatted_text from prompt_toolkit.formatted_text.base import StyleAndTextTuples from prompt_toolkit.key_binding.vi_state import InputMode from prompt_toolkit.layout.containers import ( @@ -60,7 +56,6 @@ SystemToolbar, ValidationToolbar, ) -from pygments.lexers import PythonLexer from .filters import HasSignature, ShowDocstring, ShowSidebar, ShowSignature from .prompt_style import PromptStyle @@ -74,6 +69,7 @@ class CompletionVisualisation(Enum): "Visualisation method for the completions." + NONE = "none" POP_UP = "pop-up" MULTI_COLUMN = "multi-column" @@ -151,7 +147,7 @@ def goto_next(mouse_event: MouseEvent) -> None: append_category(category) for option in category.options: - append(i, option.title, "%s" % (option.get_current_value(),)) + append(i, option.title, str(option.get_current_value())) i += 1 tokens.pop() # Remove last newline. @@ -302,13 +298,15 @@ def get_text_fragments() -> StyleAndTextTuples: content=Window( FormattedTextControl(get_text_fragments), height=Dimension.exact(1) ), - filter= # Show only when there is a signature - HasSignature(python_input) & + filter=HasSignature(python_input) + & # Signature needs to be shown. - ShowSignature(python_input) & + ShowSignature(python_input) + & # And no sidebar is visible. - ~ShowSidebar(python_input) & + ~ShowSidebar(python_input) + & # Not done yet. ~is_done, ) diff --git a/ptpython/lexer.py b/ptpython/lexer.py index 81924c9d..d925e95c 100644 --- a/ptpython/lexer.py +++ b/ptpython/lexer.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import Callable, Optional +from typing import Callable from prompt_toolkit.document import Document from prompt_toolkit.formatted_text import StyleAndTextTuples diff --git a/ptpython/python_input.py b/ptpython/python_input.py index 0c7fef6f..211d36c9 100644 --- a/ptpython/python_input.py +++ b/ptpython/python_input.py @@ -6,18 +6,7 @@ from asyncio import get_event_loop from functools import partial -from typing import ( - TYPE_CHECKING, - Any, - Callable, - Dict, - Generic, - List, - Mapping, - Optional, - Tuple, - TypeVar, -) +from typing import TYPE_CHECKING, Any, Callable, Dict, Generic, Mapping, TypeVar from prompt_toolkit.application import Application, get_app from prompt_toolkit.auto_suggest import ( @@ -333,7 +322,7 @@ def __init__( # Cursor shapes. self.cursor_shape_config = "Block" - self.all_cursor_shape_configs: Dict[str, AnyCursorShapeConfig] = { + self.all_cursor_shape_configs: dict[str, AnyCursorShapeConfig] = { "Block": CursorShape.BLOCK, "Underline": CursorShape.UNDERLINE, "Beam": CursorShape.BEAM, @@ -607,10 +596,10 @@ def get_values() -> dict[str, Callable[[], bool]]: description="Change the cursor style, possibly according " "to the Vi input mode.", get_current_value=lambda: self.cursor_shape_config, - get_values=lambda: dict( - (s, partial(enable, "cursor_shape_config", s)) + get_values=lambda: { + s: partial(enable, "cursor_shape_config", s) for s in self.all_cursor_shape_configs - ), + }, ), simple_option( title="Paste mode", diff --git a/ptpython/repl.py b/ptpython/repl.py index 02a5075d..3a74c3c3 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -18,7 +18,7 @@ import warnings from dis import COMPILER_FLAG_NAMES from enum import Enum -from typing import Any, Callable, ContextManager, Dict, Optional +from typing import Any, Callable, ContextManager from prompt_toolkit.formatted_text import ( HTML, @@ -547,12 +547,12 @@ def _format_exception_output(self, e: BaseException) -> PygmentsTokens: tblist = tblist[line_nr:] break - l = traceback.format_list(tblist) - if l: - l.insert(0, "Traceback (most recent call last):\n") - l.extend(traceback.format_exception_only(t, v)) + tb_list = traceback.format_list(tblist) + if tb_list: + tb_list.insert(0, "Traceback (most recent call last):\n") + tb_list.extend(traceback.format_exception_only(t, v)) - tb_str = "".join(l) + tb_str = "".join(tb_list) # Format exception and write to output. # (We use the default style. Most other styles result diff --git a/ptpython/signatures.py b/ptpython/signatures.py index 5a6f286a..d4cb98c2 100644 --- a/ptpython/signatures.py +++ b/ptpython/signatures.py @@ -10,7 +10,7 @@ import inspect from inspect import Signature as InspectSignature from inspect import _ParameterKind as ParameterKind -from typing import TYPE_CHECKING, Any, Dict, List, Optional, Sequence, Tuple +from typing import TYPE_CHECKING, Any, Sequence from prompt_toolkit.document import Document @@ -203,7 +203,6 @@ def get_signatures_using_eval( running `eval()` over the detected function name. """ # Look for open parenthesis, before cursor position. - text = document.text_before_cursor pos = document.cursor_position - 1 paren_mapping = {")": "(", "}": "{", "]": "["} diff --git a/ptpython/style.py b/ptpython/style.py index 199d5abf..c5a04e58 100644 --- a/ptpython/style.py +++ b/ptpython/style.py @@ -1,7 +1,5 @@ from __future__ import annotations -from typing import Dict - from prompt_toolkit.styles import BaseStyle, Style, merge_styles from prompt_toolkit.styles.pygments import style_from_pygments_cls from prompt_toolkit.utils import is_conemu_ansi, is_windows, is_windows_vt100_supported diff --git a/ptpython/utils.py b/ptpython/utils.py index d973d726..28887d20 100644 --- a/ptpython/utils.py +++ b/ptpython/utils.py @@ -4,17 +4,7 @@ from __future__ import annotations import re -from typing import ( - TYPE_CHECKING, - Any, - Callable, - Dict, - Iterable, - Optional, - Type, - TypeVar, - cast, -) +from typing import TYPE_CHECKING, Any, Callable, Iterable, TypeVar, cast from prompt_toolkit.document import Document from prompt_toolkit.formatted_text import to_formatted_text diff --git a/ptpython/validator.py b/ptpython/validator.py index 3b36d273..91b9c284 100644 --- a/ptpython/validator.py +++ b/ptpython/validator.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import Callable, Optional +from typing import Callable from prompt_toolkit.document import Document from prompt_toolkit.validation import ValidationError, Validator diff --git a/pyproject.toml b/pyproject.toml index b356239f..d9d839ed 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,13 +1,34 @@ -[tool.black] -target-version = ['py36'] - - -[tool.isort] -# isort configuration that is compatible with Black. -multi_line_output = 3 -include_trailing_comma = true -known_first_party = "ptpython" -known_third_party = "prompt_toolkit,pygments,asyncssh" -force_grid_wrap = 0 -use_parentheses = true -line_length = 88 +[tool.ruff] +target-version = "py37" +select = [ + "E", # pycodestyle errors + "W", # pycodestyle warnings + "F", # pyflakes + "C", # flake8-comprehensions + "T", # Print. + "I", # isort + # "B", # flake8-bugbear + "UP", # pyupgrade + "RUF100", # unused-noqa + "Q", # quotes +] +ignore = [ + "E501", # Line too long, handled by black + "C901", # Too complex + "E722", # bare except. +] + + +[tool.ruff.per-file-ignores] +"examples/*" = ["T201"] # Print allowed in examples. +"examples/ptpython_config/config.py" = ["F401"] # Unused imports in config. +"ptpython/entry_points/run_ptipython.py" = ["T201", "F401"] # Print, import usage. +"ptpython/entry_points/run_ptpython.py" = ["T201"] # Print usage. +"ptpython/ipython.py" = ["T100"] # Import usage. +"ptpython/repl.py" = ["T201"] # Print usage. +"tests/run_tests.py" = ["F401"] # Unused imports. + + +[tool.ruff.isort] +known-first-party = ["ptpython"] +known-third-party = ["prompt_toolkit", "pygments", "asyncssh"] diff --git a/setup.py b/setup.py index 18d2911a..ae9838ea 100644 --- a/setup.py +++ b/setup.py @@ -39,12 +39,14 @@ "ptpython = ptpython.entry_points.run_ptpython:run", "ptipython = ptpython.entry_points.run_ptipython:run", "ptpython%s = ptpython.entry_points.run_ptpython:run" % sys.version_info[0], - "ptpython%s.%s = ptpython.entry_points.run_ptpython:run" - % sys.version_info[:2], + "ptpython{}.{} = ptpython.entry_points.run_ptpython:run".format( + *sys.version_info[:2] + ), "ptipython%s = ptpython.entry_points.run_ptipython:run" % sys.version_info[0], - "ptipython%s.%s = ptpython.entry_points.run_ptipython:run" - % sys.version_info[:2], + "ptipython{}.{} = ptpython.entry_points.run_ptipython:run".format( + *sys.version_info[:2] + ), ] }, extras_require={ From 945426bfaa61ecc8c602983c2e2f023a9d17df22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tero=20Ykspet=C3=A4j=C3=A4?= Date: Tue, 12 Sep 2023 09:42:54 +0300 Subject: [PATCH 173/220] Add cursor_shape_config example Describe setting the cursor shape to modal for Vi mode. List other possible options for the setting. --- examples/ptpython_config/config.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/examples/ptpython_config/config.py b/examples/ptpython_config/config.py index 2f3f49dd..9e13879a 100644 --- a/examples/ptpython_config/config.py +++ b/examples/ptpython_config/config.py @@ -70,6 +70,9 @@ def configure(repl): # Vi mode. repl.vi_mode = False + # Enable the modal cursor (when using Vi mode). Other options are 'Block', 'Underline', 'Beam', 'Blink under', 'Blink block', and 'Blink beam' + repl.cursor_shape_config = 'Modal (vi)' + # Paste mode. (When True, don't insert whitespace after new line.) repl.paste_mode = False From 9ea323855977119fb5e49f428eadcf552b2260f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E7=A9=86=E7=A9=86?= Date: Wed, 6 Sep 2023 13:36:04 +0800 Subject: [PATCH 174/220] fix top-level await in ipython --- ptpython/ipython.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/ptpython/ipython.py b/ptpython/ipython.py index fb4b5ed9..ae85a265 100644 --- a/ptpython/ipython.py +++ b/ptpython/ipython.py @@ -38,6 +38,7 @@ from .completer import PythonCompleter from .python_input import PythonInput +from .repl import PyCF_ALLOW_TOP_LEVEL_AWAIT from .style import default_ui_style from .validator import PythonValidator @@ -211,6 +212,12 @@ def __init__(self, ipython_shell, *a, **kw): self.ui_styles = {"default": Style.from_dict(style_dict)} self.use_ui_colorscheme("default") + def get_compiler_flags(self): + flags = super().get_compiler_flags() + if self.ipython_shell.autoawait: + flags |= PyCF_ALLOW_TOP_LEVEL_AWAIT + return flags + class InteractiveShellEmbed(_InteractiveShellEmbed): """ From 48c7b3885c786fcb3f5f75404cd4cc2caa20bfe9 Mon Sep 17 00:00:00 2001 From: gpotter2 <10530980+gpotter2@users.noreply.github.com> Date: Sat, 22 Jul 2023 15:12:19 +0200 Subject: [PATCH 175/220] Fix IPython DeprecationWarning ptipython raises the following error since IPython 7: >>> IPython.core.inputsplitter is deprecated since IPython 7 in favor of `IPython.core.inputtransformer2` --- ptpython/ipython.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ptpython/ipython.py b/ptpython/ipython.py index ae85a265..ad0516a3 100644 --- a/ptpython/ipython.py +++ b/ptpython/ipython.py @@ -14,7 +14,7 @@ from warnings import warn from IPython import utils as ipy_utils -from IPython.core.inputsplitter import IPythonInputSplitter +from IPython.core.inputtransformer2 import TransformerManager from IPython.terminal.embed import InteractiveShellEmbed as _InteractiveShellEmbed from IPython.terminal.ipapp import load_default_config from prompt_toolkit.completion import ( @@ -66,7 +66,7 @@ def out_prompt(self) -> AnyFormattedText: class IPythonValidator(PythonValidator): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - self.isp = IPythonInputSplitter() + self.isp = TransformerManager() def validate(self, document: Document) -> None: document = Document(text=self.isp.transform_cell(document.text)) From d25e67874ffc745a3a65a6776c1b9aa401dca076 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Mon, 4 Sep 2023 08:01:04 -0500 Subject: [PATCH 176/220] Only interrupt run_config() for explicitly passed config_file --- ptpython/repl.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/ptpython/repl.py b/ptpython/repl.py index 3a74c3c3..ce92c660 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -630,23 +630,28 @@ def enable_deprecation_warnings() -> None: warnings.filterwarnings("default", category=DeprecationWarning, module="__main__") -def run_config( - repl: PythonInput, config_file: str = "~/.config/ptpython/config.py" -) -> None: +DEFAULT_CONFIG_FILE = "~/.config/ptpython/config.py" + + +def run_config(repl: PythonInput, config_file: str | None = None) -> None: """ Execute REPL config file. :param repl: `PythonInput` instance. :param config_file: Path of the configuration file. """ + explicit_config_file = config_file is not None + # Expand tildes. - config_file = os.path.expanduser(config_file) + config_file = os.path.expanduser( + config_file if config_file is not None else DEFAULT_CONFIG_FILE + ) def enter_to_continue() -> None: input("\nPress ENTER to continue...") # Check whether this file exists. - if not os.path.exists(config_file): + if not os.path.exists(config_file) and explicit_config_file: print("Impossible to read %r" % config_file) enter_to_continue() return From dc2163383e3dcc54eb19795fe87c0162a578bbfb Mon Sep 17 00:00:00 2001 From: Ryan Delaney Date: Fri, 10 Mar 2023 17:01:23 -0500 Subject: [PATCH 177/220] Add BSD License classifier --- setup.py | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.py b/setup.py index ae9838ea..ad26545a 100644 --- a/setup.py +++ b/setup.py @@ -27,6 +27,7 @@ ], python_requires=">=3.7", classifiers=[ + "License :: OSI Approved :: BSD License", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.7", "Programming Language :: Python :: 3.7", From 03b279ecd6a0ec670cba144f5f680a7a78fc2fc7 Mon Sep 17 00:00:00 2001 From: Ryan Delaney Date: Fri, 10 Mar 2023 17:01:38 -0500 Subject: [PATCH 178/220] Update copyright dates --- LICENSE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index 910b80a7..89a51144 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2015, Jonathan Slenders +Copyright (c) 2015-2023, Jonathan Slenders All rights reserved. Redistribution and use in source and binary forms, with or without modification, From 46b1076cea63f7d0642b2e820d7fbcbff89336a1 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Fri, 3 Nov 2023 13:45:22 +0000 Subject: [PATCH 179/220] Fix code formatting. --- examples/ptpython_config/config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/ptpython_config/config.py b/examples/ptpython_config/config.py index 9e13879a..b25850a2 100644 --- a/examples/ptpython_config/config.py +++ b/examples/ptpython_config/config.py @@ -71,7 +71,7 @@ def configure(repl): repl.vi_mode = False # Enable the modal cursor (when using Vi mode). Other options are 'Block', 'Underline', 'Beam', 'Blink under', 'Blink block', and 'Blink beam' - repl.cursor_shape_config = 'Modal (vi)' + repl.cursor_shape_config = "Modal (vi)" # Paste mode. (When True, don't insert whitespace after new line.) repl.paste_mode = False From 655b354a83aa56c423ba7ecebe4df88928c99526 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Tue, 12 Dec 2023 10:43:26 +0000 Subject: [PATCH 180/220] Refactor output printer so that it can render big outputs without memory issues. Previously, an expression like `b'\x90' * 40_000_000` would kill ptpython because it rendered the whole output at once. This implementation streams the rendering logic while it's paginating. --- ptpython/printer.py | 435 ++++++++++++++++++++++++++++++++++++++++++++ ptpython/repl.py | 400 +++++----------------------------------- pyproject.toml | 1 + 3 files changed, 478 insertions(+), 358 deletions(-) create mode 100644 ptpython/printer.py diff --git a/ptpython/printer.py b/ptpython/printer.py new file mode 100644 index 00000000..3618934e --- /dev/null +++ b/ptpython/printer.py @@ -0,0 +1,435 @@ +from __future__ import annotations + +import sys +import traceback +from dataclasses import dataclass +from enum import Enum +from typing import Generator, Iterable + +from prompt_toolkit.formatted_text import ( + HTML, + AnyFormattedText, + FormattedText, + OneStyleAndTextTuple, + StyleAndTextTuples, + fragment_list_width, + merge_formatted_text, + to_formatted_text, +) +from prompt_toolkit.formatted_text.utils import split_lines +from prompt_toolkit.input import Input +from prompt_toolkit.key_binding import KeyBindings, KeyPressEvent +from prompt_toolkit.output import Output +from prompt_toolkit.shortcuts import PromptSession, print_formatted_text +from prompt_toolkit.styles import BaseStyle, StyleTransformation +from prompt_toolkit.styles.pygments import pygments_token_to_classname +from prompt_toolkit.utils import get_cwidth +from pygments.lexers import PythonLexer, PythonTracebackLexer + +__all__ = ["OutputPrinter"] + +# Never reformat results larger than this: +MAX_REFORMAT_SIZE = 1_000_000 + + +@dataclass +class OutputPrinter: + """ + Result printer. + + Usage:: + + printer = OutputPrinter(...) + printer.display_result(...) + printer.display_exception(...) + """ + + output: Output + input: Input + style: BaseStyle + title: AnyFormattedText + style_transformation: StyleTransformation + + def display_result( + self, + result: object, + *, + out_prompt: AnyFormattedText, + reformat: bool, + highlight: bool, + paginate: bool, + ) -> None: + """ + Show __repr__ (or `__pt_repr__`) for an `eval` result and print to output. + + :param reformat: Reformat result using 'black' before printing if the + result is parsable as Python code. + :param highlight: Syntax highlight the result. + :param paginate: Show paginator when the result does not fit on the + screen. + """ + out_prompt = to_formatted_text(out_prompt) + out_prompt_width = fragment_list_width(out_prompt) + + result = self._insert_out_prompt_and_split_lines( + self._format_result_output( + result, + reformat=reformat, + highlight=highlight, + line_length=self.output.get_size().columns - out_prompt_width, + paginate=paginate, + ), + out_prompt=out_prompt, + ) + self._display_result(result, paginate=paginate) + + def display_exception( + self, e: BaseException, *, highlight: bool, paginate: bool + ) -> None: + """ + Render an exception. + """ + result = self._insert_out_prompt_and_split_lines( + self._format_exception_output(e, highlight=highlight), + out_prompt="", + ) + self._display_result(result, paginate=paginate) + + def display_style_and_text_tuples( + self, + result: Iterable[OneStyleAndTextTuple], + *, + paginate: bool, + ) -> None: + self._display_result( + self._insert_out_prompt_and_split_lines(result, out_prompt=""), + paginate=paginate, + ) + + def _display_result( + self, + lines: Iterable[StyleAndTextTuples], + *, + paginate: bool, + ) -> None: + if paginate: + self._print_paginated_formatted_text(lines) + else: + for line in lines: + self._print_formatted_text(line) + + self.output.flush() + + def _print_formatted_text(self, line: StyleAndTextTuples, end: str = "\n") -> None: + print_formatted_text( + FormattedText(line), + style=self.style, + style_transformation=self.style_transformation, + include_default_pygments_style=False, + output=self.output, + end=end, + ) + + def _format_result_output( + self, + result: object, + *, + reformat: bool, + highlight: bool, + line_length: int, + paginate: bool, + ) -> Generator[OneStyleAndTextTuple, None, None]: + """ + Format __repr__ for an `eval` result. + + Note: this can raise `KeyboardInterrupt` if either calling `__repr__`, + `__pt_repr__` or formatting the output with "Black" takes to long + and the user presses Control-C. + """ + # If __pt_repr__ is present, take this. This can return prompt_toolkit + # formatted text. + try: + if hasattr(result, "__pt_repr__"): + formatted_result_repr = to_formatted_text( + getattr(result, "__pt_repr__")() + ) + yield from formatted_result_repr + return + except KeyboardInterrupt: + raise # Don't catch here. + except: + # For bad code, `__getattr__` can raise something that's not an + # `AttributeError`. This happens already when calling `hasattr()`. + pass + + # Call `__repr__` of given object first, to turn it in a string. + try: + result_repr = repr(result) + except KeyboardInterrupt: + raise # Don't catch here. + except BaseException as e: + # Calling repr failed. + self.display_exception(e, highlight=highlight, paginate=paginate) + return + + # Determine whether it's valid Python code. If not, + # reformatting/highlighting won't be applied. + if len(result_repr) < MAX_REFORMAT_SIZE: + try: + compile(result_repr, "", "eval") + except SyntaxError: + valid_python = False + else: + valid_python = True + else: + valid_python = False + + if valid_python and reformat: + # Inline import. Slightly speed up start-up time if black is + # not used. + try: + import black + + if not hasattr(black, "Mode"): + raise ImportError + except ImportError: + pass # no Black package in your installation + else: + result_repr = black.format_str( + result_repr, + mode=black.Mode(line_length=line_length), + ) + + if valid_python and highlight: + yield from _lex_python_result(result_repr) + else: + yield ("", result_repr) + + def _insert_out_prompt_and_split_lines( + self, result: Iterable[OneStyleAndTextTuple], out_prompt: AnyFormattedText + ) -> Iterable[StyleAndTextTuples]: + r""" + Split styled result in lines (based on the \n characters in the result) + an insert output prompt on whitespace in front of each line. (This does + not yet do the soft wrapping.) + + Yield lines as a result. + """ + out_prompt = to_formatted_text(out_prompt) + out_prompt_width = fragment_list_width(out_prompt) + prefix = ("", " " * out_prompt_width) + + for i, line in enumerate(split_lines(result)): + if i == 0: + line = [*out_prompt, *line] + else: + line = [prefix, *line] + yield line + + def _apply_soft_wrapping( + self, lines: Iterable[StyleAndTextTuples] + ) -> Iterable[StyleAndTextTuples]: + """ + Apply soft wrapping to the given lines. Wrap according to the terminal + width. Insert whitespace in front of each wrapped line to align it with + the output prompt. + """ + line_length = self.output.get_size().columns + + # Iterate over hard wrapped lines. + for lineno, line in enumerate(lines): + columns_in_buffer = 0 + current_line: list[OneStyleAndTextTuple] = [] + + for style, text, *_ in line: + for c in text: + width = get_cwidth(c) + + # (Soft) wrap line if it doesn't fit. + if columns_in_buffer + width > line_length: + yield current_line + columns_in_buffer = 0 + current_line = [] + + columns_in_buffer += width + current_line.append((style, c)) + + if len(current_line) > 0: + yield current_line + + def _print_paginated_formatted_text( + self, lines: Iterable[StyleAndTextTuples] + ) -> None: + """ + Print formatted text, using --MORE-- style pagination. + (Avoid filling up the terminal's scrollback buffer.) + """ + lines = self._apply_soft_wrapping(lines) + pager_prompt = create_pager_prompt( + self.style, self.title, output=self.output, input=self.input + ) + + abort = False + print_all = False + + # Max number of lines allowed in the buffer before painting. + size = self.output.get_size() + max_rows = size.rows - 1 + + # Page buffer. + page: StyleAndTextTuples = [] + + def show_pager() -> None: + nonlocal abort, max_rows, print_all + + # Run pager prompt in another thread. + # Same as for the input. This prevents issues with nested event + # loops. + pager_result = pager_prompt.prompt(in_thread=True) + + if pager_result == PagerResult.ABORT: + print("...") + abort = True + + elif pager_result == PagerResult.NEXT_LINE: + max_rows = 1 + + elif pager_result == PagerResult.NEXT_PAGE: + max_rows = size.rows - 1 + + elif pager_result == PagerResult.PRINT_ALL: + print_all = True + + # Loop over lines. Show --MORE-- prompt when page is filled. + rows = 0 + + for lineno, line in enumerate(lines): + page.extend(line) + page.append(("", "\n")) + rows += 1 + + if rows >= max_rows: + self._print_formatted_text(page, end="") + page = [] + rows = 0 + + if not print_all: + show_pager() + if abort: + return + + self._print_formatted_text(page) + + def _format_exception_output( + self, e: BaseException, highlight: bool + ) -> Generator[OneStyleAndTextTuple, None, None]: + # Instead of just calling ``traceback.format_exc``, we take the + # traceback and skip the bottom calls of this framework. + t, v, tb = sys.exc_info() + + # Required for pdb.post_mortem() to work. + sys.last_type, sys.last_value, sys.last_traceback = t, v, tb + + tblist = list(traceback.extract_tb(tb)) + + for line_nr, tb_tuple in enumerate(tblist): + if tb_tuple[0] == "": + tblist = tblist[line_nr:] + break + + tb_list = traceback.format_list(tblist) + if tb_list: + tb_list.insert(0, "Traceback (most recent call last):\n") + tb_list.extend(traceback.format_exception_only(t, v)) + + tb_str = "".join(tb_list) + + # Format exception and write to output. + # (We use the default style. Most other styles result + # in unreadable colors for the traceback.) + if highlight: + for index, tokentype, text in PythonTracebackLexer().get_tokens_unprocessed( + tb_str + ): + yield ("class:" + pygments_token_to_classname(tokentype), text) + else: + yield ("", tb_str) + + +class PagerResult(Enum): + ABORT = "ABORT" + NEXT_LINE = "NEXT_LINE" + NEXT_PAGE = "NEXT_PAGE" + PRINT_ALL = "PRINT_ALL" + + +def create_pager_prompt( + style: BaseStyle, + title: AnyFormattedText = "", + input: Input | None = None, + output: Output | None = None, +) -> PromptSession[PagerResult]: + """ + Create a "--MORE--" prompt for paginated output. + """ + bindings = KeyBindings() + + @bindings.add("enter") + @bindings.add("down") + def next_line(event: KeyPressEvent) -> None: + event.app.exit(result=PagerResult.NEXT_LINE) + + @bindings.add("space") + def next_page(event: KeyPressEvent) -> None: + event.app.exit(result=PagerResult.NEXT_PAGE) + + @bindings.add("a") + def print_all(event: KeyPressEvent) -> None: + event.app.exit(result=PagerResult.PRINT_ALL) + + @bindings.add("q") + @bindings.add("c-c") + @bindings.add("c-d") + @bindings.add("escape", eager=True) + def no(event: KeyPressEvent) -> None: + event.app.exit(result=PagerResult.ABORT) + + @bindings.add("") + def _(event: KeyPressEvent) -> None: + "Disallow inserting other text." + pass + + session: PromptSession[PagerResult] = PromptSession( + merge_formatted_text( + [ + title, + HTML( + "" + " -- MORE -- " + "[Enter] Scroll " + "[Space] Next page " + "[a] Print all " + "[q] Quit " + ": " + ), + ] + ), + key_bindings=bindings, + erase_when_done=True, + style=style, + input=input, + output=output, + ) + return session + + +def _lex_python_result(result: str) -> Generator[tuple[str, str], None, None]: + "Return token list for Python string." + lexer = PythonLexer() + # Use `get_tokens_unprocessed`, so that we get exactly the same string, + # without line endings appended. `print_formatted_text` already appends a + # line ending, and otherwise we'll have two line endings. + tokens = lexer.get_tokens_unprocessed(result) + + for index, tokentype, text in tokens: + yield ("class:" + pygments_token_to_classname(tokentype), text) diff --git a/ptpython/repl.py b/ptpython/repl.py index ce92c660..98b01afa 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -17,33 +17,18 @@ import types import warnings from dis import COMPILER_FLAG_NAMES -from enum import Enum -from typing import Any, Callable, ContextManager - -from prompt_toolkit.formatted_text import ( - HTML, - AnyFormattedText, - FormattedText, - PygmentsTokens, - StyleAndTextTuples, - fragment_list_width, - merge_formatted_text, - to_formatted_text, -) -from prompt_toolkit.formatted_text.utils import fragment_list_to_text, split_lines -from prompt_toolkit.key_binding import KeyBindings, KeyPressEvent +from typing import Any, Callable, ContextManager, Iterable + +from prompt_toolkit.formatted_text import OneStyleAndTextTuple from prompt_toolkit.patch_stdout import patch_stdout as patch_stdout_context from prompt_toolkit.shortcuts import ( - PromptSession, clear_title, - print_formatted_text, set_title, ) -from prompt_toolkit.styles import BaseStyle -from prompt_toolkit.utils import DummyContext, get_cwidth -from pygments.lexers import PythonLexer, PythonTracebackLexer -from pygments.token import Token +from prompt_toolkit.utils import DummyContext +from pygments.lexers import PythonTracebackLexer # noqa: F401 +from .printer import OutputPrinter from .python_input import PythonInput PyCF_ALLOW_TOP_LEVEL_AWAIT: int @@ -108,7 +93,9 @@ def run_and_show_expression(self, expression: str) -> None: else: # Print. if result is not None: - self.show_result(result) + self._show_result(result) + if self.insert_blank_line_after_output: + self.app.output.write("\n") # Loop. self.current_statement_index += 1 @@ -123,6 +110,24 @@ def run_and_show_expression(self, expression: str) -> None: # any case.) self._handle_keyboard_interrupt(e) + def _get_output_printer(self) -> OutputPrinter: + return OutputPrinter( + output=self.app.output, + input=self.app.input, + style=self._current_style, + style_transformation=self.style_transformation, + title=self.title, + ) + + def _show_result(self, result: object) -> None: + self._get_output_printer().display_result( + result=result, + out_prompt=self.get_output_prompt(), + reformat=self.enable_output_formatting, + highlight=self.enable_syntax_highlighting, + paginate=self.enable_pager, + ) + def run(self) -> None: """ Run the REPL loop. @@ -167,7 +172,7 @@ async def run_and_show_expression_async(self, text: str): else: # Print. if result is not None: - await loop.run_in_executor(None, lambda: self.show_result(result)) + await loop.run_in_executor(None, lambda: self._show_result(result)) # Loop. self.current_statement_index += 1 @@ -318,264 +323,12 @@ def _compile_with_flags(self, code: str, mode: str): dont_inherit=True, ) - def _format_result_output(self, result: object) -> StyleAndTextTuples: - """ - Format __repr__ for an `eval` result. - - Note: this can raise `KeyboardInterrupt` if either calling `__repr__`, - `__pt_repr__` or formatting the output with "Black" takes to long - and the user presses Control-C. - """ - out_prompt = to_formatted_text(self.get_output_prompt()) - - # If the repr is valid Python code, use the Pygments lexer. - try: - result_repr = repr(result) - except KeyboardInterrupt: - raise # Don't catch here. - except BaseException as e: - # Calling repr failed. - self._handle_exception(e) - return [] - - try: - compile(result_repr, "", "eval") - except SyntaxError: - formatted_result_repr = to_formatted_text(result_repr) - else: - # Syntactically correct. Format with black and syntax highlight. - if self.enable_output_formatting: - # Inline import. Slightly speed up start-up time if black is - # not used. - try: - import black - - if not hasattr(black, "Mode"): - raise ImportError - except ImportError: - pass # no Black package in your installation - else: - result_repr = black.format_str( - result_repr, - mode=black.Mode(line_length=self.app.output.get_size().columns), - ) - - formatted_result_repr = to_formatted_text( - PygmentsTokens(list(_lex_python_result(result_repr))) - ) - - # If __pt_repr__ is present, take this. This can return prompt_toolkit - # formatted text. - try: - if hasattr(result, "__pt_repr__"): - formatted_result_repr = to_formatted_text( - getattr(result, "__pt_repr__")() - ) - if isinstance(formatted_result_repr, list): - formatted_result_repr = FormattedText(formatted_result_repr) - except KeyboardInterrupt: - raise # Don't catch here. - except: - # For bad code, `__getattr__` can raise something that's not an - # `AttributeError`. This happens already when calling `hasattr()`. - pass - - # Align every line to the prompt. - line_sep = "\n" + " " * fragment_list_width(out_prompt) - indented_repr: StyleAndTextTuples = [] - - lines = list(split_lines(formatted_result_repr)) - - for i, fragment in enumerate(lines): - indented_repr.extend(fragment) - - # Add indentation separator between lines, not after the last line. - if i != len(lines) - 1: - indented_repr.append(("", line_sep)) - - # Write output tokens. - if self.enable_syntax_highlighting: - formatted_output = merge_formatted_text([out_prompt, indented_repr]) - else: - formatted_output = FormattedText( - out_prompt + [("", fragment_list_to_text(formatted_result_repr))] - ) - - return to_formatted_text(formatted_output) - - def show_result(self, result: object) -> None: - """ - Show __repr__ for an `eval` result and print to output. - """ - formatted_text_output = self._format_result_output(result) - - if self.enable_pager: - self.print_paginated_formatted_text(formatted_text_output) - else: - self.print_formatted_text(formatted_text_output) - - self.app.output.flush() - - if self.insert_blank_line_after_output: - self.app.output.write("\n") - - def print_formatted_text( - self, formatted_text: StyleAndTextTuples, end: str = "\n" - ) -> None: - print_formatted_text( - FormattedText(formatted_text), - style=self._current_style, - style_transformation=self.style_transformation, - include_default_pygments_style=False, - output=self.app.output, - end=end, - ) - - def print_paginated_formatted_text( - self, - formatted_text: StyleAndTextTuples, - end: str = "\n", - ) -> None: - """ - Print formatted text, using --MORE-- style pagination. - (Avoid filling up the terminal's scrollback buffer.) - """ - pager_prompt = self.create_pager_prompt() - size = self.app.output.get_size() - - abort = False - print_all = False - - # Max number of lines allowed in the buffer before painting. - max_rows = size.rows - 1 - - # Page buffer. - rows_in_buffer = 0 - columns_in_buffer = 0 - page: StyleAndTextTuples = [] - - def flush_page() -> None: - nonlocal page, columns_in_buffer, rows_in_buffer - self.print_formatted_text(page, end="") - page = [] - columns_in_buffer = 0 - rows_in_buffer = 0 - - def show_pager() -> None: - nonlocal abort, max_rows, print_all - - # Run pager prompt in another thread. - # Same as for the input. This prevents issues with nested event - # loops. - pager_result = pager_prompt.prompt(in_thread=True) - - if pager_result == PagerResult.ABORT: - print("...") - abort = True - - elif pager_result == PagerResult.NEXT_LINE: - max_rows = 1 - - elif pager_result == PagerResult.NEXT_PAGE: - max_rows = size.rows - 1 - - elif pager_result == PagerResult.PRINT_ALL: - print_all = True - - # Loop over lines. Show --MORE-- prompt when page is filled. - - formatted_text = formatted_text + [("", end)] - lines = list(split_lines(formatted_text)) - - for lineno, line in enumerate(lines): - for style, text, *_ in line: - for c in text: - width = get_cwidth(c) - - # (Soft) wrap line if it doesn't fit. - if columns_in_buffer + width > size.columns: - # Show pager first if we get too many lines after - # wrapping. - if rows_in_buffer + 1 >= max_rows and not print_all: - page.append(("", "\n")) - flush_page() - show_pager() - if abort: - return - - rows_in_buffer += 1 - columns_in_buffer = 0 - - columns_in_buffer += width - page.append((style, c)) - - if rows_in_buffer + 1 >= max_rows and not print_all: - page.append(("", "\n")) - flush_page() - show_pager() - if abort: - return - else: - # Add line ending between lines (if `end="\n"` was given, one - # more empty line is added in `split_lines` automatically to - # take care of the final line ending). - if lineno != len(lines) - 1: - page.append(("", "\n")) - rows_in_buffer += 1 - columns_in_buffer = 0 - - flush_page() - - def create_pager_prompt(self) -> PromptSession[PagerResult]: - """ - Create pager --MORE-- prompt. - """ - return create_pager_prompt(self._current_style, self.title) - - def _format_exception_output(self, e: BaseException) -> PygmentsTokens: - # Instead of just calling ``traceback.format_exc``, we take the - # traceback and skip the bottom calls of this framework. - t, v, tb = sys.exc_info() - - # Required for pdb.post_mortem() to work. - sys.last_type, sys.last_value, sys.last_traceback = t, v, tb - - tblist = list(traceback.extract_tb(tb)) - - for line_nr, tb_tuple in enumerate(tblist): - if tb_tuple[0] == "": - tblist = tblist[line_nr:] - break - - tb_list = traceback.format_list(tblist) - if tb_list: - tb_list.insert(0, "Traceback (most recent call last):\n") - tb_list.extend(traceback.format_exception_only(t, v)) - - tb_str = "".join(tb_list) - - # Format exception and write to output. - # (We use the default style. Most other styles result - # in unreadable colors for the traceback.) - if self.enable_syntax_highlighting: - tokens = list(_lex_python_traceback(tb_str)) - else: - tokens = [(Token, tb_str)] - return PygmentsTokens(tokens) - def _handle_exception(self, e: BaseException) -> None: - output = self.app.output - - tokens = self._format_exception_output(e) - - print_formatted_text( - tokens, - style=self._current_style, - style_transformation=self.style_transformation, - include_default_pygments_style=False, - output=output, + self._get_output_printer().display_exception( + e, + highlight=self.enable_syntax_highlighting, + paginate=self.enable_pager, ) - output.flush() def _handle_keyboard_interrupt(self, e: KeyboardInterrupt) -> None: output = self.app.output @@ -602,21 +355,16 @@ def _remove_from_namespace(self) -> None: globals = self.get_globals() del globals["get_ptpython"] - -def _lex_python_traceback(tb): - "Return token list for traceback string." - lexer = PythonTracebackLexer() - return lexer.get_tokens(tb) - - -def _lex_python_result(tb): - "Return token list for Python string." - lexer = PythonLexer() - # Use `get_tokens_unprocessed`, so that we get exactly the same string, - # without line endings appended. `print_formatted_text` already appends a - # line ending, and otherwise we'll have two line endings. - tokens = lexer.get_tokens_unprocessed(tb) - return [(tokentype, value) for index, tokentype, value in tokens] + def print_paginated_formatted_text( + self, + formatted_text: Iterable[OneStyleAndTextTuple], + end: str = "\n", + ) -> None: + # Warning: This is mainly here backwards-compatibility. Some projects + # call `print_paginated_formatted_text` on the Repl object. + self._get_output_printer().display_style_and_text_tuples( + formatted_text, paginate=True + ) def enable_deprecation_warnings() -> None: @@ -746,67 +494,3 @@ async def coroutine() -> None: else: with patch_context: repl.run() - - -class PagerResult(Enum): - ABORT = "ABORT" - NEXT_LINE = "NEXT_LINE" - NEXT_PAGE = "NEXT_PAGE" - PRINT_ALL = "PRINT_ALL" - - -def create_pager_prompt( - style: BaseStyle, title: AnyFormattedText = "" -) -> PromptSession[PagerResult]: - """ - Create a "continue" prompt for paginated output. - """ - bindings = KeyBindings() - - @bindings.add("enter") - @bindings.add("down") - def next_line(event: KeyPressEvent) -> None: - event.app.exit(result=PagerResult.NEXT_LINE) - - @bindings.add("space") - def next_page(event: KeyPressEvent) -> None: - event.app.exit(result=PagerResult.NEXT_PAGE) - - @bindings.add("a") - def print_all(event: KeyPressEvent) -> None: - event.app.exit(result=PagerResult.PRINT_ALL) - - @bindings.add("q") - @bindings.add("c-c") - @bindings.add("c-d") - @bindings.add("escape", eager=True) - def no(event: KeyPressEvent) -> None: - event.app.exit(result=PagerResult.ABORT) - - @bindings.add("") - def _(event: KeyPressEvent) -> None: - "Disallow inserting other text." - pass - - style - - session: PromptSession[PagerResult] = PromptSession( - merge_formatted_text( - [ - title, - HTML( - "" - " -- MORE -- " - "[Enter] Scroll " - "[Space] Next page " - "[a] Print all " - "[q] Quit " - ": " - ), - ] - ), - key_bindings=bindings, - erase_when_done=True, - style=style, - ) - return session diff --git a/pyproject.toml b/pyproject.toml index d9d839ed..5421c454 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -26,6 +26,7 @@ ignore = [ "ptpython/entry_points/run_ptpython.py" = ["T201"] # Print usage. "ptpython/ipython.py" = ["T100"] # Import usage. "ptpython/repl.py" = ["T201"] # Print usage. +"ptpython/printer.py" = ["T201"] # Print usage. "tests/run_tests.py" = ["F401"] # Unused imports. From 6801f94006951e5c06f232862e40fa19cd58aa82 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Tue, 12 Dec 2023 20:27:49 +0000 Subject: [PATCH 181/220] Fix type annotations in various places. --- examples/asyncio-python-embed.py | 15 +++--- examples/asyncio-ssh-python-embed.py | 18 +++---- examples/python-embed-with-custom-prompt.py | 12 ++--- examples/python-embed.py | 2 +- examples/ssh-and-telnet-embed.py | 11 ++-- ptpython/contrib/asyncssh_repl.py | 26 +++++---- ptpython/python_input.py | 58 ++++++++++++++------- ptpython/repl.py | 2 +- 8 files changed, 80 insertions(+), 64 deletions(-) diff --git a/examples/asyncio-python-embed.py b/examples/asyncio-python-embed.py index 05f52f1d..a8fbba5a 100755 --- a/examples/asyncio-python-embed.py +++ b/examples/asyncio-python-embed.py @@ -19,7 +19,7 @@ counter = [0] -async def print_counter(): +async def print_counter() -> None: """ Coroutine that prints counters and saves it in a global variable. """ @@ -29,7 +29,7 @@ async def print_counter(): await asyncio.sleep(3) -async def interactive_shell(): +async def interactive_shell() -> None: """ Coroutine that starts a Python REPL from which we can access the global counter variable. @@ -44,13 +44,10 @@ async def interactive_shell(): loop.stop() -def main(): - asyncio.ensure_future(print_counter()) - asyncio.ensure_future(interactive_shell()) - - loop.run_forever() - loop.close() +async def main() -> None: + asyncio.create_task(print_counter()) + await interactive_shell() if __name__ == "__main__": - main() + asyncio.run(main()) diff --git a/examples/asyncio-ssh-python-embed.py b/examples/asyncio-ssh-python-embed.py index 86b56073..be0689e7 100755 --- a/examples/asyncio-ssh-python-embed.py +++ b/examples/asyncio-ssh-python-embed.py @@ -32,31 +32,25 @@ def session_requested(self): return ReplSSHServerSession(self.get_namespace) -def main(port=8222): +async def main(port: int = 8222) -> None: """ Example that starts the REPL through an SSH server. """ - loop = asyncio.get_event_loop() - # Namespace exposed in the REPL. environ = {"hello": "world"} # Start SSH server. - def create_server(): + def create_server() -> MySSHServer: return MySSHServer(lambda: environ) print("Listening on :%i" % port) print('To connect, do "ssh localhost -p %i"' % port) - loop.run_until_complete( - asyncssh.create_server( - create_server, "", port, server_host_keys=["/etc/ssh/ssh_host_dsa_key"] - ) + await asyncssh.create_server( + create_server, "", port, server_host_keys=["/etc/ssh/ssh_host_dsa_key"] ) - - # Run eventloop. - loop.run_forever() + await asyncio.Future() # Wait forever. if __name__ == "__main__": - main() + asyncio.run(main()) diff --git a/examples/python-embed-with-custom-prompt.py b/examples/python-embed-with-custom-prompt.py index 968aedc5..d54da1da 100755 --- a/examples/python-embed-with-custom-prompt.py +++ b/examples/python-embed-with-custom-prompt.py @@ -2,26 +2,26 @@ """ Example of embedding a Python REPL, and setting a custom prompt. """ -from prompt_toolkit.formatted_text import HTML +from prompt_toolkit.formatted_text import HTML, AnyFormattedText from ptpython.prompt_style import PromptStyle from ptpython.repl import embed -def configure(repl): +def configure(repl) -> None: # Probably, the best is to add a new PromptStyle to `all_prompt_styles` and # activate it. This way, the other styles are still selectable from the # menu. class CustomPrompt(PromptStyle): - def in_prompt(self): + def in_prompt(self) -> AnyFormattedText: return HTML("Input[%s]: ") % ( repl.current_statement_index, ) - def in2_prompt(self, width): + def in2_prompt(self, width: int) -> AnyFormattedText: return "...: ".rjust(width) - def out_prompt(self): + def out_prompt(self) -> AnyFormattedText: return HTML("Result[%s]: ") % ( repl.current_statement_index, ) @@ -30,7 +30,7 @@ def out_prompt(self): repl.prompt_style = "custom" -def main(): +def main() -> None: embed(globals(), locals(), configure=configure) diff --git a/examples/python-embed.py b/examples/python-embed.py index ac2cd06f..49224ac2 100755 --- a/examples/python-embed.py +++ b/examples/python-embed.py @@ -4,7 +4,7 @@ from ptpython.repl import embed -def main(): +def main() -> None: embed(globals(), locals(), vi_mode=False) diff --git a/examples/ssh-and-telnet-embed.py b/examples/ssh-and-telnet-embed.py index 378784ce..62fa76d9 100755 --- a/examples/ssh-and-telnet-embed.py +++ b/examples/ssh-and-telnet-embed.py @@ -11,13 +11,16 @@ import asyncssh from prompt_toolkit import print_formatted_text -from prompt_toolkit.contrib.ssh.server import PromptToolkitSSHServer +from prompt_toolkit.contrib.ssh.server import ( + PromptToolkitSSHServer, + PromptToolkitSSHSession, +) from prompt_toolkit.contrib.telnet.server import TelnetServer from ptpython.repl import embed -def ensure_key(filename="ssh_host_key"): +def ensure_key(filename: str = "ssh_host_key") -> str: path = pathlib.Path(filename) if not path.exists(): rsa_key = asyncssh.generate_private_key("ssh-rsa") @@ -25,12 +28,12 @@ def ensure_key(filename="ssh_host_key"): return str(path) -async def interact(connection=None): +async def interact(connection: PromptToolkitSSHSession) -> None: global_dict = {**globals(), "print": print_formatted_text} await embed(return_asyncio_coroutine=True, globals=global_dict) -async def main(ssh_port=8022, telnet_port=8023): +async def main(ssh_port: int = 8022, telnet_port: int = 8023) -> None: ssh_server = PromptToolkitSSHServer(interact=interact) await asyncssh.create_server( lambda: ssh_server, "", ssh_port, server_host_keys=[ensure_key()] diff --git a/ptpython/contrib/asyncssh_repl.py b/ptpython/contrib/asyncssh_repl.py index 051519de..35da7426 100644 --- a/ptpython/contrib/asyncssh_repl.py +++ b/ptpython/contrib/asyncssh_repl.py @@ -9,20 +9,20 @@ from __future__ import annotations import asyncio -from typing import Any, TextIO, cast +from typing import Any, AnyStr, TextIO, cast import asyncssh from prompt_toolkit.data_structures import Size from prompt_toolkit.input import create_pipe_input from prompt_toolkit.output.vt100 import Vt100_Output -from ptpython.python_input import _GetNamespace +from ptpython.python_input import _GetNamespace, _Namespace from ptpython.repl import PythonRepl __all__ = ["ReplSSHServerSession"] -class ReplSSHServerSession(asyncssh.SSHServerSession): +class ReplSSHServerSession(asyncssh.SSHServerSession[str]): """ SSH server session that runs a Python REPL. @@ -35,7 +35,7 @@ def __init__( ) -> None: self._chan: Any = None - def _globals() -> dict: + def _globals() -> _Namespace: data = get_globals() data.setdefault("print", self._print) return data @@ -79,7 +79,7 @@ def _get_size(self) -> Size: width, height, pixwidth, pixheight = self._chan.get_terminal_size() return Size(rows=height, columns=width) - def connection_made(self, chan): + def connection_made(self, chan: Any) -> None: """ Client connected, run repl in coroutine. """ @@ -89,7 +89,7 @@ def connection_made(self, chan): f = asyncio.ensure_future(self.repl.run_async()) # Close channel when done. - def done(_) -> None: + def done(_: object) -> None: chan.close() self._chan = None @@ -98,24 +98,28 @@ def done(_) -> None: def shell_requested(self) -> bool: return True - def terminal_size_changed(self, width, height, pixwidth, pixheight): + def terminal_size_changed( + self, width: int, height: int, pixwidth: int, pixheight: int + ) -> None: """ When the terminal size changes, report back to CLI. """ self.repl.app._on_resize() - def data_received(self, data, datatype): + def data_received(self, data: AnyStr, datatype: int | None) -> None: """ When data is received, send to inputstream of the CLI and repaint. """ self._input_pipe.send(data) - def _print(self, *data, sep=" ", end="\n", file=None) -> None: + def _print( + self, *data: object, sep: str = " ", end: str = "\n", file: Any = None + ) -> None: """ Alternative 'print' function that prints back into the SSH channel. """ # Pop keyword-only arguments. (We cannot use the syntax from the # signature. Otherwise, Python2 will give a syntax error message when # installing.) - data = sep.join(map(str, data)) - self._chan.write(data + end) + data_as_str = sep.join(map(str, data)) + self._chan.write(data_as_str + end) diff --git a/ptpython/python_input.py b/ptpython/python_input.py index 211d36c9..14995db4 100644 --- a/ptpython/python_input.py +++ b/ptpython/python_input.py @@ -6,7 +6,7 @@ from asyncio import get_event_loop from functools import partial -from typing import TYPE_CHECKING, Any, Callable, Dict, Generic, Mapping, TypeVar +from typing import TYPE_CHECKING, Any, Callable, Dict, Generic, Mapping, TypeVar, Union from prompt_toolkit.application import Application, get_app from prompt_toolkit.auto_suggest import ( @@ -31,7 +31,7 @@ ) from prompt_toolkit.document import Document from prompt_toolkit.enums import DEFAULT_BUFFER, EditingMode -from prompt_toolkit.filters import Condition +from prompt_toolkit.filters import Condition, FilterOrBool from prompt_toolkit.formatted_text import AnyFormattedText from prompt_toolkit.history import ( FileHistory, @@ -49,8 +49,13 @@ from prompt_toolkit.key_binding.bindings.open_in_editor import ( load_open_in_editor_bindings, ) +from prompt_toolkit.key_binding.key_bindings import Binding, KeyHandlerCallable +from prompt_toolkit.key_binding.key_processor import KeyPressEvent from prompt_toolkit.key_binding.vi_state import InputMode +from prompt_toolkit.keys import Keys from prompt_toolkit.layout.containers import AnyContainer +from prompt_toolkit.layout.dimension import AnyDimension +from prompt_toolkit.layout.processors import Processor from prompt_toolkit.lexers import DynamicLexer, Lexer, SimpleLexer from prompt_toolkit.output import ColorDepth, Output from prompt_toolkit.styles import ( @@ -91,22 +96,23 @@ from typing_extensions import Protocol class _SupportsLessThan(Protocol): - # Taken from typeshed. _T is used by "sorted", which needs anything + # Taken from typeshed. _T_lt is used by "sorted", which needs anything # sortable. def __lt__(self, __other: Any) -> bool: ... -_T = TypeVar("_T", bound="_SupportsLessThan") +_T_lt = TypeVar("_T_lt", bound="_SupportsLessThan") +_T_kh = TypeVar("_T_kh", bound=Union[KeyHandlerCallable, Binding]) -class OptionCategory(Generic[_T]): - def __init__(self, title: str, options: list[Option[_T]]) -> None: +class OptionCategory(Generic[_T_lt]): + def __init__(self, title: str, options: list[Option[_T_lt]]) -> None: self.title = title self.options = options -class Option(Generic[_T]): +class Option(Generic[_T_lt]): """ Ptpython configuration option that can be shown and modified from the sidebar. @@ -122,10 +128,10 @@ def __init__( self, title: str, description: str, - get_current_value: Callable[[], _T], + get_current_value: Callable[[], _T_lt], # We accept `object` as return type for the select functions, because # often they return an unused boolean. Maybe this can be improved. - get_values: Callable[[], Mapping[_T, Callable[[], object]]], + get_values: Callable[[], Mapping[_T_lt, Callable[[], object]]], ) -> None: self.title = title self.description = description @@ -133,7 +139,7 @@ def __init__( self.get_values = get_values @property - def values(self) -> Mapping[_T, Callable[[], object]]: + def values(self) -> Mapping[_T_lt, Callable[[], object]]: return self.get_values() def activate_next(self, _previous: bool = False) -> None: @@ -208,10 +214,10 @@ def __init__( _completer: Completer | None = None, _validator: Validator | None = None, _lexer: Lexer | None = None, - _extra_buffer_processors=None, + _extra_buffer_processors: list[Processor] | None = None, _extra_layout_body: AnyContainer | None = None, - _extra_toolbars=None, - _input_buffer_height=None, + _extra_toolbars: list[AnyContainer] | None = None, + _input_buffer_height: AnyDimension | None = None, ) -> None: self.get_globals: _GetNamespace = get_globals or (lambda: {}) self.get_locals: _GetNamespace = get_locals or self.get_globals @@ -466,24 +472,36 @@ def get_compiler_flags(self) -> int: return flags - @property - def add_key_binding(self) -> Callable[[_T], _T]: + def add_key_binding( + self, + *keys: Keys | str, + filter: FilterOrBool = True, + eager: FilterOrBool = False, + is_global: FilterOrBool = False, + save_before: Callable[[KeyPressEvent], bool] = (lambda e: True), + record_in_macro: FilterOrBool = True, + ) -> Callable[[_T_kh], _T_kh]: """ Shortcut for adding new key bindings. (Mostly useful for a config.py file, that receives a PythonInput/Repl instance as input.) + All arguments are identical to prompt_toolkit's `KeyBindings.add`. + :: @python_input.add_key_binding(Keys.ControlX, filter=...) def handler(event): ... """ - - def add_binding_decorator(*k, **kw): - return self.extra_key_bindings.add(*k, **kw) - - return add_binding_decorator + return self.extra_key_bindings.add( + *keys, + filter=filter, + eager=eager, + is_global=is_global, + save_before=save_before, + record_in_macro=record_in_macro, + ) def install_code_colorscheme(self, name: str, style: BaseStyle) -> None: """ diff --git a/ptpython/repl.py b/ptpython/repl.py index 98b01afa..1db2e64f 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -158,7 +158,7 @@ def run(self) -> None: clear_title() self._remove_from_namespace() - async def run_and_show_expression_async(self, text: str): + async def run_and_show_expression_async(self, text: str) -> object: loop = asyncio.get_event_loop() try: From f0193017e5a38b07e9ad1dfdac8bc5e416229089 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Tue, 12 Dec 2023 22:22:00 +0000 Subject: [PATCH 182/220] Many asyncio REPL improvements. - Added `--asyncio` flag to the `ptpython` entry point to activate the asyncio-REPL. This will ensure that an event loop is created at the start in which we can run top-level await statements. - Use `get_running_loop()` instead of `get_event_loop()`. - Better handling of `SystemExit` and control-c in the async REPL. --- ptpython/contrib/asyncssh_repl.py | 2 +- ptpython/entry_points/run_ptpython.py | 13 ++++- ptpython/python_input.py | 4 +- ptpython/repl.py | 76 +++++++++++++++++++-------- 4 files changed, 70 insertions(+), 25 deletions(-) diff --git a/ptpython/contrib/asyncssh_repl.py b/ptpython/contrib/asyncssh_repl.py index 35da7426..2f74eb2b 100644 --- a/ptpython/contrib/asyncssh_repl.py +++ b/ptpython/contrib/asyncssh_repl.py @@ -110,7 +110,7 @@ def data_received(self, data: AnyStr, datatype: int | None) -> None: """ When data is received, send to inputstream of the CLI and repaint. """ - self._input_pipe.send(data) + self._input_pipe.send(data) # type: ignore def _print( self, *data: object, sep: str = " ", end: str = "\n", file: Any = None diff --git a/ptpython/entry_points/run_ptpython.py b/ptpython/entry_points/run_ptpython.py index c0b4078b..7fa69c66 100644 --- a/ptpython/entry_points/run_ptpython.py +++ b/ptpython/entry_points/run_ptpython.py @@ -9,6 +9,7 @@ -h, --help show this help message and exit --vi Enable Vi key bindings -i, --interactive Start interactive shell after executing this file. + --asyncio Run an asyncio event loop to support top-level "await". --light-bg Run on a light background (use dark colors for text). --dark-bg Run on a dark background (use light colors for text). --config-file CONFIG_FILE @@ -24,6 +25,7 @@ from __future__ import annotations import argparse +import asyncio import os import pathlib import sys @@ -68,6 +70,11 @@ def create_parser() -> _Parser: action="store_true", help="Start interactive shell after executing this file.", ) + parser.add_argument( + "--asyncio", + action="store_true", + help='Run an asyncio event loop to support top-level "await".', + ) parser.add_argument( "--light-bg", action="store_true", @@ -206,7 +213,7 @@ def configure(repl: PythonRepl) -> None: import __main__ - embed( + embed_result = embed( # type: ignore vi_mode=a.vi, history_filename=history_file, configure=configure, @@ -214,8 +221,12 @@ def configure(repl: PythonRepl) -> None: globals=__main__.__dict__, startup_paths=startup_paths, title="Python REPL (ptpython)", + return_asyncio_coroutine=a.asyncio, ) + if a.asyncio: + asyncio.run(embed_result) + if __name__ == "__main__": run() diff --git a/ptpython/python_input.py b/ptpython/python_input.py index 14995db4..54ddbef2 100644 --- a/ptpython/python_input.py +++ b/ptpython/python_input.py @@ -4,7 +4,7 @@ """ from __future__ import annotations -from asyncio import get_event_loop +from asyncio import get_running_loop from functools import partial from typing import TYPE_CHECKING, Any, Callable, Dict, Generic, Mapping, TypeVar, Union @@ -1010,7 +1010,7 @@ def get_signatures_in_executor(document: Document) -> list[Signature]: app = self.app async def on_timeout_task() -> None: - loop = get_event_loop() + loop = get_running_loop() # Never run multiple get-signature threads. if self._get_signatures_thread_running: diff --git a/ptpython/repl.py b/ptpython/repl.py index 1db2e64f..e7058ea1 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -12,6 +12,7 @@ import asyncio import builtins import os +import signal import sys import traceback import types @@ -158,27 +159,58 @@ def run(self) -> None: clear_title() self._remove_from_namespace() - async def run_and_show_expression_async(self, text: str) -> object: - loop = asyncio.get_event_loop() + async def run_and_show_expression_async(self, text: str) -> Any: + loop = asyncio.get_running_loop() + system_exit: SystemExit | None = None try: - result = await self.eval_async(text) - except KeyboardInterrupt: # 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)) + try: + # Create `eval` task. Ensure that control-c will cancel this + # task. + async def eval() -> Any: + nonlocal system_exit + try: + return await self.eval_async(text) + except SystemExit as e: + # Don't propagate SystemExit in `create_task()`. That + # will kill the event loop. We want to handle it + # gracefully. + system_exit = e + + task = asyncio.create_task(eval()) + loop.add_signal_handler(signal.SIGINT, lambda *_: task.cancel()) + result = await task + + if system_exit is not None: + raise system_exit + except KeyboardInterrupt: + # KeyboardInterrupt doesn't inherit from Exception. + raise + except SystemExit: + raise + 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 = [] - # Return the result for future consumers. - return result + # Loop. + self.current_statement_index += 1 + self.signatures = [] + # Return the result for future consumers. + return result + finally: + loop.remove_signal_handler(signal.SIGINT) + + 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) async def run_async(self) -> None: """ @@ -192,7 +224,7 @@ async def run_async(self) -> None: (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() + loop = asyncio.get_running_loop() if self.terminal_title: set_title(self.terminal_title) @@ -222,6 +254,8 @@ async def run_async(self) -> None: # `KeyboardInterrupt` exceptions can end up in the event # loop selector. self._handle_keyboard_interrupt(e) + except SystemExit: + return finally: if self.terminal_title: clear_title() @@ -250,7 +284,7 @@ def eval(self, line: str) -> object: result = eval(code, self.get_globals(), self.get_locals()) if _has_coroutine_flag(code): - result = asyncio.get_event_loop().run_until_complete(result) + result = asyncio.get_running_loop().run_until_complete(result) self._store_eval_result(result) return result @@ -263,7 +297,7 @@ def eval(self, line: str) -> object: result = eval(code, self.get_globals(), self.get_locals()) if _has_coroutine_flag(code): - result = asyncio.get_event_loop().run_until_complete(result) + result = asyncio.get_running_loop().run_until_complete(result) return None From eb39a3201eb6f45f95f1d47434c5e31f3bd4ed36 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Tue, 12 Dec 2023 22:30:49 +0000 Subject: [PATCH 183/220] Show help information when starting asyncio-REPL. --- ptpython/entry_points/run_ptpython.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ptpython/entry_points/run_ptpython.py b/ptpython/entry_points/run_ptpython.py index 7fa69c66..1d4a5329 100644 --- a/ptpython/entry_points/run_ptpython.py +++ b/ptpython/entry_points/run_ptpython.py @@ -225,6 +225,8 @@ def configure(repl: PythonRepl) -> None: ) if a.asyncio: + print("Starting ptpython asyncio REPL") + print('Use "await" directly instead of "asyncio.run()".') asyncio.run(embed_result) From 96d621cf305ae4cf9a29db5d92f0a5b510470cf0 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Tue, 12 Dec 2023 22:34:23 +0000 Subject: [PATCH 184/220] Added info about top-level await to the README. --- README.rst | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.rst b/README.rst index 2db3f695..8ec9aca4 100644 --- a/README.rst +++ b/README.rst @@ -71,6 +71,7 @@ The help menu shows basic command-line options. -h, --help show this help message and exit --vi Enable Vi key bindings -i, --interactive Start interactive shell after executing this file. + --asyncio Run an asyncio event loop to support top-level "await". --light-bg Run on a light background (use dark colors for text). --dark-bg Run on a dark background (use light colors for text). --config-file CONFIG_FILE @@ -171,6 +172,20 @@ error. .. image :: https://github.com/jonathanslenders/ptpython/raw/master/docs/images/validation.png +Asyncio REPL and top level await +******************************** + +In order to get top-level ``await`` support, start ptpython as follows: + +.. code:: + + ptpython --asyncio + +This will spawn an asyncio event loop and embed the async REPL in the event +loop. After this, top-level await will work and statements like ``await +asyncio.sleep(10)`` will execute. + + Additional features ******************* From eda7f58d453c3c1b96e4357dfa203f3160cfc4c1 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Wed, 13 Dec 2023 09:33:54 +0000 Subject: [PATCH 185/220] Required prompt_toolkit 3.0.34 because of 'OneStyleAndTextTuple' import. --- setup.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index ad26545a..d091d290 100644 --- a/setup.py +++ b/setup.py @@ -21,8 +21,8 @@ "appdirs", "importlib_metadata;python_version<'3.8'", "jedi>=0.16.0", - # Use prompt_toolkit 3.0.28, because of cursor shape support. - "prompt_toolkit>=3.0.28,<3.1.0", + # Use prompt_toolkit 3.0.34, because of `OneStyleAndTextTuple` import. + "prompt_toolkit>=3.0.34,<3.1.0", "pygments", ], python_requires=">=3.7", From d2e35e7c617a015299ce10b53d30067b347b03c9 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Wed, 13 Dec 2023 09:35:13 +0000 Subject: [PATCH 186/220] Release 3.0.24 --- CHANGELOG | 20 ++++++++++++++++++++ setup.py | 2 +- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index e753cfd9..879e7439 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,26 @@ CHANGELOG ========= +3.0.24: 2023-12-13 +------------------ + +Fixes: +- Don't show "Impossible to read config file" warnings when no config file was + passed to `run_config()`. +- IPython integration fixes: + * Fix top-level await in IPython. + * Fix IPython `DeprecationWarning`. +- Output printing fixes: + * Paginate exceptions if pagination is enabled. + * Handle big outputs without running out of memory. +- Asyncio REPL improvements: + * From now on, passing `--asyncio` is required to activate the asyncio-REPL. + This will ensure that an event loop is created at the start in which we can + run top-level await statements. + * Use `get_running_loop()` instead of `get_event_loop()`. + * Better handling of `SystemExit` and control-c in the async REPL. + + 3.0.23: 2023-02-22 ------------------ diff --git a/setup.py b/setup.py index d091d290..a35a4797 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ setup( name="ptpython", author="Jonathan Slenders", - version="3.0.23", + version="3.0.24", url="https://github.com/prompt-toolkit/ptpython", description="Python REPL build on top of prompt_toolkit", long_description=long_description, From 961b945abb20d4d57615da97905d5d00ab10f1fe Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Wed, 13 Dec 2023 12:12:04 +0000 Subject: [PATCH 187/220] Fix handling of 'config file does not exist' when embedding ptpython. --- ptpython/repl.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/ptpython/repl.py b/ptpython/repl.py index e7058ea1..fc9b9da1 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -433,9 +433,10 @@ def enter_to_continue() -> None: input("\nPress ENTER to continue...") # Check whether this file exists. - if not os.path.exists(config_file) and explicit_config_file: - print("Impossible to read %r" % config_file) - enter_to_continue() + if not os.path.exists(config_file): + if explicit_config_file: + print(f"Impossible to read {config_file}") + enter_to_continue() return # Run the config file in an empty namespace. From 1a96f0ee6a2691c18dd91d756d045f488975faec Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Thu, 14 Dec 2023 09:33:03 +0000 Subject: [PATCH 188/220] Release 3.0.25 --- CHANGELOG | 7 +++++++ setup.py | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 879e7439..e8277002 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,13 @@ CHANGELOG ========= +3.0.25: 2023-12-14 +------------------ + +Fixes: +- Fix handling of 'config file does not exist' when embedding ptpython. + + 3.0.24: 2023-12-13 ------------------ diff --git a/setup.py b/setup.py index a35a4797..bc1241bb 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ setup( name="ptpython", author="Jonathan Slenders", - version="3.0.24", + version="3.0.25", url="https://github.com/prompt-toolkit/ptpython", description="Python REPL build on top of prompt_toolkit", long_description=long_description, From 17d04b9f9d4c812ed4d161c110fe9cd54069c4be Mon Sep 17 00:00:00 2001 From: tomaszchalupnik Date: Fri, 2 Feb 2024 22:28:50 +0100 Subject: [PATCH 189/220] Reraise GeneratorExit error as excepted exception --- ptpython/printer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ptpython/printer.py b/ptpython/printer.py index 3618934e..85bd9c88 100644 --- a/ptpython/printer.py +++ b/ptpython/printer.py @@ -155,7 +155,7 @@ def _format_result_output( ) yield from formatted_result_repr return - except KeyboardInterrupt: + except (GeneratorExit, KeyboardInterrupt): raise # Don't catch here. except: # For bad code, `__getattr__` can raise something that's not an From 1c558f861c2d47ad7bdf639567fa9a5c9237ade1 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Tue, 6 Feb 2024 10:16:06 +0000 Subject: [PATCH 190/220] Release 3.0.26 --- CHANGELOG | 7 +++++++ setup.py | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index e8277002..d8738625 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,13 @@ CHANGELOG ========= +3.0.26: 2024-02-06 +------------------ + +Fixes: +- Handle `GeneratorExit` exception when leaving the paginator. + + 3.0.25: 2023-12-14 ------------------ diff --git a/setup.py b/setup.py index bc1241bb..a54da35d 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ setup( name="ptpython", author="Jonathan Slenders", - version="3.0.25", + version="3.0.26", url="https://github.com/prompt-toolkit/ptpython", description="Python REPL build on top of prompt_toolkit", long_description=long_description, From 4f6b3a3d8a60387cf9e22e6112a320809ab91679 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Sun, 17 Dec 2023 07:11:01 -0600 Subject: [PATCH 191/220] Package: Add PyPI Links to repo, issues, and changelog --- setup.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/setup.py b/setup.py index a54da35d..38f30282 100644 --- a/setup.py +++ b/setup.py @@ -15,6 +15,14 @@ url="https://github.com/prompt-toolkit/ptpython", description="Python REPL build on top of prompt_toolkit", long_description=long_description, + package_urls={ + "Changelog": "https://github.com/prompt-toolkit/ptpython/blob/master/CHANGELOG", + }, + project_urls={ + "Bug Tracker": "https://github.com/prompt-toolkit/ptpython/issues", + "Source Code": "https://github.com/prompt-toolkit/ptpython", + "Changelog": "https://github.com/prompt-toolkit/ptpython/blob/master/CHANGELOG", + }, packages=find_packages("."), package_data={"ptpython": ["py.typed"]}, install_requires=[ From d63ebc5cdb60fd57db524eaee97b099acf45dee6 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Sun, 17 Dec 2023 07:01:28 -0600 Subject: [PATCH 192/220] docs(README): Update GitHub action button --- README.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index 8ec9aca4..8616132a 100644 --- a/README.rst +++ b/README.rst @@ -288,8 +288,8 @@ Special thanks to - `wcwidth `_: Determine columns needed for a wide characters. - `prompt_toolkit `_ for the interface. -.. |Build Status| image:: https://api.travis-ci.org/prompt-toolkit/ptpython.svg?branch=master - :target: https://travis-ci.org/prompt-toolkit/ptpython# +.. |Build Status| image:: https://github.com/prompt-toolkit/ptpython/actions/workflows/test.yaml/badge.svg + :target: https://github.com/prompt-toolkit/ptpython/actions/workflows/test.yaml .. |License| image:: https://img.shields.io/github/license/prompt-toolkit/ptpython.svg :target: https://github.com/prompt-toolkit/ptpython/blob/master/LICENSE From f40e091012e9022babafe5a077bea7da154e3b39 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Sun, 17 Dec 2023 07:02:33 -0600 Subject: [PATCH 193/220] docs(README): Fix PyPI badge and link --- README.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index 8616132a..63d9aca5 100644 --- a/README.rst +++ b/README.rst @@ -294,6 +294,6 @@ Special thanks to .. |License| image:: https://img.shields.io/github/license/prompt-toolkit/ptpython.svg :target: https://github.com/prompt-toolkit/ptpython/blob/master/LICENSE -.. |PyPI| image:: https://pypip.in/version/ptpython/badge.svg - :target: https://pypi.python.org/pypi/ptpython/ +.. |PyPI| image:: https://img.shields.io/pypi/v/ptpython.svg + :target: https://pypi.org/project/ptpython/ :alt: Latest Version From 7f76e0df8697fd134e4d785343e143ba3b2f0780 Mon Sep 17 00:00:00 2001 From: Matthew Judy Date: Tue, 27 Feb 2024 18:07:25 -0500 Subject: [PATCH 194/220] Update `prompt_toolkit` from `3.0.34` to `3.0.43` Resolves https://github.com/prompt-toolkit/ptpython/issues/564 where `cannot import name 'OneStyleAndTextTuple'` is emitted when launching `ptipython`. --- setup.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index 38f30282..b2fde169 100644 --- a/setup.py +++ b/setup.py @@ -29,8 +29,8 @@ "appdirs", "importlib_metadata;python_version<'3.8'", "jedi>=0.16.0", - # Use prompt_toolkit 3.0.34, because of `OneStyleAndTextTuple` import. - "prompt_toolkit>=3.0.34,<3.1.0", + # Use prompt_toolkit 3.0.43, because of `OneStyleAndTextTuple` import. + "prompt_toolkit>=3.0.43,<3.1.0", "pygments", ], python_requires=">=3.7", From 3df92f35d86f048f5c634c3c8ba853ad7bc80568 Mon Sep 17 00:00:00 2001 From: "David J. Mack" Date: Fri, 18 Nov 2022 16:32:48 +0100 Subject: [PATCH 195/220] docs: Add windows terminal profile configuration --- README.rst | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.rst b/README.rst index 63d9aca5..130e4581 100644 --- a/README.rst +++ b/README.rst @@ -255,6 +255,22 @@ Windows. Some things might not work, but it is usable: .. image :: https://github.com/jonathanslenders/ptpython/raw/master/docs/images/windows.png +Windows terminal integration +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +If you are using the `Windows Terminal `_ and want to +integrate ``ptpython`` as a profile, go to *Settings -> Open JSON file* and add the +following profile under *profiles.list*: + +.. code-block:: JSON + + { + "commandline": "%SystemRoot%\\System32\\cmd.exe /k ptpython", + "guid": "{f91d49a3-741b-409c-8a15-c4360649121f}", + "hidden": false, + "icon": "https://upload.wikimedia.org/wikipedia/commons/e/e6/Python_Windows_interpreter_icon_2006%E2%80%932016_Tiny.png", + "name": "ptpython@cmd" + } FAQ *** From 394fe38a2ec1206131036d901688dd695bdda439 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Thu, 16 May 2024 12:05:31 +0000 Subject: [PATCH 196/220] Limit number of completions to 5k (for performance). --- ptpython/completer.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ptpython/completer.py b/ptpython/completer.py index 91d66474..264918e8 100644 --- a/ptpython/completer.py +++ b/ptpython/completer.py @@ -6,6 +6,7 @@ import keyword import re from enum import Enum +from itertools import islice from typing import TYPE_CHECKING, Any, Callable, Iterable from prompt_toolkit.completion import ( @@ -617,7 +618,10 @@ def __init__( def get_completions( self, document: Document, complete_event: CompleteEvent ) -> Iterable[Completion]: - completions = list(self.completer.get_completions(document, complete_event)) + completions = list( + # Limit at 5k completions for performance. + islice(self.completer.get_completions(document, complete_event), 0, 5000) + ) complete_private_attributes = self.complete_private_attributes() hide_private = False From 5fb21bd51f71d220c018fca9d732df48c72c52b8 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Thu, 16 May 2024 12:07:47 +0000 Subject: [PATCH 197/220] Apply latest Ruff for formatting. --- examples/asyncio-python-embed.py | 1 + examples/asyncio-ssh-python-embed.py | 1 + examples/ptpython_config/config.py | 1 + examples/python-embed-with-custom-prompt.py | 1 + examples/python-embed.py | 4 ++-- examples/python-input.py | 4 ++-- examples/test-cases/ptpython-in-other-thread.py | 1 + ptpython/__main__.py | 1 + ptpython/contrib/asyncssh_repl.py | 1 + ptpython/entry_points/run_ptpython.py | 1 + ptpython/eventloop.py | 1 + ptpython/history_browser.py | 1 + ptpython/ipython.py | 1 + ptpython/layout.py | 1 + ptpython/python_input.py | 4 ++-- ptpython/repl.py | 1 + ptpython/signatures.py | 1 + ptpython/utils.py | 1 + pyproject.toml | 8 ++++---- 19 files changed, 25 insertions(+), 10 deletions(-) diff --git a/examples/asyncio-python-embed.py b/examples/asyncio-python-embed.py index a8fbba5a..38cc1c20 100755 --- a/examples/asyncio-python-embed.py +++ b/examples/asyncio-python-embed.py @@ -11,6 +11,7 @@ to stdout, it won't break the input line, but instead writes nicely above the prompt. """ + import asyncio from ptpython.repl import embed diff --git a/examples/asyncio-ssh-python-embed.py b/examples/asyncio-ssh-python-embed.py index be0689e7..9bbad86f 100755 --- a/examples/asyncio-ssh-python-embed.py +++ b/examples/asyncio-ssh-python-embed.py @@ -5,6 +5,7 @@ Run this example and then SSH to localhost, port 8222. """ + import asyncio import logging diff --git a/examples/ptpython_config/config.py b/examples/ptpython_config/config.py index b25850a2..bfd3914e 100644 --- a/examples/ptpython_config/config.py +++ b/examples/ptpython_config/config.py @@ -5,6 +5,7 @@ On Linux, this is: ~/.config/ptpython/config.py On macOS, this is: ~/Library/Application Support/ptpython/config.py """ + from prompt_toolkit.filters import ViInsertMode from prompt_toolkit.key_binding.key_processor import KeyPress from prompt_toolkit.keys import Keys diff --git a/examples/python-embed-with-custom-prompt.py b/examples/python-embed-with-custom-prompt.py index d54da1da..5e8c7079 100755 --- a/examples/python-embed-with-custom-prompt.py +++ b/examples/python-embed-with-custom-prompt.py @@ -2,6 +2,7 @@ """ Example of embedding a Python REPL, and setting a custom prompt. """ + from prompt_toolkit.formatted_text import HTML, AnyFormattedText from ptpython.prompt_style import PromptStyle diff --git a/examples/python-embed.py b/examples/python-embed.py index 49224ac2..a7481011 100755 --- a/examples/python-embed.py +++ b/examples/python-embed.py @@ -1,6 +1,6 @@ #!/usr/bin/env python -""" -""" +""" """ + from ptpython.repl import embed diff --git a/examples/python-input.py b/examples/python-input.py index 567c2ee6..d586d0f5 100755 --- a/examples/python-input.py +++ b/examples/python-input.py @@ -1,6 +1,6 @@ #!/usr/bin/env python -""" -""" +""" """ + from ptpython.python_input import PythonInput diff --git a/examples/test-cases/ptpython-in-other-thread.py b/examples/test-cases/ptpython-in-other-thread.py index 7c788464..bfe14109 100644 --- a/examples/test-cases/ptpython-in-other-thread.py +++ b/examples/test-cases/ptpython-in-other-thread.py @@ -5,6 +5,7 @@ (For testing whether it's working fine if it's not embedded in the main thread.) """ + import threading from ptpython.repl import embed diff --git a/ptpython/__main__.py b/ptpython/__main__.py index c0062613..3a2f7ddf 100644 --- a/ptpython/__main__.py +++ b/ptpython/__main__.py @@ -1,6 +1,7 @@ """ Make `python -m ptpython` an alias for running `./ptpython`. """ + from __future__ import annotations from .entry_points.run_ptpython import run diff --git a/ptpython/contrib/asyncssh_repl.py b/ptpython/contrib/asyncssh_repl.py index 2f74eb2b..a86737b6 100644 --- a/ptpython/contrib/asyncssh_repl.py +++ b/ptpython/contrib/asyncssh_repl.py @@ -6,6 +6,7 @@ should make sure not to use Python 3-only syntax, because this package should be installable in Python 2 as well! """ + from __future__ import annotations import asyncio diff --git a/ptpython/entry_points/run_ptpython.py b/ptpython/entry_points/run_ptpython.py index 1d4a5329..05df9714 100644 --- a/ptpython/entry_points/run_ptpython.py +++ b/ptpython/entry_points/run_ptpython.py @@ -22,6 +22,7 @@ PTPYTHON_CONFIG_HOME: a configuration directory to use PYTHONSTARTUP: file executed on interactive startup (no default) """ + from __future__ import annotations import argparse diff --git a/ptpython/eventloop.py b/ptpython/eventloop.py index 14ab64be..670d09bc 100644 --- a/ptpython/eventloop.py +++ b/ptpython/eventloop.py @@ -7,6 +7,7 @@ in readline. ``prompt-toolkit`` doesn't understand that input hook, but this will fix it for Tk.) """ + from __future__ import annotations import sys diff --git a/ptpython/history_browser.py b/ptpython/history_browser.py index b667be12..383cd975 100644 --- a/ptpython/history_browser.py +++ b/ptpython/history_browser.py @@ -4,6 +4,7 @@ `create_history_application` creates an `Application` instance that runs will run as a sub application of the Repl/PythonInput. """ + from __future__ import annotations from functools import partial diff --git a/ptpython/ipython.py b/ptpython/ipython.py index ad0516a3..263a981d 100644 --- a/ptpython/ipython.py +++ b/ptpython/ipython.py @@ -8,6 +8,7 @@ offer. """ + from __future__ import annotations from typing import Iterable diff --git a/ptpython/layout.py b/ptpython/layout.py index 2c1ec15f..fc00005b 100644 --- a/ptpython/layout.py +++ b/ptpython/layout.py @@ -1,6 +1,7 @@ """ Creation of the `Layout` instance for the Python input/REPL. """ + from __future__ import annotations import platform diff --git a/ptpython/python_input.py b/ptpython/python_input.py index 54ddbef2..18421c88 100644 --- a/ptpython/python_input.py +++ b/ptpython/python_input.py @@ -2,6 +2,7 @@ Application for reading Python input. This can be used for creation of Python REPLs. """ + from __future__ import annotations from asyncio import get_running_loop @@ -98,8 +99,7 @@ class _SupportsLessThan(Protocol): # Taken from typeshed. _T_lt is used by "sorted", which needs anything # sortable. - def __lt__(self, __other: Any) -> bool: - ... + def __lt__(self, __other: Any) -> bool: ... _T_lt = TypeVar("_T_lt", bound="_SupportsLessThan") diff --git a/ptpython/repl.py b/ptpython/repl.py index fc9b9da1..bbbd852e 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -7,6 +7,7 @@ embed(globals(), locals(), vi_mode=False) """ + from __future__ import annotations import asyncio diff --git a/ptpython/signatures.py b/ptpython/signatures.py index d4cb98c2..b3e5c914 100644 --- a/ptpython/signatures.py +++ b/ptpython/signatures.py @@ -5,6 +5,7 @@ Either with the Jedi library, or using `inspect.signature` if Jedi fails and we can use `eval()` to evaluate the function object. """ + from __future__ import annotations import inspect diff --git a/ptpython/utils.py b/ptpython/utils.py index 28887d20..92cfc2a1 100644 --- a/ptpython/utils.py +++ b/ptpython/utils.py @@ -1,6 +1,7 @@ """ For internal use only. """ + from __future__ import annotations import re diff --git a/pyproject.toml b/pyproject.toml index 5421c454..ce420372 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.ruff] target-version = "py37" -select = [ +lint.select = [ "E", # pycodestyle errors "W", # pycodestyle warnings "F", # pyflakes @@ -12,14 +12,14 @@ select = [ "RUF100", # unused-noqa "Q", # quotes ] -ignore = [ +lint.ignore = [ "E501", # Line too long, handled by black "C901", # Too complex "E722", # bare except. ] -[tool.ruff.per-file-ignores] +[tool.ruff.lint.per-file-ignores] "examples/*" = ["T201"] # Print allowed in examples. "examples/ptpython_config/config.py" = ["F401"] # Unused imports in config. "ptpython/entry_points/run_ptipython.py" = ["T201", "F401"] # Print, import usage. @@ -30,6 +30,6 @@ ignore = [ "tests/run_tests.py" = ["F401"] # Unused imports. -[tool.ruff.isort] +[tool.ruff.lint.isort] known-first-party = ["ptpython"] known-third-party = ["prompt_toolkit", "pygments", "asyncssh"] From 3ec97d7360450f9d79a745a67e14312243227825 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Thu, 16 May 2024 12:09:19 +0000 Subject: [PATCH 198/220] Apply latest ruff fixes. --- ptpython/history_browser.py | 2 +- ptpython/ipython.py | 9 ++++----- ptpython/layout.py | 4 ++-- ptpython/prompt_style.py | 4 ++-- ptpython/python_input.py | 8 ++++---- ptpython/validator.py | 2 +- setup.py | 5 ++--- 7 files changed, 16 insertions(+), 18 deletions(-) diff --git a/ptpython/history_browser.py b/ptpython/history_browser.py index 383cd975..ae0ac03e 100644 --- a/ptpython/history_browser.py +++ b/ptpython/history_browser.py @@ -411,7 +411,7 @@ def __init__( if len(history_strings) > HISTORY_COUNT: history_lines[0] = ( - "# *** History has been truncated to %s lines ***" % HISTORY_COUNT + f"# *** History has been truncated to {HISTORY_COUNT} lines ***" ) self.history_lines = history_lines diff --git a/ptpython/ipython.py b/ptpython/ipython.py index 263a981d..0692214d 100644 --- a/ptpython/ipython.py +++ b/ptpython/ipython.py @@ -157,7 +157,7 @@ def get_completions( for m in sorted(self.magics_manager.magics["line"]): if m.startswith(text): - yield Completion("%s" % m, -len(text)) + yield Completion(f"{m}", -len(text)) class AliasCompleter(Completer): @@ -173,7 +173,7 @@ def get_completions( for a, cmd in sorted(aliases, key=lambda a: a[0]): if a.startswith(text): - yield Completion("%s" % a, -len(text), display_meta=cmd) + yield Completion(f"{a}", -len(text), display_meta=cmd) class IPythonInput(PythonInput): @@ -280,9 +280,8 @@ def initialize_extensions(shell, extensions): shell.extension_manager.load_extension(ext) except: warn( - "Error in loading extension: %s" % ext - + "\nCheck your config files in %s" - % ipy_utils.path.get_ipython_dir() + f"Error in loading extension: {ext}" + + f"\nCheck your config files in {ipy_utils.path.get_ipython_dir()}" ) shell.showtraceback() diff --git a/ptpython/layout.py b/ptpython/layout.py index fc00005b..622df594 100644 --- a/ptpython/layout.py +++ b/ptpython/layout.py @@ -132,7 +132,7 @@ def goto_next(mouse_event: MouseEvent) -> None: tokens.append(("class:sidebar" + sel, " >" if selected else " ")) tokens.append(("class:sidebar.label" + sel, "%-24s" % label, select_item)) tokens.append(("class:sidebar.status" + sel, " ", select_item)) - tokens.append(("class:sidebar.status" + sel, "%s" % status, goto_next)) + tokens.append(("class:sidebar.status" + sel, f"{status}", goto_next)) if selected: tokens.append(("[SetCursorPosition]", "")) @@ -529,7 +529,7 @@ def create_exit_confirmation( def get_text_fragments() -> StyleAndTextTuples: # Show "Do you really want to exit?" return [ - (style, "\n %s ([y]/n) " % python_input.exit_message), + (style, f"\n {python_input.exit_message} ([y]/n) "), ("[SetCursorPosition]", ""), (style, " \n"), ] diff --git a/ptpython/prompt_style.py b/ptpython/prompt_style.py index 96b738f7..465c3dbe 100644 --- a/ptpython/prompt_style.py +++ b/ptpython/prompt_style.py @@ -48,7 +48,7 @@ def __init__(self, python_input: PythonInput) -> None: def in_prompt(self) -> AnyFormattedText: return [ ("class:in", "In ["), - ("class:in.number", "%s" % self.python_input.current_statement_index), + ("class:in.number", f"{self.python_input.current_statement_index}"), ("class:in", "]: "), ] @@ -58,7 +58,7 @@ def in2_prompt(self, width: int) -> AnyFormattedText: def out_prompt(self) -> AnyFormattedText: return [ ("class:out", "Out["), - ("class:out.number", "%s" % self.python_input.current_statement_index), + ("class:out.number", f"{self.python_input.current_statement_index}"), ("class:out", "]:"), ("", " "), ] diff --git a/ptpython/python_input.py b/ptpython/python_input.py index 18421c88..d66b5ae8 100644 --- a/ptpython/python_input.py +++ b/ptpython/python_input.py @@ -880,18 +880,18 @@ def get_values() -> dict[str, Callable[[], bool]]: Option( title="Min brightness", description="Minimum brightness for the color scheme (default=0.0).", - get_current_value=lambda: "%.2f" % self.min_brightness, + get_current_value=lambda: f"{self.min_brightness:.2f}", get_values=lambda: { - "%.2f" % value: partial(self._set_min_brightness, value) + f"{value:.2f}": partial(self._set_min_brightness, value) for value in brightness_values }, ), Option( title="Max brightness", description="Maximum brightness for the color scheme (default=1.0).", - get_current_value=lambda: "%.2f" % self.max_brightness, + get_current_value=lambda: f"{self.max_brightness:.2f}", get_values=lambda: { - "%.2f" % value: partial(self._set_max_brightness, value) + f"{value:.2f}": partial(self._set_max_brightness, value) for value in brightness_values }, ), diff --git a/ptpython/validator.py b/ptpython/validator.py index 91b9c284..cf2ee542 100644 --- a/ptpython/validator.py +++ b/ptpython/validator.py @@ -59,4 +59,4 @@ def validate(self, document: Document) -> None: except ValueError as e: # In Python 2, compiling "\x9" (an invalid escape sequence) raises # ValueError instead of SyntaxError. - raise ValidationError(0, "Syntax Error: %s" % e) + raise ValidationError(0, f"Syntax Error: {e}") diff --git a/setup.py b/setup.py index b2fde169..a2618a61 100644 --- a/setup.py +++ b/setup.py @@ -47,12 +47,11 @@ "console_scripts": [ "ptpython = ptpython.entry_points.run_ptpython:run", "ptipython = ptpython.entry_points.run_ptipython:run", - "ptpython%s = ptpython.entry_points.run_ptpython:run" % sys.version_info[0], + f"ptpython{sys.version_info[0]} = ptpython.entry_points.run_ptpython:run", "ptpython{}.{} = ptpython.entry_points.run_ptpython:run".format( *sys.version_info[:2] ), - "ptipython%s = ptpython.entry_points.run_ptipython:run" - % sys.version_info[0], + f"ptipython{sys.version_info[0]} = ptpython.entry_points.run_ptipython:run", "ptipython{}.{} = ptpython.entry_points.run_ptipython:run".format( *sys.version_info[:2] ), From c1a431047e88ae4b2e2b0613bf66c68095f61a4c Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Thu, 16 May 2024 12:21:32 +0000 Subject: [PATCH 199/220] Several typing fixes. --- ptpython/python_input.py | 14 ++++++-------- ptpython/repl.py | 17 +++++++++-------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/ptpython/python_input.py b/ptpython/python_input.py index d66b5ae8..975d3d98 100644 --- a/ptpython/python_input.py +++ b/ptpython/python_input.py @@ -347,14 +347,6 @@ def __init__( "classic": ClassicPrompt(), } - self.get_input_prompt = lambda: self.all_prompt_styles[ - self.prompt_style - ].in_prompt() - - self.get_output_prompt = lambda: self.all_prompt_styles[ - self.prompt_style - ].out_prompt() - #: Load styles. self.code_styles: dict[str, BaseStyle] = get_all_code_styles() self.ui_styles = get_all_ui_styles() @@ -425,6 +417,12 @@ def __init__( else: self._app = None + def get_input_prompt(self) -> AnyFormattedText: + return self.all_prompt_styles[self.prompt_style].in_prompt() + + def get_output_prompt(self) -> AnyFormattedText: + return self.all_prompt_styles[self.prompt_style].out_prompt() + def _accept_handler(self, buff: Buffer) -> bool: app = get_app() app.exit(result=buff.text) diff --git a/ptpython/repl.py b/ptpython/repl.py index bbbd852e..ea2d84f0 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -19,7 +19,8 @@ import types import warnings from dis import COMPILER_FLAG_NAMES -from typing import Any, Callable, ContextManager, Iterable +from pathlib import Path +from typing import Any, Callable, ContextManager, Iterable, Sequence from prompt_toolkit.formatted_text import OneStyleAndTextTuple from prompt_toolkit.patch_stdout import patch_stdout as patch_stdout_context @@ -64,7 +65,7 @@ def _has_coroutine_flag(code: types.CodeType) -> bool: class PythonRepl(PythonInput): def __init__(self, *a, **kw) -> None: - self._startup_paths = kw.pop("startup_paths", None) + self._startup_paths: Sequence[str | Path] | None = kw.pop("startup_paths", None) super().__init__(*a, **kw) self._load_start_paths() @@ -348,7 +349,7 @@ def _store_eval_result(self, result: object) -> None: def get_compiler_flags(self) -> int: return super().get_compiler_flags() | PyCF_ALLOW_TOP_LEVEL_AWAIT - def _compile_with_flags(self, code: str, mode: str): + def _compile_with_flags(self, code: str, mode: str) -> Any: "Compile code with the right compiler flags." return compile( code, @@ -459,13 +460,13 @@ def enter_to_continue() -> None: def embed( - globals=None, - locals=None, + globals: dict[str, Any] | None = None, + locals: dict[str, Any] | None = None, configure: Callable[[PythonRepl], None] | None = None, vi_mode: bool = False, history_filename: str | None = None, title: str | None = None, - startup_paths=None, + startup_paths: Sequence[str | Path] | None = None, patch_stdout: bool = False, return_asyncio_coroutine: bool = False, ) -> None: @@ -494,10 +495,10 @@ def embed( locals = locals or globals - def get_globals(): + def get_globals() -> dict[str, Any]: return globals - def get_locals(): + def get_locals() -> dict[str, Any]: return locals # Create REPL. From 95afc939fe348558486139909b6273f1f7fa245c Mon Sep 17 00:00:00 2001 From: Elliot Ford Date: Fri, 10 May 2024 16:41:19 +0100 Subject: [PATCH 200/220] Update supported versions on README.rst --- README.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 130e4581..06c1e02b 100644 --- a/README.rst +++ b/README.rst @@ -12,7 +12,7 @@ ptpython .. image :: https://github.com/jonathanslenders/ptpython/raw/master/docs/images/example1.png Ptpython is an advanced Python REPL. It should work on all -Python versions from 2.6 up to 3.9 and work cross platform (Linux, +Python versions from 2.6 up to 3.11 and work cross platform (Linux, BSD, OS X and Windows). Note: this version of ptpython requires at least Python 3.6. Install ptpython From 8f68b6ceccbe57d15cb864fae45a5e7b82524bdc Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Mon, 27 May 2024 20:52:04 +0000 Subject: [PATCH 201/220] Ruff compatibility: fix import order. --- ptpython/eventloop.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ptpython/eventloop.py b/ptpython/eventloop.py index 670d09bc..a6462748 100644 --- a/ptpython/eventloop.py +++ b/ptpython/eventloop.py @@ -24,9 +24,8 @@ def _inputhook_tk(inputhook_context: InputHookContext) -> None: Run the Tk eventloop until prompt-toolkit needs to process the next input. """ # Get the current TK application. - import tkinter - import _tkinter # Keep this imports inline! + import tkinter root = tkinter._default_root # type: ignore From fb9bed1e5956ac5f109fd4cb401b3fae997efcd7 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Mon, 27 May 2024 20:46:01 +0000 Subject: [PATCH 202/220] Release 3.0.27 --- CHANGELOG | 7 +++++++ setup.py | 7 +++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index d8738625..6f2bbb9a 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,13 @@ CHANGELOG ========= +3.0.27: 2024-05-27 +------------------ + +- Limit number of completions to 5k (for performance). +- Several typing fixes. + + 3.0.26: 2024-02-06 ------------------ diff --git a/setup.py b/setup.py index a2618a61..84f18be2 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ setup( name="ptpython", author="Jonathan Slenders", - version="3.0.26", + version="3.0.27", url="https://github.com/prompt-toolkit/ptpython", description="Python REPL build on top of prompt_toolkit", long_description=long_description, @@ -38,8 +38,11 @@ "License :: OSI Approved :: BSD License", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.7", - "Programming Language :: Python :: 3.7", "Programming Language :: Python :: 3.8", + "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", "Programming Language :: Python :: 3 :: Only", "Programming Language :: Python", ], From f66a289544a21089f561e21f7632305ff4eed204 Mon Sep 17 00:00:00 2001 From: Valentin Valls Date: Fri, 28 Jun 2024 16:36:46 +0200 Subject: [PATCH 203/220] Clean up signatures on ctrl-c --- ptpython/python_input.py | 1 + 1 file changed, 1 insertion(+) diff --git a/ptpython/python_input.py b/ptpython/python_input.py index 975d3d98..b1773643 100644 --- a/ptpython/python_input.py +++ b/ptpython/python_input.py @@ -1116,4 +1116,5 @@ def pre_run( return result except KeyboardInterrupt: # Abort - try again. + self.signatures = [] self.default_buffer.document = Document() From 4b456890f9b06fc9ea75eef681bb9773c2172c89 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Mon, 22 Jul 2024 09:21:01 +0000 Subject: [PATCH 204/220] Add custom 'exit' function to return from REPL. - Don't terminate `sys.stdin` when `exit` is called (important for `embed()`). - Don't require 'exit' to be called with parentheses. --- ptpython/repl.py | 48 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/ptpython/repl.py b/ptpython/repl.py index ea2d84f0..6b60018e 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -20,7 +20,7 @@ import warnings from dis import COMPILER_FLAG_NAMES from pathlib import Path -from typing import Any, Callable, ContextManager, Iterable, Sequence +from typing import Any, Callable, ContextManager, Iterable, NoReturn, Sequence from prompt_toolkit.formatted_text import OneStyleAndTextTuple from prompt_toolkit.patch_stdout import patch_stdout as patch_stdout_context @@ -40,7 +40,15 @@ except ImportError: PyCF_ALLOW_TOP_LEVEL_AWAIT = 0 -__all__ = ["PythonRepl", "enable_deprecation_warnings", "run_config", "embed"] + +__all__ = [ + "PythonRepl", + "enable_deprecation_warnings", + "run_config", + "embed", + "exit", + "ReplExit", +] def _get_coroutine_flag() -> int | None: @@ -91,9 +99,16 @@ def run_and_show_expression(self, expression: str) -> None: raise except SystemExit: raise + except ReplExit: + raise except BaseException as e: self._handle_exception(e) else: + if isinstance(result, exit): + # When `exit` is evaluated without parentheses. + # Automatically trigger the `ReplExit` exception. + raise ReplExit + # Print. if result is not None: self._show_result(result) @@ -155,7 +170,10 @@ def run(self) -> None: continue # Run it; display the result (or errors if applicable). - self.run_and_show_expression(text) + try: + self.run_and_show_expression(text) + except ReplExit: + return finally: if self.terminal_title: clear_title() @@ -383,6 +401,7 @@ def get_ptpython() -> PythonInput: return self globals["get_ptpython"] = get_ptpython + globals["exit"] = exit() def _remove_from_namespace(self) -> None: """ @@ -459,6 +478,29 @@ def enter_to_continue() -> None: enter_to_continue() +class exit: + """ + Exit the ptpython REPL. + """ + + # This custom exit function ensures that the `embed` function returns from + # where we are embedded, and Python doesn't close `sys.stdin` like + # the default `exit` from `_sitebuiltins.Quitter` does. + + def __call__(self) -> NoReturn: + raise ReplExit + + def __repr__(self) -> str: + # (Same message as the built-in Python REPL.) + return "Use exit() or Ctrl-D (i.e. EOF) to exit" + + +class ReplExit(Exception): + """ + Exception raised by ptpython's exit function. + """ + + def embed( globals: dict[str, Any] | None = None, locals: dict[str, Any] | None = None, From cd54c27a6205226bdb00c5c44f045c32d9547acd Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Mon, 22 Jul 2024 09:28:07 +0000 Subject: [PATCH 205/220] Fix GitHub actions workflow. Use 'ruff check' instead of 'ruff'. --- .github/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 9a50f3bc..c62bdc39 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -27,7 +27,7 @@ jobs: - name: Type Checker run: | mypy ptpython - ruff . + ruff check . ruff format --check . - name: Run Tests run: | From 79cb14b1982fee48b86e1b0fdee8f70d8f849d56 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Mon, 22 Jul 2024 09:36:04 +0000 Subject: [PATCH 206/220] Release 3.0.28 --- CHANGELOG | 13 +++++++++++++ setup.py | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 6f2bbb9a..999f13d6 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,19 @@ CHANGELOG ========= +3.0.28: 2024-07-22 +------------------ + +New features: +- Custom 'exit' function to return from REPL that + * doesn't terminate `sys.stdin` when `exit` is called (important for + `embed()`). + * doesn't require to be called with parentheses. + +Fixes: +- Clean up signatures on control-c. + + 3.0.27: 2024-05-27 ------------------ diff --git a/setup.py b/setup.py index 84f18be2..8e84906e 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ setup( name="ptpython", author="Jonathan Slenders", - version="3.0.27", + version="3.0.28", url="https://github.com/prompt-toolkit/ptpython", description="Python REPL build on top of prompt_toolkit", long_description=long_description, From 3e7f68ee48995de1d89e1d4c6ba255bdd1bc7ff2 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Mon, 22 Jul 2024 12:26:12 +0000 Subject: [PATCH 207/220] Improve dictionary completion performance. This improves the performance for dictionary-like objects where iterating over the keys is fast, but doing a lookup for the values is slow. This change ensures we only do value lookups when really needed. The change also caches the meta text so that we don't have to recompute it during navigation of the completion menu. --- ptpython/completer.py | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/ptpython/completer.py b/ptpython/completer.py index 264918e8..e8bab285 100644 --- a/ptpython/completer.py +++ b/ptpython/completer.py @@ -476,20 +476,34 @@ def _get_item_lookup_completions( Complete dictionary keys. """ - def meta_repr(value: object) -> Callable[[], str]: + def meta_repr(obj: object, key: object) -> Callable[[], str]: "Abbreviate meta text, make sure it fits on one line." + cached_result: str | None = None # We return a function, so that it gets computed when it's needed. # When there are many completions, that improves the performance # quite a bit (for the multi-column completion menu, we only need # to display one meta text). + # Note that we also do the lookup itself in here (`obj[key]`), + # because this part can also be slow for some mapping + # implementations. def get_value_repr() -> str: - text = self._do_repr(value) + nonlocal cached_result + if cached_result is not None: + return cached_result + + try: + value = obj[key] # type: ignore + + text = self._do_repr(value) + except BaseException: + return "-" # Take first line, if multiple lines. if "\n" in text: text = text.split("\n", 1)[0] + "..." + cached_result = text return text return get_value_repr @@ -504,24 +518,24 @@ def get_value_repr() -> str: # If this object is a dictionary, complete the keys. if isinstance(result, (dict, collections_abc.Mapping)): # Try to evaluate the key. - key_obj = key + key_obj_str = str(key) for k in [key, key + '"', key + "'"]: try: - key_obj = ast.literal_eval(k) + key_obj_str = str(ast.literal_eval(k)) except (SyntaxError, ValueError): continue else: break - for k, v in result.items(): - if str(k).startswith(str(key_obj)): + for k in result: + if str(k).startswith(key_obj_str): try: k_repr = self._do_repr(k) yield Completion( k_repr + "]", -len(key), display=f"[{k_repr}]", - display_meta=meta_repr(v), + display_meta=meta_repr(result, k), ) except ReprFailedError: pass @@ -537,7 +551,7 @@ def get_value_repr() -> str: k_repr + "]", -len(key), display=f"[{k_repr}]", - display_meta=meta_repr(result[k]), + display_meta=meta_repr(result, k), ) except KeyError: # `result[k]` lookup failed. Trying to complete From 5021832f76309755097b744f274c4e687a690b85 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Mon, 22 Jul 2024 12:42:43 +0000 Subject: [PATCH 208/220] Release 3.0.29 --- CHANGELOG | 7 +++++++ setup.py | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 999f13d6..bef7d07f 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,13 @@ CHANGELOG ========= +3.0.29: 2024-07-22 +------------------ + +Fixes: +- Further improve performance of dictionary completions. + + 3.0.28: 2024-07-22 ------------------ diff --git a/setup.py b/setup.py index 8e84906e..aa101764 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ setup( name="ptpython", author="Jonathan Slenders", - version="3.0.28", + version="3.0.29", url="https://github.com/prompt-toolkit/ptpython", description="Python REPL build on top of prompt_toolkit", long_description=long_description, From b5d8c28535578eca504572c11a6ff893728ecac0 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Thu, 10 Apr 2025 12:53:50 +0000 Subject: [PATCH 209/220] Show exception cause/context when printing an exception. --- ptpython/printer.py | 27 ++++++++++++++++----------- ptpython/repl.py | 4 ++++ 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/ptpython/printer.py b/ptpython/printer.py index 85bd9c88..81ea16f3 100644 --- a/ptpython/printer.py +++ b/ptpython/printer.py @@ -254,8 +254,7 @@ def _apply_soft_wrapping( columns_in_buffer += width current_line.append((style, c)) - if len(current_line) > 0: - yield current_line + yield current_line def _print_paginated_formatted_text( self, lines: Iterable[StyleAndTextTuples] @@ -323,14 +322,20 @@ def show_pager() -> None: def _format_exception_output( self, e: BaseException, highlight: bool ) -> Generator[OneStyleAndTextTuple, None, None]: - # Instead of just calling ``traceback.format_exc``, we take the - # traceback and skip the bottom calls of this framework. - t, v, tb = sys.exc_info() - - # Required for pdb.post_mortem() to work. - sys.last_type, sys.last_value, sys.last_traceback = t, v, tb - - tblist = list(traceback.extract_tb(tb)) + if e.__cause__: + yield from self._format_exception_output(e.__cause__, highlight=highlight) + yield ( + "", + "\nThe above exception was the direct cause of the following exception:\n\n", + ) + elif e.__context__: + yield from self._format_exception_output(e.__context__, highlight=highlight) + yield ( + "", + "\nDuring handling of the above exception, another exception occurred:\n\n", + ) + + tblist = list(traceback.extract_tb(e.__traceback__)) for line_nr, tb_tuple in enumerate(tblist): if tb_tuple[0] == "": @@ -340,7 +345,7 @@ def _format_exception_output( tb_list = traceback.format_list(tblist) if tb_list: tb_list.insert(0, "Traceback (most recent call last):\n") - tb_list.extend(traceback.format_exception_only(t, v)) + tb_list.extend(traceback.format_exception_only(type(e), e)) tb_str = "".join(tb_list) diff --git a/ptpython/repl.py b/ptpython/repl.py index 6b60018e..9142d909 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -378,6 +378,10 @@ def _compile_with_flags(self, code: str, mode: str) -> Any: ) def _handle_exception(self, e: BaseException) -> None: + # Required for pdb.post_mortem() to work. + t, v, tb = sys.exc_info() + sys.last_type, sys.last_value, sys.last_traceback = t, v, tb + self._get_output_printer().display_exception( e, highlight=self.enable_syntax_highlighting, From 37763164fd444771c9232ed10e1021d34b7a5d20 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Thu, 10 Apr 2025 12:58:18 +0000 Subject: [PATCH 210/220] Drop Python 3.8, given it's end of life and no longer supported on GitHub CI. Also some typing fixes. --- .github/workflows/test.yaml | 6 +++--- ptpython/entry_points/run_ptpython.py | 13 ++++++------- setup.py | 5 ++--- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index c62bdc39..2311e02a 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -10,12 +10,12 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: [3.7, 3.8, 3.9, "3.10", "3.11"] + python-version: [3.8, 3.9, "3.10", "3.11", "3.12", "3.13"] steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - name: Setup Python ${{ matrix.python-version }} - uses: actions/setup-python@v1 + uses: actions/setup-python@v4 with: python-version: ${{ matrix.python-version }} - name: Install Dependencies diff --git a/ptpython/entry_points/run_ptpython.py b/ptpython/entry_points/run_ptpython.py index 05df9714..d083858d 100644 --- a/ptpython/entry_points/run_ptpython.py +++ b/ptpython/entry_points/run_ptpython.py @@ -30,8 +30,9 @@ import os import pathlib import sys +from importlib import metadata from textwrap import dedent -from typing import IO +from typing import Protocol import appdirs from prompt_toolkit.formatted_text import HTML @@ -39,17 +40,15 @@ from ptpython.repl import PythonRepl, embed, enable_deprecation_warnings, run_config -try: - from importlib import metadata # type: ignore -except ImportError: - import importlib_metadata as metadata # type: ignore +__all__ = ["create_parser", "get_config_and_history_file", "run"] -__all__ = ["create_parser", "get_config_and_history_file", "run"] +class _SupportsWrite(Protocol): + def write(self, s: str, /) -> object: ... class _Parser(argparse.ArgumentParser): - def print_help(self, file: IO[str] | None = None) -> None: + def print_help(self, file: _SupportsWrite | None = None) -> None: super().print_help() print( dedent( diff --git a/setup.py b/setup.py index aa101764..bd2f962a 100644 --- a/setup.py +++ b/setup.py @@ -27,22 +27,21 @@ package_data={"ptpython": ["py.typed"]}, install_requires=[ "appdirs", - "importlib_metadata;python_version<'3.8'", "jedi>=0.16.0", # Use prompt_toolkit 3.0.43, because of `OneStyleAndTextTuple` import. "prompt_toolkit>=3.0.43,<3.1.0", "pygments", ], - python_requires=">=3.7", + python_requires=">=3.8", classifiers=[ "License :: OSI Approved :: BSD License", "Programming Language :: Python :: 3", - "Programming Language :: Python :: 3.7", "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.13", "Programming Language :: Python :: 3 :: Only", "Programming Language :: Python", ], From 04235d791b483af0ad36f578608d06bf4331f825 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Thu, 10 Apr 2025 13:36:23 +0000 Subject: [PATCH 211/220] Use f-strings instead of %-style formatting. --- examples/asyncio-python-embed.py | 2 +- examples/asyncio-ssh-python-embed.py | 4 ++-- ptpython/layout.py | 12 +++++------- ptpython/printer.py | 1 - ptpython/repl.py | 2 +- 5 files changed, 9 insertions(+), 12 deletions(-) diff --git a/examples/asyncio-python-embed.py b/examples/asyncio-python-embed.py index 38cc1c20..cb909731 100755 --- a/examples/asyncio-python-embed.py +++ b/examples/asyncio-python-embed.py @@ -25,7 +25,7 @@ async def print_counter() -> None: Coroutine that prints counters and saves it in a global variable. """ while True: - print("Counter: %i" % counter[0]) + print(f"Counter: {counter[0]}") counter[0] += 1 await asyncio.sleep(3) diff --git a/examples/asyncio-ssh-python-embed.py b/examples/asyncio-ssh-python-embed.py index 9bbad86f..bf79df78 100755 --- a/examples/asyncio-ssh-python-embed.py +++ b/examples/asyncio-ssh-python-embed.py @@ -44,8 +44,8 @@ async def main(port: int = 8222) -> None: def create_server() -> MySSHServer: return MySSHServer(lambda: environ) - print("Listening on :%i" % port) - print('To connect, do "ssh localhost -p %i"' % port) + print(f"Listening on: {port}") + print(f'To connect, do "ssh localhost -p {port}"') await asyncssh.create_server( create_server, "", port, server_host_keys=["/etc/ssh/ssh_host_dsa_key"] diff --git a/ptpython/layout.py b/ptpython/layout.py index 622df594..9768598e 100644 --- a/ptpython/layout.py +++ b/ptpython/layout.py @@ -108,7 +108,7 @@ def append_category(category: OptionCategory[Any]) -> None: tokens.extend( [ ("class:sidebar", " "), - ("class:sidebar.title", " %-36s" % category.title), + ("class:sidebar.title", f" {category.title:36}"), ("class:sidebar", "\n"), ] ) @@ -130,7 +130,7 @@ def goto_next(mouse_event: MouseEvent) -> None: sel = ",selected" if selected else "" tokens.append(("class:sidebar" + sel, " >" if selected else " ")) - tokens.append(("class:sidebar.label" + sel, "%-24s" % label, select_item)) + tokens.append(("class:sidebar.label" + sel, f"{label:24}", select_item)) tokens.append(("class:sidebar.status" + sel, " ", select_item)) tokens.append(("class:sidebar.status" + sel, f"{status}", goto_next)) @@ -332,7 +332,7 @@ def get_continuation( width: int, line_number: int, is_soft_wrap: bool ) -> StyleAndTextTuples: if python_input.show_line_numbers and not is_soft_wrap: - text = ("%i " % (line_number + 1)).rjust(width) + text = f"{line_number + 1} ".rjust(width) return [("class:line-number", text)] else: return to_formatted_text(get_prompt_style().in2_prompt(width)) @@ -368,8 +368,7 @@ def get_text_fragments() -> StyleAndTextTuples: append( ( TB, - "%i/%i " - % (python_buffer.working_index + 1, len(python_buffer._working_lines)), + f"{python_buffer.working_index + 1}/{len(python_buffer._working_lines)} ", ) ) @@ -492,8 +491,7 @@ def toggle_sidebar(mouse_event: MouseEvent) -> None: ("class:status-toolbar", " - "), ( "class:status-toolbar.python-version", - "%s %i.%i.%i" - % (platform.python_implementation(), version[0], version[1], version[2]), + f"{platform.python_implementation()} {version[0]}.{version[1]}.{version[2]}", ), ("class:status-toolbar", " "), ] diff --git a/ptpython/printer.py b/ptpython/printer.py index 81ea16f3..a3578de7 100644 --- a/ptpython/printer.py +++ b/ptpython/printer.py @@ -1,6 +1,5 @@ from __future__ import annotations -import sys import traceback from dataclasses import dataclass from enum import Enum diff --git a/ptpython/repl.py b/ptpython/repl.py index 9142d909..ba6717fb 100644 --- a/ptpython/repl.py +++ b/ptpython/repl.py @@ -362,7 +362,7 @@ async def eval_async(self, line: str) -> object: def _store_eval_result(self, result: object) -> None: locals: dict[str, Any] = self.get_locals() - locals["_"] = locals["_%i" % self.current_statement_index] = result + locals["_"] = locals[f"_{self.current_statement_index}"] = result def get_compiler_flags(self) -> int: return super().get_compiler_flags() | PyCF_ALLOW_TOP_LEVEL_AWAIT From ce3a9e2f5495a7ae5146942e468e3565cbe3a87c Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Thu, 10 Apr 2025 13:51:45 +0000 Subject: [PATCH 212/220] Use uv in github actions. --- .github/workflows/test.yaml | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 2311e02a..c9fb0ae8 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -10,20 +10,18 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: [3.8, 3.9, "3.10", "3.11", "3.12", "3.13"] + python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13"] steps: - uses: actions/checkout@v4 - - name: Setup Python ${{ matrix.python-version }} - uses: actions/setup-python@v4 + + - uses: astral-sh/setup-uv@v5 with: python-version: ${{ matrix.python-version }} - name: Install Dependencies run: | - sudo apt remove python3-pip - python -m pip install --upgrade pip - python -m pip install . ruff mypy pytest readme_renderer - pip list + uv pip install . ruff mypy pytest readme_renderer + uv pip list - name: Type Checker run: | mypy ptpython From 1f1eb1796a67699bbc2bba21129aaf1e6dab978b Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Thu, 10 Apr 2025 14:00:42 +0000 Subject: [PATCH 213/220] Reworked dummy test directory. --- .github/workflows/test.yaml | 2 +- tests/run_tests.py | 24 ------------------------ tests/test_dummy.py | 31 +++++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 25 deletions(-) delete mode 100755 tests/run_tests.py create mode 100755 tests/test_dummy.py diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index c9fb0ae8..3f527abe 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -29,7 +29,7 @@ jobs: ruff format --check . - name: Run Tests run: | - ./tests/run_tests.py + pytest tests/ - name: Validate README.md # Ensure that the README renders correctly (required for uploading to PyPI). run: | diff --git a/tests/run_tests.py b/tests/run_tests.py deleted file mode 100755 index 0de37430..00000000 --- a/tests/run_tests.py +++ /dev/null @@ -1,24 +0,0 @@ -#!/usr/bin/env python -from __future__ import annotations - -import unittest - -import ptpython.completer -import ptpython.eventloop -import ptpython.filters -import ptpython.history_browser -import ptpython.key_bindings -import ptpython.layout -import ptpython.python_input -import ptpython.repl -import ptpython.style -import ptpython.utils -import ptpython.validator - -# For now there are no tests here. -# However this is sufficient for Travis to do at least a syntax check. -# That way we are at least sure to restrict to the Python 2.6 syntax. - - -if __name__ == "__main__": - unittest.main() diff --git a/tests/test_dummy.py b/tests/test_dummy.py new file mode 100755 index 00000000..922c6a39 --- /dev/null +++ b/tests/test_dummy.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python +from __future__ import annotations + +import ptpython.completer +import ptpython.eventloop +import ptpython.filters +import ptpython.history_browser +import ptpython.key_bindings +import ptpython.layout +import ptpython.python_input +import ptpython.repl +import ptpython.style +import ptpython.utils +import ptpython.validator + +# For now there are no tests here. +# However this is sufficient to do at least a syntax check. + + +def test_dummy() -> None: + assert ptpython.completer + assert ptpython.eventloop + assert ptpython.filters + assert ptpython.history_browser + assert ptpython.key_bindings + assert ptpython.layout + assert ptpython.python_input + assert ptpython.repl + assert ptpython.style + assert ptpython.utils + assert ptpython.validator From f1dea7efe97426eec9e7218a0fdc0e17bc47aca8 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Thu, 10 Apr 2025 20:35:59 +0000 Subject: [PATCH 214/220] Use pyproject.toml instead of setup.py Cherry-picked from: https://github.com/prompt-toolkit/ptpython/pull/599 Thanks to: Branch Vincent --- pyproject.toml | 58 +++++++++++++++++++++++++++++++++++++++++++- setup.cfg | 41 ------------------------------- setup.py | 66 -------------------------------------------------- 3 files changed, 57 insertions(+), 108 deletions(-) delete mode 100644 setup.cfg delete mode 100644 setup.py diff --git a/pyproject.toml b/pyproject.toml index ce420372..3780f9d6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,3 +1,55 @@ +[project] +name = "ptpython" +version = "3.0.29" +description = "Python REPL build on top of prompt_toolkit" +readme = "README.rst" +authors = [{ name = "Jonathan Slenders" }] +classifiers = [ + "License :: OSI Approved :: BSD License", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.8", + "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.13", + "Programming Language :: Python :: 3 :: Only", + "Programming Language :: Python", +] +requires-python = ">=3.8" +dependencies = [ + "appdirs", + "jedi>=0.16.0", + # Use prompt_toolkit 3.0.43, because of `OneStyleAndTextTuple` import. + "prompt_toolkit>=3.0.43,<3.1.0", + "pygments", +] + + +[project.urls] +Homepage = "https://github.com/prompt-toolkit/ptpython" +Changelog = "https://github.com/prompt-toolkit/ptpython/blob/master/CHANGELOG" +"Bug Tracker" = "https://github.com/prompt-toolkit/ptpython/issues" +"Source Code" = "https://github.com/prompt-toolkit/ptpython" + + +[project.scripts] +ptpython = "ptpython.entry_points.run_ptpython:run" +ptipython = "ptpython.entry_points.run_ptipython:run" + + +[project.optional-dependencies] +ptipython = ["ipython"] # For ptipython, we need to have IPython + + +[tool.mypy] +ignore_missing_imports = true +no_implicit_optional = true +platform = "win32" +strict_equality = true +strict_optional = true + + [tool.ruff] target-version = "py37" lint.select = [ @@ -27,9 +79,13 @@ lint.ignore = [ "ptpython/ipython.py" = ["T100"] # Import usage. "ptpython/repl.py" = ["T201"] # Print usage. "ptpython/printer.py" = ["T201"] # Print usage. -"tests/run_tests.py" = ["F401"] # Unused imports. [tool.ruff.lint.isort] known-first-party = ["ptpython"] known-third-party = ["prompt_toolkit", "pygments", "asyncssh"] + + +[build-system] +requires = ["setuptools>=68"] +build-backend = "setuptools.build_meta" diff --git a/setup.cfg b/setup.cfg deleted file mode 100644 index 80dfec6a..00000000 --- a/setup.cfg +++ /dev/null @@ -1,41 +0,0 @@ -[bdist_wheel] -universal=1 - -[flake8] -exclude=__init__.py -max_line_length=150 -ignore= - E114, - E116, - E117, - E121, - E122, - E123, - E125, - E126, - E127, - E128, - E131, - E171, - E203, - E211, - E221, - E227, - E231, - E241, - E251, - E301, - E402, - E501, - E701, - E702, - E704, - E731, - E741, - F401, - F403, - F405, - F811, - W503, - W504, - E722 diff --git a/setup.py b/setup.py deleted file mode 100644 index bd2f962a..00000000 --- a/setup.py +++ /dev/null @@ -1,66 +0,0 @@ -#!/usr/bin/env python -import os -import sys - -from setuptools import find_packages, setup - -with open(os.path.join(os.path.dirname(__file__), "README.rst")) as f: - long_description = f.read() - - -setup( - name="ptpython", - author="Jonathan Slenders", - version="3.0.29", - url="https://github.com/prompt-toolkit/ptpython", - description="Python REPL build on top of prompt_toolkit", - long_description=long_description, - package_urls={ - "Changelog": "https://github.com/prompt-toolkit/ptpython/blob/master/CHANGELOG", - }, - project_urls={ - "Bug Tracker": "https://github.com/prompt-toolkit/ptpython/issues", - "Source Code": "https://github.com/prompt-toolkit/ptpython", - "Changelog": "https://github.com/prompt-toolkit/ptpython/blob/master/CHANGELOG", - }, - packages=find_packages("."), - package_data={"ptpython": ["py.typed"]}, - install_requires=[ - "appdirs", - "jedi>=0.16.0", - # Use prompt_toolkit 3.0.43, because of `OneStyleAndTextTuple` import. - "prompt_toolkit>=3.0.43,<3.1.0", - "pygments", - ], - python_requires=">=3.8", - classifiers=[ - "License :: OSI Approved :: BSD License", - "Programming Language :: Python :: 3", - "Programming Language :: Python :: 3.8", - "Programming Language :: Python :: 3.9", - "Programming Language :: Python :: 3.10", - "Programming Language :: Python :: 3.11", - "Programming Language :: Python :: 3.12", - "Programming Language :: Python :: 3.13", - "Programming Language :: Python :: 3 :: Only", - "Programming Language :: Python", - ], - entry_points={ - "console_scripts": [ - "ptpython = ptpython.entry_points.run_ptpython:run", - "ptipython = ptpython.entry_points.run_ptipython:run", - f"ptpython{sys.version_info[0]} = ptpython.entry_points.run_ptpython:run", - "ptpython{}.{} = ptpython.entry_points.run_ptpython:run".format( - *sys.version_info[:2] - ), - f"ptipython{sys.version_info[0]} = ptpython.entry_points.run_ptipython:run", - "ptipython{}.{} = ptpython.entry_points.run_ptipython:run".format( - *sys.version_info[:2] - ), - ] - }, - extras_require={ - "ptipython": ["ipython"], # For ptipython, we need to have IPython - "all": ["black"], # Black not always possible on PyPy - }, -) From acf61459a7b203815a738cf6dc5ec20288e3ce19 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Thu, 10 Apr 2025 20:48:11 +0000 Subject: [PATCH 215/220] Use uvx in GitHub workflows. --- .github/workflows/test.yaml | 18 ++++++++---------- ptpython/history_browser.py | 4 +++- ptpython/key_bindings.py | 4 +++- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 3f527abe..74c3c7b8 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -14,23 +14,21 @@ jobs: steps: - uses: actions/checkout@v4 - - uses: astral-sh/setup-uv@v5 with: python-version: ${{ matrix.python-version }} - - name: Install Dependencies + - name: Type Checking run: | - uv pip install . ruff mypy pytest readme_renderer - uv pip list - - name: Type Checker + uvx --with . mypy ptpython + - name: Code formatting run: | - mypy ptpython - ruff check . - ruff format --check . - - name: Run Tests + uvx ruff check . + uvx ruff format --check . + - name: Unit test run: | - pytest tests/ + uvx --with . pytest tests/ - name: Validate README.md # Ensure that the README renders correctly (required for uploading to PyPI). run: | + uv pip install readme_renderer python -m readme_renderer README.rst > /dev/null diff --git a/ptpython/history_browser.py b/ptpython/history_browser.py index ae0ac03e..72bc576d 100644 --- a/ptpython/history_browser.py +++ b/ptpython/history_browser.py @@ -58,13 +58,15 @@ from .utils import if_mousedown if TYPE_CHECKING: + from typing_extensions import TypeAlias + from .python_input import PythonInput HISTORY_COUNT = 2000 __all__ = ["HistoryLayout", "PythonHistory"] -E = KeyPressEvent +E: TypeAlias = KeyPressEvent HELP_TEXT = """ This interface is meant to select multiple lines from the diff --git a/ptpython/key_bindings.py b/ptpython/key_bindings.py index d7bb575e..48c5f5ae 100644 --- a/ptpython/key_bindings.py +++ b/ptpython/key_bindings.py @@ -22,6 +22,8 @@ from .utils import document_is_multiline_python if TYPE_CHECKING: + from typing_extensions import TypeAlias + from .python_input import PythonInput __all__ = [ @@ -30,7 +32,7 @@ "load_confirm_exit_bindings", ] -E = KeyPressEvent +E: TypeAlias = KeyPressEvent @Condition From 39b1cbda27e7b579e7b470311d409924457e072b Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Thu, 10 Apr 2025 20:59:43 +0000 Subject: [PATCH 216/220] Remove mypy.ini --- mypy.ini | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 mypy.ini diff --git a/mypy.ini b/mypy.ini deleted file mode 100644 index 5a7ef2eb..00000000 --- a/mypy.ini +++ /dev/null @@ -1,6 +0,0 @@ -[mypy] -ignore_missing_imports = True -no_implicit_optional = True -platform = win32 -strict_equality = True -strict_optional = True From 1527d0527625a2c72b154a6cb937f0e4dec9a87a Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Thu, 10 Apr 2025 21:07:52 +0000 Subject: [PATCH 217/220] use src/ directory for source code. --- .github/workflows/test.yaml | 2 +- pyproject.toml | 10 +++++----- {ptpython => src/ptpython}/__init__.py | 0 {ptpython => src/ptpython}/__main__.py | 0 {ptpython => src/ptpython}/completer.py | 0 {ptpython => src/ptpython}/contrib/__init__.py | 0 {ptpython => src/ptpython}/contrib/asyncssh_repl.py | 0 {ptpython => src/ptpython}/entry_points/__init__.py | 0 .../ptpython}/entry_points/run_ptipython.py | 0 .../ptpython}/entry_points/run_ptpython.py | 0 {ptpython => src/ptpython}/eventloop.py | 0 {ptpython => src/ptpython}/filters.py | 0 {ptpython => src/ptpython}/history_browser.py | 0 {ptpython => src/ptpython}/ipython.py | 0 {ptpython => src/ptpython}/key_bindings.py | 0 {ptpython => src/ptpython}/layout.py | 0 {ptpython => src/ptpython}/lexer.py | 0 {ptpython => src/ptpython}/printer.py | 0 {ptpython => src/ptpython}/prompt_style.py | 0 {ptpython => src/ptpython}/py.typed | 0 {ptpython => src/ptpython}/python_input.py | 0 {ptpython => src/ptpython}/repl.py | 0 {ptpython => src/ptpython}/signatures.py | 0 {ptpython => src/ptpython}/style.py | 0 {ptpython => src/ptpython}/utils.py | 0 {ptpython => src/ptpython}/validator.py | 0 26 files changed, 6 insertions(+), 6 deletions(-) rename {ptpython => src/ptpython}/__init__.py (100%) rename {ptpython => src/ptpython}/__main__.py (100%) rename {ptpython => src/ptpython}/completer.py (100%) rename {ptpython => src/ptpython}/contrib/__init__.py (100%) rename {ptpython => src/ptpython}/contrib/asyncssh_repl.py (100%) rename {ptpython => src/ptpython}/entry_points/__init__.py (100%) rename {ptpython => src/ptpython}/entry_points/run_ptipython.py (100%) rename {ptpython => src/ptpython}/entry_points/run_ptpython.py (100%) rename {ptpython => src/ptpython}/eventloop.py (100%) rename {ptpython => src/ptpython}/filters.py (100%) rename {ptpython => src/ptpython}/history_browser.py (100%) rename {ptpython => src/ptpython}/ipython.py (100%) rename {ptpython => src/ptpython}/key_bindings.py (100%) rename {ptpython => src/ptpython}/layout.py (100%) rename {ptpython => src/ptpython}/lexer.py (100%) rename {ptpython => src/ptpython}/printer.py (100%) rename {ptpython => src/ptpython}/prompt_style.py (100%) rename {ptpython => src/ptpython}/py.typed (100%) rename {ptpython => src/ptpython}/python_input.py (100%) rename {ptpython => src/ptpython}/repl.py (100%) rename {ptpython => src/ptpython}/signatures.py (100%) rename {ptpython => src/ptpython}/style.py (100%) rename {ptpython => src/ptpython}/utils.py (100%) rename {ptpython => src/ptpython}/validator.py (100%) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 74c3c7b8..457a4e48 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -19,7 +19,7 @@ jobs: python-version: ${{ matrix.python-version }} - name: Type Checking run: | - uvx --with . mypy ptpython + uvx --with . mypy src/ptpython - name: Code formatting run: | uvx ruff check . diff --git a/pyproject.toml b/pyproject.toml index 3780f9d6..680d7087 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -74,11 +74,11 @@ lint.ignore = [ [tool.ruff.lint.per-file-ignores] "examples/*" = ["T201"] # Print allowed in examples. "examples/ptpython_config/config.py" = ["F401"] # Unused imports in config. -"ptpython/entry_points/run_ptipython.py" = ["T201", "F401"] # Print, import usage. -"ptpython/entry_points/run_ptpython.py" = ["T201"] # Print usage. -"ptpython/ipython.py" = ["T100"] # Import usage. -"ptpython/repl.py" = ["T201"] # Print usage. -"ptpython/printer.py" = ["T201"] # Print usage. +"src/ptpython/entry_points/run_ptipython.py" = ["T201", "F401"] # Print, import usage. +"src/ptpython/entry_points/run_ptpython.py" = ["T201"] # Print usage. +"src/ptpython/ipython.py" = ["T100"] # Import usage. +"src/ptpython/repl.py" = ["T201"] # Print usage. +"src/ptpython/printer.py" = ["T201"] # Print usage. [tool.ruff.lint.isort] diff --git a/ptpython/__init__.py b/src/ptpython/__init__.py similarity index 100% rename from ptpython/__init__.py rename to src/ptpython/__init__.py diff --git a/ptpython/__main__.py b/src/ptpython/__main__.py similarity index 100% rename from ptpython/__main__.py rename to src/ptpython/__main__.py diff --git a/ptpython/completer.py b/src/ptpython/completer.py similarity index 100% rename from ptpython/completer.py rename to src/ptpython/completer.py diff --git a/ptpython/contrib/__init__.py b/src/ptpython/contrib/__init__.py similarity index 100% rename from ptpython/contrib/__init__.py rename to src/ptpython/contrib/__init__.py diff --git a/ptpython/contrib/asyncssh_repl.py b/src/ptpython/contrib/asyncssh_repl.py similarity index 100% rename from ptpython/contrib/asyncssh_repl.py rename to src/ptpython/contrib/asyncssh_repl.py diff --git a/ptpython/entry_points/__init__.py b/src/ptpython/entry_points/__init__.py similarity index 100% rename from ptpython/entry_points/__init__.py rename to src/ptpython/entry_points/__init__.py diff --git a/ptpython/entry_points/run_ptipython.py b/src/ptpython/entry_points/run_ptipython.py similarity index 100% rename from ptpython/entry_points/run_ptipython.py rename to src/ptpython/entry_points/run_ptipython.py diff --git a/ptpython/entry_points/run_ptpython.py b/src/ptpython/entry_points/run_ptpython.py similarity index 100% rename from ptpython/entry_points/run_ptpython.py rename to src/ptpython/entry_points/run_ptpython.py diff --git a/ptpython/eventloop.py b/src/ptpython/eventloop.py similarity index 100% rename from ptpython/eventloop.py rename to src/ptpython/eventloop.py diff --git a/ptpython/filters.py b/src/ptpython/filters.py similarity index 100% rename from ptpython/filters.py rename to src/ptpython/filters.py diff --git a/ptpython/history_browser.py b/src/ptpython/history_browser.py similarity index 100% rename from ptpython/history_browser.py rename to src/ptpython/history_browser.py diff --git a/ptpython/ipython.py b/src/ptpython/ipython.py similarity index 100% rename from ptpython/ipython.py rename to src/ptpython/ipython.py diff --git a/ptpython/key_bindings.py b/src/ptpython/key_bindings.py similarity index 100% rename from ptpython/key_bindings.py rename to src/ptpython/key_bindings.py diff --git a/ptpython/layout.py b/src/ptpython/layout.py similarity index 100% rename from ptpython/layout.py rename to src/ptpython/layout.py diff --git a/ptpython/lexer.py b/src/ptpython/lexer.py similarity index 100% rename from ptpython/lexer.py rename to src/ptpython/lexer.py diff --git a/ptpython/printer.py b/src/ptpython/printer.py similarity index 100% rename from ptpython/printer.py rename to src/ptpython/printer.py diff --git a/ptpython/prompt_style.py b/src/ptpython/prompt_style.py similarity index 100% rename from ptpython/prompt_style.py rename to src/ptpython/prompt_style.py diff --git a/ptpython/py.typed b/src/ptpython/py.typed similarity index 100% rename from ptpython/py.typed rename to src/ptpython/py.typed diff --git a/ptpython/python_input.py b/src/ptpython/python_input.py similarity index 100% rename from ptpython/python_input.py rename to src/ptpython/python_input.py diff --git a/ptpython/repl.py b/src/ptpython/repl.py similarity index 100% rename from ptpython/repl.py rename to src/ptpython/repl.py diff --git a/ptpython/signatures.py b/src/ptpython/signatures.py similarity index 100% rename from ptpython/signatures.py rename to src/ptpython/signatures.py diff --git a/ptpython/style.py b/src/ptpython/style.py similarity index 100% rename from ptpython/style.py rename to src/ptpython/style.py diff --git a/ptpython/utils.py b/src/ptpython/utils.py similarity index 100% rename from ptpython/utils.py rename to src/ptpython/utils.py diff --git a/ptpython/validator.py b/src/ptpython/validator.py similarity index 100% rename from ptpython/validator.py rename to src/ptpython/validator.py From 030790f8fb8da7736cc91a76712c99f230d1ebe1 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Thu, 10 Apr 2025 22:02:03 +0000 Subject: [PATCH 218/220] Add typos to workflow. --- .github/workflows/test.yaml | 6 ++++++ pyproject.toml | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 457a4e48..6d2877b3 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -21,13 +21,19 @@ jobs: run: | uvx --with . mypy src/ptpython - name: Code formatting + if: ${{ matrix.python-version == '3.13' }} run: | uvx ruff check . uvx ruff format --check . + - name: Typos + if: ${{ matrix.python-version == '3.13' }} + run: | + uvx typos . - name: Unit test run: | uvx --with . pytest tests/ - name: Validate README.md + if: ${{ matrix.python-version == '3.13' }} # Ensure that the README renders correctly (required for uploading to PyPI). run: | uv pip install readme_renderer diff --git a/pyproject.toml b/pyproject.toml index 680d7087..72259863 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -85,6 +85,10 @@ lint.ignore = [ known-first-party = ["ptpython"] known-third-party = ["prompt_toolkit", "pygments", "asyncssh"] +[tool.typos.default] +extend-ignore-re = [ + "impotr" # Intentional typo in: ./examples/ptpython_config/config.py +] [build-system] requires = ["setuptools>=68"] From fb4949ad52ce7d603ab5bb52fba572c6dfdaad0b Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Thu, 10 Apr 2025 22:21:44 +0000 Subject: [PATCH 219/220] Typecheck examples. --- .github/workflows/test.yaml | 3 ++- examples/ssh-and-telnet-embed.py | 6 +++-- src/ptpython/repl.py | 43 ++++++++++++++++++++++++++++++-- 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 6d2877b3..d53bfcc1 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -19,7 +19,8 @@ jobs: python-version: ${{ matrix.python-version }} - name: Type Checking run: | - uvx --with . mypy src/ptpython + uvx --with . mypy src/ptpython/ + uvx --with . mypy examples/ - name: Code formatting if: ${{ matrix.python-version == '3.13' }} run: | diff --git a/examples/ssh-and-telnet-embed.py b/examples/ssh-and-telnet-embed.py index 62fa76d9..2b293e6f 100755 --- a/examples/ssh-and-telnet-embed.py +++ b/examples/ssh-and-telnet-embed.py @@ -6,6 +6,8 @@ https://gist.github.com/vxgmichel/7685685b3e5ead04ada4a3ba75a48eef """ +from __future__ import annotations + import asyncio import pathlib @@ -15,7 +17,7 @@ PromptToolkitSSHServer, PromptToolkitSSHSession, ) -from prompt_toolkit.contrib.telnet.server import TelnetServer +from prompt_toolkit.contrib.telnet.server import TelnetConnection, TelnetServer from ptpython.repl import embed @@ -28,7 +30,7 @@ def ensure_key(filename: str = "ssh_host_key") -> str: return str(path) -async def interact(connection: PromptToolkitSSHSession) -> None: +async def interact(connection: PromptToolkitSSHSession | TelnetConnection) -> None: global_dict = {**globals(), "print": print_formatted_text} await embed(return_asyncio_coroutine=True, globals=global_dict) diff --git a/src/ptpython/repl.py b/src/ptpython/repl.py index ba6717fb..469ed694 100644 --- a/src/ptpython/repl.py +++ b/src/ptpython/repl.py @@ -20,7 +20,17 @@ import warnings from dis import COMPILER_FLAG_NAMES from pathlib import Path -from typing import Any, Callable, ContextManager, Iterable, NoReturn, Sequence +from typing import ( + Any, + Callable, + ContextManager, + Coroutine, + Iterable, + Literal, + NoReturn, + Sequence, + overload, +) from prompt_toolkit.formatted_text import OneStyleAndTextTuple from prompt_toolkit.patch_stdout import patch_stdout as patch_stdout_context @@ -505,6 +515,34 @@ class ReplExit(Exception): """ +@overload +def embed( + globals: dict[str, Any] | None = ..., + locals: dict[str, Any] | None = ..., + configure: Callable[[PythonRepl], None] | None = ..., + vi_mode: bool = ..., + history_filename: str | None = ..., + title: str | None = ..., + startup_paths: Sequence[str | Path] | None = ..., + patch_stdout: bool = ..., + return_asyncio_coroutine: Literal[False] = ..., +) -> None: ... + + +@overload +def embed( + globals: dict[str, Any] | None = ..., + locals: dict[str, Any] | None = ..., + configure: Callable[[PythonRepl], None] | None = ..., + vi_mode: bool = ..., + history_filename: str | None = ..., + title: str | None = ..., + startup_paths: Sequence[str | Path] | None = ..., + patch_stdout: bool = ..., + return_asyncio_coroutine: Literal[True] = ..., +) -> Coroutine[Any, Any, None]: ... + + def embed( globals: dict[str, Any] | None = None, locals: dict[str, Any] | None = None, @@ -515,7 +553,7 @@ def embed( startup_paths: Sequence[str | Path] | None = None, patch_stdout: bool = False, return_asyncio_coroutine: bool = False, -) -> None: +) -> None | Coroutine[Any, Any, None]: """ Call this to embed Python shell at the current point in your program. It's similar to `IPython.embed` and `bpython.embed`. :: @@ -577,3 +615,4 @@ async def coroutine() -> None: else: with patch_context: repl.run() + return None From 836431ff6775aac2c2e3aafa3295b259ebe99d0a Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Tue, 15 Apr 2025 09:24:02 +0000 Subject: [PATCH 220/220] Release 3.0.30 --- CHANGELOG | 11 +++++++++++ pyproject.toml | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index bef7d07f..7706260d 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,17 @@ CHANGELOG ========= +3.0.30: 2025-04-15 +------------------ + +New features: +- Show exception cause/context when printing chained exceptions. +- Reworked project layout and use pyproject.toml instead of setup.py. + +Breaking changes: +- Drop Python 3.7 support. + + 3.0.29: 2024-07-22 ------------------ diff --git a/pyproject.toml b/pyproject.toml index 72259863..00e2d5f8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "ptpython" -version = "3.0.29" +version = "3.0.30" description = "Python REPL build on top of prompt_toolkit" readme = "README.rst" authors = [{ name = "Jonathan Slenders" }]