bpo-22831: Use "with" to avoid possible fd leaks in tools (part 2). (GH-10927)

This commit is contained in:
Serhiy Storchaka 2019-03-30 08:33:02 +02:00 committed by GitHub
parent afbb7a371f
commit 172bb39452
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
27 changed files with 249 additions and 259 deletions

View file

@ -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)