More type conversion

This commit is contained in:
Scott Maxwell 2013-10-31 15:25:45 -07:00
parent 09a4ffb282
commit 7a45d3c70f
10 changed files with 27 additions and 18 deletions

View File

@ -19,6 +19,7 @@
import socket import socket
import sys import sys
from paramiko.py3compat import u
# windows does not have termios... # windows does not have termios...
try: try:
@ -49,7 +50,7 @@ def posix_shell(chan):
r, w, e = select.select([chan, sys.stdin], [], []) r, w, e = select.select([chan, sys.stdin], [], [])
if chan in r: if chan in r:
try: try:
x = chan.recv(1024) x = u(chan.recv(1024))
if len(x) == 0: if len(x) == 0:
sys.stdout.write('\r\n*** EOF\r\n') sys.stdout.write('\r\n*** EOF\r\n')
break break

View File

@ -1059,7 +1059,7 @@ class Channel (object):
elif key == 'x11-req': elif key == 'x11-req':
single_connection = m.get_boolean() single_connection = m.get_boolean()
auth_proto = m.get_text() auth_proto = m.get_text()
auth_cookie = m.get_text() auth_cookie = m.get_binary()
screen_number = m.get_int() screen_number = m.get_int()
if server is None: if server is None:
ok = False ok = False

View File

@ -129,7 +129,7 @@ if PY3:
four_byte = b'\x04' four_byte = b'\x04'
max_byte = b'\xff' max_byte = b'\xff'
newline_byte = b'\n' newline_byte = b'\n'
cr_byte = b'\r' cr_byte = 13
else: else:
zero_byte = '\x00' zero_byte = '\x00'
one_byte = '\x01' one_byte = '\x01'

View File

@ -235,7 +235,7 @@ class BufferedFile (object):
self._rbuffer = '' self._rbuffer = ''
self._pos += len(line) self._pos += len(line)
return line return line
line += new_data line += b2s(new_data)
self._realpos += len(new_data) self._realpos += len(new_data)
# find the newline # find the newline
pos = line.find('\n') pos = line.find('\n')

View File

@ -321,7 +321,7 @@ class PKey (object):
try: try:
encryption_type, saltstr = headers['dek-info'].split(',') encryption_type, saltstr = headers['dek-info'].split(',')
except: except:
raise SSHException('Can\'t parse DEK-info in private key file') raise SSHException("Can't parse DEK-info in private key file")
if encryption_type not in self._CIPHER_TABLE: if encryption_type not in self._CIPHER_TABLE:
raise SSHException('Unknown private key cipher "%s"' % encryption_type) raise SSHException('Unknown private key cipher "%s"' % encryption_type)
# if no password was passed in, raise an exception pointing out that we need one # if no password was passed in, raise an exception pointing out that we need one
@ -373,10 +373,10 @@ class PKey (object):
n = blocksize - len(data) % blocksize n = blocksize - len(data) % blocksize
#data += rng.read(n) #data += rng.read(n)
# that would make more sense ^, but it confuses openssh. # that would make more sense ^, but it confuses openssh.
data += '\0' * n data += zero_byte * n
data = cipher.new(key, mode, salt).encrypt(data) data = cipher.new(key, mode, salt).encrypt(data)
f.write('Proc-Type: 4,ENCRYPTED\n') f.write('Proc-Type: 4,ENCRYPTED\n')
f.write('DEK-Info: %s,%s\n' % (cipher_name, hexlify(salt).upper())) f.write('DEK-Info: %s,%s\n' % (cipher_name, u(hexlify(salt)).upper()))
f.write('\n') f.write('\n')
s = u(base64.encodestring(data)) s = u(base64.encodestring(data))
# re-wrap to 64-char lines # re-wrap to 64-char lines

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', 'StringIO', 'BytesIO', 'is_callable', 'MAXSIZE', 'next'] __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']
PY3 = sys.version_info[0] >= 3 PY3 = sys.version_info[0] >= 3
@ -47,6 +47,9 @@ if PY3:
else: else:
raise TypeError("Expected unicode or bytes, got %r" % s) raise TypeError("Expected unicode or bytes, got %r" % s)
def b2s(s):
return s.decode() if isinstance(s, bytes) else s
import io import io
StringIO = io.StringIO # NOQA StringIO = io.StringIO # NOQA
BytesIO = io.BytesIO # NOQA BytesIO = io.BytesIO # NOQA
@ -97,6 +100,9 @@ else:
else: else:
raise TypeError("Expected unicode or bytes, got %r" % s) raise TypeError("Expected unicode or bytes, got %r" % s)
def b2s(s):
return s
try: try:
import cStringIO import cStringIO
StringIO = cStringIO.StringIO # NOQA StringIO = cStringIO.StringIO # NOQA

View File

@ -805,7 +805,7 @@ class Transport (threading.Thread):
""" """
if not self.active: if not self.active:
raise SSHException('SSH session not active') raise SSHException('SSH session not active')
address = str(address) address = address
port = int(port) port = int(port)
response = self.global_request('tcpip-forward', (address, port), wait=True) response = self.global_request('tcpip-forward', (address, port), wait=True)
if response is None: if response is None:

View File

@ -21,7 +21,7 @@
""" """
import threading, socket import threading, socket
from paramiko.py3compat import * from paramiko.common import *
class LoopSocket (object): class LoopSocket (object):
@ -47,6 +47,7 @@ class LoopSocket (object):
self.__lock.release() self.__lock.release()
def send(self, data): def send(self, data):
data = asbytes(data)
if self.__mate is None: if self.__mate is None:
# EOF # EOF
raise EOFError() raise EOFError()

View File

@ -29,6 +29,7 @@ import weakref
from binascii import hexlify from binascii import hexlify
from tests.util import test_path from tests.util import test_path
import paramiko import paramiko
from paramiko.py3compat import b
class NullServer (paramiko.ServerInterface): class NullServer (paramiko.ServerInterface):
@ -44,7 +45,7 @@ class NullServer (paramiko.ServerInterface):
return paramiko.AUTH_FAILED return paramiko.AUTH_FAILED
def check_auth_publickey(self, username, key): def check_auth_publickey(self, username, key):
if (key.get_name() == 'ssh-dss') and (hexlify(key.get_fingerprint()) == '4478f0b9a23cc5182009ff755bc1d26c'): if (key.get_name() == 'ssh-dss') and (hexlify(key.get_fingerprint()) == b('4478f0b9a23cc5182009ff755bc1d26c')):
return paramiko.AUTH_SUCCESSFUL return paramiko.AUTH_SUCCESSFUL
return paramiko.AUTH_FAILED return paramiko.AUTH_FAILED

View File

@ -362,7 +362,7 @@ class TransportTest(ParamikoTest):
self.assertEquals([], w) self.assertEquals([], w)
self.assertEquals([], e) self.assertEquals([], e)
self.assertEquals('hello\n', chan.recv(6)) self.assertEquals(b('hello\n'), chan.recv(6))
# and, should be dead again now # and, should be dead again now
r, w, e = select.select([chan], [], [], 0.1) r, w, e = select.select([chan], [], [], 0.1)
@ -381,7 +381,7 @@ class TransportTest(ParamikoTest):
self.assertEquals([chan], r) self.assertEquals([chan], r)
self.assertEquals([], w) self.assertEquals([], w)
self.assertEquals([], e) self.assertEquals([], e)
self.assertEquals('', chan.recv(16)) self.assertEquals(bytes(), chan.recv(16))
# make sure the pipe is still open for now... # make sure the pipe is still open for now...
p = chan._pipe p = chan._pipe
@ -463,7 +463,7 @@ class TransportTest(ParamikoTest):
self.assertEquals(6093, requested[0][1]) self.assertEquals(6093, requested[0][1])
x11_server.send('hello') x11_server.send('hello')
self.assertEquals('hello', x11_client.recv(5)) self.assertEquals(b('hello'), x11_client.recv(5))
x11_server.close() x11_server.close()
x11_client.close() x11_client.close()
@ -496,7 +496,7 @@ class TransportTest(ParamikoTest):
cch = self.tc.accept() cch = self.tc.accept()
sch.send('hello') sch.send('hello')
self.assertEquals('hello', cch.recv(5)) self.assertEquals(b('hello'), cch.recv(5))
sch.close() sch.close()
cch.close() cch.close()
ss.close() ss.close()
@ -528,12 +528,12 @@ class TransportTest(ParamikoTest):
cch.connect(self.server._tcpip_dest) cch.connect(self.server._tcpip_dest)
ss, _ = greeting_server.accept() ss, _ = greeting_server.accept()
ss.send('Hello!\n') ss.send(b('Hello!\n'))
ss.close() ss.close()
sch.send(cch.recv(8192)) sch.send(cch.recv(8192))
sch.close() sch.close()
self.assertEquals('Hello!\n', cs.recv(7)) self.assertEquals(b('Hello!\n'), cs.recv(7))
cs.close() cs.close()
def test_G_stderr_select(self): def test_G_stderr_select(self):
@ -564,7 +564,7 @@ class TransportTest(ParamikoTest):
self.assertEquals([], w) self.assertEquals([], w)
self.assertEquals([], e) self.assertEquals([], e)
self.assertEquals('hello\n', chan.recv_stderr(6)) self.assertEquals(b('hello\n'), chan.recv_stderr(6))
# and, should be dead again now # and, should be dead again now
r, w, e = select.select([chan], [], [], 0.1) r, w, e = select.select([chan], [], [], 0.1)