diff options
author | Martin Packman <gzlist@googlemail.com> | 2017-05-25 22:14:19 +0100 |
---|---|---|
committer | Jeff Forcier <jeff@bitprophet.org> | 2017-06-09 13:42:49 -0700 |
commit | c2c402cb6e4a4f86ff3053389bb6300c4b4505f1 (patch) | |
tree | fe7cc0aa3252f97c2c836fecfacb1eaacfbe67ea /paramiko/common.py | |
parent | 02b0aef5932438c2dd0b7d649a6e61450fb71be3 (diff) | |
download | paramiko-c2c402cb6e4a4f86ff3053389bb6300c4b4505f1.tar.gz |
Allow any buffer type to be sent to Channel
Fixes #968
Changes the behaviour of the underlying asbytes helper to
pass along unknown types. Most callers already handle this
by passing the bytes along to a file or socket-like object
which will raise TypeError anyway.
Adds test coverage through the Transport implementation.
Change against the 1.17 branch.
Diffstat (limited to 'paramiko/common.py')
-rw-r--r-- | paramiko/common.py | 19 |
1 files changed, 10 insertions, 9 deletions
diff --git a/paramiko/common.py b/paramiko/common.py index 556f046a..3b18858b 100644 --- a/paramiko/common.py +++ b/paramiko/common.py @@ -21,7 +21,7 @@ Common constants and global variables. """ import logging from paramiko.py3compat import ( - byte_chr, PY2, bytes_types, string_types, b, long, + byte_chr, PY2, bytes_types, text_type, string_types, b, long, ) MSG_DISCONNECT, MSG_IGNORE, MSG_UNIMPLEMENTED, MSG_DEBUG, \ @@ -163,14 +163,15 @@ else: def asbytes(s): - if not isinstance(s, bytes_types): - if isinstance(s, string_types): - s = b(s) - else: - try: - s = s.asbytes() - except Exception: - raise Exception('Unknown type') + if isinstance(s, bytes_types): + return s + if isinstance(s, text_type): + # GZ 2017-05-25: Accept text and encode as utf-8 for compatibilty only. + return s.encode("utf-8") + asbytes = getattr(s, "asbytes", None) + if asbytes is not None: + return asbytes() + # May be an object that implements the buffer api, let callers decide return s |