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

Appearance settings

Commit aded70a

Browse filesBrowse files
gh-119064: Use os_helper.FakePath instead of pathlib.Path in tests
1 parent ee13797 commit aded70a
Copy full SHA for aded70a

19 files changed

+129
-126
lines changed

‎Lib/test/_test_multiprocessing.py

Copy file name to clipboardExpand all lines: Lib/test/_test_multiprocessing.py
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import subprocess
2323
import struct
2424
import operator
25-
import pathlib
2625
import pickle
2726
import weakref
2827
import warnings
@@ -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
for path in paths:
331331
self.set_executable(path)

‎Lib/test/test_asyncio/test_unix_events.py

Copy file name to clipboardExpand all lines: Lib/test/test_asyncio/test_unix_events.py
+5-6Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import multiprocessing
77
from multiprocessing.util import _cleanup_tests as multiprocessing_cleanup_tests
88
import os
9-
import pathlib
109
import signal
1110
import socket
1211
import stat
@@ -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-
def test_create_unix_server_pathlib(self):
306+
def test_create_unix_server_pathlike(self):
308307
with test_utils.unix_socket_path() as path:
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-
def test_create_unix_connection_pathlib(self):
314+
def test_create_unix_connection_pathlike(self):
316315
with test_utils.unix_socket_path() as path:
317-
path = pathlib.Path(path)
316+
path = os_helper.FakePath(path)
318317
coro = self.loop.create_unix_connection(lambda: None, path)
319318
with self.assertRaises(FileNotFoundError):
320-
# If pathlib.Path wasn't supported, the exception would be
319+
# If path-like object weren't supported, the exception would be
321320
# different.
322321
self.loop.run_until_complete(coro)
323322

‎Lib/test/test_compileall.py

Copy file name to clipboardExpand all lines: Lib/test/test_compileall.py
+13-13Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import importlib.util
55
import io
66
import os
7-
import pathlib
87
import py_compile
98
import shutil
109
import struct
@@ -31,6 +30,7 @@
3130
from test.support import script_helper
3231
from test.test_py_compile import without_source_date_epoch
3332
from test.test_py_compile import SourceDateEpochTestMeta
33+
from test.support.os_helper import FakePath
3434

3535

3636
def get_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
with support.captured_stdout() as stdout:
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
def test_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
def test_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
def test_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
def test_compile_dir_pathlike(self):
229229
self.assertFalse(os.path.isfile(self.bc_path))
230230
with support.captured_stdout() as stdout:
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
def test_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
def test_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

Copy file name to clipboardExpand all lines: Lib/test/test_configparser.py
+2-3Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import configparser
33
import io
44
import os
5-
import pathlib
65
import textwrap
76
import unittest
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

Copy file name to clipboardExpand all lines: Lib/test/test_ctypes/test_loading.py
+1-4Lines 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-
class CTypesTestPathLikeCls:
46-
def __fspath__(self):
47-
return test_lib
48-
CDLL(CTypesTestPathLikeCls())
45+
CDLL(os_helper.FakePath(test_lib))
4946
self.assertRaises(OSError, CDLL, self.unknowndll)
5047

5148
def test_load_version(self):

‎Lib/test/test_fileinput.py

Copy file name to clipboardExpand all lines: Lib/test/test_fileinput.py
+8-9Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@
2323

2424
from io import BytesIO, StringIO
2525
from fileinput import FileInput, hook_encoded
26-
from pathlib import Path
2726

2827
from test.support import verbose
29-
from test.support.os_helper import TESTFN
28+
from test.support.os_helper import TESTFN, FakePath
3029
from test.support.os_helper import unlink as safe_unlink
3130
from test.support import os_helper
3231
from test import support
@@ -478,23 +477,23 @@ def test_iteration_buffering(self):
478477
self.assertRaises(StopIteration, next, fi)
479478
self.assertEqual(src.linesread, [])
480479

481-
def test_pathlib_file(self):
482-
t1 = Path(self.writeTmp("Pathlib file."))
480+
def test_pathlike_file(self):
481+
t1 = FakePath(self.writeTmp("Path-like file."))
483482
with FileInput(t1, encoding="utf-8") as fi:
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-
def test_pathlib_file_inplace(self):
491-
t1 = Path(self.writeTmp('Pathlib file.'))
489+
def test_pathlike_file_inplace(self):
490+
t1 = FakePath(self.writeTmp('Path-like file.'))
492491
with FileInput(t1, inplace=True, encoding="utf-8") as fi:
493492
line = fi.readline()
494-
self.assertEqual(line, 'Pathlib file.')
493+
self.assertEqual(line, 'Path-like file.')
495494
print('Modified %s' % line)
496495
with open(t1, encoding="utf-8") as f:
497-
self.assertEqual(f.read(), 'Modified Pathlib file.\n')
496+
self.assertEqual(f.read(), 'Modified Path-like file.\n')
498497

499498

500499
class MockFileInput:

‎Lib/test/test_http_cookiejar.py

Copy file name to clipboardExpand all lines: Lib/test/test_http_cookiejar.py
+3-4Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import time
1010
import unittest
1111
import urllib.request
12-
import pathlib
1312

1413
from http.cookiejar import (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
def test_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
def test_constructor_with_none(self):
345344
c = LWPCookieJar(None)

‎Lib/test/test_logging.py

Copy file name to clipboardExpand all lines: Lib/test/test_logging.py
+3-3Lines 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-
def test_path_objects(self):
660+
def test_pathlike_objects(self):
661661
"""
662-
Test that Path objects are accepted as filename arguments to handlers.
662+
Test that path-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

Copy file name to clipboardExpand all lines: Lib/test/test_mimetypes.py
+10-3Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import io
22
import mimetypes
33
import os
4-
import pathlib
54
import sys
65
import unittest.mock
76

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

8483
with os_helper.temp_dir() as directory:
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+
with open(file, 'w', encoding="utf-8") as f:
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+
with open(file, 'w', encoding="utf-8") as f:
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

Copy file name to clipboardExpand all lines: Lib/test/test_pkgutil.py
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import zipfile
1414

1515
from test.support.import_helper import DirsOnSysPath
16+
from test.support.os_helper import FakePath
1617
from test.test_importlib.util import uncache
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-
for moduleinfo in pkgutil.iter_modules([Path(zip_file)]):
125+
for moduleinfo in pkgutil.iter_modules([FakePath(zip_file)]):
125126
self.assertIsInstance(moduleinfo, pkgutil.ModuleInfo)
126127
names.append(moduleinfo.name)
127128
self.assertEqual(names, [pkg])

‎Lib/test/test_runpy.py

Copy file name to clipboardExpand all lines: Lib/test/test_runpy.py
+6-7Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from test.support import (infinite_recursion, no_tracing, verbose,
1616
requires_subprocess, requires_resource)
1717
from test.support.import_helper import forget, make_legacy_pyc, unload
18-
from test.support.os_helper import create_empty_file, temp_dir
18+
from test.support.os_helper import create_empty_file, temp_dir, FakePath
1919
from test.support.script_helper import make_script, make_zip_script
2020

2121

@@ -657,14 +657,13 @@ def test_basic_script(self):
657657
self._check_script(script_name, "<run_path>", script_name,
658658
script_name, expect_spec=False)
659659

660-
def test_basic_script_with_path_object(self):
660+
def test_basic_script_with_pathlike_object(self):
661661
with temp_dir() as script_dir:
662662
mod_name = 'script'
663-
script_name = pathlib.Path(self._make_test_script(script_dir,
664-
mod_name))
665-
self._check_script(script_name, "<run_path>",
666-
os.fsdecode(script_name),
667-
os.fsdecode(script_name),
663+
script_name = self._make_test_script(script_dir, mod_name)
664+
self._check_script(FakePath(script_name), "<run_path>",
665+
script_name,
666+
script_name,
668667
expect_spec=False)
669668

670669
def test_basic_script_no_suffix(self):

‎Lib/test/test_shutil.py

Copy file name to clipboardExpand all lines: Lib/test/test_shutil.py
+4-4Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,7 @@ def _ignore(src, names):
914914
'test.txt')))
915915

916916
dst_dir = join(self.mkdtemp(), 'destination')
917-
shutil.copytree(pathlib.Path(src_dir), dst_dir, ignore=_ignore)
917+
shutil.copytree(FakePath(src_dir), dst_dir, ignore=_ignore)
918918
self.assertTrue(exists(join(dst_dir, 'test_dir', 'subdir',
919919
'test.txt')))
920920

@@ -2107,7 +2107,7 @@ def check_unpack_archive(self, format, **kwargs):
21072107
self.check_unpack_archive_with_converter(
21082108
format, lambda path: path, **kwargs)
21092109
self.check_unpack_archive_with_converter(
2110-
format, pathlib.Path, **kwargs)
2110+
format, FakePath, **kwargs)
21112111
self.check_unpack_archive_with_converter(format, FakePath, **kwargs)
21122112

21132113
def check_unpack_archive_with_converter(self, format, converter, **kwargs):
@@ -2672,12 +2672,12 @@ def test_move_file_to_dir(self):
26722672

26732673
def test_move_file_to_dir_pathlike_src(self):
26742674
# Move a pathlike file to another location on the same filesystem.
2675-
src = pathlib.Path(self.src_file)
2675+
src = FakePath(self.src_file)
26762676
self._check_move_file(src, self.dst_dir, self.dst_file)
26772677

26782678
def test_move_file_to_dir_pathlike_dst(self):
26792679
# Move a file to another pathlike location on the same filesystem.
2680-
dst = pathlib.Path(self.dst_dir)
2680+
dst = FakePath(self.dst_dir)
26812681
self._check_move_file(self.src_file, dst, self.dst_file)
26822682

26832683
@mock_rename

‎Lib/test/test_subprocess.py

Copy file name to clipboardExpand all lines: Lib/test/test_subprocess.py
+2-5Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import gc
2626
import textwrap
2727
import json
28-
import pathlib
2928
from test.support.os_helper import FakePath
3029

3130
try:
@@ -1522,17 +1521,15 @@ def test_communicate_epipe(self):
15221521
p.communicate(b"x" * 2**20)
15231522

15241523
def test_repr(self):
1525-
path_cmd = pathlib.Path("my-tool.py")
1526-
pathlib_cls = path_cmd.__class__.__name__
1527-
15281524
cases = [
15291525
("ls", True, 123, "<Popen: returncode: 123 args: 'ls'>"),
15301526
('a' * 100, True, 0,
15311527
"<Popen: returncode: 0 args: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...>"),
15321528
(["ls"], False, None, "<Popen: returncode: None args: ['ls']>"),
15331529
(["ls", '--my-opts', 'a' * 100], False, None,
15341530
"<Popen: returncode: None args: ['ls', '--my-opts', 'aaaaaaaaaaaaaaaaaaaaaaaa...>"),
1535-
(path_cmd, False, 7, f"<Popen: returncode: 7 args: {pathlib_cls}('my-tool.py')>")
1531+
(os_helper.FakePath("my-tool.py"), False, 7,
1532+
"<Popen: returncode: 7 args: <FakePath 'my-tool.py'>>")
15361533
]
15371534
with unittest.mock.patch.object(subprocess.Popen, '_execute_child'):
15381535
for cmd, shell, code, sx in cases:

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.