Skip to content

Commit f5e5158

Browse files
cmaloneyclaude
andcommitted
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 <noreply@anthropic.com>
1 parent dc222c8 commit f5e5158

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

‎Lib/test/test_importlib/metadata/fixtures.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
import shutil
77
import sys
88
import textwrap
9+
import zipfile
910
from importlib import resources
11+
from importlib.metadata import FastPath
1012

1113
from test.support import import_helper
1214
from test.support import os_helper
@@ -365,11 +367,21 @@ def _fixture_on_path(self, filename):
365367
assert file.name.startswith('example'), file.name
366368
sys.path.insert(0, str(file))
367369
self.resources.callback(sys.path.pop, 0)
370+
self.resources.callback(self._close_cached_zip, str(file))
371+
372+
@staticmethod
373+
def _close_cached_zip(root):
374+
# TODO gh-162342: Remove once FastPath no longer leaks the zip file.
375+
zip_path = getattr(FastPath(root).joinpath, '__self__', None)
376+
if isinstance(zip_path, zipfile.Path):
377+
zip_path.root.close()
368378

369379
def setUp(self):
370380
# Add self.zip_name to the front of sys.path.
371381
self.resources = contextlib.ExitStack()
372382
self.addCleanup(self.resources.close)
383+
# Registered first so it runs last, after every zip has been closed.
384+
self.resources.callback(FastPath.__new__.cache_clear)
373385

374386

375387
def parameterize(*args_set):

0 commit comments

Comments
 (0)