Skip to content

Commit

Permalink
Inspect continuation prompt signature and pass only viable arguments. (
Browse files Browse the repository at this point in the history
  • Loading branch information
Carreau authored Jan 8, 2024
2 parents fd2cf18 + d379f51 commit 59eccb2
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 19 deletions.
23 changes: 20 additions & 3 deletions IPython/terminal/interactiveshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import os
import sys
import inspect
from warnings import warn
from typing import Union as UnionType, Optional

Expand Down Expand Up @@ -65,8 +66,8 @@
PTK3 = ptk_version.startswith('3.')


class _NoStyle(Style): pass

class _NoStyle(Style):
pass


_style_overrides_light_bg = {
Expand All @@ -83,6 +84,20 @@ class _NoStyle(Style): pass
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']
Expand Down Expand Up @@ -762,7 +777,9 @@ def get_message():
"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,
Expand Down
8 changes: 3 additions & 5 deletions docs/source/config/details.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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:
Expand All @@ -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, ' >>>')]
Expand Down
8 changes: 4 additions & 4 deletions examples/Embedding/embed_class_long.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,26 @@
# %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:
Expand Down
10 changes: 3 additions & 7 deletions examples/utils/cwd_prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

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)
Expand All @@ -20,7 +20,3 @@ def unload_ipython_extension(shell):
print("cannot unload")
else:
shell.prompts = shell.prompts.old_prompts




0 comments on commit 59eccb2

Please sign in to comment.