diff options
author | Victor Stinner <victor.stinner@haypocalc.com> | 2011-05-19 13:51:27 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@haypocalc.com> | 2011-05-19 13:51:27 (GMT) |
commit | 21a9c748aa8698c8201e30a58320d587f6bb7540 (patch) | |
tree | ddfcb60a800bc010237ee750a449c4639e5515e3 /Lib/packaging/command | |
parent | 0e3f3a70760f5ef1c0d1f7baf55046514b6bfbb6 (diff) | |
download | cpython-21a9c748aa8698c8201e30a58320d587f6bb7540.zip cpython-21a9c748aa8698c8201e30a58320d587f6bb7540.tar.gz cpython-21a9c748aa8698c8201e30a58320d587f6bb7540.tar.bz2 |
packaging: use with open() instead of try/finally: close
Diffstat (limited to 'Lib/packaging/command')
-rw-r--r-- | Lib/packaging/command/config.py | 38 | ||||
-rw-r--r-- | Lib/packaging/command/upload_docs.py | 15 |
2 files changed, 25 insertions, 28 deletions
diff --git a/Lib/packaging/command/config.py b/Lib/packaging/command/config.py index a5feb91..264c139 100644 --- a/Lib/packaging/command/config.py +++ b/Lib/packaging/command/config.py @@ -110,15 +110,14 @@ class config(Command): def _gen_temp_sourcefile(self, body, headers, lang): filename = "_configtest" + LANG_EXT[lang] - file = open(filename, "w") - if headers: - for header in headers: - file.write("#include <%s>\n" % header) - file.write("\n") - file.write(body) - if body[-1] != "\n": - file.write("\n") - file.close() + with open(filename, "w") as file: + if headers: + for header in headers: + file.write("#include <%s>\n" % header) + file.write("\n") + file.write(body) + if body[-1] != "\n": + file.write("\n") return filename def _preprocess(self, body, headers, include_dirs, lang): @@ -207,17 +206,16 @@ class config(Command): if isinstance(pattern, str): pattern = re.compile(pattern) - file = open(out) - match = False - while True: - line = file.readline() - if line == '': - break - if pattern.search(line): - match = True - break - - file.close() + with open(out) as file: + match = False + while True: + line = file.readline() + if line == '': + break + if pattern.search(line): + match = True + break + self._clean() return match diff --git a/Lib/packaging/command/upload_docs.py b/Lib/packaging/command/upload_docs.py index 29ea6e9..47e6217 100644 --- a/Lib/packaging/command/upload_docs.py +++ b/Lib/packaging/command/upload_docs.py @@ -18,14 +18,13 @@ from packaging.command.cmd import Command def zip_dir(directory): """Compresses recursively contents of directory into a BytesIO object""" destination = BytesIO() - zip_file = zipfile.ZipFile(destination, "w") - for root, dirs, files in os.walk(directory): - for name in files: - full = os.path.join(root, name) - relative = root[len(directory):].lstrip(os.path.sep) - dest = os.path.join(relative, name) - zip_file.write(full, dest) - zip_file.close() + with zipfile.ZipFile(destination, "w") as zip_file: + for root, dirs, files in os.walk(directory): + for name in files: + full = os.path.join(root, name) + relative = root[len(directory):].lstrip(os.path.sep) + dest = os.path.join(relative, name) + zip_file.write(full, dest) return destination |