Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix this better by detecting paste mode
  • Loading branch information
pablogsal committed May 7, 2024
commit b060efae85ff3ca53eb71bb43e0c854ceea09bfb
3 changes: 3 additions & 0 deletions Lib/_pyrepl/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,13 +460,16 @@ def do(self) -> None:
class paste_mode(Command):

def do(self) -> None:
if not self.reader.paste_mode:
self.reader.was_paste_mode_activated = True
self.reader.paste_mode = not self.reader.paste_mode
self.reader.dirty = True


class enable_bracketed_paste(Command):
def do(self) -> None:
self.reader.paste_mode = True
self.reader.was_paste_mode_activated = True

class disable_bracketed_paste(Command):
def do(self) -> None:
Expand Down
1 change: 1 addition & 0 deletions Lib/_pyrepl/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ class Reader:
dirty: bool = False
finished: bool = False
paste_mode: bool = False
was_paste_mode_activated: bool = False
commands: dict[str, type[Command]] = field(default_factory=make_default_commands)
last_command: type[Command] | None = None
syntax_table: dict[str, int] = field(default_factory=make_default_syntax_table)
Expand Down
3 changes: 2 additions & 1 deletion Lib/_pyrepl/readline.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,11 @@ def multiline_input(self, more_lines, ps1, ps2):
reader.more_lines = more_lines
reader.ps1 = reader.ps2 = ps1
reader.ps3 = reader.ps4 = ps2
return reader.readline()
return reader.readline(), reader.was_paste_mode_activated
finally:
reader.more_lines = saved
reader.paste_mode = False
reader.was_paste_mode_activated = False

def parse_and_bind(self, string: str) -> None:
pass # XXX we don't support parsing GNU-readline-style init files
Expand Down
12 changes: 5 additions & 7 deletions Lib/_pyrepl/simple_interact.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,6 @@ def __init__(
def showtraceback(self):
super().showtraceback(colorize=self.can_colorize)

def push(self, line, filename=None, symbol="single"):
if line.count("\n") > 0:
symbol = "exec"
return super().push(line, filename=filename, _symbol=symbol)


def run_multiline_interactive_console(
mainmodule: ModuleType | None= None, future_flags: int = 0
Expand Down Expand Up @@ -140,7 +135,7 @@ def more_lines(unicodetext: str) -> bool:
ps1 = getattr(sys, "ps1", ">>> ")
ps2 = getattr(sys, "ps2", "... ")
try:
statement = multiline_input(more_lines, ps1, ps2)
statement, contains_pasted_code = multiline_input(more_lines, ps1, ps2)
except EOFError:
break

Expand All @@ -149,7 +144,10 @@ def more_lines(unicodetext: str) -> bool:

input_name = f"<python-input-{input_n}>"
linecache._register_code(input_name, statement, "<stdin>") # type: ignore[attr-defined]
more = console.push(_strip_final_indent(statement), filename=input_name)
symbol = "single" if not contains_pasted_code else "exec"
more = console.push(_strip_final_indent(statement), filename=input_name, _symbol=symbol)
if contains_pasted_code and more:
more = console.push(_strip_final_indent(statement), filename=input_name, _symbol="single")
assert not more
input_n += 1
except KeyboardInterrupt:
Expand Down
18 changes: 0 additions & 18 deletions Lib/test/test_pyrepl.py
Original file line number Diff line number Diff line change
Expand Up @@ -977,24 +977,6 @@ def test_setpos_fromxy_in_wrapped_line(self):
reader.setpos_from_xy(0, 1)
self.assertEqual(reader.pos, 9)

class TestInteractiveColoredConsole(unittest.TestCase):
def test_showtraceback(self):
console = InteractiveColoredConsole()
with patch('code.InteractiveConsole.showtraceback') as mock_showtraceback:
console.showtraceback()
mock_showtraceback.assert_called_once_with(colorize=console.can_colorize)

def test_push_single_line(self):
console = InteractiveColoredConsole()
with patch('code.InteractiveConsole.runsource') as mock_runsource:
console.push('print("Hello, world!")')
mock_runsource.assert_called_once_with('print("Hello, world!")', '<console>', symbol='single')

def test_push_multiline(self):
console = InteractiveColoredConsole()
with patch('code.InteractiveConsole.runsource') as mock_runsource:
console.push('if True:\n print("Hello, world!")')
mock_runsource.assert_called_once_with('if True:\n print("Hello, world!")', '<console>', symbol='exec')

if __name__ == '__main__':
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WHAT IS THIS

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The resistance won't be defeated

unittest.main()