Skip to content

Commit

Permalink
Fix pypy compatibility (not all distributions are packages)
Browse files Browse the repository at this point in the history
  • Loading branch information
malthe committed Jan 18, 2024
1 parent 8ffa8e1 commit 6813410
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/chameleon/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,24 @@
from chameleon.compiler import ExpressionEngine


def get_package_versions() -> list[tuple[str, str]]:
if sys.version_info >= (3, 10):
import importlib.metadata as importlib_metadata
else:
import importlib_metadata
if sys.version_info >= (3, 10):
import importlib.metadata as importlib_metadata
else:
import importlib_metadata


def safe_get_package_version(name: str) -> str | None:
try:
return importlib_metadata.version(name)
except importlib_metadata.PackageNotFoundError:
return None

def get_package_versions() -> list[tuple[str, str]]:
distributions = importlib_metadata.packages_distributions().values()
versions = {
x: importlib_metadata.version(x)
for x in sum(importlib_metadata.packages_distributions().values(), [])}
x: safe_get_package_version(x) or ""
for x in sum(distributions, [])
}
return sorted(versions.items())


Expand Down

0 comments on commit 6813410

Please sign in to comment.