Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 59eccb2

Browse files
authored
Inspect continuation prompt signature and pass only viable arguments. (#14274)
Closes #14273
2 parents fd2cf18 + d379f51 commit 59eccb2

4 files changed

Lines changed: 30 additions & 19 deletions

File tree

IPython/terminal/interactiveshell.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import os
44
import sys
5+
import inspect
56
from warnings import warn
67
from typing import Union as UnionType, Optional
78

@@ -65,8 +66,8 @@
6566
PTK3 = ptk_version.startswith('3.')
6667

6768

68-
class _NoStyle(Style): pass
69-
69+
class _NoStyle(Style):
70+
pass
7071

7172

7273
_style_overrides_light_bg = {
@@ -83,6 +84,20 @@ class _NoStyle(Style): pass
8384
Token.OutPromptNum: '#ansired bold',
8485
}
8586

87+
88+
def _backward_compat_continuation_prompt_tokens(method, width: int, *, lineno: int):
89+
"""
90+
Sagemath use custom prompt and we broke them in 8.19.
91+
"""
92+
sig = inspect.signature(method)
93+
if "lineno" in inspect.signature(method).parameters or any(
94+
[p.kind == p.VAR_KEYWORD for p in sig.parameters.values()]
95+
):
96+
return method(width, lineno=lineno)
97+
else:
98+
return method(width)
99+
100+
86101
def get_default_editor():
87102
try:
88103
return os.environ['EDITOR']
@@ -762,7 +777,9 @@ def get_message():
762777
"message": get_message,
763778
"prompt_continuation": (
764779
lambda width, lineno, is_soft_wrap: PygmentsTokens(
765-
self.prompts.continuation_prompt_tokens(width, lineno=lineno)
780+
_backward_compat_continuation_prompt_tokens(
781+
self.prompts.continuation_prompt_tokens, width, lineno=lineno
782+
)
766783
)
767784
),
768785
"multiline": True,

docs/source/config/details.rst

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ which defines the defaults. The required interface is like this:
3333
Prompt style definition. *shell* is a reference to the
3434
:class:`~.TerminalInteractiveShell` instance.
3535

36-
.. method:: in_prompt_tokens(cli=None)
37-
continuation_prompt_tokens(self, cli=None, width=None)
36+
.. method:: in_prompt_tokens()
37+
continuation_prompt_tokens(self, width=None)
3838
rewrite_prompt_tokens()
3939
out_prompt_tokens()
4040

@@ -43,8 +43,6 @@ which defines the defaults. The required interface is like this:
4343
For continuation prompts, *width* is an integer representing the width of
4444
the prompt area in terminal columns.
4545

46-
*cli*, where used, is the prompt_toolkit ``CommandLineInterface`` instance.
47-
This is mainly for compatibility with the API prompt_toolkit expects.
4846

4947
Here is an example Prompt class that will show the current working directory
5048
in the input prompt:
@@ -55,7 +53,7 @@ in the input prompt:
5553
import os
5654
5755
class MyPrompt(Prompts):
58-
def in_prompt_tokens(self, cli=None):
56+
def in_prompt_tokens(self):
5957
return [(Token, os.getcwd()),
6058
(Token.Prompt, ' >>>')]
6159

examples/Embedding/embed_class_long.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,26 @@
1818
# %run example-embed.py)
1919

2020
from IPython.terminal.prompts import Prompts, Token
21+
from traitlets.config.loader import Config
2122

2223
class CustomPrompt(Prompts):
2324

24-
def in_prompt_tokens(self, cli=None):
25+
def in_prompt_tokens(self):
2526

26-
return [
27+
return [
2728
(Token.Prompt, 'In <'),
2829
(Token.PromptNum, str(self.shell.execution_count)),
2930
(Token.Prompt, '>: '),
3031
]
3132

3233
def out_prompt_tokens(self):
33-
return [
34+
return [
3435
(Token.OutPrompt, 'Out<'),
3536
(Token.OutPromptNum, str(self.shell.execution_count)),
3637
(Token.OutPrompt, '>: '),
3738
]
3839

3940

40-
from traitlets.config.loader import Config
4141
try:
4242
get_ipython
4343
except NameError:

examples/utils/cwd_prompt.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
class MyPrompt(Prompts):
88

9-
def in_prompt_tokens(self, cli=None):
10-
return [(Token, os.getcwd()),
11-
(Token.Prompt, '>>>')]
9+
def in_prompt_tokens(self):
10+
return [(Token, os.getcwd()), (Token.Prompt, ">>>")]
11+
1212

1313
def load_ipython_extension(shell):
1414
new_prompts = MyPrompt(shell)
@@ -20,7 +20,3 @@ def unload_ipython_extension(shell):
2020
print("cannot unload")
2121
else:
2222
shell.prompts = shell.prompts.old_prompts
23-
24-
25-
26-

0 commit comments

Comments
 (0)