Skip to content

Commit 36725ed

Browse files
committed
FIX: do not raise in lru_cached function
If the cached function raises it will not be cached and we will continuously pay for cache misses.
1 parent d51abff commit 36725ed

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

lib/matplotlib/font_manager.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,9 +1345,12 @@ def findfont(self, prop, fontext='ttf', directory=None,
13451345
rc_params = tuple(tuple(mpl.rcParams[key]) for key in [
13461346
"font.serif", "font.sans-serif", "font.cursive", "font.fantasy",
13471347
"font.monospace"])
1348-
return self._findfont_cached(
1348+
ret = self._findfont_cached(
13491349
prop, fontext, directory, fallback_to_default, rebuild_if_missing,
13501350
rc_params)
1351+
if isinstance(ret, Exception):
1352+
raise ret
1353+
return ret
13511354

13521355
def get_font_names(self):
13531356
"""Return the list of available fonts."""
@@ -1443,7 +1446,6 @@ def _find_fonts_by_props(self, prop, fontext='ttf', directory=None,
14431446
except ValueError:
14441447
_log.warning("findfont: Font family %r not found.", family)
14451448

1446-
14471449
# only add default family if no other font was found and
14481450
# fallback_to_default is enabled
14491451
if not fpaths:
@@ -1517,8 +1519,9 @@ def _findfont_cached(self, prop, fontext, directory, fallback_to_default,
15171519
return self.findfont(default_prop, fontext, directory,
15181520
fallback_to_default=False)
15191521
else:
1520-
raise ValueError(f"Failed to find font {prop}, and fallback "
1521-
f"to the default font was disabled")
1522+
# yes return, exceptions are not cached
1523+
return ValueError(f"Failed to find font {prop}, and fallback "
1524+
f"to the default font was disabled")
15221525
else:
15231526
_log.debug('findfont: Matching %s to %s (%r) with score of %f.',
15241527
prop, best_font.name, best_font.fname, best_score)
@@ -1537,7 +1540,8 @@ def _findfont_cached(self, prop, fontext, directory, fallback_to_default,
15371540
return self.findfont(
15381541
prop, fontext, directory, rebuild_if_missing=False)
15391542
else:
1540-
raise ValueError("No valid font could be found")
1543+
# yes return, exceptions are not cached
1544+
return ValueError("No valid font could be found")
15411545

15421546
return _cached_realpath(result)
15431547

0 commit comments

Comments
 (0)