diff options
author | Elliott Sales de Andrade <quantum.analyst@gmail.com> | 2016-08-24 19:29:11 -0400 |
---|---|---|
committer | Elliott Sales de Andrade <quantum.analyst@gmail.com> | 2016-08-27 18:29:55 -0400 |
commit | 4c71695e41a50dda3199d26ed7aedbaaf3150768 (patch) | |
tree | 8bd499a2a113c3da5c1dee8ed29f4b3c69d27dd7 /mesonbuild/mesonlib.py | |
parent | 7830cb61c39fbaf57933ac403dcdf5007667d87d (diff) | |
download | meson-4c71695e41a50dda3199d26ed7aedbaaf3150768.zip meson-4c71695e41a50dda3199d26ed7aedbaaf3150768.tar.gz meson-4c71695e41a50dda3199d26ed7aedbaaf3150768.tar.bz2 |
Use context manager for file I/O.
There are a few cases where a context manager cannot be used, such as
the logger.
Diffstat (limited to 'mesonbuild/mesonlib.py')
-rw-r--r-- | mesonbuild/mesonlib.py | 16 |
1 files changed, 10 insertions, 6 deletions
diff --git a/mesonbuild/mesonlib.py b/mesonbuild/mesonlib.py index b5b5339..007aea8 100644 --- a/mesonbuild/mesonlib.py +++ b/mesonbuild/mesonlib.py @@ -96,7 +96,8 @@ def is_32bit(): def is_debianlike(): try: - open('/etc/debian_version', 'r') + with open('/etc/debian_version', 'r'): + pass return True except FileNotFoundError: return False @@ -262,7 +263,8 @@ def do_mesondefine(line, confdata): def do_conf_file(src, dst, confdata): try: - data = open(src).readlines() + with open(src) as f: + data = f.readlines() except Exception: raise MesonException('Could not read input file %s.' % src) # Only allow (a-z, A-Z, 0-9, _, -) as valid characters for a define @@ -276,7 +278,8 @@ def do_conf_file(src, dst, confdata): line = do_replacement(regex, line, confdata) result.append(line) dst_tmp = dst + '~' - open(dst_tmp, 'w').writelines(result) + with open(dst_tmp, 'w') as f: + f.writelines(result) shutil.copymode(src, dst_tmp) replace_if_different(dst, dst_tmp) @@ -306,9 +309,10 @@ def replace_if_different(dst, dst_tmp): # If contents are identical, don't touch the file to prevent # unnecessary rebuilds. try: - if open(dst, 'r').read() == open(dst_tmp, 'r').read(): - os.unlink(dst_tmp) - return + with open(dst, 'r') as f1, open(dst_tmp, 'r') as f2: + if f1.read() == f2.read(): + os.unlink(dst_tmp) + return except FileNotFoundError: pass os.replace(dst_tmp, dst) |