diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2019-03-30 08:33:02 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-30 08:33:02 +0200 |
commit | 172bb39452ae8b3ccdf5d1f23ead46f44200cd49 (patch) | |
tree | 5e1effbca3664b839a81eb7a7d62fa4974cfbfb1 /Tools/scripts/fixnotice.py | |
parent | afbb7a371fb44edc731344eab5b474ad8f7b57d7 (diff) | |
download | cpython-git-172bb39452ae8b3ccdf5d1f23ead46f44200cd49.tar.gz |
bpo-22831: Use "with" to avoid possible fd leaks in tools (part 2). (GH-10927)
Diffstat (limited to 'Tools/scripts/fixnotice.py')
-rwxr-xr-x | Tools/scripts/fixnotice.py | 20 |
1 files changed, 8 insertions, 12 deletions
diff --git a/Tools/scripts/fixnotice.py b/Tools/scripts/fixnotice.py index ad967f9478..317051dd82 100755 --- a/Tools/scripts/fixnotice.py +++ b/Tools/scripts/fixnotice.py @@ -73,22 +73,19 @@ def main(): elif opt == '--dry-run': DRYRUN = 1 elif opt == '--oldnotice': - fp = open(arg) - OLD_NOTICE = fp.read() - fp.close() + with open(arg) as fp: + OLD_NOTICE = fp.read() elif opt == '--newnotice': - fp = open(arg) - NEW_NOTICE = fp.read() - fp.close() + with open(arg) as fp: + NEW_NOTICE = fp.read() for arg in args: process(arg) def process(file): - f = open(file) - data = f.read() - f.close() + with open(file) as f: + data = f.read() i = data.find(OLD_NOTICE) if i < 0: if VERBOSE: @@ -102,9 +99,8 @@ def process(file): data = data[:i] + NEW_NOTICE + data[i+len(OLD_NOTICE):] new = file + ".new" backup = file + ".bak" - f = open(new, "w") - f.write(data) - f.close() + with open(new, "w") as f: + f.write(data) os.rename(file, backup) os.rename(new, file) |