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

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
QuLogic merged 2 commits intomatplotlib:mainfromwsykala:figure-pickle-load-fix
Jul 26, 2022

Conversation

wsykala
Copy link
Contributor

PR Summary

When saving a figure with thepickle module in onepython process and then loading it in a different process a following error appears:

/figure.py", line 2911, in __setstate__    mgr = plt._backend_mod.new_figure_manager_given_figure(num, self)AttributeError: 'NoneType' object has no attribute 'new_figure_manager_given_figure'

This PR replaces theplt._backend_mod with the call toplt._get_backend_mod() in theFigure.__setstate__ method. This change ensures that the_backend_mod is notNone duringpickle load.

A reproducible example is provided in the comments

PR Checklist

Tests and Styling

  • Has pytest style unit tests (andpytest passes).
  • IsFlake 8 compliant (installflake8-docstrings and runflake8 --docstring-convention=all).

Documentation

  • New features are documented, with examples if plot related.
  • New features have an entry indoc/users/next_whats_new/ (follow instructions in README.rst there).
  • API changes documented indoc/api/next_api_changes/ (follow instructions in README.rst there).
  • Documentation is sphinx and numpydoc compliant (the docs shouldbuild without error).

@wsykala
Copy link
ContributorAuthor

wsykala commentedJul 21, 2022
edited
Loading

Reproducible example:

# save.pyimport numpy as npimport matplotlib.pyplot as pltimport picklefig_handle = plt.figure()x = np.linspace(0,2*np.pi)y = np.sin(x)with open('sinus.pickle','wb') as file:    pickle.dump(fig_handle, file)
# read.pyimport matplotlib.pyplot as pltimport pickleimport numpy as npwith open('sinus.pickle', 'rb') as blob:    fig = pickle.load(blob)

Outcome:

$ python save.py$ python read.pyTraceback (most recent call last):  File "/Users/wsykala/projects/playground/read.py", line 6, in <module>    fig_handle = pl.load(open('sinus.pickle','rb'))  File "/Users/wsykala/miniconda3/envs/matplotlib/lib/python3.9/site-packages/matplotlib/figure.py", line 2911, in __setstate__    mgr = plt._backend_mod.new_figure_manager_given_figure(num, self)AttributeError: 'NoneType' object has no attribute 'new_figure_manager_given_figure'

@wsykalawsykalaforce-pushed thefigure-pickle-load-fix branch fromea0363d to30da817CompareJuly 21, 2022 20:03
Copy link

@github-actionsgithub-actionsbot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a while, please feel free to ping@matplotlib/developers or anyone who has commented on the PR. Most of our reviewers are volunteers and sometimes things fall through the cracks.

You can also join uson gitter for real-time discussion.

For details on testing, writing docs, and our review process, please seethe developer guide

We strive to be a welcoming and open project. Please follow ourCode of Conduct.

@QuLogic
Copy link
Member

We already have tests for pickling; I assume they didn't catch this because it's all in the same process.

We definitely need a test to be sure that this is working.

@tacaswelltacaswell added this to thev3.5.3 milestoneJul 21, 2022
@tacaswell
Copy link
Member

This is another fallout from making backend setup lazier in pyplot (previously we would callswitch_backend on import which prevented this).

I suspecthttps://docs.pytest.org/en/7.1.x/how-to/tmp_path.html#the-tmp-path-fixture +

defsubprocess_run_helper(func,*args,timeout,extra_env=None):
"""
Run a function in a sub-process.
Parameters
----------
func : function
The function to be run. It must be in a module that is importable.
*args : str
Any additional command line arguments to be passed in
the first argument to ``subprocess.run``.
extra_env : dict[str, str]
Any additional environment variables to be set for the subprocess.
"""
is what we need to test this.

@wsykalawsykalaforce-pushed thefigure-pickle-load-fix branch from7d5f53a to2a3de64CompareJuly 22, 2022 12:55
@wsykalawsykalaforce-pushed thefigure-pickle-load-fix branch from2a3de64 to3c5c2f8CompareJuly 22, 2022 13:39
@wsykala
Copy link
ContributorAuthor

wsykala commentedJul 22, 2022
edited
Loading

@tacaswell@QuLogic
Added a test for this behaviour and extracted the code that was duplicated. The test scenario is as follows:

We create and pickle the figure in the main process. Next we run thepickle.load and the dump it to stdout in the subprocess. After that the binary representation of the figure is loaded from stdout usingast, and we compare it the same way as intest_complete.

I am not sure why thecircleci job for building docs failed though.

Also one more point. When creating the test I came across a strange bug. I described it in this issue#23471

@jklymak
Copy link
Member

The CI errors are

/home/circleci/project/lib/matplotlib/axes/_base.py:docstring of matplotlib.axes._base._AxesBase.set_prop_cycle:14: WARNING: py:obj reference target not found: cycler.Cycler/home/circleci/project/lib/matplotlib/axes/_base.py:docstring of matplotlib.axes._base._AxesBase.set_prop_cycle:16: WARNING: py:obj reference target not found: cycler.Cycler/home/circleci/project/lib/matplotlib/axes/_base.py:docstring of matplotlib.axes._base._AxesBase.set_prop_cycle:23: WARNING: py:obj reference target not found: cycler.Cycler/home/circleci/project/lib/matplotlib/axes/_base.py:docstring of matplotlib.axes._base._AxesBase.set_prop_cycle:23: WARNING: py:obj reference target not found: cycler.cycler/home/circleci/project/lib/matplotlib/axes/_base.py:docstring of matplotlib.axes._base._AxesBase.set_prop_cycle:55: WARNING: py:obj reference target not found: cycler.cycler/home/circleci/project/lib/matplotlib/rcsetup.py:docstring of matplotlib.rcsetup.cycler:2: WARNING: py:obj reference target not found: cycler.Cycler/home/circleci/project/lib/matplotlib/rcsetup.py:docstring of matplotlib.rcsetup.cycler:2: WARNING: py:func reference target not found: cycler.cycler/home/circleci/project/lib/matplotlib/rcsetup.py:docstring of matplotlib.rcsetup.cycler:11: WARNING: py:obj reference target not found: cycler.Cycler/home/circleci/project/lib/matplotlib/rcsetup.py:docstring of matplotlib.rcsetup.cycler:13: WARNING: py:obj reference target not found: cycler.Cycler/home/circleci/project/lib/matplotlib/rcsetup.py:docstring of matplotlib.rcsetup.cycler:17: WARNING: py:obj reference target not found: cycler.Cycler/home/circleci/project/lib/matplotlib/rcsetup.py:docstring of matplotlib.rcsetup.cycler:38: WARNING: py:class reference target not found: cycler.Cycler

But I don't see that this PR should have caused that....

@tacaswell
Copy link
Member

I think the docs failure is related to hosting issues (which were resolved by our providers), re-restarted docs build.

jklymak and wsykala reacted with thumbs up emoji

@QuLogicQuLogic merged commit7117a16 intomatplotlib:mainJul 26, 2022
@lumberbot-app
Copy link

Owee, I'm MrMeeseeks, Look at me.

There seem to be a conflict, please backport manually. Here are approximate instructions:

  1. Checkout backport branch and update it.
git checkout v3.5.xgit pull
  1. Cherry pick the first parent branch of the this PR on top of the older branch:
git cherry-pick -x -m1 7117a16eabe67c374fde085f17e0eb905149cadc
  1. You will likely have some merge/cherry-pick conflict here, fix them and commit:
git commit -am 'Backport PR #23462: Fix AttributeError for pickle load of Figure class'
  1. Push to a named branch:
git push YOURFORK v3.5.x:auto-backport-of-pr-23462-on-v3.5.x
  1. Create a PR against branch v3.5.x, I would have named this PR:

"Backport PR#23462 on branch v3.5.x (Fix AttributeError for pickle load of Figure class)"

And apply the correct labels and milestones.

Congratulations — you did some good work! Hopefully your backport PR will be tested by the continuous integration and merged soon!

Remember to remove theStill Needs Manual Backport label once the PR gets merged.

If these instructions are inaccurate, feel free tosuggest an improvement.

@QuLogic
Copy link
Member

Thanks@wsykala! Congratulations on your first PR to Matplotlib 🎉 We hope to hear from you again.

wsykala reacted with heart emoji

@tacaswell
Copy link
Member

There are same pretty big changes in the test file on main that are not an 3.5.x (for example the pickle test went to an figures equal test from an image comparison test!).

I'm inclined to backport this without backporting the tests. It is not ideal, but I think it is less bad than re-writing the tests to not confilct (and dealing with a messy merge up) or implicitly backporting a bunch of other changes to the tests.

@tacaswell
Copy link
Member

Discussed on the call. Plan: copy the tests back and accept the code duplication and clean it up on merge up.

tacaswell added a commit to tacaswell/matplotlib that referenced this pull requestJul 29, 2022
…igure classThere were a number of complications with the backport: - the new test generalized an existing test that had picked up   additional changes on the main branch - the signature of the subproccess helper has changed on the main branch
QuLogic added a commit that referenced this pull requestJul 29, 2022
…-v3.5.xBackport PR#23462: Fix AttributeError for pickle load of Figure class
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@github-actionsgithub-actions[bot]github-actions[bot] left review comments

@tacaswelltacaswelltacaswell approved these changes

@QuLogicQuLogicQuLogic approved these changes

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
v3.5.3
Development

Successfully merging this pull request may close these issues.

4 participants
@wsykala@QuLogic@tacaswell@jklymak

[8]ページ先頭

©2009-2025 Movatter.jp