summaryrefslogtreecommitdiffstats
path: root/Lib/importlib
diff options
context:
space:
mode:
authorJason R. Coombs <jaraco@jaraco.com>2022-11-10 15:59:38 (GMT)
committerGitHub <noreply@github.com>2022-11-10 15:59:38 (GMT)
commit9a5ca31af0ee07e5401ec7e0dda8ff1d035ce32d (patch)
tree4f24bc46f3b81d4482068507b0299eab1750bf34 /Lib/importlib
parente6f066af2e15b4da17866a375e11010535d9c692 (diff)
downloadcpython-9a5ca31af0ee07e5401ec7e0dda8ff1d035ce32d.zip
cpython-9a5ca31af0ee07e5401ec7e0dda8ff1d035ce32d.tar.gz
cpython-9a5ca31af0ee07e5401ec7e0dda8ff1d035ce32d.tar.bz2
[3.10] gh-99130: Apply bugfixes from importlib_metadata 4.11.4. (#99132)
Diffstat (limited to 'Lib/importlib')
-rw-r--r--Lib/importlib/metadata/__init__.py22
-rw-r--r--Lib/importlib/metadata/_functools.py19
2 files changed, 36 insertions, 5 deletions
diff --git a/Lib/importlib/metadata/__init__.py b/Lib/importlib/metadata/__init__.py
index b3063cd..682067d 100644
--- a/Lib/importlib/metadata/__init__.py
+++ b/Lib/importlib/metadata/__init__.py
@@ -17,7 +17,7 @@ import collections
from . import _adapters, _meta
from ._meta import PackageMetadata
from ._collections import FreezableDefaultDict, Pair
-from ._functools import method_cache
+from ._functools import method_cache, pass_none
from ._itertools import unique_everseen
from ._meta import PackageMetadata, SimplePath
@@ -938,13 +938,25 @@ class PathDistribution(Distribution):
normalized name from the file system path.
"""
stem = os.path.basename(str(self._path))
- return self._name_from_stem(stem) or super()._normalized_name
+ return (
+ pass_none(Prepared.normalize)(self._name_from_stem(stem))
+ or super()._normalized_name
+ )
- def _name_from_stem(self, stem):
- name, ext = os.path.splitext(stem)
+ @staticmethod
+ def _name_from_stem(stem):
+ """
+ >>> PathDistribution._name_from_stem('foo-3.0.egg-info')
+ 'foo'
+ >>> PathDistribution._name_from_stem('CherryPy-3.0.dist-info')
+ 'CherryPy'
+ >>> PathDistribution._name_from_stem('face.egg-info')
+ 'face'
+ """
+ filename, ext = os.path.splitext(stem)
if ext not in ('.dist-info', '.egg-info'):
return
- name, sep, rest = stem.partition('-')
+ name, sep, rest = filename.partition('-')
return name
diff --git a/Lib/importlib/metadata/_functools.py b/Lib/importlib/metadata/_functools.py
index 73f50d0..71f66bd 100644
--- a/Lib/importlib/metadata/_functools.py
+++ b/Lib/importlib/metadata/_functools.py
@@ -83,3 +83,22 @@ def method_cache(method, cache_wrapper=None):
wrapper.cache_clear = lambda: None
return wrapper
+
+
+# From jaraco.functools 3.3
+def pass_none(func):
+ """
+ Wrap func so it's not called if its first param is None
+
+ >>> print_text = pass_none(print)
+ >>> print_text('text')
+ text
+ >>> print_text(None)
+ """
+
+ @functools.wraps(func)
+ def wrapper(param, *args, **kwargs):
+ if param is not None:
+ return func(param, *args, **kwargs)
+
+ return wrapper