Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit bd3a6f1

Browse filesBrowse files
committed
Version gate instead of ignoring, fix PDF to always properly close empty files
1 parent 1f53454 commit bd3a6f1
Copy full SHA for bd3a6f1

File tree

Expand file treeCollapse file tree

4 files changed

+49
-50
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+49
-50
lines changed

‎lib/matplotlib/backends/backend_pdf.py

Copy file name to clipboardExpand all lines: lib/matplotlib/backends/backend_pdf.py
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2708,6 +2708,7 @@ def __enter__(self):
27082708
return self
27092709

27102710
def __exit__(self, exc_type, exc_val, exc_tb):
2711+
print(f"Closing file {self._filename}")
27112712
self.close()
27122713

27132714
def _ensure_file(self):
@@ -2728,7 +2729,7 @@ def close(self):
27282729
_api.warn_deprecated("3.8", message=(
27292730
"Keeping empty pdf files is deprecated since %(since)s and support "
27302731
"will be removed %(removal)s."))
2731-
PdfFile(self._filename, metadata=self._metadata) # touch the file.
2732+
PdfFile(self._filename, metadata=self._metadata).close() # touch the file.
27322733

27332734
def infodict(self):
27342735
"""

‎lib/matplotlib/tests/test_backend_pdf.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_backend_pdf.py
+34-39Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import io
44
import os
55
from pathlib import Path
6-
import warnings
76

87
import numpy as np
98
import pytest
@@ -85,44 +84,40 @@ def test_multipage_keep_empty(tmp_path):
8584
os.chdir(tmp_path)
8685

8786
# test empty pdf files
88-
# Due to order of `with` block execution, a warning for unclosed file is raised
89-
# but the pytest.warns must happen before the PdfPages is created
90-
with warnings.catch_warnings():
91-
warnings.filterwarnings("ignore", category=ResourceWarning)
92-
93-
# an empty pdf is left behind with keep_empty unset
94-
with pytest.warns(mpl.MatplotlibDeprecationWarning), PdfPages("a.pdf") as pdf:
95-
pass
96-
assert os.path.exists("a.pdf")
97-
98-
# an empty pdf is left behind with keep_empty=True
99-
with pytest.warns(mpl.MatplotlibDeprecationWarning), \
100-
PdfPages("b.pdf", keep_empty=True) as pdf:
101-
pass
102-
assert os.path.exists("b.pdf")
103-
104-
# an empty pdf deletes itself afterwards with keep_empty=False
105-
with PdfPages("c.pdf", keep_empty=False) as pdf:
106-
pass
107-
assert not os.path.exists("c.pdf")
108-
109-
# test pdf files with content, they should never be deleted
110-
111-
# a non-empty pdf is left behind with keep_empty unset
112-
with PdfPages("d.pdf") as pdf:
113-
pdf.savefig(plt.figure())
114-
assert os.path.exists("d.pdf")
115-
116-
# a non-empty pdf is left behind with keep_empty=True
117-
with pytest.warns(mpl.MatplotlibDeprecationWarning), \
118-
PdfPages("e.pdf", keep_empty=True) as pdf:
119-
pdf.savefig(plt.figure())
120-
assert os.path.exists("e.pdf")
121-
122-
# a non-empty pdf is left behind with keep_empty=False
123-
with PdfPages("f.pdf", keep_empty=False) as pdf:
124-
pdf.savefig(plt.figure())
125-
assert os.path.exists("f.pdf")
87+
88+
# an empty pdf is left behind with keep_empty unset
89+
with pytest.warns(mpl.MatplotlibDeprecationWarning), PdfPages("a.pdf") as pdf:
90+
pass
91+
assert os.path.exists("a.pdf")
92+
93+
# an empty pdf is left behind with keep_empty=True
94+
with pytest.warns(mpl.MatplotlibDeprecationWarning), \
95+
PdfPages("b.pdf", keep_empty=True) as pdf:
96+
pass
97+
assert os.path.exists("b.pdf")
98+
99+
# an empty pdf deletes itself afterwards with keep_empty=False
100+
with PdfPages("c.pdf", keep_empty=False) as pdf:
101+
pass
102+
assert not os.path.exists("c.pdf")
103+
104+
# test pdf files with content, they should never be deleted
105+
106+
# a non-empty pdf is left behind with keep_empty unset
107+
with PdfPages("d.pdf") as pdf:
108+
pdf.savefig(plt.figure())
109+
assert os.path.exists("d.pdf")
110+
111+
# a non-empty pdf is left behind with keep_empty=True
112+
with pytest.warns(mpl.MatplotlibDeprecationWarning), \
113+
PdfPages("e.pdf", keep_empty=True) as pdf:
114+
pdf.savefig(plt.figure())
115+
assert os.path.exists("e.pdf")
116+
117+
# a non-empty pdf is left behind with keep_empty=False
118+
with PdfPages("f.pdf", keep_empty=False) as pdf:
119+
pdf.savefig(plt.figure())
120+
assert os.path.exists("f.pdf")
126121

127122

128123
def test_composite_image():

‎lib/matplotlib/tests/test_colors.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_colors.py
+5-3Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import copy
22
import itertools
33
import unittest.mock
4-
import warnings
4+
from packaging.version import parse as parse_version
55

66
from io import BytesIO
77
import numpy as np
@@ -149,10 +149,12 @@ def test_double_register_builtin_cmap():
149149
with pytest.warns(mpl.MatplotlibDeprecationWarning):
150150
cm.register_cmap(name, mpl.colormaps[name])
151151

152-
with warnings.catch_warnings():
153-
warnings.filterwarnings("ignore", category=mpl.MatplotlibDeprecationWarning)
152+
if parse_version(pytest.__version__).major < 8:
154153
with pytest.warns(UserWarning):
155154
cm.register_cmap(name, mpl.colormaps[name], override_builtin=True)
155+
else:
156+
with pytest.warns(UserWarning), pytest.warns(mpl.MatplotlibDeprecationWarning):
157+
cm.register_cmap(name, mpl.colormaps[name], override_builtin=True)
156158

157159

158160
def test_unregister_builtin_cmap():

‎lib/matplotlib/tests/test_ticker.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_ticker.py
+8-7Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import locale
44
import logging
55
import re
6-
import warnings
6+
from packaging.version import parse as parse_version
77

88
import numpy as np
99
from numpy.testing import assert_almost_equal, assert_array_equal
@@ -915,16 +915,17 @@ def test_mathtext_ticks(self):
915915
'axes.formatter.use_mathtext': False
916916
})
917917

918-
# Glyph warning unrelated
919-
with warnings.catch_warnings():
920-
warnings.filterwarnings(
921-
"ignore", category=UserWarning, message="Glyph 8722"
922-
)
923-
918+
if parse_version(pytest.__version__).major < 8:
924919
with pytest.warns(UserWarning, match='cmr10 font should ideally'):
925920
fig, ax = plt.subplots()
926921
ax.set_xticks([-1, 0, 1])
927922
fig.canvas.draw()
923+
else:
924+
with (pytest.warns(UserWarning, match="Glyph 8722"),
925+
pytest.warns(UserWarning, match='cmr10 font should ideally')):
926+
fig, ax = plt.subplots()
927+
ax.set_xticks([-1, 0, 1])
928+
fig.canvas.draw()
928929

929930
def test_cmr10_substitutions(self, caplog):
930931
mpl.rcParams.update({

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.