Skip to content

Commit 4e7a5bc

Browse files
committed
Suppress repeated logwarns in postscript output.
For a given call to savefig(), we only need to warn once about alpha or usetex support. (RendererPS is generated afresh for each savefig() call, so it's fine to do the caching on it.)
1 parent 295f9c0 commit 4e7a5bc

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

lib/matplotlib/backends/backend_ps.py

+21-20
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import codecs
66
import datetime
77
from enum import Enum
8+
import functools
89
import glob
910
from io import StringIO
1011
import logging
@@ -251,6 +252,21 @@ def __init__(self, width, height, pswriter, imagedpi=72):
251252
self._path_collection_id = 0
252253

253254
self._character_tracker = _backend_pdf_ps.CharacterTracker()
255+
self._logwarn_once = functools.lru_cache(None)(_log.warning)
256+
257+
def _is_transparent(self, rgb_or_rgba):
258+
if rgb_or_rgba is None:
259+
return True # Consistent with rgbFace semantics.
260+
elif len(rgb_or_rgba) == 4:
261+
if rgb_or_rgba[3] == 0:
262+
return True
263+
if rgb_or_rgba[3] != 1:
264+
self._logwarn_once(
265+
"The PostScript backend does not support transparency; "
266+
"partially transparent artists will be rendered opaque.")
267+
return False
268+
else: # len() == 3.
269+
return False
254270

255271
def set_color(self, r, g, b, store=True):
256272
if (r, g, b) != self.color:
@@ -447,7 +463,7 @@ def draw_markers(
447463

448464
ps_color = (
449465
None
450-
if _is_transparent(rgbFace)
466+
if self._is_transparent(rgbFace)
451467
else '%1.3f setgray' % rgbFace[0]
452468
if rgbFace[0] == rgbFace[1] == rgbFace[2]
453469
else '%1.3f %1.3f %1.3f setrgbcolor' % rgbFace[:3])
@@ -540,7 +556,7 @@ def draw_path_collection(self, gc, master_transform, paths, all_transforms,
540556
def draw_tex(self, gc, x, y, s, prop, angle, *, mtext=None):
541557
# docstring inherited
542558
if not hasattr(self, "psfrag"):
543-
_log.warning(
559+
self._logwarn_once(
544560
"The PS backend determines usetex status solely based on "
545561
"rcParams['text.usetex'] and does not support having "
546562
"usetex=True only for some elements; this element will thus "
@@ -579,7 +595,7 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
579595
if debugPS:
580596
self._pswriter.write("% text\n")
581597

582-
if _is_transparent(gc.get_rgb()):
598+
if self._is_transparent(gc.get_rgb()):
583599
return # Special handling for fully transparent.
584600

585601
if ismath == 'TeX':
@@ -723,10 +739,10 @@ def _draw_ps(self, ps, gc, rgbFace, fill=True, stroke=True, command=None):
723739
if debugPS and command:
724740
write("% "+command+"\n")
725741
mightstroke = (gc.get_linewidth() > 0
726-
and not _is_transparent(gc.get_rgb()))
742+
and not self._is_transparent(gc.get_rgb()))
727743
if not mightstroke:
728744
stroke = False
729-
if _is_transparent(rgbFace):
745+
if self._is_transparent(rgbFace):
730746
fill = False
731747
hatch = gc.get_hatch()
732748

@@ -764,21 +780,6 @@ def _draw_ps(self, ps, gc, rgbFace, fill=True, stroke=True, command=None):
764780
write("grestore\n")
765781

766782

767-
def _is_transparent(rgb_or_rgba):
768-
if rgb_or_rgba is None:
769-
return True # Consistent with rgbFace semantics.
770-
elif len(rgb_or_rgba) == 4:
771-
if rgb_or_rgba[3] == 0:
772-
return True
773-
if rgb_or_rgba[3] != 1:
774-
_log.warning(
775-
"The PostScript backend does not support transparency; "
776-
"partially transparent artists will be rendered opaque.")
777-
return False
778-
else: # len() == 3.
779-
return False
780-
781-
782783
@_api.deprecated("3.4", alternative="GraphicsContextBase")
783784
class GraphicsContextPS(GraphicsContextBase):
784785
def get_capstyle(self):

lib/matplotlib/tests/test_backend_ps.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,11 @@ def test_failing_latex():
149149
@needs_usetex
150150
def test_partial_usetex(caplog):
151151
caplog.set_level("WARNING")
152-
plt.figtext(.5, .5, "foo", usetex=True)
152+
plt.figtext(.1, .1, "foo", usetex=True)
153+
plt.figtext(.2, .2, "bar", usetex=True)
153154
plt.savefig(io.BytesIO(), format="ps")
154-
assert caplog.records and all("as if usetex=False" in record.getMessage()
155-
for record in caplog.records)
155+
record, = caplog.records # asserts there's a single record.
156+
assert "as if usetex=False" in record.getMessage()
156157

157158

158159
@image_comparison(["useafm.eps"])

0 commit comments

Comments
 (0)