From a03b58490d19cda22c044531e5600e1779cf361a Mon Sep 17 00:00:00 2001 From: barneygale Date: Sat, 16 Dec 2023 22:17:22 +0000 Subject: [PATCH 1/5] GH-110109: pathlib ABCs: do not vary path syntax by host OS. Change the value of `pathlib._abc.PurePathBase.pathmod` from `os.path` to `posixpath`. User subclasses of `PurePathBase` and `PathBase` previously used the host OS's path syntax, e.g. backslashes as separators on Windows. This is wrong in most use cases, and likely to catch developers out unless they test on both Windows and non-Windows machines. In this patch we change the default to POSIX syntax, regardless of OS. This is somewhat arguable (why not make all aspects of syntax abstract and individually configurable?) but an improvement all the same. This change has no effect on `PurePath`, `Path`, nor their subclasses. Only private APIs are affected. --- Lib/pathlib/__init__.py | 1 + Lib/pathlib/_abc.py | 3 +- Lib/test/test_pathlib/test_pathlib.py | 43 +++++++++++++++++++++++ Lib/test/test_pathlib/test_pathlib_abc.py | 40 ++++----------------- 4 files changed, 51 insertions(+), 36 deletions(-) diff --git a/Lib/pathlib/__init__.py b/Lib/pathlib/__init__.py index b020d2db350da8..7f18946513e5a0 100644 --- a/Lib/pathlib/__init__.py +++ b/Lib/pathlib/__init__.py @@ -59,6 +59,7 @@ class PurePath(_abc.PurePathBase): # path. It's set when `__hash__()` is called for the first time. '_hash', ) + pathmod = os.path def __new__(cls, *args, **kwargs): """Construct a PurePath from one or several strings and or existing diff --git a/Lib/pathlib/_abc.py b/Lib/pathlib/_abc.py index 4808d0e61f7038..ea6121d8070ac8 100644 --- a/Lib/pathlib/_abc.py +++ b/Lib/pathlib/_abc.py @@ -1,7 +1,6 @@ import functools import io import ntpath -import os import posixpath import sys import warnings @@ -204,7 +203,7 @@ class PurePathBase: # work from occurring when `resolve()` calls `stat()` or `readlink()`. '_resolving', ) - pathmod = os.path + pathmod = posixpath def __init__(self, *paths): self._raw_paths = paths diff --git a/Lib/test/test_pathlib/test_pathlib.py b/Lib/test/test_pathlib/test_pathlib.py index 304786fc6f7fd9..7c10b0d74cca27 100644 --- a/Lib/test/test_pathlib/test_pathlib.py +++ b/Lib/test/test_pathlib/test_pathlib.py @@ -2,8 +2,10 @@ import os import sys import errno +import ntpath import pathlib import pickle +import posixpath import socket import stat import tempfile @@ -44,6 +46,47 @@ class PurePathTest(test_pathlib_abc.DummyPurePathTest): cls = pathlib.PurePath + def test_concrete_class(self): + if self.cls is pathlib.PurePath: + expected = pathlib.PureWindowsPath if os.name == 'nt' else pathlib.PurePosixPath + else: + expected = self.cls + p = self.cls('a') + self.assertIs(type(p), expected) + + def test_concrete_pathmod(self): + if self.cls is pathlib.PurePosixPath: + expected = posixpath + elif self.cls is pathlib.PureWindowsPath: + expected = ntpath + else: + expected = os.path + p = self.cls('a') + self.assertIs(p.pathmod, expected) + + def test_different_pathmods_unequal(self): + p = self.cls('a') + if p.pathmod is posixpath: + q = pathlib.PureWindowsPath('a') + else: + q = pathlib.PurePosixPath('a') + self.assertNotEqual(p, q) + + def test_different_pathmods_unordered(self): + p = self.cls('a') + if p.pathmod is posixpath: + q = pathlib.PureWindowsPath('a') + else: + q = pathlib.PurePosixPath('a') + with self.assertRaises(TypeError): + p < q + with self.assertRaises(TypeError): + p <= q + with self.assertRaises(TypeError): + p > q + with self.assertRaises(TypeError): + p >= q + def test_constructor_nested(self): P = self.cls P(FakePath("a/b/c")) diff --git a/Lib/test/test_pathlib/test_pathlib_abc.py b/Lib/test/test_pathlib/test_pathlib_abc.py index 61ed3cb6a4a7f8..9a9cc896b59f16 100644 --- a/Lib/test/test_pathlib/test_pathlib_abc.py +++ b/Lib/test/test_pathlib/test_pathlib_abc.py @@ -48,6 +48,9 @@ def test_magic_methods(self): self.assertIs(P.__gt__, object.__gt__) self.assertIs(P.__ge__, object.__ge__) + def test_pathmod(self): + self.assertIs(self.cls.pathmod, posixpath) + class DummyPurePath(pathlib._abc.PurePathBase): def __eq__(self, other): @@ -93,37 +96,6 @@ def test_constructor_common(self): P('a/b/c') P('/a/b/c') - def test_concrete_class(self): - if self.cls is pathlib.PurePath: - expected = pathlib.PureWindowsPath if os.name == 'nt' else pathlib.PurePosixPath - else: - expected = self.cls - p = self.cls('a') - self.assertIs(type(p), expected) - - def test_different_pathmods_unequal(self): - p = self.cls('a') - if p.pathmod is posixpath: - q = pathlib.PureWindowsPath('a') - else: - q = pathlib.PurePosixPath('a') - self.assertNotEqual(p, q) - - def test_different_pathmods_unordered(self): - p = self.cls('a') - if p.pathmod is posixpath: - q = pathlib.PureWindowsPath('a') - else: - q = pathlib.PurePosixPath('a') - with self.assertRaises(TypeError): - p < q - with self.assertRaises(TypeError): - p <= q - with self.assertRaises(TypeError): - p > q - with self.assertRaises(TypeError): - p >= q - def _check_str_subclass(self, *args): # Issue #21127: it should be possible to construct a PurePath object # from a str subclass instance, and it then gets converted to @@ -1420,7 +1392,7 @@ def test_resolve_common(self): self._check_resolve_relative(p, P(BASE, 'dirB', 'fileB', 'foo', 'in', 'spam'), False) p = P(BASE, 'dirA', 'linkC', '..', 'foo', 'in', 'spam') - if os.name == 'nt' and isinstance(p, pathlib.Path): + if self.cls.pathmod is not posixpath: # In Windows, if linkY points to dirB, 'dirA\linkY\..' # resolves to 'dirA' without resolving linkY first. self._check_resolve_relative(p, P(BASE, 'dirA', 'foo', 'in', @@ -1440,7 +1412,7 @@ def test_resolve_common(self): self._check_resolve_relative(p, P(BASE, 'dirB', 'foo', 'in', 'spam'), False) p = P(BASE, 'dirA', 'linkX', 'linkY', '..', 'foo', 'in', 'spam') - if os.name == 'nt' and isinstance(p, pathlib.Path): + if self.cls.pathmod is not posixpath: # In Windows, if linkY points to dirB, 'dirA\linkY\..' # resolves to 'dirA' without resolving linkY first. self._check_resolve_relative(p, P(d, 'foo', 'in', 'spam'), False) @@ -1473,7 +1445,7 @@ def _check_symlink_loop(self, *args): def test_resolve_loop(self): if not self.can_symlink: self.skipTest("symlinks required") - if os.name == 'nt' and issubclass(self.cls, pathlib.Path): + if self.cls.pathmod is not posixpath: self.skipTest("symlink loops work differently with concrete Windows paths") # Loops with relative symlinks. self.cls(BASE, 'linkX').symlink_to('linkX/inside') From f4facbbd4ba0240c3794b95affefece303466ffd Mon Sep 17 00:00:00 2001 From: barneygale Date: Sat, 16 Dec 2023 23:03:24 +0000 Subject: [PATCH 2/5] Don't use system working directory in ABC tests --- Lib/test/test_pathlib/test_pathlib.py | 167 +++++++------ Lib/test/test_pathlib/test_pathlib_abc.py | 284 +++++++++++----------- 2 files changed, 223 insertions(+), 228 deletions(-) diff --git a/Lib/test/test_pathlib/test_pathlib.py b/Lib/test/test_pathlib/test_pathlib.py index 7c10b0d74cca27..4af07adba7de50 100644 --- a/Lib/test/test_pathlib/test_pathlib.py +++ b/Lib/test/test_pathlib/test_pathlib.py @@ -26,10 +26,6 @@ # Make sure any symbolic links in the base test path are resolved. -BASE = os.path.realpath(TESTFN) -join = lambda *x: os.path.join(BASE, *x) -rel_join = lambda *x: os.path.join(TESTFN, *x) - only_nt = unittest.skipIf(os.name != 'nt', 'test requires a Windows-compatible system') only_posix = unittest.skipIf(os.name == 'nt', @@ -45,6 +41,7 @@ class PurePathTest(test_pathlib_abc.DummyPurePathTest): cls = pathlib.PurePath + base = os.path.realpath(TESTFN) def test_concrete_class(self): if self.cls is pathlib.PurePath: @@ -980,11 +977,11 @@ class PathTest(test_pathlib_abc.DummyPathTest, PurePathTest): def setUp(self): super().setUp() - os.chmod(join('dirE'), 0) + os.chmod(self.pathmod.join(self.base, 'dirE'), 0) def tearDown(self): - os.chmod(join('dirE'), 0o777) - os_helper.rmtree(BASE) + os.chmod(self.pathmod.join(self.base, 'dirE'), 0o777) + os_helper.rmtree(self.base) def tempdir(self): d = os_helper._longpath(tempfile.mkdtemp(suffix='-dirD', @@ -1021,23 +1018,23 @@ def test_absolute_common(self): P = self.cls with mock.patch("os.getcwd") as getcwd: - getcwd.return_value = BASE + getcwd.return_value = self.base # Simple relative paths. - self.assertEqual(str(P().absolute()), BASE) - self.assertEqual(str(P('.').absolute()), BASE) - self.assertEqual(str(P('a').absolute()), os.path.join(BASE, 'a')) - self.assertEqual(str(P('a', 'b', 'c').absolute()), os.path.join(BASE, 'a', 'b', 'c')) + self.assertEqual(str(P().absolute()), self.base) + self.assertEqual(str(P('.').absolute()), self.base) + self.assertEqual(str(P('a').absolute()), os.path.join(self.base, 'a')) + self.assertEqual(str(P('a', 'b', 'c').absolute()), os.path.join(self.base, 'a', 'b', 'c')) # Symlinks should not be resolved. - self.assertEqual(str(P('linkB', 'fileB').absolute()), os.path.join(BASE, 'linkB', 'fileB')) - self.assertEqual(str(P('brokenLink').absolute()), os.path.join(BASE, 'brokenLink')) - self.assertEqual(str(P('brokenLinkLoop').absolute()), os.path.join(BASE, 'brokenLinkLoop')) + self.assertEqual(str(P('linkB', 'fileB').absolute()), os.path.join(self.base, 'linkB', 'fileB')) + self.assertEqual(str(P('brokenLink').absolute()), os.path.join(self.base, 'brokenLink')) + self.assertEqual(str(P('brokenLinkLoop').absolute()), os.path.join(self.base, 'brokenLinkLoop')) # '..' entries should be preserved and not normalised. - self.assertEqual(str(P('..').absolute()), os.path.join(BASE, '..')) - self.assertEqual(str(P('a', '..').absolute()), os.path.join(BASE, 'a', '..')) - self.assertEqual(str(P('..', 'b').absolute()), os.path.join(BASE, '..', 'b')) + self.assertEqual(str(P('..').absolute()), os.path.join(self.base, '..')) + self.assertEqual(str(P('a', '..').absolute()), os.path.join(self.base, 'a', '..')) + self.assertEqual(str(P('..', 'b').absolute()), os.path.join(self.base, '..', 'b')) def _test_home(self, p): q = self.cls(os.path.expanduser('~')) @@ -1054,11 +1051,11 @@ def test_home(self): self._test_home(self.cls.home()) env.clear() - env['USERPROFILE'] = os.path.join(BASE, 'userprofile') + env['USERPROFILE'] = os.path.join(self.base, 'userprofile') self._test_home(self.cls.home()) # bpo-38883: ignore `HOME` when set on windows - env['HOME'] = os.path.join(BASE, 'home') + env['HOME'] = os.path.join(self.base, 'home') self._test_home(self.cls.home()) @unittest.skipIf(is_wasi, "WASI has no user accounts.") @@ -1085,7 +1082,7 @@ def __init__(self, *pathsegments, session_id): def with_segments(self, *pathsegments): return type(self)(*pathsegments, session_id=self.session_id) - p = P(BASE, session_id=42) + p = P(self.base, session_id=42) self.assertEqual(42, p.absolute().session_id) self.assertEqual(42, p.resolve().session_id) if not is_wasi: # WASI has no user accounts. @@ -1104,7 +1101,7 @@ def with_segments(self, *pathsegments): self.assertEqual(42, dirpath.session_id) def test_open_unbuffered(self): - p = self.cls(BASE) + p = self.cls(self.base) with (p / 'fileA').open('rb', buffering=0) as f: self.assertIsInstance(f, io.RawIOBase) self.assertEqual(f.read().strip(), b"this is file A") @@ -1113,15 +1110,15 @@ def test_resolve_nonexist_relative_issue38671(self): p = self.cls('non', 'exist') old_cwd = os.getcwd() - os.chdir(BASE) + os.chdir(self.base) try: - self.assertEqual(p.resolve(), self.cls(BASE, p)) + self.assertEqual(p.resolve(), self.cls(self.base, p)) finally: os.chdir(old_cwd) @os_helper.skip_unless_working_chmod def test_chmod(self): - p = self.cls(BASE) / 'fileA' + p = self.cls(self.base) / 'fileA' mode = p.stat().st_mode # Clear writable bit. new_mode = mode & ~0o222 @@ -1136,7 +1133,7 @@ def test_chmod(self): @only_posix @os_helper.skip_unless_working_chmod def test_chmod_follow_symlinks_true(self): - p = self.cls(BASE) / 'linkA' + p = self.cls(self.base) / 'linkA' q = p.resolve() mode = q.stat().st_mode # Clear writable bit. @@ -1159,7 +1156,7 @@ def _get_pw_name_or_skip_test(self, uid): @unittest.skipUnless(pwd, "the pwd module is needed for this test") def test_owner(self): - p = self.cls(BASE) / 'fileA' + p = self.cls(self.base) / 'fileA' expected_uid = p.stat().st_uid expected_name = self._get_pw_name_or_skip_test(expected_uid) @@ -1172,8 +1169,8 @@ def test_owner_no_follow_symlinks(self): if len(all_users) < 2: self.skipTest("test needs more than one user") - target = self.cls(BASE) / 'fileA' - link = self.cls(BASE) / 'linkA' + target = self.cls(self.base) / 'fileA' + link = self.cls(self.base) / 'linkA' uid_1, uid_2 = all_users[:2] os.chown(target, uid_1, -1) @@ -1194,7 +1191,7 @@ def _get_gr_name_or_skip_test(self, gid): @unittest.skipUnless(grp, "the grp module is needed for this test") def test_group(self): - p = self.cls(BASE) / 'fileA' + p = self.cls(self.base) / 'fileA' expected_gid = p.stat().st_gid expected_name = self._get_gr_name_or_skip_test(expected_gid) @@ -1207,8 +1204,8 @@ def test_group_no_follow_symlinks(self): if len(all_groups) < 2: self.skipTest("test needs more than one group") - target = self.cls(BASE) / 'fileA' - link = self.cls(BASE) / 'linkA' + target = self.cls(self.base) / 'fileA' + link = self.cls(self.base) / 'linkA' gid_1, gid_2 = all_groups[:2] os.chown(target, -1, gid_1) @@ -1221,18 +1218,18 @@ def test_group_no_follow_symlinks(self): self.assertEqual(expected_name, link.group(follow_symlinks=False)) def test_unlink(self): - p = self.cls(BASE) / 'fileA' + p = self.cls(self.base) / 'fileA' p.unlink() self.assertFileNotFound(p.stat) self.assertFileNotFound(p.unlink) def test_unlink_missing_ok(self): - p = self.cls(BASE) / 'fileAAA' + p = self.cls(self.base) / 'fileAAA' self.assertFileNotFound(p.unlink) p.unlink(missing_ok=True) def test_rmdir(self): - p = self.cls(BASE) / 'dirA' + p = self.cls(self.base) / 'dirA' for q in p.iterdir(): q.unlink() p.rmdir() @@ -1241,7 +1238,7 @@ def test_rmdir(self): @unittest.skipUnless(hasattr(os, "link"), "os.link() is not present") def test_hardlink_to(self): - P = self.cls(BASE) + P = self.cls(self.base) target = P / 'fileA' size = target.stat().st_size # linking to another path. @@ -1252,14 +1249,14 @@ def test_hardlink_to(self): self.assertTrue(target.exists()) # Linking to a str of a relative path. link2 = P / 'dirA' / 'fileAAA' - target2 = rel_join('fileA') + target2 = self.pathmod.join(self.base, 'fileA') link2.hardlink_to(target2) self.assertEqual(os.stat(target2).st_size, size) self.assertTrue(link2.exists()) @unittest.skipIf(hasattr(os, "link"), "os.link() is present") def test_hardlink_to_unsupported(self): - P = self.cls(BASE) + P = self.cls(self.base) p = P / 'fileA' # linking to another path. q = P / 'dirA' / 'fileAA' @@ -1267,7 +1264,7 @@ def test_hardlink_to_unsupported(self): q.hardlink_to(p) def test_rename(self): - P = self.cls(BASE) + P = self.cls(self.base) p = P / 'fileA' size = p.stat().st_size # Renaming to another path. @@ -1277,14 +1274,14 @@ def test_rename(self): self.assertEqual(q.stat().st_size, size) self.assertFileNotFound(p.stat) # Renaming to a str of a relative path. - r = rel_join('fileAAA') + r = self.pathmod.join(self.base, 'fileAAA') renamed_q = q.rename(r) self.assertEqual(renamed_q, self.cls(r)) self.assertEqual(os.stat(r).st_size, size) self.assertFileNotFound(q.stat) def test_replace(self): - P = self.cls(BASE) + P = self.cls(self.base) p = P / 'fileA' size = p.stat().st_size # Replacing a non-existing path. @@ -1294,14 +1291,14 @@ def test_replace(self): self.assertEqual(q.stat().st_size, size) self.assertFileNotFound(p.stat) # Replacing another (existing) path. - r = rel_join('dirB', 'fileB') + r = self.pathmod.join(self.base, 'dirB', 'fileB') replaced_q = q.replace(r) self.assertEqual(replaced_q, self.cls(r)) self.assertEqual(os.stat(r).st_size, size) self.assertFileNotFound(q.stat) def test_touch_common(self): - P = self.cls(BASE) + P = self.cls(self.base) p = P / 'newfileA' self.assertFalse(p.exists()) p.touch() @@ -1325,14 +1322,14 @@ def test_touch_common(self): self.assertRaises(OSError, p.touch, exist_ok=False) def test_touch_nochange(self): - P = self.cls(BASE) + P = self.cls(self.base) p = P / 'fileA' p.touch() with p.open('rb') as f: self.assertEqual(f.read().strip(), b"this is file A") def test_mkdir(self): - P = self.cls(BASE) + P = self.cls(self.base) p = P / 'newdirA' self.assertFalse(p.exists()) p.mkdir() @@ -1344,7 +1341,7 @@ def test_mkdir(self): def test_mkdir_parents(self): # Creating a chain of directories. - p = self.cls(BASE, 'newdirB', 'newdirC') + p = self.cls(self.base, 'newdirB', 'newdirC') self.assertFalse(p.exists()) with self.assertRaises(OSError) as cm: p.mkdir() @@ -1357,7 +1354,7 @@ def test_mkdir_parents(self): self.assertEqual(cm.exception.errno, errno.EEXIST) # Test `mode` arg. mode = stat.S_IMODE(p.stat().st_mode) # Default mode. - p = self.cls(BASE, 'newdirD', 'newdirE') + p = self.cls(self.base, 'newdirD', 'newdirE') p.mkdir(0o555, parents=True) self.assertTrue(p.exists()) self.assertTrue(p.is_dir()) @@ -1368,7 +1365,7 @@ def test_mkdir_parents(self): self.assertEqual(stat.S_IMODE(p.parent.stat().st_mode), mode) def test_mkdir_exist_ok(self): - p = self.cls(BASE, 'dirB') + p = self.cls(self.base, 'dirB') st_ctime_first = p.stat().st_ctime self.assertTrue(p.exists()) self.assertTrue(p.is_dir()) @@ -1380,7 +1377,7 @@ def test_mkdir_exist_ok(self): self.assertEqual(p.stat().st_ctime, st_ctime_first) def test_mkdir_exist_ok_with_parent(self): - p = self.cls(BASE, 'dirC') + p = self.cls(self.base, 'dirC') self.assertTrue(p.exists()) with self.assertRaises(FileExistsError) as cm: p.mkdir() @@ -1414,7 +1411,7 @@ def test_mkdir_with_unknown_drive(self): (p / 'child' / 'path').mkdir(parents=True) def test_mkdir_with_child_file(self): - p = self.cls(BASE, 'dirB', 'fileB') + p = self.cls(self.base, 'dirB', 'fileB') self.assertTrue(p.exists()) # An exception is raised when the last path component is an existing # regular file, regardless of whether exist_ok is true or not. @@ -1426,7 +1423,7 @@ def test_mkdir_with_child_file(self): self.assertEqual(cm.exception.errno, errno.EEXIST) def test_mkdir_no_parents_file(self): - p = self.cls(BASE, 'fileA') + p = self.cls(self.base, 'fileA') self.assertTrue(p.exists()) # An exception is raised when the last path component is an existing # regular file, regardless of whether exist_ok is true or not. @@ -1439,7 +1436,7 @@ def test_mkdir_no_parents_file(self): def test_mkdir_concurrent_parent_creation(self): for pattern_num in range(32): - p = self.cls(BASE, 'dirCPC%d' % pattern_num) + p = self.cls(self.base, 'dirCPC%d' % pattern_num) self.assertFalse(p.exists()) real_mkdir = os.mkdir @@ -1470,7 +1467,7 @@ def my_mkdir(path, mode=0o777): def test_symlink_to(self): if not self.can_symlink: self.skipTest("symlinks required") - P = self.cls(BASE) + P = self.cls(self.base) target = P / 'fileA' # Symlinking a path target. link = P / 'dirA' / 'linkAA' @@ -1494,7 +1491,7 @@ def test_symlink_to(self): @unittest.skipIf(hasattr(os, "symlink"), "os.symlink() is present") def test_symlink_to_unsupported(self): - P = self.cls(BASE) + P = self.cls(self.base) p = P / 'fileA' # linking to another path. q = P / 'dirA' / 'fileAA' @@ -1502,7 +1499,7 @@ def test_symlink_to_unsupported(self): q.symlink_to(p) def test_is_junction(self): - P = self.cls(BASE) + P = self.cls(self.base) with mock.patch.object(P.pathmod, 'isjunction'): self.assertEqual(P.is_junction(), P.pathmod.isjunction.return_value) @@ -1512,7 +1509,7 @@ def test_is_junction(self): @unittest.skipIf(sys.platform == "vxworks", "fifo requires special path on VxWorks") def test_is_fifo_true(self): - P = self.cls(BASE, 'myfifo') + P = self.cls(self.base, 'myfifo') try: os.mkfifo(str(P)) except PermissionError as e: @@ -1520,8 +1517,8 @@ def test_is_fifo_true(self): self.assertTrue(P.is_fifo()) self.assertFalse(P.is_socket()) self.assertFalse(P.is_file()) - self.assertIs(self.cls(BASE, 'myfifo\udfff').is_fifo(), False) - self.assertIs(self.cls(BASE, 'myfifo\x00').is_fifo(), False) + self.assertIs(self.cls(self.base, 'myfifo\udfff').is_fifo(), False) + self.assertIs(self.cls(self.base, 'myfifo\x00').is_fifo(), False) @unittest.skipUnless(hasattr(socket, "AF_UNIX"), "Unix sockets required") @unittest.skipIf( @@ -1531,7 +1528,7 @@ def test_is_fifo_true(self): is_wasi, "Cannot create socket on WASI." ) def test_is_socket_true(self): - P = self.cls(BASE, 'mysock') + P = self.cls(self.base, 'mysock') sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) self.addCleanup(sock.close) try: @@ -1543,8 +1540,8 @@ def test_is_socket_true(self): self.assertTrue(P.is_socket()) self.assertFalse(P.is_fifo()) self.assertFalse(P.is_file()) - self.assertIs(self.cls(BASE, 'mysock\udfff').is_socket(), False) - self.assertIs(self.cls(BASE, 'mysock\x00').is_socket(), False) + self.assertIs(self.cls(self.base, 'mysock\udfff').is_socket(), False) + self.assertIs(self.cls(self.base, 'mysock\x00').is_socket(), False) def test_is_char_device_true(self): # Under Unix, /dev/null should generally be a char device. @@ -1616,7 +1613,7 @@ def test_walk_bad_dir(self): def test_walk_many_open_files(self): depth = 30 - base = self.cls(BASE, 'deep') + base = self.cls(self.base, 'deep') path = self.cls(base, *(['d']*depth)) path.mkdir(parents=True) @@ -1658,15 +1655,15 @@ def test_absolute(self): def test_open_mode(self): old_mask = os.umask(0) self.addCleanup(os.umask, old_mask) - p = self.cls(BASE) + p = self.cls(self.base) with (p / 'new_file').open('wb'): pass - st = os.stat(join('new_file')) + st = os.stat(self.pathmod.join(self.base, 'new_file')) self.assertEqual(stat.S_IMODE(st.st_mode), 0o666) os.umask(0o022) with (p / 'other_new_file').open('wb'): pass - st = os.stat(join('other_new_file')) + st = os.stat(self.pathmod.join(self.base, 'other_new_file')) self.assertEqual(stat.S_IMODE(st.st_mode), 0o644) def test_resolve_root(self): @@ -1685,31 +1682,31 @@ def test_resolve_root(self): def test_touch_mode(self): old_mask = os.umask(0) self.addCleanup(os.umask, old_mask) - p = self.cls(BASE) + p = self.cls(self.base) (p / 'new_file').touch() - st = os.stat(join('new_file')) + st = os.stat(self.pathmod.join(self.base, 'new_file')) self.assertEqual(stat.S_IMODE(st.st_mode), 0o666) os.umask(0o022) (p / 'other_new_file').touch() - st = os.stat(join('other_new_file')) + st = os.stat(self.pathmod.join(self.base, 'other_new_file')) self.assertEqual(stat.S_IMODE(st.st_mode), 0o644) (p / 'masked_new_file').touch(mode=0o750) - st = os.stat(join('masked_new_file')) + st = os.stat(self.pathmod.join(self.base, 'masked_new_file')) self.assertEqual(stat.S_IMODE(st.st_mode), 0o750) def test_glob(self): P = self.cls - p = P(BASE) + p = P(self.base) given = set(p.glob("FILEa")) - expect = set() if not os_helper.fs_is_case_insensitive(BASE) else given + expect = set() if not os_helper.fs_is_case_insensitive(self.base) else given self.assertEqual(given, expect) self.assertEqual(set(p.glob("FILEa*")), set()) def test_rglob(self): P = self.cls - p = P(BASE, "dirC") + p = P(self.base, "dirC") given = set(p.rglob("FILEd")) - expect = set() if not os_helper.fs_is_case_insensitive(BASE) else given + expect = set() if not os_helper.fs_is_case_insensitive(self.base) else given self.assertEqual(given, expect) self.assertEqual(set(p.rglob("FILEd*")), set()) @@ -1840,17 +1837,17 @@ def test_absolute(self): self.assertEqual(str(P('a', 'b', 'c').absolute()), os.path.join(share, 'a', 'b', 'c')) - drive = os.path.splitdrive(BASE)[0] - with os_helper.change_cwd(BASE): + drive = os.path.splitdrive(self.base)[0] + with os_helper.change_cwd(self.base): # Relative path with root self.assertEqual(str(P('\\').absolute()), drive + '\\') self.assertEqual(str(P('\\foo').absolute()), drive + '\\foo') # Relative path on current drive - self.assertEqual(str(P(drive).absolute()), BASE) - self.assertEqual(str(P(drive + 'foo').absolute()), os.path.join(BASE, 'foo')) + self.assertEqual(str(P(drive).absolute()), self.base) + self.assertEqual(str(P(drive + 'foo').absolute()), os.path.join(self.base, 'foo')) - with os_helper.subst_drive(BASE) as other_drive: + with os_helper.subst_drive(self.base) as other_drive: # Set the working directory on the substitute drive saved_cwd = os.getcwd() other_cwd = f'{other_drive}\\dirA' @@ -1863,18 +1860,18 @@ def test_absolute(self): def test_glob(self): P = self.cls - p = P(BASE) - self.assertEqual(set(p.glob("FILEa")), { P(BASE, "fileA") }) - self.assertEqual(set(p.glob("*a\\")), { P(BASE, "dirA/") }) - self.assertEqual(set(p.glob("F*a")), { P(BASE, "fileA") }) + p = P(self.base) + self.assertEqual(set(p.glob("FILEa")), { P(self.base, "fileA") }) + self.assertEqual(set(p.glob("*a\\")), { P(self.base, "dirA/") }) + self.assertEqual(set(p.glob("F*a")), { P(self.base, "fileA") }) self.assertEqual(set(map(str, p.glob("FILEa"))), {f"{p}\\fileA"}) self.assertEqual(set(map(str, p.glob("F*a"))), {f"{p}\\fileA"}) def test_rglob(self): P = self.cls - p = P(BASE, "dirC") - self.assertEqual(set(p.rglob("FILEd")), { P(BASE, "dirC/dirD/fileD") }) - self.assertEqual(set(p.rglob("*\\")), { P(BASE, "dirC/dirD/") }) + p = P(self.base, "dirC") + self.assertEqual(set(p.rglob("FILEd")), { P(self.base, "dirC/dirD/fileD") }) + self.assertEqual(set(p.rglob("*\\")), { P(self.base, "dirC/dirD/") }) self.assertEqual(set(map(str, p.rglob("FILEd"))), {f"{p}\\dirD\\fileD"}) def test_expanduser(self): diff --git a/Lib/test/test_pathlib/test_pathlib_abc.py b/Lib/test/test_pathlib/test_pathlib_abc.py index 9a9cc896b59f16..c6bd9e21eca6ec 100644 --- a/Lib/test/test_pathlib/test_pathlib_abc.py +++ b/Lib/test/test_pathlib/test_pathlib_abc.py @@ -18,16 +18,6 @@ def test_is_notimplemented(self): self.assertTrue(isinstance(pathlib.UnsupportedOperation(), NotImplementedError)) -# Make sure any symbolic links in the base test path are resolved. -BASE = os.path.realpath(TESTFN) -join = lambda *x: os.path.join(BASE, *x) - -only_nt = unittest.skipIf(os.name != 'nt', - 'test requires a Windows-compatible system') -only_posix = unittest.skipIf(os.name == 'nt', - 'test requires a POSIX-compatible system') - - # # Tests for the pure classes. # @@ -64,6 +54,7 @@ def __hash__(self): class DummyPurePathTest(unittest.TestCase): cls = DummyPurePath + base = f'/BASE/{TESTFN}' # Keys are canonical paths, values are list of tuples of arguments # supposed to produce equal paths. @@ -854,7 +845,7 @@ class DummyPathTest(DummyPurePathTest): def setUp(self): super().setUp() pathmod = self.cls.pathmod - p = self.cls(BASE) + p = self.cls(self.base) p.mkdir(parents=True) p.joinpath('dirA').mkdir() p.joinpath('dirB').mkdir() @@ -886,7 +877,7 @@ def tearDown(self): cls._symlinks.clear() def tempdir(self): - path = self.cls(BASE).with_name('tmp-dirD') + path = self.cls(self.base).with_name('tmp-dirD') path.mkdir() return path @@ -896,11 +887,13 @@ def assertFileNotFound(self, func, *args, **kwargs): self.assertEqual(cm.exception.errno, errno.ENOENT) def assertEqualNormCase(self, path_a, path_b): - self.assertEqual(os.path.normcase(path_a), os.path.normcase(path_b)) + normcase = self.pathmod.normcase + self.assertEqual(normcase(path_a), normcase(path_b)) def test_samefile(self): - fileA_path = os.path.join(BASE, 'fileA') - fileB_path = os.path.join(BASE, 'dirB', 'fileB') + pathmod = self.pathmod + fileA_path = pathmod.join(self.base, 'fileA') + fileB_path = pathmod.join(self.base, 'dirB', 'fileB') p = self.cls(fileA_path) pp = self.cls(fileA_path) q = self.cls(fileB_path) @@ -909,7 +902,7 @@ def test_samefile(self): self.assertFalse(p.samefile(fileB_path)) self.assertFalse(p.samefile(q)) # Test the non-existent file case - non_existent = os.path.join(BASE, 'foo') + non_existent = pathmod.join(self.base, 'foo') r = self.cls(non_existent) self.assertRaises(FileNotFoundError, p.samefile, r) self.assertRaises(FileNotFoundError, p.samefile, non_existent) @@ -925,7 +918,7 @@ def test_empty_path(self): def test_exists(self): P = self.cls - p = P(BASE) + p = P(self.base) self.assertIs(True, p.exists()) self.assertIs(True, (p / 'dirA').exists()) self.assertIs(True, (p / 'fileA').exists()) @@ -939,11 +932,11 @@ def test_exists(self): self.assertIs(True, (p / 'brokenLink').exists(follow_symlinks=False)) self.assertIs(False, (p / 'foo').exists()) self.assertIs(False, P('/xyzzy').exists()) - self.assertIs(False, P(BASE + '\udfff').exists()) - self.assertIs(False, P(BASE + '\x00').exists()) + self.assertIs(False, P(self.base + '\udfff').exists()) + self.assertIs(False, P(self.base + '\x00').exists()) def test_open_common(self): - p = self.cls(BASE) + p = self.cls(self.base) with (p / 'fileA').open('r') as f: self.assertIsInstance(f, io.TextIOBase) self.assertEqual(f.read(), "this is file A\n") @@ -952,7 +945,7 @@ def test_open_common(self): self.assertEqual(f.read().strip(), b"this is file A") def test_read_write_bytes(self): - p = self.cls(BASE) + p = self.cls(self.base) (p / 'fileA').write_bytes(b'abcdefg') self.assertEqual((p / 'fileA').read_bytes(), b'abcdefg') # Check that trying to write str does not truncate the file. @@ -960,7 +953,7 @@ def test_read_write_bytes(self): self.assertEqual((p / 'fileA').read_bytes(), b'abcdefg') def test_read_write_text(self): - p = self.cls(BASE) + p = self.cls(self.base) (p / 'fileA').write_text('äbcdefg', encoding='latin-1') self.assertEqual((p / 'fileA').read_text( encoding='utf-8', errors='ignore'), 'bcdefg') @@ -969,7 +962,7 @@ def test_read_write_text(self): self.assertEqual((p / 'fileA').read_text(encoding='latin-1'), 'äbcdefg') def test_read_text_with_newlines(self): - p = self.cls(BASE) + p = self.cls(self.base) # Check that `\n` character change nothing (p / 'fileA').write_bytes(b'abcde\r\nfghlk\n\rmnopq') self.assertEqual((p / 'fileA').read_text(newline='\n'), @@ -984,7 +977,7 @@ def test_read_text_with_newlines(self): 'abcde\r\nfghlk\n\rmnopq') def test_write_text_with_newlines(self): - p = self.cls(BASE) + p = self.cls(self.base) # Check that `\n` character change nothing (p / 'fileA').write_text('abcde\r\nfghlk\n\rmnopq', newline='\n') self.assertEqual((p / 'fileA').read_bytes(), @@ -1005,27 +998,27 @@ def test_write_text_with_newlines(self): def test_iterdir(self): P = self.cls - p = P(BASE) + p = P(self.base) it = p.iterdir() paths = set(it) expected = ['dirA', 'dirB', 'dirC', 'dirE', 'fileA'] if self.can_symlink: expected += ['linkA', 'linkB', 'brokenLink', 'brokenLinkLoop'] - self.assertEqual(paths, { P(BASE, q) for q in expected }) + self.assertEqual(paths, { P(self.base, q) for q in expected }) def test_iterdir_symlink(self): if not self.can_symlink: self.skipTest("symlinks required") # __iter__ on a symlink to a directory. P = self.cls - p = P(BASE, 'linkB') + p = P(self.base, 'linkB') paths = set(p.iterdir()) - expected = { P(BASE, 'linkB', q) for q in ['fileB', 'linkD'] } + expected = { P(self.base, 'linkB', q) for q in ['fileB', 'linkD'] } self.assertEqual(paths, expected) def test_iterdir_nodir(self): # __iter__ on something that is not a directory. - p = self.cls(BASE, 'fileA') + p = self.cls(self.base, 'fileA') with self.assertRaises(OSError) as cm: p.iterdir() # ENOENT or EINVAL under Windows, ENOTDIR otherwise @@ -1035,9 +1028,9 @@ def test_iterdir_nodir(self): def test_glob_common(self): def _check(glob, expected): - self.assertEqual(set(glob), { P(BASE, q) for q in expected }) + self.assertEqual(set(glob), { P(self.base, q) for q in expected }) P = self.cls - p = P(BASE) + p = P(self.base) it = p.glob("fileA") self.assertIsInstance(it, collections.abc.Iterator) _check(it, ["fileA"]) @@ -1073,9 +1066,9 @@ def test_glob_case_sensitive(self): P = self.cls def _check(path, pattern, case_sensitive, expected): actual = {str(q) for q in path.glob(pattern, case_sensitive=case_sensitive)} - expected = {str(P(BASE, q)) for q in expected} + expected = {str(P(self.base, q)) for q in expected} self.assertEqual(actual, expected) - path = P(BASE) + path = P(self.base) _check(path, "DIRB/FILE*", True, []) _check(path, "DIRB/FILE*", False, ["dirB/fileB"]) _check(path, "dirb/file*", True, []) @@ -1087,9 +1080,9 @@ def test_glob_follow_symlinks_common(self): def _check(path, glob, expected): actual = {path for path in path.glob(glob, follow_symlinks=True) if "linkD" not in path.parent.parts} # exclude symlink loop. - self.assertEqual(actual, { P(BASE, q) for q in expected }) + self.assertEqual(actual, { P(self.base, q) for q in expected }) P = self.cls - p = P(BASE) + p = P(self.base) _check(p, "fileB", []) _check(p, "dir*/file*", ["dirB/fileB", "dirC/fileC"]) _check(p, "*A", ["dirA", "fileA", "linkA"]) @@ -1112,9 +1105,9 @@ def test_glob_no_follow_symlinks_common(self): self.skipTest("symlinks required") def _check(path, glob, expected): actual = {path for path in path.glob(glob, follow_symlinks=False)} - self.assertEqual(actual, { P(BASE, q) for q in expected }) + self.assertEqual(actual, { P(self.base, q) for q in expected }) P = self.cls - p = P(BASE) + p = P(self.base) _check(p, "fileB", []) _check(p, "dir*/file*", ["dirB/fileB", "dirC/fileC"]) _check(p, "*A", ["dirA", "fileA", "linkA"]) @@ -1132,9 +1125,9 @@ def _check(path, glob, expected): def test_rglob_common(self): def _check(glob, expected): - self.assertEqual(set(glob), {P(BASE, q) for q in expected}) + self.assertEqual(set(glob), {P(self.base, q) for q in expected}) P = self.cls - p = P(BASE) + p = P(self.base) it = p.rglob("fileA") self.assertIsInstance(it, collections.abc.Iterator) _check(it, ["fileA"]) @@ -1159,7 +1152,7 @@ def _check(glob, expected): ]) _check(p.rglob(""), ["./", "dirA/", "dirB/", "dirC/", "dirE/", "dirC/dirD/"]) - p = P(BASE, "dirC") + p = P(self.base, "dirC") _check(p.rglob("*"), ["dirC/fileC", "dirC/novel.txt", "dirC/dirD", "dirC/dirD/fileD"]) _check(p.rglob("file*"), ["dirC/fileC", "dirC/dirD/fileD"]) @@ -1179,9 +1172,9 @@ def test_rglob_follow_symlinks_common(self): def _check(path, glob, expected): actual = {path for path in path.rglob(glob, follow_symlinks=True) if 'linkD' not in path.parent.parts} # exclude symlink loop. - self.assertEqual(actual, { P(BASE, q) for q in expected }) + self.assertEqual(actual, { P(self.base, q) for q in expected }) P = self.cls - p = P(BASE) + p = P(self.base) _check(p, "fileB", ["dirB/fileB", "dirA/linkC/fileB", "linkB/fileB"]) _check(p, "*/fileA", []) _check(p, "*/fileB", ["dirB/fileB", "dirA/linkC/fileB", "linkB/fileB"]) @@ -1192,7 +1185,7 @@ def _check(path, glob, expected): _check(p, "", ["./", "dirA/", "dirA/linkC/", "dirA/linkC/linkD/", "dirB/", "dirB/linkD/", "dirC/", "dirE/", "dirC/dirD/", "linkB/", "linkB/linkD/"]) - p = P(BASE, "dirC") + p = P(self.base, "dirC") _check(p, "*", ["dirC/fileC", "dirC/novel.txt", "dirC/dirD", "dirC/dirD/fileD"]) _check(p, "file*", ["dirC/fileC", "dirC/dirD/fileD"]) @@ -1208,9 +1201,9 @@ def test_rglob_no_follow_symlinks_common(self): self.skipTest("symlinks required") def _check(path, glob, expected): actual = {path for path in path.rglob(glob, follow_symlinks=False)} - self.assertEqual(actual, { P(BASE, q) for q in expected }) + self.assertEqual(actual, { P(self.base, q) for q in expected }) P = self.cls - p = P(BASE) + p = P(self.base) _check(p, "fileB", ["dirB/fileB"]) _check(p, "*/fileA", []) _check(p, "*/fileB", ["dirB/fileB"]) @@ -1218,7 +1211,7 @@ def _check(path, glob, expected): _check(p, "*/", ["dirA/", "dirB/", "dirC/", "dirC/dirD/", "dirE/"]) _check(p, "", ["./", "dirA/", "dirB/", "dirC/", "dirE/", "dirC/dirD/"]) - p = P(BASE, "dirC") + p = P(self.base, "dirC") _check(p, "*", ["dirC/fileC", "dirC/novel.txt", "dirC/dirD", "dirC/dirD/fileD"]) _check(p, "file*", ["dirC/fileC", "dirC/dirD/fileD"]) @@ -1234,7 +1227,7 @@ def test_rglob_symlink_loop(self): if not self.can_symlink: self.skipTest("symlinks required") P = self.cls - p = P(BASE) + p = P(self.base) given = set(p.rglob('*')) expect = {'brokenLink', 'dirA', 'dirA/linkC', @@ -1252,7 +1245,7 @@ def test_rglob_symlink_loop(self): def test_glob_many_open_files(self): depth = 30 P = self.cls - p = base = P(BASE) / 'deep' + p = base = P(self.base) / 'deep' p.mkdir() for _ in range(depth): p /= 'd' @@ -1271,30 +1264,30 @@ def test_glob_many_open_files(self): def test_glob_dotdot(self): # ".." is not special in globs. P = self.cls - p = P(BASE) - self.assertEqual(set(p.glob("..")), { P(BASE, "..") }) - self.assertEqual(set(p.glob("../..")), { P(BASE, "..", "..") }) - self.assertEqual(set(p.glob("dirA/..")), { P(BASE, "dirA", "..") }) - self.assertEqual(set(p.glob("dirA/../file*")), { P(BASE, "dirA/../fileA") }) + p = P(self.base) + self.assertEqual(set(p.glob("..")), { P(self.base, "..") }) + self.assertEqual(set(p.glob("../..")), { P(self.base, "..", "..") }) + self.assertEqual(set(p.glob("dirA/..")), { P(self.base, "dirA", "..") }) + self.assertEqual(set(p.glob("dirA/../file*")), { P(self.base, "dirA/../fileA") }) self.assertEqual(set(p.glob("dirA/../file*/..")), set()) self.assertEqual(set(p.glob("../xyzzy")), set()) self.assertEqual(set(p.glob("xyzzy/..")), set()) - self.assertEqual(set(p.glob("/".join([".."] * 50))), { P(BASE, *[".."] * 50)}) + self.assertEqual(set(p.glob("/".join([".."] * 50))), { P(self.base, *[".."] * 50)}) def test_glob_permissions(self): # See bpo-38894 if not self.can_symlink: self.skipTest("symlinks required") P = self.cls - base = P(BASE) / 'permissions' + base = P(self.base) / 'permissions' base.mkdir() for i in range(100): link = base / f"link{i}" if i % 2: - link.symlink_to(P(BASE, "dirE", "nonexistent")) + link.symlink_to(P(self.base, "dirE", "nonexistent")) else: - link.symlink_to(P(BASE, "dirC")) + link.symlink_to(P(self.base, "dirC")) self.assertEqual(len(set(base.glob("*"))), 100) self.assertEqual(len(set(base.glob("*/"))), 50) @@ -1305,7 +1298,7 @@ def test_glob_long_symlink(self): # See gh-87695 if not self.can_symlink: self.skipTest("symlinks required") - base = self.cls(BASE) / 'long_symlink' + base = self.cls(self.base) / 'long_symlink' base.mkdir() bad_link = base / 'bad_link' bad_link.symlink_to("bad" * 200) @@ -1315,7 +1308,7 @@ def test_glob_above_recursion_limit(self): recursion_limit = 50 # directory_depth > recursion_limit directory_depth = recursion_limit + 10 - base = self.cls(BASE, 'deep') + base = self.cls(self.base, 'deep') path = base.joinpath(*(['d'] * directory_depth)) path.mkdir(parents=True) @@ -1324,7 +1317,7 @@ def test_glob_above_recursion_limit(self): def test_glob_recursive_no_trailing_slash(self): P = self.cls - p = P(BASE) + p = P(self.base) with self.assertWarns(FutureWarning): p.glob('**') with self.assertWarns(FutureWarning): @@ -1338,7 +1331,7 @@ def test_glob_recursive_no_trailing_slash(self): def test_readlink(self): if not self.can_symlink: self.skipTest("symlinks required") - P = self.cls(BASE) + P = self.cls(self.base) self.assertEqual((P / 'linkA').readlink(), self.cls('fileA')) self.assertEqual((P / 'brokenLink').readlink(), self.cls('non-existing')) @@ -1349,7 +1342,7 @@ def test_readlink(self): @unittest.skipIf(hasattr(os, "readlink"), "os.readlink() is present") def test_readlink_unsupported(self): - P = self.cls(BASE) + P = self.cls(self.base) p = P / 'fileA' with self.assertRaises(pathlib.UnsupportedOperation): q.readlink(p) @@ -1365,53 +1358,55 @@ def test_resolve_common(self): if not self.can_symlink: self.skipTest("symlinks required") P = self.cls - p = P(BASE, 'foo') + p = P(self.base, 'foo') with self.assertRaises(OSError) as cm: p.resolve(strict=True) self.assertEqual(cm.exception.errno, errno.ENOENT) # Non-strict + pathmod = self.pathmod self.assertEqualNormCase(str(p.resolve(strict=False)), - os.path.join(BASE, 'foo')) - p = P(BASE, 'foo', 'in', 'spam') + pathmod.join(self.base, 'foo')) + p = P(self.base, 'foo', 'in', 'spam') self.assertEqualNormCase(str(p.resolve(strict=False)), - os.path.join(BASE, 'foo', 'in', 'spam')) - p = P(BASE, '..', 'foo', 'in', 'spam') + pathmod.join(self.base, 'foo', 'in', 'spam')) + p = P(self.base, '..', 'foo', 'in', 'spam') self.assertEqualNormCase(str(p.resolve(strict=False)), - os.path.abspath(os.path.join('foo', 'in', 'spam'))) + pathmod.join(pathmod.dirname(self.base), 'foo', 'in', 'spam')) # These are all relative symlinks. - p = P(BASE, 'dirB', 'fileB') + p = P(self.base, 'dirB', 'fileB') self._check_resolve_relative(p, p) - p = P(BASE, 'linkA') - self._check_resolve_relative(p, P(BASE, 'fileA')) - p = P(BASE, 'dirA', 'linkC', 'fileB') - self._check_resolve_relative(p, P(BASE, 'dirB', 'fileB')) - p = P(BASE, 'dirB', 'linkD', 'fileB') - self._check_resolve_relative(p, P(BASE, 'dirB', 'fileB')) + p = P(self.base, 'linkA') + self._check_resolve_relative(p, P(self.base, 'fileA')) + p = P(self.base, 'dirA', 'linkC', 'fileB') + self._check_resolve_relative(p, P(self.base, 'dirB', 'fileB')) + p = P(self.base, 'dirB', 'linkD', 'fileB') + self._check_resolve_relative(p, P(self.base, 'dirB', 'fileB')) # Non-strict - p = P(BASE, 'dirA', 'linkC', 'fileB', 'foo', 'in', 'spam') - self._check_resolve_relative(p, P(BASE, 'dirB', 'fileB', 'foo', 'in', + p = P(self.base, 'dirA', 'linkC', 'fileB', 'foo', 'in', 'spam') + self._check_resolve_relative(p, P(self.base, 'dirB', 'fileB', 'foo', 'in', 'spam'), False) - p = P(BASE, 'dirA', 'linkC', '..', 'foo', 'in', 'spam') + p = P(self.base, 'dirA', 'linkC', '..', 'foo', 'in', 'spam') if self.cls.pathmod is not posixpath: # In Windows, if linkY points to dirB, 'dirA\linkY\..' # resolves to 'dirA' without resolving linkY first. - self._check_resolve_relative(p, P(BASE, 'dirA', 'foo', 'in', + self._check_resolve_relative(p, P(self.base, 'dirA', 'foo', 'in', 'spam'), False) else: # In Posix, if linkY points to dirB, 'dirA/linkY/..' # resolves to 'dirB/..' first before resolving to parent of dirB. - self._check_resolve_relative(p, P(BASE, 'foo', 'in', 'spam'), False) + self._check_resolve_relative(p, P(self.base, 'foo', 'in', 'spam'), False) # Now create absolute symlinks. + pathmod = self.pathmod d = self.tempdir() - P(BASE, 'dirA', 'linkX').symlink_to(d) - P(BASE, str(d), 'linkY').symlink_to(join('dirB')) - p = P(BASE, 'dirA', 'linkX', 'linkY', 'fileB') - self._check_resolve_absolute(p, P(BASE, 'dirB', 'fileB')) + P(self.base, 'dirA', 'linkX').symlink_to(d) + P(self.base, str(d), 'linkY').symlink_to(pathmod.join(self.base, 'dirB')) + p = P(self.base, 'dirA', 'linkX', 'linkY', 'fileB') + self._check_resolve_absolute(p, P(self.base, 'dirB', 'fileB')) # Non-strict - p = P(BASE, 'dirA', 'linkX', 'linkY', 'foo', 'in', 'spam') - self._check_resolve_relative(p, P(BASE, 'dirB', 'foo', 'in', 'spam'), + p = P(self.base, 'dirA', 'linkX', 'linkY', 'foo', 'in', 'spam') + self._check_resolve_relative(p, P(self.base, 'dirB', 'foo', 'in', 'spam'), False) - p = P(BASE, 'dirA', 'linkX', 'linkY', '..', 'foo', 'in', 'spam') + p = P(self.base, 'dirA', 'linkX', 'linkY', '..', 'foo', 'in', 'spam') if self.cls.pathmod is not posixpath: # In Windows, if linkY points to dirB, 'dirA\linkY\..' # resolves to 'dirA' without resolving linkY first. @@ -1419,16 +1414,17 @@ def test_resolve_common(self): else: # In Posix, if linkY points to dirB, 'dirA/linkY/..' # resolves to 'dirB/..' first before resolving to parent of dirB. - self._check_resolve_relative(p, P(BASE, 'foo', 'in', 'spam'), False) + self._check_resolve_relative(p, P(self.base, 'foo', 'in', 'spam'), False) def test_resolve_dot(self): # See http://web.archive.org/web/20200623062557/https://bitbucket.org/pitrou/pathlib/issues/9/ if not self.can_symlink: self.skipTest("symlinks required") - p = self.cls(BASE) + pathmod = self.pathmod + p = self.cls(self.base) p.joinpath('0').symlink_to('.', target_is_directory=True) - p.joinpath('1').symlink_to(os.path.join('0', '0'), target_is_directory=True) - p.joinpath('2').symlink_to(os.path.join('1', '1'), target_is_directory=True) + p.joinpath('1').symlink_to(pathmod.join('0', '0'), target_is_directory=True) + p.joinpath('2').symlink_to(pathmod.join('1', '1'), target_is_directory=True) q = p / '2' self.assertEqual(q.resolve(strict=True), p) r = q / '3' / '4' @@ -1448,30 +1444,31 @@ def test_resolve_loop(self): if self.cls.pathmod is not posixpath: self.skipTest("symlink loops work differently with concrete Windows paths") # Loops with relative symlinks. - self.cls(BASE, 'linkX').symlink_to('linkX/inside') - self._check_symlink_loop(BASE, 'linkX') - self.cls(BASE, 'linkY').symlink_to('linkY') - self._check_symlink_loop(BASE, 'linkY') - self.cls(BASE, 'linkZ').symlink_to('linkZ/../linkZ') - self._check_symlink_loop(BASE, 'linkZ') + self.cls(self.base, 'linkX').symlink_to('linkX/inside') + self._check_symlink_loop(self.base, 'linkX') + self.cls(self.base, 'linkY').symlink_to('linkY') + self._check_symlink_loop(self.base, 'linkY') + self.cls(self.base, 'linkZ').symlink_to('linkZ/../linkZ') + self._check_symlink_loop(self.base, 'linkZ') # Non-strict - p = self.cls(BASE, 'linkZ', 'foo') + p = self.cls(self.base, 'linkZ', 'foo') self.assertEqual(p.resolve(strict=False), p) # Loops with absolute symlinks. - self.cls(BASE, 'linkU').symlink_to(join('linkU/inside')) - self._check_symlink_loop(BASE, 'linkU') - self.cls(BASE, 'linkV').symlink_to(join('linkV')) - self._check_symlink_loop(BASE, 'linkV') - self.cls(BASE, 'linkW').symlink_to(join('linkW/../linkW')) - self._check_symlink_loop(BASE, 'linkW') + pathmod = self.pathmod + self.cls(self.base, 'linkU').symlink_to(pathmod.join(self.base, 'linkU/inside')) + self._check_symlink_loop(self.base, 'linkU') + self.cls(self.base, 'linkV').symlink_to(pathmod.join(self.base, 'linkV')) + self._check_symlink_loop(self.base, 'linkV') + self.cls(self.base, 'linkW').symlink_to(pathmod.join(self.base, 'linkW/../linkW')) + self._check_symlink_loop(self.base, 'linkW') # Non-strict - q = self.cls(BASE, 'linkW', 'foo') + q = self.cls(self.base, 'linkW', 'foo') self.assertEqual(q.resolve(strict=False), q) def test_stat(self): - statA = self.cls(BASE).joinpath('fileA').stat() - statB = self.cls(BASE).joinpath('dirB', 'fileB').stat() - statC = self.cls(BASE).joinpath('dirC').stat() + statA = self.cls(self.base).joinpath('fileA').stat() + statB = self.cls(self.base).joinpath('dirB', 'fileB').stat() + statC = self.cls(self.base).joinpath('dirC').stat() # st_mode: files are the same, directory differs. self.assertIsInstance(statA.st_mode, int) self.assertEqual(statA.st_mode, statB.st_mode) @@ -1491,29 +1488,29 @@ def test_stat(self): def test_stat_no_follow_symlinks(self): if not self.can_symlink: self.skipTest("symlinks required") - p = self.cls(BASE) / 'linkA' + p = self.cls(self.base) / 'linkA' st = p.stat() self.assertNotEqual(st, p.stat(follow_symlinks=False)) def test_stat_no_follow_symlinks_nosymlink(self): - p = self.cls(BASE) / 'fileA' + p = self.cls(self.base) / 'fileA' st = p.stat() self.assertEqual(st, p.stat(follow_symlinks=False)) def test_lstat(self): if not self.can_symlink: self.skipTest("symlinks required") - p = self.cls(BASE)/ 'linkA' + p = self.cls(self.base)/ 'linkA' st = p.stat() self.assertNotEqual(st, p.lstat()) def test_lstat_nosymlink(self): - p = self.cls(BASE) / 'fileA' + p = self.cls(self.base) / 'fileA' st = p.stat() self.assertEqual(st, p.lstat()) def test_is_dir(self): - P = self.cls(BASE) + P = self.cls(self.base) self.assertTrue((P / 'dirA').is_dir()) self.assertFalse((P / 'fileA').is_dir()) self.assertFalse((P / 'non-existing').is_dir()) @@ -1526,7 +1523,7 @@ def test_is_dir(self): self.assertFalse((P / 'dirA\x00').is_dir()) def test_is_dir_no_follow_symlinks(self): - P = self.cls(BASE) + P = self.cls(self.base) self.assertTrue((P / 'dirA').is_dir(follow_symlinks=False)) self.assertFalse((P / 'fileA').is_dir(follow_symlinks=False)) self.assertFalse((P / 'non-existing').is_dir(follow_symlinks=False)) @@ -1539,7 +1536,7 @@ def test_is_dir_no_follow_symlinks(self): self.assertFalse((P / 'dirA\x00').is_dir(follow_symlinks=False)) def test_is_file(self): - P = self.cls(BASE) + P = self.cls(self.base) self.assertTrue((P / 'fileA').is_file()) self.assertFalse((P / 'dirA').is_file()) self.assertFalse((P / 'non-existing').is_file()) @@ -1552,7 +1549,7 @@ def test_is_file(self): self.assertFalse((P / 'fileA\x00').is_file()) def test_is_file_no_follow_symlinks(self): - P = self.cls(BASE) + P = self.cls(self.base) self.assertTrue((P / 'fileA').is_file(follow_symlinks=False)) self.assertFalse((P / 'dirA').is_file(follow_symlinks=False)) self.assertFalse((P / 'non-existing').is_file(follow_symlinks=False)) @@ -1565,7 +1562,7 @@ def test_is_file_no_follow_symlinks(self): self.assertFalse((P / 'fileA\x00').is_file(follow_symlinks=False)) def test_is_mount(self): - P = self.cls(BASE) + P = self.cls(self.base) self.assertFalse((P / 'fileA').is_mount()) self.assertFalse((P / 'dirA').is_mount()) self.assertFalse((P / 'non-existing').is_mount()) @@ -1574,7 +1571,7 @@ def test_is_mount(self): self.assertFalse((P / 'linkA').is_mount()) def test_is_symlink(self): - P = self.cls(BASE) + P = self.cls(self.base) self.assertFalse((P / 'fileA').is_symlink()) self.assertFalse((P / 'dirA').is_symlink()) self.assertFalse((P / 'non-existing').is_symlink()) @@ -1590,7 +1587,7 @@ def test_is_symlink(self): self.assertIs((P / 'linkA\x00').is_file(), False) def test_is_junction_false(self): - P = self.cls(BASE) + P = self.cls(self.base) self.assertFalse((P / 'fileA').is_junction()) self.assertFalse((P / 'dirA').is_junction()) self.assertFalse((P / 'non-existing').is_junction()) @@ -1599,7 +1596,7 @@ def test_is_junction_false(self): self.assertFalse((P / 'fileA\x00').is_junction()) def test_is_fifo_false(self): - P = self.cls(BASE) + P = self.cls(self.base) self.assertFalse((P / 'fileA').is_fifo()) self.assertFalse((P / 'dirA').is_fifo()) self.assertFalse((P / 'non-existing').is_fifo()) @@ -1608,7 +1605,7 @@ def test_is_fifo_false(self): self.assertIs((P / 'fileA\x00').is_fifo(), False) def test_is_socket_false(self): - P = self.cls(BASE) + P = self.cls(self.base) self.assertFalse((P / 'fileA').is_socket()) self.assertFalse((P / 'dirA').is_socket()) self.assertFalse((P / 'non-existing').is_socket()) @@ -1617,7 +1614,7 @@ def test_is_socket_false(self): self.assertIs((P / 'fileA\x00').is_socket(), False) def test_is_block_device_false(self): - P = self.cls(BASE) + P = self.cls(self.base) self.assertFalse((P / 'fileA').is_block_device()) self.assertFalse((P / 'dirA').is_block_device()) self.assertFalse((P / 'non-existing').is_block_device()) @@ -1626,7 +1623,7 @@ def test_is_block_device_false(self): self.assertIs((P / 'fileA\x00').is_block_device(), False) def test_is_char_device_false(self): - P = self.cls(BASE) + P = self.cls(self.base) self.assertFalse((P / 'fileA').is_char_device()) self.assertFalse((P / 'dirA').is_char_device()) self.assertFalse((P / 'non-existing').is_char_device()) @@ -1635,7 +1632,7 @@ def test_is_char_device_false(self): self.assertIs((P / 'fileA\x00').is_char_device(), False) def test_pickling_common(self): - p = self.cls(BASE, 'fileA') + p = self.cls(self.base, 'fileA') for proto in range(0, pickle.HIGHEST_PROTOCOL + 1): dumped = pickle.dumps(p, proto) pp = pickle.loads(dumped) @@ -1655,25 +1652,26 @@ def _check_complex_symlinks(self, link0_target): self.skipTest("symlinks required") # Test solving a non-looping chain of symlinks (issue #19887). - P = self.cls(BASE) - P.joinpath('link1').symlink_to(os.path.join('link0', 'link0'), target_is_directory=True) - P.joinpath('link2').symlink_to(os.path.join('link1', 'link1'), target_is_directory=True) - P.joinpath('link3').symlink_to(os.path.join('link2', 'link2'), target_is_directory=True) + pathmod = self.pathmod + P = self.cls(self.base) + P.joinpath('link1').symlink_to(pathmod.join('link0', 'link0'), target_is_directory=True) + P.joinpath('link2').symlink_to(pathmod.join('link1', 'link1'), target_is_directory=True) + P.joinpath('link3').symlink_to(pathmod.join('link2', 'link2'), target_is_directory=True) P.joinpath('link0').symlink_to(link0_target, target_is_directory=True) # Resolve absolute paths. p = (P / 'link0').resolve() self.assertEqual(p, P) - self.assertEqualNormCase(str(p), BASE) + self.assertEqualNormCase(str(p), self.base) p = (P / 'link1').resolve() self.assertEqual(p, P) - self.assertEqualNormCase(str(p), BASE) + self.assertEqualNormCase(str(p), self.base) p = (P / 'link2').resolve() self.assertEqual(p, P) - self.assertEqualNormCase(str(p), BASE) + self.assertEqualNormCase(str(p), self.base) p = (P / 'link3').resolve() self.assertEqual(p, P) - self.assertEqualNormCase(str(p), BASE) + self.assertEqualNormCase(str(p), self.base) # Resolve relative paths. try: @@ -1681,31 +1679,31 @@ def _check_complex_symlinks(self, link0_target): except pathlib.UnsupportedOperation: return old_path = os.getcwd() - os.chdir(BASE) + os.chdir(self.base) try: p = self.cls('link0').resolve() self.assertEqual(p, P) - self.assertEqualNormCase(str(p), BASE) + self.assertEqualNormCase(str(p), self.base) p = self.cls('link1').resolve() self.assertEqual(p, P) - self.assertEqualNormCase(str(p), BASE) + self.assertEqualNormCase(str(p), self.base) p = self.cls('link2').resolve() self.assertEqual(p, P) - self.assertEqualNormCase(str(p), BASE) + self.assertEqualNormCase(str(p), self.base) p = self.cls('link3').resolve() self.assertEqual(p, P) - self.assertEqualNormCase(str(p), BASE) + self.assertEqualNormCase(str(p), self.base) finally: os.chdir(old_path) def test_complex_symlinks_absolute(self): - self._check_complex_symlinks(BASE) + self._check_complex_symlinks(self.base) def test_complex_symlinks_relative(self): self._check_complex_symlinks('.') def test_complex_symlinks_relative_dot_dot(self): - self._check_complex_symlinks(os.path.join('dirA', '..')) + self._check_complex_symlinks(self.pathmod.join('dirA', '..')) def setUpWalk(self): # Build: @@ -1722,7 +1720,7 @@ def setUpWalk(self): # broken_link2 # TEST2/ # tmp4 a lone file - self.walk_path = self.cls(BASE, "TEST1") + self.walk_path = self.cls(self.base, "TEST1") self.sub1_path = self.walk_path / "SUB1" self.sub11_path = self.sub1_path / "SUB11" self.sub2_path = self.walk_path / "SUB2" @@ -1730,8 +1728,8 @@ def setUpWalk(self): tmp2_path = self.sub1_path / "tmp2" tmp3_path = self.sub2_path / "tmp3" self.link_path = self.sub2_path / "link" - t2_path = self.cls(BASE, "TEST2") - tmp4_path = self.cls(BASE, "TEST2", "tmp4") + t2_path = self.cls(self.base, "TEST2") + tmp4_path = self.cls(self.base, "TEST2", "tmp4") broken_link_path = self.sub2_path / "broken_link" broken_link2_path = self.sub2_path / "broken_link2" @@ -1858,7 +1856,7 @@ def test_walk_above_recursion_limit(self): recursion_limit = 40 # directory_depth > recursion_limit directory_depth = recursion_limit + 10 - base = self.cls(BASE, 'deep') + base = self.cls(self.base, 'deep') path = base.joinpath(*(['d'] * directory_depth)) path.mkdir(parents=True) From 60ef4db4c34d133ca29bc91e2f6064fb2565fb8f Mon Sep 17 00:00:00 2001 From: barneygale Date: Sat, 16 Dec 2023 23:28:12 +0000 Subject: [PATCH 3/5] Fix docstring test --- Lib/test/test_pathlib/test_pathlib.py | 13 +++++++++++++ Lib/test/test_pathlib/test_pathlib_abc.py | 9 --------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/Lib/test/test_pathlib/test_pathlib.py b/Lib/test/test_pathlib/test_pathlib.py index 4af07adba7de50..ddfd7fe41fc52b 100644 --- a/Lib/test/test_pathlib/test_pathlib.py +++ b/Lib/test/test_pathlib/test_pathlib.py @@ -989,6 +989,19 @@ def tempdir(self): self.addCleanup(os_helper.rmtree, d) return d + def test_matches_pathbase_api(self): + our_names = {name for name in dir(self.cls) if name[0] != '_'} + path_names = {name for name in dir(pathlib._abc.PathBase) if name[0] != '_'} + self.assertEqual(our_names, path_names) + for attr_name in our_names: + if attr_name == 'pathmod': + # On Windows, Path.pathmod is ntpath, but PathBase.pathmod is + # posixpath, and so their docstrings differ. + continue + our_attr = getattr(self.cls, attr_name) + path_attr = getattr(pathlib._abc.PathBase, attr_name) + self.assertEqual(our_attr.__doc__, path_attr.__doc__) + def test_concrete_class(self): if self.cls is pathlib.Path: expected = pathlib.WindowsPath if os.name == 'nt' else pathlib.PosixPath diff --git a/Lib/test/test_pathlib/test_pathlib_abc.py b/Lib/test/test_pathlib/test_pathlib_abc.py index c6bd9e21eca6ec..658ec0d31f4768 100644 --- a/Lib/test/test_pathlib/test_pathlib_abc.py +++ b/Lib/test/test_pathlib/test_pathlib_abc.py @@ -703,15 +703,6 @@ def test_fspath_common(self): def test_as_bytes_common(self): self.assertRaises(TypeError, bytes, self.cls()) - def test_matches_path_api(self): - our_names = {name for name in dir(self.cls) if name[0] != '_'} - path_names = {name for name in dir(pathlib.Path) if name[0] != '_'} - self.assertEqual(our_names, path_names) - for attr_name in our_names: - our_attr = getattr(self.cls, attr_name) - path_attr = getattr(pathlib.Path, attr_name) - self.assertEqual(our_attr.__doc__, path_attr.__doc__) - class DummyPathIO(io.BytesIO): """ From 7958effa4b19bc54c3d9312ba57ee81227069ca9 Mon Sep 17 00:00:00 2001 From: barneygale Date: Sun, 17 Dec 2023 00:29:55 +0000 Subject: [PATCH 4/5] Fix dodgy merge --- Lib/test/test_pathlib/test_pathlib.py | 2 ++ Lib/test/test_pathlib/test_pathlib_abc.py | 3 --- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_pathlib/test_pathlib.py b/Lib/test/test_pathlib/test_pathlib.py index 8b11b35197cb64..6fd5129aaa3a77 100644 --- a/Lib/test/test_pathlib/test_pathlib.py +++ b/Lib/test/test_pathlib/test_pathlib.py @@ -40,6 +40,8 @@ class PurePathTest(test_pathlib_abc.DummyPurePathTest): cls = pathlib.PurePath + + # Make sure any symbolic links in the base test path are resolved. base = os.path.realpath(TESTFN) def test_concrete_class(self): diff --git a/Lib/test/test_pathlib/test_pathlib_abc.py b/Lib/test/test_pathlib/test_pathlib_abc.py index d5f515546e0ac9..bc98c3c01a8765 100644 --- a/Lib/test/test_pathlib/test_pathlib_abc.py +++ b/Lib/test/test_pathlib/test_pathlib_abc.py @@ -56,9 +56,6 @@ class DummyPurePathTest(unittest.TestCase): cls = DummyPurePath base = f'/BASE/{TESTFN}' - # Make sure any symbolic links in the base test path are resolved. - base = os.path.realpath(TESTFN) - # Keys are canonical paths, values are list of tuples of arguments # supposed to produce equal paths. equivalences = { From 2867922fa44d1f6e9f8bcdcaabc1998e0c43599f Mon Sep 17 00:00:00 2001 From: barneygale Date: Sun, 17 Dec 2023 01:29:13 +0000 Subject: [PATCH 5/5] Add comment --- Lib/test/test_pathlib/test_pathlib_abc.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_pathlib/test_pathlib_abc.py b/Lib/test/test_pathlib/test_pathlib_abc.py index bc98c3c01a8765..154d604c05f587 100644 --- a/Lib/test/test_pathlib/test_pathlib_abc.py +++ b/Lib/test/test_pathlib/test_pathlib_abc.py @@ -54,7 +54,9 @@ def __hash__(self): class DummyPurePathTest(unittest.TestCase): cls = DummyPurePath - base = f'/BASE/{TESTFN}' + + # Use a base path that's unrelated to any real filesystem path. + base = f'/this/path/kills/fascists/{TESTFN}' # Keys are canonical paths, values are list of tuples of arguments # supposed to produce equal paths.