diff options
author | Michael Williamson <mike@zwobble.org> | 2013-09-29 17:36:32 +0100 |
---|---|---|
committer | Michael Williamson <mike@zwobble.org> | 2014-09-07 18:27:19 +0100 |
commit | 3b7c8ee3bd87905d8ae6964aea3e2c1181672289 (patch) | |
tree | adacfc3cfec6d53835677429347be1f1066053c4 | |
parent | 64d54e942c8839acdb615bb9cbe402d7c9bd0709 (diff) |
Convert Channel into a context manager
-rw-r--r-- | paramiko/channel.py | 3 | ||||
-rw-r--r-- | tests/test_transport.py | 18 |
2 files changed, 20 insertions, 1 deletions
diff --git a/paramiko/channel.py b/paramiko/channel.py index 49d8dd6e..144edefd 100644 --- a/paramiko/channel.py +++ b/paramiko/channel.py @@ -37,13 +37,14 @@ from paramiko.ssh_exception import SSHException from paramiko.file import BufferedFile from paramiko.buffered_pipe import BufferedPipe, PipeTimeout from paramiko import pipe +from paramiko.util import ClosingContextManager # lower bound on the max packet size we'll accept from the remote host MIN_PACKET_SIZE = 1024 -class Channel (object): +class Channel (ClosingContextManager): """ A secure tunnel across an SSH `.Transport`. A Channel is meant to behave like a socket, and has an API that should be indistinguishable from the diff --git a/tests/test_transport.py b/tests/test_transport.py index 485a18e8..ae0f5e01 100644 --- a/tests/test_transport.py +++ b/tests/test_transport.py @@ -20,6 +20,8 @@ Some unit tests for the ssh2 protocol in Transport. """ +from __future__ import with_statement + from binascii import hexlify import select import socket @@ -278,6 +280,22 @@ class TransportTest(ParamikoTest): self.assertEqual('Hello there.\n', f.readline()) self.assertEqual('This is on stderr.\n', f.readline()) self.assertEqual('', f.readline()) + + def test_6a_channel_can_be_used_as_context_manager(self): + """ + verify that exec_command() does something reasonable. + """ + self.setup_test_server() + + with self.tc.open_session() as chan: + with self.ts.accept(1.0) as schan: + chan.exec_command('yes') + schan.send('Hello there.\n') + schan.close() + + f = chan.makefile() + self.assertEqual('Hello there.\n', f.readline()) + self.assertEqual('', f.readline()) def test_7_invoke_shell(self): """ |