[Python 3]: Added workaround for unified integer and long types in Python 3.

This commit is contained in:
Dorian 2013-10-24 01:07:04 -04:00
parent b2f74c1291
commit b94fce4df9
10 changed files with 66 additions and 33 deletions

1
.gitignore vendored
View File

@ -5,3 +5,4 @@ dist/
paramiko.egg-info/ paramiko.egg-info/
test.log test.log
docs/ docs/
.idea/

View File

@ -28,11 +28,14 @@ from paramiko import util
from paramiko.message import Message from paramiko.message import Message
from paramiko.ssh_exception import SSHException from paramiko.ssh_exception import SSHException
import six
if six.PY3:
long = lambda x: int(x)
_MSG_KEXDH_INIT, _MSG_KEXDH_REPLY = range(30, 32) _MSG_KEXDH_INIT, _MSG_KEXDH_REPLY = range(30, 32)
# draft-ietf-secsh-transport-09.txt, page 17 # draft-ietf-secsh-transport-09.txt, page 17
P = 0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFFL P = long(0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF)
G = 2 G = 2
@ -42,9 +45,9 @@ class KexGroup1(object):
def __init__(self, transport): def __init__(self, transport):
self.transport = transport self.transport = transport
self.x = 0L self.x = 0
self.e = 0L self.e = 0
self.f = 0L self.f = 0
def start_kex(self): def start_kex(self):
self._generate_x() self._generate_x()

View File

@ -277,7 +277,7 @@ class Message (object):
elif type(i) is int: elif type(i) is int:
return self.add_int(i) return self.add_int(i)
elif type(i) is long: elif type(i) is long:
if i > 0xffffffffL: if i > long(0xffffffff):
return self.add_mpint(i) return self.add_mpint(i)
else: else:
return self.add_int(i) return self.add_int(i)

View File

@ -27,6 +27,10 @@ import struct
import threading import threading
import time import time
import six
if six.PY3:
long = lambda x: int(x)
from paramiko.common import * from paramiko.common import *
from paramiko import util from paramiko import util
from paramiko.ssh_exception import SSHException, ProxyCommandFailure from paramiko.ssh_exception import SSHException, ProxyCommandFailure
@ -94,8 +98,8 @@ class Packetizer (object):
self.__mac_key_in = '' self.__mac_key_in = ''
self.__compress_engine_out = None self.__compress_engine_out = None
self.__compress_engine_in = None self.__compress_engine_in = None
self.__sequence_number_out = 0L self.__sequence_number_out = 0
self.__sequence_number_in = 0L self.__sequence_number_in = 0
# lock around outbound writes (packet computation) # lock around outbound writes (packet computation)
self.__write_lock = threading.RLock() self.__write_lock = threading.RLock()
@ -315,7 +319,7 @@ class Packetizer (object):
if self.__block_engine_out != None: if self.__block_engine_out != None:
payload = struct.pack('>I', self.__sequence_number_out) + packet payload = struct.pack('>I', self.__sequence_number_out) + packet
out += compute_hmac(self.__mac_key_out, payload, self.__mac_engine_out)[:self.__mac_size_out] out += compute_hmac(self.__mac_key_out, payload, self.__mac_engine_out)[:self.__mac_size_out]
self.__sequence_number_out = (self.__sequence_number_out + 1) & 0xffffffffL self.__sequence_number_out = (self.__sequence_number_out + 1) & long(0xffffffff)
self.write_all(out) self.write_all(out)
self.__sent_bytes += len(out) self.__sent_bytes += len(out)
@ -375,7 +379,7 @@ class Packetizer (object):
msg = Message(payload[1:]) msg = Message(payload[1:])
msg.seqno = self.__sequence_number_in msg.seqno = self.__sequence_number_in
self.__sequence_number_in = (self.__sequence_number_in + 1) & 0xffffffffL self.__sequence_number_in = (self.__sequence_number_in + 1) & long(0xffffffff)
# check for rekey # check for rekey
raw_packet_size = packet_size + self.__mac_size_in + 4 raw_packet_size = packet_size + self.__mac_size_in + 4

View File

@ -16,6 +16,10 @@
# along with Paramiko; if not, write to the Free Software Foundation, Inc., # along with Paramiko; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
import six
if six.PY3:
long = lambda x: int(x)
import stat import stat
import time import time
from paramiko.common import * from paramiko.common import *
@ -44,7 +48,7 @@ class SFTPAttributes (object):
FLAG_UIDGID = 2 FLAG_UIDGID = 2
FLAG_PERMISSIONS = 4 FLAG_PERMISSIONS = 4
FLAG_AMTIME = 8 FLAG_AMTIME = 8
FLAG_EXTENDED = 0x80000000L FLAG_EXTENDED = long(0x80000000)
def __init__(self): def __init__(self):
""" """
@ -200,7 +204,7 @@ class SFTPAttributes (object):
else: else:
ks = '?---------' ks = '?---------'
# compute display date # compute display date
if (self.st_mtime is None) or (self.st_mtime == 0xffffffffL): if (self.st_mtime is None) or (self.st_mtime == long(0xffffffff)):
# shouldn't really happen # shouldn't really happen
datestr = '(unknown date)' datestr = '(unknown date)'
else: else:

View File

@ -25,10 +25,15 @@ import socket
import string import string
import struct import struct
import sys import sys
import threading import threading
import time import time
import weakref import weakref
import six
if six.PY3:
long = lambda x: int(x)
import paramiko import paramiko
from paramiko import util from paramiko import util
from paramiko.auth_handler import AuthHandler from paramiko.auth_handler import AuthHandler
@ -374,7 +379,8 @@ class Transport (threading.Thread):
@rtype: str @rtype: str
""" """
out = '<paramiko.Transport at %s' % hex(long(id(self)) & 0xffffffffL)
out = '<paramiko.Transport at %s' % hex(long(id(self)) & long(0xffffffff))
if not self.active: if not self.active:
out += ' (unconnected)' out += ' (unconnected)'
else: else:
@ -1549,9 +1555,9 @@ class Transport (threading.Thread):
# active=True occurs before the thread is launched, to avoid a race # active=True occurs before the thread is launched, to avoid a race
_active_threads.append(self) _active_threads.append(self)
if self.server_mode: if self.server_mode:
self._log(DEBUG, 'starting thread (server mode): %s' % hex(long(id(self)) & 0xffffffffL)) self._log(DEBUG, 'starting thread (server mode): %s' % hex(long(id(self)) & long(0xffffffff)))
else: else:
self._log(DEBUG, 'starting thread (client mode): %s' % hex(long(id(self)) & 0xffffffffL)) self._log(DEBUG, 'starting thread (client mode): %s' % hex(long(id(self)) & long(0xffffffff)))
try: try:
try: try:
self.packetizer.write_all(self.local_version + '\r\n') self.packetizer.write_all(self.local_version + '\r\n')

View File

@ -25,7 +25,7 @@ from __future__ import generators
import array import array
from binascii import hexlify, unhexlify from binascii import hexlify, unhexlify
import errno import errno
import sys
import struct import struct
import traceback import traceback
import threading import threading
@ -33,6 +33,9 @@ import threading
from paramiko.common import * from paramiko.common import *
from paramiko.config import SSHConfig from paramiko.config import SSHConfig
import six
if six.PY3:
long = lambda x: int(x)
# Change by RogerB - python < 2.3 doesn't have enumerate so we implement it # Change by RogerB - python < 2.3 doesn't have enumerate so we implement it
if sys.version_info < (2,3): if sys.version_info < (2,3):
@ -48,7 +51,7 @@ if sys.version_info < (2,3):
def inflate_long(s, always_positive=False): def inflate_long(s, always_positive=False):
"turns a normalized byte string into a long-int (adapted from Crypto.Util.number)" "turns a normalized byte string into a long-int (adapted from Crypto.Util.number)"
out = 0L out = 0
negative = 0 negative = 0
if not always_positive and (len(s) > 0) and (ord(s[0]) >= 0x80): if not always_positive and (len(s) > 0) and (ord(s[0]) >= 0x80):
negative = 1 negative = 1
@ -60,7 +63,7 @@ def inflate_long(s, always_positive=False):
for i in range(0, len(s), 4): for i in range(0, len(s), 4):
out = (out << 32) + struct.unpack('>I', s[i:i+4])[0] out = (out << 32) + struct.unpack('>I', s[i:i+4])[0]
if negative: if negative:
out -= (1L << (8 * len(s))) out -= (long(1) << (8 * len(s)))
return out return out
def deflate_long(n, add_sign_padding=True): def deflate_long(n, add_sign_padding=True):
@ -69,7 +72,7 @@ def deflate_long(n, add_sign_padding=True):
s = '' s = ''
n = long(n) n = long(n)
while (n != 0) and (n != -1): while (n != 0) and (n != -1):
s = struct.pack('>I', n & 0xffffffffL) + s s = struct.pack('>I', n & long(0xffffffff)) + s
n = n >> 32 n = n >> 32
# strip off leading zeros, FFs # strip off leading zeros, FFs
for i in enumerate(s): for i in enumerate(s):
@ -282,7 +285,7 @@ def retry_on_signal(function):
class Counter (object): class Counter (object):
"""Stateful counter for CTR mode crypto""" """Stateful counter for CTR mode crypto"""
def __init__(self, nbits, initial_value=1L, overflow=0L): def __init__(self, nbits, initial_value=long(1), overflow=0):
self.blocksize = nbits / 8 self.blocksize = nbits / 8
self.overflow = overflow self.overflow = overflow
# start with value - 1 so we don't have to store intermediate values when counting # start with value - 1 so we don't have to store intermediate values when counting
@ -306,6 +309,6 @@ class Counter (object):
self.value = array.array('c', '\x00' * (self.blocksize - len(x)) + x) self.value = array.array('c', '\x00' * (self.blocksize - len(x)) + x)
return self.value.tostring() return self.value.tostring()
def new(cls, nbits, initial_value=1L, overflow=0L): def new(cls, nbits, initial_value=long(1), overflow=0):
return cls(nbits, initial_value=initial_value, overflow=overflow) return cls(nbits, initial_value=initial_value, overflow=overflow)
new = classmethod(new) new = classmethod(new)

View File

@ -27,6 +27,10 @@ from paramiko.kex_group1 import KexGroup1
from paramiko.kex_gex import KexGex from paramiko.kex_gex import KexGex
from paramiko import Message from paramiko import Message
import six
if six.PY3:
long = lambda x: int(x)
class FakeRng (object): class FakeRng (object):
def read(self, n): def read(self, n):
@ -41,7 +45,7 @@ class FakeKey (object):
class FakeModulusPack (object): class FakeModulusPack (object):
P = 0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFFL P = long(0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF)
G = 2 G = 2
def get_modulus(self, min, ask, max): def get_modulus(self, min, ask, max):
return self.G, self.P return self.G, self.P
@ -75,7 +79,7 @@ class FakeTransport (object):
class KexTest (unittest.TestCase): class KexTest (unittest.TestCase):
K = 14730343317708716439807310032871972459448364195094179797249681733965528989482751523943515690110179031004049109375612685505881911274101441415545039654102474376472240501616988799699744135291070488314748284283496055223852115360852283821334858541043710301057312858051901453919067023103730011648890038847384890504L K = long(14730343317708716439807310032871972459448364195094179797249681733965528989482751523943515690110179031004049109375612685505881911274101441415545039654102474376472240501616988799699744135291070488314748284283496055223852115360852283821334858541043710301057312858051901453919067023103730011648890038847384890504)
def setUp(self): def setUp(self):
pass pass
@ -204,7 +208,7 @@ class KexTest (unittest.TestCase):
msg.add_mpint(12345) msg.add_mpint(12345)
msg.rewind() msg.rewind()
kex.parse_next(paramiko.kex_gex._MSG_KEXDH_GEX_INIT, msg) kex.parse_next(paramiko.kex_gex._MSG_KEXDH_GEX_INIT, msg)
K = 67592995013596137876033460028393339951879041140378510871612128162185209509220726296697886624612526735888348020498716482757677848959420073720160491114319163078862905400020959196386947926388406687288901564192071077389283980347784184487280885335302632305026248574716290537036069329724382811853044654824945750581L K = long(67592995013596137876033460028393339951879041140378510871612128162185209509220726296697886624612526735888348020498716482757677848959420073720160491114319163078862905400020959196386947926388406687288901564192071077389283980347784184487280885335302632305026248574716290537036069329724382811853044654824945750581)
H = 'CE754197C21BF3452863B4F44D0B3951F12516EF' H = 'CE754197C21BF3452863B4F44D0B3951F12516EF'
x = '210000000866616B652D6B6579000000807E2DDB1743F3487D6545F04F1C8476092FB912B013626AB5BCEB764257D88BBA64243B9F348DF7B41B8C814A995E00299913503456983FFB9178D3CD79EB6D55522418A8ABF65375872E55938AB99A84A0B5FC8A1ECC66A7C3766E7E0F80B7CE2C9225FC2DD683F4764244B72963BBB383F529DCF0C5D17740B8A2ADBE9208D40000000866616B652D736967' x = '210000000866616B652D6B6579000000807E2DDB1743F3487D6545F04F1C8476092FB912B013626AB5BCEB764257D88BBA64243B9F348DF7B41B8C814A995E00299913503456983FFB9178D3CD79EB6D55522418A8ABF65375872E55938AB99A84A0B5FC8A1ECC66A7C3766E7E0F80B7CE2C9225FC2DD683F4764244B72963BBB383F529DCF0C5D17740B8A2ADBE9208D40000000866616B652D736967'
self.assertEquals(K, transport._K) self.assertEquals(K, transport._K)
@ -231,7 +235,7 @@ class KexTest (unittest.TestCase):
msg.add_mpint(12345) msg.add_mpint(12345)
msg.rewind() msg.rewind()
kex.parse_next(paramiko.kex_gex._MSG_KEXDH_GEX_INIT, msg) kex.parse_next(paramiko.kex_gex._MSG_KEXDH_GEX_INIT, msg)
K = 67592995013596137876033460028393339951879041140378510871612128162185209509220726296697886624612526735888348020498716482757677848959420073720160491114319163078862905400020959196386947926388406687288901564192071077389283980347784184487280885335302632305026248574716290537036069329724382811853044654824945750581L K = long(67592995013596137876033460028393339951879041140378510871612128162185209509220726296697886624612526735888348020498716482757677848959420073720160491114319163078862905400020959196386947926388406687288901564192071077389283980347784184487280885335302632305026248574716290537036069329724382811853044654824945750581)
H = 'B41A06B2E59043CEFC1AE16EC31F1E2D12EC455B' H = 'B41A06B2E59043CEFC1AE16EC31F1E2D12EC455B'
x = '210000000866616B652D6B6579000000807E2DDB1743F3487D6545F04F1C8476092FB912B013626AB5BCEB764257D88BBA64243B9F348DF7B41B8C814A995E00299913503456983FFB9178D3CD79EB6D55522418A8ABF65375872E55938AB99A84A0B5FC8A1ECC66A7C3766E7E0F80B7CE2C9225FC2DD683F4764244B72963BBB383F529DCF0C5D17740B8A2ADBE9208D40000000866616B652D736967' x = '210000000866616B652D6B6579000000807E2DDB1743F3487D6545F04F1C8476092FB912B013626AB5BCEB764257D88BBA64243B9F348DF7B41B8C814A995E00299913503456983FFB9178D3CD79EB6D55522418A8ABF65375872E55938AB99A84A0B5FC8A1ECC66A7C3766E7E0F80B7CE2C9225FC2DD683F4764244B72963BBB383F529DCF0C5D17740B8A2ADBE9208D40000000866616B652D736967'
self.assertEquals(K, transport._K) self.assertEquals(K, transport._K)

View File

@ -20,6 +20,10 @@
Some unit tests for ssh protocol message blocks. Some unit tests for ssh protocol message blocks.
""" """
import six
if six.PY3:
long = lambda x: int(x)
import unittest import unittest
from paramiko.message import Message from paramiko.message import Message
@ -50,10 +54,10 @@ class MessageTest (unittest.TestCase):
msg = Message() msg = Message()
msg.add_int64(5) msg.add_int64(5)
msg.add_int64(0xf5e4d3c2b109L) msg.add_int64(long(0xf5e4d3c2b109))
msg.add_mpint(17) msg.add_mpint(17)
msg.add_mpint(0xf5e4d3c2b109L) msg.add_mpint(long(0xf5e4d3c2b109))
msg.add_mpint(-0x65e4d3c2b109L) msg.add_mpint(long(-0x65e4d3c2b109))
self.assertEquals(str(msg), self.__c) self.assertEquals(str(msg), self.__c)
def test_2_decode(self): def test_2_decode(self):
@ -73,15 +77,15 @@ class MessageTest (unittest.TestCase):
msg = Message(self.__c) msg = Message(self.__c)
self.assertEquals(msg.get_int64(), 5) self.assertEquals(msg.get_int64(), 5)
self.assertEquals(msg.get_int64(), 0xf5e4d3c2b109L) self.assertEquals(msg.get_int64(), long(0xf5e4d3c2b109))
self.assertEquals(msg.get_mpint(), 17) self.assertEquals(msg.get_mpint(), 17)
self.assertEquals(msg.get_mpint(), 0xf5e4d3c2b109L) self.assertEquals(msg.get_mpint(), long(0xf5e4d3c2b109))
self.assertEquals(msg.get_mpint(), -0x65e4d3c2b109L) self.assertEquals(msg.get_mpint(), long(-0x65e4d3c2b109))
def test_3_add(self): def test_3_add(self):
msg = Message() msg = Message()
msg.add(5) msg.add(5)
msg.add(0x1122334455L) msg.add(long(0x1122334455))
msg.add(True) msg.add(True)
msg.add('cat') msg.add('cat')
msg.add(['a', 'b']) msg.add(['a', 'b'])
@ -90,7 +94,7 @@ class MessageTest (unittest.TestCase):
def test_4_misc(self): def test_4_misc(self):
msg = Message(self.__d) msg = Message(self.__d)
self.assertEquals(msg.get_int(), 5) self.assertEquals(msg.get_int(), 5)
self.assertEquals(msg.get_mpint(), 0x1122334455L) self.assertEquals(msg.get_mpint(), long(0x1122334455))
self.assertEquals(msg.get_so_far(), self.__d[:13]) self.assertEquals(msg.get_so_far(), self.__d[:13])
self.assertEquals(msg.get_remainder(), self.__d[13:]) self.assertEquals(msg.get_remainder(), self.__d[13:])
msg.rewind() msg.rewind()

View File

@ -29,6 +29,10 @@ import threading
import unittest import unittest
import random import random
import six
if six.PY3:
long = lambda x: int(x)
from paramiko import Transport, SecurityOptions, ServerInterface, RSAKey, DSSKey, \ from paramiko import Transport, SecurityOptions, ServerInterface, RSAKey, DSSKey, \
SSHException, BadAuthenticationType, InteractiveQuery, ChannelException SSHException, BadAuthenticationType, InteractiveQuery, ChannelException
from paramiko import AUTH_FAILED, AUTH_PARTIALLY_SUCCESSFUL, AUTH_SUCCESSFUL from paramiko import AUTH_FAILED, AUTH_PARTIALLY_SUCCESSFUL, AUTH_SUCCESSFUL
@ -158,7 +162,7 @@ class TransportTest(ParamikoTest):
pass pass
def test_2_compute_key(self): def test_2_compute_key(self):
self.tc.K = 123281095979686581523377256114209720774539068973101330872763622971399429481072519713536292772709507296759612401802191955568143056534122385270077606457721553469730659233569339356140085284052436697480759510519672848743794433460113118986816826624865291116513647975790797391795651716378444844877749505443714557929L self.tc.K = long(123281095979686581523377256114209720774539068973101330872763622971399429481072519713536292772709507296759612401802191955568143056534122385270077606457721553469730659233569339356140085284052436697480759510519672848743794433460113118986816826624865291116513647975790797391795651716378444844877749505443714557929)
self.tc.H = unhexlify('0C8307CDE6856FF30BA93684EB0F04C2520E9ED3') self.tc.H = unhexlify('0C8307CDE6856FF30BA93684EB0F04C2520E9ED3')
self.tc.session_id = self.tc.H self.tc.session_id = self.tc.H
key = self.tc._compute_key('C', 32) key = self.tc._compute_key('C', 32)