diff options
author | Bharat123rox <bharatraghunthan9767@gmail.com> | 2019-05-07 23:45:16 +0530 |
---|---|---|
committer | Bharat123rox <bharatraghunthan9767@gmail.com> | 2019-05-07 23:45:16 +0530 |
commit | 38025696d7f268b47d81e215bcf33c50ca1c98c1 (patch) | |
tree | 667fbfc1d3e2fb68ce657ec8fc101921f6b9784d /numpy/distutils | |
parent | dfc0e00eeeb9d3659aa8c06c5d6ea54c5d20742d (diff) | |
download | numpy-38025696d7f268b47d81e215bcf33c50ca1c98c1.tar.gz |
Use with statement to open/close files to fix LGTM alerts
Diffstat (limited to 'numpy/distutils')
-rw-r--r-- | numpy/distutils/conv_template.py | 32 | ||||
-rw-r--r-- | numpy/distutils/from_template.py | 32 | ||||
-rw-r--r-- | numpy/distutils/line_endings.py | 52 |
3 files changed, 58 insertions, 58 deletions
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<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)) - 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: |