GH-114847: Speed up posixpath.realpath() (#114848)

Apply the following optimizations to `posixpath.realpath()`:

- Remove use of recursion
- Construct child paths directly rather than using `join()`
- Use `os.getcwd[b]()` rather than `abspath()`
- Use `startswith(sep)` rather than `isabs()`
- Use slicing rather than `split()`

Co-authored-by: Petr Viktorin <encukou@gmail.com>
This commit is contained in:
Barney Gale 2024-04-05 13:35:01 +01:00 committed by GitHub
parent 9ceaee74db
commit abfa16b44b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 64 additions and 34 deletions

View file

@ -456,6 +456,15 @@ class PosixPathTest(unittest.TestCase):
finally:
os_helper.unlink(ABSTFN)
@os_helper.skip_unless_symlink
@skip_if_ABSTFN_contains_backslash
def test_realpath_missing_pardir(self):
try:
os.symlink(os_helper.TESTFN + "1", os_helper.TESTFN)
self.assertEqual(realpath("nonexistent/../" + os_helper.TESTFN), ABSTFN + "1")
finally:
os_helper.unlink(os_helper.TESTFN)
@os_helper.skip_unless_symlink
@skip_if_ABSTFN_contains_backslash
def test_realpath_symlink_loops(self):