Skip to content

gh-115892: Fix ntpath.splitext() with UNC paths #115996

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions Lib/ntpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__


Expand Down
31 changes: 31 additions & 0 deletions Lib/test/test_ntpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Loading