mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-06-30 19:12:48 -05:00
Migrate all Python scripts to Python 3.
Most of them are now _mandatory_ P3 scripts, because I'm tired of maintaining everything to be compatible with both versions. The current exceptions are gdb.py (which has to live with whatever gdb gives it), and kh2reg.py (which is actually designed for other people to use, and some of them might still be stuck on P2 for the moment).
This commit is contained in:
@ -10,6 +10,8 @@ import collections
|
||||
from ssh import *
|
||||
import agenttestdata
|
||||
|
||||
assert sys.version_info[:2] >= (3,0), "This is Python 3 code"
|
||||
|
||||
test_session_id = b'Test16ByteSessId'
|
||||
assert len(test_session_id) == 16
|
||||
test_message_to_sign = b'test message to sign'
|
||||
|
@ -1,5 +1,9 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import sys
|
||||
|
||||
assert sys.version_info[:2] >= (3,0), "This is Python 3 code"
|
||||
|
||||
def generate():
|
||||
import hashlib
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import sys
|
||||
import unittest
|
||||
import struct
|
||||
import itertools
|
||||
@ -17,6 +18,8 @@ from eccref import *
|
||||
from testcrypt import *
|
||||
from ssh import *
|
||||
|
||||
assert sys.version_info[:2] >= (3,0), "This is Python 3 code"
|
||||
|
||||
try:
|
||||
base64decode = base64.decodebytes
|
||||
except AttributeError:
|
||||
@ -90,7 +93,7 @@ def last(iterable):
|
||||
def queued_random_data(nbytes, seed):
|
||||
hashsize = 512 // 8
|
||||
data = b''.join(
|
||||
hashlib.sha512(unicode_to_bytes("preimage:{:d}:{}".format(i, seed)))
|
||||
hashlib.sha512("preimage:{:d}:{}".format(i, seed).encode('ascii'))
|
||||
.digest() for i in range((nbytes + hashsize - 1) // hashsize))
|
||||
data = data[:nbytes]
|
||||
random_queue(data)
|
||||
@ -199,9 +202,9 @@ class mpint(MyTestBase):
|
||||
n = mp_from_hex(hexstr)
|
||||
i = int(hexstr, 16)
|
||||
self.assertEqual(mp_get_hex(n),
|
||||
unicode_to_bytes("{:x}".format(i)))
|
||||
"{:x}".format(i).encode('ascii'))
|
||||
self.assertEqual(mp_get_hex_uppercase(n),
|
||||
unicode_to_bytes("{:X}".format(i)))
|
||||
"{:X}".format(i).encode('ascii'))
|
||||
checkHex("0")
|
||||
checkHex("f")
|
||||
checkHex("00000000000000000000000000000000000000000000000000")
|
||||
@ -212,7 +215,7 @@ class mpint(MyTestBase):
|
||||
n = mp_from_hex(hexstr)
|
||||
i = int(hexstr, 16)
|
||||
self.assertEqual(mp_get_decimal(n),
|
||||
unicode_to_bytes("{:d}".format(i)))
|
||||
"{:d}".format(i).encode('ascii'))
|
||||
checkDec("0")
|
||||
checkDec("f")
|
||||
checkDec("00000000000000000000000000000000000000000000000000")
|
||||
@ -1857,10 +1860,10 @@ culpa qui officia deserunt mollit anim id est laborum.
|
||||
# both parts. Other than that, we don't do much to
|
||||
# make this a rigorous cryptographic test.
|
||||
for n, d in [(1,3),(2,3)]:
|
||||
sigbytes = list(bytevals(sigblob))
|
||||
sigbytes = list(sigblob)
|
||||
bit = 8 * len(sigbytes) * n // d
|
||||
sigbytes[bit // 8] ^= 1 << (bit % 8)
|
||||
badsig = valbytes(sigbytes)
|
||||
badsig = bytes(sigbytes)
|
||||
for key in [pubkey, privkey, privkey2]:
|
||||
self.assertFalse(ssh_key_verify(
|
||||
key, badsig, test_message))
|
||||
|
@ -1,4 +1,4 @@
|
||||
#!/usr/bin/env python
|
||||
#!/usr/bin/env python3
|
||||
|
||||
# Reference implementation of DES.
|
||||
#
|
||||
@ -15,6 +15,8 @@ import struct
|
||||
import functools
|
||||
import argparse
|
||||
|
||||
assert sys.version_info[:2] >= (3,0), "This is Python 3 code"
|
||||
|
||||
def bitor(x, y):
|
||||
return x | y
|
||||
def split_words(val, width=32):
|
||||
|
@ -1,6 +1,9 @@
|
||||
import sys
|
||||
import numbers
|
||||
import itertools
|
||||
|
||||
assert sys.version_info[:2] >= (3,0), "This is Python 3 code"
|
||||
|
||||
from numbertheory import *
|
||||
|
||||
class AffinePoint(object):
|
||||
|
@ -1,7 +1,10 @@
|
||||
import sys
|
||||
import numbers
|
||||
import itertools
|
||||
import unittest
|
||||
|
||||
assert sys.version_info[:2] >= (3,0), "This is Python 3 code"
|
||||
|
||||
def invert(a, b):
|
||||
"Multiplicative inverse of a mod b. a,b must be coprime."
|
||||
A = (a, 1, 0)
|
||||
|
@ -1,6 +1,9 @@
|
||||
import sys
|
||||
import struct
|
||||
import itertools
|
||||
|
||||
assert sys.version_info[:2] >= (3,0), "This is Python 3 code"
|
||||
|
||||
def nbits(n):
|
||||
# Mimic mp_get_nbits for ordinary Python integers.
|
||||
assert 0 <= n
|
||||
|
@ -6,21 +6,14 @@ import re
|
||||
import struct
|
||||
from binascii import hexlify
|
||||
|
||||
assert sys.version_info[:2] >= (3,0), "This is Python 3 code"
|
||||
|
||||
# Expect to be run from the 'test' subdirectory, one level down from
|
||||
# the main source
|
||||
putty_srcdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
def unicode_to_bytes(arg):
|
||||
# Slightly fiddly way to do this which should work in Python 2 and 3
|
||||
if isinstance(arg, type(u'a')) and not isinstance(arg, type(b'a')):
|
||||
arg = arg.encode("UTF-8")
|
||||
return arg
|
||||
|
||||
def bytevals(b):
|
||||
return struct.unpack("{:d}B".format(len(b)), b)
|
||||
def valbytes(b):
|
||||
b = list(b)
|
||||
return struct.pack("{:d}B".format(len(b)), *b)
|
||||
def coerce_to_bytes(arg):
|
||||
return arg.encode("UTF-8") if isinstance(arg, str) else arg
|
||||
|
||||
class ChildProcessFailure(Exception):
|
||||
pass
|
||||
@ -75,8 +68,8 @@ class ChildProcess(object):
|
||||
if self.sp is None:
|
||||
assert self.exitstatus is None
|
||||
self.start()
|
||||
self.write_line(unicode_to_bytes(cmd) + b" " + b" ".join(
|
||||
unicode_to_bytes(arg) for arg in args))
|
||||
self.write_line(coerce_to_bytes(cmd) + b" " + b" ".join(
|
||||
coerce_to_bytes(arg) for arg in args))
|
||||
argcount = int(self.read_line())
|
||||
return [self.read_line() for arg in range(argcount)]
|
||||
def wait_for_exit(self):
|
||||
@ -154,11 +147,10 @@ def make_argword(arg, argtype, fnname, argindex, to_preserve):
|
||||
return "NULL"
|
||||
typename = typename[4:]
|
||||
if typename == "val_string":
|
||||
arg = unicode_to_bytes(arg)
|
||||
arg = coerce_to_bytes(arg)
|
||||
if isinstance(arg, bytes):
|
||||
retwords = childprocess.funcall(
|
||||
"newstring", ["".join("%{:02x}".format(b)
|
||||
for b in bytevals(arg))])
|
||||
"newstring", ["".join("%{:02x}".format(b) for b in arg)])
|
||||
arg = make_retvals([typename], retwords, unpack_strings=False)[0]
|
||||
to_preserve.append(arg)
|
||||
if typename == "val_mpint" and isinstance(arg, numbers.Integral):
|
||||
@ -179,7 +171,7 @@ def make_argword(arg, argtype, fnname, argindex, to_preserve):
|
||||
if typename in {
|
||||
"hashalg", "macalg", "keyalg", "cipheralg",
|
||||
"dh_group", "ecdh_alg", "rsaorder", "primegenpolicy"}:
|
||||
arg = unicode_to_bytes(arg)
|
||||
arg = coerce_to_bytes(arg)
|
||||
if isinstance(arg, bytes) and b" " not in arg:
|
||||
return arg
|
||||
if typename == "mpint_list":
|
||||
@ -188,7 +180,7 @@ def make_argword(arg, argtype, fnname, argindex, to_preserve):
|
||||
for val in arg:
|
||||
sublist.append(make_argword(val, ("val_mpint", False),
|
||||
fnname, argindex, to_preserve))
|
||||
return b" ".join(unicode_to_bytes(sub) for sub in sublist)
|
||||
return b" ".join(coerce_to_bytes(sub) for sub in sublist)
|
||||
raise TypeError(
|
||||
"Can't convert {}() argument {:d} to {} (value was {!r})".format(
|
||||
fnname, argindex, typename, arg))
|
||||
@ -197,7 +189,7 @@ def unpack_string(identifier):
|
||||
retwords = childprocess.funcall("getstring", [identifier])
|
||||
childprocess.funcall("free", [identifier])
|
||||
return re.sub(b"%[0-9A-F][0-9A-F]",
|
||||
lambda m: valbytes([int(m.group(0)[1:], 16)]),
|
||||
lambda m: bytes([int(m.group(0)[1:], 16)]),
|
||||
retwords[0])
|
||||
|
||||
def unpack_mp(identifier):
|
||||
|
Reference in New Issue
Block a user