mirror of
https://github.com/python/cpython.git
synced 2026-01-06 15:32:22 +00:00
gh-130080: fix warnings in tests (#131471)
This commit is contained in:
parent
684a759c20
commit
749e24b48f
1 changed files with 244 additions and 140 deletions
|
|
@ -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
|
||||
try:
|
||||
pass
|
||||
finally:
|
||||
break
|
||||
self.assertEqual(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'])
|
||||
|
||||
count = 0
|
||||
while count < 2:
|
||||
count += 1
|
||||
try:
|
||||
continue
|
||||
finally:
|
||||
break
|
||||
self.assertEqual(count, 1)
|
||||
|
||||
count = 0
|
||||
while count < 2:
|
||||
count += 1
|
||||
# *********** 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
|
||||
self.assertEqual(count, 1)
|
||||
""",
|
||||
1)
|
||||
|
||||
for count in [0, 1]:
|
||||
self.assertEqual(count, 0)
|
||||
run_case(
|
||||
self,
|
||||
"""
|
||||
for result in [0, 1]:
|
||||
self.assertEqual(result, 0)
|
||||
try:
|
||||
pass
|
||||
finally:
|
||||
break
|
||||
self.assertEqual(count, 0)
|
||||
""",
|
||||
0)
|
||||
|
||||
for count in [0, 1]:
|
||||
self.assertEqual(count, 0)
|
||||
run_case(
|
||||
self,
|
||||
"""
|
||||
for result in [0, 1]:
|
||||
self.assertEqual(result, 0)
|
||||
try:
|
||||
continue
|
||||
finally:
|
||||
break
|
||||
self.assertEqual(count, 0)
|
||||
""",
|
||||
0)
|
||||
|
||||
for count in [0, 1]:
|
||||
self.assertEqual(count, 0)
|
||||
run_case(
|
||||
self,
|
||||
"""
|
||||
for result in [0, 1]:
|
||||
self.assertEqual(result, 0)
|
||||
try:
|
||||
1/0
|
||||
finally:
|
||||
break
|
||||
self.assertEqual(count, 0)
|
||||
""",
|
||||
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 in finally ***********
|
||||
|
||||
run_case(
|
||||
self,
|
||||
"""
|
||||
result = 0
|
||||
while result < 2:
|
||||
result += 1
|
||||
try:
|
||||
pass
|
||||
finally:
|
||||
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)
|
||||
|
||||
for count in [0, 1]:
|
||||
try:
|
||||
pass
|
||||
finally:
|
||||
continue
|
||||
break
|
||||
self.assertEqual(count, 1)
|
||||
run_case(
|
||||
self,
|
||||
"""
|
||||
result = 0
|
||||
while result < 2:
|
||||
result += 1
|
||||
try:
|
||||
break
|
||||
finally:
|
||||
continue
|
||||
""",
|
||||
2)
|
||||
|
||||
for count in [0, 1]:
|
||||
try:
|
||||
run_case(
|
||||
self,
|
||||
"""
|
||||
result = 0
|
||||
while result < 2:
|
||||
result += 1
|
||||
try:
|
||||
1/0
|
||||
finally:
|
||||
continue
|
||||
break
|
||||
finally:
|
||||
continue
|
||||
self.assertEqual(count, 1)
|
||||
""",
|
||||
2)
|
||||
|
||||
for count in [0, 1]:
|
||||
try:
|
||||
1/0
|
||||
finally:
|
||||
continue
|
||||
break
|
||||
self.assertEqual(count, 1)
|
||||
run_case(
|
||||
self,
|
||||
"""
|
||||
for result in [0, 1]:
|
||||
try:
|
||||
pass
|
||||
finally:
|
||||
continue
|
||||
break
|
||||
""",
|
||||
1)
|
||||
|
||||
def test_return_in_finally(self):
|
||||
def g1():
|
||||
try:
|
||||
pass
|
||||
finally:
|
||||
return 1
|
||||
self.assertEqual(g1(), 1)
|
||||
run_case(
|
||||
self,
|
||||
"""
|
||||
for result in [0, 1]:
|
||||
try:
|
||||
break
|
||||
finally:
|
||||
continue
|
||||
""",
|
||||
1)
|
||||
|
||||
def g2():
|
||||
try:
|
||||
return 2
|
||||
finally:
|
||||
return 3
|
||||
self.assertEqual(g2(), 3)
|
||||
run_case(
|
||||
self,
|
||||
"""
|
||||
for result in [0, 1]:
|
||||
try:
|
||||
1/0
|
||||
finally:
|
||||
continue
|
||||
break
|
||||
""",
|
||||
1)
|
||||
|
||||
def g3():
|
||||
try:
|
||||
1/0
|
||||
finally:
|
||||
return 4
|
||||
self.assertEqual(g3(), 4)
|
||||
|
||||
def test_break_in_finally_after_return(self):
|
||||
# *********** Return in finally ***********
|
||||
|
||||
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)
|
||||
|
||||
# See issue #37830
|
||||
def g1(x):
|
||||
for count in [0, 1]:
|
||||
count2 = 0
|
||||
while count2 < 20:
|
||||
count2 += 10
|
||||
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)
|
||||
|
||||
# See issue #37830
|
||||
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))
|
||||
continue
|
||||
return 'end', count
|
||||
|
||||
def g2(x):
|
||||
for count in [0, 1]:
|
||||
for count2 in [10, 20]:
|
||||
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))
|
||||
continue
|
||||
return 'end', count
|
||||
|
||||
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))
|
||||
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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue