diff -r d083b6ead156 Lib/base64.py --- a/Lib/base64.py Mon Mar 10 11:05:07 2014 +0100 +++ b/Lib/base64.py Mon Mar 10 11:37:38 2014 +0100 @@ -274,10 +274,23 @@ def b16decode(s, casefold=False): # Ascii85 encoding/decoding # +_a85chars = None +_a85chars2 = None +_A85START = b"<~" +_A85END = b"~>" + +def _init_a85_tables(): + """Precompute tables for faster ascii85 codec.""" + global _a85chars, _a85chars2 + if _a85chars is None: + _a85chars = [bytes([i]) for i in range(33, 118)] + _a85chars2 = [(a + b) for a in _a85chars for b in _a85chars] + def _85encode(b, chars, chars2, pad=False, foldnuls=False, foldspaces=False): # Helper function for a85encode and b85encode if not isinstance(b, bytes_types): b = memoryview(b).tobytes() + _init_a85_tables() padding = (-len(b)) % 4 if padding: @@ -300,11 +313,6 @@ def _85encode(b, chars, chars2, pad=Fals return b''.join(chunks) -_A85START = b"<~" -_A85END = b"~>" -_a85chars = [bytes([i]) for i in range(33, 118)] -_a85chars2 = [(a + b) for a in _a85chars for b in _a85chars] - def a85encode(b, *, foldspaces=False, wrapcol=0, pad=False, adobe=False): """Encode a byte string using Ascii85. @@ -324,6 +332,7 @@ def a85encode(b, *, foldspaces=False, wr adobe controls whether the encoded byte sequence is framed with <~ and ~>, which is used by the Adobe implementation. """ + _init_a85_tables() result = _85encode(b, _a85chars, _a85chars2, pad, True, foldspaces) if adobe: @@ -408,23 +417,33 @@ def a85decode(b, *, foldspaces=False, ad # The following code is originally taken (with permission) from Mercurial -_b85chars = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" \ - b"abcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~" -_b85chars = [bytes([i]) for i in _b85chars] -_b85chars2 = [(a + b) for a in _b85chars for b in _b85chars] +_b85chars = None +_b85chars2 = None _b85dec = None +def _init_b85_tables(): + """Precompute tables for faster base85 codec.""" + global _b85chars, _b85chars2 + if _b85chars is None: + _b85chars = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" \ + b"abcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~" + _b85chars = [bytes([i]) for i in _b85chars] + _b85chars2 = [(a + b) for a in _b85chars for b in _b85chars] + def b85encode(b, pad=False): """Encode an ASCII-encoded byte array in base85 format. If pad is true, the input is padded with "\0" so its length is a multiple of 4 characters before encoding. """ + _init_b85_tables() return _85encode(b, _b85chars, _b85chars2, pad) def b85decode(b): """Decode base85-encoded byte array""" b = _bytes_from_decode_data(b) + _init_b85_tables() + global _b85dec if _b85dec is None: _b85dec = [None] * 256