8000 ENH: Tempfile context manager by charris · Pull Request #6866 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: Tempfile context manager #6866

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 2 commits into from
Dec 20, 2015
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
33 changes: 12 additions & 21 deletions numpy/lib/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from numpy.testing import (
TestCase, run_module_suite, assert_warns, assert_,
assert_raises_regex, assert_raises, assert_allclose,
assert_array_equal,
assert_array_equal,temppath
)
from numpy.testing.utils import tempdir

Expand Down Expand Up @@ -259,26 +259,17 @@ def writer(error_list):
def test_not_closing_opened_fid(self):
# Test that issue #2178 is fixed:
# verify could seek on 'loaded' file

fd, tmp = mkstemp(suffix='.npz')
os.close(fd)
try:
fp = open(tmp, 'wb')
np.savez(fp, data='LOVELY LOAD')
fp.close()

fp = open(tmp, 'rb', 10000)
fp.seek(0)
assert_(not fp.closed)
np.load(fp)['data']
# fp must not get closed by .load
assert_(not fp.closed)
fp.seek(0)
assert_(not fp.closed)

finally:
fp.close()
os.remove(tmp)
with temppath(suffix='.npz') as tmp:
with open(tmp, 'wb') as fp:
np.savez(fp, data='LOVELY LOAD')
with open(tmp, 'rb', 10000) as fp:
fp.seek(0)
assert_(not fp.closed)
np.load(fp)['data']
# fp must not get closed by .load
assert_(not fp.closed)
fp.seek(0)
assert_(not fp.closed)

def test_closing_fid(self):
# Test that issue #1517 (too many opened files) remains closed
Expand Down
37 changes: 36 additions & 1 deletion numpy/testing/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import warnings
import sys
import os

import numpy as np
from numpy.testing import (
Expand All @@ -10,7 +11,7 @@
assert_warns, assert_no_warnings, assert_allclose, assert_approx_equal,
assert_array_almost_equal_nulp, assert_array_max_ulp,
clear_and_catch_warnings, run_module_suite,
assert_string_equal
assert_string_equal, assert_, tempdir, temppath,
)
import unittest

Expand Down Expand Up @@ -780,6 +781,40 @@ def test_clear_and_catch_warnings():
assert_warn_len_equal(my_mod, 2)


def test_tempdir():
with tempdir() as tdir:
fpath = os.path.join(tdir, 'tmp')
with open(fpath, 'w'):
pass
assert_(not os.path.isdir(tdir))

raised = False
try:
with tempdir() as tdir:
raise ValueError()
except ValueError:
raised = True
assert_(raised)
assert_(not os.path.isdir(tdir))



def test_temppath():
with temppath() as fpath:
with open(fpath, 'w') as f:
pass
assert_(not os.path.isfile(fpath))

raised = False
try:
with temppath() as fpath:
raise ValueError()
except ValueError:
raised = True
assert_(raised)
assert_(not os.path.isfile(fpath))


class my_cacw(clear_and_catch_warnings):

class_modules = (sys.modules[__name__],)
Expand Down
31 changes: 27 additions & 4 deletions numpy/testing/utils.py
9D54
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from functools import partial
import shutil
import contextlib
from tempfile import mkdtemp
from tempfile import mkdtemp, mkstemp

from .nosetester import import_nose
from numpy.core import float32, empty, arange, array_repr, ndarray
Expand All @@ -30,7 +30,7 @@
'assert_', 'assert_array_almost_equal_nulp', 'assert_raises_regex',
'assert_array_max_ulp', 'assert_warns', 'assert_no_warnings',
'assert_allclose', 'IgnoreException', 'clear_and_catch_warnings',
'SkipTest', 'KnownFailureException']
'SkipTest', 'KnownFailureException', 'temppath', 'tempdir']


class KnownFailureException(Exception):
Expand Down Expand Up @@ -1810,8 +1810,31 @@ def tempdir(*args, **kwargs):

"""
tmpdir = mkdtemp(*args, **kwargs)
yield tmpdir
shutil.rmtree(tmpdir)
try:
yield tmpdir
finally:
shutil.rmtree(tmpdir)

@contextlib.contextmanager
def temppath(*args, **kwargs):
"""Context manager for temporary files.

Context manager that returns the path to a closed temporary file. Its
parameters are the same as for tempfile.mkstemp and are passed directly
to that function. The underlying file is removed when the context is
exited, so it should be closed at that time.

Windows does not allow a temporary file to be opened if it is already
open, so the underlying file must be closed after opening before it
can be opened again.

"""
fd, path = mkstemp(*args, **kwargs)
os.close(fd)
try:
yield path
finally:
os.remove(path)


class clear_and_catch_warnings(warnings.catch_warnings):
Expand Down
0