From 90a753bf8e89d4b8c8db53c6cfec5c91691fbb19 Mon Sep 17 00:00:00 2001 From: Matthias Bussonnier Date: Wed, 25 Jan 2023 08:40:09 +0100 Subject: [PATCH 1/2] MAINT: don't format logs in log call. In particular f-string: There are many reason not to use f-string in logs, - F-strings are eager, so might be slow. So this may affect performance (we can filter before formatting). - prevent structured logging or handler to highlight. - Security (untrusted input can lead to DOS on formatting, https://discuss.python.org/t/safer-logging-methods-for-f-strings-and-new-style-formatting/13802) But also % formatting in a couple of places. --- doc/sphinxext/redirect_from.py | 8 ++++---- lib/matplotlib/_afm.py | 2 +- lib/matplotlib/_mathtext.py | 8 ++++---- lib/matplotlib/backends/backend_webagg_core.py | 2 +- lib/matplotlib/font_manager.py | 2 +- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/doc/sphinxext/redirect_from.py b/doc/sphinxext/redirect_from.py index f8aee0a3e52a..7a7e6aa9cdc8 100644 --- a/doc/sphinxext/redirect_from.py +++ b/doc/sphinxext/redirect_from.py @@ -112,10 +112,10 @@ def _generate_redirects(app, exception): html = HTML_TEMPLATE.format(v=builder.get_relative_uri(k, v)) if p.is_file(): if p.read_text() != html: - logger.warning(f'A redirect-from directive is trying to ' - f'create {p}, but that file already exists ' - f'(perhaps you need to run "make clean")') + logger.warning('A redirect-from directive is trying to ' + 'create %s, but that file already exists ' + '(perhaps you need to run "make clean")', p) else: - logger.info(f'making refresh html file: {k} redirect to {v}') + logger.info('making refresh html file: %s redirect to %s', k, v) p.parent.mkdir(parents=True, exist_ok=True) p.write_text(html, encoding='utf-8') diff --git a/lib/matplotlib/_afm.py b/lib/matplotlib/_afm.py index 3d02d7f9c1d6..558efe16392f 100644 --- a/lib/matplotlib/_afm.py +++ b/lib/matplotlib/_afm.py @@ -154,7 +154,7 @@ def _parse_header(fh): try: converter = header_converters[key] except KeyError: - _log.error('Found an unknown keyword in AFM header (was %r)' % key) + _log.error("Found an unknown keyword in AFM header (was %r)", key) continue try: d[key] = converter(val) diff --git a/lib/matplotlib/_mathtext.py b/lib/matplotlib/_mathtext.py index 2b597f5aee95..542f60b5f362 100644 --- a/lib/matplotlib/_mathtext.py +++ b/lib/matplotlib/_mathtext.py @@ -530,7 +530,7 @@ def _get_glyph(self, fontname, font_class, sym): except ValueError: uniindex = ord('?') found_symbol = False - _log.warning(f"No TeX to Unicode mapping for {sym!a}.") + _log.warning("No TeX to Unicode mapping for %a.", sym) fontname, uniindex = self._map_virtual_font( fontname, font_class, uniindex) @@ -576,9 +576,9 @@ def _get_glyph(self, fontname, font_class, sym): if (fontname in ('it', 'regular') and isinstance(self, StixFonts)): return self._get_glyph('rm', font_class, sym) - _log.warning(f"Font {new_fontname!r} does not have a glyph " - f"for {sym!a} [U+{uniindex:x}], substituting " - "with a dummy symbol.") + _log.warning("Font %r does not have a glyph " + "for %a [U+%x], substituting " + "with a dummy symbol.", new_fontname, sym, uniindex) font = self._get_font('rm') uniindex = 0xA4 # currency char, for lack of anything better slanted = False diff --git a/lib/matplotlib/backends/backend_webagg_core.py b/lib/matplotlib/backends/backend_webagg_core.py index caa39e89789d..93876c999d3c 100644 --- a/lib/matplotlib/backends/backend_webagg_core.py +++ b/lib/matplotlib/backends/backend_webagg_core.py @@ -266,7 +266,7 @@ def handle_event(self, event): return handler(event) def handle_unknown_event(self, event): - _log.warning(f'Unhandled message type {event["type"]}. {event}') + _log.warning('Unhandled message type %s. %s', event["type"], event) def handle_ack(self, event): # Network latency tends to decrease if traffic is flowing diff --git a/lib/matplotlib/font_manager.py b/lib/matplotlib/font_manager.py index f5b094e54de0..b540feb84063 100644 --- a/lib/matplotlib/font_manager.py +++ b/lib/matplotlib/font_manager.py @@ -958,7 +958,7 @@ def json_dump(data, filename): try: json.dump(data, fh, cls=_JSONEncoder, indent=2) except OSError as e: - _log.warning(f'Could not save font_manager cache {e}') + _log.warning('Could not save font_manager cache %s', e) def json_load(filename): From 9fceadf47252465dd52d8e3fbce3e497c8b0c37e Mon Sep 17 00:00:00 2001 From: Matthias Bussonnier Date: Wed, 25 Jan 2023 04:59:04 -0800 Subject: [PATCH 2/2] Update lib/matplotlib/_mathtext.py Co-authored-by: Elliott Sales de Andrade --- lib/matplotlib/_mathtext.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/_mathtext.py b/lib/matplotlib/_mathtext.py index 542f60b5f362..46340843f691 100644 --- a/lib/matplotlib/_mathtext.py +++ b/lib/matplotlib/_mathtext.py @@ -576,9 +576,9 @@ def _get_glyph(self, fontname, font_class, sym): if (fontname in ('it', 'regular') and isinstance(self, StixFonts)): return self._get_glyph('rm', font_class, sym) - _log.warning("Font %r does not have a glyph " - "for %a [U+%x], substituting " - "with a dummy symbol.", new_fontname, sym, uniindex) + _log.warning("Font %r does not have a glyph for %a [U+%x], " + "substituting with a dummy symbol.", + new_fontname, sym, uniindex) font = self._get_font('rm') uniindex = 0xA4 # currency char, for lack of anything better slanted = False