@@ -31,12 +31,9 @@ def test_visibility():
31
31
for artist in b :
32
32
artist .set_visible (False )
33
33
34
- fd = BytesIO ()
35
- fig .savefig (fd , format = 'svg' )
36
-
37
- fd .seek (0 )
38
- buf = fd .read ()
39
- fd .close ()
34
+ with BytesIO () as fd :
35
+ fig .savefig (fd , format = 'svg' )
36
+ buf = fd .getvalue ()
40
37
41
38
parser = xml .parsers .expat .ParserCreate ()
42
39
parser .Parse (buf ) # this will raise ExpatError if the svg is invalid
@@ -65,11 +62,9 @@ def test_text_urls():
65
62
test_url = "http://test_text_urls.matplotlib.org"
66
63
fig .suptitle ("test_text_urls" , url = test_url )
67
64
68
- fd = BytesIO ()
69
- fig .savefig (fd , format = 'svg' )
70
- fd .seek (0 )
71
- buf = fd .read ().decode ()
72
- fd .close ()
65
+ with BytesIO () as fd :
66
+ fig .savefig (fd , format = 'svg' )
67
+ buf = fd .getvalue ().decode ()
73
68
74
69
expected = '<a xlink:href="{0}">' .format (test_url )
75
70
assert expected in buf
@@ -177,11 +172,9 @@ def test_gid():
177
172
gdic [gid ] = obj
178
173
obj .set_gid (gid )
179
174
180
- fd = BytesIO ()
181
- fig .savefig (fd , format = 'svg' )
182
- fd .seek (0 )
183
- buf = fd .read ().decode ()
184
- fd .close ()
175
+ with BytesIO () as fd :
176
+ fig .savefig (fd , format = 'svg' )
177
+ buf = fd .getvalue ().decode ()
185
178
186
179
def include (gid , obj ):
187
180
# we need to exclude certain objects which will not appear in the svg
@@ -270,11 +263,9 @@ def test_svg_default_metadata(monkeypatch):
270
263
monkeypatch .setenv ('SOURCE_DATE_EPOCH' , '19680801' )
271
264
272
265
fig , ax = plt .subplots ()
273
- fd = BytesIO ()
274
- fig .savefig (fd , format = 'svg' )
275
- fd .seek (0 )
276
- buf = fd .read ().decode ()
277
- fd .close ()
266
+ with BytesIO () as fd :
267
+ fig .savefig (fd , format = 'svg' )
268
+ buf = fd .getvalue ().decode ()
278
269
279
270
# Creator
280
271
assert mpl .__version__ in buf
@@ -299,11 +290,9 @@ def test_svg_metadata():
299
290
}
300
291
301
292
fig , ax = plt .subplots ()
302
- fd = BytesIO ()
303
- fig .savefig (fd , format = 'svg' , metadata = metadata )
304
- fd .seek (0 )
305
- buf = fd .read ().decode ()
306
- fd .close ()
293
+ with BytesIO () as fd :
294
+ fig .savefig (fd , format = 'svg' , metadata = metadata )
295
+ buf = fd .getvalue ().decode ()
307
296
308
297
# Check things that are easy, single or multi-value entries.
309
298
for k in ['Description' , * single_value ]:
0 commit comments