Skip to content

Commit faa4640

Browse files
Fix the test on Windows
A loaded extension module cannot be removed on Windows, so the temporary directory is now removed with ignore_errors. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent cb57ba8 commit faa4640

1 file changed

Lines changed: 20 additions & 17 deletions

File tree

Lib/test/test_import/__init__.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import stat
2121
import subprocess
2222
import sys
23+
import tempfile
2324
import textwrap
2425
import threading
2526
import time
@@ -1272,23 +1273,25 @@ def test_import_from_undecodable_path(self):
12721273
origin = _testsinglephase.__file__
12731274
# The module is cached by its path, so restore it afterwards.
12741275
self.addCleanup(restore__testsinglephase)
1275-
with os_helper.temp_dir() as tempdir:
1276-
subdir = os.path.join(os.fsencode(tempdir),
1277-
os_helper.TESTFN_UNDECODABLE)
1278-
try:
1279-
os.mkdir(subdir)
1280-
except OSError:
1281-
self.skipTest('undecodable paths are not supported')
1282-
path = os.path.join(subdir, os.fsencode(os.path.basename(origin)))
1283-
shutil.copyfile(origin, path)
1284-
path = os.fsdecode(path)
1285-
spec = importlib.util.spec_from_file_location('_testsinglephase',
1286-
path)
1287-
module = importlib.util.module_from_spec(spec)
1288-
spec.loader.exec_module(module)
1289-
self.assertEqual(module.__name__, '_testsinglephase')
1290-
self.assertEqual(module.__file__, path)
1291-
_testinternalcapi.clear_extension('_testsinglephase', path)
1276+
tempdir = tempfile.mkdtemp()
1277+
# The copied extension module stays loaded, so on Windows it cannot
1278+
# be removed.
1279+
self.addCleanup(shutil.rmtree, tempdir, ignore_errors=True)
1280+
subdir = os.path.join(os.fsencode(tempdir),
1281+
os_helper.TESTFN_UNDECODABLE)
1282+
try:
1283+
os.mkdir(subdir)
1284+
except OSError:
1285+
self.skipTest('undecodable paths are not supported')
1286+
path = os.path.join(subdir, os.fsencode(os.path.basename(origin)))
1287+
shutil.copyfile(origin, path)
1288+
path = os.fsdecode(path)
1289+
spec = importlib.util.spec_from_file_location('_testsinglephase', path)
1290+
module = importlib.util.module_from_spec(spec)
1291+
spec.loader.exec_module(module)
1292+
self.assertEqual(module.__name__, '_testsinglephase')
1293+
self.assertEqual(module.__file__, path)
1294+
_testinternalcapi.clear_extension('_testsinglephase', path)
12921295

12931296
def test_create_builtin(self):
12941297
class Spec:

0 commit comments

Comments
 (0)