8000 ghpython-116307: Proper fix for 'mod' leaking across importlib tests by jaraco · Pull Request #116678 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

ghpython-116307: Proper fix for 'mod' leaking across importlib tests #116678

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 35 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
9b3f0f7
gh-116303: Skip some sqlite3 tests if testcapi is unavailable
erlend-aasland Mar 4, 2024
0f6f74e
Skip more tests
erlend-aasland Mar 4, 2024
2f750c3
Split up sqlite3 tests
erlend-aasland Mar 4, 2024
7c4477d
Amend previous commit
erlend-aasland Mar 4, 2024
a9c6f62
Pull in main
erlend-aasland Mar 4, 2024
cdc6df4
Use import_helper iso. requires_limited_api
erlend-aasland Mar 5, 2024
d05dcd7
More skips
erlend-aasland Mar 5, 2024
cb7cff4
Fix gc and weakref tests
erlend-aasland Mar 5, 2024
ad5e3b6
Remove local debug stuff
erlend-aasland Mar 5, 2024
cdb8bf4
Fix test_os
erlend-aasland Mar 5, 2024
e4a30b0
Fixup stable_abi.py
erlend-aasland Mar 5, 2024
fabb007
Fixup run_in_subinterp
erlend-aasland Mar 5, 2024
c51dafa
8000 Fixup test_call
erlend-aasland Mar 5, 2024
c3f9c99
Fixup test_audit
erlend-aasland Mar 5, 2024
fde9548
Fix some _testsinglephase issues
erlend-aasland Mar 5, 2024
ea72ced
Address review: use setUpClass in test_call; don't skip everything
erlend-aasland Mar 5, 2024
628896c
Address review: no need to skip in tearDown
erlend-aasland Mar 5, 2024
88c6739
Address review: check import at the top of test_threading.test_frame_…
erlend-aasland Mar 5, 2024
c02cd6f
Fixup some test.support helpers
erlend-aasland Mar 5, 2024
d49532b
Fixup test_coroutines
erlend-aasland Mar 5, 2024
2427111
Fixup test_threading
erlend-aasland Mar 5, 2024
25d0999
Fixup test_repl
erlend-aasland Mar 5, 2024
bdd8cff
Fixup test_monitoring
erlend-aasland Mar 5, 2024
ee9fa51
Amend test_embed
erlend-aasland Mar 5, 2024
a47c5ff
Amend test_audit
erlend-aasland Mar 5, 2024
d84a5c4
Fix test_socket
erlend-aasland Mar 5, 2024
e877ffb
Resolve test_exceptions nicer
erlend-aasland Mar 5, 2024
1ec0c66
Merge branch 'main' into sqlite/testcapi
erlend-aasland Mar 5, 2024
afa58a9
Use import_helper in test_importlib
erlend-aasland Mar 5, 2024
760c6cb
Fixup test_embed
erlend-aasland Mar 5, 2024
d7f060a
Pull in main
erlend-aasland Mar 5, 2024
a1106b7
Revert spurious docs change
erlend-aasland Mar 5, 2024
db45852
Workaround: use a different test module name in test_module_resources
erlend-aasland Mar 5, 2024
bbb927a
gh-116307: Create a new import helper 'isolated modules' and use that…
jaraco Mar 6, 2024
369fcff
Merge branch 'sqlite/testcapi' of https://github.com/erlend-aasland/c…
jaraco Mar 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
More skips
  • Loading branch information
erlend-aasland committed Mar 5, 2024
commit d05dcd7e6c2ab19b62cea93cdc7bea84d658d9db
15 changes: 12 additions & 3 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1715,7 +1715,10 @@ def run_in_subinterp_with_config(code, *, own_gil=None, **config):
module is enabled.
"""
_check_tracemalloc()
import _testinternalcapi
try:
import _testinternalcapi
except ImportError:
raise SkipTest("requires _testinternalcapi")
if own_gil is not None:
assert 'gil' not in config, (own_gil, config)
config['gil'] = 2 if own_gil else 1
Expand Down Expand Up @@ -1877,12 +1880,18 @@ def restore(self):


def with_pymalloc():
import _testcapi
try:
import _testcapi
except ImportError:
raise unittest.SkipTest("requires _testcapi")
return _testcapi.WITH_PYMALLOC and not Py_GIL_DISABLED


def with_mimalloc():
import _testcapi
try:
import _testcapi
except ImportError:
raise unittest.SkipTest("requires _testcapi")
return _testcapi.WITH_MIMALLOC


Expand Down
4 changes: 1 addition & 3 deletions Lib/test/test_capi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import os
from test.support import load_package_tests, import_helper

import_helper.import_module("_testcapi")
from test.support import load_package_tests

def load_tests(*args):
return load_package_tests(os.path.dirname(__file__), *args)
2 changes: 1 addition & 1 deletion Lib/test/test_capi/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from collections import OrderedDict, UserDict
from types import MappingProxyType
from test import support
import _testcapi
_testcapi = support.import_helper.import_module("_testcapi")


NULL = None
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import gc
import os

import _testinternalcapi

from test.support import script_helper, requires_specialization
from test.support.import_helper import import_module
_testinternalcapi = import_module("_testinternalcapi")


@contextlib.contextmanager
Expand Down
4 changes: 3 additions & 1 deletion Lib/test/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import re
import test.support as support
import unittest
from test.support.import_helper import import_module

maxsize = support.MAX_Py_ssize_t

Expand Down Expand Up @@ -478,7 +479,8 @@ def test_precision(self):

@support.cpython_only
def test_precision_c_limits(self):
from _testcapi import INT_MAX
_testcapi = import_module("_testcapi")
INT_MAX = _testcapi.INT_MAX

f = 1.2
with self.assertRaises(ValueError) as cm:
Expand Down
4 changes: 3 additions & 1 deletion Lib/test/test_gc.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
try:
from _testcapi import with_tp_del
except ImportError:
_testcapi = None
def with_tp_del(cls):
class C(object):
def __new__(cls, *args, **kwargs):
raise TypeError('requires _testcapi.with_tp_del')
raise unittest.SkipTest('requires _testcapi.with_tp_del')
return C

try:
Expand Down Expand Up @@ -689,6 +690,7 @@ def do_work():

@cpython_only
@requires_subprocess()
@unittest.skipIf(_testcapi is None, "requires _testcapi")
def test_garbage_at_shutdown(self):
import subprocess
code = """if 1:
Expand Down
5 changes: 4 additions & 1 deletion Lib/test/test_genericclass.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import unittest
from test import support
from test.support.import_helper import import_module


class TestMROEntry(unittest.TestCase):
Expand Down Expand Up @@ -277,7 +278,9 @@ def __class_getitem__(cls, item):
class CAPITest(unittest.TestCase):

def test_c_class(self):
from _testcapi import Generic, GenericAlias
_testcapi = import_module("_testcapi")
Generic = _testcapi.Generic
GenericAlias = _testcapi.GenericAlias
self.assertIsInstance(Generic.__class_getitem__(int), GenericAlias)

IntGeneric = Generic[int]
Expand Down
12 changes: 8 additions & 4 deletions Lib/test/test_import/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
is_wasi, run_in_subinterp, run_in_subinterp_with_config, Py_TRACE_REFS)
from test.support.import_helper import (
forget, make_legacy_pyc, unlink, unload, ready_to_import,
DirsOnSysPath, CleanImport)
DirsOnSysPath, CleanImport, import_module)
from test.support.os_helper import (
TESTFN, rmtree, temp_umask, TESTFN_UNENCODABLE)
from test.support import script_helper
Expand Down Expand Up @@ -352,7 +352,7 @@ def test_from_import_missing_attr_has_name_and_path(self):

@cpython_only
def test_from_import_missing_attr_has_name_and_so_path(self):
import _testcapi
_testcapi = import_module("_testcapi")
with self.assertRaises(ImportError) as cm:
from _testcapi import i_dont_exist
self.assertEqual(cm.exception.name, '_testcapi')
Expand Down Expand Up @@ -1836,6 +1836,7 @@ def check_incompatible_fresh(self, name, *, isolated=False):
f'ImportError: module {name} does not support loading in subinterpreters',
)

@unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi")
def test_builtin_compat(self):
# For now we avoid using sys or builtins
# since they still don't implement multi-phase init.
Expand All @@ -1847,6 +1848,7 @@ def test_builtin_compat(self):
self.check_compatible_here(module, strict=True)

@cpython_only
@unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi")
def test_frozen_compat(self):
module = '_frozen_importlib'
require_frozen(module, skip=True)
Expand Down Expand Up @@ -1917,6 +1919,7 @@ def test_multi_init_extension_per_interpreter_gil_compat(self):
self.check_compatible_here(modname, filename,
strict=False, isolated=False)

@unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi")
def test_python_compat(self):
module = 'threading'
require_pure_python(module)
Expand Down Expand Up @@ -1962,6 +1965,7 @@ def check_incompatible(setting, override):
with self.subTest('config: check disabled; override: disabled'):
check_compatible(False, -1)

@unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi")
def test_isolated_config(self):
module = 'threading'
require_pure_python(module)
Expand Down Expand Up @@ -2691,7 +2695,7 @@ class CAPITests(unittest.TestCase):
def test_pyimport_addmodule(self):
# gh-105922: Test PyImport_AddModuleRef(), PyImport_AddModule()
# and PyImport_AddModuleObject()
import _testcapi
_testcapi = import_module("_testcapi")
for name in (
'sys', # frozen module
'test', # package
Expand All @@ -2701,7 +2705,7 @@ def test_pyimport_addmodule(self):

def test_pyimport_addmodule_create(self):
# gh-105922: Test PyImport_AddModuleRef(), create a new module
import _testcapi
_testcapi = import_module("_testcapi")
name = 'dontexist'
self.assertNotIn(name, sys.modules)
self.addCleanup(unload, name)
Expand Down
5 changes: 3 additions & 2 deletions Lib/test/test_monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import asyncio
from test import support
from test.support import requires_specialization, script_helper
from test.support.import_helper import import_module

PAIR = (0,1)

Expand Down Expand Up @@ -1811,14 +1812,14 @@ def test_gh108976(self):
class TestOptimizer(MonitoringTestBase, unittest.TestCase):

def setUp(self):
import _testinternalcapi
_testinternalcapi = import_module("_testinternalcapi")
self.old_opt = _testinternalcapi.get_optimizer()
opt = _testinternalcapi.new_counter_optimizer()
_testinternalcapi.set_optimizer(opt)
super(TestOptimizer, self).setUp()

def tearDown(self):
import _testinternalcapi
_testinternalcapi = import_module("_testinternalcapi")
super(TestOptimizer, self).tearDown()
_testinternalcapi.set_optimizer(self.old_opt)

Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_multibytecodec.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from test import support
from test.support import os_helper
from test.support.os_helper import TESTFN
from test.support.import_helper import import_module

ALL_CJKENCODINGS = [
# _codecs_cn
Expand Down Expand Up @@ -212,7 +213,7 @@ def test_issue5640(self):
@support.cpython_only
def test_subinterp(self):
# bpo-42846: Test a CJK codec in a subinterpreter
import _testcapi
_testcapi = import_module("_testcapi")
encoding = 'cp932'
text = "Python の開発は、1990 年ごろから開始されています。"
code = textwrap.dedent("""
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_opcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
import types
import unittest
from test.support import threading_helper, check_impl_detail, requires_specialization
from test.support.import_helper import import_module

# Skip this module on other interpreters, it is cpython specific:
if check_impl_detail(cpython=False):
raise unittest.SkipTest('implementation detail specific to cpython')

import _testinternalcapi
_testinternalcapi = import_module("_testinternalcapi")


def disabling_optimizer(func):
Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
try:
from _testcapi import INT_MAX, PY_SSIZE_T_MAX
except ImportError:
_testcapi = None
INT_MAX = PY_SSIZE_T_MAX = sys.maxsize

try:
Expand Down Expand Up @@ -5258,6 +5259,7 @@ def test_fork(self):

@unittest.skipUnless(sys.platform in ("linux", "darwin"),
"Only Linux and macOS detect this today.")
@unittest.skipIf(_testcapi is None, "requires _testcapi")
def test_fork_warns_when_non_python_thread_exists(self):
code = """if 1:
import os, threading, warnings
Expand Down
6 changes: 5 additions & 1 deletion Lib/test/test_perfmaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
import sys
import unittest

from _testinternalcapi import perf_map_state_teardown, write_perf_map_entry
try:
from _testinternalcapi import perf_map_state_teardown, write_perf_map_entry
except ImportError:
raise unittest.SkipTest("requires _testinternalcapi")


if sys.platform != 'linux':
raise unittest.SkipTest('Linux only')
Expand Down
5 changes: 4 additions & 1 deletion Lib/test/test_poll.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,10 @@ def test_poll3(self):

@cpython_only
def test_poll_c_limits(self):
from _testcapi import USHRT_MAX, INT_MAX, UINT_MAX
try:
from _testcapi import USHRT_MAX, INT_MAX, UINT_MAX
except ImportError:
raise unittest.SkipTest("requires _testcapi")
pollster = select.poll()
pollster.register(1)

Expand Down
8 changes: 8 additions & 0 deletions Lib/test/test_regrtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1724,6 +1724,10 @@ def test_other_bug(self):

@support.cpython_only
def test_uncollectable(self):
try:
import _testcapi
except ImportError:
raise unittest.SkipTest("requires _testcapi")
code = textwrap.dedent(r"""
import _testcapi
import gc
Expand Down Expand Up @@ -2106,6 +2110,10 @@ def test_unload_tests(self):

def check_add_python_opts(self, option):
# --fast-ci and --slow-ci add "-u -W default -bb -E" options to Python
try:
import _testinternalcapi
except ImportError:
raise unittest.SkipTest("requires _testinternalcapi")
code = textwrap.dedent(r"""
import sys
import unittest
Expand Down
20 changes: 10 additions & 10 deletions Lib/test/test_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from test.support import (
is_apple, os_helper, refleak_helper, socket_helper, threading_helper
)

from test.support.import_helper import import_module
import _thread as thread
import array
import contextlib
Expand Down Expand Up @@ -1171,7 +1171,7 @@ def testNtoH(self):

@support.cpython_only
def testNtoHErrors(self):
import _testcapi
_testcapi = import_module("_testcapi")
s_good_values = [0, 1, 2, 0xffff]
l_good_values = s_good_values + [0xffffffff]
l_bad_values = [-1, -2, 1<<32, 1<<1000]
Expand Down Expand Up @@ -1649,9 +1649,9 @@ def test_getaddrinfo_int_port_overflow(self):
# prior to 3.12 did for ints outside of a [LONG_MIN, LONG_MAX] range.
# Leave the error up to the underlying string based platform C API.

from _testcapi import ULONG_MAX, LONG_MAX, LONG_MIN
_testcapi = import_module("_testcapi")
try:
socket.getaddrinfo(None, ULONG_MAX + 1, type=socket.SOCK_STREAM)
socket.getaddrinfo(None, _testcapi.ULONG_MAX + 1, type=socket.SOCK_STREAM)
except OverflowError:
# Platforms differ as to what values consitute a getaddrinfo() error
# return. Some fail for LONG_MAX+1, others ULONG_MAX+1, and Windows
Expand All @@ -1661,21 +1661,21 @@ def test_getaddrinfo_int_port_overflow(self):
pass

try:
socket.getaddrinfo(None, LONG_MAX + 1, type=socket.SOCK_STREAM)
socket.getaddrinfo(None, _testcapi.LONG_MAX + 1, type=socket.SOCK_STREAM)
except OverflowError:
self.fail("Either no error or socket.gaierror expected.")
except socket.gaierror:
pass

try:
socket.getaddrinfo(None, LONG_MAX - 0xffff + 1, type=socket.SOCK_STREAM)
socket.getaddrinfo(None, _testcapi.LONG_MAX - 0xffff + 1, type=socket.SOCK_STREAM)
except OverflowError:
self.fail("Either no error or socket.gaierror expected.")
except socket.gaierror:
pass

try:
socket.getaddrinfo(None, LONG_MIN - 1, type=socket.SOCK_STREAM)
socket.getaddrinfo(None, _testcapi.LONG_MIN - 1, type=socket.SOCK_STREAM)
except OverflowError:
self.fail("Either no error or socket.gaierror expected.")
except socket.gaierror:
Expand Down Expand Up @@ -1831,7 +1831,7 @@ def test_listen_backlog(self):
@support.cpython_only
def test_listen_backlog_overflow(self):
# Issue 15989
import _testcapi
_testcapi = import_module("_testcapi")
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as srv:
srv.bind((HOST, 0))
self.assertRaises(OverflowError, srv.listen, _testcapi.INT_MAX + 1)
Expand Down Expand Up @@ -2727,7 +2727,7 @@ def _testShutdown(self):

@support.cpython_only
def _testShutdown_overflow(self):
import _testcapi
_testcapi = import_module("_testcapi")
self.serv_conn.send(MSG)
# Issue 15989
self.assertRaises(OverflowError, self.serv_conn.shutdown,
Expand Down Expand Up @@ -4884,7 +4884,7 @@ def _testSetBlocking(self):
@support.cpython_only
def testSetBlocking_overflow(self):
# Issue 15989
import _testcapi
_testcapi = import_module("_testcapi")
if _testcapi.UINT_MAX >= _testcapi.ULONG_MAX:
self.skipTest('needs UINT_MAX < ULONG_MAX')

Expand Down
Loading
0