From c229ba71967996852f26082c526e3a65742aef48 Mon Sep 17 00:00:00 2001 From: Itamar Turner-Trauring Date: Tue, 21 Jul 2020 13:55:53 -0400 Subject: [PATCH] Workaround future/pypy incompatibility. --- src/allmydata/util/base32.py | 13 ++++++++----- src/allmydata/util/base62.py | 18 +++++++++++++----- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/allmydata/util/base32.py b/src/allmydata/util/base32.py index cb21d1a8a..ba5f80d67 100644 --- a/src/allmydata/util/base32.py +++ b/src/allmydata/util/base32.py @@ -19,21 +19,24 @@ if PY2: consistently until other modules are ported. """ return getattr(b, "__native__", lambda: b)() + import string + maketrans = string.maketrans else: def backwardscompat_bytes(b): return b + maketrans = bytes.maketrans import base64 from allmydata.util.assertutil import precondition -rfc3548_alphabet = bytes(b"abcdefghijklmnopqrstuvwxyz234567") # RFC3548 standard used by Gnutella, Content-Addressable Web, THEX, Bitzi, Web-Calculus... +rfc3548_alphabet = b"abcdefghijklmnopqrstuvwxyz234567" # RFC3548 standard used by Gnutella, Content-Addressable Web, THEX, Bitzi, Web-Calculus... chars = rfc3548_alphabet -vals = bytes(range(32)) -c2vtranstable = bytes.maketrans(chars, vals) -v2ctranstable = bytes.maketrans(vals, chars) -identitytranstable = bytes.maketrans(b'', b'') +vals = backwardscompat_bytes(bytes(range(32))) +c2vtranstable = maketrans(chars, vals) +v2ctranstable = maketrans(vals, chars) +identitytranstable = maketrans(b'', b'') def _get_trailing_chars_without_lsbs(N, d): """ diff --git a/src/allmydata/util/base62.py b/src/allmydata/util/base62.py index 7f349caaa..3d80671ba 100644 --- a/src/allmydata/util/base62.py +++ b/src/allmydata/util/base62.py @@ -12,6 +12,14 @@ from future.utils import PY2 if PY2: from builtins import filter, map, zip, ascii, chr, hex, input, next, oct, open, pow, round, super, bytes, dict, int, list, object, range, str, max, min # noqa: F401 +if PY2: + import string + maketrans = string.maketrans + translate = string.translate +else: + maketrans = bytes.maketrans + translate = bytes.translate + from past.builtins import chr as byteschr from allmydata.util.mathutil import log_ceil, log_floor @@ -21,9 +29,9 @@ chars = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" BASE62CHAR = b'[' + chars + b']' vals = b''.join([byteschr(i) for i in range(62)]) -c2vtranstable = bytes.maketrans(chars, vals) -v2ctranstable = bytes.maketrans(vals, chars) -identitytranstable = bytes.maketrans(chars, chars) +c2vtranstable = maketrans(chars, vals) +v2ctranstable = maketrans(vals, chars) +identitytranstable = maketrans(chars, chars) def b2a(os): """ @@ -77,7 +85,7 @@ def b2a_l(os, lengthinbits): value //= 62 numvalues //= 62 - return bytes.translate(bytes([c for c in reversed(chars)]), v2ctranstable) # make it big-endian + return translate(bytes([c for c in reversed(chars)]), v2ctranstable) # make it big-endian def num_octets_that_encode_to_this_many_chars(numcs): return log_floor(62**numcs, 256) @@ -108,7 +116,7 @@ def a2b_l(cs, lengthinbits): """ # We call bytes() again for Python 2, to ensure literals are using future's # Python 3-compatible variant. - cs = [c for c in reversed(bytes(bytes.translate(cs, c2vtranstable)))] # treat cs as big-endian -- and we want to process the least-significant c first + cs = [c for c in reversed(bytes(translate(cs, c2vtranstable)))] # treat cs as big-endian -- and we want to process the least-significant c first value = 0 numvalues = 1 # the number of possible values that value could be