Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix AttributeError for pickle load of Figure class #23462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -3038,7 +3038,8 @@ def __setstate__(self, state):
import matplotlib._pylab_helpers as pylab_helpers
allnums = plt.get_fignums()
num = max(allnums) + 1 if allnums else 1
mgr = plt._backend_mod.new_figure_manager_given_figure(num, self)
backend = plt._get_backend_mod()
mgr = backend.new_figure_manager_given_figure(num, self)
pylab_helpers.Gcf._set_new_active_manager(mgr)
plt.draw_if_interactive()

Expand Down
53 changes: 49 additions & 4 deletions lib/matplotlib/tests/test_pickle.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from io import BytesIO
import ast
import pickle

import numpy as np
import pytest

import matplotlib as mpl
from matplotlib import cm
from matplotlib.testing import subprocess_run_helper
from matplotlib.testing.decorators import check_figures_equal
from matplotlib.dates import rrulewrapper
from matplotlib.lines import VertexSelector
Expand Down Expand Up @@ -42,9 +44,7 @@ def test_simple():
pickle.dump(fig, BytesIO(), pickle.HIGHEST_PROTOCOL)


@mpl.style.context("default")
@check_figures_equal(extensions=["png"])
def test_complete(fig_test, fig_ref):
def _generate_complete_test_figure(fig_ref):
fig_ref.set_size_inches((10, 6))
plt.figure(fig_ref)

Expand Down Expand Up @@ -83,12 +83,17 @@ def test_complete(fig_test, fig_ref):
plt.quiver(x, y, u, v)

plt.subplot(3, 3, 8)
plt.scatter(x, x**2, label='$x^2$')
plt.scatter(x, x ** 2, label='$x^2$')
plt.legend(loc='upper left')

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


@mpl.style.context("default")
@check_figures_equal(extensions=["png"])
def test_complete(fig_test, fig_ref):
_generate_complete_test_figure(fig_ref)
# plotting is done, now test its pickle-ability
pkl = BytesIO()
pickle.dump(fig_ref, pkl, pickle.HIGHEST_PROTOCOL)
Expand All @@ -101,6 +106,46 @@ def test_complete(fig_test, fig_ref):
plt.close(loaded)


def _pickle_load_subprocess():
import os
import pickle

path = os.environ['PICKLE_FILE_PATH']

with open(path, 'rb') as blob:
fig = pickle.load(blob)

print(str(pickle.dumps(fig)))


@mpl.style.context("default")
@check_figures_equal(extensions=['png'])
def test_pickle_load_from_subprocess(fig_test, fig_ref, tmp_path):
_generate_complete_test_figure(fig_ref)

fp = tmp_path / 'sinus.pickle'
assert not fp.exists()

with fp.open('wb') as file:
pickle.dump(fig_ref, file, pickle.HIGHEST_PROTOCOL)
assert fp.exists()

proc = subprocess_run_helper(
_pickle_load_subprocess,
timeout=60,
extra_env={'PICKLE_FILE_PATH': str(fp)}
)

loaded_fig = pickle.loads(ast.literal_eval(proc.stdout))

loaded_fig.canvas.draw()

fig_test.set_size_inches(loaded_fig.get_size_inches())
fig_test.figimage(loaded_fig.canvas.renderer.buffer_rgba())

plt.close(loaded_fig)


def test_gcf():
fig = plt.figure("a label")
buf = BytesIO()
Expand Down