Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita853610

Browse files
gh-119826: Improved fallback for ntpath.abspath() on Windows (GH-119938)
(cherry picked from commit4b00aba)Co-authored-by: Nice Zombies <nineteendo19d0@gmail.com>
1 parent219b826 commita853610

File tree

3 files changed

+35
-18
lines changed

3 files changed

+35
-18
lines changed

‎Lib/ntpath.py‎

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -553,36 +553,49 @@ def normpath(path):
553553
returnprefix+sep.join(comps)
554554

555555

556-
def_abspath_fallback(path):
557-
"""Return the absolute version of a path as a fallback function in case
558-
`nt._getfullpathname` is not available or raises OSError. See bpo-31047 for
559-
more.
560-
561-
"""
562-
563-
path=os.fspath(path)
564-
ifnotisabs(path):
565-
ifisinstance(path,bytes):
566-
cwd=os.getcwdb()
567-
else:
568-
cwd=os.getcwd()
569-
path=join(cwd,path)
570-
returnnormpath(path)
571-
572556
# Return an absolute path.
573557
try:
574558
fromntimport_getfullpathname
575559

576560
exceptImportError:# not running on Windows - mock up something sensible
577-
abspath=_abspath_fallback
561+
defabspath(path):
562+
"""Return the absolute version of a path."""
563+
path=os.fspath(path)
564+
ifnotisabs(path):
565+
ifisinstance(path,bytes):
566+
cwd=os.getcwdb()
567+
else:
568+
cwd=os.getcwd()
569+
path=join(cwd,path)
570+
returnnormpath(path)
578571

579572
else:# use native Windows method on Windows
580573
defabspath(path):
581574
"""Return the absolute version of a path."""
582575
try:
583576
return_getfullpathname(normpath(path))
584577
except (OSError,ValueError):
585-
return_abspath_fallback(path)
578+
# See gh-75230, handle outside for cleaner traceback
579+
pass
580+
path=os.fspath(path)
581+
ifnotisabs(path):
582+
ifisinstance(path,bytes):
583+
sep=b'\\'
584+
getcwd=os.getcwdb
585+
else:
586+
sep='\\'
587+
getcwd=os.getcwd
588+
drive,root,path=splitroot(path)
589+
# Either drive or root can be nonempty, but not both.
590+
ifdriveorroot:
591+
try:
592+
path=join(_getfullpathname(drive+root),path)
593+
except (OSError,ValueError):
594+
# Drive "\0:" cannot exist; use the root directory.
595+
path=drive+sep+path
596+
else:
597+
path=join(getcwd(),path)
598+
returnnormpath(path)
586599

587600
try:
588601
fromntimport_findfirstfile,_getfinalpathname,readlinkas_nt_readlink

‎Lib/test/test_ntpath.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,9 @@ def test_abspath(self):
811811
tester('ntpath.abspath("C:\\spam. . .")',"C:\\spam")
812812
tester('ntpath.abspath("C:/nul")',"\\\\.\\nul")
813813
tester('ntpath.abspath("C:\\nul")',"\\\\.\\nul")
814+
self.assertTrue(ntpath.isabs(ntpath.abspath("C:spam")))
815+
self.assertEqual(ntpath.abspath("C:\x00"),ntpath.join(ntpath.abspath("C:"),"\x00"))
816+
self.assertEqual(ntpath.abspath("\x00:spam"),"\x00:\\spam")
814817
tester('ntpath.abspath("//..")',"\\\\")
815818
tester('ntpath.abspath("//../")',"\\\\..\\")
816819
tester('ntpath.abspath("//../..")',"\\\\..\\")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Always return an absolute path for:func:`os.path.abspath` on Windows.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp