14
14
from matplotlib import pyplot as plt
15
15
from matplotlib .testing .decorators import (image_comparison , knownfailureif ,
16
16
cleanup )
17
- from matplotlib .testing import skip
17
+ from matplotlib .testing import skip , closed_tempfile
18
18
19
19
if 'TRAVIS' not in os .environ :
20
20
@image_comparison (baseline_images = ['pdf_use14corefonts' ],
@@ -57,14 +57,12 @@ def test_multipage_pagecount():
57
57
@cleanup
58
58
def test_multipage_keep_empty ():
59
59
from matplotlib .backends .backend_pdf import PdfPages
60
- from tempfile import NamedTemporaryFile
61
60
# test empty pdf files
62
61
# test that an empty pdf is left behind with keep_empty=True (default)
63
- with NamedTemporaryFile ( delete = False ) as tmp :
62
+ with closed_tempfile ( ".pdf" ) as tmp :
64
63
with PdfPages (tmp ) as pdf :
65
64
filename = pdf ._file .fh .name
66
65
assert os .path .exists (filename )
67
- os .remove (filename )
68
66
# test if an empty pdf is deleting itself afterwards with keep_empty=False
69
67
with PdfPages (filename , keep_empty = False ) as pdf :
70
68
pass
@@ -74,19 +72,17 @@ def test_multipage_keep_empty():
74
72
ax = fig .add_subplot (111 )
75
73
ax .plot ([1 , 2 , 3 ])
76
74
# test that a non-empty pdf is left behind with keep_empty=True (default)
77
- with NamedTemporaryFile ( delete = False ) as tmp :
75
+ with closed_tempfile ( ".pdf" ) as tmp :
78
76
with PdfPages (tmp ) as pdf :
79
77
filename = pdf ._file .fh .name
80
78
pdf .savefig ()
81
79
assert os .path .exists (filename )
82
- os .remove (filename )
83
80
# test that a non-empty pdf is left behind with keep_empty=False
84
- with NamedTemporaryFile ( delete = False ) as tmp :
81
+ with closed_tempfile ( ".pdf" ) as tmp :
85
82
with PdfPages (tmp , keep_empty = False ) as pdf :
86
83
filename = pdf ._file .fh .name
87
84
pdf .savefig ()
88
85
assert os .path .exists (filename )
89
- os .remove (filename )
90
86
91
87
92
88
@cleanup
@@ -137,20 +133,17 @@ def test_grayscale_alpha():
137
133
138
134
@cleanup
139
135
def test_pdfpages_accept_pep_519 ():
140
- from tempfile import NamedTemporaryFile
141
-
142
136
class FakeFSPathClass (object ):
143
137
def __init__ (self , path ):
144
138
self ._path = path
145
139
146
140
def __fspath__ (self ):
147
141
return self ._path
148
- tmpfile = NamedTemporaryFile (suffix = '.pdf' )
149
- tmpfile .close ()
150
- with PdfPages (FakeFSPathClass (tmpfile .name )) as pdf :
151
- fig , ax = plt .subplots ()
152
- ax .plot ([1 , 2 ], [3 , 4 ])
153
- pdf .savefig (fig )
142
+ with closed_tempfile (suffix = '.pdf' ) as fname :
143
+ with PdfPages (FakeFSPathClass (fname )) as pdf :
144
+ fig , ax = plt .subplots ()
145
+ ax .plot ([1 , 2 ], [3 , 4 ])
146
+ pdf .savefig (fig )
154
147
155
148
156
149
@cleanup
@@ -159,13 +152,11 @@ def test_savefig_accept_pathlib():
159
152
from pathlib import Path
160
153
except ImportError :
161
154
skip ("pathlib not installed" )
162
- from tempfile import NamedTemporaryFile
163
155
164
156
fig , ax = plt .subplots ()
165
157
ax .plot ([1 , 2 ], [3 , 4 ])
166
- tmpfile = NamedTemporaryFile (suffix = '.pdf' )
167
- tmpfile .close ()
168
- with PdfPages (Path (tmpfile .name )) as pdf :
169
- fig , ax = plt .subplots ()
170
- ax .plot ([1 , 2 ], [3 , 4 ])
171
- pdf .savefig (fig )
158
+ with closed_tempfile (suffix = '.pdf' ) as fname :
159
+ with PdfPages (Path (fname )) as pdf :
160
+ fig , ax = plt .subplots ()
161
+ ax .plot ([1 , 2 ], [3 , 4 ])
162
+ pdf .savefig (fig )
0 commit comments