Skip to content

os.path.commonprefix returns insane results #158994

Description

@darryldixon

Bug report

>>> os.path.commonprefix(('root/blah', 'rawr/oaeu'))
'r'
>>> os.path.commonprefix(('/root/blah', '/rawr/oaeu'))
'/r'
>>> 

This is clearly not a sane result.

To be clear, a sane result would look like
''
for the first example, and
'/'
for the second example.

This would then be consistent with how the module behaves for other examples:

os.path.commonprefix(('/root/blah', '/root/oaeu'))
'/root/'

This is also insane:

os.path.commonprefix(('/root/blah', '/root/baeu'))
'/root/b'

os.path.commonprefix(('/root/blah', '/root/baeu'))
'/root/b'

The returned example is not a "path". It is not a 'path prefix' in any meaningful sense. This is just a string match up to the point the string changes. It has nothing whatsoever to do with the notion of a 'path'.

CPython versions tested on:

3.12

Operating systems tested on:

Linux

Activity

  1. StanFromIreland commented on Oct 7, 2026

    @StanFromIreland
    Member

    Please see the documentation for newer versions, it now clearly explains what the function does, and why it's probably not what you want. You may also find this blog post interesting: https://sethmlarson.dev/deprecate-confusing-apis-like-os-path-commonprefix

  2. darryldixon commented on Oct 7, 2026

    @darryldixon
    Author

    Incredible that this one survived all the way to 3.5 and beyond when many, many other functions were quite happily simply backwards-incompatibly broken. Amazing. Anyway.

  3. gvanrossum commented on Oct 7, 2026

    @gvanrossum
    Member

    @darryldixon

    Your hyperbolic language is unnecessary and a blemish on your reputation.

  4. darryldixon commented on Oct 7, 2026

    @darryldixon
    Author

    Sorry you took it personally. I think the blog post linked earlier captures the surprise of the wider community over the course of decades quite well, and on the basis that this exact behaviour has been involved in (multiple) CVE security issues I disagree on the characterisation of my language as "hyperbolic". To each their own, thanks for Python, we love it despite its occasional weirdness regardless.

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

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions