From: whitequark Date: Wed, 1 Jul 2020 07:00:02 +0000 (+0000) Subject: Gracefully handle missing dependencies. X-Git-Tag: 24jan2021_ls180~58 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=126f0be731ab008657ca1c4fc05ce7cc6e355169;p=nmigen.git Gracefully handle missing dependencies. Some people's workflows involve not using `pip`. This is not a recommended way to use nMigen, but is prevalent enough for good enough reason that we try to keep them working anyway. --- diff --git a/nmigen/__init__.py b/nmigen/__init__.py index 78a8dec..93a3a9a 100644 --- a/nmigen/__init__.py +++ b/nmigen/__init__.py @@ -1,8 +1,14 @@ try: - from importlib import metadata as importlib_metadata # py3.8+ stdlib + try: + from importlib import metadata as importlib_metadata # py3.8+ stdlib + except ImportError: + import importlib_metadata # py3.7- shim + __version__ = importlib_metadata.version(__package__) except ImportError: - import importlib_metadata # py3.7- shim -__version__ = importlib_metadata.version(__package__) + # No importlib_metadata. This shouldn't normally happen, but some people prefer not installing + # packages via pip at all, instead using PYTHONPATH directly or copying the package files into + # `lib/pythonX.Y/site-packages`. Although not a recommended way, we still try to support it. + __version__ = "unknown" from .hdl import * diff --git a/nmigen/_yosys.py b/nmigen/_yosys.py index c02c7c4..e84da53 100644 --- a/nmigen/_yosys.py +++ b/nmigen/_yosys.py @@ -12,13 +12,16 @@ except ImportError: except ImportError: importlib_metadata = None # not installed try: - from importlib import resources as importlib_resources try: - importlib_resources.files # py3.9+ stdlib - except AttributeError: - import importlib_resources # py3.8- shim + from importlib import resources as importlib_resources + try: + importlib_resources.files # py3.9+ stdlib + except AttributeError: + import importlib_resources # py3.8- shim + except ImportError: + import importlib_resources # py3.6- shim except ImportError: - import importlib_resources # py3.6- shim + importlib_resources = None from ._toolchain import has_tool, require_tool @@ -115,7 +118,7 @@ class _BuiltinYosys(YosysBinary): @classmethod def available(cls): - if importlib_metadata is None: + if importlib_metadata is None or importlib_resources is None: return False try: importlib_metadata.version(cls.YOSYS_PACKAGE)