From 1c2e8f36028ecba598b70efb7cadb0a0cbffe833 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Wed, 5 Feb 2025 11:53:39 +0100 Subject: [PATCH] Use functools.cache instead of lru_cache to establish singletons. The intent is clearer (and cache is slightly more optimized). --- lib/matplotlib/_fontconfig_pattern.py | 4 ++-- lib/matplotlib/dviread.py | 4 ++-- lib/matplotlib/font_manager.py | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/matplotlib/_fontconfig_pattern.py b/lib/matplotlib/_fontconfig_pattern.py index a1341c633243..bc7a6c8cd84a 100644 --- a/lib/matplotlib/_fontconfig_pattern.py +++ b/lib/matplotlib/_fontconfig_pattern.py @@ -9,7 +9,7 @@ # there would have created cyclical dependency problems, because it also needs # to be available from `matplotlib.rcsetup` (for parsing matplotlibrc files). -from functools import lru_cache, partial +from functools import cache, lru_cache, partial import re from pyparsing import ( @@ -52,7 +52,7 @@ } -@lru_cache # The parser instance is a singleton. +@cache # The parser instance is a singleton. def _make_fontconfig_parser(): def comma_separated(elem): return elem + ZeroOrMore(Suppress(",") + elem) diff --git a/lib/matplotlib/dviread.py b/lib/matplotlib/dviread.py index 9850b73ea410..a8371ff4de41 100644 --- a/lib/matplotlib/dviread.py +++ b/lib/matplotlib/dviread.py @@ -19,7 +19,7 @@ from collections import namedtuple import enum -from functools import lru_cache, partial, wraps +from functools import cache, lru_cache, partial, wraps import logging import os from pathlib import Path @@ -1020,7 +1020,7 @@ def _parse_enc(path): class _LuatexKpsewhich: - @lru_cache # A singleton. + @cache # A singleton. def __new__(cls): self = object.__new__(cls) self._proc = self._new_proc() diff --git a/lib/matplotlib/font_manager.py b/lib/matplotlib/font_manager.py index cbcd72ea3cec..583473464036 100644 --- a/lib/matplotlib/font_manager.py +++ b/lib/matplotlib/font_manager.py @@ -30,7 +30,7 @@ from base64 import b64encode import copy import dataclasses -from functools import lru_cache +from functools import cache, lru_cache import functools from io import BytesIO import json @@ -247,7 +247,7 @@ def _get_win32_installed_fonts(): return items -@lru_cache +@cache def _get_fontconfig_fonts(): """Cache and list the font paths known to ``fc-list``.""" try: @@ -261,7 +261,7 @@ def _get_fontconfig_fonts(): return [Path(os.fsdecode(fname)) for fname in out.split(b'\n')] -@lru_cache +@cache def _get_macos_fonts(): """Cache and list the font paths known to ``system_profiler SPFontsDataType``.""" try: