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

Appearance settings

Commit db121ec

Browse filesBrowse files
committed
TST: add tests for option 2
1 parent b2f73a6 commit db121ec
Copy full SHA for db121ec

File tree

1 file changed

+37
-2
lines changed
Filter options

1 file changed

+37
-2
lines changed

‎lib/matplotlib/tests/test_text.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_text.py
+37-2Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
from datetime import datetime
2+
import gc
3+
import inspect
24
import io
35
import warnings
46

@@ -881,7 +883,12 @@ def test_pdf_chars_beyond_bmp():
881883

882884
@needs_usetex
883885
def test_metrics_cache():
884-
mpl.text._get_text_metrics_with_cache_impl.cache_clear()
886+
# dig into the signature to get the mutable default used as a cache
887+
renderer_cache = inspect.signature(
888+
mpl.text._get_text_metrics_function
889+
).parameters['_cache'].default
890+
891+
renderer_cache.clear()
885892

886893
fig = plt.figure()
887894
fig.text(.3, .5, "foo\nbar")
@@ -890,6 +897,7 @@ def test_metrics_cache():
890897
fig.canvas.draw()
891898
renderer = fig._get_renderer()
892899
ys = {} # mapping of strings to where they were drawn in y with draw_tex.
900+
assert renderer in renderer_cache
893901

894902
def call(*args, **kwargs):
895903
renderer, x, y, s, *_ = args
@@ -904,12 +912,39 @@ def call(*args, **kwargs):
904912
# get incorrectly reused by the first TeX string.
905913
assert len(ys["foo"]) == len(ys["bar"]) == 1
906914

907-
info = mpl.text._get_text_metrics_with_cache_impl.cache_info()
915+
info = renderer_cache[renderer].cache_info()
908916
# Every string gets a miss for the first layouting (extents), then a hit
909917
# when drawing, but "foo\nbar" gets two hits as it's drawn twice.
910918
assert info.hits > info.misses
911919

912920

921+
def test_metrics_cache2():
922+
# dig into the signature to get the mutable default used as a cache
923+
renderer_cache = inspect.signature(
924+
mpl.text._get_text_metrics_function
925+
).parameters['_cache'].default
926+
gc.collect()
927+
assert len(renderer_cache) == 0
928+
929+
def helper():
930+
fig, ax = plt.subplots()
931+
fig.draw_without_rendering()
932+
# show we hit the outer cache
933+
assert len(renderer_cache) == 1
934+
func = renderer_cache[fig.canvas.get_renderer()]
935+
cache_info = func.cache_info()
936+
# show we hit the inner cache
937+
assert cache_info.currsize > 0
938+
assert cache_info.currsize == cache_info.misses
939+
assert cache_info.hits > cache_info.misses
940+
plt.close(fig)
941+
942+
helper()
943+
gc.collect()
944+
# show the outer cache has a lifetime tied to the renderer (via the figure)
945+
assert len(renderer_cache) == 0
946+
947+
913948
def test_annotate_offset_fontsize():
914949
# Test that offset_fontsize parameter works and uses accurate values
915950
fig, ax = plt.subplots()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.