Skip to content

Commit c03252c

Browse files
committed
merge unaryop folding tests
1 parent 3796884 commit c03252c

1 file changed

Lines changed: 38 additions & 56 deletions

File tree

Lib/test/test_peepholer.py

Lines changed: 38 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -307,44 +307,6 @@ def test_binary_subscr_on_unicode(self):
307307
self.assertInBytecode(code, 'BINARY_OP')
308308
self.check_lnotab(code)
309309

310-
def test_folding_of_unaryops_on_constants(self):
311-
for line, elem in (
312-
('-0.5', -0.5), # unary negative
313-
('-0.0', -0.0), # -0.0
314-
('-(1.0-1.0)', -0.0), # -0.0 after folding
315-
('-0', 0), # -0
316-
('~-2', 1), # unary invert
317-
('+1', 1), # unary positive
318-
):
319-
with self.subTest(line=line):
320-
code = compile(line, '', 'single')
321-
if isinstance(elem, int):
322-
self.assertInBytecode(code, 'LOAD_SMALL_INT', elem)
323-
else:
324-
self.assertInBytecode(code, 'LOAD_CONST', elem)
325-
for instr in dis.get_instructions(code):
326-
self.assertFalse(instr.opname.startswith('UNARY_'))
327-
self.check_lnotab(code)
328-
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-
337-
# Verify that unfoldables are skipped
338-
for line, elem, opname in (
339-
('-"abc"', 'abc', 'UNARY_NEGATIVE'),
340-
('~"abc"', 'abc', 'UNARY_INVERT'),
341-
):
342-
with self.subTest(line=line):
343-
code = compile(line, '', 'single')
344-
self.assertInBytecode(code, 'LOAD_CONST', elem)
345-
self.assertInBytecode(code, opname)
346-
self.check_lnotab(code)
347-
348310
def test_elim_extra_return(self):
349311
# RETURN LOAD_CONST None RETURN --> RETURN
350312
def f(x):
@@ -518,27 +480,47 @@ def test_constant_folding_small_int(self):
518480
def test_folding_unaryop(self):
519481
intrinsic_positive = 5
520482
tests = [
521-
('---1', 'UNARY_NEGATIVE', None, True),
522-
('---""', 'UNARY_NEGATIVE', None, False),
523-
('~~~1', 'UNARY_INVERT', None, True),
524-
('~~~""', 'UNARY_INVERT', None, False),
525-
('not not True', 'UNARY_NOT', None, True),
526-
('not not x', 'UNARY_NOT', None, True), # this should be optimized regardless of constant or not
527-
('+++1', 'CALL_INTRINSIC_1', intrinsic_positive, True),
528-
('---x', 'UNARY_NEGATIVE', None, False),
529-
('~~~x', 'UNARY_INVERT', None, False),
530-
('+++x', 'CALL_INTRINSIC_1', intrinsic_positive, False),
531-
]
532-
533-
for expr, opcode, oparg, optimized in tests:
534-
with self.subTest(expr=expr, optimized=optimized):
535-
code = compile(expr, '', 'single')
536-
if optimized:
537-
self.assertNotInBytecode(code, opcode, argval=oparg)
483+
('-0', 'UNARY_NEGATIVE', None, True, 'LOAD_SMALL_INT', 0),
484+
('-0.0', 'UNARY_NEGATIVE', None, True, 'LOAD_CONST', -0.0),
485+
('-(1.0-1.0)', 'UNARY_NEGATIVE', None, True, 'LOAD_CONST', -0.0),
486+
('-0.5', 'UNARY_NEGATIVE', None, True, 'LOAD_CONST', -0.5),
487+
('---1', 'UNARY_NEGATIVE', None, True, 'LOAD_CONST', -1),
488+
('---""', 'UNARY_NEGATIVE', None, False, None, None),
489+
('~~~1', 'UNARY_INVERT', None, True, 'LOAD_CONST', -2),
490+
('~~~""', 'UNARY_INVERT', None, False, None, None),
491+
('not not True', 'UNARY_NOT', None, True, 'LOAD_CONST', True),
492+
('not not x', 'UNARY_NOT', None, True, 'LOAD_NAME', 'x'), # this should be optimized regardless of constant or not
493+
('+++1', 'CALL_INTRINSIC_1', intrinsic_positive, True, 'LOAD_SMALL_INT', 1),
494+
('---x', 'UNARY_NEGATIVE', None, False, None, None),
495+
('~~~x', 'UNARY_INVERT', None, False, None, None),
496+
('+++x', 'CALL_INTRINSIC_1', intrinsic_positive, False, None, None),
497+
]
498+
499+
for (
500+
expr,
501+
original_opcode,
502+
original_argval,
503+
is_optimized,
504+
optimized_opcode,
505+
optimized_argval,
506+
) in tests:
507+
with self.subTest(expr=expr, is_optimized=is_optimized):
508+
code = compile(expr, "", "single")
509+
if is_optimized:
510+
self.assertNotInBytecode(code, original_opcode, argval=original_argval)
511+
self.assertInBytecode(code, optimized_opcode, argval=optimized_argval)
538512
else:
539-
self.assertInBytecode(code, opcode, argval=oparg)
513+
self.assertInBytecode(code, original_opcode, argval=original_argval)
540514
self.check_lnotab(code)
541515

516+
# Check that -0.0 works after marshaling
517+
def negzero():
518+
return -(1.0-1.0)
519+
520+
for instr in dis.get_instructions(negzero):
521+
self.assertFalse(instr.opname.startswith('UNARY_'))
522+
self.check_lnotab(negzero)
523+
542524
def test_folding_binop(self):
543525
tests = [
544526
('1 + 2', False, 'NB_ADD'),

0 commit comments

Comments
 (0)