Skip to content

Commit 31f5121

Browse files
Issue #19456: ntpath.join() now joins relative paths correctly when a drive
is present.
1 parent 027ab39 commit 31f5121

3 files changed

Lines changed: 61 additions & 68 deletions

File tree

‎Lib/ntpath.py‎

Lines changed: 24 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -59,61 +59,30 @@ def isabs(s):
5959

6060

6161
# Join two (or more) paths.
62-
63-
def join(a, *p):
64-
"""Join two or more pathname components, inserting "\\" as needed.
65-
If any component is an absolute path, all previous path components
66-
will be discarded."""
67-
path = a
68-
for b in p:
69-
b_wins = 0 # set to 1 iff b makes path irrelevant
70-
if path == "":
71-
b_wins = 1
72-
73-
elif isabs(b):
74-
# This probably wipes out path so far. However, it's more
75-
# complicated if path begins with a drive letter:
76-
# 1. join('c:', '/a') == 'c:/a'
77-
# 2. join('c:/', '/a') == 'c:/a'
78-
# But
79-
# 3. join('c:/a', '/b') == '/b'
80-
# 4. join('c:', 'd:/') = 'd:/'
81-
# 5. join('c:/', 'd:/') = 'd:/'
82-
if path[1:2] != ":" or b[1:2] == ":":
83-
# Path doesn't start with a drive letter, or cases 4 and 5.
84-
b_wins = 1
85-
86-
# Else path has a drive letter, and b doesn't but is absolute.
87-
elif len(path) > 3 or (len(path) == 3 and
88-
path[-1] not in "/\\"):
89-
# case 3
90-
b_wins = 1
91-
92-
if b_wins:
93-
path = b
94-
else:
95-
# Join, and ensure there's a separator.
96-
assert len(path) > 0
97-
if path[-1] in "/\\":
98-
if b and b[0] in "/\\":
99-
path += b[1:]
100-
else:
101-
path += b
102-
elif path[-1] == ":":
103-
path += b
104-
elif b:
105-
if b[0] in "/\\":
106-
path += b
107-
else:
108-
path += "\\" + b
109-
else:
110-
# path is not empty and does not end with a backslash,
111-
# but b is empty; since, e.g., split('a/') produces
112-
# ('a', ''), it's best if join() adds a backslash in
113-
# this case.
114-
path += '\\'
115-
116-
return path
62+
def join(path, *paths):
63+
"""Join two or more pathname components, inserting "\\" as needed."""
64+
result_drive, result_path = splitdrive(path)
65+
for p in paths:
66+
p_drive, p_path = splitdrive(p)
67+
if p_path and p_path[0] in '\\/':
68+
# Second path is absolute
69+
if p_drive or not result_drive:
70+
result_drive = p_drive
71+
result_path = p_path
72+
continue
73+
elif p_drive and p_drive != result_drive:
74+
if p_drive.lower() != result_drive.lower():
75+
# Different drives => ignore the first path entirely
76+
result_drive = p_drive
77+
result_path = p_path
78+
continue
79+
# Same drive in different case
80+
result_drive = p_drive
81+
# Second path is relative to the first
82+
if result_path and result_path[-1] not in '\\/':
83+
result_path = result_path + '\\'
84+
result_path = result_path + p_path
85+
return result_drive + result_path
11786

11887

11988
# Split a path in a drive specification (a drive letter followed by a

‎Lib/test/test_ntpath.py‎

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,33 +85,54 @@ def test_join(self):
8585
tester('ntpath.join("/a")', '/a')
8686
tester('ntpath.join("\\a")', '\\a')
8787
tester('ntpath.join("a:")', 'a:')
88-
tester('ntpath.join("a:", "b")', 'a:b')
89-
tester('ntpath.join("a:", "/b")', 'a:/b')
9088
tester('ntpath.join("a:", "\\b")', 'a:\\b')
91-
tester('ntpath.join("a", "/b")', '/b')
9289
tester('ntpath.join("a", "\\b")', '\\b')
9390
tester('ntpath.join("a", "b", "c")', 'a\\b\\c')
9491
tester('ntpath.join("a\\", "b", "c")', 'a\\b\\c')
9592
tester('ntpath.join("a", "b\\", "c")', 'a\\b\\c')
9693
tester('ntpath.join("a", "b", "\\c")', '\\c')
9794
tester('ntpath.join("d:\\", "\\pleep")', 'd:\\pleep')
9895
tester('ntpath.join("d:\\", "a", "b")', 'd:\\a\\b')
99-
tester("ntpath.join('c:', '/a')", 'c:/a')
100-
tester("ntpath.join('c:/', '/a')", 'c:/a')
101-
tester("ntpath.join('c:/a', '/b')", '/b')
102-
tester("ntpath.join('c:', 'd:/')", 'd:/')
103-
tester("ntpath.join('c:/', 'd:/')", 'd:/')
104-
tester("ntpath.join('c:/', 'd:/a/b')", 'd:/a/b')
105-
106-
tester("ntpath.join('')", '')
107-
tester("ntpath.join('', '', '', '', '')", '')
108-
tester("ntpath.join('a')", 'a')
96+
10997
tester("ntpath.join('', 'a')", 'a')
11098
tester("ntpath.join('', '', '', '', 'a')", 'a')
11199
tester("ntpath.join('a', '')", 'a\\')
112100
tester("ntpath.join('a', '', '', '', '')", 'a\\')
113101
tester("ntpath.join('a\\', '')", 'a\\')
114102
tester("ntpath.join('a\\', '', '', '', '')", 'a\\')
103+
tester("ntpath.join('a/', '')", 'a/')
104+
105+
tester("ntpath.join('a/b', 'x/y')", 'a/b\\x/y')
106+
tester("ntpath.join('/a/b', 'x/y')", '/a/b\\x/y')
107+
tester("ntpath.join('/a/b/', 'x/y')", '/a/b/x/y')
108+
tester("ntpath.join('c:', 'x/y')", 'c:x/y')
109+
tester("ntpath.join('c:a/b', 'x/y')", 'c:a/b\\x/y')
110+
tester("ntpath.join('c:a/b/', 'x/y')", 'c:a/b/x/y')
111+
tester("ntpath.join('c:/', 'x/y')", 'c:/x/y')
112+
tester("ntpath.join('c:/a/b', 'x/y')", 'c:/a/b\\x/y')
113+
tester("ntpath.join('c:/a/b/', 'x/y')", 'c:/a/b/x/y')
114+
#tester("ntpath.join('//computer/share', 'x/y')", '//computer/share\\x/y')
115+
#tester("ntpath.join('//computer/share/', 'x/y')", '//computer/share/x/y')
116+
#tester("ntpath.join('//computer/share/a/b', 'x/y')", '//computer/share/a/b\\x/y')
117+
118+
tester("ntpath.join('a/b', '/x/y')", '/x/y')
119+
tester("ntpath.join('/a/b', '/x/y')", '/x/y')
120+
tester("ntpath.join('c:', '/x/y')", 'c:/x/y')
121+
tester("ntpath.join('c:a/b', '/x/y')", 'c:/x/y')
122+
tester("ntpath.join('c:/', '/x/y')", 'c:/x/y')
123+
tester("ntpath.join('c:/a/b', '/x/y')", 'c:/x/y')
124+
#tester("ntpath.join('//computer/share', '/x/y')", '//computer/share/x/y')
125+
#tester("ntpath.join('//computer/share/', '/x/y')", '//computer/share/x/y')
126+
#tester("ntpath.join('//computer/share/a', '/x/y')", '//computer/share/x/y')
127+
128+
tester("ntpath.join('c:', 'C:x/y')", 'C:x/y')
129+
tester("ntpath.join('c:a/b', 'C:x/y')", 'C:a/b\\x/y')
130+
tester("ntpath.join('c:/', 'C:x/y')", 'C:/x/y')
131+
tester("ntpath.join('c:/a/b', 'C:x/y')", 'C:/a/b\\x/y')
132+
133+
for x in ('', 'a/b', '/a/b', 'c:', 'c:a/b', 'c:/', 'c:/a/b'):
134+
for y in ('d:', 'd:x/y', 'd:/', 'd:/x/y'):
135+
tester("ntpath.join(%r, %r)" % (x, y), y)
115136

116137
def test_normpath(self):
117138
tester("ntpath.normpath('A//////././//.//B')", r'A\B')

‎Misc/NEWS‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ Core and Builtins
3838
Library
3939
-------
4040

41+
- Issue #19456: ntpath.join() now joins relative paths correctly when a drive
42+
is present.
43+
4144
- Issue #8260: The read(), readline() and readlines() methods of
4245
codecs.StreamReader returned incomplete data when were called after
4346
readline() or read(size). Based on patch by Amaury Forgeot d'Arc.

0 commit comments

Comments
 (0)