diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2018-01-02 08:56:40 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-02 08:56:40 (GMT) |
commit | b495377a8bdd711e9aab0885c60cd148284156e8 (patch) | |
tree | 8bac640cfae6479bdccf5908229004b3e8a75c23 | |
parent | 2de47ca109d0418c7c01e451b5614f748ad76ee9 (diff) | |
download | cpython-b495377a8bdd711e9aab0885c60cd148284156e8.zip cpython-b495377a8bdd711e9aab0885c60cd148284156e8.tar.gz cpython-b495377a8bdd711e9aab0885c60cd148284156e8.tar.bz2 |
[2.7] bpo-32478: Add tests for 'break' and 'return' inside 'finally' clause. (GH-5078). (#5084)
(cherry picked from commit 7cc42c356b0dc5ad9eaa9392789e84bd4aa1c7de)
-rw-r--r-- | Lib/test/test_grammar.py | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/Lib/test/test_grammar.py b/Lib/test/test_grammar.py index fc675c3..47b6b3e 100644 --- a/Lib/test/test_grammar.py +++ b/Lib/test/test_grammar.py @@ -490,6 +490,80 @@ hello world x = g2() check_syntax_error(self, "class foo:return 1") + def test_break_in_finally(self): + count = 0 + while count < 2: + count += 1 + try: + pass + finally: + break + self.assertEqual(count, 1) + + count = 0 + while count < 2: + count += 1 + try: + continue + finally: + break + self.assertEqual(count, 1) + + count = 0 + while count < 2: + count += 1 + try: + 1/0 + finally: + break + self.assertEqual(count, 1) + + for count in [0, 1]: + self.assertEqual(count, 0) + try: + pass + finally: + break + self.assertEqual(count, 0) + + for count in [0, 1]: + self.assertEqual(count, 0) + try: + continue + finally: + break + self.assertEqual(count, 0) + + for count in [0, 1]: + self.assertEqual(count, 0) + try: + 1/0 + finally: + break + self.assertEqual(count, 0) + + def test_return_in_finally(self): + def g1(): + try: + pass + finally: + return 1 + self.assertEqual(g1(), 1) + + def g2(): + try: + return 2 + finally: + return 3 + self.assertEqual(g2(), 3) + + def g3(): + try: + 1/0 + finally: + return 4 + self.assertEqual(g3(), 4) + def testYield(self): check_syntax_error(self, "class foo:yield 1") |