More type conversion

This commit is contained in:
Scott Maxwell 2013-10-31 16:19:11 -07:00
parent 951e8cfd3a
commit d26bf3e63e
5 changed files with 11 additions and 11 deletions

View File

@ -969,7 +969,7 @@ class Channel (object):
self.transport._send_user_message(m) self.transport._send_user_message(m)
def _feed(self, m): def _feed(self, m):
if isinstance(m, bytes_type): if isinstance(m, bytes_types):
# passed from _feed_extended # passed from _feed_extended
s = m s = m
else: else:

View File

@ -140,7 +140,7 @@ else:
def asbytes(s): def asbytes(s):
if not isinstance(s, bytes_type): if not isinstance(s, bytes_types):
if isinstance(s, string_types): if isinstance(s, string_types):
s = b(s) s = b(s)
else: else:

View File

@ -1,6 +1,6 @@
import sys import sys
__all__ = ['PY3', 'string_types', 'integer_types', 'text_type', 'bytes_type', 'long', 'input', 'bytestring', 'byte_ord', 'byte_chr', 'byte_mask', 'b', 'u', 'b2s', 'StringIO', 'BytesIO', 'is_callable', 'MAXSIZE', 'next'] __all__ = ['PY3', 'string_types', 'integer_types', 'text_type', 'bytes_types', 'long', 'input', 'bytestring', 'byte_ord', 'byte_chr', 'byte_mask', 'b', 'u', 'b2s', 'StringIO', 'BytesIO', 'is_callable', 'MAXSIZE', 'next']
PY3 = sys.version_info[0] >= 3 PY3 = sys.version_info[0] >= 3
@ -8,9 +8,9 @@ if PY3:
import collections import collections
import struct import struct
string_types = str string_types = str
integer_types = int
text_type = str text_type = str
bytes_type = bytes bytes_types = bytes
integer_types = int
long = int long = int
input = input input = input
@ -65,9 +65,9 @@ if PY3:
MAXSIZE = sys.maxsize # NOQA MAXSIZE = sys.maxsize # NOQA
else: else:
string_types = basestring string_types = basestring
integer_types = (int, long)
text_type = unicode text_type = unicode
bytes_type = str bytes_types = str
integer_types = (int, long)
long = long long = long
input = raw_input input = raw_input

View File

@ -142,7 +142,7 @@ class BaseSFTP (object):
return return
def _read_all(self, n): def _read_all(self, n):
out = '' out = bytes()
while n > 0: while n > 0:
if isinstance(self.sock, socket.socket): if isinstance(self.sock, socket.socket):
# sometimes sftp is used directly over a socket instead of # sometimes sftp is used directly over a socket instead of
@ -176,7 +176,7 @@ class BaseSFTP (object):
x = self._read_all(4) x = self._read_all(4)
# most sftp servers won't accept packets larger than about 32k, so # most sftp servers won't accept packets larger than about 32k, so
# anything with the high byte set (> 16MB) is just garbage. # anything with the high byte set (> 16MB) is just garbage.
if x[0] != '\x00': if byte_ord(x[0]):
raise SFTPError('Garbage packet received') raise SFTPError('Garbage packet received')
size = struct.unpack('>I', x)[0] size = struct.unpack('>I', x)[0]
data = self._read_all(size) data = self._read_all(size)

View File

@ -42,7 +42,7 @@ def _to_unicode(s):
""" """
try: try:
return s.encode('ascii') return s.encode('ascii')
except UnicodeError: except (UnicodeError, AttributeError):
try: try:
return s.decode('utf-8') return s.decode('utf-8')
except UnicodeError: except UnicodeError:
@ -698,7 +698,7 @@ class SFTPClient (BaseSFTP):
msg.add_int(item) msg.add_int(item)
elif isinstance(item, long): elif isinstance(item, long):
msg.add_int64(item) msg.add_int64(item)
elif isinstance(item, string_types): elif isinstance(item, (string_types, bytes_types)):
msg.add_string(item) msg.add_string(item)
elif isinstance(item, SFTPAttributes): elif isinstance(item, SFTPAttributes):
item._pack(msg) item._pack(msg)