Skip to content

os.path.splitext() can split UNC drive on Windows #115892

Open
@barneygale

Description

@barneygale

Bug report

>>> import ntpath
>>> ntpath.splitext('//server/share.jpg')
('//server/share', '.jpg')  # expected: ('//server/share.jpg', '')

This path has an empty basename, and so its suffix should also be empty. Affects all current Python versions.

Linked PRs

Activity

added
3.11only security fixes
3.12only security fixes
3.13bugs and security fixes
on Feb 24, 2024
zooba

zooba commented on Feb 26, 2024

@zooba
Member

I don't think this is necessarily an "obvious" change to make. We don't currently validate anything about the path, so it works on relative and absolute paths alike. This would introduce validation at the start, making new assumptions about the type of path we've been given.

Meanwhile, there's nothing actually bad about splitting the share name like this? No file operation is going to work on it anyway, so you're about to get an error even if you don't at this step, and if you know you've got a UNC share or a directory path, why are you asking for its suffix?

I feel like this is too risky to backport, and I'm not convinced of the value going forward given that we currently don't require a full or relative path (and probably shouldn't).

added a commit that references this issue on Feb 27, 2024

pythongh-115892: Fix ntpath.splitext() with UNC paths

nineteendo

nineteendo commented on Apr 26, 2024

@nineteendo
Contributor

It's it worth fixing this with splitroot? Or do we close this?

zooba

zooba commented on Apr 29, 2024

@zooba
Member

I think we should close. Fixing this would suggest we should also detect directories with . and avoid splitting those, and that to me is clearly crossing the line. I'd rather keep this function simple and let it blindly split at the last dot after the last separator.

nineteendo

nineteendo commented on Apr 29, 2024

@nineteendo
Contributor

Fixing this would suggest we should also detect directories with . and avoid splitting those

I don't think that's what @barneygale had in mind. He doesn't like the inconsistency with ntpath.split():

>>> import ntpath
>>> ntpath.split('//server/share.jpg')
('//server/share.jpg', '') # empty basename
>>> ntpath.splitext('//server/share.jpg') 
('//server/share', '.jpg') # but extension?
zooba

zooba commented on Apr 29, 2024

@zooba
Member

I'm sure I can come up with a justification, but really, both operations are somewhat nonsense (or more politely, a boundary condition), so the result can be arbitrary. Arbitrarily, I'd like the splitext implementation that is simpler, faster, and more reliable.

(I guess you could reasonably argue that split(path_with_no_basename) is a valid base case for a recursive algorithm, but I think you'd struggle to say the same for splitext(path_with_no_filename). So there's a justification. But it's really just the simpler implementation that I want.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.11only security fixes3.12only security fixes3.13bugs and security fixesOS-windowstype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @barneygale@zooba@serhiy-storchaka@nineteendo

        Issue actions

          `os.path.splitext()` can split UNC drive on Windows · Issue #115892 · python/cpython