Skip to content

Commit 740f283

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 6b58ae3 commit 740f283

File tree

2 files changed

+24
-23
lines changed

2 files changed

+24
-23
lines changed

lib/matplotlib/backends/backend_ps.py

+20-20
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,21 @@ def __init__(self, width, height, pswriter, imagedpi=72):
265265
self._path_collection_id = 0
266266

267267
self._character_tracker = _backend_pdf_ps.CharacterTracker()
268+
self._logwarn_once = functools.lru_cache(None)(_log.warning)
269+
270+
def _is_transparent(self, rgb_or_rgba):
271+
if rgb_or_rgba is None:
272+
return True # Consistent with rgbFace semantics.
273+
elif len(rgb_or_rgba) == 4:
274+
if rgb_or_rgba[3] == 0:
275+
return True
276+
if rgb_or_rgba[3] != 1:
277+
self._logwarn_once(
278+
"The PostScript backend does not support transparency; "
279+
"partially transparent artists will be rendered opaque.")
280+
return False
281+
else: # len() == 3.
282+
return False
268283

269284
def set_color(self, r, g, b, store=True):
270285
if (r, g, b) != self.color:
@@ -457,7 +472,7 @@ def draw_markers(
457472

458473
ps_color = (
459474
None
460-
if _is_transparent(rgbFace)
475+
if self._is_transparent(rgbFace)
461476
else '%1.3f setgray' % rgbFace[0]
462477
if rgbFace[0] == rgbFace[1] == rgbFace[2]
463478
else '%1.3f %1.3f %1.3f setrgbcolor' % rgbFace[:3])
@@ -552,7 +567,7 @@ def draw_path_collection(self, gc, master_transform, paths, all_transforms,
552567
def draw_tex(self, gc, x, y, s, prop, angle, *, mtext=None):
553568
# docstring inherited
554569
if not hasattr(self, "psfrag"):
555-
_log.warning(
570+
self._logwarn_once(
556571
"The PS backend determines usetex status solely based on "
557572
"rcParams['text.usetex'] and does not support having "
558573
"usetex=True only for some elements; this element will thus "
@@ -589,7 +604,7 @@ def draw_tex(self, gc, x, y, s, prop, angle, *, mtext=None):
589604
def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
590605
# docstring inherited
591606

592-
if _is_transparent(gc.get_rgb()):
607+
if self._is_transparent(gc.get_rgb()):
593608
return # Special handling for fully transparent.
594609

595610
if ismath == 'TeX':
@@ -729,10 +744,10 @@ def _draw_ps(self, ps, gc, rgbFace, *, fill=True, stroke=True):
729744
"""
730745
write = self._pswriter.write
731746
mightstroke = (gc.get_linewidth() > 0
732-
and not _is_transparent(gc.get_rgb()))
747+
and not self._is_transparent(gc.get_rgb()))
733748
if not mightstroke:
734749
stroke = False
735-
if _is_transparent(rgbFace):
750+
if self._is_transparent(rgbFace):
736751
fill = False
737752
hatch = gc.get_hatch()
738753

@@ -769,21 +784,6 @@ def _draw_ps(self, ps, gc, rgbFace, *, fill=True, stroke=True):
769784
write("grestore\n")
770785

771786

772-
def _is_transparent(rgb_or_rgba):
773-
if rgb_or_rgba is None:
774-
return True # Consistent with rgbFace semantics.
775-
elif len(rgb_or_rgba) == 4:
776-
if rgb_or_rgba[3] == 0:
777-
return True
778-
if rgb_or_rgba[3] != 1:
779-
_log.warning(
780-
"The PostScript backend does not support transparency; "
781-
"partially transparent artists will be rendered opaque.")
782-
return False
783-
else: # len() == 3.
784-
return False
785-
786-
787787
@_api.deprecated("3.4", alternative="GraphicsContextBase")
788788
class GraphicsContextPS(GraphicsContextBase):
789789
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)