diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2015-12-24 21:04:13 -0700 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2015-12-26 16:04:12 -0700 |
commit | 555787a5b6a0ec4e27ce05a2c96d97b2aa48cef7 (patch) | |
tree | f1b77809cb499f255f1d6a67f5a40d46de0c317c /numpy/f2py/tests | |
parent | 76f2870e2f9c821b704ead165e13e173282f0aa9 (diff) | |
download | numpy-555787a5b6a0ec4e27ce05a2c96d97b2aa48cef7.tar.gz |
MAINT: Simplify some tests using temppath context manager.
This replaces code of the pattern
```
fd, name = tempfile.mkstemp(...)
os.close(fd)
try:
do stuff with name
finally:
os.remove(name)
```
with
```
with temppath() as name:
do stuff with name
```
A few more complicated cases are also handled. The remains some
particularly gnarly code the could probably be refactored to use
temppath, but that is a more demanding project.
Diffstat (limited to 'numpy/f2py/tests')
-rw-r--r-- | numpy/f2py/tests/util.py | 32 |
1 files changed, 12 insertions, 20 deletions
diff --git a/numpy/f2py/tests/util.py b/numpy/f2py/tests/util.py index 8d06d9680..0c9e91568 100644 --- a/numpy/f2py/tests/util.py +++ b/numpy/f2py/tests/util.py @@ -19,7 +19,7 @@ import random from numpy.compat import asbytes, asstr import numpy.f2py -from numpy.testing import SkipTest +from numpy.testing import SkipTest, temppath try: from hashlib import md5 @@ -159,16 +159,11 @@ def build_code(source_code, options=[], skip=[], only=[], suffix=None, """ if suffix is None: suffix = '.f' - - fd, tmp_fn = tempfile.mkstemp(suffix=suffix) - os.write(fd, asbytes(source_code)) - os.close(fd) - - try: - return build_module([tmp_fn], options=options, skip=skip, only=only, + with temppath(suffix=suffix) as path: + with open(path, 'w') as f: + f.write(source_code) + return build_module([path], options=options, skip=skip, only=only, module_name=module_name) - finally: - os.unlink(tmp_fn) # # Check if compilers are available at all... @@ -209,22 +204,19 @@ sys.exit(99) """ code = code % dict(syspath=repr(sys.path)) - fd, script = tempfile.mkstemp(suffix='.py') - os.write(fd, asbytes(code)) - os.close(fd) + with temppath(suffix='.py') as script: + with open(script, 'w') as f: + f.write(code) - try: cmd = [sys.executable, script, 'config'] p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) out, err = p.communicate() - m = re.search(asbytes(r'COMPILERS:(\d+),(\d+),(\d+)'), out) - if m: - _compiler_status = (bool(int(m.group(1))), bool(int(m.group(2))), - bool(int(m.group(3)))) - finally: - os.unlink(script) + m = re.search(asbytes(r'COMPILERS:(\d+),(\d+),(\d+)'), out) + if m: + _compiler_status = (bool(int(m.group(1))), bool(int(m.group(2))), + bool(int(m.group(3)))) # Finished return _compiler_status |