Skip to content

TST: Always try to close file descriptors of tempfiles #5626

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
Dec 1, 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
6 changes: 5 additions & 1 deletion pandas/io/tests/test_pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -3731,7 +3731,7 @@ def do_copy(f = None, new_f = None, keys = None, propindexes = True, **kwargs):

if new_f is None:
import tempfile
new_f = tempfile.mkstemp()[1]
fd, new_f = tempfile.mkstemp()

tstore = store.copy(new_f, keys = keys, propindexes = propindexes, **kwargs)

Expand All @@ -3757,6 +3757,10 @@ def do_copy(f = None, new_f = None, keys = None, propindexes = True, **kwargs):
finally:
safe_close(store)
safe_close(tstore)
try:
os.close(fd)
except:
pass
safe_remove(new_f)

do_copy()
Expand Down
9 changes: 6 additions & 3 deletions pandas/util/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,17 +345,20 @@ def ensure_clean(filename=None, return_filelike=False):
yield f
finally:
f.close()

else:

# don't generate tempfile if using a path with directory specified
if len(os.path.dirname(filename)):
raise ValueError("Can't pass a qualified name to ensure_clean()")

try:
filename = tempfile.mkstemp(suffix=filename)[1]
fd, filename = tempfile.mkstemp(suffix=filename)
yield filename
finally:
try:
os.close(fd)
except Exception as e:
print("Couldn't close file descriptor: %d (file: %s)" %
(fd, filename))
try:
if os.path.exists(filename):
os.remove(filename)
Expand Down