summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_pathlib/test_pathlib_abc.py
diff options
context:
space:
mode:
authorBarney Gale <barney.gale@gmail.com>2024-11-09 18:47:49 (GMT)
committerGitHub <noreply@github.com>2024-11-09 18:47:49 (GMT)
commit266328552e922fd9030cd699e10a25f03a67c8ba (patch)
tree5a210ddb5613acd82d11e112959972ed4d4a7a84 /Lib/test/test_pathlib/test_pathlib_abc.py
parent0f47a3199c51ba7c49e72b4c645dbf599aa17be4 (diff)
downloadcpython-266328552e922fd9030cd699e10a25f03a67c8ba.zip
cpython-266328552e922fd9030cd699e10a25f03a67c8ba.tar.gz
cpython-266328552e922fd9030cd699e10a25f03a67c8ba.tar.bz2
pathlib ABCs: tighten up `resolve()` and `absolute()` (#126611)
In `PathBase.resolve()`, raise `UnsupportedOperation` if a non-POSIX path parser is used (our implementation uses `posixpath._realpath()`, which produces incorrect results for non-POSIX path flavours.) Also tweak code to call `self.absolute()` upfront rather than supplying an emulated `getcwd()` function. Adjust `PathBase.absolute()` to work somewhat like `resolve()`. If a POSIX path parser is used, we treat the root directory as the current directory. This is the simplest useful behaviour for concrete path types without a current directory cursor.
Diffstat (limited to 'Lib/test/test_pathlib/test_pathlib_abc.py')
-rw-r--r--Lib/test/test_pathlib/test_pathlib_abc.py40
1 files changed, 17 insertions, 23 deletions
diff --git a/Lib/test/test_pathlib/test_pathlib_abc.py b/Lib/test/test_pathlib/test_pathlib_abc.py
index bb2e418..b69d674 100644
--- a/Lib/test/test_pathlib/test_pathlib_abc.py
+++ b/Lib/test/test_pathlib/test_pathlib_abc.py
@@ -2493,6 +2493,23 @@ class DummyPathTest(DummyPurePathTest):
bad_link.symlink_to("bad" * 200)
self.assertEqual(sorted(base.glob('**/*')), [bad_link])
+ @needs_posix
+ def test_absolute_posix(self):
+ P = self.cls
+ # The default implementation uses '/' as the current directory
+ self.assertEqual(str(P('').absolute()), '/')
+ self.assertEqual(str(P('a').absolute()), '/a')
+ self.assertEqual(str(P('a/b').absolute()), '/a/b')
+
+ self.assertEqual(str(P('/').absolute()), '/')
+ self.assertEqual(str(P('/a').absolute()), '/a')
+ self.assertEqual(str(P('/a/b').absolute()), '/a/b')
+
+ # '//'-prefixed absolute path (supported by POSIX).
+ self.assertEqual(str(P('//').absolute()), '//')
+ self.assertEqual(str(P('//a').absolute()), '//a')
+ self.assertEqual(str(P('//a/b').absolute()), '//a/b')
+
@needs_symlinks
def test_readlink(self):
P = self.cls(self.base)
@@ -2810,29 +2827,6 @@ class DummyPathTest(DummyPurePathTest):
self.assertEqual(p, P)
self.assertEqualNormCase(str(p), self.base)
- # Resolve relative paths.
- try:
- self.cls('').absolute()
- except UnsupportedOperation:
- return
- old_path = os.getcwd()
- os.chdir(self.base)
- try:
- p = self.cls('link0').resolve()
- self.assertEqual(p, P)
- self.assertEqualNormCase(str(p), self.base)
- p = self.cls('link1').resolve()
- self.assertEqual(p, P)
- self.assertEqualNormCase(str(p), self.base)
- p = self.cls('link2').resolve()
- self.assertEqual(p, P)
- self.assertEqualNormCase(str(p), self.base)
- p = self.cls('link3').resolve()
- self.assertEqual(p, P)
- self.assertEqualNormCase(str(p), self.base)
- finally:
- os.chdir(old_path)
-
@needs_symlinks
def test_complex_symlinks_absolute(self):
self._check_complex_symlinks(self.base)