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

Skip to content

Commit f0b133d

Browse files
committed
Simplify BytesIO usage in svg tests.
1 parent 15e2117 commit f0b133d

File tree

1 file changed

+15
-26
lines changed

1 file changed

+15
-26
lines changed

lib/matplotlib/tests/test_backend_svg.py

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,9 @@ def test_visibility():
3131
for artist in b:
3232
artist.set_visible(False)
3333

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()
4037

4138
parser = xml.parsers.expat.ParserCreate()
4239
parser.Parse(buf) # this will raise ExpatError if the svg is invalid
@@ -65,11 +62,9 @@ def test_text_urls():
6562
test_url = "http://test_text_urls.matplotlib.org"
6663
fig.suptitle("test_text_urls", url=test_url)
6764

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()
7368

7469
expected = '<a xlink:href="{0}">'.format(test_url)
7570
assert expected in buf
@@ -177,11 +172,9 @@ def test_gid():
177172
gdic[gid] = obj
178173
obj.set_gid(gid)
179174

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()
185178

186179
def include(gid, obj):
187180
# we need to exclude certain objects which will not appear in the svg
@@ -270,11 +263,9 @@ def test_svg_default_metadata(monkeypatch):
270263
monkeypatch.setenv('SOURCE_DATE_EPOCH', '19680801')
271264

272265
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()
278269

279270
# Creator
280271
assert mpl.__version__ in buf
@@ -299,11 +290,9 @@ def test_svg_metadata():
299290
}
300291

301292
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()
307296

308297
# Check things that are easy, single or multi-value entries.
309298
for k in ['Description', *single_value]:

0 commit comments

Comments
 (0)