Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit98bbbbe

Browse files
gh-93616: Fix env changed issue in test_modulefinder (GH-93617)
(cherry picked from commitcffa4f7)Co-authored-by: Christian Heimes <christian@python.org>
1 parent47a7855 commit98bbbbe

File tree

2 files changed

+40
-37
lines changed

2 files changed

+40
-37
lines changed

‎Lib/test/test_modulefinder.py‎

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010

1111
importmodulefinder
1212

13-
TEST_DIR=tempfile.mkdtemp()
14-
TEST_PATH= [TEST_DIR,os.path.dirname(tempfile.__file__)]
15-
1613
# Each test description is a list of 5 items:
1714
#
1815
# 1. a module name that will be imported by modulefinder
@@ -23,9 +20,9 @@
2320
# about because they MAY be not found
2421
# 5. a string specifying packages to create; the format is obvious imo.
2522
#
26-
# Each package will be created inTEST_DIR, andTEST_DIR will be
23+
# Each package will be created intest_dir, andtest_dir will be
2724
# removed after the tests again.
28-
# Modulefinder searches in a path that containsTEST_DIR, plus
25+
# Modulefinder searches in a path that containstest_dir, plus
2926
# the standard Lib directory.
3027

3128
maybe_test= [
@@ -300,7 +297,7 @@ def open_file(path):
300297
returnopen(path,'wb')
301298

302299

303-
defcreate_package(source):
300+
defcreate_package(test_dir,source):
304301
ofi=None
305302
try:
306303
forlineinsource.splitlines():
@@ -313,41 +310,45 @@ def create_package(source):
313310
ofi.close()
314311
iftype(line)==bytes:
315312
line=line.decode('utf-8')
316-
ofi=open_file(os.path.join(TEST_DIR,line.strip()))
313+
ofi=open_file(os.path.join(test_dir,line.strip()))
317314
finally:
318315
ifofi:
319316
ofi.close()
320317

321318
classModuleFinderTest(unittest.TestCase):
319+
defsetUp(self):
320+
self.test_dir=tempfile.mkdtemp()
321+
self.test_path= [self.test_dir,os.path.dirname(tempfile.__file__)]
322+
323+
deftearDown(self):
324+
shutil.rmtree(self.test_dir)
325+
322326
def_do_test(self,info,report=False,debug=0,replace_paths=[],modulefinder_class=modulefinder.ModuleFinder):
323327
import_this,modules,missing,maybe_missing,source=info
324-
create_package(source)
325-
try:
326-
mf=modulefinder_class(path=TEST_PATH,debug=debug,
327-
replace_paths=replace_paths)
328-
mf.import_hook(import_this)
329-
ifreport:
330-
mf.report()
331-
## # This wouldn't work in general when executed several times:
332-
## opath = sys.path[:]
333-
## sys.path = TEST_PATH
334-
## try:
335-
## __import__(import_this)
336-
## except:
337-
## import traceback; traceback.print_exc()
338-
## sys.path = opath
339-
## return
340-
modules=sorted(set(modules))
341-
found=sorted(mf.modules)
342-
# check if we found what we expected, not more, not less
343-
self.assertEqual(found,modules)
344-
345-
# check for missing and maybe missing modules
346-
bad,maybe=mf.any_missing_maybe()
347-
self.assertEqual(bad,missing)
348-
self.assertEqual(maybe,maybe_missing)
349-
finally:
350-
shutil.rmtree(TEST_DIR)
328+
create_package(self.test_dir,source)
329+
mf=modulefinder_class(path=self.test_path,debug=debug,
330+
replace_paths=replace_paths)
331+
mf.import_hook(import_this)
332+
ifreport:
333+
mf.report()
334+
## # This wouldn't work in general when executed several times:
335+
## opath = sys.path[:]
336+
## sys.path = self.test_path
337+
## try:
338+
## __import__(import_this)
339+
## except:
340+
## import traceback; traceback.print_exc()
341+
## sys.path = opath
342+
## return
343+
modules=sorted(set(modules))
344+
found=sorted(mf.modules)
345+
# check if we found what we expected, not more, not less
346+
self.assertEqual(found,modules)
347+
348+
# check for missing and maybe missing modules
349+
bad,maybe=mf.any_missing_maybe()
350+
self.assertEqual(bad,missing)
351+
self.assertEqual(maybe,maybe_missing)
351352

352353
deftest_package(self):
353354
self._do_test(package_test)
@@ -380,7 +381,7 @@ def test_same_name_as_bad(self):
380381
self._do_test(same_name_as_bad_test)
381382

382383
deftest_bytecode(self):
383-
base_path=os.path.join(TEST_DIR,'a')
384+
base_path=os.path.join(self.test_dir,'a')
384385
source_path=base_path+importlib.machinery.SOURCE_SUFFIXES[0]
385386
bytecode_path=base_path+importlib.machinery.BYTECODE_SUFFIXES[0]
386387
withopen_file(source_path)asfile:
@@ -390,8 +391,8 @@ def test_bytecode(self):
390391
self._do_test(bytecode_test)
391392

392393
deftest_replace_paths(self):
393-
old_path=os.path.join(TEST_DIR,'a','module.py')
394-
new_path=os.path.join(TEST_DIR,'a','spam.py')
394+
old_path=os.path.join(self.test_dir,'a','module.py')
395+
new_path=os.path.join(self.test_dir,'a','spam.py')
395396
withsupport.captured_stdout()asoutput:
396397
self._do_test(maybe_test,debug=2,
397398
replace_paths=[(old_path,new_path)])
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
``test_modulefinder`` now creates a temporary directory in
2+
``ModuleFinderTest.setUp()`` instead of module scope.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp