Skip to content

Commit 6d93343

Browse files
committed
Tweak tests
1 parent 7a96d47 commit 6d93343

File tree

4 files changed

+13
-103
lines changed

4 files changed

+13
-103
lines changed

Lib/test/test_ast/test_ast.py

Lines changed: 5 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3153,14 +3153,6 @@ def test_folding_binop(self):
31533153
):
31543154
self.assert_ast(result_code, non_optimized_target, optimized_target)
31553155

3156-
# Tuple folding is currently disabled in the AST optimizer
3157-
# Multiplication of constant tuples must be folded
3158-
# code = "(1,) * 3"
3159-
# non_optimized_target = self.wrap_expr(self.create_binop("*", ast.Tuple(elts=[ast.Constant(value=1)]), ast.Constant(value=3)))
3160-
# optimized_target = self.wrap_expr(ast.Constant(eval(code)))
3161-
3162-
# self.assert_ast(code, non_optimized_target, optimized_target)
3163-
31643156
def test_folding_unaryop(self):
31653157
code = "%s1"
31663158
operators = self.unaryop.keys()
@@ -3180,39 +3172,6 @@ def create_unaryop(operand):
31803172
):
31813173
self.assert_ast(result_code, non_optimized_target, optimized_target)
31823174

3183-
@unittest.skip("Tuple folding is currently disabled in the AST optimizer")
3184-
def test_folding_not(self):
3185-
code = "not (1 %s (1,))"
3186-
operators = {
3187-
"in": ast.In(),
3188-
"is": ast.Is(),
3189-
}
3190-
opt_operators = {
3191-
"is": ast.IsNot(),
3192-
"in": ast.NotIn(),
3193-
}
3194-
3195-
def create_notop(operand):
3196-
return ast.UnaryOp(op=ast.Not(), operand=ast.Compare(
3197-
left=ast.Constant(value=1),
3198-
ops=[operators[operand]],
3199-
comparators=[ast.Tuple(elts=[ast.Constant(value=1)])]
3200-
))
3201-
3202-
for op in operators.keys():
3203-
result_code = code % op
3204-
non_optimized_target = self.wrap_expr(create_notop(op))
3205-
optimized_target = self.wrap_expr(
3206-
ast.Compare(left=ast.Constant(1), ops=[opt_operators[op]], comparators=[ast.Constant(value=(1,))])
3207-
)
3208-
3209-
with self.subTest(
3210-
result_code=result_code,
3211-
non_optimized_target=non_optimized_target,
3212-
optimized_target=optimized_target
3213-
):
3214-
self.assert_ast(result_code, non_optimized_target, optimized_target)
3215-
32163175
def test_folding_format(self):
32173176
code = "'%s' % (a,)"
32183177

@@ -3232,15 +3191,6 @@ def test_folding_format(self):
32323191

32333192
self.assert_ast(code, non_optimized_target, optimized_target)
32343193

3235-
@unittest.skip("Tuple folding is currently disabled in the AST optimizer")
3236-
def test_folding_tuple(self):
3237-
code = "(1,)"
3238-
3239-
non_optimized_target = self.wrap_expr(ast.Tuple(elts=[ast.Constant(1)]))
3240-
optimized_target = self.wrap_expr(ast.Constant(value=(1,)))
3241-
3242-
self.assert_ast(code, non_optimized_target, optimized_target)
3243-
32443194
def test_folding_comparator(self):
32453195
code = "1 %s %s1%s"
32463196
operators = [("in", ast.In()), ("not in", ast.NotIn())]
@@ -3281,15 +3231,15 @@ def test_folding_iter(self):
32813231

32823232
self.assert_ast(code % (left, right), non_optimized_target, optimized_target)
32833233

3284-
@unittest.skip("Tuple folding is currently disabled in the AST optimizer")
32853234
def test_folding_subscript(self):
3286-
code = "(1,)[0]"
3235+
code = "'abcd'[0]"
32873236

32883237
non_optimized_target = self.wrap_expr(
3289-
ast.Subscript(value=ast.Tuple(elts=[ast.Constant(value=1)]), slice=ast.Constant(value=0))
3238+
ast.Subscript(value=ast.Constant(value='abcd'), slice=ast.Constant(value=0))
3239+
)
3240+
optimized_target = self.wrap_expr(
3241+
ast.JoinedStr(values=[ast.Constant(value='a')])
32903242
)
3291-
optimized_target = self.wrap_expr(ast.Constant(value=1))
3292-
32933243
self.assert_ast(code, non_optimized_target, optimized_target)
32943244

32953245
def test_folding_type_param_in_function_def(self):

Lib/test/test_compile.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -793,19 +793,6 @@ def check_same_constant(const):
793793
self.check_constant(f1, Ellipsis)
794794
self.assertEqual(repr(f1()), repr(Ellipsis))
795795

796-
# Merge constants in tuple or frozenset
797-
f1, f2 = lambda: "not a name", lambda: ("not a name",)
798-
f3 = lambda x: x in {("not a name",)}
799-
# TODO: I'm not sure if this is right..
800-
self.assertIs(f1.__code__.co_consts[0],
801-
f2.__code__.co_consts[0])
802-
self.assertIs(f1.__code__.co_consts[0],
803-
f2.__code__.co_consts[1][0])
804-
self.assertIs(f1.__code__.co_consts[0],
805-
f3.__code__.co_consts[0])
806-
self.assertIs(f1.__code__.co_consts[0],
807-
f3.__code__.co_consts[1][0])
808-
809796
# {0} is converted to a constant frozenset({0}) by the peephole
810797
# optimizer
811798
f1, f2 = lambda x: x in {0}, lambda x: x in {0}

Lib/test/test_opcache.py

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,26 +1508,14 @@ def to_bool_str():
15081508
@cpython_only
15091509
@requires_specialization_ft
15101510
def test_unpack_sequence(self):
1511-
# Tuple folding is currently disabled in the AST optimizer
1512-
# def unpack_sequence_two_tuple():
1513-
# for _ in range(100):
1514-
# a, b = 1, 2
1515-
# self.assertEqual(a, 1)
1516-
# self.assertEqual(b, 2)
1517-
1518-
# unpack_sequence_two_tuple()
1519-
# self.assert_specialized(unpack_sequence_two_tuple,
1520-
# "UNPACK_SEQUENCE_TWO_TUPLE")
1521-
# self.assert_no_opcode(unpack_sequence_two_tuple, "UNPACK_SEQUENCE")
1522-
1523-
# def unpack_sequence_tuple():
1524-
# for _ in range(100):
1525-
# a, = 1,
1526-
# self.assertEqual(a, 1)
1527-
1528-
# unpack_sequence_tuple()
1529-
# self.assert_specialized(unpack_sequence_tuple, "UNPACK_SEQUENCE_TUPLE")
1530-
# self.assert_no_opcode(unpack_sequence_tuple, "UNPACK_SEQUENCE")
1511+
def unpack_sequence_tuple():
1512+
for _ in range(100):
1513+
a, b, c, d = 1, 2, 3, 4
1514+
self.assertEqual((a, b, c, d), (1, 2, 3, 4))
1515+
1516+
unpack_sequence_tuple()
1517+
self.assert_specialized(unpack_sequence_tuple, "UNPACK_SEQUENCE_TUPLE")
1518+
self.assert_no_opcode(unpack_sequence_tuple, "UNPACK_SEQUENCE")
15311519

15321520
def unpack_sequence_list():
15331521
for _ in range(100):

Python/ast_opt.c

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -555,20 +555,6 @@ make_const_tuple(asdl_expr_seq *elts)
555555
return newval;
556556
}
557557

558-
static int
559-
fold_tuple(expr_ty node, PyArena *arena, _PyASTOptimizeState *state)
560-
{
561-
return 1;
562-
// Disable tuple folding for now
563-
// PyObject *newval;
564-
565-
// if (node->v.Tuple.ctx != Load)
566-
// return 1;
567-
568-
// newval = make_const_tuple(node->v.Tuple.elts);
569-
// return make_const(node, newval, arena);
570-
}
571-
572558
static int
573559
fold_subscr(expr_ty node, PyArena *arena, _PyASTOptimizeState *state)
574560
{
@@ -839,7 +825,6 @@ astfold_expr(expr_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
839825
break;
840826
case Tuple_kind:
841827
CALL_SEQ(astfold_expr, expr, node_->v.Tuple.elts);
842-
CALL(fold_tuple, expr_ty, node_);
843828
break;
844829
case Name_kind:
845830
if (node_->v.Name.ctx == Load &&

0 commit comments

Comments
 (0)