More type conversion

This commit is contained in:
Scott Maxwell 2013-10-31 17:19:58 -07:00
parent d26bf3e63e
commit 8e1a7ef4d8
3 changed files with 12 additions and 11 deletions

View File

@ -48,6 +48,7 @@ def _to_unicode(s):
except UnicodeError: except UnicodeError:
return s return s
b_slash = b('/')
class SFTPClient (BaseSFTP): class SFTPClient (BaseSFTP):
""" """
@ -185,8 +186,8 @@ class SFTPClient (BaseSFTP):
raise SFTPError('Expected name response') raise SFTPError('Expected name response')
count = msg.get_int() count = msg.get_int()
for i in range(count): for i in range(count):
filename = _to_unicode(msg.get_string()) filename = msg.get_text()
longname = _to_unicode(msg.get_string()) longname = msg.get_text()
attr = SFTPAttributes._from_msg(msg, filename, longname) attr = SFTPAttributes._from_msg(msg, filename, longname)
if (filename != '.') and (filename != '..'): if (filename != '.') and (filename != '..'):
filelist.append(attr) filelist.append(attr)
@ -494,7 +495,7 @@ class SFTPClient (BaseSFTP):
count = msg.get_int() count = msg.get_int()
if count != 1: if count != 1:
raise SFTPError('Realpath returned %d results' % count) raise SFTPError('Realpath returned %d results' % count)
return _to_unicode(msg.get_string()) return msg.get_text()
def chdir(self, path): def chdir(self, path):
""" """
@ -517,7 +518,7 @@ class SFTPClient (BaseSFTP):
return return
if not stat.S_ISDIR(self.stat(path).st_mode): if not stat.S_ISDIR(self.stat(path).st_mode):
raise SFTPError(errno.ENOTDIR, "%s: %s" % (os.strerror(errno.ENOTDIR), path)) raise SFTPError(errno.ENOTDIR, "%s: %s" % (os.strerror(errno.ENOTDIR), path))
self._cwd = self.normalize(path).encode('utf-8') self._cwd = b(self.normalize(path))
def getcwd(self): def getcwd(self):
""" """
@ -530,7 +531,7 @@ class SFTPClient (BaseSFTP):
@since: 1.4 @since: 1.4
""" """
return self._cwd return u(self._cwd)
def putfo(self, fl, remotepath, file_size=0, callback=None, confirm=True): def putfo(self, fl, remotepath, file_size=0, callback=None, confirm=True):
""" """
@ -769,15 +770,15 @@ class SFTPClient (BaseSFTP):
Return an adjusted path if we're emulating a "current working Return an adjusted path if we're emulating a "current working
directory" for the server. directory" for the server.
""" """
path = bytestring(path) path = b(path)
if self._cwd is None: if self._cwd is None:
return path return path
if (len(path) > 0) and (path[0] == '/'): if (len(path) > 0) and (path[0] == '/'):
# absolute path # absolute path
return path return path
if self._cwd == '/': if self._cwd == b_slash:
return self._cwd + path return self._cwd + path
return self._cwd + '/' + path return self._cwd + b_slash + path
class SFTP (SFTPClient): class SFTP (SFTPClient):

View File

@ -165,7 +165,7 @@ class SFTPFile (BufferedFile):
def _write(self, data): def _write(self, data):
# may write less than requested if it would exceed max packet size # may write less than requested if it would exceed max packet size
chunk = min(len(data), self.MAX_REQUEST_SIZE) chunk = min(len(data), self.MAX_REQUEST_SIZE)
self._reqs.append(self.sftp._async_request(type(None), CMD_WRITE, self.handle, long(self._realpos), str(data[:chunk]))) self._reqs.append(self.sftp._async_request(type(None), CMD_WRITE, self.handle, long(self._realpos), data[:chunk]))
if not self.pipelined or (len(self._reqs) > 100 and self.sftp.sock.recv_ready()): if not self.pipelined or (len(self._reqs) > 100 and self.sftp.sock.recv_ready()):
while len(self._reqs): while len(self._reqs):
req = self._reqs.popleft() req = self._reqs.popleft()

View File

@ -183,7 +183,7 @@ class SFTPServer (BaseSFTP, SubsystemHandler):
msg.add_int(item) msg.add_int(item)
elif type(item) is long: elif type(item) is 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 type(item) is SFTPAttributes: elif type(item) is SFTPAttributes:
item._pack(msg) item._pack(msg)
@ -196,7 +196,7 @@ class SFTPServer (BaseSFTP, SubsystemHandler):
# must be error code # must be error code
self._send_status(request_number, handle) self._send_status(request_number, handle)
return return
handle._set_name('hx%d' % self.next_handle) handle._set_name(b('hx%d' % self.next_handle))
self.next_handle += 1 self.next_handle += 1
if folder: if folder:
self.folder_table[handle._get_name()] = handle self.folder_table[handle._get_name()] = handle