8000 update tests · python/cpython@a6babab · GitHub
[go: up one dir, main page]

Skip to content

Commit a6babab

Browse files
committed
update tests
1 parent 04b4a84 commit a6babab

File tree

4 files changed

+26
-65
lines changed

4 files changed

+26
-65
lines changed

Lib/test/test_ast/test_ast.py

Lines changed: 19 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -147,18 +147,17 @@ def test_optimization_levels__debug__(self):
147147
self.assertEqual(res.body[0].value.id, expected)
148148

149149
def test_optimization_levels_const_folding(self):
150-
folded = ('Expr', (1, 0, 1, 5), ('Constant', (1, 0, 1, 5), 3, None))
151-
not_folded = ('Expr', (1, 0, 1, 5),
152-
('BinOp', (1, 0, 1, 5),
153-
('Constant', (1, 0, 1, 1), 1, None),
154-
('Add',),
155-
('Constant', (1, 4, 1, 5), 2, None)))
150+
folded = ('Expr', (1, 0, 1, 6), ('Constant', (1, 0, 1, 6), (1, 2), None))
151+
not_folded = ('Expr', (1, 0, 1, 6),
152+
('Tuple', (1, 0, 1, 6),
153+
[('Constant', (1, 1, 1, 2), 1, None),
154+
('Constant', (1, 4, 1, 5), 2, None)], ('Load',)))
156155

157156
cases = [(-1, not_folded), (0, not_folded), (1, folded), (2, folded)]
158157
for (optval, expected) in cases:
159158
with self.subTest(optval=optval):
160-
tree1 = ast.parse("1 + 2", optimize=optval)
161-
tree2 = ast.parse(ast.parse("1 + 2"), optimize=optval)
159+
tree1 = ast.parse("(1, 2)", optimize=optval)
160+
tree2 = ast.parse(ast.parse("(1, 2)"), optimize=optval)
162161
for tree in [tree1, tree2]:
163162
res = to_tuple(tree.body[0])
164163
self.assertEqual(res, expected)
@@ -3134,32 +3133,6 @@ def assert_ast(self, code, non_optimized_target, optimized_target):
31343133
f"{ast.dump(optimized_tree)}",
31353134
)
31363135

3137-
def create_binop(self, operand, left=ast.Constant(1), right=ast.Constant(1)):
3138-
return ast.BinOp(left=left, op=self.binop[operand], right=right)
3139-
3140-
def test_folding_binop(self):
3141-
code = "1 %s 1"
3142-
operators = self.binop.keys()
3143-
3144-
for op in operators:
3145-
result_code = code % op
3146-
non_optimized_target = self.wrap_expr(self.create_binop(op))
3147-
optimized_target = self.wrap_expr(ast.Constant(value=eval(result_code)))
3148-
3149-
with self.subTest(
3150-
result_code=result_code,
3151-
non_optimized_target=non_optimized_target,
3152-
optimized_target=optimized_target
3153-
):
3154-
self.assert_ast(result_code, non_optimized_target, optimized_target)
3155-
3156-
# Multiplication of constant tuples must be folded
3157-
code = "(1,) * 3"
3158-
non_optimized_target = self.wrap_expr(self.create_binop("*", ast.Tuple(elts=[ast.Constant(value=1)]), ast.Constant(value=3)))
3159-
optimized_target = self.wrap_expr(ast.Constant(eval(code)))
3160-
3161-
self.assert_ast(code, non_optimized_target, optimized_target)
3162-
31633136
def test_folding_unaryop(self):
31643137
code = "%s1"
31653138
operators = self.unaryop.keys()
@@ -3240,9 +3213,9 @@ def test_folding_tuple(self):
32403213
self.assert_ast(code, non_optimized_target, optimized_target)
32413214

32423215
def test_folding_type_param_in_function_def(self):
3243-
code = "def foo[%s = 1 + 1](): pass"
3216+
code = "def foo[%s = (1, 2)](): pass"
32443217

3245-
unoptimized_binop = self.create_binop("+")
3218+
unoptimized_tuple = ast.Tuple(elts=[ast.Constant(1), ast.Constant(2)])
32463219
unoptimized_type_params = [
32473220
("T", "T", ast.TypeVar),
32483221
("**P", "P", ast.ParamSpec),
@@ -3256,23 +3229,23 @@ def test_folding_type_param_in_function_def(self):
32563229
name='foo',
32573230
args=ast.arguments(),
32583231
body=[ast.Pass()],
3259-
type_params=[type_param(name=name, default_value=ast.Constant(2))]
3232+
type_params=[type_param(name=name, default_value=ast.Constant((1, 2)))]
32603233
)
32613234
)
32623235
non_optimized_target = self.wrap_statement(
32633236
ast.FunctionDef(
32643237
name='foo',
32653238
args=ast.arguments(),
32663239
body=[ast.Pass()],
3267-
type_params=[type_param(name=name, default_value=unoptimized_binop)]
3240+
type_params=[type_param(name=name, default_value=unoptimized_tuple)]
32683241
)
32693242
)
32703243
self.assert_ast(result_code, non_optimized_target, optimized_target)
32713244

32723245
def test_folding_type_param_in_class_def(self):
3273-
code = "class foo[%s = 1 + 1]: pass"
3246+
code = "class foo[%s = (1, 2)]: pass"
32743247

3275-
unoptimized_binop = self.create_binop("+")
3248+
unoptimized_tuple = ast.Tuple(elts=[ast.Constant(1), ast.Constant(2)])
32763249
unoptimized_type_params = [
32773250
("T", "T", ast.TypeVar),
32783251
("**P", "P", ast.ParamSpec),
@@ -3285,22 +3258,22 @@ def test_folding_type_param_in_class_def(self):
32853258
ast.ClassDef(
32863259
name='foo',
32873260
body=[ast.Pass()],
3288-
type_params=[type_param(name=name, default_value=ast.Constant(2))]
3261+
type_params=[type_param(name=name, default_value=ast.Constant((1, 2)))]
32893262
)
32903263
)
32913264
non_optimized_target = self.wrap_statement(
32923265
ast.ClassDef(
32933266
name='foo',
32943267
body=[ast.Pass()],
3295-
type_params=[type_param(name=name, default_value=unoptimized_binop)]
3268+
type_params=[type_param(name=name, default_value=unoptimized_tuple)]
32963269
)
32973270
)
32983271
self.assert_ast(result_code, non_optimized_target, optimized_target)
32993272

33003273
def test_folding_type_param_in_type_alias(self):
3301-
code = "type foo[%s = 1 + 1] = 1"
3274+
code = "type foo[%s = (1, 2)] = 1"
33023275

3303-
unoptimized_binop = self.create_binop("+")
3276+
unoptimized_tuple = ast.Tuple(elts=[ast.Constant(1), ast.Constant(2)])
33043277
unoptimized_type_params = [
33053278
("T", "T", ast.TypeVar),
33063279
("**P", "P", ast.ParamSpec),
@@ -3312,14 +3285,14 @@ def test_folding_type_param_in_type_alias(self):
33123285
optimized_target = self.wrap_statement(
33133286
ast.TypeAlias(
33143287
name=ast.Name(id='foo', ctx=ast.Store()),
3315-
type_params=[type_param(name=name, default_value=ast.Constant(2))],
3288+
type_params=[type_param(name=name, default_value=ast.Constant((1, 2)))],
33163289
value=ast.Constant(value=1),
33173290
)
33183291
)
33193292
non_optimized_target = self.wrap_statement(
33203293
ast.TypeAlias(
33213294
name=ast.Name(id='foo', ctx=ast.Store()),
3322-
type_params=[type_param(name=name, default_value=unoptimized_binop)],
3295+
type_params=[type_param(name=name, default_value=unoptimized_tuple)],
33233296
value=ast.Constant(value=1),
33243297
)
33253298
)

Lib/test/test_ast/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
def to_tuple(t):
2-
if t is None or isinstance(t, (str, int, complex< 10000 /span>, float, bytes)) or t is Ellipsis:
2+
if t is None or isinstance(t, (str, int, complex, float, bytes, tuple)) or t is Ellipsis:
33
return t
44
elif isinstance(t, list):
55
return [to_tuple(e) for e in t]

Lib/test/test_builtin.py

Lines changed: 6 additions & 9 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*(1,2)", "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,17 +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 BinOp(1, '+', 2)
570-
self.assertIsInstance(raw_right, ast.BinOp)
571-
self.assertIsInstance(raw_right.left, ast.Constant)
572-
self.assertEqual(raw_right.left.value, 1)
573-
self.assertIsInstance(raw_right.right, ast.Constant)
574-
self.assertEqual(raw_right.right.value, 2)
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])
575572

576573
for opt in [opt1, opt2]:
577-
opt_right = opt.value.right # expect Constant(3)
574+
opt_right = opt.value.right # expect Constant((1,2))
578575
self.assertIsInstance(opt_right, ast.Constant)
579-
self.assertEqual(opt_right.value, 3)
576+
self.assertEqual(opt_right.value, (1, 2))
580577

581578
def test_delattr(self):
582579
sys.spam = 1

Lib/test/test_peepholer.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,6 @@ def test_folding_of_unaryops_on_constants(self):
311311
for line, elem in (
312312
('-0.5', -0.5), # unary negative
313313
('-0.0', -0.0), # -0.0
314-
('-(1.0-1.0)', -0.0), # -0.0 after folding
315314
('-0', 0), # -0
316315
('~-2', 1), # unary invert
317316
('+1', 1), # unary positive
@@ -326,14 +325,6 @@ def test_folding_of_unaryops_on_constants(self):
326325
self.assertFalse(instr.opname.startswith('UNARY_'))
327326
self.check_lnotab(code)
328327

329-
# Check that -0.0 works after marshaling
330-
def negzero():
331-
return -(1.0-1.0)
332-
333-
for instr in dis.get_instructions(negzero):
334-
self.assertFalse(instr.opname.startswith('UNARY_'))
335-
self.check_lnotab(negzero)
336-
337328
# Verify that unfoldables are skipped
338329
for line, elem, opname in (
339330
('-"abc"', 'abc', 'UNARY_NEGATIVE'),

0 commit comments

Comments
 (0)
0