Skip to content

Commit 8dd519e

Browse files
committed
python-stdlib/os-path: Replace join with POSIX-compliant implementation, add realpath.
The previous join() simply concatenated with '/' separators, failing for absolute path components and trailing separators. Replace with a POSIX-compliant implementation that handles absolute paths, empty parts, and trailing separators correctly. Add realpath as an alias for os.realpath. Made-with: Cursor
1 parent 5d21b33 commit 8dd519e

1 file changed

Lines changed: 19 additions & 5 deletions

File tree

python-stdlib/os-path/os/path.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,26 @@ def abspath(s):
1818
return s
1919

2020

21-
def join(*args):
22-
# TODO: this is non-compliant
23-
if type(args[0]) is bytes:
24-
return b"/".join(args)
21+
realpath = os.realpath
22+
23+
24+
def join(a, *p):
25+
"""Combine multiple path components using '/', adding separators as necessary.
26+
If an absolute path is encountered, all parts before it are ignored.
27+
If the final component is empty, the result will have a trailing separator."""
28+
if type(a) is bytes:
29+
sep = b"/"
2530
else:
26-
return "/".join(args)
31+
sep = "/"
32+
path = a
33+
for b in p:
34+
if b.startswith(sep) or not path:
35+
path = b
36+
elif path.endswith(sep):
37+
path += b
38+
else:
39+
path += sep + b
40+
return path
2741

2842

2943
def split(path):

0 commit comments

Comments
 (0)