Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
nineteendo opened this issue Apr 8, 2024 · 0 comments · Fixed by #117638
Closed

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

nineteendo opened this issue Apr 8, 2024 · 0 comments · Fixed by #117638
Labels
performance Performance or resource usage stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@nineteendo
Copy link
Contributor

nineteendo commented Apr 8, 2024

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

@nineteendo nineteendo added the type-feature A feature request or enhancement label Apr 8, 2024
@AlexWaygood AlexWaygood added performance Performance or resource usage stdlib Python modules in the Lib dir labels Apr 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Performance or resource usage stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants