This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Deprecate os.path.commonprefix
Type: Stage:
Components: Library (Lib) Versions:
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Valentin.Lorentz, brett.cannon, nedbat, rhettinger, serhiy.storchaka, vstinner
Priority: low Keywords:

Created on 2017-05-04 16:02 by Valentin.Lorentz, last changed 2022-04-11 14:58 by admin.

Messages (6)
msg292993 - (view) Author: ProgVal (Valentin.Lorentz) Date: 2017-05-04 16:02
The function os.path.commonprefix computes the longest prefix of strings (any iterable, actually), regardless of their meaning as paths.

I do not see any reason to use this function for paths, and keeping it in the os.path module makes it prone to be confused with os.path.commonpath (which was introduced in Python 3.5).

I believe making this function raise a DeprecationWarning would help avoid having this kind of bugs.
msg292994 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-05-04 16:07
Ned Batchelder wrote an article about this function in 2010 :-)

The docs helpfully include the warning:

    Note that this may return invalid paths because it works a character at a time.

But it should say:

    This function is in the wrong place, and has nothing to do with paths, don't use it if you are interested in file paths!
msg293004 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-05-04 18:47
We shouldn't deprecate a function until add an alternative. There is a working alternative for paths, but commonprefix() is used in the wild for non-paths (for example in unittest.util). The problem is that there is no right place for this function. The string module is wrong place because commonprefix() supports not only strings. Perhaps the new seqtools module would be a right place.
msg293140 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2017-05-05 21:24
I agree with Serhiy that it might be time to create a seqtools module.
msg293173 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2017-05-06 20:16
The ``os.path.commonprefix`` function has been around for a very long time.  Deprecating it will just cause unnecessary pain for users and make it harder to upgrade to Python 3.  The function isn't broken, the only issue here is that a new function was added with a similar name.
msg413320 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2022-02-16 07:38
For now, there are three uses of commonprefix() in the stdlib:

1. In urllib.request it causes a security issue (see issue46756). commonpath() or just str.startswith() should be used instead.

2. In lib2to3.main. The code contains a workaround around commonprefix(). It could be simplified by using commonpath().

3. In unittest.util. This is the only correct use of commonprefix(). It cannot be replaced by commonpath().

I think that we should add commonprefix() in the string module (or maybe in a new module for operations on sequences), deprecate os.path.commonprefix() in documentation only, several versions later add a deprecation warning in os.path.commonprefix(), and several versions later remove os.path.commonprefix().
Date User Action Args
2022-04-11 14:58:46adminsetgithub: 74453
2022-02-16 07:38:29serhiy.storchakasetmessages: + msg413320
2017-05-06 20:16:23rhettingersetpriority: normal -> low
nosy: + rhettinger
messages: + msg293173

2017-05-05 21:24:03brett.cannonsetnosy: + brett.cannon
messages: + msg293140
2017-05-04 18:47:41serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg293004
2017-05-04 16:07:06vstinnersetnosy: + nedbat, vstinner
messages: + msg292994
2017-05-04 16:02:33Valentin.Lorentzcreate