diff options
author | Eric Wieser <wieser.eric@gmail.com> | 2019-02-28 00:45:55 -0800 |
---|---|---|
committer | Eric Wieser <wieser.eric@gmail.com> | 2019-03-01 20:36:10 -0800 |
commit | 73e6067c1ab5083a16150bb70aa7698a958dcd7b (patch) | |
tree | 7edc523a692ad0067f44adf6d830d8ebc1898e1c /numpy/distutils/misc_util.py | |
parent | 2f2dfa19839d69a20713b2fe05ca1ca35f6454a7 (diff) | |
download | numpy-73e6067c1ab5083a16150bb70aa7698a958dcd7b.tar.gz |
MAINT: Use with statements for opening files in distutils
Not super important, but saves some lines. Only the low-hanging ones here.
Found while tracking down an unrelated ResourceWarning
Diffstat (limited to 'numpy/distutils/misc_util.py')
-rw-r--r-- | numpy/distutils/misc_util.py | 73 |
1 files changed, 33 insertions, 40 deletions
diff --git a/numpy/distutils/misc_util.py b/numpy/distutils/misc_util.py index 5df4cced3..cba84bffa 100644 --- a/numpy/distutils/misc_util.py +++ b/numpy/distutils/misc_util.py @@ -218,15 +218,14 @@ def get_mathlibs(path=None): raise DistutilsError('_numpyconfig.h not found in numpy include ' 'dirs %r' % (dirs,)) - fid = open(config_file) - mathlibs = [] - s = '#define MATHLIB' - for line in fid: - if line.startswith(s): - value = line[len(s):].strip() - if value: - mathlibs.extend(value.split(',')) - fid.close() + with open(config_file) as fid: + mathlibs = [] + s = '#define MATHLIB' + for line in fid: + if line.startswith(s): + value = line[len(s):].strip() + if value: + mathlibs.extend(value.split(',')) return mathlibs def minrelpath(path): @@ -443,14 +442,13 @@ def _get_f90_modules(source): if not f90_ext_match(source): return [] modules = [] - f = open(source, 'r') - for line in f: - m = f90_module_name_match(line) - if m: - name = m.group('name') - modules.append(name) - # break # XXX can we assume that there is one module per file? - f.close() + with open(source, 'r') as f: + for line in f: + m = f90_module_name_match(line) + if m: + name = m.group('name') + modules.append(name) + # break # XXX can we assume that there is one module per file? return modules def is_string(s): @@ -1848,9 +1846,8 @@ class Configuration(object): else: entries = njoin(path, '.svn', 'entries') if os.path.isfile(entries): - f = open(entries) - fstr = f.read() - f.close() + with open(entries) as f: + fstr = f.read() if fstr[:5] == '<?xml': # pre 1.4 m = re.search(r'revision="(?P<revision>\d+)"', fstr) if m: @@ -1879,9 +1876,8 @@ class Configuration(object): if os.path.isfile(branch_fn): branch0 = None - f = open(branch_fn) - revision0 = f.read().strip() - f.close() + with open(branch_fn) as f: + revision0 = f.read().strip() branch_map = {} for line in file(branch_cache_fn, 'r'): @@ -1994,9 +1990,8 @@ class Configuration(object): if not os.path.isfile(target): version = str(revision) self.info('Creating %s (version=%r)' % (target, version)) - f = open(target, 'w') - f.write('version = %r\n' % (version)) - f.close() + with open(target, 'w') as f: + f.write('version = %r\n' % (version)) def rm_file(f=target,p=self.info): if delete: @@ -2035,9 +2030,8 @@ class Configuration(object): if not os.path.isfile(target): version = str(revision) self.info('Creating %s (version=%r)' % (target, version)) - f = open(target, 'w') - f.write('version = %r\n' % (version)) - f.close() + with open(target, 'w') as f: + f.write('version = %r\n' % (version)) def rm_file(f=target,p=self.info): if delete: @@ -2273,13 +2267,13 @@ def generate_config_py(target): from numpy.distutils.system_info import system_info from distutils.dir_util import mkpath mkpath(os.path.dirname(target)) - f = open(target, 'w') - f.write('# This file is generated by numpy\'s %s\n' % (os.path.basename(sys.argv[0]))) - f.write('# It contains system_info results at the time of building this package.\n') - f.write('__all__ = ["get_info","show"]\n\n') + with open(target, 'w') as f: + f.write('# This file is generated by numpy\'s %s\n' % (os.path.basename(sys.argv[0]))) + f.write('# It contains system_info results at the time of building this package.\n') + f.write('__all__ = ["get_info","show"]\n\n') - # For gfortran+msvc combination, extra shared libraries may exist - f.write(""" + # For gfortran+msvc combination, extra shared libraries may exist + f.write(""" import os import sys @@ -2292,9 +2286,9 @@ if sys.platform == 'win32' and os.path.isdir(extra_dll_dir): """) - for k, i in system_info.saved_results.items(): - f.write('%s=%r\n' % (k, i)) - f.write(r''' + for k, i in system_info.saved_results.items(): + f.write('%s=%r\n' % (k, i)) + f.write(r''' def get_info(name): g = globals() return g.get(name, g.get(name + "_info", {})) @@ -2310,9 +2304,8 @@ def show(): if k == "sources" and len(v) > 200: v = v[:60] + " ...\n... " + v[-60:] print(" %s = %s" % (k,v)) - ''') + ''') - f.close() return target def msvc_version(compiler): |