Skip to content

Commit

Permalink
close the local/remote files in finally blocks in sftp get & put, so …
Browse files Browse the repository at this point in the history
…fds don't get lost. bug #379240
  • Loading branch information
Robey Pointer committed Jul 19, 2009
1 parent ac42ba8 commit fe35f44
Showing 1 changed file with 33 additions and 25 deletions.
58 changes: 33 additions & 25 deletions paramiko/sftp_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,19 +546,23 @@ def put(self, localpath, remotepath, callback=None):
"""
file_size = os.stat(localpath).st_size
fl = file(localpath, 'rb')
fr = self.file(remotepath, 'wb')
fr.set_pipelined(True)
size = 0
while True:
data = fl.read(32768)
if len(data) == 0:
break
fr.write(data)
size += len(data)
if callback is not None:
callback(size, file_size)
fl.close()
fr.close()
try:
fr = self.file(remotepath, 'wb')
fr.set_pipelined(True)
size = 0
try:
while True:
data = fl.read(32768)
if len(data) == 0:
break
fr.write(data)
size += len(data)
if callback is not None:
callback(size, file_size)
finally:
fr.close()
finally:
fl.close()
s = self.stat(remotepath)
if s.st_size != size:
raise IOError('size mismatch in put! %d != %d' % (s.st_size, size))
Expand All @@ -584,18 +588,22 @@ def get(self, remotepath, localpath, callback=None):
fr = self.file(remotepath, 'rb')
file_size = self.stat(remotepath).st_size
fr.prefetch()
fl = file(localpath, 'wb')
size = 0
while True:
data = fr.read(32768)
if len(data) == 0:
break
fl.write(data)
size += len(data)
if callback is not None:
callback(size, file_size)
fl.close()
fr.close()
try:
fl = file(localpath, 'wb')
try:
size = 0
while True:
data = fr.read(32768)
if len(data) == 0:
break
fl.write(data)
size += len(data)
if callback is not None:
callback(size, file_size)
finally:
fl.close()
finally:
fr.close()
s = os.stat(localpath)
if s.st_size != size:
raise IOError('size mismatch in get! %d != %d' % (s.st_size, size))
Expand Down

0 comments on commit fe35f44

Please sign in to comment.