Matplotlib savefig() closes BytesIO object when saving in postscript format #1862

Merged
merged 1 commit into from Mar 27, 2013

Conversation

Projects
None yet
2 participants
Contributor

mdboom commented Mar 27, 2013

In Python 3, I am seeing the following bug:

In [1]: from io import BytesIO

In [2]: import matplotlib.pyplot as plt

In [3]: b = BytesIO()

In [4]: fig = plt.figure()

In [5]: ax = fig.add_subplot(1,1,1)

In [6]: ax.plot([1,2,3])
Out[6]: [<matplotlib.lines.Line2D at 0x104a90990>]

In [7]: fig.savefig(b, format='eps')

In [8]: b.seek(0)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-8-22abda41fe70> in <module>()
----> 1 b.seek(0)

ValueError: I/O operation on closed file.

As far as I can tell, this does not occur with StringIO in Python 2.x.

@mdboom - is this related to #1779?

Owner

mdboom commented Mar 27, 2013

Nope -- this bug is only on master, and perhaps related to #1826.

@mdboom mdboom Fixes #1862: The PS backend would inadvertently close file handles pa…
…ssed to it on Python 3. The TextIOWrapper that handles the encoding from unicode to ascii needs to be detached before its destructor is called, otherwise it will call close on the underlying file object. Also cleans up how with statements are handled to avoid the sometimes problematic null contextmanager.
820a326
Contributor

astrofrog commented Mar 27, 2013

@mdboom - this works for me - thanks!

@mdboom mdboom added a commit that referenced this pull request Mar 27, 2013

@mdboom mdboom Merge pull request #1862 from mdboom/ps-file-closing
Matplotlib savefig() closes BytesIO object when saving in postscript format
ca476df

@mdboom mdboom merged commit ca476df into matplotlib:master Mar 27, 2013

1 check failed

default The Travis build could not complete due to an error
Details

@HubertHolin HubertHolin added a commit to HubertHolin/matplotlib that referenced this pull request Apr 7, 2013

@mdboom @HubertHolin mdboom + HubertHolin Fixes #1862: The PS backend would inadvertently close file handles pa…
…ssed to it on Python 3. The TextIOWrapper that handles the encoding from unicode to ascii needs to be detached before its destructor is called, otherwise it will call close on the underlying file object. Also cleans up how with statements are handled to avoid the sometimes problematic null contextmanager.
1a81502

@pelson pelson added a commit to pelson/matplotlib that referenced this pull request Apr 29, 2013

@mdboom @pelson mdboom + pelson Fixes #1862: The PS backend would inadvertently close file handles pa…
…ssed to it on Python 3. The TextIOWrapper that handles the encoding from unicode to ascii needs to be detached before its destructor is called, otherwise it will call close on the underlying file object. Also cleans up how with statements are handled to avoid the sometimes problematic null contextmanager.
8a992cc

mdboom deleted the mdboom:ps-file-closing branch Aug 7, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment