[3.10] bpo-46091: Correctly calculate indentation levels for whitespace lines with continuation characters (GH-30130). (GH-30898)
(cherry picked from commit a0efc0c1960e2c49e0092694d98395555270914c)
Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py
index 39fc7e9..95af9e2 100644
--- a/Lib/test/test_ast.py
+++ b/Lib/test/test_ast.py
@@ -1045,8 +1045,7 @@ def test_literal_eval_malformed_lineno(self):
ast.literal_eval(node)
def test_literal_eval_syntax_errors(self):
- msg = "unexpected character after line continuation character"
- with self.assertRaisesRegex(SyntaxError, msg):
+ with self.assertRaisesRegex(SyntaxError, "unexpected indent"):
ast.literal_eval(r'''
\
(\
diff --git a/Lib/test/test_syntax.py b/Lib/test/test_syntax.py
index 7aa93a0..ac5a41c 100644
--- a/Lib/test/test_syntax.py
+++ b/Lib/test/test_syntax.py
@@ -1463,6 +1463,36 @@ def test_empty_line_after_linecont(self):
except SyntaxError:
self.fail("Empty line after a line continuation character is valid.")
+ # See issue-46091
+ s1 = r"""\
+def fib(n):
+ \
+'''Print a Fibonacci series up to n.'''
+ \
+a, b = 0, 1
+"""
+ s2 = r"""\
+def fib(n):
+ '''Print a Fibonacci series up to n.'''
+ a, b = 0, 1
+"""
+ try:
+ self.assertEqual(compile(s1, '<string>', 'exec'), compile(s2, '<string>', 'exec'))
+ except SyntaxError:
+ self.fail("Indented statement over multiple lines is valid")
+
+ def test_continuation_bad_indentation(self):
+ # Check that code that breaks indentation across multiple lines raises a syntax error
+
+ code = r"""\
+if x:
+ y = 1
+ \
+ foo = 1
+ """
+
+ self.assertRaises(IndentationError, exec, code)
+
@support.cpython_only
def test_nested_named_except_blocks(self):
code = ""
diff --git a/Lib/test/test_tokenize.py b/Lib/test/test_tokenize.py
index 4bce1ca..127f0a1 100644
--- a/Lib/test/test_tokenize.py
+++ b/Lib/test/test_tokenize.py
@@ -6,6 +6,7 @@
NEWLINE)
from io import BytesIO, StringIO
import unittest
+from textwrap import dedent
from unittest import TestCase, mock
from test.test_grammar import (VALID_UNDERSCORE_LITERALS,
INVALID_UNDERSCORE_LITERALS)
@@ -45,7 +46,6 @@ def check_tokenize(self, s, expected):
# The ENDMARKER and final NEWLINE are omitted.
f = BytesIO(s.encode('utf-8'))
result = stringify_tokens_from_source(tokenize(f.readline), s)
-
self.assertEqual(result,
[" ENCODING 'utf-8' (0, 0) (0, 0)"] +
expected.rstrip().splitlines())