Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit3c5c2f8

Browse files
committed
Add test case for loading figure from a subprocess, and refactor the code.
1 parent30da817 commit3c5c2f8

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
fromioimportBytesIO
2+
importast
23
importpickle
34

45
importnumpyasnp
56
importpytest
67

78
importmatplotlibasmpl
89
frommatplotlibimportcm
10+
frommatplotlib.testingimportsubprocess_run_helper
911
frommatplotlib.testing.decoratorsimportcheck_figures_equal
1012
frommatplotlib.datesimportrrulewrapper
1113
frommatplotlib.linesimportVertexSelector
@@ -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-
deftest_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+
deftest_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+
importos
111+
importpickle
112+
113+
path=os.environ['PICKLE_FILE_PATH']
114+
115+
withopen(path,'rb')asblob:
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+
deftest_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+
assertnotfp.exists()
128+
129+
withfp.open('wb')asfile:
130+
pickle.dump(fig_ref,file,pickle.HIGHEST_PROTOCOL)
131+
assertfp.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
deftest_gcf():
105150
fig=plt.figure("a label")
106151
buf=BytesIO()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp