2525import unicodedata
2626from warnings import warn
2727
28+ try :
29+ from functools import lru_cache
30+ except ImportError : # Py2
31+ from backports .functools_lru_cache import lru_cache
32+
2833from numpy import inf , isinf
2934import numpy as np
3035
@@ -3257,8 +3262,8 @@ def __init__(self, output):
32573262 Create a MathTextParser for the given backend *output*.
32583263 """
32593264 self ._output = output .lower ()
3260- self ._cache = maxdict (50 )
32613265
3266+ @lru_cache (50 )
32623267 def parse (self , s , dpi = 72 , prop = None ):
32633268 """
32643269 Parse the given math expression *s* at the given *dpi*. If
@@ -3270,16 +3275,10 @@ def parse(self, s, dpi = 72, prop = None):
32703275 The results are cached, so multiple calls to :meth:`parse`
32713276 with the same expression should be fast.
32723277 """
3273- # There is a bug in Python 3.x where it leaks frame references,
3274- # and therefore can't handle this caching
3278+
32753279 if prop is None :
32763280 prop = FontProperties ()
32773281
3278- cacheKey = (s , dpi , hash (prop ))
3279- result = self ._cache .get (cacheKey )
3280- if result is not None :
3281- return result
3282-
32833282 if self ._output == 'ps' and rcParams ['ps.useafm' ]:
32843283 font_output = StandardPsFonts (prop )
32853284 else :
@@ -3302,9 +3301,7 @@ def parse(self, s, dpi = 72, prop = None):
33023301
33033302 box = self ._parser .parse (s , font_output , fontsize , dpi )
33043303 font_output .set_canvas_size (box .width , box .height , box .depth )
3305- result = font_output .get_results (box )
3306- self ._cache [cacheKey ] = result
3307- return result
3304+ return font_output .get_results (box )
33083305
33093306 def to_mask (self , texstr , dpi = 120 , fontsize = 14 ):
33103307 """
0 commit comments