diff --git a/Lib/ntpath.py b/Lib/ntpath.py index 1b1873f08b608b..5309f41e386c60 100644 --- a/Lib/ntpath.py +++ b/Lib/ntpath.py @@ -243,9 +243,15 @@ def split(p): def splitext(p): p = os.fspath(p) if isinstance(p, bytes): - return genericpath._splitext(p, b'\\', b'/', b'.') + prefixes = (b'\\\\', b'//', b'\\/', b'/\\') + root, ext = genericpath._splitext(p, b'\\', b'/', b'.') else: - return genericpath._splitext(p, '\\', '/', '.') + prefixes = ('\\\\', '//', '\\/', '/\\') + root, ext = genericpath._splitext(p, '\\', '/', '.') + 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 64cbfaaaaa0690..94d482abeee43a 100644 --- a/Lib/test/test_ntpath.py +++ b/Lib/test/test_ntpath.py @@ -100,6 +100,37 @@ def test_splitext(self): tester('ntpath.splitext("xx\\foo.bar.ext")', ('xx\\foo.bar', '.ext')) tester('ntpath.splitext("c:a/b\\c.d")', ('c:a/b\\c', '.d')) + tester(r'ntpath.splitext("\\server\share.ext")', + (r'\\server\share.ext', '')) + tester(r'ntpath.splitext("//server/share.ext")', + (r'//server/share.ext', '')) + tester(r'ntpath.splitext("\\server.ext")', + (r'\\server.ext', '')) + 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")', + (r'//server/share/file', '.ext')) + tester(r'ntpath.splitext("\\server\share/file.ext")', + (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('')", ('', '')) tester("ntpath.splitdrive('foo')", ('', 'foo')) diff --git a/Misc/NEWS.d/next/Library/2024-02-27-19-13-25.gh-issue-115892.JouB3p.rst b/Misc/NEWS.d/next/Library/2024-02-27-19-13-25.gh-issue-115892.JouB3p.rst new file mode 100644 index 00000000000000..c0ad89a4b0c11c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-02-27-19-13-25.gh-issue-115892.JouB3p.rst @@ -0,0 +1,2 @@ +:func:`os.path.splitext` on Windows no longer splits an "extension" from the +server or share name in the UNC path.