Skip to content

Remove redundant type check in os.path.join() #117636

Description

@nineteendo

Feature or enhancement

Proposal:

These type checks were introduced a long time ago in 5bfc03f and became redundant in 3f9183b with the addition of os.fspath(). They can be safely removed, slightly speeding up os.path.join() in the process:

 def join(path, *paths):
     path = os.fspath(path)
     if isinstance(path, bytes):
         sep = b'\\'
         seps = b'\\/'
         colon_seps = b':\\/'
     else:
         sep = '\\'
         seps = '\\/'
         colon_seps = ':\\/'
     try:
-        if not paths:
-            path[:0] + sep  #23780: Ensure compatible data type even if p is null.
 def join(a, *p):
     """Join two or more pathname components, inserting '/' as needed.
     If any component is an absolute path, all previous path components
     will be discarded.  An empty last part will result in a path that
     ends with a separator."""
     a = os.fspath(a)
     sep = _get_sep(a)
     path = a
     try:
-        if not p:
-            path[:0] + sep  #23780: Ensure compatible data type even if p is null.

I also noticed we're concatenating b to an empty string in posixpath.join() in case path has a length of 0. Which we can fix quite easily:

-if b.startswith(sep):
+if b.startswith(sep) or not path:
     path = b
-elif not path or path.endswith(sep):
+elif path.endswith(sep):
     path += b

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    performancePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions