Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 96c1737

Browse files
authored
gh-115796: fix exception table construction in _testinternalcapi.assemble_code_object (#115797)
1 parent 8aa372e commit 96c1737

File tree

3 files changed

+52
-8
lines changed

3 files changed

+52
-8
lines changed

Lib/test/test_compiler_assemble.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import dis
2+
import io
3+
import textwrap
14
import types
25

36
from test.support.bytecode_helper import AssemblerTestCase
@@ -22,11 +25,13 @@ def complete_metadata(self, metadata, filename="myfile.py"):
2225
metadata.setdefault('filename', filename)
2326
return metadata
2427

25-
def assemble_test(self, insts, metadata, expected):
28+
def insts_to_code_object(self, insts, metadata):
2629
metadata = self.complete_metadata(metadata)
2730
insts = self.complete_insts_info(insts)
31+
return self.get_code_object(metadata['filename'], insts, metadata)
2832

29-
co = self.get_code_object(metadata['filename'], insts, metadata)
33+
def assemble_test(self, insts, metadata, expected):
34+
co = self.insts_to_code_object(insts, metadata)
3035
self.assertIsInstance(co, types.CodeType)
3136

3237
expected_metadata = {}
@@ -108,3 +113,35 @@ def inner():
108113

109114
expected = {(0,): 0, (1,): 1, (2,): 0, (120,): 0, (121,): 1}
110115
self.assemble_test(instructions, metadata, expected)
116+
117+
118+
def test_exception_table(self):
119+
metadata = {
120+
'filename' : 'exc.py',
121+
'name' : 'exc',
122+
'consts' : {2 : 0},
123+
}
124+
125+
# code for "try: pass\n except: pass"
126+
insts = [
127+
('RESUME', 0),
128+
('SETUP_FINALLY', 3),
129+
('RETURN_CONST', 0),
130+
('SETUP_CLEANUP', 8),
131+
('PUSH_EXC_INFO', 0),
132+
('POP_TOP', 0),
133+
('POP_EXCEPT', 0),
134+
('RETURN_CONST', 0),
135+
('COPY', 3),
136+
('POP_EXCEPT', 0),
137+
('RERAISE', 1),
138+
]
139+
co = self.insts_to_code_object(insts, metadata)
140+
output = io.StringIO()
141+
dis.dis(co, file=output)
142+
exc_table = textwrap.dedent("""
143+
ExceptionTable:
144+
L1 to L2 -> L2 [0]
145+
L2 to L3 -> L3 [1] lasti
146+
""")
147+
self.assertTrue(output.getvalue().endswith(exc_table))
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Make '_testinternalcapi.assemble_code_object' construct the exception table
2+
for the code object.

Python/flowgraph.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -665,12 +665,6 @@ translate_jump_labels_to_targets(basicblock *entryblock)
665665
return SUCCESS;
666666
}
667667

668-
int
669-
_PyCfg_JumpLabelsToTargets(cfg_builder *g)
670-
{
671-
return translate_jump_labels_to_targets(g->g_entryblock);
672-
}
673-
674668
static int
675669
mark_except_handlers(basicblock *entryblock) {
676670
#ifndef NDEBUG
@@ -2790,3 +2784,14 @@ _PyCfg_OptimizedCfgToInstructionSequence(cfg_builder *g,
27902784

27912785
return SUCCESS;
27922786
}
2787+
2788+
/* This is used by _PyCompile_Assemble to fill in the jump and exception
2789+
* targets in a synthetic CFG (which is not the ouptut of the builtin compiler).
2790+
*/
2791+
int
2792+
_PyCfg_JumpLabelsToTargets(cfg_builder *g)
2793+
{
2794+
RETURN_IF_ERROR(translate_jump_labels_to_targets(g->g_entryblock));
2795+
RETURN_IF_ERROR(label_exception_targets(g->g_entryblock));
2796+
return SUCCESS;
2797+
}

0 commit comments

Comments
 (0)