diff options
| author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2024-07-16 06:28:41 (GMT) |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-16 06:28:41 (GMT) |
| commit | 73f77e642a4b67348274efa12d6671f957d7f41f (patch) | |
| tree | 600e0845f13303dc72f594efa6295194bb788fab /Lib/test/test_pyrepl/test_interact.py | |
| parent | a1505afd392b91553d19b63841628d6ef3b3e7f9 (diff) | |
| download | cpython-73f77e642a4b67348274efa12d6671f957d7f41f.zip cpython-73f77e642a4b67348274efa12d6671f957d7f41f.tar.gz cpython-73f77e642a4b67348274efa12d6671f957d7f41f.tar.bz2 | |
[3.13] gh-121610: pyrepl - handle extending blocks when multi-statement blocks are pasted (GH-121757) (GH-121825)
console.compile with the "single" param throws an exception when
there are multiple statements, never allowing to adding newlines
to a pasted code block (gh-121610)
This adds a few extra checks to allow extending when in an indented
block, and tests for a few examples.
(cherry picked from commit 7d111dac160c658b277ec0fac75eee8edcfbe9dc)
Co-authored-by: saucoide <32314353+saucoide@users.noreply.github.com>
Co-authored-by: Ćukasz Langa <lukasz@langa.pl>
Diffstat (limited to 'Lib/test/test_pyrepl/test_interact.py')
| -rw-r--r-- | Lib/test/test_pyrepl/test_interact.py | 103 |
1 files changed, 102 insertions, 1 deletions
diff --git a/Lib/test/test_pyrepl/test_interact.py b/Lib/test/test_pyrepl/test_interact.py index df97b13..538dfd3 100644 --- a/Lib/test/test_pyrepl/test_interact.py +++ b/Lib/test/test_pyrepl/test_interact.py @@ -7,7 +7,7 @@ from textwrap import dedent from test.support import force_not_colorized from _pyrepl.console import InteractiveColoredConsole - +from _pyrepl.simple_interact import _more_lines class TestSimpleInteract(unittest.TestCase): def test_multiple_statements(self): @@ -111,3 +111,104 @@ class TestSimpleInteract(unittest.TestCase): result = console.runsource(source) self.assertFalse(result) self.assertEqual(f.getvalue(), "{'x': <class 'int'>}\n") + + +class TestMoreLines(unittest.TestCase): + def test_invalid_syntax_single_line(self): + namespace = {} + code = "if foo" + console = InteractiveColoredConsole(namespace, filename="<stdin>") + self.assertFalse(_more_lines(console, code)) + + def test_empty_line(self): + namespace = {} + code = "" + console = InteractiveColoredConsole(namespace, filename="<stdin>") + self.assertFalse(_more_lines(console, code)) + + def test_valid_single_statement(self): + namespace = {} + code = "foo = 1" + console = InteractiveColoredConsole(namespace, filename="<stdin>") + self.assertFalse(_more_lines(console, code)) + + def test_multiline_single_assignment(self): + namespace = {} + code = dedent("""\ + foo = [ + 1, + 2, + 3, + ]""") + console = InteractiveColoredConsole(namespace, filename="<stdin>") + self.assertFalse(_more_lines(console, code)) + + def test_multiline_single_block(self): + namespace = {} + code = dedent("""\ + def foo(): + '''docs''' + + return 1""") + console = InteractiveColoredConsole(namespace, filename="<stdin>") + self.assertTrue(_more_lines(console, code)) + + def test_multiple_statements_single_line(self): + namespace = {} + code = "foo = 1;bar = 2" + console = InteractiveColoredConsole(namespace, filename="<stdin>") + self.assertFalse(_more_lines(console, code)) + + def test_multiple_statements(self): + namespace = {} + code = dedent("""\ + import time + + foo = 1""") + console = InteractiveColoredConsole(namespace, filename="<stdin>") + self.assertTrue(_more_lines(console, code)) + + def test_multiple_blocks(self): + namespace = {} + code = dedent("""\ + from dataclasses import dataclass + + @dataclass + class Point: + x: float + y: float""") + console = InteractiveColoredConsole(namespace, filename="<stdin>") + self.assertTrue(_more_lines(console, code)) + + def test_multiple_blocks_empty_newline(self): + namespace = {} + code = dedent("""\ + from dataclasses import dataclass + + @dataclass + class Point: + x: float + y: float + """) + console = InteractiveColoredConsole(namespace, filename="<stdin>") + self.assertFalse(_more_lines(console, code)) + + def test_multiple_blocks_indented_newline(self): + namespace = {} + code = ( + "from dataclasses import dataclass\n" + "\n" + "@dataclass\n" + "class Point:\n" + " x: float\n" + " y: float\n" + " " + ) + console = InteractiveColoredConsole(namespace, filename="<stdin>") + self.assertFalse(_more_lines(console, code)) + + def test_incomplete_statement(self): + namespace = {} + code = "if foo:" + console = InteractiveColoredConsole(namespace, filename="<stdin>") + self.assertTrue(_more_lines(console, code)) |
