mirror of
https://github.com/python/cpython.git
synced 2025-07-09 20:35:26 +00:00
bpo-22831: Use "with" to avoid possible fd leaks in tools (part 2). (GH-10927)
This commit is contained in:
parent
afbb7a371f
commit
172bb39452
27 changed files with 249 additions and 259 deletions
|
@ -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)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue