Skip to content

Commit

Permalink
Don't trigger matplotlib.__getattr__("__version__") if possible.
Browse files Browse the repository at this point in the history
  • Loading branch information
anntzer committed Sep 9, 2021
1 parent 84a017d commit 7aa2de7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
22 changes: 19 additions & 3 deletions lib/mplcairo/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import ast
import functools
import os
import sys
import warnings


try:
from ._version import version as __version__
except ImportError:
Expand Down Expand Up @@ -54,18 +56,32 @@ def _init_options():
_init_options()


@functools.lru_cache(1)
def _get_mpl_version():
# Don't trigger a git subprocess for Matplotlib's __version__ resolution if
# possible, and cache the result as early versions of importlib.metadata
# are slow. We can't cache get_versions() directly as the result depends
# on whether raqm is loaded.
try:
import importlib.metadata
return importlib.metadata.version("matplotlib")
except ImportError:
# No importlib.metadata on Py<3.8 *or* not-installed Matplotlib.
return matplotlib.__version__


def get_versions():
"""
Return a mapping indicating the versions of mplcairo and its dependencies.
This function is solely intended to help gather information for bug
reports; its output may change without notice.
This function is intended to help gather information for bug reports, and
not part of the stable API.
"""
versions = _mplcairo.get_versions()
return {
"python": sys.version,
"mplcairo": __version__,
"matplotlib": matplotlib.__version__,
"matplotlib": _get_mpl_version(),
**versions,
}

Expand Down
5 changes: 3 additions & 2 deletions lib/mplcairo/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from matplotlib.backends import backend_ps
from matplotlib.mathtext import MathTextParser

from . import _mplcairo, _util
from . import _mplcairo, _util, get_versions
from ._backports import get_glyph_name
from ._mplcairo import _StreamSurfaceType

Expand Down Expand Up @@ -404,7 +404,8 @@ def print_png(self, path_or_stream, *,
return
metadata = {
"Software":
f"matplotlib version {mpl.__version__}, https://matplotlib.org",
f"matplotlib version {get_versions()['matplotlib']}, "
f"https://matplotlib.org",
**(metadata if metadata is not None else {}),
}
if pil_kwargs is None:
Expand Down

0 comments on commit 7aa2de7

Please sign in to comment.