8000 Add test case for loading fugire from a subprocess, and refactor the … · matplotlib/matplotlib@7d5f53a · GitHub
[go: up one dir, main page]

Skip to content

Commit 7d5f53a

Browse files
committed
Add test case for loading fugire from a subprocess, and refactor the code.
1 parent 30da817 commit 7d5f53a

File tree

1 file changed

+50
-4
lines changed

1 file changed

+50
-4
lines changed

lib/matplotlib/tests/test_pickle.py

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
from io import BytesIO
2+
import ast
23
import pickle
34

45
import numpy as np
56
import pytest
67

78
import matplotlib as mpl
89
from matplotlib import cm
10+
from matplotlib.testing import subprocess_run_helper
911
from matplotlib.testing.decorators import check_figures_equal
1012
from matplotlib.dates import rrulewrapper
1113
from matplotlib.lines import VertexSelector
@@ -42,9 +44,7 @@ def test_simple():
4244
pickle.dump(fig, BytesIO(), pickle.HIGHEST_PROTOCOL)
4345

4446

45-
@mpl.style.context("default")
46-
@check_figures_equal(extensions=["png"])
47-
def test_complete(fig_test, fig_ref):
47+
def _generate_complete_test_figure(fig_ref):
4848
fig_ref.set_size_inches((10, 6))
4949
plt.figure(fig_ref)
5050

@@ -83,12 +83,18 @@ def test_complete(fig_test, fig_ref):
8383
plt.quiver(x, y, u, v)
8484

8585
plt.subplot(3, 3, 8)
86-
plt.scatter(x, x**2, label='$x^2$')
86+
plt.scatter(x, x ** 2, label='$x^2$')
8787
plt.legend(loc='upper left')
8888

8989
plt.subplot(3, 3, 9)
9090
plt.errorbar(x, x * -0.5, xerr=0.2, yerr=0.4)
9191

92+
93+
@mpl.style.context("default")
94+
@check_figures_equal(extensions=["png"])
95+
def test_complete(fig_test, fig_ref):
96+
_generate_complete_test_figure(fig_ref)
97+
9298
# plotting is done, now test its pickle-ability
9399
pkl = BytesIO()
94100
pickle.dump(fig_ref, pkl, pickle.HIGHEST_PROTOCOL)
@@ -101,6 +107,46 @@ def test_complete(fig_test, fig_ref):
101107
plt.close(loaded)
102108

103109

110+
def _pickle_load_subprocess():
111+
import os
112+
import pickle
113+
114+
path = os.environ['PICKLE_FILE_PATH']
115+
116+
with open(path, 'rb') as blob:
117+
fig = pickle.load(blob)
118+
119+
print(str(pickle.dumps(fig)))
120+
121+
122+
@mpl.style.context("default")
123+
@check_figures_equal(extensions=['png'])
124+
def test_pickle_load_from_subprocess(fig_test, fig_ref, tmp_path):
125+
_generate_complete_test_figure(fig_ref)
126+
127+
fp = tmp_path / 'sinus.pickle'
128+
assert not fp.exists()
129+
130+
with fp.open('wb') as file:
131+
pickle.dump(fig_ref, file, pickle.HIGHEST_PROTOCOL)
132+
assert fp.exists()
133+
134+
proc = subprocess_run_helper(
135+
_pickle_load_subprocess,
136+
timeout=60,
137+
extra_env={'PICKLE_FILE_PATH': str(fp)}
138+
)
139+
140+
loaded_fig = pickle.loads(ast.literal_eval(proc.stdout))
141+
142+
loaded_fig.canvas.draw()
143+
144+
fig_test.set_size_inches(loaded_fig.get_size_inches())
145+
fig_test.figimage(loaded_fig.canvas.renderer.buffer_rgba())
146+
147+
plt.close(loaded_fig)
148+
149+
104150
def test_gcf():
105151
fig = plt.figure("a label")
106152
buf = BytesIO()

0 commit comments

Comments
 (0)
0