Skip to content

Commit 83479c2

Browse files
authored
gh-130080: fix warnings in tests (#131400)
1 parent 51d3099 commit 83479c2

File tree

6 files changed

+82
-77
lines changed

6 files changed

+82
-77
lines changed

Lib/test/test_compile.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2415,7 +2415,9 @@ def compile_snippet(i):
24152415
script = """def func():\n""" + i * snippet
24162416
if async_:
24172417
script = "async " + script
2418-
code = compile(script, "<script>", "exec")
2418+
with warnings.catch_warnings():
2419+
warnings.simplefilter('ignore', SyntaxWarning)
2420+
code = compile(script, "<script>", "exec")
24192421
exec(code, ns, ns)
24202422
return ns['func'].__code__
24212423

Lib/test/test_functools.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3011,7 +3011,8 @@ def cls_context_manager(cls, arg: int) -> str:
30113011
try:
30123012
yield str(arg)
30133013
finally:
3014-
return 'Done'
3014+
pass
3015+
return 'Done'
30153016

30163017
@classmethod_friendly_decorator
30173018
@classmethod
@@ -3027,7 +3028,8 @@ def cls_context_manager(cls, arg: int) -> str:
30273028
try:
30283029
yield str(arg)
30293030
finally:
3030-
return 'Done'
3031+
pass
3032+
return 'Done'
30313033

30323034
@functools.singledispatchmethod
30333035
@classmethod_friendly_decorator

Lib/test/test_grammar.py

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -917,56 +917,59 @@ def g3():
917917
check_syntax_error(self, "class foo:return 1")
918918

919919
def test_break_in_finally(self):
920-
count = 0
921-
while count < 2:
922-
count += 1
923-
try:
924-
pass
925-
finally:
926-
break
927-
self.assertEqual(count, 1)
920+
with warnings.catch_warnings():
921+
warnings.simplefilter('ignore', SyntaxWarning)
928922

929-
count = 0
930-
while count < 2:
931-
count += 1
932-
try:
933-
continue
934-
finally:
935-
break
936-
self.assertEqual(count, 1)
923+
count = 0
924+
while count < 2:
925+
count += 1
926+
try:
927+
pass
928+
finally:
929+
break
930+
self.assertEqual(count, 1)
937931

938-
count = 0
939-
while count < 2:
940-
count += 1
941-
try:
942-
1/0
943-
finally:
944-
break
945-
self.assertEqual(count, 1)
932+
count = 0
933+
while count < 2:
934+
count += 1
935+
try:
936+
continue
937+
finally:
938+
break
939+
self.assertEqual(count, 1)
946940

947-
for count in [0, 1]:
941+
count = 0
942+
while count < 2:
943+
count += 1
944+
try:
945+
1/0
946+
finally:
947+
break
948+
self.assertEqual(count, 1)
949+
950+
for count in [0, 1]:
951+
self.assertEqual(count, 0)
952+
try:
953+
pass
954+
finally:
955+
break
948956
self.assertEqual(count, 0)
949-
try:
950-
pass
951-
finally:
952-
break
953-
self.assertEqual(count, 0)
954957

955-
for count in [0, 1]:
958+
for count in [0, 1]:
959+
self.assertEqual(count, 0)
960+
try:
961+
continue
962+
finally:
963+
break
956964
self.assertEqual(count, 0)
957-
try:
958-
continue
959-
finally:
960-
break
961-
self.assertEqual(count, 0)
962965

963-
for count in [0, 1]:
966+
for count in [0, 1]:
967+
self.assertEqual(count, 0)
968+
try:
969+
1/0
970+
finally:
971+
break
964972
self.assertEqual(count, 0)
965-
try:
966-
1/0
967-
finally:
968-
break
969-
self.assertEqual(count, 0)
970973

971974
def test_continue_in_finally(self):
972975
count = 0

Lib/test/test_sys_settrace.py

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -920,20 +920,28 @@ def test_try_except_with_wrong_type(self):
920920

921921
def func():
922922
try:
923-
2/0
924-
except IndexError:
925-
4
926-
finally:
927-
return 6
923+
try:
924+
2/0
925+
except IndexError:
926+
5
927+
finally:
928+
7
929+
except:
930+
pass
931+
return 10
928932

929933
self.run_and_compare(func,
930934
[(0, 'call'),
931935
(1, 'line'),
932936
(2, 'line'),
933-
(2, 'exception'),
934937
(3, 'line'),
935-
(6, 'line'),
936-
(6, 'return')])
938+
(3, 'exception'),
939+
(4, 'line'),
940+
(7, 'line'),
941+
(8, 'line'),
942+
(9, 'line'),
943+
(10, 'line'),
944+
(10, 'return')])
937945

938946
def test_finally_with_conditional(self):
939947

@@ -2228,21 +2236,6 @@ def test_jump_in_nested_finally_3(output):
22282236
output.append(11)
22292237
output.append(12)
22302238

2231-
@jump_test(5, 11, [2, 4], (ValueError, 'comes after the current code block'))
2232-
def test_no_jump_over_return_try_finally_in_finally_block(output):
2233-
try:
2234-
output.append(2)
2235-
finally:
2236-
output.append(4)
2237-
output.append(5)
2238-
return
2239-
try:
2240-
output.append(8)
2241-
finally:
2242-
output.append(10)
2243-
pass
2244-
output.append(12)
2245-
22462239
@jump_test(3, 4, [1], (ValueError, 'after'))
22472240
def test_no_jump_infinite_while_loop(output):
22482241
output.append(1)
@@ -2766,7 +2759,7 @@ def test_no_jump_over_return_out_of_finally_block(output):
27662759
finally:
27672760
output.append(4)
27682761
output.append(5)
2769-
return
2762+
return
27702763
output.append(7)
27712764

27722765
@jump_test(7, 4, [1, 6], (ValueError, 'into'))

Lib/test/test_unparse.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pathlib
66
import random
77
import tokenize
8+
import warnings
89
import ast
910
from test.support.ast_helper import ASTTestMixin
1011

@@ -839,13 +840,16 @@ def files_to_test(cls):
839840
return items
840841

841842
def test_files(self):
842-
for item in self.files_to_test():
843-
if test.support.verbose:
844-
print(f"Testing {item.absolute()}")
843+
with warnings.catch_warnings():
844+
warnings.simplefilter('ignore', SyntaxWarning)
845845

846-
with self.subTest(filename=item):
847-
source = read_pyfile(item)
848-
self.check_ast_roundtrip(source)
846+
for item in self.files_to_test():
847+
if test.support.verbose:
848+
print(f"Testing {item.absolute()}")
849+
850+
with self.subTest(filename=item):
851+
source = read_pyfile(item)
852+
self.check_ast_roundtrip(source)
849853

850854

851855
if __name__ == "__main__":

Lib/test/test_yield_from.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,8 +1520,9 @@ def inner():
15201520
try:
15211521
yield yielded_first
15221522
yield yielded_second
1523-
finally:
1524-
return returned
1523+
except:
1524+
pass
1525+
return returned
15251526

15261527
def outer():
15271528
return (yield from inner())

0 commit comments

Comments
 (0)