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

Commit0152dc4

Browse files
gh-119064: Use os_helper.FakePath instead of pathlib.Path in tests (GH-119065)
1 parent0142a22 commit0152dc4

19 files changed

+115
-127
lines changed

‎Lib/test/_test_multiprocessing.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
importsubprocess
2323
importstruct
2424
importoperator
25-
importpathlib
2625
importpickle
2726
importweakref
2827
importwarnings
@@ -324,8 +323,9 @@ def test_set_executable(self):
324323
self.skipTest(f'test not appropriate for{self.TYPE}')
325324
paths= [
326325
sys.executable,# str
327-
sys.executable.encode(),# bytes
328-
pathlib.Path(sys.executable)# os.PathLike
326+
os.fsencode(sys.executable),# bytes
327+
os_helper.FakePath(sys.executable),# os.PathLike
328+
os_helper.FakePath(os.fsencode(sys.executable)),# os.PathLike bytes
329329
]
330330
forpathinpaths:
331331
self.set_executable(path)

‎Lib/test/test_asyncio/test_unix_events.py‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
importmultiprocessing
77
frommultiprocessing.utilimport_cleanup_testsasmultiprocessing_cleanup_tests
88
importos
9-
importpathlib
109
importsignal
1110
importsocket
1211
importstat
@@ -304,20 +303,20 @@ def test_create_unix_server_existing_path_sock(self):
304303
self.loop.run_until_complete(srv.wait_closed())
305304

306305
@socket_helper.skip_unless_bind_unix_socket
307-
deftest_create_unix_server_pathlib(self):
306+
deftest_create_unix_server_pathlike(self):
308307
withtest_utils.unix_socket_path()aspath:
309-
path=pathlib.Path(path)
308+
path=os_helper.FakePath(path)
310309
srv_coro=self.loop.create_unix_server(lambda:None,path)
311310
srv=self.loop.run_until_complete(srv_coro)
312311
srv.close()
313312
self.loop.run_until_complete(srv.wait_closed())
314313

315-
deftest_create_unix_connection_pathlib(self):
314+
deftest_create_unix_connection_pathlike(self):
316315
withtest_utils.unix_socket_path()aspath:
317-
path=pathlib.Path(path)
316+
path=os_helper.FakePath(path)
318317
coro=self.loop.create_unix_connection(lambda:None,path)
319318
withself.assertRaises(FileNotFoundError):
320-
# Ifpathlib.Path wasn't supported, the exception would be
319+
# Ifpath-like object weren't supported, the exception would be
321320
# different.
322321
self.loop.run_until_complete(coro)
323322

‎Lib/test/test_compileall.py‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
importimportlib.util
55
importio
66
importos
7-
importpathlib
87
importpy_compile
98
importshutil
109
importstruct
@@ -31,6 +30,7 @@
3130
fromtest.supportimportscript_helper
3231
fromtest.test_py_compileimportwithout_source_date_epoch
3332
fromtest.test_py_compileimportSourceDateEpochTestMeta
33+
fromtest.support.os_helperimportFakePath
3434

3535

3636
defget_pyc(script,opt):
@@ -156,28 +156,28 @@ def test_compile_file_pathlike(self):
156156
self.assertFalse(os.path.isfile(self.bc_path))
157157
# we should also test the output
158158
withsupport.captured_stdout()asstdout:
159-
self.assertTrue(compileall.compile_file(pathlib.Path(self.source_path)))
159+
self.assertTrue(compileall.compile_file(FakePath(self.source_path)))
160160
self.assertRegex(stdout.getvalue(),r'Compiling ([^WindowsPath|PosixPath].*)')
161161
self.assertTrue(os.path.isfile(self.bc_path))
162162

163163
deftest_compile_file_pathlike_ddir(self):
164164
self.assertFalse(os.path.isfile(self.bc_path))
165-
self.assertTrue(compileall.compile_file(pathlib.Path(self.source_path),
166-
ddir=pathlib.Path('ddir_path'),
165+
self.assertTrue(compileall.compile_file(FakePath(self.source_path),
166+
ddir=FakePath('ddir_path'),
167167
quiet=2))
168168
self.assertTrue(os.path.isfile(self.bc_path))
169169

170170
deftest_compile_file_pathlike_stripdir(self):
171171
self.assertFalse(os.path.isfile(self.bc_path))
172-
self.assertTrue(compileall.compile_file(pathlib.Path(self.source_path),
173-
stripdir=pathlib.Path('stripdir_path'),
172+
self.assertTrue(compileall.compile_file(FakePath(self.source_path),
173+
stripdir=FakePath('stripdir_path'),
174174
quiet=2))
175175
self.assertTrue(os.path.isfile(self.bc_path))
176176

177177
deftest_compile_file_pathlike_prependdir(self):
178178
self.assertFalse(os.path.isfile(self.bc_path))
179-
self.assertTrue(compileall.compile_file(pathlib.Path(self.source_path),
180-
prependdir=pathlib.Path('prependdir_path'),
179+
self.assertTrue(compileall.compile_file(FakePath(self.source_path),
180+
prependdir=FakePath('prependdir_path'),
181181
quiet=2))
182182
self.assertTrue(os.path.isfile(self.bc_path))
183183

@@ -228,22 +228,22 @@ def test_optimize(self):
228228
deftest_compile_dir_pathlike(self):
229229
self.assertFalse(os.path.isfile(self.bc_path))
230230
withsupport.captured_stdout()asstdout:
231-
compileall.compile_dir(pathlib.Path(self.directory))
231+
compileall.compile_dir(FakePath(self.directory))
232232
line=stdout.getvalue().splitlines()[0]
233233
self.assertRegex(line,r'Listing ([^WindowsPath|PosixPath].*)')
234234
self.assertTrue(os.path.isfile(self.bc_path))
235235

236236
deftest_compile_dir_pathlike_stripdir(self):
237237
self.assertFalse(os.path.isfile(self.bc_path))
238-
self.assertTrue(compileall.compile_dir(pathlib.Path(self.directory),
239-
stripdir=pathlib.Path('stripdir_path'),
238+
self.assertTrue(compileall.compile_dir(FakePath(self.directory),
239+
stripdir=FakePath('stripdir_path'),
240240
quiet=2))
241241
self.assertTrue(os.path.isfile(self.bc_path))
242242

243243
deftest_compile_dir_pathlike_prependdir(self):
244244
self.assertFalse(os.path.isfile(self.bc_path))
245-
self.assertTrue(compileall.compile_dir(pathlib.Path(self.directory),
246-
prependdir=pathlib.Path('prependdir_path'),
245+
self.assertTrue(compileall.compile_dir(FakePath(self.directory),
246+
prependdir=FakePath('prependdir_path'),
247247
quiet=2))
248248
self.assertTrue(os.path.isfile(self.bc_path))
249249

‎Lib/test/test_configparser.py‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
importconfigparser
33
importio
44
importos
5-
importpathlib
65
importtextwrap
76
importunittest
87

@@ -745,12 +744,12 @@ def test_read_returns_file_list(self):
745744
self.assertEqual(cf.get("Foo Bar","foo"),"newbar")
746745
# check when we pass only a Path object:
747746
cf=self.newconfig()
748-
parsed_files=cf.read(pathlib.Path(file1),encoding="utf-8")
747+
parsed_files=cf.read(os_helper.FakePath(file1),encoding="utf-8")
749748
self.assertEqual(parsed_files, [file1])
750749
self.assertEqual(cf.get("Foo Bar","foo"),"newbar")
751750
# check when we passed both a filename and a Path object:
752751
cf=self.newconfig()
753-
parsed_files=cf.read([pathlib.Path(file1),file1],encoding="utf-8")
752+
parsed_files=cf.read([os_helper.FakePath(file1),file1],encoding="utf-8")
754753
self.assertEqual(parsed_files, [file1,file1])
755754
self.assertEqual(cf.get("Foo Bar","foo"),"newbar")
756755
# check when we pass only missing files:

‎Lib/test/test_ctypes/test_loading.py‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@ def test_load(self):
4242
self.skipTest('could not find library to load')
4343
CDLL(test_lib)
4444
CDLL(os.path.basename(test_lib))
45-
classCTypesTestPathLikeCls:
46-
def__fspath__(self):
47-
returntest_lib
48-
CDLL(CTypesTestPathLikeCls())
45+
CDLL(os_helper.FakePath(test_lib))
4946
self.assertRaises(OSError,CDLL,self.unknowndll)
5047

5148
deftest_load_version(self):

‎Lib/test/test_fileinput.py‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@
2323

2424
fromioimportBytesIO,StringIO
2525
fromfileinputimportFileInput,hook_encoded
26-
frompathlibimportPath
2726

2827
fromtest.supportimportverbose
29-
fromtest.support.os_helperimportTESTFN
28+
fromtest.support.os_helperimportTESTFN,FakePath
3029
fromtest.support.os_helperimportunlinkassafe_unlink
3130
fromtest.supportimportos_helper
3231
fromtestimportsupport
@@ -478,23 +477,23 @@ def test_iteration_buffering(self):
478477
self.assertRaises(StopIteration,next,fi)
479478
self.assertEqual(src.linesread, [])
480479

481-
deftest_pathlib_file(self):
482-
t1=Path(self.writeTmp("Pathlib file."))
480+
deftest_pathlike_file(self):
481+
t1=FakePath(self.writeTmp("Path-like file."))
483482
withFileInput(t1,encoding="utf-8")asfi:
484483
line=fi.readline()
485-
self.assertEqual(line,'Pathlib file.')
484+
self.assertEqual(line,'Path-like file.')
486485
self.assertEqual(fi.lineno(),1)
487486
self.assertEqual(fi.filelineno(),1)
488487
self.assertEqual(fi.filename(),os.fspath(t1))
489488

490-
deftest_pathlib_file_inplace(self):
491-
t1=Path(self.writeTmp('Pathlib file.'))
489+
deftest_pathlike_file_inplace(self):
490+
t1=FakePath(self.writeTmp('Path-like file.'))
492491
withFileInput(t1,inplace=True,encoding="utf-8")asfi:
493492
line=fi.readline()
494-
self.assertEqual(line,'Pathlib file.')
493+
self.assertEqual(line,'Path-like file.')
495494
print('Modified %s'%line)
496495
withopen(t1,encoding="utf-8")asf:
497-
self.assertEqual(f.read(),'ModifiedPathlib file.\n')
496+
self.assertEqual(f.read(),'ModifiedPath-like file.\n')
498497

499498

500499
classMockFileInput:

‎Lib/test/test_http_cookiejar.py‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
importtime
1010
importunittest
1111
importurllib.request
12-
importpathlib
1312

1413
fromhttp.cookiejarimport (time2isoz,http2time,iso2time,time2netscape,
1514
parse_ns_headers,join_header_words,split_header_words,Cookie,
@@ -337,9 +336,9 @@ def test_constructor_with_str(self):
337336
self.assertEqual(c.filename,filename)
338337

339338
deftest_constructor_with_path_like(self):
340-
filename=pathlib.Path(os_helper.TESTFN)
341-
c=LWPCookieJar(filename)
342-
self.assertEqual(c.filename,os.fspath(filename))
339+
filename=os_helper.TESTFN
340+
c=LWPCookieJar(os_helper.FakePath(filename))
341+
self.assertEqual(c.filename,filename)
343342

344343
deftest_constructor_with_none(self):
345344
c=LWPCookieJar(None)

‎Lib/test/test_logging.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -657,15 +657,15 @@ def test_builtin_handlers(self):
657657
self.assertFalse(h.shouldFlush(r))
658658
h.close()
659659

660-
deftest_path_objects(self):
660+
deftest_pathlike_objects(self):
661661
"""
662-
Test thatPath objects are accepted as filename arguments to handlers.
662+
Test thatpath-like objects are accepted as filename arguments to handlers.
663663
664664
See Issue #27493.
665665
"""
666666
fn=make_temp_file()
667667
os.unlink(fn)
668-
pfn=pathlib.Path(fn)
668+
pfn=os_helper.FakePath(fn)
669669
cases= (
670670
(logging.FileHandler, (pfn,'w')),
671671
(logging.handlers.RotatingFileHandler, (pfn,'a')),

‎Lib/test/test_mimetypes.py‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
importio
22
importmimetypes
33
importos
4-
importpathlib
54
importsys
65
importunittest.mock
76

@@ -83,11 +82,19 @@ def test_read_mime_types(self):
8382

8483
withos_helper.temp_dir()asdirectory:
8584
data="x-application/x-unittest pyunit\n"
86-
file=pathlib.Path(directory,"sample.mimetype")
87-
file.write_text(data,encoding="utf-8")
85+
file=os.path.join(directory,"sample.mimetype")
86+
withopen(file,'w',encoding="utf-8")asf:
87+
f.write(data)
8888
mime_dict=mimetypes.read_mime_types(file)
8989
eq(mime_dict[".pyunit"],"x-application/x-unittest")
9090

91+
data="x-application/x-unittest2 pyunit2\n"
92+
file=os.path.join(directory,"sample2.mimetype")
93+
withopen(file,'w',encoding="utf-8")asf:
94+
f.write(data)
95+
mime_dict=mimetypes.read_mime_types(os_helper.FakePath(file))
96+
eq(mime_dict[".pyunit2"],"x-application/x-unittest2")
97+
9198
# bpo-41048: read_mime_types should read the rule file with 'utf-8' encoding.
9299
# Not with locale encoding. _bootlocale has been imported because io.open(...)
93100
# uses it.

‎Lib/test/test_pkgutil.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
importzipfile
1414

1515
fromtest.support.import_helperimportDirsOnSysPath
16+
fromtest.support.os_helperimportFakePath
1617
fromtest.test_importlib.utilimportuncache
1718

1819
# Note: pkgutil.walk_packages is currently tested in test_runpy. This is
@@ -121,7 +122,7 @@ def test_issue44061_iter_modules(self):
121122

122123
# make sure iter_modules accepts Path objects
123124
names= []
124-
formoduleinfoinpkgutil.iter_modules([Path(zip_file)]):
125+
formoduleinfoinpkgutil.iter_modules([FakePath(zip_file)]):
125126
self.assertIsInstance(moduleinfo,pkgutil.ModuleInfo)
126127
names.append(moduleinfo.name)
127128
self.assertEqual(names, [pkg])

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp