MCPcopy Create free account
hub / github.com/python/cpython / test_constant_folding_unaryop

Method test_constant_folding_unaryop

Lib/test/test_peepholer.py:278–321  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

276 self.check_lnotab(code)
277
278 def test_constant_folding_unaryop(self):
279 intrinsic_positive = 5
280 tests = [
281 ('-0', 'UNARY_NEGATIVE', None, True, 'LOAD_SMALL_INT', 0),
282 ('-0.0', 'UNARY_NEGATIVE', None, True, 'LOAD_CONST', -0.0),
283 ('-(1.0-1.0)', 'UNARY_NEGATIVE', None, True, 'LOAD_CONST', -0.0),
284 ('-0.5', 'UNARY_NEGATIVE', None, True, 'LOAD_CONST', -0.5),
285 ('---1', 'UNARY_NEGATIVE', None, True, 'LOAD_CONST', -1),
286 ('---""', 'UNARY_NEGATIVE', None, False, None, None),
287 ('~~~1', 'UNARY_INVERT', None, True, 'LOAD_CONST', -2),
288 ('~~~""', 'UNARY_INVERT', None, False, None, None),
289 ('not not True', 'UNARY_NOT', None, True, 'LOAD_CONST', True),
290 ('not not x', 'UNARY_NOT', None, True, 'LOAD_NAME', 'x'), # this should be optimized regardless of constant or not
291 ('+++1', 'CALL_INTRINSIC_1', intrinsic_positive, True, 'LOAD_SMALL_INT', 1),
292 ('---x', 'UNARY_NEGATIVE', None, False, None, None),
293 ('~~~x', 'UNARY_INVERT', None, False, None, None),
294 ('+++x', 'CALL_INTRINSIC_1', intrinsic_positive, False, None, None),
295 ('~True', 'UNARY_INVERT', None, False, None, None),
296 ]
297
298 for (
299 expr,
300 original_opcode,
301 original_argval,
302 is_optimized,
303 optimized_opcode,
304 optimized_argval,
305 ) in tests:
306 with self.subTest(expr=expr, is_optimized=is_optimized):
307 code = compile(expr, "", "single")
308 if is_optimized:
309 self.assertNotInBytecode(code, original_opcode, argval=original_argval)
310 self.assertInBytecode(code, optimized_opcode, argval=optimized_argval)
311 else:
312 self.assertInBytecode(code, original_opcode, argval=original_argval)
313 self.check_lnotab(code)
314
315 # Check that -0.0 works after marshaling
316 def negzero():
317 return -(1.0-1.0)
318
319 for instr in dis.get_instructions(negzero):
320 self.assertNotStartsWith(instr.opname, 'UNARY_')
321 self.check_lnotab(negzero)
322
323 def test_constant_folding_binop(self):
324 tests = [

Callers

nothing calls this directly

Calls 7

check_lnotabMethod · 0.95
assertNotInBytecodeMethod · 0.80
assertInBytecodeMethod · 0.80
get_instructionsMethod · 0.80
assertNotStartsWithMethod · 0.80
compileFunction · 0.50
subTestMethod · 0.45

Tested by

no test coverage detected