From 30da817683ba6ef7ec02c5919a8f0055b74f442f Mon Sep 17 00:00:00 2001 From: wsykala <38567820+wsykala@users.noreply.github.com> Date: Thu, 21 Jul 2022 22:02:59 +0200 Subject: [PATCH 1/2] Replace the _backend_mod attribute access with the call to _get_backend_mod function in Figure.__setstate__ --- lib/matplotlib/figure.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index 096e84ae4bf9..b3d00201c2dc 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -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() From 3c5c2f8bbdbaa8cd57098022b80d73eebeee3b34 Mon Sep 17 00:00:00 2001 From: wsykala <38567820+wsykala@users.noreply.github.com> Date: Fri, 22 Jul 2022 15:39:06 +0200 Subject: [PATCH 2/2] Add test case for loading figure from a subprocess, and refactor the code. --- lib/matplotlib/tests/test_pickle.py | 53 ++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/tests/test_pickle.py b/lib/matplotlib/tests/test_pickle.py index 0d0a5e597767..ec6bdcc2fe14 100644 --- a/lib/matplotlib/tests/test_pickle.py +++ b/lib/matplotlib/tests/test_pickle.py @@ -1,4 +1,5 @@ from io import BytesIO +import ast import pickle import numpy as np @@ -6,6 +7,7 @@ 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 @@ -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) @@ -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) @@ -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() 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