channel operations raise an exception on error now instead of returning a bool
This commit is contained in:
Robey Pointer 2006-05-07 16:01:53 -07:00
parent 581103665b
commit de1e072c73
3 changed files with 37 additions and 27 deletions

View File

@ -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.

View File

@ -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)

View File

@ -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