https://github.com/python/cpython/commit/e8fb67377b4cb62eabc36b1d178365d61402f3b8
commit: e8fb67377b4cb62eabc36b1d178365d61402f3b8
branch: main
author: Cody Maloney <[email protected]>
committer: cmaloney <[email protected]>
date: 2026-11-20T21:28:01-08:00
summary:

gh-162342: Close cached zip files in importlib.metadata test fixtures (#162352)

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 <[email protected]>

files:
M Lib/test/test_importlib/metadata/fixtures.py

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):

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to