From 17c8e261418577982700b829f1eeca6bc77d0513 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Tue, 27 Feb 2024 19:13:28 +0200 Subject: [PATCH] gh-115892: Fix ntpath.splitext() with UNC paths os.path.splitext() on Windows no longer splits an "extension" from the server or share name in the UNC path. --- Lib/ntpath.py | 10 ++++++++-- Lib/test/test_ntpath.py | 18 ++++++++++++++++++ ...4-02-27-19-13-25.gh-issue-115892.JouB3p.rst | 2 ++ 3 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2024-02-27-19-13-25.gh-issue-115892.JouB3p.rst diff --git a/Lib/ntpath.py b/Lib/ntpath.py index e7cbfe17ecb3c8..33317a1c693ac3 100644 --- a/Lib/ntpath.py +++ b/Lib/ntpath.py @@ -249,9 +249,15 @@ def split(p): def splitext(p): p = os.fspath(p) if isinstance(p, bytes): - return genericpath._splitext(p, b'\\', b'/', b'.') + seps = (b'\\', b'/') + root, ext = genericpath._splitext(p, b'\\', b'/', b'.') else: - return genericpath._splitext(p, '\\', '/', '.') + seps = ('\\', '/') + 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): + 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 9cb03e3cd5de8d..1732489d4c17f7 100644 --- a/Lib/test/test_ntpath.py +++ b/Lib/test/test_ntpath.py @@ -100,6 +100,24 @@ 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("\\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')) + 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.