diff -r 87d49e2cdd34 Lib/ntpath.py --- a/Lib/ntpath.py Sun Nov 03 17:00:51 2013 +1000 +++ b/Lib/ntpath.py Mon Nov 04 00:52:49 2013 +0200 @@ -100,83 +100,36 @@ return len(s) > 0 and s[:1] in _get_bothseps(s) +# Concatenates two simple paths (no drives) using sep as a separator +def _join(first, second, sep): + seps = _get_bothseps(first) + if first: + if first[-1:] in seps: + return first + second + return first + sep + second + return second + + # Join two (or more) paths. - -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.""" - sep = _get_sep(a) - seps = _get_bothseps(a) - colon = _get_colon(a) - path = a - for b in p: - b_wins = 0 # set to 1 iff b makes path irrelevant - if not path: - b_wins = 1 - - elif isabs(b): - # This probably wipes out path so far. However, it's more - # complicated if path begins with a drive letter. You get a+b - # (minus redundant slashes) in these four cases: - # 1. join('c:', '/a') == 'c:/a' - # 2. join('//computer/share', '/a') == '//computer/share/a' - # 3. join('c:/', '/a') == 'c:/a' - # 4. join('//computer/share/', '/a') == '//computer/share/a' - # But b wins in all of these cases: - # 5. join('c:/a', '/b') == '/b' - # 6. join('//computer/share/a', '/b') == '/b' - # 7. join('c:', 'd:/') == 'd:/' - # 8. join('c:', '//computer/share/') == '//computer/share/' - # 9. join('//computer/share', 'd:/') == 'd:/' - # 10. join('//computer/share', '//computer/share/') == '//computer/share/' - # 11. join('c:/', 'd:/') == 'd:/' - # 12. join('c:/', '//computer/share/') == '//computer/share/' - # 13. join('//computer/share/', 'd:/') == 'd:/' - # 14. join('//computer/share/', '//computer/share/') == '//computer/share/' - b_prefix, b_rest = splitdrive(b) - - # if b has a prefix, it always wins. - if b_prefix: - b_wins = 1 - else: - # b doesn't have a prefix. - # but isabs(b) returned true. - # and therefore b_rest[0] must be a slash. - # (but let's check that.) - assert(b_rest and b_rest[0] in seps) - - # so, b still wins if path has a rest that's more than a sep. - # you get a+b if path_rest is empty or only has a sep. - # (see cases 1-4 for times when b loses.) - path_rest = splitdrive(path)[1] - b_wins = path_rest and path_rest not in seps - - if b_wins: - path = b +def join(path, *paths): + sep = _get_sep(path) + colon = _get_colon(path) + result_drive, result_path = splitdrive(path) + for p in paths: + p_drive, p_path = splitdrive(p) + # check if drive or UNC changed; if it did, update everything + if p_drive and p_drive != result_drive: + result_drive, result_path = p_drive, p_path + # check if new path is absolute; if it is, update path + if isabs(p_path): + result_path = p_path + sep = p_path[0] else: - # Join, and ensure there's a separator. - assert len(path) > 0 - if path[-1:] in seps: - if b and b[:1] in seps: - path += b[1:] - else: - path += b - elif path[-1:] == colon: - path += b - elif b: - if b[:1] in seps: - path += b - else: - path += sep + b - else: - # path is not empty and does not end with a backslash, - # but b is empty; since, e.g., split('a/') produces - # ('a', ''), it's best if join() adds a backslash in - # this case. - path += sep - - return path + result_path = _join(result_path, p_path, sep) + # add separator between UNC and non-absolute path + if not isabs(result_path) and result_drive and result_drive[-1:] != colon: + return result_drive + sep + result_path + return result_drive + result_path # Split a path in a drive specification (a drive letter followed by a diff -r 87d49e2cdd34 Lib/test/test_ntpath.py --- a/Lib/test/test_ntpath.py Sun Nov 03 17:00:51 2013 +1000 +++ b/Lib/test/test_ntpath.py Mon Nov 04 00:52:49 2013 +0200 @@ -113,7 +113,7 @@ tester('ntpath.join("d:\\", "a", "b")', 'd:\\a\\b') tester("ntpath.join('c:', '/a')", 'c:/a') tester("ntpath.join('c:/', '/a')", 'c:/a') - tester("ntpath.join('c:/a', '/b')", '/b') + tester("ntpath.join('c:/a', '/b')", 'c:/b') tester("ntpath.join('c:', 'd:/')", 'd:/') tester("ntpath.join('c:/', 'd:/')", 'd:/') tester("ntpath.join('c:/', 'd:/a/b')", 'd:/a/b') @@ -133,8 +133,8 @@ tester("ntpath.join('//computer/share', '/a')", '//computer/share/a') tester("ntpath.join('c:/', '/a')", 'c:/a') tester("ntpath.join('//computer/share/', '/a')", '//computer/share/a') - tester("ntpath.join('c:/a', '/b')", '/b') - tester("ntpath.join('//computer/share/a', '/b')", '/b') + tester("ntpath.join('c:/a', '/b')", 'c:/b') + tester("ntpath.join('//computer/share/a', '/b')", '//computer/share/b') tester("ntpath.join('c:', 'd:/')", 'd:/') tester("ntpath.join('c:', '//computer/share/')", '//computer/share/') tester("ntpath.join('//computer/share', 'd:/')", 'd:/')