Skip to content

Commit d57df98

Browse files
committed
Fix cache overflow
1 parent 41bfe1e commit d57df98

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

qwt/text.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ def draw(self, painter, rect, flags, text):
234234
_FONT_TUPLE_CACHE: dict = {} # tuple_key -> tuple_key (interning, also acts
235235
# as the "have we seen this logical font" set)
236236
_FONT_KEY_CACHE_LIMIT = 1024
237+
_FM_CACHE_LIMIT = 256 # max QFontMetrics/QFontMetricsF entries per engine
237238

238239

239240
def _font_tuple_key(font):
@@ -317,13 +318,17 @@ def fontmetrics(self, font):
317318
try:
318319
return self._fm_cache[fid]
319320
except KeyError:
321+
if len(self._fm_cache) >= _FM_CACHE_LIMIT:
322+
self._fm_cache.clear()
320323
return self._fm_cache.setdefault(fid, QFontMetrics(font))
321324

322325
def fontmetrics_f(self, font):
323326
fid = font_key_cached(font)
324327
try:
325328
return self._fm_cache_f[fid]
326329
except KeyError:
330+
if len(self._fm_cache_f) >= _FM_CACHE_LIMIT:
331+
self._fm_cache_f.clear()
327332
return self._fm_cache_f.setdefault(fid, QFontMetricsF(font))
328333

329334
def heightForWidth(self, font, flags, text, width):
@@ -359,6 +364,8 @@ def effectiveAscent(self, font):
359364
ascent = ASCENTCACHE.get(fontKey)
360365
if ascent is not None:
361366
return ascent
367+
if len(ASCENTCACHE) >= _FM_CACHE_LIMIT:
368+
ASCENTCACHE.clear()
362369
return ASCENTCACHE.setdefault(fontKey, self.findAscent(font))
363370

364371
def findAscent(self, font):
@@ -411,6 +418,8 @@ def textMargins(self, font):
411418
if cached is None:
412419
fm = self.fontmetrics(font)
413420
cached = (0, 0, fm.ascent() - self.effectiveAscent(font), fm.descent())
421+
if len(self._margins_cache) >= _FM_CACHE_LIMIT:
422+
self._margins_cache.clear()
414423
self._margins_cache[fkey] = cached
415424
self._margins_last_id = font_id
416425
self._margins_last_value = cached

0 commit comments

Comments
 (0)