Skip to content

Commit

Permalink
pythongh-115892: Fix ntpath.splitext() with UNC paths
Browse files Browse the repository at this point in the history
os.path.splitext() on Windows no longer splits an "extension" from
the server or share name in the UNC path.
  • Loading branch information
serhiy-storchaka committed Feb 27, 2024
1 parent 3a72fc3 commit 17c8e26
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
10 changes: 8 additions & 2 deletions Lib/ntpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__


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

0 comments on commit 17c8e26

Please sign in to comment.