You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Remove redundant type check in os.path.join() #117636
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
Activity
gh-117636: Remove redundant type check in `os.path.join()` (#117638)
pythongh-117636: Remove redundant type check in `os.path.join()` (pyt…