From 38025696d7f268b47d81e215bcf33c50ca1c98c1 Mon Sep 17 00:00:00 2001 From: Bharat123rox Date: Tue, 7 May 2019 23:45:16 +0530 Subject: Use with statement to open/close files to fix LGTM alerts --- numpy/core/code_generators/generate_umath.py | 7 +- numpy/core/numeric.py | 3 +- numpy/distutils/conv_template.py | 32 ++++---- numpy/distutils/from_template.py | 32 ++++---- numpy/distutils/line_endings.py | 52 ++++++------- numpy/f2py/f2py2e.py | 9 +-- numpy/f2py/rules.py | 105 +++++++++++++-------------- numpy/linalg/lapack_lite/clapack_scrub.py | 5 +- numpy/linalg/lapack_lite/fortran.py | 21 +++--- numpy/ma/mrecords.py | 9 +-- 10 files changed, 133 insertions(+), 142 deletions(-) (limited to 'numpy') diff --git a/numpy/core/code_generators/generate_umath.py b/numpy/core/code_generators/generate_umath.py index e17523451..3bd180854 100644 --- a/numpy/core/code_generators/generate_umath.py +++ b/numpy/core/code_generators/generate_umath.py @@ -1154,7 +1154,6 @@ def make_code(funcdict, filename): if __name__ == "__main__": filename = __file__ - fid = open('__umath_generated.c', 'w') - code = make_code(defdict, filename) - fid.write(code) - fid.close() + with open('__umath_generated.c', 'w') as fid: + code = make_code(defdict, filename) + fid.write(code) diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index 34705efc7..c2fc8da16 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -1984,7 +1984,8 @@ def load(file): "np.core.numeric.load is deprecated, use pickle.load instead", DeprecationWarning, stacklevel=2) if isinstance(file, type("")): - file = open(file, "rb") + with open(file, "rb") as file_pointer: + return pickle.load(file_pointer) return pickle.load(file) diff --git a/numpy/distutils/conv_template.py b/numpy/distutils/conv_template.py index b33e315b4..9f3b30957 100644 --- a/numpy/distutils/conv_template.py +++ b/numpy/distutils/conv_template.py @@ -267,22 +267,19 @@ include_src_re = re.compile(r"(\n|\A)#include\s*['\"]" def resolve_includes(source): d = os.path.dirname(source) - fid = open(source) - lines = [] - for line in fid: - m = include_src_re.match(line) - if m: - fn = m.group('name') - if not os.path.isabs(fn): - fn = os.path.join(d, fn) - if os.path.isfile(fn): - print('Including file', fn) - lines.extend(resolve_includes(fn)) - else: - lines.append(line) - else: - lines.append(line) - fid.close() + with open(source) as fid: + lines = [] + for line in fid: + m = include_src_re.match(line) + if m: + fn = m.group('name') + if not os.path.isabs(fn): + fn = os.path.join(d, fn) + if os.path.isfile(fn): + print('Including file', fn) + lines.extend(resolve_includes(fn)) + else: + lines.append(line) return lines def process_file(source): @@ -331,7 +328,10 @@ def main(): except ValueError: e = get_exception() raise ValueError("In %s loop at %s" % (file, e)) + finally: + fid.close() outfile.write(writestr) + outfile.close() if __name__ == "__main__": main() diff --git a/numpy/distutils/from_template.py b/numpy/distutils/from_template.py index 65c60c498..ed84df196 100644 --- a/numpy/distutils/from_template.py +++ b/numpy/distutils/from_template.py @@ -212,22 +212,19 @@ include_src_re = re.compile(r"(\n|\A)\s*include\s*['\"](?P[\w\d./\\]+[.]sr def resolve_includes(source): d = os.path.dirname(source) - fid = open(source) - lines = [] - for line in fid: - m = include_src_re.match(line) - if m: - fn = m.group('name') - if not os.path.isabs(fn): - fn = os.path.join(d, fn) - if os.path.isfile(fn): - print('Including file', fn) - lines.extend(resolve_includes(fn)) - else: - lines.append(line) - else: - lines.append(line) - fid.close() + with open(source) as fid: + lines = [] + for line in fid: + m = include_src_re.match(line) + if m: + fn = m.group('name') + if not os.path.isabs(fn): + fn = os.path.join(d, fn) + if os.path.isfile(fn): + print('Including file', fn) + lines.extend(resolve_includes(fn)) + else: + lines.append(line) return lines def process_file(source): @@ -255,10 +252,11 @@ def main(): (base, ext) = os.path.splitext(file) newname = base outfile = open(newname, 'w') - allstr = fid.read() writestr = process_str(allstr) outfile.write(writestr) + fid.close() + outfile.close() if __name__ == "__main__": main() diff --git a/numpy/distutils/line_endings.py b/numpy/distutils/line_endings.py index 2420798ab..ed3363d39 100644 --- a/numpy/distutils/line_endings.py +++ b/numpy/distutils/line_endings.py @@ -11,19 +11,20 @@ def dos2unix(file): print(file, "Directory!") return - data = open(file, "rb").read() - if '\0' in data: - print(file, "Binary!") - return + with open(file, "rb") as fp: + data = fp.read() + if '\0' in data: + print(file, "Binary!") + return - newdata = re.sub("\r\n", "\n", data) - if newdata != data: - print('dos2unix:', file) - with open(file, "wb") as f: - f.write(newdata) - return file - else: - print(file, 'ok') + newdata = re.sub("\r\n", "\n", data) + if newdata != data: + print('dos2unix:', file) + with open(file, "wb") as f: + f.write(newdata) + return file + else: + print(file, 'ok') def dos2unix_one_dir(modified_files, dir_name, file_names): for file in file_names: @@ -44,19 +45,20 @@ def unix2dos(file): print(file, "Directory!") return - data = open(file, "rb").read() - if '\0' in data: - print(file, "Binary!") - return - newdata = re.sub("\r\n", "\n", data) - newdata = re.sub("\n", "\r\n", newdata) - if newdata != data: - print('unix2dos:', file) - with open(file, "wb") as f: - f.write(newdata) - return file - else: - print(file, 'ok') + with open(file, "rb") as fp: + data = fp.read() + if '\0' in data: + print(file, "Binary!") + return + newdata = re.sub("\r\n", "\n", data) + newdata = re.sub("\n", "\r\n", newdata) + if newdata != data: + print('unix2dos:', file) + with open(file, "wb") as f: + f.write(newdata) + return file + else: + print(file, 'ok') def unix2dos_one_dir(modified_files, dir_name, file_names): for file in file_names: diff --git a/numpy/f2py/f2py2e.py b/numpy/f2py/f2py2e.py index 47223151f..6113b2167 100755 --- a/numpy/f2py/f2py2e.py +++ b/numpy/f2py/f2py2e.py @@ -269,8 +269,8 @@ def scaninputline(inputline): options["f2py_wrapper_output"] = l elif f == 1: try: - open(l).close() - files.append(l) + with open(l) as f: + files.append(l) except IOError as detail: errmess('IOError: %s. Skipping file "%s".\n' % (str(detail), l)) @@ -333,9 +333,8 @@ def callcrackfortran(files, options): if options['signsfile'][-6:] == 'stdout': sys.stdout.write(pyf) else: - f = open(options['signsfile'], 'w') - f.write(pyf) - f.close() + with open(options['signsfile'], 'w') as f: + f.write(pyf) if options["coutput"] is None: for mod in postlist: mod["coutput"] = "%smodule.c" % mod["name"] diff --git a/numpy/f2py/rules.py b/numpy/f2py/rules.py index 23d36b2c2..11f36cc32 100644 --- a/numpy/f2py/rules.py +++ b/numpy/f2py/rules.py @@ -1257,82 +1257,77 @@ def buildmodule(m, um): fn = os.path.join(options['buildpath'], vrd['coutput']) ret['csrc'] = fn - f = open(fn, 'w') - f.write(ar['modulebody'].replace('\t', 2 * ' ')) - f.close() + with open(fn, 'w') as f: + f.write(ar['modulebody'].replace('\t', 2 * ' ')) outmess('\tWrote C/API module "%s" to file "%s"\n' % (m['name'], fn)) if options['dorestdoc']: fn = os.path.join( options['buildpath'], vrd['modulename'] + 'module.rest') - f = open(fn, 'w') - f.write('.. -*- rest -*-\n') - f.write('\n'.join(ar['restdoc'])) - f.close() + with open(fn, 'w') as f: + f.write('.. -*- rest -*-\n') + f.write('\n'.join(ar['restdoc'])) outmess('\tReST Documentation is saved to file "%s/%smodule.rest"\n' % (options['buildpath'], vrd['modulename'])) if options['dolatexdoc']: fn = os.path.join( options['buildpath'], vrd['modulename'] + 'module.tex') ret['ltx'] = fn - f = open(fn, 'w') - f.write( - '%% This file is auto-generated with f2py (version:%s)\n' % (f2py_version)) - if 'shortlatex' not in options: - f.write( - '\\documentclass{article}\n\\usepackage{a4wide}\n\\begin{document}\n\\tableofcontents\n\n') - f.write('\n'.join(ar['latexdoc'])) - if 'shortlatex' not in options: - f.write('\\end{document}') - f.close() + with open(fn, 'w') as f: + f.write( + '%% This file is auto-generated with f2py (version:%s)\n' % (f2py_version)) + if 'shortlatex' not in options: + f.write( + '\\documentclass{article}\n\\usepackage{a4wide}\n\\begin{document}\n\\tableofcontents\n\n') + f.write('\n'.join(ar['latexdoc'])) + if 'shortlatex' not in options: + f.write('\\end{document}') outmess('\tDocumentation is saved to file "%s/%smodule.tex"\n' % (options['buildpath'], vrd['modulename'])) if funcwrappers: wn = os.path.join(options['buildpath'], vrd['f2py_wrapper_output']) ret['fsrc'] = wn - f = open(wn, 'w') - f.write('C -*- fortran -*-\n') - f.write( - 'C This file is autogenerated with f2py (version:%s)\n' % (f2py_version)) - f.write( - 'C It contains Fortran 77 wrappers to fortran functions.\n') - lines = [] - for l in ('\n\n'.join(funcwrappers) + '\n').split('\n'): - if l and l[0] == ' ': - while len(l) >= 66: - lines.append(l[:66] + '\n &') - l = l[66:] - lines.append(l + '\n') - else: - lines.append(l + '\n') - lines = ''.join(lines).replace('\n &\n', '\n') - f.write(lines) - f.close() + with open(wn, 'w') as f: + f.write('C -*- fortran -*-\n') + f.write( + 'C This file is autogenerated with f2py (version:%s)\n' % (f2py_version)) + f.write( + 'C It contains Fortran 77 wrappers to fortran functions.\n') + lines = [] + for l in ('\n\n'.join(funcwrappers) + '\n').split('\n'): + if l and l[0] == ' ': + while len(l) >= 66: + lines.append(l[:66] + '\n &') + l = l[66:] + lines.append(l + '\n') + else: + lines.append(l + '\n') + lines = ''.join(lines).replace('\n &\n', '\n') + f.write(lines) outmess('\tFortran 77 wrappers are saved to "%s"\n' % (wn)) if funcwrappers2: wn = os.path.join( options['buildpath'], '%s-f2pywrappers2.f90' % (vrd['modulename'])) ret['fsrc'] = wn - f = open(wn, 'w') - f.write('! -*- f90 -*-\n') - f.write( - '! This file is autogenerated with f2py (version:%s)\n' % (f2py_version)) - f.write( - '! It contains Fortran 90 wrappers to fortran functions.\n') - lines = [] - for l in ('\n\n'.join(funcwrappers2) + '\n').split('\n'): - if len(l) > 72 and l[0] == ' ': - lines.append(l[:72] + '&\n &') - l = l[72:] - while len(l) > 66: - lines.append(l[:66] + '&\n &') - l = l[66:] - lines.append(l + '\n') - else: - lines.append(l + '\n') - lines = ''.join(lines).replace('\n &\n', '\n') - f.write(lines) - f.close() + with open(wn, 'w') as f: + f.write('! -*- f90 -*-\n') + f.write( + '! This file is autogenerated with f2py (version:%s)\n' % (f2py_version)) + f.write( + '! It contains Fortran 90 wrappers to fortran functions.\n') + lines = [] + for l in ('\n\n'.join(funcwrappers2) + '\n').split('\n'): + if len(l) > 72 and l[0] == ' ': + lines.append(l[:72] + '&\n &') + l = l[72:] + while len(l) > 66: + lines.append(l[:66] + '&\n &') + l = l[66:] + lines.append(l + '\n') + else: + lines.append(l + '\n') + lines = ''.join(lines).replace('\n &\n', '\n') + f.write(lines) outmess('\tFortran 90 wrappers are saved to "%s"\n' % (wn)) return ret diff --git a/numpy/linalg/lapack_lite/clapack_scrub.py b/numpy/linalg/lapack_lite/clapack_scrub.py index e72a39e64..434586113 100644 --- a/numpy/linalg/lapack_lite/clapack_scrub.py +++ b/numpy/linalg/lapack_lite/clapack_scrub.py @@ -294,9 +294,8 @@ 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)) - fo = open(filename, 'r') - source = fo.read() - fo.close() + with open(filename, 'r') as fo: + source = fo.read() if len(sys.argv) > 3: nsteps = int(sys.argv[3]) diff --git a/numpy/linalg/lapack_lite/fortran.py b/numpy/linalg/lapack_lite/fortran.py index 3b6ac70f0..58c15f032 100644 --- a/numpy/linalg/lapack_lite/fortran.py +++ b/numpy/linalg/lapack_lite/fortran.py @@ -110,15 +110,14 @@ def getDependencies(filename): """For a Fortran source file, return a list of routines declared as EXTERNAL in it. """ - fo = open(filename) - external_pat = re.compile(r'^\s*EXTERNAL\s', re.I) - routines = [] - for lineno, line in fortranSourceLines(fo): - m = external_pat.match(line) - if m: - names = line = line[m.end():].strip().split(',') - names = [n.strip().lower() for n in names] - names = [n for n in names if n] - routines.extend(names) - fo.close() + with open(filename) as fo: + external_pat = re.compile(r'^\s*EXTERNAL\s', re.I) + routines = [] + for lineno, line in fortranSourceLines(fo): + m = external_pat.match(line) + if m: + names = line = line[m.end():].strip().split(',') + names = [n.strip().lower() for n in names] + names = [n for n in names if n] + routines.extend(names) return routines diff --git a/numpy/ma/mrecords.py b/numpy/ma/mrecords.py index 9bcb3947d..c7d7f5a36 100644 --- a/numpy/ma/mrecords.py +++ b/numpy/ma/mrecords.py @@ -659,13 +659,12 @@ def openfile(fname): return fname # Try to open the file and guess its type try: - f = open(fname) + with open(fname) as f: + if f.readline()[:2] != "\\x": + f.seek(0, 0) + return f except IOError: raise IOError("No such file: '%s'" % fname) - if f.readline()[:2] != "\\x": - f.seek(0, 0) - return f - f.close() raise NotImplementedError("Wow, binary file") -- cgit v1.2.1 From dcb2ecfb1ac05761a52671bc4179b120da7fe8e9 Mon Sep 17 00:00:00 2001 From: Bharat123rox Date: Wed, 8 May 2019 10:46:32 +0530 Subject: Fixed possible regressions --- numpy/distutils/conv_template.py | 2 ++ numpy/distutils/from_template.py | 2 ++ numpy/f2py/rules.py | 4 ++-- numpy/ma/mrecords.py | 9 +++++---- 4 files changed, 11 insertions(+), 6 deletions(-) (limited to 'numpy') diff --git a/numpy/distutils/conv_template.py b/numpy/distutils/conv_template.py index 9f3b30957..157086f97 100644 --- a/numpy/distutils/conv_template.py +++ b/numpy/distutils/conv_template.py @@ -280,6 +280,8 @@ def resolve_includes(source): lines.extend(resolve_includes(fn)) else: lines.append(line) + else: + lines.append(line) return lines def process_file(source): diff --git a/numpy/distutils/from_template.py b/numpy/distutils/from_template.py index ed84df196..24b61fc02 100644 --- a/numpy/distutils/from_template.py +++ b/numpy/distutils/from_template.py @@ -225,6 +225,8 @@ def resolve_includes(source): lines.extend(resolve_includes(fn)) else: lines.append(line) + else: + lines.append(line) return lines def process_file(source): diff --git a/numpy/f2py/rules.py b/numpy/f2py/rules.py index 11f36cc32..6769f1b1f 100644 --- a/numpy/f2py/rules.py +++ b/numpy/f2py/rules.py @@ -1274,12 +1274,12 @@ def buildmodule(m, um): options['buildpath'], vrd['modulename'] + 'module.tex') ret['ltx'] = fn with open(fn, 'w') as f: - f.write( + f.write( '%% This file is auto-generated with f2py (version:%s)\n' % (f2py_version)) if 'shortlatex' not in options: f.write( '\\documentclass{article}\n\\usepackage{a4wide}\n\\begin{document}\n\\tableofcontents\n\n') - f.write('\n'.join(ar['latexdoc'])) + f.write('\n'.join(ar['latexdoc'])) if 'shortlatex' not in options: f.write('\\end{document}') outmess('\tDocumentation is saved to file "%s/%smodule.tex"\n' % diff --git a/numpy/ma/mrecords.py b/numpy/ma/mrecords.py index c7d7f5a36..9bcb3947d 100644 --- a/numpy/ma/mrecords.py +++ b/numpy/ma/mrecords.py @@ -659,12 +659,13 @@ def openfile(fname): return fname # Try to open the file and guess its type try: - with open(fname) as f: - if f.readline()[:2] != "\\x": - f.seek(0, 0) - return f + f = open(fname) except IOError: raise IOError("No such file: '%s'" % fname) + if f.readline()[:2] != "\\x": + f.seek(0, 0) + return f + f.close() raise NotImplementedError("Wow, binary file") -- cgit v1.2.1 From 802d8e5d52434642713a019cd067af9917239565 Mon Sep 17 00:00:00 2001 From: Bharat123rox Date: Wed, 8 May 2019 11:33:03 +0530 Subject: Add code review suggestions from @eric-wieser --- numpy/distutils/conv_template.py | 14 ++++++------ numpy/distutils/from_template.py | 14 ++++++------ numpy/distutils/line_endings.py | 46 ++++++++++++++++++++-------------------- numpy/f2py/f2py2e.py | 5 +++-- 4 files changed, 41 insertions(+), 38 deletions(-) (limited to 'numpy') diff --git a/numpy/distutils/conv_template.py b/numpy/distutils/conv_template.py index 157086f97..7916f2875 100644 --- a/numpy/distutils/conv_template.py +++ b/numpy/distutils/conv_template.py @@ -316,24 +316,24 @@ def main(): try: file = sys.argv[1] except IndexError: - fid = sys.stdin - outfile = sys.stdout + fid = contextlib_nullcontext(sys.stdin) + outfile = contextlib_nullcontext(sys.stdout) else: fid = open(file, 'r') (base, ext) = os.path.splitext(file) newname = base outfile = open(newname, 'w') - allstr = fid.read() + with fid: + allstr = fid.read() try: writestr = process_str(allstr) except ValueError: e = get_exception() raise ValueError("In %s loop at %s" % (file, e)) - finally: - fid.close() - outfile.write(writestr) - outfile.close() + + with outfile: + outfile.write(writestr) if __name__ == "__main__": main() diff --git a/numpy/distutils/from_template.py b/numpy/distutils/from_template.py index 24b61fc02..419c0e7a8 100644 --- a/numpy/distutils/from_template.py +++ b/numpy/distutils/from_template.py @@ -247,18 +247,20 @@ def main(): try: file = sys.argv[1] except IndexError: - fid = sys.stdin - outfile = sys.stdout + fid = contextlib_nullcontext(sys.stdin) + outfile = contextlib_nullcontext(sys.stdout) else: fid = open(file, 'r') (base, ext) = os.path.splitext(file) newname = base outfile = open(newname, 'w') - allstr = fid.read() + + with fid: + allstr = fid.read() writestr = process_str(allstr) - outfile.write(writestr) - fid.close() - outfile.close() + with outfile: + outfile.write(writestr) + if __name__ == "__main__": main() diff --git a/numpy/distutils/line_endings.py b/numpy/distutils/line_endings.py index ed3363d39..fe8fd1b0f 100644 --- a/numpy/distutils/line_endings.py +++ b/numpy/distutils/line_endings.py @@ -13,18 +13,18 @@ def dos2unix(file): with open(file, "rb") as fp: data = fp.read() - if '\0' in data: - print(file, "Binary!") - return + if '\0' in data: + print(file, "Binary!") + return - newdata = re.sub("\r\n", "\n", data) - if newdata != data: - print('dos2unix:', file) - with open(file, "wb") as f: - f.write(newdata) - return file - else: - print(file, 'ok') + newdata = re.sub("\r\n", "\n", data) + if newdata != data: + print('dos2unix:', file) + with open(file, "wb") as f: + f.write(newdata) + return file + else: + print(file, 'ok') def dos2unix_one_dir(modified_files, dir_name, file_names): for file in file_names: @@ -47,18 +47,18 @@ def unix2dos(file): with open(file, "rb") as fp: data = fp.read() - if '\0' in data: - print(file, "Binary!") - return - newdata = re.sub("\r\n", "\n", data) - newdata = re.sub("\n", "\r\n", newdata) - if newdata != data: - print('unix2dos:', file) - with open(file, "wb") as f: - f.write(newdata) - return file - else: - print(file, 'ok') + if '\0' in data: + print(file, "Binary!") + return + newdata = re.sub("\r\n", "\n", data) + newdata = re.sub("\n", "\r\n", newdata) + if newdata != data: + print('unix2dos:', file) + with open(file, "wb") as f: + f.write(newdata) + return file + else: + print(file, 'ok') def unix2dos_one_dir(modified_files, dir_name, file_names): for file in file_names: diff --git a/numpy/f2py/f2py2e.py b/numpy/f2py/f2py2e.py index 6113b2167..110337f92 100755 --- a/numpy/f2py/f2py2e.py +++ b/numpy/f2py/f2py2e.py @@ -269,8 +269,9 @@ def scaninputline(inputline): options["f2py_wrapper_output"] = l elif f == 1: try: - with open(l) as f: - files.append(l) + with open(l): + pass + files.append(l) except IOError as detail: errmess('IOError: %s. Skipping file "%s".\n' % (str(detail), l)) -- cgit v1.2.1 From fb514fcecddf73ecc20a4796abcc711699508182 Mon Sep 17 00:00:00 2001 From: Bharat123rox Date: Wed, 8 May 2019 12:11:26 +0530 Subject: Add missing imports --- numpy/distutils/conv_template.py | 1 + numpy/distutils/from_template.py | 2 ++ 2 files changed, 3 insertions(+) (limited to 'numpy') diff --git a/numpy/distutils/conv_template.py b/numpy/distutils/conv_template.py index 7916f2875..e8823ed53 100644 --- a/numpy/distutils/conv_template.py +++ b/numpy/distutils/conv_template.py @@ -87,6 +87,7 @@ import os import sys import re +from numpy.compat import contextlib_nullcontext from numpy.distutils.compat import get_exception # names for replacement that are already global. diff --git a/numpy/distutils/from_template.py b/numpy/distutils/from_template.py index 419c0e7a8..026de0093 100644 --- a/numpy/distutils/from_template.py +++ b/numpy/distutils/from_template.py @@ -53,6 +53,8 @@ import os import sys import re +from numpy.compat import contextlib_nullcontext + routine_start_re = re.compile(r'(\n|\A)(( (\$|\*))|)\s*(subroutine|function)\b', re.I) routine_end_re = re.compile(r'\n\s*end\s*(subroutine|function)\b.*(\n|\Z)', re.I) function_start_re = re.compile(r'\n (\$|\*)\s*function\b', re.I) -- cgit v1.2.1 From 3c8a5403f3dee6f0a999670ddd03e4beab11df2f Mon Sep 17 00:00:00 2001 From: Bharat123rox Date: Wed, 8 May 2019 13:35:59 +0530 Subject: Revert contextlib changes --- numpy/distutils/conv_template.py | 11 ++++------- numpy/distutils/from_template.py | 12 ++++-------- 2 files changed, 8 insertions(+), 15 deletions(-) (limited to 'numpy') diff --git a/numpy/distutils/conv_template.py b/numpy/distutils/conv_template.py index e8823ed53..3bcb7b884 100644 --- a/numpy/distutils/conv_template.py +++ b/numpy/distutils/conv_template.py @@ -87,7 +87,6 @@ import os import sys import re -from numpy.compat import contextlib_nullcontext from numpy.distutils.compat import get_exception # names for replacement that are already global. @@ -317,24 +316,22 @@ def main(): try: file = sys.argv[1] except IndexError: - fid = contextlib_nullcontext(sys.stdin) - outfile = contextlib_nullcontext(sys.stdout) + fid = sys.stdin + outfile = sys.stdout else: fid = open(file, 'r') (base, ext) = os.path.splitext(file) newname = base outfile = open(newname, 'w') - with fid: - allstr = fid.read() + allstr = fid.read() try: writestr = process_str(allstr) except ValueError: e = get_exception() raise ValueError("In %s loop at %s" % (file, e)) - with outfile: - outfile.write(writestr) + outfile.write(writestr) if __name__ == "__main__": main() diff --git a/numpy/distutils/from_template.py b/numpy/distutils/from_template.py index 026de0093..c5c1163c6 100644 --- a/numpy/distutils/from_template.py +++ b/numpy/distutils/from_template.py @@ -53,8 +53,6 @@ import os import sys import re -from numpy.compat import contextlib_nullcontext - routine_start_re = re.compile(r'(\n|\A)(( (\$|\*))|)\s*(subroutine|function)\b', re.I) routine_end_re = re.compile(r'\n\s*end\s*(subroutine|function)\b.*(\n|\Z)', re.I) function_start_re = re.compile(r'\n (\$|\*)\s*function\b', re.I) @@ -249,19 +247,17 @@ def main(): try: file = sys.argv[1] except IndexError: - fid = contextlib_nullcontext(sys.stdin) - outfile = contextlib_nullcontext(sys.stdout) + fid = sys.stdin + outfile = sys.stdout else: fid = open(file, 'r') (base, ext) = os.path.splitext(file) newname = base outfile = open(newname, 'w') - with fid: - allstr = fid.read() + allstr = fid.read() writestr = process_str(allstr) - with outfile: - outfile.write(writestr) + outfile.write(writestr) if __name__ == "__main__": -- cgit v1.2.1 From 2ba8d620030d81b5d185be2da8b0cf7a51328ed2 Mon Sep 17 00:00:00 2001 From: Bharat123rox Date: Sat, 11 May 2019 23:16:57 +0530 Subject: Moved statements out of with block --- numpy/core/code_generators/generate_umath.py | 2 +- numpy/linalg/lapack_lite/fortran.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'numpy') diff --git a/numpy/core/code_generators/generate_umath.py b/numpy/core/code_generators/generate_umath.py index 3bd180854..d8a6a3e77 100644 --- a/numpy/core/code_generators/generate_umath.py +++ b/numpy/core/code_generators/generate_umath.py @@ -1154,6 +1154,6 @@ def make_code(funcdict, filename): if __name__ == "__main__": filename = __file__ + code = make_code(defdict, filename) with open('__umath_generated.c', 'w') as fid: - code = make_code(defdict, filename) fid.write(code) diff --git a/numpy/linalg/lapack_lite/fortran.py b/numpy/linalg/lapack_lite/fortran.py index 58c15f032..87c27aab9 100644 --- a/numpy/linalg/lapack_lite/fortran.py +++ b/numpy/linalg/lapack_lite/fortran.py @@ -110,9 +110,9 @@ def getDependencies(filename): """For a Fortran source file, return a list of routines declared as EXTERNAL in it. """ + external_pat = re.compile(r'^\s*EXTERNAL\s', re.I) + routines = [] with open(filename) as fo: - external_pat = re.compile(r'^\s*EXTERNAL\s', re.I) - routines = [] for lineno, line in fortranSourceLines(fo): m = external_pat.match(line) if m: -- cgit v1.2.1