Skip to content

Commit

Permalink
Merge pull request #27986 from meeseeksmachine/auto-backport-of-pr-27…
Browse files Browse the repository at this point in the history
…985-on-v3.8.x
  • Loading branch information
ksunden committed Mar 28, 2024
2 parents fbe1942 + b22bb88 commit 473b407
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 28 deletions.
32 changes: 18 additions & 14 deletions lib/matplotlib/tests/test_backend_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,43 +81,47 @@ def test_multipage_properfinalize():


def test_multipage_keep_empty(tmp_path):
os.chdir(tmp_path)

# test empty pdf files

# an empty pdf is left behind with keep_empty unset
with pytest.warns(mpl.MatplotlibDeprecationWarning), PdfPages("a.pdf") as pdf:
fn = tmp_path / "a.pdf"
with pytest.warns(mpl.MatplotlibDeprecationWarning), PdfPages(fn) as pdf:
pass
assert os.path.exists("a.pdf")
assert fn.exists()

# an empty pdf is left behind with keep_empty=True
fn = tmp_path / "b.pdf"
with pytest.warns(mpl.MatplotlibDeprecationWarning), \
PdfPages("b.pdf", keep_empty=True) as pdf:
PdfPages(fn, keep_empty=True) as pdf:
pass
assert os.path.exists("b.pdf")
assert fn.exists()

# an empty pdf deletes itself afterwards with keep_empty=False
with PdfPages("c.pdf", keep_empty=False) as pdf:
fn = tmp_path / "c.pdf"
with PdfPages(fn, keep_empty=False) as pdf:
pass
assert not os.path.exists("c.pdf")
assert not fn.exists()

# test pdf files with content, they should never be deleted

# a non-empty pdf is left behind with keep_empty unset
with PdfPages("d.pdf") as pdf:
fn = tmp_path / "d.pdf"
with PdfPages(fn) as pdf:
pdf.savefig(plt.figure())
assert os.path.exists("d.pdf")
assert fn.exists()

# a non-empty pdf is left behind with keep_empty=True
fn = tmp_path / "e.pdf"
with pytest.warns(mpl.MatplotlibDeprecationWarning), \
PdfPages("e.pdf", keep_empty=True) as pdf:
PdfPages(fn, keep_empty=True) as pdf:
pdf.savefig(plt.figure())
assert os.path.exists("e.pdf")
assert fn.exists()

# a non-empty pdf is left behind with keep_empty=False
with PdfPages("f.pdf", keep_empty=False) as pdf:
fn = tmp_path / "f.pdf"
with PdfPages(fn, keep_empty=False) as pdf:
pdf.savefig(plt.figure())
assert os.path.exists("f.pdf")
assert fn.exists()


def test_composite_image():
Expand Down
32 changes: 18 additions & 14 deletions lib/matplotlib/tests/test_backend_pgf.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,43 +288,47 @@ def test_pdf_pages_metadata_check(monkeypatch, system):

@needs_pgf_xelatex
def test_multipage_keep_empty(tmp_path):
os.chdir(tmp_path)

# test empty pdf files

# an empty pdf is left behind with keep_empty unset
with pytest.warns(mpl.MatplotlibDeprecationWarning), PdfPages("a.pdf") as pdf:
fn = tmp_path / "a.pdf"
with pytest.warns(mpl.MatplotlibDeprecationWarning), PdfPages(fn) as pdf:
pass
assert os.path.exists("a.pdf")
assert fn.exists()

# an empty pdf is left behind with keep_empty=True
fn = tmp_path / "b.pdf"
with pytest.warns(mpl.MatplotlibDeprecationWarning), \
PdfPages("b.pdf", keep_empty=True) as pdf:
PdfPages(fn, keep_empty=True) as pdf:
pass
assert os.path.exists("b.pdf")
assert fn.exists()

# an empty pdf deletes itself afterwards with keep_empty=False
with PdfPages("c.pdf", keep_empty=False) as pdf:
fn = tmp_path / "c.pdf"
with PdfPages(fn, keep_empty=False) as pdf:
pass
assert not os.path.exists("c.pdf")
assert not fn.exists()

# test pdf files with content, they should never be deleted

# a non-empty pdf is left behind with keep_empty unset
with PdfPages("d.pdf") as pdf:
fn = tmp_path / "d.pdf"
with PdfPages(fn) as pdf:
pdf.savefig(plt.figure())
assert os.path.exists("d.pdf")
assert fn.exists()

# a non-empty pdf is left behind with keep_empty=True
fn = tmp_path / "e.pdf"
with pytest.warns(mpl.MatplotlibDeprecationWarning), \
PdfPages("e.pdf", keep_empty=True) as pdf:
PdfPages(fn, keep_empty=True) as pdf:
pdf.savefig(plt.figure())
assert os.path.exists("e.pdf")
assert fn.exists()

# a non-empty pdf is left behind with keep_empty=False
with PdfPages("f.pdf", keep_empty=False) as pdf:
fn = tmp_path / "f.pdf"
with PdfPages(fn, keep_empty=False) as pdf:
pdf.savefig(plt.figure())
assert os.path.exists("f.pdf")
assert fn.exists()


@needs_pgf_xelatex
Expand Down

0 comments on commit 473b407

Please sign in to comment.