Skip to content

Commit

Permalink
MAINT: remove redundant open() modes and io.open() alias
Browse files Browse the repository at this point in the history
  • Loading branch information
mwtoews committed Oct 29, 2022
1 parent a8ebbd5 commit 080cf82
Show file tree
Hide file tree
Showing 24 changed files with 58 additions and 59 deletions.
6 changes: 3 additions & 3 deletions doc/postprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"""
Post-processes HTML and Latex files output by Sphinx.
"""
import io


def main():
import argparse
Expand All @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions doc/preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ 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))

for dpath, _, files in os.walk(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
Expand Down
4 changes: 2 additions & 2 deletions doc/source/reference/simd/gen_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")),
Expand Down
2 changes: 1 addition & 1 deletion numpy/core/code_generators/genapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion numpy/core/tests/test_cpu_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 13 additions & 13 deletions numpy/core/tests/test_longdouble.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -153,31 +153,31 @@ 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=",")
assert_equal(res, np.array([1., 2., 3., 4.]))

# 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=",")
assert_equal(res, np.array([1.j, -2.j, 3.j, 40.j]))

# 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=",")
assert_equal(res, np.array([1. + 1.j, 2. - 2.j, - 3. + 3.j, - 40. + 4j]))

# 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):
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion numpy/distutils/ccompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions numpy/distutils/command/build_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions numpy/distutils/command/build_src.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion numpy/distutils/command/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion numpy/distutils/fcompiler/ibm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<path>.*)/crt1.o').match
for line in fi:
m = crt1_match(line)
Expand Down
4 changes: 2 additions & 2 deletions numpy/distutils/misc_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions numpy/distutils/system_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions numpy/distutils/tests/test_system_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,15 +271,15 @@ 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)
info = mkl_info()
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)
Expand Down
2 changes: 1 addition & 1 deletion numpy/f2py/capi_maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
2 changes: 1 addition & 1 deletion numpy/linalg/lapack_lite/clapack_scrub.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions numpy/linalg/lapack_lite/make_lite.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -296,15 +296,15 @@ 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:
symbols.add(m.group(2).lower())

# 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:
Expand Down
6 changes: 3 additions & 3 deletions numpy/testing/_private/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 080cf82

Please sign in to comment.