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

Skip to content

Commit e2fbeb3

Browse files
committed
Merge pull request matplotlib#3250 from cgohlke/patch-2
Fix WindowsError: [Error 32] The process cannot access the file
1 parent 142435c commit e2fbeb3

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

lib/matplotlib/backends/backend_pdf.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
"""
44
A PDF matplotlib backend
5-
Author: Jouni K Seppänen <[email protected]>
5+
Author: Jouni K Sepp�nen <[email protected]>
66
"""
77
from __future__ import (absolute_import, division, print_function,
88
unicode_literals)
@@ -2382,7 +2382,8 @@ def close(self):
23822382
PDF file.
23832383
"""
23842384
self._file.close()
2385-
if self.get_pagecount() == 0 and self.keep_empty is False:
2385+
if (self.get_pagecount() == 0 and not self.keep_empty
2386+
and not self._file.passed_in_file_object):
23862387
os.remove(self._file.fh.name)
23872388
self._file = None
23882389

lib/matplotlib/tests/test_backend_pdf.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,11 @@ def test_multipage_keep_empty():
6464
with PdfPages(tmp) as pdf:
6565
filename = pdf._file.fh.name
6666
assert os.path.exists(filename)
67-
os.remove(filename)
67+
os.remove(filename)
6868
# test if an empty pdf is deleting itself afterwards with keep_empty=False
69-
with NamedTemporaryFile(delete=False) as tmp:
70-
with PdfPages(tmp, keep_empty=False) as pdf:
71-
filename = pdf._file.fh.name
72-
assert not os.path.exists(filename)
69+
with PdfPages(filename, keep_empty=False) as pdf:
70+
pass
71+
assert not os.path.exists(filename)
7372
### test pdf files with content, they should never be deleted
7473
fig = plt.figure()
7574
ax = fig.add_subplot(111)
@@ -80,11 +79,11 @@ def test_multipage_keep_empty():
8079
filename = pdf._file.fh.name
8180
pdf.savefig()
8281
assert os.path.exists(filename)
83-
os.remove(filename)
82+
os.remove(filename)
8483
# test that a non-empty pdf is left behind with keep_empty=False
8584
with NamedTemporaryFile(delete=False) as tmp:
8685
with PdfPages(tmp, keep_empty=False) as pdf:
8786
filename = pdf._file.fh.name
8887
pdf.savefig()
8988
assert os.path.exists(filename)
90-
os.remove(filename)
89+
os.remove(filename)

0 commit comments

Comments
 (0)