Skip to content

Commit 6f9e00a

Browse files
committed
adjust tests
1 parent a0b5616 commit 6f9e00a

File tree

4 files changed

+14
-121
lines changed

4 files changed

+14
-121
lines changed

Lib/test/test_ast/test_ast.py

Lines changed: 0 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -153,22 +153,6 @@ def test_optimization_levels__debug__(self):
153153
self.assertIsInstance(res.body[0].value, ast.Name)
154154
self.assertEqual(res.body[0].value.id, expected)
155155

156-
def test_optimization_levels_const_folding(self):
157-
folded = ('Expr', (1, 0, 1, 6), ('Constant', (1, 0, 1, 6), (1, 2), None))
158-
not_folded = ('Expr', (1, 0, 1, 6),
159-
('Tuple', (1, 0, 1, 6),
160-
[('Constant', (1, 1, 1, 2), 1, None),
161-
('Constant', (1, 4, 1, 5), 2, None)], ('Load',)))
162-
163-
cases = [(-1, not_folded), (0, not_folded), (1, folded), (2, folded)]
164-
for (optval, expected) in cases:
165-
with self.subTest(optval=optval):
166-
tree1 = ast.parse("(1, 2)", optimize=optval)
167-
tree2 = ast.parse(ast.parse("(1, 2)"), optimize=optval)
168-
for tree in [tree1, tree2]:
169-
res = to_tuple(tree.body[0])
170-
self.assertEqual(res, expected)
171-
172156
def test_invalid_position_information(self):
173157
invalid_linenos = [
174158
(10, 1), (-10, -11), (10, -11), (-5, -2), (-5, 1)
@@ -3138,101 +3122,6 @@ def test_folding_format(self):
31383122

31393123
self.assert_ast(code, non_optimized_target, optimized_target)
31403124

3141-
3142-
def test_folding_tuple(self):
3143-
code = "(1,)"
3144-
3145-
non_optimized_target = self.wrap_expr(ast.Tuple(elts=[ast.Constant(1)]))
3146-
optimized_target = self.wrap_expr(ast.Constant(value=(1,)))
3147-
3148-
self.assert_ast(code, non_optimized_target, optimized_target)
3149-
3150-
def test_folding_type_param_in_function_def(self):
3151-
code = "def foo[%s = (1, 2)](): pass"
3152-
3153-
unoptimized_tuple = ast.Tuple(elts=[ast.Constant(1), ast.Constant(2)])
3154-
unoptimized_type_params = [
3155-
("T", "T", ast.TypeVar),
3156-
("**P", "P", ast.ParamSpec),
3157-
("*Ts", "Ts", ast.TypeVarTuple),
3158-
]
3159-
3160-
for type, name, type_param in unoptimized_type_params:
3161-
result_code = code % type
3162-
optimized_target = self.wrap_statement(
3163-
ast.FunctionDef(
3164-
name='foo',
3165-
args=ast.arguments(),
3166-
body=[ast.Pass()],
3167-
type_params=[type_param(name=name, default_value=ast.Constant((1, 2)))]
3168-
)
3169-
)
3170-
non_optimized_target = self.wrap_statement(
3171-
ast.FunctionDef(
3172-
name='foo',
3173-
args=ast.arguments(),
3174-
body=[ast.Pass()],
3175-
type_params=[type_param(name=name, default_value=unoptimized_tuple)]
3176-
)
3177-
)
3178-
self.assert_ast(result_code, non_optimized_target, optimized_target)
3179-
3180-
def test_folding_type_param_in_class_def(self):
3181-
code = "class foo[%s = (1, 2)]: pass"
3182-
3183-
unoptimized_tuple = ast.Tuple(elts=[ast.Constant(1), ast.Constant(2)])
3184-
unoptimized_type_params = [
3185-
("T", "T", ast.TypeVar),
3186-
("**P", "P", ast.ParamSpec),
3187-
("*Ts", "Ts", ast.TypeVarTuple),
3188-
]
3189-
3190-
for type, name, type_param in unoptimized_type_params:
3191-
result_code = code % type
3192-
optimized_target = self.wrap_statement(
3193-
ast.ClassDef(
3194-
name='foo',
3195-
body=[ast.Pass()],
3196-
type_params=[type_param(name=name, default_value=ast.Constant((1, 2)))]
3197-
)
3198-
)
3199-
non_optimized_target = self.wrap_statement(
3200-
ast.ClassDef(
3201-
name='foo',
3202-
body=[ast.Pass()],
3203-
type_params=[type_param(name=name, default_value=unoptimized_tuple)]
3204-
)
3205-
)
3206-
self.assert_ast(result_code, non_optimized_target, optimized_target)
3207-
3208-
def test_folding_type_param_in_type_alias(self):
3209-
code = "type foo[%s = (1, 2)] = 1"
3210-
3211-
unoptimized_tuple = ast.Tuple(elts=[ast.Constant(1), ast.Constant(2)])
3212-
unoptimized_type_params = [
3213-
("T", "T", ast.TypeVar),
3214-
("**P", "P", ast.ParamSpec),
3215-
("*Ts", "Ts", ast.TypeVarTuple),
3216-
]
3217-
3218-
for type, name, type_param in unoptimized_type_params:
3219-
result_code = code % type
3220-
optimized_target = self.wrap_statement(
3221-
ast.TypeAlias(
3222-
name=ast.Name(id='foo', ctx=ast.Store()),
3223-
type_params=[type_param(name=name, default_value=ast.Constant((1, 2)))],
3224-
value=ast.Constant(value=1),
3225-
)
3226-
)
3227-
non_optimized_target = self.wrap_statement(
3228-
ast.TypeAlias(
3229-
name=ast.Name(id='foo', ctx=ast.Store()),
3230-
type_params=[type_param(name=name, default_value=unoptimized_tuple)],
3231-
value=ast.Constant(value=1),
3232-
)
3233-
)
3234-
self.assert_ast(result_code, non_optimized_target, optimized_target)
3235-
32363125
def test_folding_match_case_allowed_expressions(self):
32373126
def get_match_case_values(node):
32383127
result = []

Lib/test/test_builtin.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ def test_compile_async_generator(self):
555555
self.assertEqual(type(glob['ticker']()), AsyncGeneratorType)
556556

557557
def test_compile_ast(self):
558-
args = ("a*(1,2)", "f.py", "exec")
558+
args = ("a*__debug__", "f.py", "exec")
559559
raw = compile(*args, flags = ast.PyCF_ONLY_AST).body[0]
560560
opt1 = compile(*args, flags = ast.PyCF_OPTIMIZED_AST).body[0]
561561
opt2 = compile(ast.parse(args[0]), *args[1:], flags = ast.PyCF_OPTIMIZED_AST).body[0]
@@ -566,14 +566,14 @@ def test_compile_ast(self):
566566
self.assertIsInstance(tree.value.left, ast.Name)
567567
self.assertEqual(tree.value.left.id, 'a')
568568

569-
raw_right = raw.value.right # expect Tuple((1, 2))
570-
self.assertIsInstance(raw_right, ast.Tuple)
571-
self.assertListEqual([elt.value for elt in raw_right.elts], [1, 2])
569+
raw_right = raw.value.right
570+
self.assertIsInstance(raw_right, ast.Name)
571+
self.assertEqual(raw_right.id, "__debug__")
572572

573573
for opt in [opt1, opt2]:
574-
opt_right = opt.value.right # expect Constant((1,2))
574+
opt_right = opt.value.right
575575
self.assertIsInstance(opt_right, ast.Constant)
576-
self.assertEqual(opt_right.value, (1, 2))
576+
self.assertEqual(opt_right.value, True)
577577

578578
def test_delattr(self):
579579
sys.spam = 1

Lib/test/test_compile.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -794,9 +794,9 @@ def check_same_constant(const):
794794
f1, f2 = lambda: "not a name", lambda: ("not a name",)
795795
f3 = lambda x: x in {("not a name",)}
796796
self.assertIs(f1.__code__.co_consts[0],
797-
f2.__code__.co_consts[0][0])
797+
f2.__code__.co_consts[1][0])
798798
self.assertIs(next(iter(f3.__code__.co_consts[1])),
799-
f2.__code__.co_consts[0])
799+
f2.__code__.co_consts[1])
800800

801801
# {0} is converted to a constant frozenset({0}) by the peephole
802802
# optimizer

Lib/test/test_opcache.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,7 +1666,8 @@ def to_bool_str():
16661666
def test_unpack_sequence(self):
16671667
def unpack_sequence_two_tuple():
16681668
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
1669-
a, b = 1, 2
1669+
t = 1, 2
1670+
a, b = t
16701671
self.assertEqual(a, 1)
16711672
self.assertEqual(b, 2)
16721673

@@ -1677,8 +1678,11 @@ def unpack_sequence_two_tuple():
16771678

16781679
def unpack_sequence_tuple():
16791680
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
1680-
a, = 1,
1681+
a, b, c, d = 1, 2, 3, 4
16811682
self.assertEqual(a, 1)
1683+
self.assertEqual(b, 2)
1684+
self.assertEqual(c, 3)
1685+
self.assertEqual(d, 4)
16821686

16831687
unpack_sequence_tuple()
16841688
self.assert_specialized(unpack_sequence_tuple, "UNPACK_SEQUENCE_TUPLE")

0 commit comments

Comments
 (0)