10000 gh-126782: Support qualified referencing for `ntpath.abspath()` by nineteendo · Pull Request #126784 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-126782: Support qualified referencing for ntpath.abspath() #126784

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 19 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
Split pull request
  • Loading branch information
nineteendo committed Jun 2, 2024
commit a5cfbf2b6dc34bc08c8150010c60f0a742b3819d
48 changes: 18 additions & 30 deletions Lib/ntpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,49 +554,37 @@ def normpath(path):
return prefix + sep.join(comps)


def _abspath_fallback(path):
"""Return the absolute version of a path as a fallback function in case
`nt._getfullpathname` is not available or raises OSError. See bpo-31047 for
more.

"""

path = os.fspath(path)
if not isabs(path):
if isinstance(path, bytes):
cwd = os.getcwdb()
else:
cwd = os.getcwd()
path = join(cwd, path)
return normpath(path)

# Return an absolute path.
try:
from nt import _path_normpath_ex as _normpath
from nt import _getfullpathname

except ImportError: # not running on Windows - mock up something sensible
def abspath(path):
"""Return the absolute version of a path."""
path = os.fspath(path)
if not isabs(path):
if isinstance(path, bytes):
cwd = os.getcwdb()
else:
cwd = os.getcwd()
path = join(cwd, path)
return normpath(path)
abspath = _abspath_fallback

else: # use native Windows method on Windows
def abspath(path):
"""Return the absolute version of a path."""
try:
return _getfullpathname(_normpath(path, explicit_curdir=True))
except (OSError, ValueError):
# See gh-75230, handle outside for cleaner traceback
pass
path = os.fspath(path)
if not isabs(path):
if isinstance(path, bytes):
sep = b'/'
cwd = os.getcwdb()
else:
sep = '/'
cwd = os.getcwd()
drive, root, path = splitroot(path)
if drive and drive != splitroot(cwd)[0]:
try:
path = join(_getfullpathname(drive), path)
except (OSError, ValueError):
# Invalid drive \x00: on Windows; assume root directory
path = drive + sep + path
else:
path = join(cwd, root + path)
return normpath(path)
return _abspath_fallback(path)

try:
from nt import _findfirstfile, _getfinalpathname, readlink as _nt_readlink
Expand Down
3 changes: 0 additions & 3 deletions Lib/test/test_ntpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -808,9 +808,6 @@ def test_abspath(self):
tester('ntpath.abspath("C:\\spam. . .")', "C:\\spam")
tester('ntpath.abspath("C:/nul")', "\\\\.\\nul")
tester('ntpath.abspath("C:\\nul")', "\\\\.\\nul")
self.assertTrue(ntpath.isabs(ntpath.abspath("C:spam")))
self.assertTrue(ntpath.isabs(ntpath.abspath("C:\x00")))
self.assertTrue(ntpath.isabs(ntpath.abspath("\x00:spam")))
tester('ntpath.abspath("//..")', "\\\\")
tester('ntpath.abspath("//../")', "\\\\..\\")
tester('ntpath.abspath("//../..")', "\\\\..\\")
Expand Down
Loading
0