[Python-checkins] gh-101951: use textwrap.dedent in compiler tests to make them more readable (GH-101950)

miss-islington webhook-mailer at python.org
Thu Feb 16 07:32:20 EST 2023


https://github.com/python/cpython/commit/36b139af638cdeb671cb6b8b0315b254148688f7
commit: 36b139af638cdeb671cb6b8b0315b254148688f7
branch: main
author: Irit Katriel <1055913+iritkatriel at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2023-02-16T04:31:59-08:00
summary:

gh-101951: use textwrap.dedent in compiler tests to make them more readable (GH-101950)



Fixes #101951.

Automerge-Triggered-By: GH:iritkatriel

files:
M Lib/test/test_compile.py

diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py
index a77742c0cfa6..fe775779c50f 100644
--- a/Lib/test/test_compile.py
+++ b/Lib/test/test_compile.py
@@ -115,24 +115,24 @@ def test_extended_arg(self):
         repeat = 2000
         longexpr = 'x = x or ' + '-x' * repeat
         g = {}
-        code = '''
-def f(x):
-    %s
-    %s
-    %s
-    %s
-    %s
-    %s
-    %s
-    %s
-    %s
-    %s
-    # the expressions above have no effect, x == argument
-    while x:
-        x -= 1
-        # EXTENDED_ARG/JUMP_ABSOLUTE here
-    return x
-''' % ((longexpr,)*10)
+        code = textwrap.dedent('''
+            def f(x):
+                %s
+                %s
+                %s
+                %s
+                %s
+                %s
+                %s
+                %s
+                %s
+                %s
+                # the expressions above have no effect, x == argument
+                while x:
+                    x -= 1
+                    # EXTENDED_ARG/JUMP_ABSOLUTE here
+                return x
+            ''' % ((longexpr,)*10))
         exec(code, g)
         self.assertEqual(g['f'](5), 0)
 
@@ -148,10 +148,11 @@ def test_float_literals(self):
 
     def test_indentation(self):
         # testing compile() of indented block w/o trailing newline"
-        s = """
-if 1:
-    if 2:
-        pass"""
+        s = textwrap.dedent("""
+            if 1:
+                if 2:
+                    pass
+            """)
         compile(s, "<string>", "exec")
 
     # This test is probably specific to CPython and may not generalize
@@ -1157,14 +1158,15 @@ def test_if_expression_expression_empty_block(self):
 
     def test_multi_line_lambda_as_argument(self):
         # See gh-101928
-        compile("""
-def foo(param, lambda_exp):
-    pass
+        code = textwrap.dedent("""
+            def foo(param, lambda_exp):
+                pass
 
-foo(param=0,
-    lambda_exp=lambda:
-    1)
-        """, "<test>", "exec")
+            foo(param=0,
+                lambda_exp=lambda:
+                1)
+        """)
+        compile(code, "<test>", "exec")
 
 
 @requires_debug_ranges()
@@ -1252,24 +1254,24 @@ def test_compiles_to_extended_op_arg(self):
             column=2, end_column=9, occurrence=2)
 
     def test_multiline_expression(self):
-        snippet = """\
-f(
-    1, 2, 3, 4
-)
-"""
+        snippet = textwrap.dedent("""\
+            f(
+                1, 2, 3, 4
+            )
+            """)
         compiled_code, _ = self.check_positions_against_ast(snippet)
         self.assertOpcodeSourcePositionIs(compiled_code, 'CALL',
             line=1, end_line=3, column=0, end_column=1)
 
     @requires_specialization
     def test_multiline_boolean_expression(self):
-        snippet = """\
-if (a or
-    (b and not c) or
-    not (
-        d > 0)):
-    x = 42
-"""
+        snippet = textwrap.dedent("""\
+            if (a or
+                (b and not c) or
+                not (
+                    d > 0)):
+                x = 42
+            """)
         compiled_code, _ = self.check_positions_against_ast(snippet)
         # jump if a is true:
         self.assertOpcodeSourcePositionIs(compiled_code, 'POP_JUMP_IF_TRUE',
@@ -1288,11 +1290,11 @@ def test_multiline_boolean_expression(self):
             line=4, end_line=4, column=8, end_column=13, occurrence=2)
 
     def test_multiline_assert(self):
-        snippet = """\
-assert (a > 0 and
-        bb > 0 and
-        ccc == 4), "error msg"
-"""
+        snippet = textwrap.dedent("""\
+            assert (a > 0 and
+                    bb > 0 and
+                    ccc == 4), "error msg"
+            """)
         compiled_code, _ = self.check_positions_against_ast(snippet)
         self.assertOpcodeSourcePositionIs(compiled_code, 'LOAD_ASSERTION_ERROR',
             line=1, end_line=3, column=0, end_column=30, occurrence=1)
@@ -1305,14 +1307,14 @@ def test_multiline_assert(self):
             line=1, end_line=3, column=0, end_column=30, occurrence=1)
 
     def test_multiline_generator_expression(self):
-        snippet = """\
-((x,
-    2*x)
-    for x
-    in [1,2,3] if (x > 0
-                   and x < 100
-                   and x != 50))
-"""
+        snippet = textwrap.dedent("""\
+            ((x,
+                2*x)
+                for x
+                in [1,2,3] if (x > 0
+                               and x < 100
+                               and x != 50))
+            """)
         compiled_code, _ = self.check_positions_against_ast(snippet)
         compiled_code = compiled_code.co_consts[0]
         self.assertIsInstance(compiled_code, types.CodeType)
@@ -1324,14 +1326,14 @@ def test_multiline_generator_expression(self):
             line=1, end_line=6, column=0, end_column=32, occurrence=1)
 
     def test_multiline_async_generator_expression(self):
-        snippet = """\
-((x,
-    2*x)
-    async for x
-    in [1,2,3] if (x > 0
-                   and x < 100
-                   and x != 50))
-"""
+        snippet = textwrap.dedent("""\
+            ((x,
+                2*x)
+                async for x
+                in [1,2,3] if (x > 0
+                               and x < 100
+                               and x != 50))
+            """)
         compiled_code, _ = self.check_positions_against_ast(snippet)
         compiled_code = compiled_code.co_consts[0]
         self.assertIsInstance(compiled_code, types.CodeType)
@@ -1341,14 +1343,14 @@ def test_multiline_async_generator_expression(self):
             line=1, end_line=6, column=0, end_column=32, occurrence=1)
 
     def test_multiline_list_comprehension(self):
-        snippet = """\
-[(x,
-    2*x)
-    for x
-    in [1,2,3] if (x > 0
-                   and x < 100
-                   and x != 50)]
-"""
+        snippet = textwrap.dedent("""\
+            [(x,
+                2*x)
+                for x
+                in [1,2,3] if (x > 0
+                               and x < 100
+                               and x != 50)]
+            """)
         compiled_code, _ = self.check_positions_against_ast(snippet)
         compiled_code = compiled_code.co_consts[0]
         self.assertIsInstance(compiled_code, types.CodeType)
@@ -1360,15 +1362,15 @@ def test_multiline_list_comprehension(self):
             line=1, end_line=6, column=0, end_column=32, occurrence=1)
 
     def test_multiline_async_list_comprehension(self):
-        snippet = """\
-async def f():
-    [(x,
-        2*x)
-        async for x
-        in [1,2,3] if (x > 0
-                       and x < 100
-                       and x != 50)]
-"""
+        snippet = textwrap.dedent("""\
+            async def f():
+                [(x,
+                    2*x)
+                    async for x
+                    in [1,2,3] if (x > 0
+                                   and x < 100
+                                   and x != 50)]
+            """)
         compiled_code, _ = self.check_positions_against_ast(snippet)
         g = {}
         eval(compiled_code, g)
@@ -1382,14 +1384,14 @@ async def f():
             line=2, end_line=7, column=4, end_column=36, occurrence=1)
 
     def test_multiline_set_comprehension(self):
-        snippet = """\
-{(x,
-    2*x)
-    for x
-    in [1,2,3] if (x > 0
-                   and x < 100
-                   and x != 50)}
-"""
+        snippet = textwrap.dedent("""\
+            {(x,
+                2*x)
+                for x
+                in [1,2,3] if (x > 0
+                               and x < 100
+                               and x != 50)}
+            """)
         compiled_code, _ = self.check_positions_against_ast(snippet)
         compiled_code = compiled_code.co_consts[0]
         self.assertIsInstance(compiled_code, types.CodeType)
@@ -1401,15 +1403,15 @@ def test_multiline_set_comprehension(self):
             line=1, end_line=6, column=0, end_column=32, occurrence=1)
 
     def test_multiline_async_set_comprehension(self):
-        snippet = """\
-async def f():
-    {(x,
-        2*x)
-        async for x
-        in [1,2,3] if (x > 0
-                       and x < 100
-                       and x != 50)}
-"""
+        snippet = textwrap.dedent("""\
+            async def f():
+                {(x,
+                    2*x)
+                    async for x
+                    in [1,2,3] if (x > 0
+                                   and x < 100
+                                   and x != 50)}
+            """)
         compiled_code, _ = self.check_positions_against_ast(snippet)
         g = {}
         eval(compiled_code, g)
@@ -1423,14 +1425,14 @@ async def f():
             line=2, end_line=7, column=4, end_column=36, occurrence=1)
 
     def test_multiline_dict_comprehension(self):
-        snippet = """\
-{x:
-    2*x
-    for x
-    in [1,2,3] if (x > 0
-                   and x < 100
-                   and x != 50)}
-"""
+        snippet = textwrap.dedent("""\
+            {x:
+                2*x
+                for x
+                in [1,2,3] if (x > 0
+                               and x < 100
+                               and x != 50)}
+            """)
         compiled_code, _ = self.check_positions_against_ast(snippet)
         compiled_code = compiled_code.co_consts[0]
         self.assertIsInstance(compiled_code, types.CodeType)
@@ -1442,15 +1444,15 @@ def test_multiline_dict_comprehension(self):
             line=1, end_line=6, column=0, end_column=32, occurrence=1)
 
     def test_multiline_async_dict_comprehension(self):
-        snippet = """\
-async def f():
-    {x:
-        2*x
-        async for x
-        in [1,2,3] if (x > 0
-                       and x < 100
-                       and x != 50)}
-"""
+        snippet = textwrap.dedent("""\
+            async def f():
+                {x:
+                    2*x
+                    async for x
+                    in [1,2,3] if (x > 0
+                                   and x < 100
+                                   and x != 50)}
+            """)
         compiled_code, _ = self.check_positions_against_ast(snippet)
         g = {}
         eval(compiled_code, g)
@@ -1464,11 +1466,11 @@ async def f():
             line=2, end_line=7, column=4, end_column=36, occurrence=1)
 
     def test_matchcase_sequence(self):
-        snippet = """\
-match x:
-    case a, b:
-        pass
-"""
+        snippet = textwrap.dedent("""\
+            match x:
+                case a, b:
+                    pass
+            """)
         compiled_code, _ = self.check_positions_against_ast(snippet)
         self.assertOpcodeSourcePositionIs(compiled_code, 'MATCH_SEQUENCE',
             line=2, end_line=2, column=9, end_column=13, occurrence=1)
@@ -1480,11 +1482,11 @@ def test_matchcase_sequence(self):
             line=2, end_line=2, column=9, end_column=13, occurrence=2)
 
     def test_matchcase_sequence_wildcard(self):
-        snippet = """\
-match x:
-    case a, *b, c:
-        pass
-"""
+        snippet = textwrap.dedent("""\
+            match x:
+                case a, *b, c:
+                    pass
+            """)
         compiled_code, _ = self.check_positions_against_ast(snippet)
         self.assertOpcodeSourcePositionIs(compiled_code, 'MATCH_SEQUENCE',
             line=2, end_line=2, column=9, end_column=17, occurrence=1)
@@ -1498,11 +1500,11 @@ def test_matchcase_sequence_wildcard(self):
             line=2, end_line=2, column=9, end_column=17, occurrence=3)
 
     def test_matchcase_mapping(self):
-        snippet = """\
-match x:
-    case {"a" : a, "b": b}:
-        pass
-"""
+        snippet = textwrap.dedent("""\
+            match x:
+                case {"a" : a, "b": b}:
+                    pass
+            """)
         compiled_code, _ = self.check_positions_against_ast(snippet)
         self.assertOpcodeSourcePositionIs(compiled_code, 'MATCH_MAPPING',
             line=2, end_line=2, column=9, end_column=26, occurrence=1)
@@ -1514,11 +1516,11 @@ def test_matchcase_mapping(self):
             line=2, end_line=2, column=9, end_column=26, occurrence=2)
 
     def test_matchcase_mapping_wildcard(self):
-        snippet = """\
-match x:
-    case {"a" : a, "b": b, **c}:
-        pass
-"""
+        snippet = textwrap.dedent("""\
+            match x:
+                case {"a" : a, "b": b, **c}:
+                    pass
+            """)
         compiled_code, _ = self.check_positions_against_ast(snippet)
         self.assertOpcodeSourcePositionIs(compiled_code, 'MATCH_MAPPING',
             line=2, end_line=2, column=9, end_column=31, occurrence=1)
@@ -1530,11 +1532,11 @@ def test_matchcase_mapping_wildcard(self):
             line=2, end_line=2, column=9, end_column=31, occurrence=2)
 
     def test_matchcase_class(self):
-        snippet = """\
-match x:
-    case C(a, b):
-        pass
-"""
+        snippet = textwrap.dedent("""\
+            match x:
+                case C(a, b):
+                    pass
+            """)
         compiled_code, _ = self.check_positions_against_ast(snippet)
         self.assertOpcodeSourcePositionIs(compiled_code, 'MATCH_CLASS',
             line=2, end_line=2, column=9, end_column=16, occurrence=1)
@@ -1546,11 +1548,11 @@ def test_matchcase_class(self):
             line=2, end_line=2, column=9, end_column=16, occurrence=2)
 
     def test_matchcase_or(self):
-        snippet = """\
-match x:
-    case C(1) | C(2):
-        pass
-"""
+        snippet = textwrap.dedent("""\
+            match x:
+                case C(1) | C(2):
+                    pass
+            """)
         compiled_code, _ = self.check_positions_against_ast(snippet)
         self.assertOpcodeSourcePositionIs(compiled_code, 'MATCH_CLASS',
             line=2, end_line=2, column=9, end_column=13, occurrence=1)



More information about the Python-checkins mailing list