script = """def func():\n""" + i * snippet
if async_:
script = "async " + script
- code = compile(script, "<script>", "exec")
+ with warnings.catch_warnings():
+ warnings.simplefilter('ignore', SyntaxWarning)
+ code = compile(script, "<script>", "exec")
exec(code, ns, ns)
return ns['func'].__code__
try:
yield str(arg)
finally:
- return 'Done'
+ pass
+ return 'Done'
@classmethod_friendly_decorator
@classmethod
try:
yield str(arg)
finally:
- return 'Done'
+ pass
+ return 'Done'
@functools.singledispatchmethod
@classmethod_friendly_decorator
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)
+ with warnings.catch_warnings():
+ warnings.simplefilter('ignore', SyntaxWarning)
- count = 0
- while count < 2:
- count += 1
- try:
- continue
- finally:
- break
- self.assertEqual(count, 1)
+ count = 0
+ while count < 2:
+ count += 1
+ try:
+ pass
+ finally:
+ break
+ self.assertEqual(count, 1)
- count = 0
- while count < 2:
- count += 1
- try:
- 1/0
- finally:
- break
- self.assertEqual(count, 1)
+ count = 0
+ while count < 2:
+ count += 1
+ try:
+ continue
+ finally:
+ break
+ self.assertEqual(count, 1)
- for count in [0, 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)
- try:
- pass
- finally:
- break
- self.assertEqual(count, 0)
- for count in [0, 1]:
+ for count in [0, 1]:
+ self.assertEqual(count, 0)
+ try:
+ continue
+ finally:
+ break
self.assertEqual(count, 0)
- try:
- continue
- finally:
- break
- self.assertEqual(count, 0)
- for count in [0, 1]:
+ for count in [0, 1]:
+ self.assertEqual(count, 0)
+ try:
+ 1/0
+ finally:
+ break
self.assertEqual(count, 0)
- try:
- 1/0
- finally:
- break
- self.assertEqual(count, 0)
def test_continue_in_finally(self):
count = 0
def func():
try:
- 2/0
- except IndexError:
- 4
- finally:
- return 6
+ try:
+ 2/0
+ except IndexError:
+ 5
+ finally:
+ 7
+ except:
+ pass
+ return 10
self.run_and_compare(func,
[(0, 'call'),
(1, 'line'),
(2, 'line'),
- (2, 'exception'),
(3, 'line'),
- (6, 'line'),
- (6, 'return')])
+ (3, 'exception'),
+ (4, 'line'),
+ (7, 'line'),
+ (8, 'line'),
+ (9, 'line'),
+ (10, 'line'),
+ (10, 'return')])
def test_finally_with_conditional(self):
output.append(11)
output.append(12)
- @jump_test(5, 11, [2, 4], (ValueError, 'comes after the current code block'))
- def test_no_jump_over_return_try_finally_in_finally_block(output):
- try:
- output.append(2)
- finally:
- output.append(4)
- output.append(5)
- return
- try:
- output.append(8)
- finally:
- output.append(10)
- pass
- output.append(12)
-
@jump_test(3, 4, [1], (ValueError, 'after'))
def test_no_jump_infinite_while_loop(output):
output.append(1)
finally:
output.append(4)
output.append(5)
- return
+ return
output.append(7)
@jump_test(7, 4, [1, 6], (ValueError, 'into'))
import pathlib
import random
import tokenize
+import warnings
import ast
from test.support.ast_helper import ASTTestMixin
return items
def test_files(self):
- for item in self.files_to_test():
- if test.support.verbose:
- print(f"Testing {item.absolute()}")
+ with warnings.catch_warnings():
+ warnings.simplefilter('ignore', SyntaxWarning)
- with self.subTest(filename=item):
- source = read_pyfile(item)
- self.check_ast_roundtrip(source)
+ for item in self.files_to_test():
+ if test.support.verbose:
+ print(f"Testing {item.absolute()}")
+
+ with self.subTest(filename=item):
+ source = read_pyfile(item)
+ self.check_ast_roundtrip(source)
if __name__ == "__main__":
try:
yield yielded_first
yield yielded_second
- finally:
- return returned
+ except:
+ pass
+ return returned
def outer():
return (yield from inner())