mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +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
|
@ -18,14 +18,13 @@ def mkrealfile(name):
|
|||
st = os.stat(name) # Get the mode
|
||||
mode = S_IMODE(st[ST_MODE])
|
||||
linkto = os.readlink(name) # Make sure again it's a symlink
|
||||
f_in = open(name, 'r') # This ensures it's a file
|
||||
os.unlink(name)
|
||||
f_out = open(name, 'w')
|
||||
while 1:
|
||||
buf = f_in.read(BUFSIZE)
|
||||
if not buf: break
|
||||
f_out.write(buf)
|
||||
del f_out # Flush data to disk before changing mode
|
||||
with open(name, 'rb') as f_in: # This ensures it's a file
|
||||
os.unlink(name)
|
||||
with open(name, 'wb') as f_out:
|
||||
while 1:
|
||||
buf = f_in.read(BUFSIZE)
|
||||
if not buf: break
|
||||
f_out.write(buf)
|
||||
os.chmod(name, mode)
|
||||
|
||||
def mkrealdir(name):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue