Fix message to handle long properly, even on Py3

This commit is contained in:
Scott Maxwell 2013-11-02 20:19:52 -07:00
parent 7444a99993
commit dc58b7bcb2
4 changed files with 10 additions and 9 deletions

View File

@ -170,7 +170,7 @@ class Message (object):
@return: a 64-bit unsigned integer. @return: a 64-bit unsigned integer.
@rtype: long @rtype: long
""" """
return self.get_int() return struct.unpack('>Q', self.get_bytes(8))[0]
def get_mpint(self): def get_mpint(self):
""" """
@ -289,7 +289,8 @@ class Message (object):
@param n: long int to add @param n: long int to add
@type n: long @type n: long
""" """
return self.add_int(n) self.packet.write(struct.pack('>Q', n))
return self
def add_mpint(self, z): def add_mpint(self, z):
""" """

View File

@ -695,10 +695,10 @@ class SFTPClient (BaseSFTP):
msg = Message() msg = Message()
msg.add_int(self.request_number) msg.add_int(self.request_number)
for item in arg: for item in arg:
if isinstance(item, int): if isinstance(item, long):
msg.add_int(item)
elif isinstance(item, long):
msg.add_int64(item) msg.add_int64(item)
elif isinstance(item, int):
msg.add_int(item)
elif isinstance(item, (string_types, bytes_types)): elif isinstance(item, (string_types, bytes_types)):
msg.add_string(item) msg.add_string(item)
elif isinstance(item, SFTPAttributes): elif isinstance(item, SFTPAttributes):

View File

@ -183,10 +183,10 @@ class SFTPServer (BaseSFTP, SubsystemHandler):
msg = Message() msg = Message()
msg.add_int(request_number) msg.add_int(request_number)
for item in arg: for item in arg:
if type(item) is int: if isinstance(item, long):
msg.add_int(item)
elif type(item) is long:
msg.add_int64(item) msg.add_int64(item)
elif isinstance(item, int):
msg.add_int(item)
elif isinstance(item, (string_types, bytes_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:

View File

@ -30,7 +30,7 @@ class MessageTest (unittest.TestCase):
__a = unhexlify(b('000000170760e09000000001710000000568656c6c6f000003e8')) + (b('x') * 1000) __a = unhexlify(b('000000170760e09000000001710000000568656c6c6f000003e8')) + (b('x') * 1000)
__b = unhexlify(b('0100f3003f00000010687565792c64657765792c6c6f756965')) __b = unhexlify(b('0100f3003f00000010687565792c64657765792c6c6f756965'))
__c = unhexlify(b('00000005ff0000000700f5e4d3c2b10900000001110000000700f5e4d3c2b109000000069a1b2c3d4ef7')) __c = unhexlify(b('00000000000000050000f5e4d3c2b10900000001110000000700f5e4d3c2b109000000069a1b2c3d4ef7'))
__d = unhexlify(b('00000005ff000000051122334455ff0000000a00f00000000000000000010000000363617400000003612c62')) __d = unhexlify(b('00000005ff000000051122334455ff0000000a00f00000000000000000010000000363617400000003612c62'))
def test_1_encode(self): def test_1_encode(self):