-
-
Notifications
You must be signed in to change notification settings - Fork 34k
gh-74453: Deprecate os.path.commonprefix #144436
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
base: main
Are you sure you want to change the base?
Changes from all commits
f381a5e
e8ca19c
cc320d1
879d6ec
1f2086b
166c39d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |
| from ntpath import ALL_BUT_LAST, ALLOW_MISSING | ||
| from test import support | ||
| from test.support import os_helper | ||
| from test.support import warnings_helper | ||
| from test.support.os_helper import FakePath | ||
| from test import test_genericpath | ||
| from tempfile import TemporaryFile | ||
|
|
@@ -298,12 +299,13 @@ def test_isabs(self): | |
| tester('ntpath.isabs("\\\\.\\C:")', 1) | ||
|
|
||
| def test_commonprefix(self): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto |
||
| tester('ntpath.commonprefix(["/home/swenson/spam", "/home/swen/spam"])', | ||
| "/home/swen") | ||
| tester('ntpath.commonprefix(["\\home\\swen\\spam", "\\home\\swen\\eggs"])', | ||
| "\\home\\swen\\") | ||
| tester('ntpath.commonprefix(["/home/swen/spam", "/home/swen/spam"])', | ||
| "/home/swen/spam") | ||
| with warnings_helper.check_warnings((".*commonpath().*", DeprecationWarning)): | ||
| tester('ntpath.commonprefix(["/home/swenson/spam", "/home/swen/spam"])', | ||
| "/home/swen") | ||
| tester('ntpath.commonprefix(["\\home\\swen\\spam", "\\home\\swen\\eggs"])', | ||
| "\\home\\swen\\") | ||
| tester('ntpath.commonprefix(["/home/swen/spam", "/home/swen/spam"])', | ||
| "/home/swen/spam") | ||
|
|
||
| def test_join(self): | ||
| tester('ntpath.join("")', '') | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,7 +1,6 @@ | ||||||
| """Various utility functions.""" | ||||||
|
|
||||||
| from collections import namedtuple, Counter | ||||||
| from os.path import commonprefix | ||||||
|
|
||||||
| __unittest = True | ||||||
|
|
||||||
|
|
@@ -21,13 +20,25 @@ def _shorten(s, prefixlen, suffixlen): | |||||
| s = '%s[%d chars]%s' % (s[:prefixlen], skip, s[len(s) - suffixlen:]) | ||||||
| return s | ||||||
|
|
||||||
| def _commonprefix(m, /): | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
as it's private, no need to enforce the / |
||||||
| if not m: | ||||||
| return "" | ||||||
| m = sorted(m) | ||||||
| prefix = m[0] | ||||||
| for item in m[1:]: | ||||||
| for i in range(len(prefix)): | ||||||
| if item[i] != prefix[i]: | ||||||
| prefix = prefix[:i] | ||||||
| break | ||||||
|
Comment on lines
+26
to
+32
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use the min/max + enumerate approach instead? |
||||||
| return prefix | ||||||
|
|
||||||
| def _common_shorten_repr(*args): | ||||||
| args = tuple(map(safe_repr, args)) | ||||||
| maxlen = max(map(len, args)) | ||||||
| if maxlen <= _MAX_LENGTH: | ||||||
| return args | ||||||
|
|
||||||
| prefix = commonprefix(args) | ||||||
| prefix = _commonprefix(args) | ||||||
| prefixlen = len(prefix) | ||||||
|
|
||||||
| common_len = _MAX_LENGTH - \ | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Deprecate :func:`os.path.commonprefix` in favor of | ||
| :func:`os.path.commonpath` for path segment prefixes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May I suggest that you have:
That way, the diff will be smaller