Lines Matching refs:self
38 def check_jump_targets(self, code):
47 self.fail(f'{instr.opname} at {instr.offset} '
52 self.fail(f'{instr.opname} at {instr.offset} '
56 self.fail(f'{instr.opname} at {instr.offset} '
59 def check_lnotab(self, code):
67 self.assertGreaterEqual(min_bytecode, 0)
68 self.assertLess(max_bytecode, len(code.co_code))
73 def test_unot(self):
78 self.assertNotInBytecode(unot, 'UNARY_NOT')
79 self.assertNotInBytecode(unot, 'POP_JUMP_FORWARD_IF_FALSE')
80 self.assertNotInBytecode(unot, 'POP_JUMP_BACKWARD_IF_FALSE')
81 self.assertInBytecode(unot, 'POP_JUMP_FORWARD_IF_TRUE')
82 self.check_lnotab(unot)
84 def test_elim_inversion_of_is_or_in(self):
91 with self.subTest(line=line):
93 self.assertInBytecode(code, cmp_op, invert)
94 self.check_lnotab(code)
96 def test_global_as_constant(self):
110 with self.subTest(func=func):
111 self.assertNotInBytecode(func, 'LOAD_GLOBAL')
112 self.assertInBytecode(func, 'LOAD_CONST', elem)
113 self.check_lnotab(func)
119 self.assertNotInBytecode(f, 'LOAD_GLOBAL')
120 self.assertInBytecode(f, 'LOAD_CONST', None)
121 self.check_lnotab(f)
123 def test_while_one(self):
130 self.assertNotInBytecode(f, elem)
132 self.assertInBytecode(f, elem)
133 self.check_lnotab(f)
135 def test_pack_unpack(self):
141 with self.subTest(line=line):
143 self.assertInBytecode(code, elem)
144 self.assertNotInBytecode(code, 'BUILD_TUPLE')
145 self.assertNotInBytecode(code, 'UNPACK_SEQUENCE')
146 self.check_lnotab(code)
148 def test_folding_of_tuples_of_constants(self):
156 with self.subTest(line=line):
158 self.assertInBytecode(code, 'LOAD_CONST', elem)
159 self.assertNotInBytecode(code, 'BUILD_TUPLE')
160 self.check_lnotab(code)
164 self.assertNotInBytecode(code, 'BUILD_TUPLE')
168 self.assertEqual(len(load_consts), 2)
169 self.check_lnotab(code)
187 self.check_lnotab(crater)
189 def test_folding_of_lists_of_constants(self):
197 with self.subTest(line=line):
199 self.assertInBytecode(code, 'LOAD_CONST', elem)
200 self.assertNotInBytecode(code, 'BUILD_LIST')
201 self.check_lnotab(code)
203 def test_folding_of_sets_of_constants(self):
212 with self.subTest(line=line):
214 self.assertNotInBytecode(code, 'BUILD_SET')
215 self.assertInBytecode(code, 'LOAD_CONST', elem)
216 self.check_lnotab(code)
225 self.assertTrue(f(3))
226 self.assertTrue(not f(4))
227 self.check_lnotab(f)
229 self.assertTrue(not g(3))
230 self.assertTrue(g(4))
231 self.check_lnotab(g)
234 def test_folding_of_binops_on_constants(self):
252 with self.subTest(line=line):
254 self.assertInBytecode(code, 'LOAD_CONST', elem)
256 self.assertFalse(instr.opname.startswith('BINARY_'))
257 self.check_lnotab(code)
261 self.assertInBytecode(code, 'LOAD_CONST', 2)
262 self.assertInBytecode(code, 'LOAD_CONST', 'b')
263 self.check_lnotab(code)
267 self.assertInBytecode(code, 'LOAD_CONST', 10000)
268 self.assertNotIn("x"*10000, code.co_consts)
269 self.check_lnotab(code)
271 self.assertInBytecode(code, 'LOAD_CONST', 1000)
272 self.assertNotIn(1<<1000, code.co_consts)
273 self.check_lnotab(code)
275 self.assertInBytecode(code, 'LOAD_CONST', 1000)
276 self.assertNotIn(2**1000, code.co_consts)
277 self.check_lnotab(code)
279 def test_binary_subscr_on_unicode(self):
282 self.assertInBytecode(code, 'LOAD_CONST', 'f')
283 self.assertNotInBytecode(code, 'BINARY_SUBSCR')
284 self.check_lnotab(code)
286 self.assertInBytecode(code, 'LOAD_CONST', '\uffff')
287 self.assertNotInBytecode(code,'BINARY_SUBSCR')
288 self.check_lnotab(code)
292 self.assertInBytecode(code, 'LOAD_CONST', '\U00012345')
293 self.assertNotInBytecode(code, 'BINARY_SUBSCR')
294 self.check_lnotab(code)
299 self.assertInBytecode(code, 'BINARY_SUBSCR')
300 self.check_lnotab(code)
302 def test_folding_of_unaryops_on_constants(self):
311 with self.subTest(line=line):
313 self.assertInBytecode(code, 'LOAD_CONST', elem)
315 self.assertFalse(instr.opname.startswith('UNARY_'))
316 self.check_lnotab(code)
323 self.assertFalse(instr.opname.startswith('UNARY_'))
324 self.check_lnotab(negzero)
331 with self.subTest(line=line):
333 self.assertInBytecode(code, 'LOAD_CONST', elem)
334 self.assertInBytecode(code, opname)
335 self.check_lnotab(code)
337 def test_elim_extra_return(self):
341 self.assertNotInBytecode(f, 'LOAD_CONST', None)
344 self.assertEqual(len(returns), 1)
345 self.check_lnotab(f)
349 def test_elim_jump_to_return(self):
355 self.check_jump_targets(f)
356 self.assertNotInBytecode(f, 'JUMP_FORWARD')
357 self.assertNotInBytecode(f, 'JUMP_ABSOLUTE')
360 self.assertEqual(len(returns), 2)
361 self.check_lnotab(f)
363 def test_elim_jump_to_uncond_jump(self):
373 self.check_jump_targets(f)
374 self.check_lnotab(f)
376 def test_elim_jump_to_uncond_jump2(self):
384 self.check_jump_targets(f)
385 self.check_lnotab(f)
387 def test_elim_jump_to_uncond_jump3(self):
393 self.check_jump_targets(f)
394 self.check_lnotab(f)
395 self.assertEqual(count_instr_recursively(f, 'JUMP_IF_FALSE_OR_POP'), 2)
400 self.check_jump_targets(f)
401 self.check_lnotab(f)
402 self.assertEqual(count_instr_recursively(f, 'JUMP_IF_TRUE_OR_POP'), 2)
407 self.check_jump_targets(f)
408 self.check_lnotab(f)
409 self.assertNotInBytecode(f, 'JUMP_IF_FALSE_OR_POP')
410 self.assertInBytecode(f, 'JUMP_IF_TRUE_OR_POP')
411 self.assertInBytecode(f, 'POP_JUMP_FORWARD_IF_FALSE')
416 self.check_jump_targets(f)
417 self.check_lnotab(f)
418 self.assertNotInBytecode(f, 'JUMP_IF_TRUE_OR_POP')
419 self.assertInBytecode(f, 'JUMP_IF_FALSE_OR_POP')
420 self.assertInBytecode(f, 'POP_JUMP_FORWARD_IF_TRUE')
422 def test_elim_jump_after_return1(self):
433 self.assertNotInBytecode(f, 'JUMP_FORWARD')
434 self.assertNotInBytecode(f, 'JUMP_ABSOLUTE')
437 self.assertLessEqual(len(returns), 6)
438 self.check_lnotab(f)
440 def test_make_function_doesnt_bail(self):
445 self.assertNotInBytecode(f, 'BINARY_OP')
446 self.check_lnotab(f)
448 def test_constant_folding(self):
462 with self.subTest(e=e):
465 self.assertFalse(instr.opname.startswith('UNARY_'))
466 self.assertFalse(instr.opname.startswith('BINARY_'))
467 self.assertFalse(instr.opname.startswith('BUILD_'))
468 self.check_lnotab(code)
470 def test_in_literal_list(self):
473 self.assertEqual(count_instr_recursively(containtest, 'BUILD_LIST'), 0)
474 self.check_lnotab(containtest)
476 def test_iterate_literal_list(self):
480 self.assertEqual(count_instr_recursively(forloop, 'BUILD_LIST'), 0)
481 self.check_lnotab(forloop)
483 def test_condition_with_binop_with_bools(self):
488 self.assertEqual(f(), 1)
489 self.check_lnotab(f)
491 def test_if_with_if_expression(self):
497 self.assertTrue(f(True))
498 self.check_lnotab(f)
500 def test_trailing_nops(self):
510 self.check_lnotab(f)
512 def test_assignment_idiom_in_comprehensions(self):
515 self.assertEqual(count_instr_recursively(listcomp, 'FOR_ITER'), 1)
518 self.assertEqual(count_instr_recursively(setcomp, 'FOR_ITER'), 1)
521 self.assertEqual(count_instr_recursively(dictcomp, 'FOR_ITER'), 1)
524 self.assertEqual(count_instr_recursively(genexpr, 'FOR_ITER'), 1)
526 def test_format_combinations(self):
544 with self.subTest(fmt=fmt, value=value):
547 self.assertEqual(s2, s1, f'{fmt = }')
549 def test_format_misc(self):
558 self.assertEqual(format('string'), 'string')
559 self.assertEqual(format('x = %s!', 1234), 'x = 1234!')
560 self.assertEqual(format('x = %d!', 1234), 'x = 1234!')
561 self.assertEqual(format('x = %x!', 1234), 'x = 4d2!')
562 self.assertEqual(format('x = %f!', 1234), 'x = 1234.000000!')
563 self.assertEqual(format('x = %s!', 1234.5678901), 'x = 1234.5678901!')
564 self.assertEqual(format('x = %f!', 1234.5678901), 'x = 1234.567890!')
565 self.assertEqual(format('x = %d!', 1234.5678901), 'x = 1234!')
566 self.assertEqual(format('x = %s%% %%%%', 1234), 'x = 1234% %%')
567 self.assertEqual(format('x = %s!', '%% %s'), 'x = %% %s!')
568 self.assertEqual(format('x = %s, y = %d', 12, 34), 'x = 12, y = 34')
570 def test_format_errors(self):
571 with self.assertRaisesRegex(TypeError,
574 with self.assertRaisesRegex(TypeError,
577 with self.assertRaisesRegex(ValueError, 'incomplete format'):
579 with self.assertRaisesRegex(ValueError, 'incomplete format'):
581 with self.assertRaisesRegex(TypeError,
584 with self.assertRaisesRegex(ValueError, 'unsupported format character'):
586 with self.assertRaisesRegex(TypeError, 'a real number is required, not str'):
588 with self.assertRaisesRegex(TypeError, 'an integer is required, not float'):
590 with self.assertRaisesRegex(TypeError, 'an integer is required, not str'):
592 with self.assertRaisesRegex(TypeError, 'must be real number, not str'):
594 with self.assertRaisesRegex(TypeError,
597 with self.assertRaisesRegex(TypeError,
601 def test_static_swaps_unpack_two(self):
605 self.assertNotInBytecode(f, "SWAP")
607 def test_static_swaps_unpack_three(self):
615 self.assertNotInBytecode(f, "SWAP")
617 def test_static_swaps_match_mapping(self):
620 with self.subTest(pattern):
622 self.assertNotInBytecode(code, "SWAP")
624 def test_static_swaps_match_class(self):
634 with self.subTest(pattern):
636 self.assertNotInBytecode(code, "SWAP")
638 def test_static_swaps_match_sequence(self):
644 with self.subTest(pattern):
649 self.assertInBytecode(code, "SWAP")
651 self.assertNotInBytecode(code, "SWAP")
656 def test_bug_11510(self):
663 with self.assertRaises(ValueError):
666 def test_bpo_42057(self):
673 def test_bpo_45773_pop_jump_if_true(self):
676 def test_bpo_45773_pop_jump_if_false(self):