| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -553,36 +553,49 @@ def normpath(path): | |||
| 553 | 553 | return prefix + sep.join(comps) | |
| 554 | 554 | ||
| 555 | 555 | ||
| 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 | - if not isabs(path): | ||
| 565 | - if isinstance(path, bytes): | ||
| 566 | - cwd = os.getcwdb() | ||
| 567 | - else: | ||
| 568 | - cwd = os.getcwd() | ||
| 569 | - path = join(cwd, path) | ||
| 570 | - return normpath(path) | ||
| 571 | - | ||
| 572 | 556 | # Return an absolute path. | |
| 573 | 557 | try: | |
| 574 | 558 | from nt import _getfullpathname | |
| 575 | 559 | ||
| 576 | 560 | except ImportError: # not running on Windows - mock up something sensible | |
| 577 | - abspath = _abspath_fallback | ||
| 561 | + def abspath(path): | ||
| 562 | + """Return the absolute version of a path.""" | ||
| 563 | + path = os.fspath(path) | ||
| 564 | + if not isabs(path): | ||
| 565 | + if isinstance(path, bytes): | ||
| 566 | + cwd = os.getcwdb() | ||
| 567 | + else: | ||
| 568 | + cwd = os.getcwd() | ||
| 569 | + path = join(cwd, path) | ||
| 570 | + return normpath(path) | ||
| 578 | 571 | ||
| 579 | 572 | else: # use native Windows method on Windows | |
| 580 | 573 | def abspath(path): | |
| 581 | 574 | """Return the absolute version of a path.""" | |
| 582 | 575 | try: | |
| 583 | 576 | return _getfullpathname(normpath(path)) | |
| 584 | 577 | 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 | + if not isabs(path): | ||
| 582 | + if isinstance(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 | + if drive or root: | ||
| 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 | + return normpath(path) | ||
| 586 | 599 | ||
| 587 | 600 | try: | |
| 588 | 601 | from nt import _findfirstfile, _getfinalpathname, readlink as _nt_readlink | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -806,6 +806,9 @@ def test_abspath(self): | |||
| 806 | 806 | tester('ntpath.abspath("C:\\spam. . .")', "C:\\spam") | |
| 807 | 807 | tester('ntpath.abspath("C:/nul")', "\\\\.\\nul") | |
| 808 | 808 | tester('ntpath.abspath("C:\\nul")', "\\\\.\\nul") | |
| 809 | + self.assertTrue(ntpath.isabs(ntpath.abspath("C:spam"))) | ||
| 810 | + self.assertEqual(ntpath.abspath("C:\x00"), ntpath.join(ntpath.abspath("C:"), "\x00")) | ||
| 811 | + self.assertEqual(ntpath.abspath("\x00:spam"), "\x00:\\spam") | ||
| 809 | 812 | tester('ntpath.abspath("//..")', "\\\\") | |
| 810 | 813 | tester('ntpath.abspath("//../")', "\\\\..\\") | |
| 811 | 814 | tester('ntpath.abspath("//../..")', "\\\\..\\") | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1 @@ | |||
| 1 | + Always return an absolute path for :func:`os.path.abspath` on Windows. | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments