Skip to content

Commit 2a3de64

Browse files
committed
Add test case for loading figure from a subprocess, and refactor the code.
1 parent 30da817 commit 2a3de64

File tree

1 file changed

+49
-4
lines changed

1 file changed

+49
-4
lines changed

lib/matplotlib/tests/test_pickle.py

Lines changed: 49 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,17 @@ 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)
9297
# plotting is done, now test its pickle-ability
9398
pkl = BytesIO()
9499
pickle.dump(fig_ref, pkl, pickle.HIGHEST_PROTOCOL)
@@ -101,6 +106,46 @@ def test_complete(fig_test, fig_ref):
101106
plt.close(loaded)
102107

103108

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

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy