From 080cf82ebc5858ec47eff0d49bdf48b74f955274 Mon Sep 17 00:00:00 2001 From: Mike Taves Date: Fri, 28 Oct 2022 22:37:29 +1300 Subject: [PATCH] MAINT: remove redundant open() modes and io.open() alias --- doc/postprocess.py | 6 +++--- doc/preprocess.py | 4 ++-- doc/source/reference/simd/gen_features.py | 4 ++-- numpy/core/code_generators/genapi.py | 2 +- numpy/core/tests/test_cpu_features.py | 2 +- numpy/core/tests/test_longdouble.py | 26 +++++++++++------------ numpy/distutils/ccompiler.py | 2 +- numpy/distutils/command/build_ext.py | 4 ++-- numpy/distutils/command/build_src.py | 4 ++-- numpy/distutils/command/install.py | 2 +- numpy/distutils/fcompiler/ibm.py | 2 +- numpy/distutils/misc_util.py | 4 ++-- numpy/distutils/system_info.py | 8 +++---- numpy/distutils/tests/test_system_info.py | 4 ++-- numpy/f2py/capi_maps.py | 2 +- numpy/linalg/lapack_lite/clapack_scrub.py | 2 +- numpy/linalg/lapack_lite/make_lite.py | 6 +++--- numpy/testing/_private/utils.py | 6 +++--- numpy/typing/tests/test_typing.py | 2 +- runtests.py | 6 +++--- setup.py | 4 ++-- tools/cythonize.py | 10 ++++----- tools/openblas_support.py | 2 +- tools/wheels/check_license.py | 3 +-- 24 files changed, 58 insertions(+), 59 deletions(-) diff --git a/doc/postprocess.py b/doc/postprocess.py index 3e066d22eb9e..4b48fa443149 100755 --- a/doc/postprocess.py +++ b/doc/postprocess.py @@ -2,7 +2,7 @@ """ Post-processes HTML and Latex files output by Sphinx. """ -import io + def main(): import argparse @@ -15,13 +15,13 @@ def main(): mode = args.mode for fn in args.file: - with io.open(fn, 'r', encoding="utf-8") as f: + with open(fn, encoding="utf-8") as f: if mode == 'html': lines = process_html(fn, f.readlines()) elif mode == 'tex': lines = process_tex(f.readlines()) - with io.open(fn, 'w', encoding="utf-8") as f: + with open(fn, 'w', encoding="utf-8") as f: f.write("".join(lines)) def process_html(fn, lines): diff --git a/doc/preprocess.py b/doc/preprocess.py index 870d3e12301c..83980bb2fed5 100755 --- a/doc/preprocess.py +++ b/doc/preprocess.py @@ -32,7 +32,7 @@ def doxy_config(root_path): confs = [] dsrc_path = os.path.join(root_path, "doc", "source") sub = dict(ROOT_DIR=root_path) - with open(os.path.join(dsrc_path, "doxyfile"), "r") as fd: + with open(os.path.join(dsrc_path, "doxyfile")) as fd: conf = DoxyTpl(fd.read()) confs.append(conf.substitute(CUR_DIR=dsrc_path, **sub)) @@ -40,7 +40,7 @@ def doxy_config(root_path): if ".doxyfile" not in files: continue conf_path = os.path.join(dpath, ".doxyfile") - with open(conf_path, "r") as fd: + with open(conf_path) as fd: conf = DoxyTpl(fd.read()) confs.append(conf.substitute(CUR_DIR=dpath, **sub)) return confs diff --git a/doc/source/reference/simd/gen_features.py b/doc/source/reference/simd/gen_features.py index 9a38ef5c9ba2..b141e23d0dd7 100644 --- a/doc/source/reference/simd/gen_features.py +++ b/doc/source/reference/simd/gen_features.py @@ -168,7 +168,7 @@ def wrapper_tab(title, table, tab_size=4): gen_path = path.join( path.dirname(path.realpath(__file__)), "generated_tables" ) - with open(path.join(gen_path, 'cpu_features.inc'), 'wt') as fd: + with open(path.join(gen_path, 'cpu_features.inc'), 'w') as fd: fd.write(f'.. generated via {__file__}\n\n') for arch in ( ("x86", "PPC64", "PPC64LE", "ARMHF", "AARCH64", "S390X") @@ -177,7 +177,7 @@ def wrapper_tab(title, table, tab_size=4): table = Features(arch, 'gcc').table() fd.write(wrapper_section(title, table)) - with open(path.join(gen_path, 'compilers-diff.inc'), 'wt') as fd: + with open(path.join(gen_path, 'compilers-diff.inc'), 'w') as fd: fd.write(f'.. generated via {__file__}\n\n') for arch, cc_names in ( ("x86", ("clang", "ICC", "MSVC")), diff --git a/numpy/core/code_generators/genapi.py b/numpy/core/code_generators/genapi.py index 68ae30d5bef8..e25180c4b2f6 100644 --- a/numpy/core/code_generators/genapi.py +++ b/numpy/core/code_generators/genapi.py @@ -498,7 +498,7 @@ def get_versions_hash(): d = [] file = os.path.join(os.path.dirname(__file__), 'cversions.txt') - with open(file, 'r') as fid: + with open(file) as fid: for line in fid: m = VERRE.match(line) if m: diff --git a/numpy/core/tests/test_cpu_features.py b/numpy/core/tests/test_cpu_features.py index 1a76897e206c..44a0a093ad56 100644 --- a/numpy/core/tests/test_cpu_features.py +++ b/numpy/core/tests/test_cpu_features.py @@ -8,7 +8,7 @@ def assert_features_equal(actual, desired, fname): return detected = str(__cpu_features__).replace("'", "") try: - with open("/proc/cpuinfo", "r") as fd: + with open("/proc/cpuinfo") as fd: cpuinfo = fd.read(2048) except Exception as err: cpuinfo = str(err) diff --git a/numpy/core/tests/test_longdouble.py b/numpy/core/tests/test_longdouble.py index 1a54e62d8b3d..5b76b975bb93 100644 --- a/numpy/core/tests/test_longdouble.py +++ b/numpy/core/tests/test_longdouble.py @@ -142,7 +142,7 @@ class TestFileBased: def test_fromfile_bogus(self): with temppath() as path: - with open(path, 'wt') as f: + with open(path, 'w') as f: f.write("1. 2. 3. flop 4.\n") with assert_warns(DeprecationWarning): @@ -153,7 +153,7 @@ def test_fromfile_complex(self): for ctype in ["complex", "cdouble", "cfloat"]: # Check spacing between separator and only real component specified with temppath() as path: - with open(path, 'wt') as f: + with open(path, 'w') as f: f.write("1, 2 , 3 ,4\n") res = np.fromfile(path, dtype=ctype, sep=",") @@ -161,7 +161,7 @@ def test_fromfile_complex(self): # Real component not specified with temppath() as path: - with open(path, 'wt') as f: + with open(path, 'w') as f: f.write("1j, -2j, 3j, 4e1j\n") res = np.fromfile(path, dtype=ctype, sep=",") @@ -169,7 +169,7 @@ def test_fromfile_complex(self): # Both components specified with temppath() as path: - with open(path, 'wt') as f: + with open(path, 'w') as f: f.write("1+1j,2-2j, -3+3j, -4e1+4j\n") res = np.fromfile(path, dtype=ctype, sep=",") @@ -177,7 +177,7 @@ def test_fromfile_complex(self): # Spaces at wrong places with temppath() as path: - with open(path, 'wt') as f: + with open(path, 'w') as f: f.write("1+2 j,3\n") with assert_warns(DeprecationWarning): @@ -186,7 +186,7 @@ def test_fromfile_complex(self): # Spaces at wrong places with temppath() as path: - with open(path, 'wt') as f: + with open(path, 'w') as f: f.write("1+ 2j,3\n") with assert_warns(DeprecationWarning): @@ -195,7 +195,7 @@ def test_fromfile_complex(self): # Spaces at wrong places with temppath() as path: - with open(path, 'wt') as f: + with open(path, 'w') as f: f.write("1 +2j,3\n") with assert_warns(DeprecationWarning): @@ -204,7 +204,7 @@ def test_fromfile_complex(self): # Spaces at wrong places with temppath() as path: - with open(path, 'wt') as f: + with open(path, 'w') as f: f.write("1+j\n") with assert_warns(DeprecationWarning): @@ -213,7 +213,7 @@ def test_fromfile_complex(self): # Spaces at wrong places with temppath() as path: - with open(path, 'wt') as f: + with open(path, 'w') as f: f.write("1+\n") with assert_warns(DeprecationWarning): @@ -222,7 +222,7 @@ def test_fromfile_complex(self): # Spaces at wrong places with temppath() as path: - with open(path, 'wt') as f: + with open(path, 'w') as f: f.write("1j+1\n") with assert_warns(DeprecationWarning): @@ -235,7 +235,7 @@ def test_fromfile_complex(self): reason="Need strtold_l") def test_fromfile(self): with temppath() as path: - with open(path, 'wt') as f: + with open(path, 'w') as f: f.write(self.out) res = np.fromfile(path, dtype=np.longdouble, sep="\n") assert_equal(res, self.tgt) @@ -244,7 +244,7 @@ def test_fromfile(self): reason="Need strtold_l") def test_genfromtxt(self): with temppath() as path: - with open(path, 'wt') as f: + with open(path, 'w') as f: f.write(self.out) res = np.genfromtxt(path, dtype=np.longdouble) assert_equal(res, self.tgt) @@ -253,7 +253,7 @@ def test_genfromtxt(self): reason="Need strtold_l") def test_loadtxt(self): with temppath() as path: - with open(path, 'wt') as f: + with open(path, 'w') as f: f.write(self.out) res = np.loadtxt(path, dtype=np.longdouble) assert_equal(res, self.tgt) diff --git a/numpy/distutils/ccompiler.py b/numpy/distutils/ccompiler.py index f0487cb64fdb..8e71266ee84a 100644 --- a/numpy/distutils/ccompiler.py +++ b/numpy/distutils/ccompiler.py @@ -57,7 +57,7 @@ def _needs_build(obj, cc_args, extra_postargs, pp_opts): # the last line contains the compiler commandline arguments as some # projects may compile an extension multiple times with different # arguments - with open(dep_file, "r") as f: + with open(dep_file) as f: lines = f.readlines() cmdline =_commandline_dep_string(cc_args, extra_postargs, pp_opts) diff --git a/numpy/distutils/command/build_ext.py b/numpy/distutils/command/build_ext.py index 8b568c1596de..8ab1c2684a22 100644 --- a/numpy/distutils/command/build_ext.py +++ b/numpy/distutils/command/build_ext.py @@ -634,12 +634,12 @@ def _process_unlinkable_fobjects(self, objects, libraries, if os.path.isfile(fake_lib): # Replace fake static library libraries.remove(lib) - with open(fake_lib, 'r') as f: + with open(fake_lib) as f: unlinkable_fobjects.extend(f.read().splitlines()) # Expand C objects c_lib = os.path.join(libdir, lib + '.cobjects') - with open(c_lib, 'r') as f: + with open(c_lib) as f: objects.extend(f.read().splitlines()) # Wrap unlinkable objects to a linkable one diff --git a/numpy/distutils/command/build_src.py b/numpy/distutils/command/build_src.py index 5581011f6f22..bf3d03c70e44 100644 --- a/numpy/distutils/command/build_src.py +++ b/numpy/distutils/command/build_src.py @@ -725,7 +725,7 @@ def swig_sources(self, sources, extension): _has_cpp_header = re.compile(r'-\*-\s*c\+\+\s*-\*-', re.I).search def get_swig_target(source): - with open(source, 'r') as f: + with open(source) as f: result = None line = f.readline() if _has_cpp_header(line): @@ -735,7 +735,7 @@ def get_swig_target(source): return result def get_swig_modulename(source): - with open(source, 'r') as f: + with open(source) as f: name = None for line in f: m = _swig_module_name_match(line) diff --git a/numpy/distutils/command/install.py b/numpy/distutils/command/install.py index 2eff2d145047..efa9b4740fc4 100644 --- a/numpy/distutils/command/install.py +++ b/numpy/distutils/command/install.py @@ -62,7 +62,7 @@ def run(self): # bdist_rpm fails when INSTALLED_FILES contains # paths with spaces. Such paths must be enclosed # with double-quotes. - with open(self.record, 'r') as f: + with open(self.record) as f: lines = [] need_rewrite = False for l in f: diff --git a/numpy/distutils/fcompiler/ibm.py b/numpy/distutils/fcompiler/ibm.py index eff24401a1c3..29927518c703 100644 --- a/numpy/distutils/fcompiler/ibm.py +++ b/numpy/distutils/fcompiler/ibm.py @@ -76,7 +76,7 @@ def get_flags_linker_so(self): xlf_cfg = '/etc/opt/ibmcmp/xlf/%s/xlf.cfg' % version fo, new_cfg = make_temp_file(suffix='_xlf.cfg') log.info('Creating '+new_cfg) - with open(xlf_cfg, 'r') as fi: + with open(xlf_cfg) as fi: crt1_match = re.compile(r'\s*crt\s*=\s*(?P.*)/crt1.o').match for line in fi: m = crt1_match(line) diff --git a/numpy/distutils/misc_util.py b/numpy/distutils/misc_util.py index 79ba08515452..e226b4744815 100644 --- a/numpy/distutils/misc_util.py +++ b/numpy/distutils/misc_util.py @@ -475,7 +475,7 @@ def _get_f90_modules(source): if not f90_ext_match(source): return [] modules = [] - with open(source, 'r') as f: + with open(source) as f: for line in f: m = f90_module_name_match(line) if m: @@ -1932,7 +1932,7 @@ def _get_hg_revision(self, path): revision0 = f.read().strip() branch_map = {} - with open(branch_cache_fn, 'r') as f: + with open(branch_cache_fn) as f: for line in f: branch1, revision1 = line.split()[:2] if revision1==revision0: diff --git a/numpy/distutils/system_info.py b/numpy/distutils/system_info.py index d5a1687da322..5a87c080d347 100644 --- a/numpy/distutils/system_info.py +++ b/numpy/distutils/system_info.py @@ -1260,7 +1260,7 @@ def get_mkl_rootdir(self): paths = os.environ.get('LD_LIBRARY_PATH', '').split(os.pathsep) ld_so_conf = '/etc/ld.so.conf' if os.path.isfile(ld_so_conf): - with open(ld_so_conf, 'r') as f: + with open(ld_so_conf) as f: for d in f: d = d.strip() if d: @@ -2190,7 +2190,7 @@ def get_cblas_libs(self, info): }""") src = os.path.join(tmpdir, 'source.c') try: - with open(src, 'wt') as f: + with open(src, 'w') as f: f.write(s) try: @@ -2345,7 +2345,7 @@ def check_symbols(self, info): except Exception: extra_args = [] try: - with open(src, 'wt') as f: + with open(src, 'w') as f: f.write(s) obj = c.compile([src], output_dir=tmpdir) try: @@ -2456,7 +2456,7 @@ def check_embedded_lapack(self, info): # Add the additional "extra" arguments extra_args = info.get('extra_link_args', []) try: - with open(src, 'wt') as f: + with open(src, 'w') as f: f.write(s) obj = c.compile([src], output_dir=tmpdir) try: diff --git a/numpy/distutils/tests/test_system_info.py b/numpy/distutils/tests/test_system_info.py index eb7235e04620..66304a5e50fe 100644 --- a/numpy/distutils/tests/test_system_info.py +++ b/numpy/distutils/tests/test_system_info.py @@ -271,7 +271,7 @@ def test_overrides(self): # But if we copy the values to a '[mkl]' section the value # is correct - with open(cfg, 'r') as fid: + with open(cfg) as fid: mkl = fid.read().replace('[ALL]', '[mkl]', 1) with open(cfg, 'w') as fid: fid.write(mkl) @@ -279,7 +279,7 @@ def test_overrides(self): assert info.get_lib_dirs() == lib_dirs # Also, the values will be taken from a section named '[DEFAULT]' - with open(cfg, 'r') as fid: + with open(cfg) as fid: dflt = fid.read().replace('[mkl]', '[DEFAULT]', 1) with open(cfg, 'w') as fid: fid.write(dflt) diff --git a/numpy/f2py/capi_maps.py b/numpy/f2py/capi_maps.py index f07066a09b67..98026331b4af 100644 --- a/numpy/f2py/capi_maps.py +++ b/numpy/f2py/capi_maps.py @@ -196,7 +196,7 @@ def load_f2cmap_file(f2cmap_file): # they use PARAMETERS in type specifications. try: outmess('Reading f2cmap from {!r} ...\n'.format(f2cmap_file)) - with open(f2cmap_file, 'r') as f: + with open(f2cmap_file) as f: d = eval(f.read().lower(), {}, {}) for k, d1 in d.items(): for k1 in d1.keys(): diff --git a/numpy/linalg/lapack_lite/clapack_scrub.py b/numpy/linalg/lapack_lite/clapack_scrub.py index fffd70910481..d4da9070d260 100644 --- a/numpy/linalg/lapack_lite/clapack_scrub.py +++ b/numpy/linalg/lapack_lite/clapack_scrub.py @@ -297,7 +297,7 @@ def scrubSource(source, nsteps=None, verbose=False): if __name__ == '__main__': filename = sys.argv[1] outfilename = os.path.join(sys.argv[2], os.path.basename(filename)) - with open(filename, 'r') as fo: + with open(filename) as fo: source = fo.read() if len(sys.argv) > 3: diff --git a/numpy/linalg/lapack_lite/make_lite.py b/numpy/linalg/lapack_lite/make_lite.py index ca8d4c62cc57..20b212792eb1 100755 --- a/numpy/linalg/lapack_lite/make_lite.py +++ b/numpy/linalg/lapack_lite/make_lite.py @@ -253,7 +253,7 @@ def dumpRoutineNames(library, output_dir): def concatenateRoutines(routines, output_file): with open(output_file, 'w') as output_fo: for r in routines: - with open(r.filename, 'r') as fo: + with open(r.filename) as fo: source = fo.read() output_fo.write(source) @@ -296,7 +296,7 @@ def create_name_header(output_dir): if not fn.endswith('.f'): continue - with open(fn, 'r') as f: + with open(fn) as f: for line in f: m = routine_re.match(line) if m: @@ -304,7 +304,7 @@ def create_name_header(output_dir): # f2c symbols f2c_symbols = set() - with open('f2c.h', 'r') as f: + with open('f2c.h') as f: for line in f: m = extern_re.match(line) if m: diff --git a/numpy/testing/_private/utils.py b/numpy/testing/_private/utils.py index 26b2aac4d9db..53930b78cc22 100644 --- a/numpy/testing/_private/utils.py +++ b/numpy/testing/_private/utils.py @@ -197,7 +197,7 @@ def memusage(_proc_pid_stat=f'/proc/{os.getpid()}/stat'): """ try: - with open(_proc_pid_stat, 'r') as f: + with open(_proc_pid_stat) as f: l = f.readline().split(' ') return int(l[22]) except Exception: @@ -224,7 +224,7 @@ def jiffies(_proc_pid_stat=f'/proc/{os.getpid()}/stat', _load_time=[]): if not _load_time: _load_time.append(time.time()) try: - with open(_proc_pid_stat, 'r') as f: + with open(_proc_pid_stat) as f: l = f.readline().split(' ') return int(l[13]) except Exception: @@ -2545,7 +2545,7 @@ def _get_mem_available(): if sys.platform.startswith('linux'): info = {} - with open('/proc/meminfo', 'r') as f: + with open('/proc/meminfo') as f: for line in f: p = line.split() info[p[0].strip(':').lower()] = int(p[1]) * 1024 diff --git a/numpy/typing/tests/test_typing.py b/numpy/typing/tests/test_typing.py index 5011339b56fb..bcaaf5250c9d 100644 --- a/numpy/typing/tests/test_typing.py +++ b/numpy/typing/tests/test_typing.py @@ -431,7 +431,7 @@ def test_extended_precision() -> None: output_mypy = OUTPUT_MYPY assert path in output_mypy - with open(path, "r") as f: + with open(path) as f: expression_list = f.readlines() for _msg in output_mypy[path]: diff --git a/runtests.py b/runtests.py index fea29a10dc49..5bdb6cf22305 100755 --- a/runtests.py +++ b/runtests.py @@ -220,7 +220,7 @@ def main(argv): # Don't use subprocess, since we don't want to include the # current path in PYTHONPATH. sys.argv = extra_argv - with open(extra_argv[0], 'r') as f: + with open(extra_argv[0]) as f: script = f.read() sys.modules['__main__'] = types.ModuleType('__main__') ns = dict(__name__='__main__', @@ -540,7 +540,7 @@ def build_project(args): print("Build OK") else: if not args.show_build_log: - with open(log_filename, 'r') as f: + with open(log_filename) as f: print(f.read()) print("Build failed!") sys.exit(1) @@ -624,7 +624,7 @@ def sdbm_hash(*factors): vars_hash = sdbm_hash(custom_vars, os.path.getmtime(in_config)) try: - with open(out_config, "r") as wfd: + with open(out_config) as wfd: hash_line = wfd.readline().split('hash:') if len(hash_line) > 1 and int(hash_line[1]) == vars_hash: return True diff --git a/setup.py b/setup.py index a1e495832b77..687bbb1f2a72 100755 --- a/setup.py +++ b/setup.py @@ -177,11 +177,11 @@ def __init__(self): def __enter__(self): """Concatenate files and remove LICENSES_bundled.txt""" - with open(self.f1, 'r') as f1: + with open(self.f1) as f1: self.bsd_text = f1.read() with open(self.f1, 'a') as f1: - with open(self.f2, 'r') as f2: + with open(self.f2) as f2: self.bundled_text = f2.read() f1.write('\n\n') f1.write(self.bundled_text) diff --git a/tools/cythonize.py b/tools/cythonize.py index 311a52c915a2..d66ddb98c075 100755 --- a/tools/cythonize.py +++ b/tools/cythonize.py @@ -52,7 +52,7 @@ def process_tempita_pyx(fromfile, tofile): import npy_tempita as tempita assert fromfile.endswith('.pyx.in') - with open(fromfile, "r") as f: + with open(fromfile) as f: tmpl = f.read() pyxcontent = tempita.sub(tmpl) pyxfile = fromfile[:-len('.pyx.in')] + '.pyx' @@ -66,7 +66,7 @@ def process_tempita_pyd(fromfile, tofile): assert fromfile.endswith('.pxd.in') assert tofile.endswith('.pxd') - with open(fromfile, "r") as f: + with open(fromfile) as f: tmpl = f.read() pyxcontent = tempita.sub(tmpl) with open(tofile, "w") as f: @@ -77,7 +77,7 @@ def process_tempita_pxi(fromfile, tofile): assert fromfile.endswith('.pxi.in') assert tofile.endswith('.pxi') - with open(fromfile, "r") as f: + with open(fromfile) as f: tmpl = f.read() pyxcontent = tempita.sub(tmpl) with open(tofile, "w") as f: @@ -88,7 +88,7 @@ def process_tempita_pxd(fromfile, tofile): assert fromfile.endswith('.pxd.in') assert tofile.endswith('.pxd') - with open(fromfile, "r") as f: + with open(fromfile) as f: tmpl = f.read() pyxcontent = tempita.sub(tmpl) with open(tofile, "w") as f: @@ -109,7 +109,7 @@ def load_hashes(filename): # Return { filename : (sha256 of input, sha256 of output) } if os.path.isfile(filename): hashes = {} - with open(filename, 'r') as f: + with open(filename) as f: for line in f: filename, inhash, outhash = line.split() hashes[filename] = (inhash, outhash) diff --git a/tools/openblas_support.py b/tools/openblas_support.py index ce677f9a5f89..081553865802 100644 --- a/tools/openblas_support.py +++ b/tools/openblas_support.py @@ -197,7 +197,7 @@ def make_init(dirname): ''' Create a _distributor_init.py file for OpenBlas ''' - with open(os.path.join(dirname, '_distributor_init.py'), 'wt') as fid: + with open(os.path.join(dirname, '_distributor_init.py'), 'w') as fid: fid.write(textwrap.dedent(""" ''' Helper to preload windows dlls to prevent dll not found errors. diff --git a/tools/wheels/check_license.py b/tools/wheels/check_license.py index 0fe7356c0c4f..8ced317d674c 100644 --- a/tools/wheels/check_license.py +++ b/tools/wheels/check_license.py @@ -9,7 +9,6 @@ """ import os import sys -import io import re import argparse @@ -36,7 +35,7 @@ def main(): # Check license text license_txt = os.path.join(os.path.dirname(mod.__file__), "LICENSE.txt") - with io.open(license_txt, "r", encoding="utf-8") as f: + with open(license_txt, encoding="utf-8") as f: text = f.read() ok = check_text(text)