diff --git a/Lib/ntpath.py b/Lib/ntpath.py index 923a458a407097..5309f41e386c60 100644 --- a/Lib/ntpath.py +++ b/Lib/ntpath.py @@ -243,13 +243,13 @@ def split(p): def splitext(p): p = os.fspath(p) if isinstance(p, bytes): - seps = (b'\\', b'/') + prefixes = (b'\\\\', b'//', b'\\/', b'/\\') root, ext = genericpath._splitext(p, b'\\', b'/', b'.') else: - seps = ('\\', '/') + prefixes = ('\\\\', '//', '\\/', '/\\') root, ext = genericpath._splitext(p, '\\', '/', '.') - if (ext and root[:1] in seps and root[1:2] in seps and - root.count(seps[0], 2) + root.count(seps[1], 2) <= 1): + if ext and root.startswith(prefixes) and not splitroot(root)[2]: + # \\server.ext or \\server\share.ext, but not \\server\share\path.ext return p, p[:0] return root, ext splitext.__doc__ = genericpath._splitext.__doc__ diff --git a/Lib/test/test_ntpath.py b/Lib/test/test_ntpath.py index 696a6a621ffa46..94d482abeee43a 100644 --- a/Lib/test/test_ntpath.py +++ b/Lib/test/test_ntpath.py @@ -109,6 +109,15 @@ def test_splitext(self): tester(r'ntpath.splitext("//server.ext")', (r'//server.ext', '')) + tester(r'ntpath.splitext("\\?\UNC\server\share.ext")', + (r'\\?\UNC\server\share.ext', '')) + tester(r'ntpath.splitext("//?/UNC/server/share.ext")', + (r'//?/UNC/server/share.ext', '')) + tester(r'ntpath.splitext("\\?\UNC\server.ext")', + (r'\\?\UNC\server.ext', '')) + tester(r'ntpath.splitext("//?/UNC/server.ext")', + (r'//?/UNC/server.ext', '')) + tester(r'ntpath.splitext("\\server\share\file.ext")', (r'\\server\share\file', '.ext')) tester(r'ntpath.splitext("//server/share/file.ext")', @@ -117,6 +126,10 @@ def test_splitext(self): (r'\\server\share/file', '.ext')) tester(r'ntpath.splitext("//server/share\file.ext")', (r'//server/share\file', '.ext')) + tester(r'ntpath.splitext("\\?\UNC\server\share\file.ext")', + (r'\\?\UNC\server\share\file', '.ext')) + tester(r'ntpath.splitext("//?/UNC/server/share/file.ext")', + (r'//?/UNC/server/share/file', '.ext')) def test_splitdrive(self): tester("ntpath.splitdrive('')", ('', ''))