From b38ffeadd644a405116a652f6afbab585f6dae5f Mon Sep 17 00:00:00 2001 From: sobolevn Date: Fri, 22 Sep 2023 15:10:52 +0300 Subject: [PATCH 1/2] gh-109721: Guard `_testinernalcapi` imports in tests --- Lib/test/test_cmd_line.py | 1 + Lib/test/test_import/__init__.py | 5 ++++- Lib/test/test_opcache.py | 7 ++++--- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py index e88b7c8572d9e8b..f4754dbf735a1d9 100644 --- a/Lib/test/test_cmd_line.py +++ b/Lib/test/test_cmd_line.py @@ -799,6 +799,7 @@ def check_pythonmalloc(self, env_var, name): self.assertEqual(proc.stdout.rstrip(), name) self.assertEqual(proc.returncode, 0) + @support.cpython_only def test_pythonmalloc(self): # Test the PYTHONMALLOC environment variable pymalloc = support.with_pymalloc() diff --git a/Lib/test/test_import/__init__.py b/Lib/test/test_import/__init__.py index a302a6075eca0a3..48553f9d48b0103 100644 --- a/Lib/test/test_import/__init__.py +++ b/Lib/test/test_import/__init__.py @@ -22,7 +22,6 @@ import types import unittest from unittest import mock -import _testinternalcapi import _imp from test.support import os_helper @@ -50,6 +49,10 @@ import _xxsubinterpreters as _interpreters except ModuleNotFoundError: _interpreters = None +try: + import _testinternalcapi +except ImportError: + _testinternalcapi = None skip_if_dont_write_bytecode = unittest.skipIf( diff --git a/Lib/test/test_opcache.py b/Lib/test/test_opcache.py index 692e03fbb5e0841..516953050849e46 100644 --- a/Lib/test/test_opcache.py +++ b/Lib/test/test_opcache.py @@ -4,13 +4,14 @@ import threading import types import unittest -from test.support import threading_helper -import _testinternalcapi +from test.support import threading_helper, import_helper + +# Skip this module on other interpreters, it is cpython specific: +_testinternalcapi = import_helper.import_module('_testinternalcapi') def disabling_optimizer(func): def wrapper(*args, **kwargs): - import _testinternalcapi old_opt = _testinternalcapi.get_optimizer() _testinternalcapi.set_optimizer(None) try: From c00a68bd037ac43d016a32e421c3cc66694e2e7f Mon Sep 17 00:00:00 2001 From: sobolevn Date: Fri, 22 Sep 2023 16:31:32 +0300 Subject: [PATCH 2/2] Try different approach with importing `_testinternalcapi` --- Lib/test/test_opcache.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_opcache.py b/Lib/test/test_opcache.py index 516953050849e46..2b2783d57be8f41 100644 --- a/Lib/test/test_opcache.py +++ b/Lib/test/test_opcache.py @@ -4,10 +4,13 @@ import threading import types import unittest -from test.support import threading_helper, import_helper +from test.support import threading_helper, check_impl_detail # Skip this module on other interpreters, it is cpython specific: -_testinternalcapi = import_helper.import_module('_testinternalcapi') +if check_impl_detail(cpython=False): + raise unittest.SkipTest('implementation detail specific to cpython') + +import _testinternalcapi def disabling_optimizer(func):