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 ce226e9

Browse filesBrowse files
committed
filter for findfont log message
1 parent 389a7e8 commit ce226e9
Copy full SHA for ce226e9

File tree

2 files changed

+44
-4
lines changed
Filter options

2 files changed

+44
-4
lines changed

‎lib/matplotlib/font_manager.py

Copy file name to clipboardExpand all lines: lib/matplotlib/font_manager.py
+26-1Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,30 @@
5050

5151
_log = logging.getLogger(__name__)
5252

53+
54+
class _findfontFilter(logging.Filter):
55+
"""
56+
Suppress repeat font not found warning messages on a
57+
per font and per message type basis
58+
"""
59+
_msg_cache = set()
60+
61+
@classmethod
62+
def clear_cache(cls):
63+
_findfontFilter._msg_cache.clear()
64+
_log.debug("findfont filter cache reset")
65+
66+
def filter(self, record):
67+
if record.msg in self._msg_cache:
68+
return False
69+
if record.msg.startswith('findfont'):
70+
_findfontFilter._msg_cache.add(record.msg)
71+
return True
72+
73+
_log_findfont_filter = _findfontFilter()
74+
_log.addFilter(_log_findfont_filter)
75+
_log.debug("findfont filter added to log")
76+
5377
font_scalings = {
5478
'xx-small': 0.579,
5579
'x-small': 0.694,
@@ -1011,7 +1035,7 @@ class FontManager:
10111035
# Increment this version number whenever the font cache data
10121036
# format or behavior has changed and requires an existing font
10131037
# cache files to be rebuilt.
1014-
__version__ = 330
1038+
__version__ = 331
10151039

10161040
def __init__(self, size=None, weight='normal'):
10171041
self._version = self.__version__
@@ -1576,6 +1600,7 @@ def _load_fontmanager(*, try_read_cache=True):
15761600
fm = FontManager()
15771601
json_dump(fm, fm_path)
15781602
_log.info("generated new fontManager")
1603+
_findfontFilter._clear_cache()
15791604
return fm
15801605

15811606

‎lib/matplotlib/tests/test_font_manager.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_font_manager.py
+18-3Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
from matplotlib.font_manager import (
1616
findfont, findSystemFonts, FontEntry, FontProperties, fontManager,
17-
json_dump, json_load, get_font, is_opentype_cff_font,
17+
_findfontFilter, json_dump, json_load, get_font, is_opentype_cff_font,
1818
MSUserFontDirectories, _get_fontconfig_fonts, ttfFontProperty)
1919
from matplotlib import cbook, ft2font, pyplot as plt, rc_context, figure as mfigure
2020

@@ -241,12 +241,27 @@ def test_missing_family(caplog):
241241
plt.rcParams["font.sans-serif"] = ["this-font-does-not-exist"]
242242
with caplog.at_level("WARNING"):
243243
findfont("sans")
244-
assert [rec.getMessage() for rec in caplog.records] == [
244+
assert [rec.getMessage() in [
245245
"findfont: Font family ['sans'] not found. "
246246
"Falling back to DejaVu Sans.",
247247
"findfont: Generic family 'sans' not found because none of the "
248248
"following families were found: this-font-does-not-exist",
249-
]
249+
] for rec in caplog.records]
250+
_findfontFilter.clear_cache()
251+
252+
253+
def test_not_found_filter(caplog):
254+
plt.rcParams["font.sans-serif"] = ["this-font-does-not-exist"]
255+
256+
_findfontFilter.clear_cache()
257+
_findfontFilter._msg_cache.add("findfont: Font family ['sans'] not found. "
258+
"Falling back to DejaVu Sans.")
259+
_findfontFilter._msg_cache.add("findfont: Generic family 'sans' not found "
260+
"because none of the following families "
261+
"were found: this-font-does-not-exist")
262+
with caplog.at_level("WARNING"):
263+
findfont("sans")
264+
assert len(caplog.records) == 0
250265

251266

252267
def _test_threading():

0 commit comments

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