From 439e8cd8e57a71a022e89df478501d31cca22a59 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Thu, 25 Jun 2026 15:01:22 +0300 Subject: [PATCH 1/2] Don't require `_testcapi` for `test_code` --- Lib/test/test_code.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_code.py b/Lib/test/test_code.py index 3588872ed23ac49..af912e7210fb63b 100644 --- a/Lib/test/test_code.py +++ b/Lib/test/test_code.py @@ -215,7 +215,10 @@ from test.support import threading_helper, import_helper from test.support.bytecode_helper import instructions_with_positions from opcode import opmap, opname -from _testcapi import code_offset_to_line +try: + from _testcapi import code_offset_to_line +except ImportError: + code_offset_to_line = None try: import _testinternalcapi except ModuleNotFoundError: @@ -1490,6 +1493,7 @@ async def async_func(): rc, out, err = assert_python_ok('-OO', '-c', code) + @unittest.skipIf(code_offset_to_line is None, "requires _testcapi.code_offset_to_line") def test_co_branches(self): def get_line_branches(func): From c9f646b28baa608dfa3058a375bcd54b7e386782 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Thu, 25 Jun 2026 15:14:24 +0300 Subject: [PATCH 2/2] Fix CR --- Lib/test/test_code.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_code.py b/Lib/test/test_code.py index af912e7210fb63b..657e70a92f09b9d 100644 --- a/Lib/test/test_code.py +++ b/Lib/test/test_code.py @@ -215,10 +215,6 @@ from test.support import threading_helper, import_helper from test.support.bytecode_helper import instructions_with_positions from opcode import opmap, opname -try: - from _testcapi import code_offset_to_line -except ImportError: - code_offset_to_line = None try: import _testinternalcapi except ModuleNotFoundError: @@ -1493,8 +1489,9 @@ async def async_func(): rc, out, err = assert_python_ok('-OO', '-c', code) - @unittest.skipIf(code_offset_to_line is None, "requires _testcapi.code_offset_to_line") def test_co_branches(self): + _testcapi = import_helper.import_module("_testcapi") + code_offset_to_line = _testcapi.code_offset_to_line def get_line_branches(func): code = func.__code__