1
1
from io import BytesIO
2
+ import ast
2
3
import pickle
3
4
4
5
import numpy as np
5
6
import pytest
6
7
7
8
import matplotlib as mpl
8
9
from matplotlib import cm
10
+ from matplotlib .testing import subprocess_run_helper
9
11
from matplotlib .testing .decorators import check_figures_equal
10
12
from matplotlib .dates import rrulewrapper
11
13
from matplotlib .lines import VertexSelector
@@ -42,9 +44,7 @@ def test_simple():
42
44
pickle .dump (fig , BytesIO (), pickle .HIGHEST_PROTOCOL )
43
45
44
46
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 ):
48
48
fig_ref .set_size_inches ((10 , 6 ))
49
49
plt .figure (fig_ref )
50
50
@@ -83,12 +83,17 @@ def test_complete(fig_test, fig_ref):
83
83
plt .quiver (x , y , u , v )
84
84
85
85
plt .subplot (3 , 3 , 8 )
86
- plt .scatter (x , x ** 2 , label = '$x^2$' )
86
+ plt .scatter (x , x ** 2 , label = '$x^2$' )
87
87
plt .legend (loc = 'upper left' )
88
88
89
89
plt .subplot (3 , 3 , 9 )
90
90
plt .errorbar (x , x * - 0.5 , xerr = 0.2 , yerr = 0.4 )
91
91
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 )
92
97
# plotting is done, now test its pickle-ability
93
98
pkl = BytesIO ()
94
99
pickle .dump (fig_ref , pkl , pickle .HIGHEST_PROTOCOL )
@@ -101,6 +106,46 @@ def test_complete(fig_test, fig_ref):
101
106
plt .close (loaded )
102
107
103
108
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
+
104
149
def test_gcf ():
105
150
fig = plt .figure ("a label" )
106
151
buf = BytesIO ()
0 commit comments