From de1e072c739b7f6a97fcdb5438920acfbb657576 Mon Sep 17 00:00:00 2001 From: Robey Pointer Date: Sun, 7 May 2006 16:01:53 -0700 Subject: [PATCH] [project @ robey@lag.net-20060507230153-dba6b2d664b5ef3f] channel operations raise an exception on error now instead of returning a bool --- paramiko/channel.py | 41 ++++++++++++++++++++++++----------------- paramiko/sftp_client.py | 3 +-- tests/test_transport.py | 20 ++++++++++++-------- 3 files changed, 37 insertions(+), 27 deletions(-) diff --git a/paramiko/channel.py b/paramiko/channel.py index 7eb5b08..d5d8e97 100644 --- a/paramiko/channel.py +++ b/paramiko/channel.py @@ -129,8 +129,9 @@ class Channel (object): @type width: int @param height: height (in characters) of the terminal screen @type height: int - @return: C{True} if the operation succeeded; C{False} if not. - @rtype: bool + + @raise SSHException: if the request was rejected or the channel was + closed """ if self.closed or self.eof_received or self.eof_sent or not self.active: raise SSHException('Channel is not open') @@ -147,7 +148,7 @@ class Channel (object): m.add_string('') self.event.clear() self.transport._send_user_message(m) - return self._wait_for_event() + self._wait_for_event() def invoke_shell(self): """ @@ -162,8 +163,8 @@ class Channel (object): When the shell exits, the channel will be closed and can't be reused. You must open a new channel if you wish to open another shell. - @return: C{True} if the operation succeeded; C{False} if not. - @rtype: bool + @raise SSHException: if the request was rejected or the channel was + closed """ if self.closed or self.eof_received or self.eof_sent or not self.active: raise SSHException('Channel is not open') @@ -174,7 +175,7 @@ class Channel (object): m.add_boolean(1) self.event.clear() self.transport._send_user_message(m) - return self._wait_for_event() + self._wait_for_event() def exec_command(self, command): """ @@ -188,8 +189,9 @@ class Channel (object): @param command: a shell command to execute. @type command: str - @return: C{True} if the operation succeeded; C{False} if not. - @rtype: bool + + @raise SSHException: if the request was rejected or the channel was + closed """ if self.closed or self.eof_received or self.eof_sent or not self.active: raise SSHException('Channel is not open') @@ -201,7 +203,7 @@ class Channel (object): m.add_string(command) self.event.clear() self.transport._send_user_message(m) - return self._wait_for_event() + self._wait_for_event() def invoke_subsystem(self, subsystem): """ @@ -214,8 +216,9 @@ class Channel (object): @param subsystem: name of the subsystem being requested. @type subsystem: str - @return: C{True} if the operation succeeded; C{False} if not. - @rtype: bool + + @raise SSHException: if the request was rejected or the channel was + closed """ if self.closed or self.eof_received or self.eof_sent or not self.active: raise SSHException('Channel is not open') @@ -227,7 +230,7 @@ class Channel (object): m.add_string(subsystem) self.event.clear() self.transport._send_user_message(m) - return self._wait_for_event() + self._wait_for_event() def resize_pty(self, width=80, height=24): """ @@ -238,8 +241,9 @@ class Channel (object): @type width: int @param height: new height (in characters) of the terminal screen @type height: int - @return: C{True} if the operation succeeded; C{False} if not. - @rtype: bool + + @raise SSHException: if the request was rejected or the channel was + closed """ if self.closed or self.eof_received or self.eof_sent or not self.active: raise SSHException('Channel is not open') @@ -946,10 +950,13 @@ class Channel (object): while True: self.event.wait(0.1) if self.event.isSet(): - break + return if self.closed: - return False - return True + e = self.transport.get_exception() + if e is None: + e = SSHException('Channel closed.') + raise e + return def _set_closed(self): # you are holding the lock. diff --git a/paramiko/sftp_client.py b/paramiko/sftp_client.py index f02827e..c5d5f39 100644 --- a/paramiko/sftp_client.py +++ b/paramiko/sftp_client.py @@ -93,8 +93,7 @@ class SFTPClient (BaseSFTP): chan = t.open_session() if chan is None: return None - if not chan.invoke_subsystem('sftp'): - raise SFTPError('Failed to invoke sftp subsystem') + chan.invoke_subsystem('sftp') return cls(chan) from_transport = classmethod(from_transport) diff --git a/tests/test_transport.py b/tests/test_transport.py index b2e8b6f..f73f0f7 100644 --- a/tests/test_transport.py +++ b/tests/test_transport.py @@ -353,10 +353,14 @@ class TransportTest (unittest.TestCase): chan = self.tc.open_session() schan = self.ts.accept(1.0) - self.assert_(not chan.exec_command('no')) + try: + chan.exec_command('no') + self.assert_(False) + except SSHException, x: + pass chan = self.tc.open_session() - self.assert_(chan.exec_command('yes')) + chan.exec_command('yes') schan = self.ts.accept(1.0) schan.send('Hello there.\n') schan.send_stderr('This is on stderr.\n') @@ -371,7 +375,7 @@ class TransportTest (unittest.TestCase): # now try it with combined stdout/stderr chan = self.tc.open_session() - self.assert_(chan.exec_command('yes')) + chan.exec_command('yes') schan = self.ts.accept(1.0) schan.send('Hello there.\n') schan.send_stderr('This is on stderr.\n') @@ -402,7 +406,7 @@ class TransportTest (unittest.TestCase): self.assert_(self.ts.is_active()) chan = self.tc.open_session() - self.assert_(chan.invoke_shell()) + chan.invoke_shell() schan = self.ts.accept(1.0) chan.send('communist j. cat\n') f = schan.makefile() @@ -454,7 +458,7 @@ class TransportTest (unittest.TestCase): chan = self.tc.open_session() schan = self.ts.accept(1.0) - self.assert_(chan.exec_command('yes')) + chan.exec_command('yes') schan.send('Hello there.\n') # trigger an EOF schan.shutdown_read() @@ -487,7 +491,7 @@ class TransportTest (unittest.TestCase): self.assert_(self.ts.is_active()) chan = self.tc.open_session() - self.assert_(chan.invoke_shell()) + chan.invoke_shell() schan = self.ts.accept(1.0) # nothing should be ready @@ -550,7 +554,7 @@ class TransportTest (unittest.TestCase): self.tc.packetizer.REKEY_BYTES = 16384 chan = self.tc.open_session() - self.assert_(chan.exec_command('yes')) + chan.exec_command('yes') schan = self.ts.accept(1.0) self.assertEquals(self.tc.H, self.tc.session_id) @@ -586,7 +590,7 @@ class TransportTest (unittest.TestCase): self.assert_(self.ts.is_active()) chan = self.tc.open_session() - self.assert_(chan.exec_command('yes')) + chan.exec_command('yes') schan = self.ts.accept(1.0) bytes = self.tc.packetizer._Packetizer__sent_bytes