Skip to content

gh-71189: Support all-but-last mode in os.path.realpath() #117562

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Unindent tests.
  • Loading branch information
serhiy-storchaka committed May 20, 2025
commit d93263ff4001c93ddb22647c3ad0cc705eb1b1b7
240 changes: 120 additions & 120 deletions Lib/test/test_ntpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -832,130 +832,130 @@ def test_realpath_permission(self):
self.assertPathEqual(test_file, ntpath.realpath(test_file_short))

@os_helper.skip_unless_symlink
@unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
def test_realpath_mode(self):
realpath = ntpath.realpath
ALL_BUT_LAST = ntpath.ALL_BUT_LAST
ABSTFN = ntpath.abspath(os_helper.TESTFN)
try:
os.mkdir(ABSTFN)
os.mkdir(ABSTFN + "\\dir")
open(ABSTFN + "\\file", "wb").close()
open(ABSTFN + "\\dir\\file2", "wb").close()
os.symlink("file", ABSTFN + "\\link")
os.symlink("dir", ABSTFN + "\\link2")
os.symlink("nonexistent", ABSTFN + "\\broken")
os.symlink("cycle", ABSTFN + "\\cycle")
def check(path, mode, expected, errno=None):
path = path.replace('/', '\\')
if isinstance(expected, str):
assert errno is None
self.assertEqual(realpath(path, strict=mode), ABSTFN + expected.replace('/', '\\'))
else:
with self.assertRaises(expected) as cm:
realpath(path, strict=mode)
if errno is not None:
self.assertEqual(cm.exception.errno, errno)

with os_helper.change_cwd(ABSTFN):
check("file", False, "/file")
check("file", ALL_BUT_LAST, "/file")
check("file", True, "/file")
check("file/", False, "/file")
# check("file/", ALL_BUT_LAST, NotADirectoryError)
# check("file/", True, NotADirectoryError)
check("file/file2", False, "/file/file2")
# check("file/file2", ALL_BUT_LAST, NotADirectoryError)
# check("file/file2", True, NotADirectoryError)
check("file/.", False, "/file")
# check("file/.", ALL_BUT_LAST, NotADirectoryError)
# check("file/.", True, NotADirectoryError)
check("file/../link2", False, "/dir")
check("file/../link2", ALL_BUT_LAST, "/dir")
check("file/../link2", True, "/dir")

check("dir", False, "/dir")
check("dir", ALL_BUT_LAST, "/dir")
check("dir", True, "/dir")
check("dir/", False, "/dir")
check("dir/", ALL_BUT_LAST, "/dir")
check("dir/", True, "/dir")
check("dir/file2", False, "/dir/file2")
check("dir/file2", ALL_BUT_LAST, "/dir/file2")
check("dir/file2", True, "/dir/file2")

check("link", False, "/file")
check("link", ALL_BUT_LAST, "/file")
check("link", True, "/file")
check("link/", False, "/file")
# check("link/", ALL_BUT_LAST, NotADirectoryError)
# check("link/", True, NotADirectoryError)
check("link/file2", False, "/file/file2")
# check("link/file2", ALL_BUT_LAST, NotADirectoryError)
# check("link/file2", True, NotADirectoryError)
check("link/.", False, "/file")
# check("link/.", ALL_BUT_LAST, NotADirectoryError)
# check("link/.", True, NotADirectoryError)
check("link/../link", False, "/file")
check("link/../link", ALL_BUT_LAST, "/file")
check("link/../link", True, "/file")

check("link2", False, "/dir")
check("link2", ALL_BUT_LAST, "/dir")
check("link2", True, "/dir")
check("link2/", False, "/dir")
check("link2/", ALL_BUT_LAST, "/dir")
check("link2/", True, "/dir")
check("link2/file2", False, "/dir/file2")
check("link2/file2", ALL_BUT_LAST, "/dir/file2")
check("link2/file2", True, "/dir/file2")

check("nonexistent", False, "/nonexistent")
check("nonexistent", ALL_BUT_LAST, "/nonexistent")
check("nonexistent", True, FileNotFoundError)
check("nonexistent/", False, "/nonexistent")
check("nonexistent/", ALL_BUT_LAST, "/nonexistent")
check("nonexistent/", True, FileNotFoundError)
check("nonexistent/file", False, "/nonexistent/file")
check("nonexistent/file", ALL_BUT_LAST, FileNotFoundError)
check("nonexistent/file", True, FileNotFoundError)
check("nonexistent/../link", False, "/file")
check("nonexistent/../link", ALL_BUT_LAST, "/file")
check("nonexistent/../link", True, "/file")

check("broken", False, "/nonexistent")
check("broken", ALL_BUT_LAST, "/nonexistent")
check("broken", True, FileNotFoundError)
check("broken/", False, "/nonexistent")
check("broken/", ALL_BUT_LAST, "/nonexistent")
check("broken/", True, FileNotFoundError)
check("broken/file", False, "/nonexistent/file")
check("broken/file", ALL_BUT_LAST, FileNotFoundError)
check("broken/file", True, FileNotFoundError)
check("broken/../link", False, "/file")
check("broken/../link", ALL_BUT_LAST, "/file")
check("broken/../link", True, "/file")

check("cycle", False, "/cycle")
check("cycle", ALL_BUT_LAST, OSError, errno.EINVAL)
check("cycle", True, OSError, errno.EINVAL)
check("cycle/", False, "/cycle")
check("cycle/", ALL_BUT_LAST, OSError, errno.EINVAL)
check("cycle/", True, OSError, errno.EINVAL)
check("cycle/file", False, "/cycle/file")
check("cycle/file", ALL_BUT_LAST, OSError, errno.EINVAL)
check("cycle/file", True, OSError, errno.EINVAL)
check("cycle/../link", False, "/file")
check("cycle/../link", ALL_BUT_LAST, "/file")
check("cycle/../link", True, "/file")
finally:
os_helper.unlink(ABSTFN + "/file")
os_helper.unlink(ABSTFN + "/dir/file2")
os_helper.unlink(ABSTFN + "/link")
os_helper.unlink(ABSTFN + "/link2")
os_helper.unlink(ABSTFN + "/broken")
os_helper.unlink(ABSTFN + "/cycle")
os_helper.rmdir(ABSTFN + "/dir")
os_helper.rmdir(ABSTFN)
self.addCleanup(os_helper.rmdir, ABSTFN)
self.addCleanup(os_helper.rmdir, ABSTFN + "/dir")
self.addCleanup(os_helper.unlink, ABSTFN + "/file")
self.addCleanup(os_helper.unlink, ABSTFN + "/dir/file2")
self.addCleanup(os_helper.unlink, ABSTFN + "/link")
self.addCleanup(os_helper.unlink, ABSTFN + "/link2")
self.addCleanup(os_helper.unlink, ABSTFN + "/broken")
self.addCleanup(os_helper.unlink, ABSTFN + "/cycle")

os.mkdir(ABSTFN)
os.mkdir(ABSTFN + "\\dir")
open(ABSTFN + "\\file", "wb").close()
open(ABSTFN + "\\dir\\file2", "wb").close()
os.symlink("file", ABSTFN + "\\link")
os.symlink("dir", ABSTFN + "\\link2")
os.symlink("nonexistent", ABSTFN + "\\broken")
os.symlink("cycle", ABSTFN + "\\cycle")
def check(path, mode, expected, errno=None):
path = path.replace('/', '\\')
if isinstance(expected, str):
assert errno is None
self.assertEqual(realpath(path, strict=mode), ABSTFN + expected.replace('/', '\\'))
else:
with self.assertRaises(expected) as cm:
realpath(path, strict=mode)
if errno is not None:
self.assertEqual(cm.exception.errno, errno)

self.enterContext(os_helper.change_cwd(ABSTFN))
check("file", False, "/file")
check("file", ALL_BUT_LAST, "/file")
check("file", True, "/file")
check("file/", False, "/file")
# check("file/", ALL_BUT_LAST, NotADirectoryError)
# check("file/", True, NotADirectoryError)
check("file/file2", False, "/file/file2")
# check("file/file2", ALL_BUT_LAST, NotADirectoryError)
# check("file/file2", True, NotADirectoryError)
check("file/.", False, "/file")
# check("file/.", ALL_BUT_LAST, NotADirectoryError)
# check("file/.", True, NotADirectoryError)
check("file/../link2", False, "/dir")
check("file/../link2", ALL_BUT_LAST, "/dir")
check("file/../link2", True, "/dir")

check("dir", False, "/dir")
check("dir", ALL_BUT_LAST, "/dir")
check("dir", True, "/dir")
check("dir/", False, "/dir")
check("dir/", ALL_BUT_LAST, "/dir")
check("dir/", True, "/dir")
check("dir/file2", False, "/dir/file2")
check("dir/file2", ALL_BUT_LAST, "/dir/file2")
check("dir/file2", True, "/dir/file2")

check("link", False, "/file")
check("link", ALL_BUT_LAST, "/file")
check("link", True, "/file")
check("link/", False, "/file")
# check("link/", ALL_BUT_LAST, NotADirectoryError)
# check("link/", True, NotADirectoryError)
check("link/file2", False, "/file/file2")
# check("link/file2", ALL_BUT_LAST, NotADirectoryError)
# check("link/file2", True, NotADirectoryError)
check("link/.", False, "/file")
# check("link/.", ALL_BUT_LAST, NotADirectoryError)
# check("link/.", True, NotADirectoryError)
check("link/../link", False, "/file")
check("link/../link", ALL_BUT_LAST, "/file")
check("link/../link", True, "/file")

check("link2", False, "/dir")
check("link2", ALL_BUT_LAST, "/dir")
check("link2", True, "/dir")
check("link2/", False, "/dir")
check("link2/", ALL_BUT_LAST, "/dir")
check("link2/", True, "/dir")
check("link2/file2", False, "/dir/file2")
check("link2/file2", ALL_BUT_LAST, "/dir/file2")
check("link2/file2", True, "/dir/file2")

check("nonexistent", False, "/nonexistent")
check("nonexistent", ALL_BUT_LAST, "/nonexistent")
check("nonexistent", True, FileNotFoundError)
check("nonexistent/", False, "/nonexistent")
check("nonexistent/", ALL_BUT_LAST, "/nonexistent")
check("nonexistent/", True, FileNotFoundError)
check("nonexistent/file", False, "/nonexistent/file")
check("nonexistent/file", ALL_BUT_LAST, FileNotFoundError)
check("nonexistent/file", True, FileNotFoundError)
check("nonexistent/../link", False, "/file")
check("nonexistent/../link", ALL_BUT_LAST, "/file")
check("nonexistent/../link", True, "/file")

check("broken", False, "/nonexistent")
check("broken", ALL_BUT_LAST, "/nonexistent")
check("broken", True, FileNotFoundError)
check("broken/", False, "/nonexistent")
check("broken/", ALL_BUT_LAST, "/nonexistent")
check("broken/", True, FileNotFoundError)
check("broken/file", False, "/nonexistent/file")
check("broken/file", ALL_BUT_LAST, FileNotFoundError)
check("broken/file", True, FileNotFoundError)
check("broken/../link", False, "/file")
check("broken/../link", ALL_BUT_LAST, "/file")
check("broken/../link", True, "/file")

check("cycle", False, "/cycle")
check("cycle", ALL_BUT_LAST, OSError, errno.EINVAL)
check("cycle", True, OSError, errno.EINVAL)
check("cycle/", False, "/cycle")
check("cycle/", ALL_BUT_LAST, OSError, errno.EINVAL)
check("cycle/", True, OSError, errno.EINVAL)
check("cycle/file", False, "/cycle/file")
check("cycle/file", ALL_BUT_LAST, OSError, errno.EINVAL)
check("cycle/file", True, OSError, errno.EINVAL)
check("cycle/../link", False, "/file")
check("cycle/../link", ALL_BUT_LAST, "/file")
check("cycle/../link", True, "/file")

def test_expandvars(self):
with os_helper.EnvironmentVarGuard() as env:
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy