Skip to content

Commit 6c452d6

Browse files
committed
Close the local file on errors
1 parent df55052 commit 6c452d6

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

scp.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,10 @@ def _send_files(self, files):
311311
if self.preserve_times:
312312
self._send_time(mtime, atime)
313313
fl = open(name, 'rb')
314-
self._send_file(fl, name, mode, size)
315-
fl.close()
314+
try:
315+
self._send_file(fl, name, mode, size)
316+
finally:
317+
fl.close()
316318

317319
def _send_file(self, fl, name, mode, size):
318320
basename = asbytes(os.path.basename(name))
@@ -500,16 +502,15 @@ def _recv_file(self, cmd):
500502
msg = chan.recv(512)
501503
if msg and msg[0:1] != b'\x00':
502504
raise SCPException(asunicode(msg[1:]))
503-
except SocketTimeout:
504-
chan.close()
505-
raise SCPException('Error receiving, socket.timeout')
506505

507-
file_hdl.truncate()
508-
try:
506+
file_hdl.truncate()
509507
os.utime(path, self._utime)
510508
self._utime = None
511509
os.chmod(path, mode)
512510
# should we notify the other end?
511+
except SocketTimeout:
512+
chan.close()
513+
raise SCPException('Error receiving, socket.timeout')
513514
finally:
514515
file_hdl.close()
515516
# '\x00' confirmation sent in _recv_all

0 commit comments

Comments
 (0)