From 59eccb24ac165d4fff69eed51116058788fb11dc 2024-01-08 09:34:54 From: M Bussonnier Date: 2024-01-08 09:34:54 Subject: [PATCH] Inspect continuation prompt signature and pass only viable arguments. (#14274) Closes #14273 --- diff --git a/IPython/terminal/interactiveshell.py b/IPython/terminal/interactiveshell.py index 1037634..fcb816e 100644 --- a/IPython/terminal/interactiveshell.py +++ b/IPython/terminal/interactiveshell.py @@ -2,6 +2,7 @@ import os import sys +import inspect from warnings import warn from typing import Union as UnionType, Optional @@ -65,8 +66,8 @@ from .shortcuts.auto_suggest import ( PTK3 = ptk_version.startswith('3.') -class _NoStyle(Style): pass - +class _NoStyle(Style): + pass _style_overrides_light_bg = { @@ -83,6 +84,20 @@ _style_overrides_linux = { Token.OutPromptNum: '#ansired bold', } + +def _backward_compat_continuation_prompt_tokens(method, width: int, *, lineno: int): + """ + Sagemath use custom prompt and we broke them in 8.19. + """ + sig = inspect.signature(method) + if "lineno" in inspect.signature(method).parameters or any( + [p.kind == p.VAR_KEYWORD for p in sig.parameters.values()] + ): + return method(width, lineno=lineno) + else: + return method(width) + + def get_default_editor(): try: return os.environ['EDITOR'] @@ -762,7 +777,9 @@ class TerminalInteractiveShell(InteractiveShell): "message": get_message, "prompt_continuation": ( lambda width, lineno, is_soft_wrap: PygmentsTokens( - self.prompts.continuation_prompt_tokens(width, lineno=lineno) + _backward_compat_continuation_prompt_tokens( + self.prompts.continuation_prompt_tokens, width, lineno=lineno + ) ) ), "multiline": True, diff --git a/docs/source/config/details.rst b/docs/source/config/details.rst index 6ba9855..69dad2c 100644 --- a/docs/source/config/details.rst +++ b/docs/source/config/details.rst @@ -33,8 +33,8 @@ which defines the defaults. The required interface is like this: Prompt style definition. *shell* is a reference to the :class:`~.TerminalInteractiveShell` instance. - .. method:: in_prompt_tokens(cli=None) - continuation_prompt_tokens(self, cli=None, width=None) + .. method:: in_prompt_tokens() + continuation_prompt_tokens(self, width=None) rewrite_prompt_tokens() out_prompt_tokens() @@ -43,8 +43,6 @@ which defines the defaults. The required interface is like this: For continuation prompts, *width* is an integer representing the width of the prompt area in terminal columns. - *cli*, where used, is the prompt_toolkit ``CommandLineInterface`` instance. - This is mainly for compatibility with the API prompt_toolkit expects. Here is an example Prompt class that will show the current working directory in the input prompt: @@ -55,7 +53,7 @@ in the input prompt: import os class MyPrompt(Prompts): - def in_prompt_tokens(self, cli=None): + def in_prompt_tokens(self): return [(Token, os.getcwd()), (Token.Prompt, ' >>>')] diff --git a/examples/Embedding/embed_class_long.py b/examples/Embedding/embed_class_long.py index 53a31cf..0cd09d0 100755 --- a/examples/Embedding/embed_class_long.py +++ b/examples/Embedding/embed_class_long.py @@ -18,26 +18,26 @@ The code in this file is deliberately extra-verbose, meant for learning.""" # %run example-embed.py) from IPython.terminal.prompts import Prompts, Token +from traitlets.config.loader import Config class CustomPrompt(Prompts): - def in_prompt_tokens(self, cli=None): + def in_prompt_tokens(self): - return [ + return [ (Token.Prompt, 'In <'), (Token.PromptNum, str(self.shell.execution_count)), (Token.Prompt, '>: '), ] def out_prompt_tokens(self): - return [ + return [ (Token.OutPrompt, 'Out<'), (Token.OutPromptNum, str(self.shell.execution_count)), (Token.OutPrompt, '>: '), ] -from traitlets.config.loader import Config try: get_ipython except NameError: diff --git a/examples/utils/cwd_prompt.py b/examples/utils/cwd_prompt.py index 1d1b03a..ca653da 100644 --- a/examples/utils/cwd_prompt.py +++ b/examples/utils/cwd_prompt.py @@ -6,9 +6,9 @@ import os class MyPrompt(Prompts): - def in_prompt_tokens(self, cli=None): - return [(Token, os.getcwd()), - (Token.Prompt, '>>>')] + def in_prompt_tokens(self): + return [(Token, os.getcwd()), (Token.Prompt, ">>>")] + def load_ipython_extension(shell): new_prompts = MyPrompt(shell) @@ -20,7 +20,3 @@ def unload_ipython_extension(shell): print("cannot unload") else: shell.prompts = shell.prompts.old_prompts - - - -