Skip to content

gh-116731: Fix refleaks in importlib tests #116763

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 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions Lib/importlib/_bootstrap_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -1470,6 +1470,9 @@ def invalidate_caches():
# https://bugs.python.org/issue45703
_NamespacePath._epoch += 1

from importlib.metadata import MetadataPathFinder
MetadataPathFinder().invalidate_caches()
Comment on lines +1473 to +1474
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brettcannon I included you in the review for this section. Any concerns with adding this hook here?


@staticmethod
def _path_hooks(path):
"""Search sys.path_hooks for a finder for 'path'."""
Expand Down
7 changes: 7 additions & 0 deletions Lib/test/test_importlib/resources/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import unittest
import warnings
import importlib
import inspect
import contextlib

from importlib import resources
Expand Down Expand Up @@ -90,6 +91,12 @@ def test_module_resources(self):


class ImplicitContextFilesTests(SiteDir, unittest.TestCase):

def tearDown(self):
# clean up inspect state to avoid ref leaks (#116731)
inspect._filesbymodname.clear()
inspect.modulesbyfile.clear()

def test_implicit_files(self):
"""
Without any parameter, files() will infer the location as the caller.
Expand Down
4 changes: 4 additions & 0 deletions Lib/test/test_importlib/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pickle
import unittest
import warnings
import importlib
import importlib.metadata
import contextlib
from test.support import os_helper
Expand Down Expand Up @@ -37,6 +38,9 @@ def suppress_known_deprecation():
class BasicTests(fixtures.DistInfoPkg, unittest.TestCase):
version_pattern = r'\d+\.\d+(\.\d)?'

def tearDown(self):
importlib.invalidate_caches()

def test_retrieves_version_of_self(self):
dist = Distribution.from_name('distinfo-pkg')
assert isinstance(dist.version, str)
Expand Down
3 changes: 3 additions & 0 deletions Lib/test/test_importlib/test_metadata_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ class APITests(
):
version_pattern = r'\d+\.\d+(\.\d)?'

def tearDown(self):
importlib.invalidate_caches()

def test_retrieves_version_of_self(self):
pkg_version = version('egginfo-pkg')
assert isinstance(pkg_version, str)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed nuisance refleaks warnings in tests for importlib.metadata and
importlib.resources.