Skip to content

TST: Make sure files are removed in test_graphics #5359

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 28, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,8 @@ Bug Fixes
- Fix return value/type signature of ``initObjToJSON()`` to be compatible
with numpy's ``import_array()`` (:issue:`5334`, :issue:`5326`)
- Bug when renaming then set_index on a DataFrame (:issue:`5344`)
- Test suite no longer leaves around temporary files when testing graphics. (:issue:`5347`)
(thanks for catching this @yarikoptic!)

pandas 0.12.0
-------------
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/test_graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1163,7 +1163,7 @@ def _check_plot_works(f, *args, **kwargs):
else:
assert_is_valid_plot_return_object(ret)

with ensure_clean() as path:
with ensure_clean(return_filelike=True) as path:
plt.savefig(path)
finally:
tm.close(fig)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tseries/tests/test_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -943,7 +943,7 @@ def _check_plot_works(f, freq=None, series=None, *args, **kwargs):
except Exception:
pass

with ensure_clean() as path:
with ensure_clean(return_filelike=True) as path:
plt.savefig(path)
finally:
plt.close(fig)
Expand Down
41 changes: 31 additions & 10 deletions pandas/util/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,18 +306,39 @@ def set_trace():


@contextmanager
def ensure_clean(filename=None):
# if we are not passed a filename, generate a temporary
if filename is None:
filename = tempfile.mkstemp()[1]
def ensure_clean(filename=None, return_filelike=False):
"""Gets a temporary path and agrees to remove on close.

try:
yield filename
finally:
Parameters
----------
filename : str (optional)
if None, creates a temporary file which is then removed when out of
scope.
return_filelike: bool (default False)
if True, returns a file-like which is *always* cleaned. Necessary for
savefig and other functions which want to append extensions. Ignores
filename if True.
"""

if return_filelike:
f = tempfile.TemporaryFile()
try:
os.remove(filename)
except:
pass
yield f
finally:
f.close()

else:
# if we are not passed a filename, generate a temporary
if filename is None:
filename = tempfile.mkstemp()[1]

try:
yield filename
finally:
try:
os.remove(filename)
except Exception as e:
print(e)


def get_data_path(f=''):
Expand Down