Skip to content

Skip distributions with incomplete metadata #2231

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
7 changes: 6 additions & 1 deletion tests/integrations/modules/test_modules.py
Original file line number Diff line number Diff line change
@@ -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")