mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
Merge remote-tracking branch 'origin/main' into HEAD
This commit is contained in:
commit
8d57aca95a
1284 changed files with 27792 additions and 11927 deletions
|
|
@ -13,6 +13,7 @@
|
|||
import textwrap
|
||||
import types
|
||||
import unittest
|
||||
import warnings
|
||||
import weakref
|
||||
from io import StringIO
|
||||
from pathlib import Path
|
||||
|
|
@ -1057,61 +1058,6 @@ def test_repr_large_input_crash(self):
|
|||
r"Exceeds the limit \(\d+ digits\)"):
|
||||
repr(ast.Constant(value=eval(source)))
|
||||
|
||||
def test_pep_765_warnings(self):
|
||||
srcs = [
|
||||
textwrap.dedent("""
|
||||
def f():
|
||||
try:
|
||||
pass
|
||||
finally:
|
||||
return 42
|
||||
"""),
|
||||
textwrap.dedent("""
|
||||
for x in y:
|
||||
try:
|
||||
pass
|
||||
finally:
|
||||
break
|
||||
"""),
|
||||
textwrap.dedent("""
|
||||
for x in y:
|
||||
try:
|
||||
pass
|
||||
finally:
|
||||
continue
|
||||
"""),
|
||||
]
|
||||
for src in srcs:
|
||||
with self.assertWarnsRegex(SyntaxWarning, 'finally'):
|
||||
ast.parse(src)
|
||||
|
||||
def test_pep_765_no_warnings(self):
|
||||
srcs = [
|
||||
textwrap.dedent("""
|
||||
try:
|
||||
pass
|
||||
finally:
|
||||
def f():
|
||||
return 42
|
||||
"""),
|
||||
textwrap.dedent("""
|
||||
try:
|
||||
pass
|
||||
finally:
|
||||
for x in y:
|
||||
break
|
||||
"""),
|
||||
textwrap.dedent("""
|
||||
try:
|
||||
pass
|
||||
finally:
|
||||
for x in y:
|
||||
continue
|
||||
"""),
|
||||
]
|
||||
for src in srcs:
|
||||
ast.parse(src)
|
||||
|
||||
def test_tstring(self):
|
||||
# Test AST structure for simple t-string
|
||||
tree = ast.parse('t"Hello"')
|
||||
|
|
@ -1124,6 +1070,19 @@ def test_tstring(self):
|
|||
self.assertIsInstance(tree.body[0].value.values[0], ast.Constant)
|
||||
self.assertIsInstance(tree.body[0].value.values[1], ast.Interpolation)
|
||||
|
||||
def test_filter_syntax_warnings_by_module(self):
|
||||
filename = support.findfile('test_import/data/syntax_warnings.py')
|
||||
with open(filename, 'rb') as f:
|
||||
source = f.read()
|
||||
with warnings.catch_warnings(record=True) as wlog:
|
||||
warnings.simplefilter('error')
|
||||
warnings.filterwarnings('always', module=r'<unknown>\z')
|
||||
ast.parse(source)
|
||||
self.assertEqual(sorted(wm.lineno for wm in wlog), [4, 7, 10])
|
||||
for wm in wlog:
|
||||
self.assertEqual(wm.filename, '<unknown>')
|
||||
self.assertIs(wm.category, SyntaxWarning)
|
||||
|
||||
|
||||
class CopyTests(unittest.TestCase):
|
||||
"""Test copying and pickling AST nodes."""
|
||||
|
|
@ -3308,6 +3267,15 @@ class MoreFieldsThanTypes(ast.AST):
|
|||
self.assertEqual(obj.a, 1)
|
||||
self.assertEqual(obj.b, 2)
|
||||
|
||||
def test_malformed_fields_with_bytes(self):
|
||||
class BadFields(ast.AST):
|
||||
_fields = (b'\xff'*64,)
|
||||
_field_types = {'a': int}
|
||||
|
||||
# This should not crash
|
||||
with self.assertWarnsRegex(DeprecationWarning, r"Field b'\\xff\\xff.*' .*"):
|
||||
obj = BadFields()
|
||||
|
||||
def test_complete_field_types(self):
|
||||
class _AllFieldTypes(ast.AST):
|
||||
_fields = ('a', 'b')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue