Skip to content

Special case for "" & "." in posixpath.abspath() #117639

Closed as not planned
Closed as not planned
@nineteendo

Description

@nineteendo

Feature or enhancement

Proposal:

We could add a special case for the current directory and return it without normalising. This speeds up posixpath.relpath() with one argument.

 def abspath(path):
     """Return an absolute path."""
     path = os.fspath(path)
     if isinstance(path, bytes):
-        if not path.startswith(b'/'):
-            path = join(os.getcwdb(), path)
+        sep = b'/'
+        curdir = b'.'
+        getcwd = os.getcwdb
     else:
-        if not path.startswith('/'):
-            path = join(os.getcwd(), path)
+        sep = '/'
+        curdir = '.'
+        getcwd = os.getcwd
+    if not path.startswith(sep):
+        if not path or path == curdir:
+            return getcwd()
+        path = join(getcwd(), path)
     return normpath(path)

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    performancePerformance or resource usagestdlibPython modules in the Lib dirtype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions