-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
gh-130698: Add safe methods to get prompts for new REPL #131110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 5 commits
7e633d7
707bd38
ab84ee6
5d91db7
e0b474b
02e7e84
c68a695
5293313
4fc6853
ba8633b
cac161d
60c189f
fb66f37
abd1737
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -5,6 +5,12 @@ | |||||||||||||||||||||||||||
| ANSI_ESCAPE_SEQUENCE = re.compile(r"\x1b\[[ -@]*[A-~]") | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| DEFAULT_PS1 = ">>> " | ||||||||||||||||||||||||||||
| DEFAULT_PS2 = "... " | ||||||||||||||||||||||||||||
| DEFAULT_PS3 = "... " | ||||||||||||||||||||||||||||
| DEFAULT_PS4 = "... " | ||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should these match? Lines 196 to 199 in 95800fe
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, I believe this is should be like: I had misread this Lines 171 to 179 in 71da68d
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| @functools.cache | ||||||||||||||||||||||||||||
| def str_width(c: str) -> int: | ||||||||||||||||||||||||||||
| if ord(c) < 128: | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||
| from .support import handle_all_events, handle_events_narrow_console, code_to_events, prepare_reader, prepare_console | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| from _pyrepl.console import Event | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| from _pyrepl.reader import Reader | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| from _pyrepl.utils import DEFAULT_PS1, DEFAULT_PS2, DEFAULT_PS3, DEFAULT_PS4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| class TestReader(TestCase): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -279,6 +280,113 @@ def test_prompt_length(self): | |||||||||||||||||||||||||||||||||||||||||||||||||||
| self.assertEqual(prompt, "\033[0;32m樂>\033[0m> ") | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.assertEqual(l, 5) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| def test_prompt_ps1_raise_exception(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Handles exceptions from ps1 prompt | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| class Prompt: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| def __str__(self): 1/0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| def prepare_reader_keep_prompts(*args, **kwargs): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| reader = prepare_reader(*args, **kwargs) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| del reader.get_prompt | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| reader.ps1 = Prompt() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| reader.ps2 = "... " | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| reader.ps3 = "... " | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| reader.ps4 = "" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| reader.can_colorize = False | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| reader.paste_mode = False | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return reader | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| events = code_to_events("a=1") | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| reader, _ = handle_events_narrow_console( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| events, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| prepare_reader=prepare_reader_keep_prompts, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| def prepare_reader_keep_prompts(*args, **kwargs): | |
| reader = prepare_reader(*args, **kwargs) | |
| del reader.get_prompt | |
| reader.ps1 = Prompt() | |
| reader.ps2 = "... " | |
| reader.ps3 = "... " | |
| reader.ps4 = "" | |
| reader.can_colorize = False | |
| reader.paste_mode = False | |
| return reader | |
| events = code_to_events("a=1") | |
| reader, _ = handle_events_narrow_console( | |
| events, | |
| prepare_reader=prepare_reader_keep_prompts, | |
| ) | |
| def prepare_reader(events): | |
| console = FakeConsole(events) | |
| config = ReadlineConfig(readline_completer=None) | |
| reader = ReadlineAlikeReader(console=console, config=config) | |
| reader.can_colorize = False | |
| return reader | |
| events = code_to_events("if some_condition:\nsome_function()") | |
| reader = prepare_reader(events) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately this doesn't simplify code. I have added more simpler one on my sight. Note, we should use handle_all_events (or handle_events_narrow_console) to process whole input string and get it in the reader.buffer to check if '\n' contains or not in buffer.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Avoid exiting the new REPL when prompt object raises exception. | ||
sergey-miryanov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Uh oh!
There was an error while loading. Please reload this page.