Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b905158

Browse files
committed
MNT: theory 1
1 parent 64d45cb commit b905158

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

lib/matplotlib/text.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,26 @@ def _get_text_metrics_with_cache(renderer, text, fontprop, ismath, dpi):
6666
"""Call ``renderer.get_text_width_height_descent``, caching the results."""
6767
# Cached based on a copy of fontprop so that later in-place mutations of
6868
# the passed-in argument do not mess up the cache.
69-
return _get_text_metrics_with_cache_impl(
70-
weakref.ref(renderer), text, fontprop.copy(), ismath, dpi)
69+
func = _get_text_metrics_function(weakref.ref(renderer))
70+
return func(text, fontprop.copy(), ismath, dpi)
7171

7272

73-
@functools.lru_cache(4096)
74-
def _get_text_metrics_with_cache_impl(
75-
renderer_ref, text, fontprop, ismath, dpi):
76-
# dpi is unused, but participates in cache invalidation (via the renderer).
77-
return renderer_ref().get_text_width_height_descent(text, fontprop, ismath)
73+
@functools.lru_cache(25)
74+
def _get_text_metrics_function(renderer_ref):
75+
76+
@functools.lru_cache(4096)
77+
def _text_metrics(text, fontprop, ismath, dpi):
78+
# dpi is unused, but participates in cache invalidation (via the renderer).
79+
renderer = renderer_ref()
80+
if renderer is None:
81+
raise RuntimeError(
82+
"You are trying to get the font metrics for a renderer that no "
83+
"longer exists. There is likely a deeper bug someplace else "
84+
"as this should never happen."
85+
)
86+
return renderer.get_text_width_height_descent(text, fontprop, ismath)
87+
88+
return _text_metrics
7889

7990

8091
@_docstring.interpd

0 commit comments

Comments
 (0)