From 386a088dbd7c09759aa7d8d11d78019f0e1cabf7 Mon Sep 17 00:00:00 2001 From: cgohlke Date: Sun, 13 Jul 2014 13:10:46 -0700 Subject: [PATCH 1/3] TST: do not remove files while they are open Fixes WindowsError: [Error 32] The process cannot access the file because it is being used by another process --- lib/matplotlib/tests/test_backend_pdf.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/tests/test_backend_pdf.py b/lib/matplotlib/tests/test_backend_pdf.py index a5fcb5b17e4b..323078cefab8 100644 --- a/lib/matplotlib/tests/test_backend_pdf.py +++ b/lib/matplotlib/tests/test_backend_pdf.py @@ -64,7 +64,7 @@ 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: @@ -80,11 +80,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) From aec5a567391aaf2a6640f128356fca45bc798e7b Mon Sep 17 00:00:00 2001 From: cgohlke Date: Sun, 13 Jul 2014 13:24:03 -0700 Subject: [PATCH 2/3] BUG: do not remove file if passed in file object Fix WindowsError: [Error 32] The process cannot access the file because it is being used by another process --- lib/matplotlib/backends/backend_pdf.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/backends/backend_pdf.py b/lib/matplotlib/backends/backend_pdf.py index 49350e4fdda3..109f1ef92124 100644 --- a/lib/matplotlib/backends/backend_pdf.py +++ b/lib/matplotlib/backends/backend_pdf.py @@ -2,7 +2,7 @@ """ A PDF matplotlib backend -Author: Jouni K Seppänen +Author: Jouni K Sepp�nen """ from __future__ import (absolute_import, division, print_function, unicode_literals) @@ -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 From 0a6ee424cdff64e909bfec21869a0daee1498af3 Mon Sep 17 00:00:00 2001 From: cgohlke Date: Sun, 13 Jul 2014 13:27:55 -0700 Subject: [PATCH 3/3] TST: On Windows open files can not be removed --- lib/matplotlib/tests/test_backend_pdf.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/tests/test_backend_pdf.py b/lib/matplotlib/tests/test_backend_pdf.py index 323078cefab8..0e693402ca16 100644 --- a/lib/matplotlib/tests/test_backend_pdf.py +++ b/lib/matplotlib/tests/test_backend_pdf.py @@ -66,10 +66,9 @@ def test_multipage_keep_empty(): assert os.path.exists(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)