Lines Matching refs:self

18     def compile_single(self, source):
21 def assertInvalidSingle(self, source):
22 self.assertRaises(SyntaxError, self.compile_single, source)
24 def test_no_ending_newline(self):
28 def test_empty(self):
31 def test_other_newlines(self):
37 def test_debug_assignment(self):
39 self.assertRaises(SyntaxError, compile, '__debug__ = 1', '?', 'single')
43 self.assertEqual(__debug__, prev)
46 def test_argument_handling(self):
48 self.assertRaises(SyntaxError, eval, 'lambda a,a:0')
49 self.assertRaises(SyntaxError, eval, 'lambda a,a=1:0')
50 self.assertRaises(SyntaxError, eval, 'lambda a=1,a=1:0')
51 self.assertRaises(SyntaxError, exec, 'def f(a, a): pass')
52 self.assertRaises(SyntaxError, exec, 'def f(a = 0, a = 1): pass')
53 self.assertRaises(SyntaxError, exec, 'def f(a): global a; a = 1')
55 def test_syntax_error(self):
56 self.assertRaises(SyntaxError, compile, "1+*3", "filename", "exec")
58 def test_none_keyword_arg(self):
59 self.assertRaises(SyntaxError, compile, "f(None=1)", "<string>", "exec")
61 def test_duplicate_global_local(self):
62 self.assertRaises(SyntaxError, exec, 'def f(a): global a; a = 1')
64 def test_exec_with_general_mapping_for_locals(self):
68 def __getitem__(self, key):
72 def __setitem__(self, key, value):
73 self.results = (key, value)
74 def keys(self):
80 self.assertEqual(m.results, ('z', 12))
86 self.fail('Did not detect a KeyError')
88 self.assertEqual(m.results, ('z', list('xyz')))
90 self.assertEqual(m.results, ('z', g))
92 self.assertEqual(m.results, ('z', m))
93 self.assertRaises(TypeError, exec, 'z = b', m)
99 self.assertRaises(TypeError, exec, 'z = a', g, m)
103 def __getitem__(self, key):
106 return dict.__getitem__(self, key)
109 self.assertEqual(d['z'], 12)
111 def test_extended_arg(self):
135 self.assertEqual(g['f'](5), 0)
137 def test_argument_order(self):
138 self.assertRaises(SyntaxError, exec, 'def f(a=1, b): pass')
140 def test_float_literals(self):
142 self.assertRaises(SyntaxError, eval, "2e")
143 self.assertRaises(SyntaxError, eval, "2.0e+")
144 self.assertRaises(SyntaxError, eval, "1e-")
145 self.assertRaises(SyntaxError, eval, "3-4e/21")
147 def test_indentation(self):
159 def test_leading_newlines(self):
162 self.assertEqual(co.co_firstlineno, 1)
164 self.assertEqual(lines[0][2], 0)
165 self.assertEqual(lines[1][2], 257)
167 def test_literals_with_leading_zeroes(self):
173 self.assertRaises(SyntaxError, eval, arg)
175 self.assertEqual(eval("0xff"), 255)
176 self.assertEqual(eval("0777."), 777)
177 self.assertEqual(eval("0777.0"), 777)
178 self.assertEqual(eval("000000000000000000000000000000000000000000000000000777e0"), 777)
179 self.assertEqual(eval("0777e1"), 7770)
180 self.assertEqual(eval("0e0"), 0)
181 self.assertEqual(eval("0000e-012"), 0)
182 self.assertEqual(eval("09.5"), 9.5)
183 self.assertEqual(eval("0777j"), 777j)
184 self.assertEqual(eval("000"), 0)
185 self.assertEqual(eval("00j"), 0j)
186 self.assertEqual(eval("00.0"), 0)
187 self.assertEqual(eval("0e3"), 0)
188 self.assertEqual(eval("090000000000000."), 90000000000000.)
189 self.assertEqual(eval("090000000000000.0000000000000000000000"), 90000000000000.)
190 self.assertEqual(eval("090000000000000e0"), 90000000000000.)
191 self.assertEqual(eval("090000000000000e-0"), 90000000000000.)
192 self.assertEqual(eval("090000000000000j"), 90000000000000j)
193 self.assertEqual(eval("000000000000008."), 8.)
194 self.assertEqual(eval("000000000000009."), 9.)
195 self.assertEqual(eval("0b101010"), 42)
196 self.assertEqual(eval("-0b000000000010"), -2)
197 self.assertEqual(eval("0o777"), 511)
198 self.assertEqual(eval("-0o0000010"), -8)
200 def test_int_literals_too_long(self):
206 with self.assertRaises(SyntaxError) as err_ctx:
209 self.assertEqual(exc.lineno, 3)
210 self.assertIn('Exceeds the limit ', str(exc))
211 self.assertIn(' Consider hexadecimal ', str(exc))
213 def test_unary_minus(self):
218 self.assertEqual(eval(all_one_bits), 4294967295)
219 self.assertEqual(eval("-" + all_one_bits), -4294967295)
223 self.assertEqual(eval(all_one_bits), 18446744073709551615)
224 self.assertEqual(eval("-" + all_one_bits), -18446744073709551615)
226 self.fail("How many bits *does* this machine have???")
229 self.assertIsInstance(eval("%s" % (-sys.maxsize - 1)), int)
230 self.assertIsInstance(eval("%s" % (-sys.maxsize - 2)), int)
233 def test_32_63_bit_values(self):
243 for variable in self.test_32_63_bit_values.__code__.co_consts:
245 self.assertIsInstance(variable, int)
247 def test_sequence_unpacking_error(self):
250 self.assertEqual(i, 1)
251 self.assertEqual(j, -1)
253 def test_none_assignment(self):
270 self.assertRaises(SyntaxError, compile, stmt, 'tmp', 'single')
271 self.assertRaises(SyntaxError, compile, stmt, 'tmp', 'exec')
273 def test_import(self):
318 self.assertRaises(SyntaxError, compile, stmt, 'tmp', 'exec')
320 def test_for_distinct_code_objects(self):
327 self.assertNotEqual(id(f1.__code__), id(f2.__code__))
329 def test_lambda_doc(self):
331 self.assertIsNone(l.__doc__)
333 def test_encoding(self):
335 self.assertRaises(SyntaxError, compile, code, 'tmp', 'exec')
338 self.assertEqual(eval(code), '\xc2\xa4')
340 self.assertEqual(eval(code), '\xc2\xa4')
342 self.assertEqual(eval(code), '\xa4')
344 self.assertEqual(eval(code), '\xc2\xa4')
346 self.assertEqual(eval(code), '\xa4')
348 self.assertEqual(eval(code), '\xc2\u20ac')
350 self.assertEqual(eval(code), '# -*- coding: iso8859-15 -*-\n\xc2\xa4')
352 self.assertEqual(eval(code), '# -*- coding: iso8859-15 -*-\n\xa4')
354 def test_subscripts(self):
358 def __init__(self):
359 self.data = {}
360 def __getitem__(self, key):
361 return self.data[str(key)]
362 def __setitem__(self, key, value):
363 self.data[str(key)] = value
364 def __delitem__(self, key):
365 del self.data[str(key)]
366 def __contains__(self, key):
367 return str(key) in self.data
371 self.assertEqual(d[1], 1)
373 self.assertEqual(d[1], 2)
375 self.assertNotIn(1, d)
378 self.assertEqual(d[1, 1], 1)
380 self.assertEqual(d[1, 1], 2)
382 self.assertNotIn((1, 1), d)
385 self.assertEqual(d[1:2], 1)
387 self.assertEqual(d[1:2], 2)
389 self.assertNotIn(slice(1, 2), d)
392 self.assertEqual(d[1:2, 1:2], 1)
394 self.assertEqual(d[1:2, 1:2], 2)
396 self.assertNotIn((slice(1, 2), slice(1, 2)), d)
399 self.assertEqual(d[1:2:3], 1)
401 self.assertEqual(d[1:2:3], 2)
403 self.assertNotIn(slice(1, 2, 3), d)
406 self.assertEqual(d[1:2:3, 1:2:3], 1)
408 self.assertEqual(d[1:2:3, 1:2:3], 2)
410 self.assertNotIn((slice(1, 2, 3), slice(1, 2, 3)), d)
413 self.assertEqual(d[...], 1)
415 self.assertEqual(d[...], 2)
417 self.assertNotIn(Ellipsis, d)
420 self.assertEqual(d[..., ...], 1)
422 self.assertEqual(d[..., ...], 2)
424 self.assertNotIn((Ellipsis, Ellipsis), d)
426 def test_annotation_limit(self):
432 def test_mangling(self):
440 self.assertIn("_A__mangled", A.f.__code__.co_varnames)
441 self.assertIn("__not_mangled__", A.f.__code__.co_varnames)
442 self.assertIn("_A__mangled_mod", A.f.__code__.co_varnames)
443 self.assertIn("__package__", A.f.__code__.co_varnames)
445 def test_compile_ast(self):
462 self.assertTrue(type(ast) == _ast.Module)
464 self.assertEqual(co1, co2)
466 self.assertEqual(co2.co_filename, '%s3' % fname)
470 self.assertRaises(TypeError, compile, co1, '<ast>', 'eval')
473 self.assertRaises(TypeError, compile, _ast.If(), '<ast>', 'exec')
478 self.assertRaises(TypeError, compile, ast, '<ast>', 'exec')
480 def test_dict_evaluation_order(self):
489 self.assertEqual(d, {1: 2, 3: 4})
491 def test_compile_filename(self):
494 self.assertEqual(code.co_filename, 'file.py')
496 with self.assertWarns(DeprecationWarning):
498 self.assertEqual(code.co_filename, 'file.py')
499 self.assertRaises(TypeError, compile, 'pass', list(b'file.py'), 'exec')
502 def test_same_filename_used(self):
507 self.assertIs(obj.co_filename, c.co_filename)
509 def test_single_statement(self):
510 self.compile_single("1 + 2")
511 self.compile_single("\n1 + 2")
512 self.compile_single("1 + 2\n")
513 self.compile_single("1 + 2\n\n")
514 self.compile_single("1 + 2\t\t\n")
515 self.compile_single("1 + 2\t\t\n ")
516 self.compile_single("1 + 2 # one plus two")
517 self.compile_single("1; 2")
518 self.compile_single("import sys; sys")
519 self.compile_single("def f():\n pass")
520 self.compile_single("while False:\n pass")
521 self.compile_single("if x:\n f(x)")
522 self.compile_single("if x:\n f(x)\nelse:\n g(x)")
523 self.compile_single("class T:\n pass")
524 self.compile_single("c = '''\na=1\nb=2\nc=3\n'''")
526 def test_bad_single_statement(self):
527 self.assertInvalidSingle('1\n2')
528 self.assertInvalidSingle('def f(): pass')
529 self.assertInvalidSingle('a = 13\nb = 187')
530 self.assertInvalidSingle('del x\ndel y')
531 self.assertInvalidSingle('f()\ng()')
532 self.assertInvalidSingle('f()\n# blah\nblah()')
533 self.assertInvalidSingle('f()\nxy # blah\nblah()')
534 self.assertInvalidSingle('x = 5 # comment\nx = 6\n')
535 self.assertInvalidSingle("c = '''\nd=1\n'''\na = 1\n\nb = 2\n")
537 def test_particularly_evil_undecodable(self):
545 self.assertIn(b"source code cannot contain null bytes", res.err)
547 def test_yet_more_evil_still_undecodable(self):
555 self.assertIn(b"source code cannot contain null bytes", res.err)
558 def test_compiler_recursion_limit(self):
577 with self.assertRaises(RecursionError, msg=details):
588 def test_null_terminated(self):
591 with self.assertRaisesRegex(SyntaxError, "cannot contain null"):
593 with self.assertRaisesRegex(SyntaxError, "cannot contain null"):
596 self.assertEqual(eval(code), 23)
598 self.assertEqual(eval(code), 23)
600 self.assertEqual(eval(code), 23)
603 self.assertEqual(eval(memoryview(b"1234")[1:-1]), 23)
606 self.assertEqual(namespace['x'], 12)
608 def check_constant(self, func, expected):
613 self.fail("unable to find constant %r in %r"
619 def test_merge_constants(self):
629 self.assertIs(f1.__code__.co_consts, f2.__code__.co_consts)
630 self.check_constant(f1, const)
631 self.assertEqual(repr(f1()), repr(const))
642 self.assertIs(f1.__code__.co_consts, f2.__code__.co_consts)
643 self.check_constant(f1, Ellipsis)
644 self.assertEqual(repr(f1()), repr(Ellipsis))
649 self.assertIs(f1.__code__.co_consts[1],
651 self.assertIs(next(iter(f3.__code__.co_consts[1])),
657 self.assertIs(f1.__code__.co_consts, f2.__code__.co_consts)
658 self.check_constant(f1, frozenset({0}))
659 self.assertTrue(f1(0))
664 def test_merge_code_attrs(self):
669 self.assertIs(f1.__code__.co_linetable, f2.__code__.co_linetable)
674 def test_strip_unused_consts(self):
680 self.assertEqual(f1.__code__.co_consts, ("docstring", 42))
687 def test_peephole_opt_unreachable_code_array_access_in_bounds(self):
696 self.assertEqual(
700 def test_dont_merge_constants(self):
709 self.assertIsNot(f1.__code__, f2.__code__)
710 self.assertNotEqual(f1.__code__, f2.__code__)
711 self.check_constant(f1, const1)
712 self.check_constant(f2, const2)
713 self.assertEqual(repr(f1()), repr(const1))
714 self.assertEqual(repr(f2()), repr(const2))
725 self.assertIsNot(f1.__code__, f2.__code__)
726 self.check_constant(f1, +0.0j)
727 self.check_constant(f2, -0.0j)
728 self.assertEqual(repr(f1()), repr(+0.0j))
729 self.assertEqual(repr(f2()), repr(-0.0j))
734 self.assertIsNot(f1.__code__, f2.__code__)
735 self.check_constant(f1, frozenset({0}))
736 self.check_constant(f2, frozenset({0.0}))
737 self.assertTrue(f1(0))
738 self.assertTrue(f2(0.0))
740 def test_path_like_objects(self):
744 def test_stack_overflow(self):
752 def test_dead_blocks_do_not_generate_bytecode(self):
778 self.assertLessEqual(len(opcodes), 4)
779 self.assertEqual('LOAD_CONST', opcodes[-2].opname)
780 self.assertEqual(None, opcodes[-2].argval)
781 self.assertEqual('RETURN_VALUE', opcodes[-1].opname)
783 def test_false_while_loop(self):
797 self.assertEqual(3, len(opcodes))
798 self.assertEqual('LOAD_CONST', opcodes[1].opname)
799 self.assertEqual(None, opcodes[1].argval)
800 self.assertEqual('RETURN_VALUE', opcodes[2].opname)
802 def test_consts_in_conditionals(self):
819 with self.subTest(func=func):
821 self.assertLessEqual(len(opcodes), 3)
822 self.assertIn('LOAD_', opcodes[-2].opname)
823 self.assertEqual('RETURN_VALUE', opcodes[-1].opname)
825 def test_imported_load_method(self):
852 with self.subTest(func=func.__name__):
855 self.assertNotIn('LOAD_METHOD', instructions)
856 self.assertIn('LOAD_ATTR', instructions)
857 self.assertIn('PRECALL', instructions)
859 def test_lineno_procedure_call(self):
867 def test_lineno_after_implicit_return(self):
897 with self.subTest(func=func):
899 self.assertEqual(frame.f_lineno-frame.f_code.co_firstlineno, lastline)
901 def test_lineno_after_no_code(self):
909 with self.subTest(func=func):
913 self.assertEqual(start, 0)
914 self.assertEqual(line, code.co_firstlineno)
916 def get_code_lines(self, code):
925 def test_lineno_attribute(self):
965 with self.subTest(func=func):
966 code_lines = self.get_code_lines(func.__code__)
967 self.assertEqual(lines, code_lines)
969 def test_line_number_genexp(self):
980 code_lines = self.get_code_lines(genexp_code)
981 self.assertEqual(genexp_lines, code_lines)
983 def test_line_number_implicit_return_after_async_for(self):
990 code_lines = self.get_code_lines(test.__code__)
991 self.assertEqual(expected_lines, code_lines)
993 def test_big_dict_literal(self):
1001 self.assertEqual(len(eval(the_dict)), dict_size)
1003 def test_redundant_jump_in_if_else_break(self):
1028 self.assertNotEqual(instr.arg, 0)
1030 self.assertNotEqual(instr.arg, (line + 1)*INSTR_SIZE)
1032 def test_no_wraparound_jump(self):
1040 self.assertNotEqual(instr.opname, "EXTENDED_ARG")
1042 def test_compare_positions(self):
1064 with self.subTest(source):
1065 self.assertEqual(actual_positions, expected_positions)
1067 def test_apply_static_swaps(self):
1071 self.assertEqual(f("x", "y"), "y")
1073 def test_apply_static_swaps_2(self):
1077 self.assertEqual(f("x", "y", "z"), "z")
1079 def test_apply_static_swaps_3(self):
1083 self.assertEqual(f("x", "y", "z"), "y")
1091 def check_positions_against_ast(self, snippet):
1096 self.assertTrue(type(ast_tree) == _ast.Module)
1101 def generic_visit(self, node):
1119 self.assertIn(line, lines)
1121 self.assertIn(end_line, end_lines)
1123 self.assertIn(col, columns)
1125 self.assertIn(end_col, end_columns)
1129 def assertOpcodeSourcePositionIs(self, code, opcode,
1138 self.assertEqual(position[0], line)
1139 self.assertEqual(position[1], end_line)
1140 self.assertEqual(position[2], column)
1141 self.assertEqual(position[3], end_column)
1144 self.fail(f"Opcode {opcode} not found in code")
1146 def test_simple_assignment(self):
1148 self.check_positions_against_ast(snippet)
1150 def test_compiles_to_extended_op_arg(self):
1161 compiled_code, _ = self.check_positions_against_ast(snippet)
1163 self.assertOpcodeSourcePositionIs(compiled_code, 'BINARY_OP',
1166 self.assertOpcodeSourcePositionIs(compiled_code, 'BINARY_OP',
1170 def test_multiline_expression(self):
1176 compiled_code, _ = self.check_positions_against_ast(snippet)
1177 self.assertOpcodeSourcePositionIs(compiled_code, 'CALL',
1180 def test_very_long_line_end_offset(self):
1186 compiled_code, _ = self.check_positions_against_ast(snippet)
1187 self.assertOpcodeSourcePositionIs(compiled_code, 'CALL',
1190 def test_complex_single_line_expression(self):
1193 compiled_code, _ = self.check_positions_against_ast(snippet)
1194 self.assertOpcodeSourcePositionIs(compiled_code, 'BINARY_SUBSCR',
1196 self.assertOpcodeSourcePositionIs(compiled_code, 'BINARY_OP',
1198 self.assertOpcodeSourcePositionIs(compiled_code, 'BINARY_OP',
1200 self.assertOpcodeSourcePositionIs(compiled_code, 'BINARY_OP',
1202 self.assertOpcodeSourcePositionIs(compiled_code, 'BINARY_OP',
1205 def test_multiline_assert_rewritten_as_method_call(self):
1226 def test_push_null_load_global_positions(self):
1250 with self.subTest(body):
1255 self.assertOpcodeSourcePositionIs(
1264 def test_attribute_augassign(self):
1267 self.assertOpcodeSourcePositionIs(
1270 self.assertOpcodeSourcePositionIs(
1274 def test_attribute_del(self):
1277 self.assertOpcodeSourcePositionIs(
1281 def test_attribute_load(self):
1284 self.assertOpcodeSourcePositionIs(
1288 def test_attribute_store(self):
1291 self.assertOpcodeSourcePositionIs(
1295 def test_method_call(self):
1298 self.assertOpcodeSourcePositionIs(
1301 self.assertOpcodeSourcePositionIs(
1305 def test_weird_attribute_position_regressions(self):
1318 self.assertIsNotNone(line)
1319 self.assertIsNotNone(end_line)
1320 self.assertIsNotNone(column)
1321 self.assertIsNotNone(end_column)
1322 self.assertLessEqual((line, column), (end_line, end_column))
1325 def test_column_offset_deduplication(self):
1334 with self.subTest(source):
1336 self.assertEqual(len(code.co_consts), 2)
1337 self.assertIsInstance(code.co_consts[0], types.CodeType)
1338 self.assertIsInstance(code.co_consts[1], types.CodeType)
1339 self.assertNotEqual(code.co_consts[0], code.co_consts[1])
1340 self.assertNotEqual(
1352 def check_stack_size(self, code):
1358 self.assertLessEqual(code.co_stacksize, max_size)
1360 def test_and(self):
1361 self.check_stack_size("x and " * self.N + "x")
1363 def test_or(self):
1364 self.check_stack_size("x or " * self.N + "x")
1366 def test_and_or(self):
1367 self.check_stack_size("x and x or " * self.N + "x")
1369 def test_chained_comparison(self):
1370 self.check_stack_size("x < " * self.N + "x")
1372 def test_if_else(self):
1373 self.check_stack_size("x if x else " * self.N + "x")
1375 def test_binop(self):
1376 self.check_stack_size("x + " * self.N + "x")
1378 def test_list(self):
1379 self.check_stack_size("[" + "x, " * self.N + "x]")
1381 def test_tuple(self):
1382 self.check_stack_size("(" + "x, " * self.N + "x)")
1384 def test_set(self):
1385 self.check_stack_size("{" + "x, " * self.N + "x}")
1387 def test_dict(self):
1388 self.check_stack_size("{" + "x:x, " * self.N + "x:x}")
1390 def test_func_args(self):
1391 self.check_stack_size("f(" + "x, " * self.N + ")")
1393 def test_func_kwargs(self):
1394 kwargs = (f'a{i}=x' for i in range(self.N))
1395 self.check_stack_size("f(" + ", ".join(kwargs) + ")")
1397 def test_meth_args(self):
1398 self.check_stack_size("o.m(" + "x, " * self.N + ")")
1400 def test_meth_kwargs(self):
1401 kwargs = (f'a{i}=x' for i in range(self.N))
1402 self.check_stack_size("o.m(" + ", ".join(kwargs) + ")")
1404 def test_func_and(self):
1406 code += " x and x\n" * self.N
1407 self.check_stack_size(code)
1409 def test_stack_3050(self):
1415 def test_stack_3050_2(self):
1427 def check_stack_size(self, snippet, async_=False):
1442 self.fail("stack sizes diverge with # of consecutive snippets: "
1445 def test_if(self):
1450 self.check_stack_size(snippet)
1452 def test_if_else(self):
1461 self.check_stack_size(snippet)
1463 def test_try_except_bare(self):
1470 self.check_stack_size(snippet)
1472 def test_try_except_qualified(self):
1483 self.check_stack_size(snippet)
1485 def test_try_except_as(self):
1496 self.check_stack_size(snippet)
1498 def test_try_except_star_qualified(self):
1507 self.check_stack_size(snippet)
1509 def test_try_except_star_as(self):
1518 self.check_stack_size(snippet)
1520 def test_try_except_star_finally(self):
1529 self.check_stack_size(snippet)
1531 def test_try_finally(self):
1538 self.check_stack_size(snippet)
1540 def test_with(self):
1545 self.check_stack_size(snippet)
1547 def test_while_else(self):
1554 self.check_stack_size(snippet)
1556 def test_for(self):
1561 self.check_stack_size(snippet)
1563 def test_for_else(self):
1570 self.check_stack_size(snippet)
1572 def test_for_break_continue(self):
1584 self.check_stack_size(snippet)
1586 def test_for_break_continue_inside_try_finally_block(self):
1601 self.check_stack_size(snippet)
1603 def test_for_break_continue_inside_finally_block(self):
1618 self.check_stack_size(snippet)
1620 def test_for_break_continue_inside_except_block(self):
1635 self.check_stack_size(snippet)
1637 def test_for_break_continue_inside_with_block(self):
1650 self.check_stack_size(snippet)
1652 def test_return_inside_try_finally_block(self):
1662 self.check_stack_size(snippet)
1664 def test_return_inside_finally_block(self):
1674 self.check_stack_size(snippet)
1676 def test_return_inside_except_block(self):
1686 self.check_stack_size(snippet)
1688 def test_return_inside_with_block(self):
1696 self.check_stack_size(snippet)
1698 def test_async_with(self):
1703 self.check_stack_size(snippet, async_=True)
1705 def test_async_for(self):
1710 self.check_stack_size(snippet, async_=True)
1712 def test_async_for_else(self):
1719 self.check_stack_size(snippet, async_=True)
1721 def test_for_break_continue_inside_async_with_block(self):
1734 self.check_stack_size(snippet, async_=True)
1736 def test_return_inside_async_with_block(self):
1744 self.check_stack_size(snippet, async_=True)