summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>2024-02-22 12:36:44 (GMT)
committerGitHub <noreply@github.com>2024-02-22 12:36:44 (GMT)
commit96c1737591b45bc1c528df1103eb0e500751fefe (patch)
tree8adb232e4843cfcae2a32d8cc48de73fb160a3d7 /Lib
parent8aa372edcd857d2fbec8da0cb993de15b4179308 (diff)
downloadcpython-96c1737591b45bc1c528df1103eb0e500751fefe.zip
cpython-96c1737591b45bc1c528df1103eb0e500751fefe.tar.gz
cpython-96c1737591b45bc1c528df1103eb0e500751fefe.tar.bz2
gh-115796: fix exception table construction in _testinternalcapi.assemble_code_object (#115797)
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_compiler_assemble.py41
1 files changed, 39 insertions, 2 deletions
diff --git a/Lib/test/test_compiler_assemble.py b/Lib/test/test_compiler_assemble.py
index 5696433..ab9f04d 100644
--- a/Lib/test/test_compiler_assemble.py
+++ b/Lib/test/test_compiler_assemble.py
@@ -1,3 +1,6 @@
+import dis
+import io
+import textwrap
import types
from test.support.bytecode_helper import AssemblerTestCase
@@ -22,11 +25,13 @@ class IsolatedAssembleTests(AssemblerTestCase):
metadata.setdefault('filename', filename)
return metadata
- def assemble_test(self, insts, metadata, expected):
+ def insts_to_code_object(self, insts, metadata):
metadata = self.complete_metadata(metadata)
insts = self.complete_insts_info(insts)
+ return self.get_code_object(metadata['filename'], insts, metadata)
- co = self.get_code_object(metadata['filename'], insts, metadata)
+ def assemble_test(self, insts, metadata, expected):
+ co = self.insts_to_code_object(insts, metadata)
self.assertIsInstance(co, types.CodeType)
expected_metadata = {}
@@ -108,3 +113,35 @@ class IsolatedAssembleTests(AssemblerTestCase):
expected = {(0,): 0, (1,): 1, (2,): 0, (120,): 0, (121,): 1}
self.assemble_test(instructions, metadata, expected)
+
+
+ def test_exception_table(self):
+ metadata = {
+ 'filename' : 'exc.py',
+ 'name' : 'exc',
+ 'consts' : {2 : 0},
+ }
+
+ # code for "try: pass\n except: pass"
+ insts = [
+ ('RESUME', 0),
+ ('SETUP_FINALLY', 3),
+ ('RETURN_CONST', 0),
+ ('SETUP_CLEANUP', 8),
+ ('PUSH_EXC_INFO', 0),
+ ('POP_TOP', 0),
+ ('POP_EXCEPT', 0),
+ ('RETURN_CONST', 0),
+ ('COPY', 3),
+ ('POP_EXCEPT', 0),
+ ('RERAISE', 1),
+ ]
+ co = self.insts_to_code_object(insts, metadata)
+ output = io.StringIO()
+ dis.dis(co, file=output)
+ exc_table = textwrap.dedent("""
+ ExceptionTable:
+ L1 to L2 -> L2 [0]
+ L2 to L3 -> L3 [1] lasti
+ """)
+ self.assertTrue(output.getvalue().endswith(exc_table))