mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
bpo-43757: Make pathlib use os.path.realpath() to resolve symlinks in a path (GH-25264)
Also adds a new "strict" argument to realpath() to avoid changing the default behaviour of pathlib while sharing the implementation.
This commit is contained in:
parent
859577c249
commit
baecfbd849
7 changed files with 184 additions and 109 deletions
|
@ -355,6 +355,19 @@ class PosixPathTest(unittest.TestCase):
|
|||
finally:
|
||||
os_helper.unlink(ABSTFN)
|
||||
|
||||
@unittest.skipUnless(hasattr(os, "symlink"),
|
||||
"Missing symlink implementation")
|
||||
@skip_if_ABSTFN_contains_backslash
|
||||
def test_realpath_strict(self):
|
||||
# Bug #43757: raise FileNotFoundError in strict mode if we encounter
|
||||
# a path that does not exist.
|
||||
try:
|
||||
os.symlink(ABSTFN+"1", ABSTFN)
|
||||
self.assertRaises(FileNotFoundError, realpath, ABSTFN, strict=True)
|
||||
self.assertRaises(FileNotFoundError, realpath, ABSTFN + "2", strict=True)
|
||||
finally:
|
||||
os_helper.unlink(ABSTFN)
|
||||
|
||||
@unittest.skipUnless(hasattr(os, "symlink"),
|
||||
"Missing symlink implementation")
|
||||
@skip_if_ABSTFN_contains_backslash
|
||||
|
@ -370,7 +383,7 @@ class PosixPathTest(unittest.TestCase):
|
|||
@skip_if_ABSTFN_contains_backslash
|
||||
def test_realpath_symlink_loops(self):
|
||||
# Bug #930024, return the path unchanged if we get into an infinite
|
||||
# symlink loop.
|
||||
# symlink loop in non-strict mode (default).
|
||||
try:
|
||||
os.symlink(ABSTFN, ABSTFN)
|
||||
self.assertEqual(realpath(ABSTFN), ABSTFN)
|
||||
|
@ -407,6 +420,48 @@ class PosixPathTest(unittest.TestCase):
|
|||
os_helper.unlink(ABSTFN+"c")
|
||||
os_helper.unlink(ABSTFN+"a")
|
||||
|
||||
@unittest.skipUnless(hasattr(os, "symlink"),
|
||||
"Missing symlink implementation")
|
||||
@skip_if_ABSTFN_contains_backslash
|
||||
def test_realpath_symlink_loops_strict(self):
|
||||
# Bug #43757, raise OSError if we get into an infinite symlink loop in
|
||||
# strict mode.
|
||||
try:
|
||||
os.symlink(ABSTFN, ABSTFN)
|
||||
self.assertRaises(OSError, realpath, ABSTFN, strict=True)
|
||||
|
||||
os.symlink(ABSTFN+"1", ABSTFN+"2")
|
||||
os.symlink(ABSTFN+"2", ABSTFN+"1")
|
||||
self.assertRaises(OSError, realpath, ABSTFN+"1", strict=True)
|
||||
self.assertRaises(OSError, realpath, ABSTFN+"2", strict=True)
|
||||
|
||||
self.assertRaises(OSError, realpath, ABSTFN+"1/x", strict=True)
|
||||
self.assertRaises(OSError, realpath, ABSTFN+"1/..", strict=True)
|
||||
self.assertRaises(OSError, realpath, ABSTFN+"1/../x", strict=True)
|
||||
os.symlink(ABSTFN+"x", ABSTFN+"y")
|
||||
self.assertRaises(OSError, realpath,
|
||||
ABSTFN+"1/../" + basename(ABSTFN) + "y", strict=True)
|
||||
self.assertRaises(OSError, realpath,
|
||||
ABSTFN+"1/../" + basename(ABSTFN) + "1", strict=True)
|
||||
|
||||
os.symlink(basename(ABSTFN) + "a/b", ABSTFN+"a")
|
||||
self.assertRaises(OSError, realpath, ABSTFN+"a", strict=True)
|
||||
|
||||
os.symlink("../" + basename(dirname(ABSTFN)) + "/" +
|
||||
basename(ABSTFN) + "c", ABSTFN+"c")
|
||||
self.assertRaises(OSError, realpath, ABSTFN+"c", strict=True)
|
||||
|
||||
# Test using relative path as well.
|
||||
with os_helper.change_cwd(dirname(ABSTFN)):
|
||||
self.assertRaises(OSError, realpath, basename(ABSTFN), strict=True)
|
||||
finally:
|
||||
os_helper.unlink(ABSTFN)
|
||||
os_helper.unlink(ABSTFN+"1")
|
||||
os_helper.unlink(ABSTFN+"2")
|
||||
os_helper.unlink(ABSTFN+"y")
|
||||
os_helper.unlink(ABSTFN+"c")
|
||||
os_helper.unlink(ABSTFN+"a")
|
||||
|
||||
@unittest.skipUnless(hasattr(os, "symlink"),
|
||||
"Missing symlink implementation")
|
||||
@skip_if_ABSTFN_contains_backslash
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue