diff options
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/core/code_generators/generate_umath.py | 5 | ||||
-rw-r--r-- | numpy/core/numeric.py | 3 | ||||
-rw-r--r-- | numpy/distutils/conv_template.py | 28 | ||||
-rw-r--r-- | numpy/distutils/from_template.py | 28 | ||||
-rw-r--r-- | numpy/distutils/line_endings.py | 6 | ||||
-rwxr-xr-x | numpy/f2py/f2py2e.py | 8 | ||||
-rw-r--r-- | numpy/f2py/rules.py | 103 | ||||
-rw-r--r-- | numpy/linalg/lapack_lite/clapack_scrub.py | 5 | ||||
-rw-r--r-- | numpy/linalg/lapack_lite/fortran.py | 17 |
9 files changed, 99 insertions, 104 deletions
diff --git a/numpy/core/code_generators/generate_umath.py b/numpy/core/code_generators/generate_umath.py index 53c53d7c4..76360eb5a 100644 --- a/numpy/core/code_generators/generate_umath.py +++ b/numpy/core/code_generators/generate_umath.py @@ -1156,7 +1156,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: + fid.write(code) diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index 4b59d730d..e72ab3012 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -2002,7 +2002,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..3bcb7b884 100644 --- a/numpy/distutils/conv_template.py +++ b/numpy/distutils/conv_template.py @@ -267,22 +267,21 @@ 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)) + 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) else: lines.append(line) - else: - lines.append(line) - fid.close() return lines def process_file(source): @@ -331,6 +330,7 @@ def main(): except ValueError: e = get_exception() raise ValueError("In %s loop at %s" % (file, e)) + outfile.write(writestr) if __name__ == "__main__": diff --git a/numpy/distutils/from_template.py b/numpy/distutils/from_template.py index 65c60c498..c5c1163c6 100644 --- a/numpy/distutils/from_template.py +++ b/numpy/distutils/from_template.py @@ -212,22 +212,21 @@ include_src_re = re.compile(r"(\n|\A)\s*include\s*['\"](?P<name>[\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)) + 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) else: lines.append(line) - else: - lines.append(line) - fid.close() return lines def process_file(source): @@ -260,5 +259,6 @@ def main(): writestr = process_str(allstr) outfile.write(writestr) + if __name__ == "__main__": main() diff --git a/numpy/distutils/line_endings.py b/numpy/distutils/line_endings.py index 2420798ab..fe8fd1b0f 100644 --- a/numpy/distutils/line_endings.py +++ b/numpy/distutils/line_endings.py @@ -11,7 +11,8 @@ def dos2unix(file): print(file, "Directory!") return - data = open(file, "rb").read() + with open(file, "rb") as fp: + data = fp.read() if '\0' in data: print(file, "Binary!") return @@ -44,7 +45,8 @@ def unix2dos(file): print(file, "Directory!") return - data = open(file, "rb").read() + with open(file, "rb") as fp: + data = fp.read() if '\0' in data: print(file, "Binary!") return diff --git a/numpy/f2py/f2py2e.py b/numpy/f2py/f2py2e.py index 47223151f..110337f92 100755 --- a/numpy/f2py/f2py2e.py +++ b/numpy/f2py/f2py2e.py @@ -269,7 +269,8 @@ def scaninputline(inputline): options["f2py_wrapper_output"] = l elif f == 1: try: - open(l).close() + with open(l): + pass files.append(l) except IOError as detail: errmess('IOError: %s. Skipping file "%s".\n' % @@ -333,9 +334,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..6769f1b1f 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: + with open(fn, 'w') as f: 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() + '%% 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..87c27aab9 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: + 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 |