Skip to content

Commit d0342aa

Browse files
committed
Merge pull request #5359 from jtratner/always-cleanup-graphics-files
TST: Make sure files are removed in test_graphics
2 parents 71e2f5d + c67fbb8 commit d0342aa

File tree

4 files changed

+35
-12
lines changed

4 files changed

+35
-12
lines changed

doc/source/release.rst

+2
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,8 @@ Bug Fixes
753753
- Fix return value/type signature of ``initObjToJSON()`` to be compatible
754754
with numpy's ``import_array()`` (:issue:`5334`, :issue:`5326`)
755755
- Bug when renaming then set_index on a DataFrame (:issue:`5344`)
756+
- Test suite no longer leaves around temporary files when testing graphics. (:issue:`5347`)
757+
(thanks for catching this @yarikoptic!)
756758

757759
pandas 0.12.0
758760
-------------

pandas/tests/test_graphics.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1163,7 +1163,7 @@ def _check_plot_works(f, *args, **kwargs):
11631163
else:
11641164
assert_is_valid_plot_return_object(ret)
11651165

1166-
with ensure_clean() as path:
1166+
with ensure_clean(return_filelike=True) as path:
11671167
plt.savefig(path)
11681168
finally:
11691169
tm.close(fig)

pandas/tseries/tests/test_plotting.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,7 @@ def _check_plot_works(f, freq=None, series=None, *args, **kwargs):
943943
except Exception:
944944
pass
945945

946-
with ensure_clean() as path:
946+
with ensure_clean(return_filelike=True) as path:
947947
plt.savefig(path)
948948
finally:
949949
plt.close(fig)

pandas/util/testing.py

+31-10
Original file line numberDiff line numberDiff line change
@@ -306,18 +306,39 @@ def set_trace():
306306

307307

308308
@contextmanager
309-
def ensure_clean(filename=None):
310-
# if we are not passed a filename, generate a temporary
311-
if filename is None:
312-
filename = tempfile.mkstemp()[1]
309+
def ensure_clean(filename=None, return_filelike=False):
310+
"""Gets a temporary path and agrees to remove on close.
313311
314-
try:
315-
yield filename
316-
finally:
312+
Parameters
313+
----------
314+
filename : str (optional)
315+
if None, creates a temporary file which is then removed when out of
316+
scope.
317+
return_filelike: bool (default False)
318+
if True, returns a file-like which is *always* cleaned. Necessary for
319+
savefig and other functions which want to append extensions. Ignores
320+
filename if True.
321+
"""
322+
323+
if return_filelike:
324+
f = tempfile.TemporaryFile()
317325
try:
318-
os.remove(filename)
319-
except:
320-
pass
326+
yield f
327+
finally:
328+
f.close()
329+
330+
else:
331+
# if we are not passed a filename, generate a temporary
332+
if filename is None:
333+
filename = tempfile.mkstemp()[1]
334+
335+
try:
336+
yield filename
337+
finally:
338+
try:
339+
os.remove(filename)
340+
except Exception as e:
341+
print(e)
321342

322343

323344
def get_data_path(f=''):

0 commit comments

Comments
 (0)