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 6b73d64
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
18 changes: 12 additions & 6 deletions sentry_sdk/integrations/modules.py
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
7 changes: 6 additions & 1 deletion tests/integrations/modules/test_modules.py
@@ -1,3 +1,4 @@
import pytest
import re
import sentry_sdk

Expand Down Expand Up @@ -55,12 +56,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 6b73d64

Please sign in to comment.