diff options
| author | Serhiy Storchaka <storchaka@gmail.com> | 2018-12-20 19:00:14 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-12-20 19:00:14 +0200 |
| commit | 8fa927e2b2fc81adb420285e470439fc3a28fcee (patch) | |
| tree | 24df4a75f94325760b58ae3509daf4683390f85c /command/config.py | |
| parent | f7d1e8f497ec20e55a7757e34d0a6229642ba59a (diff) | |
| download | python-setuptools-git-8fa927e2b2fc81adb420285e470439fc3a28fcee.tar.gz | |
bpo-22831: Use "with" to avoid possible fd leaks in distutils. (GH-10921)
Diffstat (limited to 'command/config.py')
| -rw-r--r-- | command/config.py | 36 |
1 files changed, 17 insertions, 19 deletions
diff --git a/command/config.py b/command/config.py index 4ae153d1..f511b888 100644 --- a/command/config.py +++ b/command/config.py @@ -106,15 +106,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): @@ -203,17 +202,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 + with open(out) as file: + match = False + while True: + line = file.readline() + if line == '': + break + if pattern.search(line): + match = True + break - file.close() self._clean() return match |
