Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite93968c

Browse files
committed
MNT: theory 2
1 parentb905158 commite93968c

File tree

1 file changed

+10
-15
lines changed

1 file changed

+10
-15
lines changed

‎lib/matplotlib/text.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,24 +66,19 @@ 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-
func=_get_text_metrics_function(weakref.ref(renderer))
69+
func=_get_text_metrics_function(renderer)
7070
returnfunc(text,fontprop.copy(),ismath,dpi)
7171

7272

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-
ifrendererisNone:
81-
raiseRuntimeError(
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-
returnrenderer.get_text_width_height_descent(text,fontprop,ismath)
73+
# use mutable default to carry hidden global state
74+
def_get_text_metrics_function(renderer,_cache=weakref.WeakKeyDictionary()):
75+
if (_text_metrics:=_cache.get(renderer,None))isNone:
76+
renderer_ref=weakref.ref(renderer)
77+
@functools.lru_cache(4096)
78+
def_text_metrics(text,fontprop,ismath,dpi):
79+
# dpi is unused, but participates in cache invalidation (via the renderer).
80+
returnrenderer_ref().get_text_width_height_descent(text,fontprop,ismath)
81+
_cache[renderer]=_text_metrics
8782

8883
return_text_metrics
8984

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp