Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork34k
gh-143658: importlib.metadata: Usestr.translate to improve performance ofimportlib.metadata.Prepared.normalized#143660
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
76e227200c76fcc1bb3cc0eae5521657880ee0e6aa7b7f9a8File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -890,6 +890,14 @@ def search(self, prepared: Prepared): | ||
| return itertools.chain(infos, eggs) | ||
| # Translation table for Prepared.normalize: lowercase and | ||
| # replace "-" (hyphen) and "." (dot) with "_" (underscore). | ||
| _normalize_table = str.maketrans( | ||
| "ABCDEFGHIJKLMNOPQRSTUVWXYZ-.", | ||
| "abcdefghijklmnopqrstuvwxyz__", | ||
hugovk marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| ) | ||
| class Prepared: | ||
| """ | ||
| A prepared search query for metadata on a possibly-named package. | ||
| @@ -925,7 +933,13 @@ def normalize(name): | ||
| """ | ||
| PEP 503 normalization plus dashes as underscores. | ||
| """ | ||
| # Emulates ``re.sub(r"[-_.]+", "-", name).lower()`` from PEP 503 | ||
hugovk marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| # About 3x faster, safe since packages only support alphanumeric characters | ||
| value = name.translate(_normalize_table) | ||
| # Condense repeats (faster than regex) | ||
| while "__" in value: | ||
| value = value.replace("__", "_") | ||
| return value | ||
| @staticmethod | ||
| def legacy_normalize(name): | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -6,6 +6,7 @@ | ||
| from importlib.metadata import ( | ||
| Distribution, | ||
| PackageNotFoundError, | ||
| Prepared, | ||
| distribution, | ||
| entry_points, | ||
| files, | ||
| @@ -313,3 +314,36 @@ class InvalidateCache(unittest.TestCase): | ||
| def test_invalidate_cache(self): | ||
| # No externally observable behavior, but ensures test coverage... | ||
| importlib.invalidate_caches() | ||
| class PreparedTests(unittest.TestCase): | ||
| def test_normalize(self): | ||
| tests = [ | ||
| # Simple | ||
| ("sample", "sample"), | ||
| # Mixed case | ||
| ("Sample", "sample"), | ||
| ("SAMPLE", "sample"), | ||
| ("SaMpLe", "sample"), | ||
| # Separator conversions | ||
| ("sample-pkg", "sample_pkg"), | ||
| ("sample.pkg", "sample_pkg"), | ||
| ("sample_pkg", "sample_pkg"), | ||
| # Multiple separators | ||
| ("sample---pkg", "sample_pkg"), | ||
| ("sample___pkg", "sample_pkg"), | ||
| ("sample...pkg", "sample_pkg"), | ||
| # Mixed separators | ||
| ("sample-._pkg", "sample_pkg"), | ||
| ("sample_.-pkg", "sample_pkg"), | ||
| # Complex | ||
| ("Sample__Pkg-name.foo", "sample_pkg_name_foo"), | ||
hugovk marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| ("Sample__Pkg.name__foo", "sample_pkg_name_foo"), | ||
| # Uppercase with separators | ||
| ("SAMPLE-PKG", "sample_pkg"), | ||
| ("Sample.Pkg", "sample_pkg"), | ||
| ("SAMPLE_PKG", "sample_pkg"), | ||
| ] | ||
| for name, expected in tests: | ||
| with self.subTest(name=name): | ||
| self.assertEqual(Prepared.normalize(name), expected) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| :mod:`importlib.metadata`: Use :meth:`str.translate` to improve performance of | ||
| :meth:`!importlib.metadata.Prepared.normalize`. Patch by Hugo van Kemenade and | ||
| Henry Schreiner. |
Uh oh!
There was an error while loading.Please reload this page.