← 输入框|github.com|刷新
Skip to content

Commit 5ce2e16

Browse files
committed
Expose compiler IR APIs in _testinternalcapi
Add InstructionSequence and compiler_codegen, optimize_cfg, and assemble_code_object. Drop expectedFailure marks on tests that pass. Assisted-by: Grok:grok-4.6
1 parent 5dd0d87 commit 5ce2e16

10 files changed

Lines changed: 957 additions & 36 deletions

File tree

‎.cspell.dict/cpython.txt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ fsrc
9696
getdict
9797
getfunc
9898
getiter
99+
getlasti
99100
getsets
100101
getslice
101102
globalgetvar
@@ -148,6 +149,7 @@ mbiencoder
148149
mbstreamreader
149150
mbstreamwriter
150151
metavars
152+
mimalloc
151153
miscompiles
152154
mult
153155
multibytecodec
@@ -207,6 +209,7 @@ preds
207209
preinitialized
208210
pybuilddir
209211
pycore
212+
PYGC
210213
pyinner
211214
pydecimal
212215
pyerrors
@@ -221,6 +224,7 @@ pystrcmp
221224
PYTHONTRACEMALLOC
222225
PYTHONUTF8
223226
pythonw
227+
pytime
224228
PYTHREAD_NAME
225229
recvd
226230
releasebuffer

‎Lib/test/support/bytecode_helper.py‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ def check_instructions(self, insts):
135135
self.assertTrue(all(isinstance(l, int) for l in loc))
136136

137137

138-
@unittest.expectedFailure # TODO: RUSTPYTHON; compiler IR pipeline (compiler_codegen)
139138
@unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi")
140139
class CodegenTestCase(CompilationStepTestCase):
141140

@@ -151,7 +150,6 @@ def get_optimized(self, seq, consts, nlocals=0):
151150
insts = _testinternalcapi.optimize_cfg(seq, consts, nlocals)
152151
return insts, consts
153152

154-
@unittest.expectedFailure # TODO: RUSTPYTHON; compiler IR pipeline (assemble_code_object)
155153
@unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi")
156154
class AssemblerTestCase(CompilationStepTestCase):
157155

‎Lib/test/test_compiler_codegen.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
import textwrap
3+
import unittest
34
from test.support.bytecode_helper import CodegenTestCase
45

56
# Tests for the code-generation stage of the compiler.
@@ -41,6 +42,7 @@ def test_if_expression(self):
4142
]
4243
self.codegen_test(snippet, expected)
4344

45+
@unittest.expectedFailure # TODO: RUSTPYTHON; for-loop NOP end_lineno differs
4446
def test_for_loop(self):
4547
snippet = "for x in l:\n\tprint(x)"
4648
false_lbl = self.Label()

‎Lib/test/test_gc.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,7 +1205,7 @@ def test_something(self):
12051205
assert_python_ok("-c", source)
12061206

12071207

1208-
@unittest.expectedFailure # TODO: RUSTPYTHON; get_long_lived_total is not implemented
1208+
@unittest.expectedFailure # TODO: RUSTPYTHON; long_lived_total does not count untracked tuples
12091209
@unittest.skipUnless(Py_GIL_DISABLED, "requires free-threaded GC")
12101210
@unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi")
12111211
def test_tuple_untrack_counts(self):
@@ -1223,7 +1223,7 @@ def test_tuple_untrack_counts(self):
12231223
# Use n // 2 just in case some other objects were collected.
12241224
self.assertTrue(new_count - count > (n // 2))
12251225

1226-
@unittest.expectedFailure # TODO: RUSTPYTHON; get_tracked_heap_size is not implemented
1226+
@unittest.expectedFailure # TODO: RUSTPYTHON; tracked heap size is not generational
12271227
@requires_gil_enabled('need generational GC')
12281228
@unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi")
12291229
def test_heap_size(self):

‎Lib/test/test_peepholer.py‎

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,6 @@ def trace(frame, event, arg):
11231123

11241124
class DirectCfgOptimizerTests(CfgOptimizationTestCase):
11251125

1126-
@unittest.expectedFailure # TODO: RUSTPYTHON; compiler IR pipeline
11271126
def test_optimize_cfg_const_index_out_of_range(self):
11281127
insts = [
11291128
('LOAD_CONST', 2, 0),
@@ -1133,7 +1132,6 @@ def test_optimize_cfg_const_index_out_of_range(self):
11331132
with self.assertRaisesRegex(ValueError, "out of range"):
11341133
_testinternalcapi.optimize_cfg(seq, [0, 1], 0)
11351134

1136-
@unittest.expectedFailure # TODO: RUSTPYTHON; compiler IR pipeline
11371135
def test_optimize_cfg_consts_must_be_list(self):
11381136
insts = [
11391137
('LOAD_CONST', 0, 0),
@@ -1143,15 +1141,13 @@ def test_optimize_cfg_consts_must_be_list(self):
11431141
with self.assertRaisesRegex(TypeError, "consts must be a list"):
11441142
_testinternalcapi.optimize_cfg(seq, (0,), 0)
11451143

1146-
@unittest.expectedFailure # TODO: RUSTPYTHON; compiler IR pipeline
11471144
def test_compiler_codegen_metadata_consts_roundtrips_optimize_cfg(self):
11481145
tree = ast.parse("x = (1, 2)", mode="exec", optimize=1)
11491146
insts, meta = _testinternalcapi.compiler_codegen(tree, "<s>", 0)
11501147
consts = meta["consts"]
11511148
self.assertIsInstance(consts, list)
11521149
_testinternalcapi.optimize_cfg(insts, consts, 0)
11531150

1154-
@unittest.expectedFailure # TODO: RUSTPYTHON; compiler IR pipeline
11551151
def test_compiler_codegen_consts_include_none_required_for_implicit_return(self):
11561152
# Module "pass" only needs the const table entry for None once
11571153
# _PyCodegen_AddReturnAtEnd runs. If metadata["consts"] were taken
@@ -2238,7 +2234,6 @@ def test_conditional_jump_forward_const_condition(self):
22382234
consts=[0, 1, 2, 3, 4],
22392235
expected_consts=[0])
22402236

2241-
@unittest.expectedFailure # TODO: RUSTPYTHON; compiler IR pipeline
22422237
def test_conditional_jump_backward_non_const_condition(self):
22432238
insts = [
22442239
lbl1 := self.Label(),
@@ -2256,7 +2251,6 @@ def test_conditional_jump_backward_non_const_condition(self):
22562251
]
22572252
self.cfg_optimization_test(insts, expected, consts=list(range(5)))
22582253

2259-
@unittest.expectedFailure # TODO: RUSTPYTHON; compiler IR pipeline
22602254
def test_conditional_jump_backward_const_condition(self):
22612255
# The unreachable branch of the jump is removed
22622256
insts = [
@@ -2294,7 +2288,6 @@ def test_except_handler_label(self):
22942288
]
22952289
self.cfg_optimization_test(insts, expected_insts, consts=list(range(5)))
22962290

2297-
@unittest.expectedFailure # TODO: RUSTPYTHON; compiler IR pipeline
22982291
def test_no_unsafe_static_swap(self):
22992292
# We can't change order of two stores to the same location
23002293
insts = [
@@ -2319,7 +2312,6 @@ def test_no_unsafe_static_swap(self):
23192312
]
23202313
self.cfg_optimization_test(insts, expected_insts, consts=list(range(3)), nlocals=1)
23212314

2322-
@unittest.expectedFailure # TODO: RUSTPYTHON; compiler IR pipeline
23232315
def test_dead_store_elimination_in_same_lineno(self):
23242316
insts = [
23252317
('LOAD_CONST', 0, 1),
@@ -2342,7 +2334,6 @@ def test_dead_store_elimination_in_same_lineno(self):
23422334
]
23432335
self.cfg_optimization_test(insts, expected_insts, consts=list(range(3)), nlocals=1)
23442336

2345-
@unittest.expectedFailure # TODO: RUSTPYTHON; compiler IR pipeline
23462337
def test_no_dead_store_elimination_in_different_lineno(self):
23472338
insts = [
23482339
('LOAD_CONST', 0, 1),
@@ -2366,7 +2357,6 @@ def test_no_dead_store_elimination_in_different_lineno(self):
23662357
]
23672358
self.cfg_optimization_test(insts, expected_insts, consts=list(range(3)), nlocals=1)
23682359

2369-
@unittest.expectedFailure # TODO: RUSTPYTHON; compiler IR pipeline
23702360
def test_unconditional_jump_threading(self):
23712361

23722362
def get_insts(lno1, lno2, op1, op2):
@@ -2411,7 +2401,6 @@ def get_insts(lno1, lno2, op1, op2):
24112401
]
24122402
self.cfg_optimization_test(insts, expected_insts, consts=list(range(5)))
24132403

2414-
@unittest.expectedFailure # TODO: RUSTPYTHON; compiler IR pipeline
24152404
def test_list_to_tuple_get_iter(self):
24162405
# for _ in (*foo, *bar) -> for _ in [*foo, *bar]
24172406
INTRINSIC_LIST_TO_TUPLE = 6
@@ -2482,7 +2471,6 @@ def check(self, insts, expected_insts, consts=None):
24822471
self.cfg_optimization_test(insts_bb, expected_insts_bb,
24832472
consts=insts_consts, expected_consts=exp_consts)
24842473

2485-
@unittest.expectedFailure # TODO: RUSTPYTHON; compiler IR pipeline
24862474
def test_optimized(self):
24872475
insts = [
24882476
("LOAD_FAST", 0, 1),
@@ -2510,7 +2498,6 @@ def test_optimized(self):
25102498
]
25112499
self.check(insts, expected)
25122500

2513-
@unittest.expectedFailure # TODO: RUSTPYTHON; compiler IR pipeline
25142501
def test_unoptimized_if_unconsumed(self):
25152502
insts = [
25162503
("LOAD_FAST", 0, 1),
@@ -2536,7 +2523,6 @@ def test_unoptimized_if_unconsumed(self):
25362523
]
25372524
self.check(insts, expected)
25382525

2539-
@unittest.expectedFailure # TODO: RUSTPYTHON; compiler IR pipeline
25402526
def test_unoptimized_if_support_killed(self):
25412527
insts = [
25422528
("LOAD_FAST", 0, 1),
@@ -2562,7 +2548,6 @@ def test_unoptimized_if_support_killed(self):
25622548
]
25632549
self.check(insts, insts)
25642550

2565-
@unittest.expectedFailure # TODO: RUSTPYTHON; compiler IR pipeline
25662551
def test_unoptimized_if_aliased(self):
25672552
insts = [
25682553
("LOAD_FAST", 0, 1),
@@ -2577,7 +2562,6 @@ def test_unoptimized_if_aliased(self):
25772562
]
25782563
self.check(insts, insts)
25792564

2580-
@unittest.expectedFailure # TODO: RUSTPYTHON; compiler IR pipeline
25812565
def test_consume_no_inputs(self):
25822566
insts = [
25832567
("LOAD_FAST", 0, 1),
@@ -2587,7 +2571,6 @@ def test_consume_no_inputs(self):
25872571
]
25882572
self.check(insts, insts)
25892573

2590-
@unittest.expectedFailure # TODO: RUSTPYTHON; compiler IR pipeline
25912574
def test_consume_some_inputs_no_outputs(self):
25922575
insts = [
25932576
("LOAD_FAST", 0, 1),
@@ -2596,7 +2579,6 @@ def test_consume_some_inputs_no_outputs(self):
25962579
]
25972580
self.check(insts, insts)
25982581

2599-
@unittest.expectedFailure # TODO: RUSTPYTHON; compiler IR pipeline
26002582
def test_check_exc_match(self):
26012583
insts = [
26022584
("LOAD_FAST", 0, 1),
@@ -2610,7 +2592,6 @@ def test_check_exc_match(self):
26102592
]
26112593
self.check(insts, expected)
26122594

2613-
@unittest.expectedFailure # TODO: RUSTPYTHON; compiler IR pipeline
26142595
def test_for_iter(self):
26152596
insts = [
26162597
("LOAD_FAST", 0, 1),
@@ -2626,7 +2607,6 @@ def test_for_iter(self):
26262607
]
26272608
self.cfg_optimization_test(insts, insts, consts=[None])
26282609

2629-
@unittest.expectedFailure # TODO: RUSTPYTHON; compiler IR pipeline
26302610
def test_load_attr(self):
26312611
insts = [
26322612
("LOAD_FAST", 0, 1),
@@ -2649,7 +2629,6 @@ def test_load_attr(self):
26492629
]
26502630
self.check(insts, expected)
26512631

2652-
@unittest.expectedFailure # TODO: RUSTPYTHON; compiler IR pipeline
26532632
def test_super_attr(self):
26542633
insts = [
26552634
("LOAD_FAST", 0, 1),
@@ -2680,7 +2659,6 @@ def test_super_attr(self):
26802659
]
26812660
self.check(insts, expected)
26822661

2683-
@unittest.expectedFailure # TODO: RUSTPYTHON; compiler IR pipeline
26842662
def test_send(self):
26852663
insts = [
26862664
("LOAD_FAST", 0, 1),
@@ -2704,7 +2682,6 @@ def test_send(self):
27042682
]
27052683
self.cfg_optimization_test(insts, expected, consts=[None])
27062684

2707-
@unittest.expectedFailure # TODO: RUSTPYTHON; compiler IR pipeline
27082685
def test_format_simple(self):
27092686
# FORMAT_SIMPLE will leave its operand on the stack if it's a unicode
27102687
# object. We treat it conservatively and assume that it always leaves
@@ -2728,7 +2705,6 @@ def test_format_simple(self):
27282705
]
27292706
self.check(insts, expected)
27302707

2731-
@unittest.expectedFailure # TODO: RUSTPYTHON; compiler IR pipeline
27322708
def test_set_function_attribute(self):
27332709
# SET_FUNCTION_ATTRIBUTE leaves the function on the stack
27342710
insts = [
@@ -2755,7 +2731,6 @@ def test_set_function_attribute(self):
27552731
]
27562732
self.cfg_optimization_test(insts, expected, consts=[None])
27572733

2758-
@unittest.expectedFailure # TODO: RUSTPYTHON; compiler IR pipeline
27592734
def test_get_yield_from_iter(self):
27602735
# GET_YIELD_FROM_ITER may leave its operand on the stack
27612736
insts = [
@@ -2774,15 +2749,13 @@ def test_get_yield_from_iter(self):
27742749
]
27752750
self.cfg_optimization_test(insts, insts, consts=[None])
27762751

2777-
@unittest.expectedFailure # TODO: RUSTPYTHON; compiler IR pipeline
27782752
def test_push_exc_info(self):
27792753
insts = [
27802754
("LOAD_FAST", 0, 1),
27812755
("PUSH_EXC_INFO", None, 2),
27822756
]
27832757
self.check(insts, insts)
27842758

2785-
@unittest.expectedFailure # TODO: RUSTPYTHON; compiler IR pipeline
27862759
def test_load_special(self):
27872760
# LOAD_SPECIAL may leave self on the stack
27882761
insts = [

0 commit comments

Comments
 (0)