From f5e5158fcdcc04d1b0da4f125e00d877ef0cc47e Mon Sep 17 00:00:00 2001 From: Cody Maloney Date: Fri, 20 Nov 2026 18:07:51 -0800 Subject: [PATCH] gh-162342: Close cached zip files in importlib.metadata test fixtures Temporary workaround to get bots green until importlib.metadata cleans these up itself. FastPath keeps the fixture ZipFiles alive because of a cycle in its cache. That means they get closed on deallocation resulting in the unclosed file warning. Co-Authored-By: Claude Opus 5.5 --- Lib/test/test_importlib/metadata/fixtures.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Lib/test/test_importlib/metadata/fixtures.py b/Lib/test/test_importlib/metadata/fixtures.py index 79b2e1e33ad7ff4..f3e2003fb910b63 100644 --- a/Lib/test/test_importlib/metadata/fixtures.py +++ b/Lib/test/test_importlib/metadata/fixtures.py @@ -6,7 +6,9 @@ import shutil import sys import textwrap +import zipfile from importlib import resources +from importlib.metadata import FastPath from test.support import import_helper from test.support import os_helper @@ -365,11 +367,21 @@ def _fixture_on_path(self, filename): assert file.name.startswith('example'), file.name sys.path.insert(0, str(file)) self.resources.callback(sys.path.pop, 0) + self.resources.callback(self._close_cached_zip, str(file)) + + @staticmethod + def _close_cached_zip(root): + # TODO gh-162342: Remove once FastPath no longer leaks the zip file. + zip_path = getattr(FastPath(root).joinpath, '__self__', None) + if isinstance(zip_path, zipfile.Path): + zip_path.root.close() def setUp(self): # Add self.zip_name to the front of sys.path. self.resources = contextlib.ExitStack() self.addCleanup(self.resources.close) + # Registered first so it runs last, after every zip has been closed. + self.resources.callback(FastPath.__new__.cache_clear) def parameterize(*args_set):