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

Skip to content

Fix WindowsError: [Error 32] The process cannot access the file #3250

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 3 commits into from
Jul 18, 2014
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
5 changes: 3 additions & 2 deletions lib/matplotlib/backends/backend_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

"""
A PDF matplotlib backend
Author: Jouni K Sepp�nen <[email protected]>
Author: Jouni K Sepp�nen <[email protected]>
"""
from __future__ import (absolute_import, division, print_function,
unicode_literals)
Expand Down Expand Up @@ -2382,7 +2382,8 @@ def close(self):
PDF file.
"""
self._file.close()
if self.get_pagecount() == 0 and self.keep_empty is False:
if (self.get_pagecount() == 0 and not self.keep_empty
and not self._file.passed_in_file_object):
os.remove(self._file.fh.name)
self._file = None

Expand Down
13 changes: 6 additions & 7 deletions lib/matplotlib/tests/test_backend_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,11 @@ def test_multipage_keep_empty():
with PdfPages(tmp) as pdf:
filename = pdf._file.fh.name
assert os.path.exists(filename)
os.remove(filename)
os.remove(filename)
# test if an empty pdf is deleting itself afterwards with keep_empty=False
with NamedTemporaryFile(delete=False) as tmp:
with PdfPages(tmp, keep_empty=False) as pdf:
filename = pdf._file.fh.name
assert not os.path.exists(filename)
with PdfPages(filename, keep_empty=False) as pdf:
pass
assert not os.path.exists(filename)
### test pdf files with content, they should never be deleted
fig = plt.figure()
ax = fig.add_subplot(111)
Expand All @@ -80,11 +79,11 @@ def test_multipage_keep_empty():
filename = pdf._file.fh.name
pdf.savefig()
assert os.path.exists(filename)
os.remove(filename)
os.remove(filename)
# test that a non-empty pdf is left behind with keep_empty=False
with NamedTemporaryFile(delete=False) as tmp:
with PdfPages(tmp, keep_empty=False) as pdf:
filename = pdf._file.fh.name
pdf.savefig()
assert os.path.exists(filename)
os.remove(filename)
os.remove(filename)