From 82cbb29f2350c2c43db06913ef936993f826890f Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Fri, 12 Nov 2021 18:41:59 -0500 Subject: [PATCH 1/2] FIX: Make sure we do not over-write eps short cuts This tests that we have not re-defined a short-cut by checking that we do not have two `/name` lines in the output file. closes #21509 Co-authored-by: Antony Lee --- lib/matplotlib/backends/backend_ps.py | 4 ++-- lib/matplotlib/tests/test_backend_ps.py | 31 +++++++++++++++++++++---- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/lib/matplotlib/backends/backend_ps.py b/lib/matplotlib/backends/backend_ps.py index 35c61b08f26b..8c88bfa3c732 100644 --- a/lib/matplotlib/backends/backend_ps.py +++ b/lib/matplotlib/backends/backend_ps.py @@ -429,7 +429,7 @@ def _get_clip_cmd(self, gc): key = (path, id(trf)) custom_clip_cmd = self._clip_paths.get(key) if custom_clip_cmd is None: - custom_clip_cmd = "c%x" % len(self._clip_paths) + custom_clip_cmd = "c%d" % len(self._clip_paths) self._pswriter.write(f"""\ /{custom_clip_cmd} {{ {self._convert_path(path, trf, simplify=False)} @@ -570,7 +570,7 @@ def draw_path_collection(self, gc, master_transform, paths, all_transforms, path_codes = [] for i, (path, transform) in enumerate(self._iter_collection_raw_paths( master_transform, paths, all_transforms)): - name = 'p%x_%x' % (self._path_collection_id, i) + name = 'p%d_%d' % (self._path_collection_id, i) path_bytes = self._convert_path(path, transform, simplify=False) self._pswriter.write(f"""\ /{name} {{ diff --git a/lib/matplotlib/tests/test_backend_ps.py b/lib/matplotlib/tests/test_backend_ps.py index 50b1b0bc0cbe..b478c3d5e4ef 100644 --- a/lib/matplotlib/tests/test_backend_ps.py +++ b/lib/matplotlib/tests/test_backend_ps.py @@ -1,16 +1,17 @@ -import io +from collections import Counter from pathlib import Path +import io import re import tempfile import pytest -import matplotlib as mpl -import matplotlib.pyplot as plt from matplotlib import cbook, patheffects -from matplotlib.testing.decorators import check_figures_equal, image_comparison from matplotlib.cbook import MatplotlibDeprecationWarning - +from matplotlib.figure import Figure +from matplotlib.testing.decorators import check_figures_equal, image_comparison +import matplotlib as mpl +import matplotlib.pyplot as plt needs_ghostscript = pytest.mark.skipif( "eps" not in mpl.testing.compare.converter, @@ -244,3 +245,23 @@ def test_linedash(): fig.savefig(buf, format="ps") assert buf.tell() > 0 + + +def test_no_definintion(): + + fig = Figure() + axs = fig.subplots(4, 4, subplot_kw=dict(projection="polar")) + for ax in axs.flat: + ax.set(xticks=[], yticks=[]) + ax.plot([1, 2]) + fig.suptitle("hello, world") + + buf = io.StringIO() + fig.savefig(buf, format='eps') + buf.seek(0) + + wds = [ln.partition(' ')[0] for + ln in buf.readlines() + if ln.startswith('/')] + + assert max(Counter(wds).values()) == 1 From 225ff8437fd77c3253f74c9b6d74ffd250cf045e Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Fri, 12 Nov 2021 19:12:56 -0500 Subject: [PATCH 2/2] FIX: improve test name Co-authored-by: Elliott Sales de Andrade --- lib/matplotlib/tests/test_backend_ps.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_backend_ps.py b/lib/matplotlib/tests/test_backend_ps.py index b478c3d5e4ef..f0d0cbdac722 100644 --- a/lib/matplotlib/tests/test_backend_ps.py +++ b/lib/matplotlib/tests/test_backend_ps.py @@ -247,7 +247,7 @@ def test_linedash(): assert buf.tell() > 0 -def test_no_definintion(): +def test_no_duplicate_definition(): fig = Figure() axs = fig.subplots(4, 4, subplot_kw=dict(projection="polar"))