8000 Merge pull request #23462 from wsykala/figure-pickle-load-fix · matplotlib/matplotlib@7117a16 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7117a16

Browse files
authored
Merge pull request #23462 from wsykala/figure-pickle-load-fix
Fix AttributeError for pickle load of Figure class
2 parents 2dcc824 + 3c5c2f8 commit 7117a16

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

lib/matplotlib/figure.py

Lines changed: 2 additions & 8000 amp; 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3055,7 +3055,8 @@ def __setstate__(self, state):
30553055
import matplotlib._pylab_helpers as pylab_helpers
30563056
allnums = plt.get_fignums()
30573057
num = max(allnums) + 1 if allnums else 1
3058-
mgr = plt._backend_mod.new_figure_manager_given_figure(num, self)
3058+
backend = plt._get_backend_mod()
3059+
mgr = backend.new_figure_manager_given_figure(num, self)
30593060
pylab_helpers.Gcf._set_new_active_manager(mgr)
30603061
plt.draw_if_interactive()
30613062

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)
0