From 0170dfe5fc97f48a32f662fd7123096940eecc9a Mon Sep 17 00:00:00 2001
From: Dan Callaghan <dcallagh@redhat.com>
Date: Mon, 27 Aug 2018 08:20:39 +1000
Subject: [PATCH 1/2] port to Python 3
These changes are unfortunately not compatible with Python 2 because of
the differences between string.maketrans (Python 2) and str.maketrans
(Python 3). The former does not accept deletechars whereas the latter
does.
---
setup.py | 2 +-
zbase32/__init__.py | 2 +-
zbase32/test/test_zbase32.py | 44 ++++++++---------
zbase32/zbase32.py | 91 +++++++++++++++++++-----------------
zbase32/zbase32id.py | 18 ++++---
5 files changed, 79 insertions(+), 78 deletions(-)
diff --git a/setup.py b/setup.py
index 1ede56d..e004253 100644
--- a/setup.py
+++ b/setup.py
@@ -49,7 +49,7 @@
if mo:
verstr = mo.group(1)
else:
- print "unable to find version in %s" % (VERSIONFILE,)
+ print("unable to find version in %s" % (VERSIONFILE,))
raise RuntimeError("if %s.py exists, it must be well-formed" % (VERSIONFILE,))
# darcsver is needed only if you want "./setup.py darcsver" to write a new
diff --git a/zbase32/__init__.py b/zbase32/__init__.py
index b4990a7..381c557 100644
--- a/zbase32/__init__.py
+++ b/zbase32/__init__.py
@@ -1 +1 @@
-from zbase32 import *
+from zbase32.zbase32 import *
diff --git a/zbase32/test/test_zbase32.py b/zbase32/test/test_zbase32.py
index 9ab9299..31dead4 100644
--- a/zbase32/test/test_zbase32.py
+++ b/zbase32/test/test_zbase32.py
@@ -22,19 +22,19 @@ def test_ende(self):
bs = insecurerandstr(2**3)
asciis=b2a(bs)
bs2=a2b(asciis)
- assert bs2 == bs, "bs2: %s, bs: %s" % (`bs2`, `bs`,)
+ assert bs2 == bs, "bs2: %r, bs: %r" % (bs2, bs,)
def test_ende_long(self):
bs = insecurerandstr(2**3)
asciis=b2a_long(bs)
bs2=a2b_long(asciis)
- assert bs2 == bs, "bs2: %s, bs: %s" % (`bs2`, `bs`,)
+ assert bs2 == bs, "bs2: %r, bs: %r" % (bs2, bs,)
def test_both(self):
bs = insecurerandstr(2**3)
asciis=b2a(bs)
asciisl=b2a_long(bs)
- assert asciis == asciisl, "asciis: %s, asciisl: %s" % (`asciis`, `asciisl`,)
+ assert asciis == asciisl, "asciis: %r, asciisl: %r" % (asciis, asciisl,)
asciis=b2a(bs)
bs2=a2b(asciis)
bs2l=a2b_long(asciis)
@@ -45,7 +45,7 @@ def test_big(self):
bs = insecurerandstr(2**9)
asciis=b2a(bs)
asciisl=b2a_long(bs)
- assert asciis == asciisl, "asciis: %s, asciisl: %s" % (`asciis`, `asciisl`,)
+ assert asciis == asciisl, "asciis: %r, asciisl: %r" % (asciis, asciisl,)
asciis=b2a(bs)
bs2=a2b(asciis)
bs2l=a2b_long(asciis)
@@ -60,21 +60,21 @@ def DISABLED_test_odd_sizes_violates_preconditions(self):
lib = random.randrange(1, 2**8)
bs = insecurerandstr(random.randrange(1, 2**5))
asciis = b2a_l(bs, lib)
- assert len(asciis) == (lib+4)/5 # the size of the base-32 encoding must be just right
+ assert len(asciis) == (lib+4)//5 # the size of the base-32 encoding must be just right
asciisl = b2a_l_long(bs, lib)
- assert len(asciisl) == (lib+4)/5 # the size of the base-32 encoding must be just right
+ assert len(asciisl) == (lib+4)//5 # the size of the base-32 encoding must be just right
assert asciis == asciisl
bs2 = a2b_l(asciis, lib)
- assert len(bs2) == (lib+7)/8 # the size of the result must be just right
+ assert len(bs2) == (lib+7)//8 # the size of the result must be just right
bs2l = a2b_l_long(asciis, lib)
- assert len(bs2l) == (lib+7)/8 # the size of the result must be just right
+ assert len(bs2l) == (lib+7)//8 # the size of the result must be just right
assert bs2 == bs2l
- assert trimnpad(bs, lib) == bs2, "trimnpad(%s, %s): %s, bs2: %s" % (`bs`, lib, `trimnpad(bs, lib)`, `bs2`,)
+ assert trimnpad(bs, lib) == bs2, "trimnpad(%r, %r): %r, bs2: %r" % (bs, lib, trimnpad(bs, lib), bs2,)
def test_odd_sizes(self):
for j in range(2**6):
lib = random.randrange(1, 2**8)
- bs = insecurerandstr((lib+7)/8)
+ bs = insecurerandstr((lib+7)//8)
# zero-out unused least-sig bits
if lib%8:
b=ord(bs[-1])
@@ -82,31 +82,31 @@ def test_odd_sizes(self):
b = b << (8 - (lib%8))
bs = bs[:-1] + chr(b)
asciis = b2a_l(bs, lib)
- assert len(asciis) == (lib+4)/5 # the size of the base-32 encoding must be just right
+ assert len(asciis) == (lib+4)//5 # the size of the base-32 encoding must be just right
asciisl = b2a_l_long(bs, lib)
- assert len(asciisl) == (lib+4)/5 # the size of the base-32 encoding must be just right
+ assert len(asciisl) == (lib+4)//5 # the size of the base-32 encoding must be just right
assert asciis == asciisl
bs2 = a2b_l(asciis, lib)
- assert len(bs2) == (lib+7)/8 # the size of the result must be just right
+ assert len(bs2) == (lib+7)//8 # the size of the result must be just right
bs2l = a2b_l_long(asciis, lib)
- assert len(bs2l) == (lib+7)/8 # the size of the result must be just right
+ assert len(bs2l) == (lib+7)//8 # the size of the result must be just right
assert bs2 == bs2l
- assert trimnpad(bs, lib) == bs2, "trimnpad(%s, %s): %s, bs2: %s" % (`bs`, lib, `trimnpad(bs, lib)`, `bs2`,)
+ assert trimnpad(bs, lib) == bs2, "trimnpad(%r, %r): %r, bs2: %r" % (bs, lib, trimnpad(bs, lib), bs2,)
def test_n8mo4_is_an_encoding(self):
- self.failUnless(could_be_base32_encoded_l('n8mo4', 25))
+ self.assertTrue(could_be_base32_encoded_l('n8mo4', 25))
def test_could_be(self):
# base-32 encoded strings could be
for j in range(2**9):
rands = insecurerandstr(random.randrange(1, 2**7))
randsenc = b2a(rands)
- assert could_be_base32_encoded(randsenc), "rands: %s, randsenc: %s, a2b(randsenc): %s" % (`rands`, `randsenc`, `a2b(randsenc)`,)
+ assert could_be_base32_encoded(randsenc), "rands: %r, randsenc: %r, a2b(randsenc): %r" % (rands, randsenc, a2b(randsenc),)
# base-32 encoded strings with unusual bit lengths could be, too
for j in range(2**9):
bitl = random.randrange(1, 2**7)
- bs = insecurerandstr((bitl+7)/8)
+ bs = insecurerandstr((bitl+7)//8)
# zero-out unused least-sig bits
if bitl%8:
b=ord(bs[-1])
@@ -137,13 +137,13 @@ def _help_bench_ed_l(N):
def benchem():
import benchfunc # from pyutil
- print "e: "
+ print("e: ")
benchfunc.bench(_help_bench_e, TOPXP=13)
- print "ed: "
+ print("ed: ")
benchfunc.bench(_help_bench_ed, TOPXP=13)
- print "e_l: "
+ print("e_l: ")
benchfunc.bench(_help_bench_e_l, TOPXP=13)
- print "ed_l: "
+ print("ed_l: ")
benchfunc.bench(_help_bench_ed_l, TOPXP=13)
def suite():
diff --git a/zbase32/zbase32.py b/zbase32/zbase32.py
index 98aa435..c840882 100644
--- a/zbase32/zbase32.py
+++ b/zbase32/zbase32.py
@@ -6,6 +6,8 @@
# deal in this work without restriction (including the rights to use, modify,
# distribute, sublicense, and/or sell copies).
+from __future__ import print_function
+
# from the Python Standard Library
import string
@@ -49,9 +51,9 @@
chars = mnet32_alphabet
vals = ''.join([chr(i) for i in range(32)])
-c2vtranstable = string.maketrans(chars, vals)
-v2ctranstable = string.maketrans(vals, chars)
-identitytranstable = string.maketrans(chars, chars)
+c2vtranstable = str.maketrans(chars, vals)
+v2ctranstable = str.maketrans(vals, chars)
+identitytranstable = str.maketrans(chars, chars)
def _get_trailing_chars_without_lsbs(N, d):
"""
@@ -62,7 +64,7 @@ def _get_trailing_chars_without_lsbs(N, d):
s.extend(_get_trailing_chars_without_lsbs(N+1, d=d))
i = 0
while i < len(chars):
- if not d.has_key(i):
+ if i not in d:
d[i] = None
s.append(chars[i])
i = i + 2**N
@@ -76,19 +78,19 @@ def get_trailing_chars_without_lsbs(N):
return ''.join(_get_trailing_chars_without_lsbs(N, d=d))
def print_trailing_chars_without_lsbs(N):
- print get_trailing_chars_without_lsbs(N)
+ print(get_trailing_chars_without_lsbs(N))
def print_trailing_chars():
N = 4
while N >= 0:
- print "%2d" % N + ": ",
+ print("%2d" % N + ": ", end='')
print_trailing_chars_without_lsbs(N)
N = N - 1
-upcasetranstable = string.maketrans(string.ascii_lowercase, string.ascii_uppercase)
-digitchars = string.translate(chars, identitytranstable, string.ascii_lowercase)
-def add_upcase(s, upcasetranstable=upcasetranstable, digitchars=digitchars):
- return s + string.translate(s, upcasetranstable, digitchars)
+digitchars = chars.translate(str.maketrans('', '', string.ascii_lowercase))
+upcasetranstable = str.maketrans(string.ascii_lowercase, string.ascii_uppercase, digitchars)
+def add_upcase(s, upcasetranstable=upcasetranstable):
+ return s + s.translate(upcasetranstable)
def b2a(os):
"""
@@ -122,7 +124,7 @@ def b2a_l(os, lengthinbits):
@return the contents of os in base-32 encoded form
"""
- precondition(isinstance(lengthinbits, (int, long,)), "lengthinbits is required to be an integer.", lengthinbits=lengthinbits)
+ precondition(isinstance(lengthinbits, int), "lengthinbits is required to be an integer.", lengthinbits=lengthinbits)
precondition(div_ceil(lengthinbits, 8) == len(os), "lengthinbits is required to specify a number of bits storable in exactly len(os) octets.", lengthinbits=lengthinbits, lenos=len(os))
# precondition((lengthinbits % 8==0) or ((ord(os[-1]) % (2**(8-(lengthinbits%8))))==0), "Any unused least-significant bits in os are required to be zero bits.", ord(os[-1]), lengthinbits=lengthinbits) # removing this precondition, because I like to use it with random os, like this: base32.b2a_l(file("/dev/urandom", "r").read(9), 65)
@@ -152,19 +154,19 @@ def b2a_l(os, lengthinbits):
cutoff = 256
continue
cutoff = cutoff * 8
- quintet = num / cutoff
+ quintet = num // cutoff
quintets.append(quintet)
num = num - (quintet * cutoff)
- cutoff = cutoff / 32
- quintet = num / cutoff
+ cutoff = cutoff // 32
+ quintet = num // cutoff
quintets.append(quintet)
num = num - (quintet * cutoff)
if len(quintets) > numquintets:
assert len(quintets) == (numquintets+1), "len(quintets): %s, numquintets: %s, quintets: %s" % (len(quintets), numquintets, quintets,)
quintets = quintets[:numquintets]
- res = string.translate(''.join([chr(q) for q in quintets]), v2ctranstable)
+ res = ''.join([chr(q) for q in quintets]).translate(v2ctranstable)
assert could_be_base32_encoded_l(res, lengthinbits), "lengthinbits: %s, res: %s" % (lengthinbits, res,)
return res
@@ -186,7 +188,7 @@ def b2a_l(os, lengthinbits):
def num_octets_that_encode_to_this_many_quintets(numqs):
# Here is a computation that conveniently expresses this:
- return (numqs*5+3)/8
+ return (numqs*5+3)//8
def a2b(cs):
"""
@@ -212,7 +214,7 @@ def a2b_l(cs, lengthinbits):
"""
precondition(could_be_base32_encoded_l(cs, lengthinbits), "cs is required to be possibly base32 encoded data.", cs=cs, lengthinbits=lengthinbits)
- qs = [ord(v) for v in string.translate(cs, c2vtranstable)]
+ qs = [ord(v) for v in cs.translate(c2vtranstable)]
numoctets = div_ceil(lengthinbits, 8)
numquintetsofdata = div_ceil(lengthinbits, 5)
@@ -227,10 +229,10 @@ def a2b_l(cs, lengthinbits):
i = 1
while len(octets) < numoctets:
while pos > 256:
- pos = pos / 32
+ pos = pos // 32
num = num + (qs[i] * pos)
i = i + 1
- octet = num / 256
+ octet = num // 256
octets.append(octet)
num = num - (octet * 256)
num = num * 256
@@ -292,11 +294,12 @@ def init_s5a():
return tuple(s5a)
s5a = init_s5a()
-def could_be_base32_encoded(s, s8=s8, tr=string.translate, identitytranstable=identitytranstable, chars=chars):
- return bool(s8[len(s)%8][ord(s[-1])] and not tr(s, identitytranstable, chars))
+deletechars = str.maketrans('', '', chars)
+def could_be_base32_encoded(s, s8=s8):
+ return bool(s8[len(s)%8][ord(s[-1])] and not s.translate(deletechars))
-def could_be_base32_encoded_l(s, lengthinbits, s5=s5, tr=string.translate, identitytranstable=identitytranstable, chars=chars):
- return bool((((lengthinbits+4)/5) == len(s)) and s5[lengthinbits%5][ord(s[-1])] and not string.translate(s, identitytranstable, chars))
+def could_be_base32_encoded_l(s, lengthinbits, s5=s5):
+ return bool((((lengthinbits+4)//5) == len(s)) and s5[lengthinbits%5][ord(s[-1])] and not s.translate(deletechars))
# the _long functions are 2/3 as fast as the normal ones. The _long variants are included for testing, documentation, and benchmarking purposes.
def b2a_long(os):
@@ -307,8 +310,8 @@ def b2a_l_long(os, lengthinbits):
os = [ord(o) for o in os]
- numquintets = (lengthinbits+4)/5
- numoctetsofdata = (lengthinbits+7)/8
+ numquintets = (lengthinbits+4)//5
+ numoctetsofdata = (lengthinbits+7)//8
# strip trailing octets that won't be used
del os[numoctetsofdata:]
# zero out any unused bits in the final octet
@@ -317,41 +320,41 @@ def b2a_l_long(os, lengthinbits):
os[-1] = os[-1] >> (8-(lengthinbits % 8))
os[-1] = os[-1] << (8-(lengthinbits % 8))
# append zero octets for padding if needed
- numoctetsneeded = (numquintets*5+7)/8+4 # append 4 extra zero octets so that I can read in 40 -bit (5-octet) chunks
+ numoctetsneeded = (numquintets*5+7)//8+4 # append 4 extra zero octets so that I can read in 40 -bit (5-octet) chunks
os.extend([0]*(numoctetsneeded-len(os)))
quintets = []
i = 0
- CUTOFF = 2L**35
+ CUTOFF = 2**35
while len(quintets) < numquintets:
# take the next 5 octets and turn them into 8 quintets
- num = 0L # i am a LONG! hear me roar
+ num = 0
for j in range(5):
num = num *256
num = num + os[i]
i = i + 1
for j in range(8):
- quintet = num / CUTOFF
+ quintet = num // CUTOFF
quintets.append(quintet)
num = num - (quintet * CUTOFF)
num = num * 32
quintets = quintets[:numquintets]
- res = string.translate(''.join([chr(q) for q in quintets]), v2ctranstable)
- assert could_be_base32_encoded_l(res, lengthinbits), "lengthinbits: %s, res: %s, origos: %s" % (lengthinbits, res, `origos`)
+ res = ''.join([chr(q) for q in quintets]).translate(v2ctranstable)
+ assert could_be_base32_encoded_l(res, lengthinbits), "lengthinbits: %s, res: %s, origos: %r" % (lengthinbits, res, origos)
return res
def a2b_long(cs):
- return a2b_l_long(cs, ((len(cs)*5+3)/8)*8)
+ return a2b_l_long(cs, ((len(cs)*5+3)//8)*8)
def a2b_l_long(cs, lengthinbits):
precondition(could_be_base32_encoded_l(cs, lengthinbits), "cs is required to be possibly base32 encoded data.", lengthinbits=lengthinbits, cs=cs)
- qs = [ord(v) for v in string.translate(cs, c2vtranstable)]
+ qs = [ord(v) for v in cs.translate(c2vtranstable)]
# print "lengthinbits: ", lengthinbits
- numoctets = (lengthinbits+7)/8
+ numoctets = (lengthinbits+7)//8
# print "numoctets: ", numoctets
- numquintetsofdata = (lengthinbits+4)/5
+ numquintetsofdata = (lengthinbits+4)//5
# print "numquintetsofdata: ", numquintetsofdata
# strip trailing quintets that won't be used
del qs[numquintetsofdata:]
@@ -360,21 +363,21 @@ def a2b_l_long(cs, lengthinbits):
qs[-1] = qs[-1] >> (5-(lengthinbits % 5))
qs[-1] = qs[-1] << (5-(lengthinbits % 5))
# append zero quintets for padding if needed
- numquintetsneeded = (numoctets*8+4)/5+7 # append 7 extra zero quintets so that I can read in 40 -bit (8-quintet) chunks
+ numquintetsneeded = (numoctets*8+4)//5+7 # append 7 extra zero quintets so that I can read in 40 -bit (8-quintet) chunks
qs.extend([0]*(numquintetsneeded-len(qs)))
octets = []
i = 0
- CUTOFF = 2L**32
+ CUTOFF = 2**32
while len(octets) < numoctets:
# take the next 8 quintets and turn them into 5 octets
- num = 0L # i am a LONG! hear me roar
+ num = 0
for j in range(8):
num = num * 32
num = num + qs[i]
i = i + 1
for j in range(5):
- octet = num / CUTOFF
+ octet = num // CUTOFF
octets.append(octet)
num = num - (octet * CUTOFF)
num = num * 256
@@ -389,7 +392,7 @@ def trimnpad(os, lengthinbits):
"""
os = [ord(o) for o in os]
mod8 = lengthinbits % 8
- div8 = lengthinbits / 8
+ div8 = lengthinbits // 8
numos = div8 + (mod8 != 0)
if len(os) >= numos:
# strip trailing octets that won't be used
@@ -429,12 +432,12 @@ def _help_bench_ed_l(N):
def benchem():
import benchfunc # from pyutil
- print "e: "
+ print("e: ")
benchfunc.bench(_help_bench_e, TOPXP=13)
- print "ed: "
+ print("ed: ")
benchfunc.bench(_help_bench_ed, TOPXP=13)
- print "e_l: "
+ print("e_l: ")
benchfunc.bench(_help_bench_e_l, TOPXP=13)
- print "ed_l: "
+ print("ed_l: ")
benchfunc.bench(_help_bench_ed_l, TOPXP=13)
diff --git a/zbase32/zbase32id.py b/zbase32/zbase32id.py
index cc3e82e..e601fc0 100644
--- a/zbase32/zbase32id.py
+++ b/zbase32/zbase32id.py
@@ -4,14 +4,12 @@
# GNU Lesser General Public License v2.1.
# See the file COPYING or visit http://www.gnu.org/ for details.
-import string
-
from pyutil import humanreadable
import zbase32
printableascii = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=+!@#$%^&*()`~[]\{}|;':\",./<>? \t" # I just typed this in by looking at my keyboard. It probably doesn't matter much if I missed some, because I only use it to guess whether a 20-byte string should be represented as a string or as an ID. If all of the characters in the string are found `printableascii', then we guess that it is a string, not an id.
-nulltrans = string.maketrans('', '')
+nonprintabletrans = str.maketrans('', '', printableascii)
def abbrev_bare(id, b2a_l=zbase32.b2a_l, trimnpad=zbase32.trimnpad):
return b2a_l(trimnpad(id[:4], 25), 25)
@@ -32,19 +30,19 @@ class AbbrevRepr(humanreadable.BetterRepr):
def __init__(self):
humanreadable.BetterRepr.__init__(self)
- def repr_str(self, obj, level, could_be_base32_encoded_l=zbase32.could_be_base32_encoded_l, nulltrans=nulltrans, printableascii=printableascii, abbrev=abbrev):
+ def repr_str(self, obj, level, could_be_base32_encoded_l=zbase32.could_be_base32_encoded_l, nonprintabletrans=nonprintabletrans, abbrev=abbrev):
if len(obj) == 20:
# But maybe it was just a 20-character human-readable string, like "credit limit reached", so this is an attempt to detect that case.
- if len(obj.translate(nulltrans, printableascii)) == 0:
+ if len(obj.translate(nonprintabletrans)) == 0:
if self.maxstring >= 22:
- return `obj`
+ return repr(obj)
# inlining repr.repr_string() here...
- s = `obj[:self.maxstring]`
+ s = repr(obj[:self.maxstring])
if len(s) > self.maxstring:
i = max(0, (self.maxstring-3)/2)
j = max(0, self.maxstring-3-i)
- s = `obj[:i] + obj[len(obj)-j:]`
+ s = repr(obj[:i] + obj[len(obj)-j:])
s = s[:i] + '...' + s[len(s)-j:]
# ... done inlining `repr.repr_string()'
return s
@@ -55,11 +53,11 @@ def repr_str(self, obj, level, could_be_base32_encoded_l=zbase32.could_be_base32
return '<' + obj[:5] + '>'
else:
# inlining repr.repr_string() here...
- s = `obj[:self.maxstring]`
+ s = repr(obj[:self.maxstring])
if len(s) > self.maxstring:
i = max(0, (self.maxstring-3)/2)
j = max(0, self.maxstring-3-i)
- s = `obj[:i] + obj[len(obj)-j:]`
+ s = repr(obj[:i] + obj[len(obj)-j:])
s = s[:i] + '...' + s[len(s)-j:]
# ... done inlining `repr.repr_string()'
return s
--
2.17.1