From fe35f44f2e010c41411587817ccdc998d27168ab Mon Sep 17 00:00:00 2001 From: Robey Pointer Date: Sun, 19 Jul 2009 15:19:10 -0700 Subject: [PATCH] close the local/remote files in finally blocks in sftp get & put, so fds don't get lost. bug #379240 --- paramiko/sftp_client.py | 58 +++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 25 deletions(-) diff --git a/paramiko/sftp_client.py b/paramiko/sftp_client.py index b3d2d56..d392908 100644 --- a/paramiko/sftp_client.py +++ b/paramiko/sftp_client.py @@ -546,19 +546,23 @@ class SFTPClient (BaseSFTP): """ 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)) @@ -584,18 +588,22 @@ class SFTPClient (BaseSFTP): 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))