From 2b3e54b12538ea113058f6f12fb170e67f557501 Mon Sep 17 00:00:00 2001 From: Malthe Borch Date: Tue, 16 Jan 2024 22:24:22 +0100 Subject: [PATCH] Fix pypy compatibility (not all distributions are packages) --- src/chameleon/template.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/chameleon/template.py b/src/chameleon/template.py index 508ad7b5..2ba93a0b 100644 --- a/src/chameleon/template.py +++ b/src/chameleon/template.py @@ -44,15 +44,25 @@ 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())