https://github.com/python/cpython/commit/749e24b48fb6b267f1ad580a6363edbcbbfa3e16
commit: 749e24b48fb6b267f1ad580a6363edbcbbfa3e16
branch: main
author: Irit Katriel <1055913+iritkatr...@users.noreply.github.com>
committer: iritkatriel <1055913+iritkatr...@users.noreply.github.com>
date: 2025-03-20T16:11:04Z
summary:

gh-130080: fix warnings in tests (#131471)

files:
M Lib/test/test_grammar.py

diff --git a/Lib/test/test_grammar.py b/Lib/test/test_grammar.py
index bde0b45b3ceb93..bee2c06e274a96 100644
--- a/Lib/test/test_grammar.py
+++ b/Lib/test/test_grammar.py
@@ -7,6 +7,7 @@
 import inspect
 import unittest
 import sys
+import textwrap
 import warnings
 # testing import *
 from sys import *
@@ -916,189 +917,292 @@ def g3():
         self.assertEqual(y, (1, 2, 3), "unparenthesized star expr return")
         check_syntax_error(self, "class foo:return 1")
 
-    def test_break_in_finally(self):
-        with warnings.catch_warnings():
-            warnings.simplefilter('ignore', SyntaxWarning)
+    def test_control_flow_in_finally(self):
 
-            count = 0
-            while count < 2:
-                count += 1
+        def run_case(self, src, expected):
+            with warnings.catch_warnings():
+                warnings.simplefilter('ignore', SyntaxWarning)
+                g, l = {}, { 'self': self }
+                exec(textwrap.dedent(src), g, l)
+                self.assertEqual(expected, l['result'])
+
+
+        # *********** Break in finally ***********
+
+        run_case(
+            self,
+            """
+                result = 0
+                while result < 2:
+                    result += 1
+                    try:
+                        pass
+                    finally:
+                        break
+            """,
+            1)
+
+        run_case(
+            self,
+            """
+                result = 0
+                while result < 2:
+                    result += 1
+                    try:
+                        continue
+                    finally:
+                        break
+            """,
+            1)
+
+        run_case(
+            self,
+            """
+            result = 0
+            while result < 2:
+                result += 1
+                try:
+                    1/0
+                finally:
+                    break
+            """,
+            1)
+
+        run_case(
+            self,
+            """
+            for result in [0, 1]:
+                self.assertEqual(result, 0)
                 try:
                     pass
                 finally:
                     break
-            self.assertEqual(count, 1)
-
-            count = 0
-            while count < 2:
-                count += 1
+            """,
+            0)
+
+        run_case(
+            self,
+            """
+            for result in [0, 1]:
+                self.assertEqual(result, 0)
                 try:
                     continue
                 finally:
                     break
-            self.assertEqual(count, 1)
-
-            count = 0
-            while count < 2:
-                count += 1
+            """,
+            0)
+
+        run_case(
+            self,
+            """
+            for result in [0, 1]:
+                self.assertEqual(result, 0)
                 try:
                     1/0
                 finally:
                     break
-            self.assertEqual(count, 1)
+            """,
+            0)
 
-            for count in [0, 1]:
-                self.assertEqual(count, 0)
+
+        # *********** Continue in finally ***********
+
+        run_case(
+            self,
+            """
+            result = 0
+            while result < 2:
+                result += 1
                 try:
                     pass
                 finally:
-                    break
-            self.assertEqual(count, 0)
+                    continue
+                break
+            """,
+            2)
+
 
-            for count in [0, 1]:
-                self.assertEqual(count, 0)
+        run_case(
+            self,
+            """
+            result = 0
+            while result < 2:
+                result += 1
                 try:
-                    continue
-                finally:
                     break
-            self.assertEqual(count, 0)
-
-            for count in [0, 1]:
-                self.assertEqual(count, 0)
+                finally:
+                    continue
+            """,
+            2)
+
+        run_case(
+            self,
+            """
+            result = 0
+            while result < 2:
+                result += 1
                 try:
                     1/0
                 finally:
-                    break
-            self.assertEqual(count, 0)
-
-    def test_continue_in_finally(self):
-        count = 0
-        while count < 2:
-            count += 1
-            try:
-                pass
-            finally:
-                continue
-            break
-        self.assertEqual(count, 2)
-
-        count = 0
-        while count < 2:
-            count += 1
-            try:
+                    continue
                 break
-            finally:
-                continue
-        self.assertEqual(count, 2)
+            """,
+            2)
 
-        count = 0
-        while count < 2:
-            count += 1
-            try:
-                1/0
-            finally:
-                continue
-            break
-        self.assertEqual(count, 2)
+        run_case(
+            self,
+            """
+            for result in [0, 1]:
+                try:
+                    pass
+                finally:
+                    continue
+                break
+            """,
+            1)
 
-        for count in [0, 1]:
-            try:
-                pass
-            finally:
-                continue
-            break
-        self.assertEqual(count, 1)
+        run_case(
+            self,
+            """
+            for result in [0, 1]:
+                try:
+                    break
+                finally:
+                    continue
+            """,
+            1)
 
-        for count in [0, 1]:
-            try:
+        run_case(
+            self,
+            """
+            for result in [0, 1]:
+                try:
+                    1/0
+                finally:
+                    continue
                 break
-            finally:
-                continue
-        self.assertEqual(count, 1)
+            """,
+            1)
 
-        for count in [0, 1]:
-            try:
-                1/0
-            finally:
-                continue
-            break
-        self.assertEqual(count, 1)
 
-    def test_return_in_finally(self):
-        def g1():
-            try:
-                pass
-            finally:
-                return 1
-        self.assertEqual(g1(), 1)
+        # *********** Return in finally ***********
 
-        def g2():
-            try:
-                return 2
-            finally:
-                return 3
-        self.assertEqual(g2(), 3)
+        run_case(
+            self,
+            """
+            def f():
+                try:
+                    pass
+                finally:
+                    return 1
+            result = f()
+            """,
+            1)
+
+        run_case(
+            self,
+            """
+            def f():
+                try:
+                    return 2
+                finally:
+                    return 3
+            result = f()
+            """,
+            3)
+
+        run_case(
+            self,
+            """
+            def f():
+                try:
+                    1/0
+                finally:
+                    return 4
+            result = f()
+            """,
+            4)
 
-        def g3():
-            try:
-                1/0
-            finally:
-                return 4
-        self.assertEqual(g3(), 4)
+        # See issue #37830
+        run_case(
+            self,
+            """
+            def break_in_finally_after_return1(x):
+                for count in [0, 1]:
+                    count2 = 0
+                    while count2 < 20:
+                        count2 += 10
+                        try:
+                            return count + count2
+                        finally:
+                            if x:
+                                break
+                return 'end', count, count2
+
+            self.assertEqual(break_in_finally_after_return1(False), 10)
+            self.assertEqual(break_in_finally_after_return1(True), ('end', 1, 
10))
+            result = True
+            """,
+            True)
+
+
+        run_case(
+            self,
+            """
+            def break_in_finally_after_return2(x):
+                for count in [0, 1]:
+                    for count2 in [10, 20]:
+                        try:
+                            return count + count2
+                        finally:
+                            if x:
+                                break
+                return 'end', count, count2
+
+            self.assertEqual(break_in_finally_after_return2(False), 10)
+            self.assertEqual(break_in_finally_after_return2(True), ('end', 1, 
10))
+            result = True
+            """,
+            True)
 
-    def test_break_in_finally_after_return(self):
         # See issue #37830
-        def g1(x):
-            for count in [0, 1]:
-                count2 = 0
-                while count2 < 20:
-                    count2 += 10
+        run_case(
+            self,
+            """
+            def continue_in_finally_after_return1(x):
+                count = 0
+                while count < 100:
+                    count += 1
                     try:
-                        return count + count2
+                        return count
                     finally:
                         if x:
-                            break
-            return 'end', count, count2
-        self.assertEqual(g1(False), 10)
-        self.assertEqual(g1(True), ('end', 1, 10))
-
-        def g2(x):
-            for count in [0, 1]:
-                for count2 in [10, 20]:
+                            continue
+                return 'end', count
+
+            self.assertEqual(continue_in_finally_after_return1(False), 1)
+            self.assertEqual(continue_in_finally_after_return1(True), ('end', 
100))
+            result = True
+            """,
+            True)
+
+        run_case(
+            self,
+            """
+            def continue_in_finally_after_return2(x):
+                for count in [0, 1]:
                     try:
-                        return count + count2
+                        return count
                     finally:
                         if x:
-                            break
-            return 'end', count, count2
-        self.assertEqual(g2(False), 10)
-        self.assertEqual(g2(True), ('end', 1, 10))
-
-    def test_continue_in_finally_after_return(self):
-        # See issue #37830
-        def g1(x):
-            count = 0
-            while count < 100:
-                count += 1
-                try:
-                    return count
-                finally:
-                    if x:
-                        continue
-            return 'end', count
-        self.assertEqual(g1(False), 1)
-        self.assertEqual(g1(True), ('end', 100))
-
-        def g2(x):
-            for count in [0, 1]:
-                try:
-                    return count
-                finally:
-                    if x:
-                        continue
-            return 'end', count
-        self.assertEqual(g2(False), 0)
-        self.assertEqual(g2(True), ('end', 1))
+                            continue
+                return 'end', count
+
+            self.assertEqual(continue_in_finally_after_return2(False), 0)
+            self.assertEqual(continue_in_finally_after_return2(True), ('end', 
1))
+            result = True
+            """,
+            True)
 
     def test_yield(self):
         # Allowed as standalone statement

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: arch...@mail-archive.com

Reply via email to