10000 Figure is unpicklable after `savefig` · Issue #6870 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Figure is unpicklable after savefig #6870

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

Closed
Kojoley opened this issue Jul 31, 2016 · 7 comments
Closed

Figure is unpicklable after savefig #6870

Kojoley opened this issue Jul 31, 2016 · 7 comments
Milestone

Comments

@Kojoley
Copy link
Member
Kojoley commented Jul 31, 2016
import matplotlib.pyplot as plt
import pickle
from io import BytesIO

def test_pickle_after_savefig():
    fig = plt.figure()
    fig.add_subplot(111)
    for fmt in ['pdf', 'png', 'svg']:
        fig.savefig(BytesIO(), format=fmt)
        try:
            pickle.dump(fig, BytesIO())
        except:
            print('Unpickable after "%s"' % fmt)
            raise

test_pickle_after_savefig()
>python test_pickle_after_savefig.py
Unpickable after "svg"
Traceback (most recent call last):
  File "test_pickle_after_savefig.py", line 16, in <module>
    test_pickle_after_savefig()
  File "test_pickle_after_savefig.py", line 11, in test_pickle_after_savefig
    pickle.dump(fig, BytesIO())
TypeError: cannot serialize '_io.TextIOWrapper' object

I think those warnings from pytest are maybe related:

>py.test -n 4 lib/matplotlib/tests/test_backend_svg.py::test_fill_black_with_alpha
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-2.9.2, py-1.4.31, pluggy-0.3.1
rootdir: C:\Working\Repositories\matplotlib, inifile: pytest.ini
plugins: xdist-1.14
gw0 [1] / gw1 [1] / gw2 [1] / gw3 [1]
scheduling tests via LoadScheduling
.sys:1: ResourceWarning: unclosed file <_io.TextIOWrapper name=0 mode='r' encoding='cp866'>
sys:1: ResourceWarning: unclosed file <_io.TextIOWrapper name=1 mode='w' encoding='cp866'>
sys:1: ResourceWarning: unclosed file <_io.TextIOWrapper name=0 mode='r' encoding='cp866'>
sys:1: ResourceWarning: unclosed file <_io.TextIOWrapper name=0 mode='r' encoding='cp866'>
sys:1: ResourceWarning: unclosed file <_io.TextIOWrapper name=1 mode='w' encoding='cp866'>
sys:1: ResourceWarning: unclosed file <_io.TextIOWrapper name=1 mode='w' encoding='cp866'>
sys:1: ResourceWarning: unclosed file <_io.TextIOWrapper name=0 mode='r' encoding='cp866'>
sys:1: ResourceWarning: unclosed file <_io.TextIOWrapper name=1 mode='w' encoding='cp866'>

With a file name pickle fails in the pdf backend (and svg too):

import matplotlib.pylab as plt
import pickle
from io import BytesIO

def test_pickle_after_savefig2():
    fig = plt.figure()
    fig.add_subplot(111)
    for fmt in ['pdf', 'png', 'svg']:
        fig.savefig('test.%s' % fmt, format=fmt)
        try:
            pickle.dump(fig, BytesIO())
        except:
            print('Unpickable format is "%s"' % fmt)
            raise

test_pickle_after_savefig2()
>python test_pickle_after_savefig2.py
Unpickable format is "pdf"
Traceback (most recent call last):
  File "test_pickle_after_savefig2.py", line 16, in <module>
    test_pickle_after_savefig()
  File "test_pickle_after_savefig2.py", line 11, in test_pickle_after_savefig
    pickle.dump(fig, BytesIO())
TypeError: cannot serialize '_io.BufferedWriter' object
@dopplershift dopplershift changed the title Figure is unpickable after savefig Figure is unpicklable after savefig Jul 31, 2016
@Kojoley
Copy link
Member Author
Kojoley commented Jul 31, 2016

I have tracked down the problem in the svg backend. XMLWriter (locates in fig._cachedRenderer._renderer.writer) keeps reference to the file object. The easiest solution is to add self.writer = None or del self.writer to the RendererSVG.finalize here, but I still do not know if it is the right solution.

@tacaswell
Copy link
Member

The way we have solved this in other places in the codebase is to customize __getstate__ and __setstate__ on the objects that have non-pickable children.

@Kojoley
Copy link
Member Author
Kojoley commented Aug 1, 2016

But then that warning from pytest will not be solved.

@tacaswell tacaswell added this to the 2.0.1 (next bug fix release) milestone Aug 1, 2016
@Kojoley
Copy link
Member Author
Kojoley commented Aug 1, 2016

I see not reason why renderer should have reference to the file object passed to savefig

@tacaswell
Copy link
Member

Yes, you are correct, we should drop the reference to the file object in in the finalize step.

@jrbrodie77
Copy link
jrbrodie77 commented Sep 22, 2016

UPDATED: I meant to post this comment on #6899 (which is related to this issue)
I was just getting ready to create an issue with test cases. I'm seeing savefig side effects with 2.0.0b4
Is this fix in that version?

@Kojoley
Copy link
Member Author
Kojoley commented Sep 22, 2016

I'm seeing savefig side effects with 2.0.0b4

Then you are commenting on a wrong issue.

QuLogic added a commit to QuLogic/matplotlib that referenced this issue Oct 22, 2016
It's not allowed to be pickled on a Figure, so presumably it shouldn't
be allowed on the Axes either.

Fixes matplotlib#6870.
Fixes matplotlib#6181.
Fixes matplotlib#3899.
@QuLogic QuLogic closed this as completed in 519b050 Nov 1, 2016
bcongdon pushed a commit to bcongdon/matplotlib that referenced this issue Nov 8, 2016
It's not allowed to be pickled on a Figure, so presumably it shouldn't
be allowed on the Axes either.

Fixes matplotlib#6870.
Fixes matplotlib#6181.
Fixes matplotlib#3899.
bcongdon pushed a commit to bcongdon/matplotlib that referenced this issue Nov 8, 2016
It's not allowed to be pickled on a Figure, so presumably it shouldn't
be allowed on the Axes either.

Fixes matplotlib#6870.
Fixes matplotlib#6181.
Fixes matplotlib#3899.
@QuLogic QuLogic modified the milestones: 2.0 (style change major release), 2.0.1 (next bug fix release) Dec 7, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants
0