Skip to content

Commit

Permalink
Skip distributions with incomplete metadata
Browse files Browse the repository at this point in the history
In rare cases, `importlib.metadata` values may contain `None`, see
python/cpython#91216
and
python/importlib_metadata#371

The fix skips all distributions with incomplete metadata.
  • Loading branch information
Roman Inflianskas committed Jul 5, 2023
1 parent a7b3136 commit 06f6d87
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
18 changes: 12 additions & 6 deletions sentry_sdk/integrations/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,18 @@ def _normalize_module_name(name):
def _generate_installed_modules():
# type: () -> Iterator[Tuple[str, str]]
try:
from importlib.metadata import distributions, version

for dist in distributions():
yield _normalize_module_name(dist.metadata["Name"]), version(
dist.metadata["Name"]
)
from importlib import metadata

for dist in metadata.distributions():
name = dist.metadata["Name"]
# `metadata` values may be `None`, see:
# https://github.com/python/cpython/issues/91216
# and
# https://github.com/python/importlib_metadata/issues/371
if name is not None:
version = metadata.version(name)
if version is not None:
yield _normalize_module_name(name), version

except ImportError:
# < py3.8
Expand Down
6 changes: 5 additions & 1 deletion tests/integrations/modules/test_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,16 @@ def test_installed_modules():
dist.metadata["Name"]
)
for dist in distributions()
if dist.metadata["Name"] is not None and
version(dist.metadata["Name"]) is not None
}
assert installed_distributions == importlib_distributions

if pkg_resources_available:
elif pkg_resources_available:
pkg_resources_distributions = {
_normalize_distribution_name(dist.key): dist.version
for dist in pkg_resources.working_set
}
assert installed_distributions == pkg_resources_distributions
else:
pytest.fail("Neither importlib nor pkg_resources is available")

0 comments on commit 06f6d87

Please sign in to comment.