summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Wieser <wieser.eric@gmail.com>2019-02-28 00:45:55 -0800
committerEric Wieser <wieser.eric@gmail.com>2019-03-01 20:36:10 -0800
commit73e6067c1ab5083a16150bb70aa7698a958dcd7b (patch)
tree7edc523a692ad0067f44adf6d830d8ebc1898e1c
parent2f2dfa19839d69a20713b2fe05ca1ca35f6454a7 (diff)
downloadnumpy-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
-rw-r--r--numpy/distutils/command/build_src.py62
-rw-r--r--numpy/distutils/command/install.py19
-rw-r--r--numpy/distutils/fcompiler/ibm.py17
-rw-r--r--numpy/distutils/line_endings.py10
-rw-r--r--numpy/distutils/misc_util.py73
5 files changed, 80 insertions, 101 deletions
diff --git a/numpy/distutils/command/build_src.py b/numpy/distutils/command/build_src.py
index 668bc23fe..6d5d305d2 100644
--- a/numpy/distutils/command/build_src.py
+++ b/numpy/distutils/command/build_src.py
@@ -28,20 +28,14 @@ def subst_vars(target, source, d):
"""Substitute any occurrence of @foo@ by d['foo'] from source file into
target."""
var = re.compile('@([a-zA-Z_]+)@')
- fs = open(source, 'r')
- try:
- ft = open(target, 'w')
- try:
+ with open(source, 'r') as fs:
+ with open(target, 'w') as ft:
for l in fs:
m = var.search(l)
if m:
ft.write(l.replace('@%s@' % m.group(1), d[m.group(1)]))
else:
ft.write(l)
- finally:
- ft.close()
- finally:
- fs.close()
class build_src(build_ext.build_ext):
@@ -425,9 +419,8 @@ class build_src(build_ext.build_ext):
else:
log.info("conv_template:> %s" % (target_file))
outstr = process_c_file(source)
- fid = open(target_file, 'w')
- fid.write(outstr)
- fid.close()
+ with open(target_file, 'w') as fid:
+ fid.write(outstr)
if _header_ext_match(target_file):
d = os.path.dirname(target_file)
if d not in include_dirs:
@@ -723,25 +716,23 @@ _has_c_header = re.compile(r'-[*]-\s*c\s*-[*]-', re.I).search
_has_cpp_header = re.compile(r'-[*]-\s*c[+][+]\s*-[*]-', re.I).search
def get_swig_target(source):
- f = open(source, 'r')
- result = None
- line = f.readline()
- if _has_cpp_header(line):
- result = 'c++'
- if _has_c_header(line):
- result = 'c'
- f.close()
+ with open(source, 'r') as f:
+ result = None
+ line = f.readline()
+ if _has_cpp_header(line):
+ result = 'c++'
+ if _has_c_header(line):
+ result = 'c'
return result
def get_swig_modulename(source):
- f = open(source, 'r')
- name = None
- for line in f:
- m = _swig_module_name_match(line)
- if m:
- name = m.group('name')
- break
- f.close()
+ with open(source, 'r') as f:
+ name = None
+ for line in f:
+ m = _swig_module_name_match(line)
+ if m:
+ name = m.group('name')
+ break
return name
def _find_swig_target(target_dir, name):
@@ -760,15 +751,14 @@ _f2py_user_module_name_match = re.compile(r'\s*python\s*module\s*(?P<name>[\w_]*
def get_f2py_modulename(source):
name = None
- f = open(source)
- for line in f:
- m = _f2py_module_name_match(line)
- if m:
- if _f2py_user_module_name_match(line): # skip *__user__* names
- continue
- name = m.group('name')
- break
- f.close()
+ with open(source) as f:
+ for line in f:
+ m = _f2py_module_name_match(line)
+ if m:
+ if _f2py_user_module_name_match(line): # skip *__user__* names
+ continue
+ name = m.group('name')
+ break
return name
##########################################
diff --git a/numpy/distutils/command/install.py b/numpy/distutils/command/install.py
index a1dd47755..c74ae9446 100644
--- a/numpy/distutils/command/install.py
+++ b/numpy/distutils/command/install.py
@@ -64,16 +64,15 @@ class install(old_install):
# bdist_rpm fails when INSTALLED_FILES contains
# paths with spaces. Such paths must be enclosed
# with double-quotes.
- f = open(self.record, 'r')
- lines = []
- need_rewrite = False
- for l in f:
- l = l.rstrip()
- if ' ' in l:
- need_rewrite = True
- l = '"%s"' % (l)
- lines.append(l)
- f.close()
+ with open(self.record, 'r') as f:
+ lines = []
+ need_rewrite = False
+ for l in f:
+ l = l.rstrip()
+ if ' ' in l:
+ need_rewrite = True
+ l = '"%s"' % (l)
+ lines.append(l)
if need_rewrite:
self.execute(write_file,
(self.record, lines),
diff --git a/numpy/distutils/fcompiler/ibm.py b/numpy/distutils/fcompiler/ibm.py
index c4cb2fca7..70d2132e1 100644
--- a/numpy/distutils/fcompiler/ibm.py
+++ b/numpy/distutils/fcompiler/ibm.py
@@ -78,15 +78,14 @@ class IBMFCompiler(FCompiler):
xlf_cfg = '/etc/opt/ibmcmp/xlf/%s/xlf.cfg' % version
fo, new_cfg = make_temp_file(suffix='_xlf.cfg')
log.info('Creating '+new_cfg)
- fi = open(xlf_cfg, 'r')
- crt1_match = re.compile(r'\s*crt\s*[=]\s*(?P<path>.*)/crt1.o').match
- for line in fi:
- m = crt1_match(line)
- if m:
- fo.write('crt = %s/bundle1.o\n' % (m.group('path')))
- else:
- fo.write(line)
- fi.close()
+ with open(xlf_cfg, 'r') as fi:
+ crt1_match = re.compile(r'\s*crt\s*[=]\s*(?P<path>.*)/crt1.o').match
+ for line in fi:
+ m = crt1_match(line)
+ if m:
+ fo.write('crt = %s/bundle1.o\n' % (m.group('path')))
+ else:
+ fo.write(line)
fo.close()
opt.append('-F'+new_cfg)
return opt
diff --git a/numpy/distutils/line_endings.py b/numpy/distutils/line_endings.py
index 5ecb104ff..2420798ab 100644
--- a/numpy/distutils/line_endings.py
+++ b/numpy/distutils/line_endings.py
@@ -19,9 +19,8 @@ def dos2unix(file):
newdata = re.sub("\r\n", "\n", data)
if newdata != data:
print('dos2unix:', file)
- f = open(file, "wb")
- f.write(newdata)
- f.close()
+ with open(file, "wb") as f:
+ f.write(newdata)
return file
else:
print(file, 'ok')
@@ -53,9 +52,8 @@ def unix2dos(file):
newdata = re.sub("\n", "\r\n", newdata)
if newdata != data:
print('unix2dos:', file)
- f = open(file, "wb")
- f.write(newdata)
- f.close()
+ with open(file, "wb") as f:
+ f.write(newdata)
return file
else:
print(file, 'ok')
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):