[pypy-svn] r79622 - in pypy/trunk: lib-python lib_pypy pypy/module/binascii pypy/module/binascii/test

arigo at codespeak.net arigo at codespeak.net
Mon Nov 29 10:59:17 CET 2010


Author: arigo
Date: Mon Nov 29 10:59:15 2010
New Revision: 79622

Added:
   pypy/trunk/pypy/module/binascii/   (props changed)
   pypy/trunk/pypy/module/binascii/__init__.py   (contents, props changed)
   pypy/trunk/pypy/module/binascii/app_binascii.py   (contents, props changed)
   pypy/trunk/pypy/module/binascii/interp_base64.py
   pypy/trunk/pypy/module/binascii/interp_binascii.py   (contents, props changed)
   pypy/trunk/pypy/module/binascii/interp_crc32.py   (contents, props changed)
   pypy/trunk/pypy/module/binascii/interp_hexlify.py   (contents, props changed)
   pypy/trunk/pypy/module/binascii/interp_hqx.py   (contents, props changed)
   pypy/trunk/pypy/module/binascii/interp_uu.py   (contents, props changed)
   pypy/trunk/pypy/module/binascii/test/   (props changed)
   pypy/trunk/pypy/module/binascii/test/test_binascii.py   (contents, props changed)
Modified:
   pypy/trunk/lib-python/conftest.py
   pypy/trunk/lib_pypy/binascii.py
Log:
Implement binascii in RPython.


Modified: pypy/trunk/lib-python/conftest.py
==============================================================================
--- pypy/trunk/lib-python/conftest.py	(original)
+++ pypy/trunk/lib-python/conftest.py	Mon Nov 29 10:59:15 2010
@@ -139,7 +139,7 @@
     RegrTest('test_augassign.py', core=True),
     RegrTest('test_base64.py'),
     RegrTest('test_bastion.py'),
-    RegrTest('test_binascii.py'),
+    RegrTest('test_binascii.py', usemodules='binascii'),
 
     RegrTest('test_binhex.py'),
 

Modified: pypy/trunk/lib_pypy/binascii.py
==============================================================================
--- pypy/trunk/lib_pypy/binascii.py	(original)
+++ pypy/trunk/lib_pypy/binascii.py	Mon Nov 29 10:59:15 2010
@@ -1,3 +1,9 @@
+"""A pure Python implementation of binascii.
+
+Rather slow and buggy in corner cases.
+PyPy provides an RPython version too.
+"""
+
 class Error(Exception):
     pass
 
@@ -277,7 +283,7 @@
         if (c > '~' or
             c == '=' or
             (header and c == '_') or
-            (c == '.' and linelen == 0 and (inp == len(data) or
+            (c == '.' and linelen == 0 and (inp+1 == len(data) or
                                             data[inp+1] == '\n' or
                                             data[inp+1] == '\r')) or
             (not istext and (c == '\r' or c == '\n')) or

Added: pypy/trunk/pypy/module/binascii/__init__.py
==============================================================================
--- (empty file)
+++ pypy/trunk/pypy/module/binascii/__init__.py	Mon Nov 29 10:59:15 2010
@@ -0,0 +1,36 @@
+
+"""
+Mixed-module definition for the binascii module.
+Note that there is also a pure Python implementation in lib_pypy/binascii.py;
+the pypy/module/binascii/ version takes precedence if it is enabled.
+"""
+
+from pypy.interpreter.mixedmodule import MixedModule
+
+
+class Module(MixedModule):
+    """binascii - Conversion between binary data and ASCII"""
+
+    appleveldefs = {
+        'Error': 'app_binascii.Error',
+        'Incomplete': 'app_binascii.Incomplete',
+        }
+
+    interpleveldefs = {
+        'a2b_uu': 'interp_uu.a2b_uu',
+        'b2a_uu': 'interp_uu.b2a_uu',
+        'a2b_base64': 'interp_base64.a2b_base64',
+        'b2a_base64': 'interp_base64.b2a_base64',
+        'a2b_qp': 'interp_qp.a2b_qp',
+        'b2a_qp': 'interp_qp.b2a_qp',
+        'a2b_hqx': 'interp_hqx.a2b_hqx',
+        'b2a_hqx': 'interp_hqx.b2a_hqx',
+        'rledecode_hqx': 'interp_hqx.rledecode_hqx',
+        'rlecode_hqx': 'interp_hqx.rlecode_hqx',
+        'crc_hqx': 'interp_hqx.crc_hqx',
+        'crc32': 'interp_crc32.crc32',
+        'b2a_hex': 'interp_hexlify.hexlify',
+        'hexlify': 'interp_hexlify.hexlify',
+        'a2b_hex': 'interp_hexlify.unhexlify',
+        'unhexlify': 'interp_hexlify.unhexlify',
+        }

Added: pypy/trunk/pypy/module/binascii/app_binascii.py
==============================================================================
--- (empty file)
+++ pypy/trunk/pypy/module/binascii/app_binascii.py	Mon Nov 29 10:59:15 2010
@@ -0,0 +1,6 @@
+
+class Error(Exception):
+    pass
+
+class Incomplete(Exception):
+    pass

Added: pypy/trunk/pypy/module/binascii/interp_base64.py
==============================================================================
--- (empty file)
+++ pypy/trunk/pypy/module/binascii/interp_base64.py	Mon Nov 29 10:59:15 2010
@@ -0,0 +1,112 @@
+from pypy.interpreter.gateway import ObjSpace
+from pypy.rlib.rstring import StringBuilder
+from pypy.module.binascii.interp_binascii import raise_Error
+from pypy.rlib.rarithmetic import ovfcheck
+
+# ____________________________________________________________
+
+PAD = '='
+
+table_a2b_base64 = [
+    -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
+    -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
+    -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
+    52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1, # Note PAD->-1 here
+    -1, 0, 1, 2,  3, 4, 5, 6,  7, 8, 9,10, 11,12,13,14,
+    15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
+    -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
+    41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1,
+    -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
+    -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
+    -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
+    -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
+    -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
+    -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
+    -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
+    -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,    
+]
+def _transform(n):
+    if n == -1:
+        return '\xff'
+    else:
+        return chr(n)
+table_a2b_base64 = ''.join(map(_transform, table_a2b_base64))
+assert len(table_a2b_base64) == 256
+
+
+def a2b_base64(space, ascii):
+    "Decode a line of base64 data."
+
+    res = StringBuilder((len(ascii) // 4) * 3)   # maximum estimate
+    quad_pos = 0
+    leftchar = 0
+    leftbits = 0
+    last_char_was_a_pad = False
+
+    for c in ascii:
+        if c == PAD:
+            if quad_pos > 2 or (quad_pos == 2 and last_char_was_a_pad):
+                break      # stop on 'xxx=' or on 'xx=='
+            last_char_was_a_pad = True
+        else:
+            n = ord(table_a2b_base64[ord(c)])
+            if n == 0xff:
+                continue    # ignore strange characters
+            #
+            # Shift it in on the low end, and see if there's
+            # a byte ready for output.
+            quad_pos = (quad_pos + 1) & 3
+            leftchar = (leftchar << 6) | n
+            leftbits += 6
+            #
+            if leftbits >= 8:
+                leftbits -= 8
+                res.append(chr(leftchar >> leftbits))
+                leftchar &= ((1 << leftbits) - 1)
+            #
+            last_char_was_a_pad = False
+    else:
+        if leftbits != 0:
+            raise_Error(space, "Incorrect padding")
+
+    return space.wrap(res.build())
+a2b_base64.unwrap_spec = [ObjSpace, 'bufferstr']
+
+# ____________________________________________________________
+
+table_b2a_base64 = (
+    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
+
+def b2a_base64(space, bin):
+    "Base64-code line of data."
+
+    newlength = (len(bin) + 2) // 3
+    try:
+        newlength = ovfcheck(newlength * 4)
+    except OverflowError:
+        raise OperationError(space.w_MemoryError, space.w_None)
+    newlength += 1
+    res = StringBuilder(newlength)
+
+    leftchar = 0
+    leftbits = 0
+    for c in bin:
+        # Shift into our buffer, and output any 6bits ready
+        leftchar = (leftchar << 8) | ord(c)
+        leftbits += 8
+        res.append(table_b2a_base64[(leftchar >> (leftbits-6)) & 0x3f])
+        leftbits -= 6
+        if leftbits >= 6:
+            res.append(table_b2a_base64[(leftchar >> (leftbits-6)) & 0x3f])
+            leftbits -= 6
+    #
+    if leftbits == 2:
+        res.append(table_b2a_base64[(leftchar & 3) << 4])
+        res.append(PAD)
+        res.append(PAD)
+    elif leftbits == 4:
+        res.append(table_b2a_base64[(leftchar & 0xf) << 2])
+        res.append(PAD)
+    res.append('\n')
+    return space.wrap(res.build())
+b2a_base64.unwrap_spec = [ObjSpace, 'bufferstr']

Added: pypy/trunk/pypy/module/binascii/interp_binascii.py
==============================================================================
--- (empty file)
+++ pypy/trunk/pypy/module/binascii/interp_binascii.py	Mon Nov 29 10:59:15 2010
@@ -0,0 +1,11 @@
+from pypy.interpreter.error import OperationError
+
+def raise_Error(space, msg):
+    w_module = space.getbuiltinmodule('binascii')
+    w_Error = space.getattr(w_module, space.wrap('Error'))
+    raise OperationError(w_Error, space.wrap(msg))
+
+def raise_Incomplete(space, msg):
+    w_module = space.getbuiltinmodule('binascii')
+    w_Error = space.getattr(w_module, space.wrap('Incomplete'))
+    raise OperationError(w_Error, space.wrap(msg))

Added: pypy/trunk/pypy/module/binascii/interp_crc32.py
==============================================================================
--- (empty file)
+++ pypy/trunk/pypy/module/binascii/interp_crc32.py	Mon Nov 29 10:59:15 2010
@@ -0,0 +1,75 @@
+from pypy.interpreter.gateway import ObjSpace
+from pypy.rlib.rarithmetic import r_uint, intmask
+
+# ____________________________________________________________
+
+crc_32_tab = [
+    0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419,
+    0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4,
+    0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07,
+    0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
+    0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856,
+    0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
+    0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4,
+    0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
+    0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3,
+    0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a,
+    0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599,
+    0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
+    0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190,
+    0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f,
+    0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0x0f00f934, 0x9609a88e,
+    0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
+    0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed,
+    0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
+    0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3,
+    0xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
+    0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a,
+    0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5,
+    0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010,
+    0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
+    0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17,
+    0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6,
+    0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615,
+    0x73dc1683, 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
+    0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 0xf00f9344,
+    0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
+    0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a,
+    0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
+    0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1,
+    0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c,
+    0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef,
+    0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
+    0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe,
+    0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31,
+    0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c,
+    0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
+    0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b,
+    0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
+    0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1,
+    0x18b74777, 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
+    0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278,
+    0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7,
+    0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66,
+    0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
+    0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605,
+    0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8,
+    0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b,
+    0x2d02ef8d
+]
+def _make_signed_32(v):
+    if v >= 0x80000000:
+        v -= 0x100000000
+    return int(v)
+crc_32_tab = map(_make_signed_32, crc_32_tab)
+
+
+def crc32(space, data, oldcrc=0):
+    "Compute the CRC-32 incrementally."
+    crc = ~oldcrc
+    for c in data:
+        crc_shifted_by_8 = intmask(r_uint(crc) >> 8)
+        crc = crc_32_tab[(crc & 0xff) ^ ord(c)] ^ crc_shifted_by_8
+    crc = ~crc
+    return space.wrap(crc)
+crc32.unwrap_spec = [ObjSpace, 'bufferstr', 'c_int']

Added: pypy/trunk/pypy/module/binascii/interp_hexlify.py
==============================================================================
--- (empty file)
+++ pypy/trunk/pypy/module/binascii/interp_hexlify.py	Mon Nov 29 10:59:15 2010
@@ -0,0 +1,58 @@
+from pypy.interpreter.error import OperationError
+from pypy.interpreter.gateway import ObjSpace
+from pypy.rlib.rstring import StringBuilder
+from pypy.rlib.rarithmetic import ovfcheck
+
+# ____________________________________________________________
+
+def _value2char(value):
+    if value < 10:
+        return chr(ord('0') + value)
+    else:
+        return chr((ord('a')-10) + value)
+_value2char._always_inline_ = True
+
+def hexlify(space, data):
+    '''Hexadecimal representation of binary data.
+This function is also available as "hexlify()".'''
+    try:
+        newlength = ovfcheck(len(data) * 2)
+    except OverflowError:
+        raise OperationError(space.w_MemoryError, space.w_None)
+    res = StringBuilder(newlength)
+    for c in data:
+        res.append(_value2char(ord(c) >> 4))
+        res.append(_value2char(ord(c) & 0xf))
+    return space.wrap(res.build())
+hexlify.unwrap_spec = [ObjSpace, 'bufferstr']
+
+# ____________________________________________________________
+
+def _char2value(space, c):
+    if c <= '9':
+        if c >= '0':
+            return ord(c) - ord('0')
+    elif c <= 'F':
+        if c >= 'A':
+            return ord(c) - (ord('A')-10)
+    elif c <= 'f':
+        if c >= 'a':
+            return ord(c) - (ord('a')-10)
+    raise OperationError(space.w_TypeError,
+                         space.wrap('Non-hexadecimal digit found'))
+_char2value._always_inline_ = True
+
+def unhexlify(space, hexstr):
+    '''Binary data of hexadecimal representation.
+hexstr must contain an even number of hex digits (upper or lower case).
+This function is also available as "unhexlify()".'''
+    if len(hexstr) & 1:
+        raise OperationError(space.w_TypeError,
+                             space.wrap('Odd-length string'))
+    res = StringBuilder(len(hexstr) >> 1)
+    for i in range(0, len(hexstr), 2):
+        a = _char2value(space, hexstr[i])
+        b = _char2value(space, hexstr[i+1])
+        res.append(chr((a << 4) | b))
+    return space.wrap(res.build())
+unhexlify.unwrap_spec = [ObjSpace, 'bufferstr']

Added: pypy/trunk/pypy/module/binascii/interp_hqx.py
==============================================================================
--- (empty file)
+++ pypy/trunk/pypy/module/binascii/interp_hqx.py	Mon Nov 29 10:59:15 2010
@@ -0,0 +1,244 @@
+from pypy.interpreter.gateway import ObjSpace
+from pypy.rlib.rstring import StringBuilder
+from pypy.module.binascii.interp_binascii import raise_Error, raise_Incomplete
+from pypy.rlib.rarithmetic import ovfcheck
+
+# ____________________________________________________________
+
+DONE = 0x7f
+SKIP = 0x7e
+FAIL = 0x7d
+
+table_a2b_hqx = [
+    #^@    ^A    ^B    ^C    ^D    ^E    ^F    ^G   
+    FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
+    #\b    \t    \n    ^K    ^L    \r    ^N    ^O   
+    FAIL, FAIL, SKIP, FAIL, FAIL, SKIP, FAIL, FAIL,
+    #^P    ^Q    ^R    ^S    ^T    ^U    ^V    ^W   
+    FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
+    #^X    ^Y    ^Z    ^[    ^\    ^]    ^^    ^_   
+    FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
+    #      !     "     #     $     %     &     '   
+    FAIL, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
+    #(     )     *     +     ,     -     .     /   
+    0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, FAIL, FAIL,
+    #0     1     2     3     4     5     6     7   
+    0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, FAIL,
+    #8     9     :     ;     <     =     >     ?   
+    0x14, 0x15, DONE, FAIL, FAIL, FAIL, FAIL, FAIL,
+    #@     A     B     C     D     E     F     G   
+    0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D,
+    #H     I     J     K     L     M     N     O   
+    0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, FAIL,
+    #P     Q     R     S     T     U     V     W   
+    0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, FAIL,
+    #X     Y     Z     [     \     ]     ^     _   
+    0x2C, 0x2D, 0x2E, 0x2F, FAIL, FAIL, FAIL, FAIL,
+    #`     a     b     c     d     e     f     g   
+    0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, FAIL,
+    #h     i     j     k     l     m     n     o   
+    0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, FAIL, FAIL,
+    #p     q     r     s     t     u     v     w   
+    0x3D, 0x3E, 0x3F, FAIL, FAIL, FAIL, FAIL, FAIL,
+    #x     y     z     {     |     }     ~    ^?   
+    FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
+    FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
+    FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
+    FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
+    FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
+    FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
+    FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
+    FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
+    FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
+    FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
+    FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
+    FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
+    FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
+    FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
+    FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
+    FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
+    FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
+]
+table_a2b_hqx = ''.join(map(chr, table_a2b_hqx))
+
+def a2b_hqx(space, ascii):
+    """Decode .hqx coding.  Returns (bin, done)."""
+
+    # overestimate the resulting length
+    res = StringBuilder(len(ascii))
+    done = 0
+    pending_value = 0
+    pending_bits = 0
+
+    for c in ascii:
+        n = ord(table_a2b_hqx[ord(c)])
+        if n <= 0x3F:
+            pending_value = (pending_value << 6) | n
+            pending_bits += 6
+            if pending_bits == 24:
+                # flush
+                res.append(chr(pending_value >> 16))
+                res.append(chr((pending_value >> 8) & 0xff))
+                res.append(chr(pending_value & 0xff))
+                pending_value = 0
+                pending_bits = 0
+        elif n == FAIL:
+            raise_Error(space, 'Illegal character')
+        elif n == DONE:
+            if pending_bits >= 8:
+                res.append(chr(pending_value >> (pending_bits - 8)))
+            if pending_bits >= 16:
+                res.append(chr((pending_value >> (pending_bits - 16)) & 0xff))
+            done = 1
+            break
+        #elif n == SKIP: pass
+    else:
+        if pending_bits > 0:
+            raise_Incomplete(space, 'String has incomplete number of bytes')
+    return space.newtuple([space.wrap(res.build()), space.wrap(done)])
+a2b_hqx.unwrap_spec = [ObjSpace, 'bufferstr']
+
+# ____________________________________________________________
+
+hqx_encoding = (
+    '!"#$%&\'()*+,-012345689 at ABCDEFGHIJKLMNPQRSTUVXYZ[`abcdefhijklmpqr')
+
+def b2a_hqx(space, bin):
+    "Encode .hqx data."
+    extra = (len(bin) + 2) // 3
+    try:
+        newlength = ovfcheck(len(bin) + extra)
+    except OverflowError:
+        raise OperationError(space.w_MemoryError, space.w_None)
+    res = StringBuilder(newlength)
+    leftchar = 0
+    leftbits = 0
+    for c in bin:
+        # Shift into our buffer, and output any 6bits ready
+        leftchar = (leftchar << 8) | ord(c)
+        leftbits += 8
+        res.append(hqx_encoding[(leftchar >> (leftbits-6)) & 0x3f])
+        leftbits -= 6
+        if leftbits >= 6:
+            res.append(hqx_encoding[(leftchar >> (leftbits-6)) & 0x3f])
+            leftbits -= 6
+    # Output a possible runt byte
+    if leftbits > 0:
+        leftchar <<= (6 - leftbits)
+        res.append(hqx_encoding[leftchar & 0x3f])
+    return space.wrap(res.build())
+b2a_hqx.unwrap_spec = [ObjSpace, 'bufferstr']
+
+# ____________________________________________________________
+
+def rledecode_hqx(space, hexbin):
+    "Decode hexbin RLE-coded string."
+
+    # that's a guesstimation of the resulting length
+    res = StringBuilder(len(hexbin))
+
+    end = len(hexbin)
+    i = 0
+    lastpushed = -1
+    while i < end:
+        c = hexbin[i]
+        i += 1
+        if c != '\x90':
+            res.append(c)
+            lastpushed = ord(c)
+        else:
+            if i == end:
+                raise_Incomplete(space, 'String ends with the RLE code \x90')
+            count = ord(hexbin[i]) - 1
+            i += 1
+            if count < 0:
+                res.append('\x90')
+                lastpushed = 0x90
+            else:
+                if lastpushed < 0:
+                    raise_Error(space, 'String starts with the RLE code \x90')
+                res.append_multiple_char(chr(lastpushed), count)
+    return space.wrap(res.build())
+rledecode_hqx.unwrap_spec = [ObjSpace, 'bufferstr']
+
+# ____________________________________________________________
+
+def rlecode_hqx(space, data):
+    "Binhex RLE-code binary data."
+
+    # that's a guesstimation of the resulting length
+    res = StringBuilder(len(data))
+
+    i = 0
+    end = len(data)
+    while i < end:
+        c = data[i]
+        res.append(c)
+        if c == '\x90':
+            # Escape it, and ignore repetitions (*).
+            res.append('\x00')
+        else:
+            # Check how many following are the same
+            inend = i + 1
+            while inend < end and data[inend] == c and inend < i + 255:
+                inend += 1
+            if inend - i > 3:
+                # More than 3 in a row. Output RLE.  For the case of more
+                # than 255, see (*) below.
+                res.append('\x90')
+                res.append(chr(inend - i))
+                i = inend
+                continue
+        i += 1
+    # (*) Note that we put simplicity before compatness here, like CPython.
+    # I am sure that if we tried harder to produce the smallest possible
+    # string that rledecode_hqx() would expand back to 'data', there are
+    # some programs somewhere that would start failing obscurely in rare
+    # cases.
+    return space.wrap(res.build())
+rlecode_hqx.unwrap_spec = [ObjSpace, 'bufferstr']
+
+# ____________________________________________________________
+
+crctab_hqx = [
+        0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
+        0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
+        0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
+        0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
+        0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
+        0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
+        0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
+        0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
+        0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
+        0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
+        0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12,
+        0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
+        0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
+        0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
+        0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70,
+        0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
+        0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
+        0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
+        0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
+        0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
+        0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
+        0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
+        0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
+        0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
+        0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
+        0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
+        0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
+        0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
+        0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,
+        0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
+        0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
+        0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0,
+]
+
+def crc_hqx(space, data, oldcrc):
+    "Compute hqx CRC incrementally."
+    crc = oldcrc
+    for c in data:
+        crc = ((crc << 8) & 0xff00) ^ crctab_hqx[((crc >> 8) & 0xff) ^ ord(c)]
+    return space.wrap(crc)
+crc_hqx.unwrap_spec = [ObjSpace, 'bufferstr', int]

Added: pypy/trunk/pypy/module/binascii/interp_uu.py
==============================================================================
--- (empty file)
+++ pypy/trunk/pypy/module/binascii/interp_uu.py	Mon Nov 29 10:59:15 2010
@@ -0,0 +1,85 @@
+from pypy.interpreter.gateway import ObjSpace
+from pypy.rlib.rstring import StringBuilder
+from pypy.module.binascii.interp_binascii import raise_Error
+
+# ____________________________________________________________
+
+def _a2b_read(space, s, index):
+    try:
+        c = s[index]
+    except IndexError:
+        return 0
+    # Check the character for legality.  The 64 instead of the expected 63
+    # is because there are a few uuencodes out there that use '`' as zero
+    # instead of space.
+    if c < ' ' or c > chr(32 + 64):
+        if c == '\n' or c == '\r':
+            return 0
+        raise_Error(space, "Illegal char")
+    return (ord(c) - 0x20) & 0x3f
+_a2b_read._always_inline_ = True
+
+def _a2b_write(space, res, length, char):
+    if res.getlength() < length:    # common case: we have enough room.
+        res.append(chr(char))
+    else:
+        # overflows.  Only accept zeros from now on.
+        if char != 0:
+            raise_Error(space, "Trailing garbage")
+_a2b_write._always_inline_ = True
+
+
+def a2b_uu(space, ascii):
+    "Decode a line of uuencoded data."
+
+    if len(ascii) == 0:    # obscure case, for compability with CPython
+        length = (-0x20) & 0x3f
+    else:
+        length = (ord(ascii[0]) - 0x20) & 0x3f
+    res = StringBuilder(length)
+
+    for i in range(1, len(ascii), 4):
+        A = _a2b_read(space, ascii, i)
+        B = _a2b_read(space, ascii, i+1)
+        C = _a2b_read(space, ascii, i+2)
+        D = _a2b_read(space, ascii, i+3)
+        #
+        _a2b_write(space, res, length, A << 2 | B >> 4)
+        _a2b_write(space, res, length, (B & 0xf) << 4 | C >> 2)
+        _a2b_write(space, res, length, (C & 0x3) << 6 | D)
+
+    res.append_multiple_char('\x00', length - res.getlength())
+    return space.wrap(res.build())
+a2b_uu.unwrap_spec = [ObjSpace, 'bufferstr']
+
+# ____________________________________________________________
+
+def _b2a_read(bin, i):
+    try:
+        return ord(bin[i])
+    except IndexError:
+        return 0
+_a2b_read._always_inline_ = True
+
+def b2a_uu(space, bin):
+    "Uuencode a line of data."
+
+    length = len(bin)
+    if length > 45:
+        raise_Error(space, 'At most 45 bytes at once')
+    res = StringBuilder(2 + ((length + 2) // 3) * 4)
+    res.append(chr(0x20 + length))
+
+    for i in range(0, length, 3):
+        A = _b2a_read(bin, i)
+        B = _b2a_read(bin, i+1)
+        C = _b2a_read(bin, i+2)
+        #
+        res.append(chr(0x20 +                  (A >> 2)))
+        res.append(chr(0x20 + ((A & 0x3) << 4 | B >> 4)))
+        res.append(chr(0x20 + ((B & 0xF) << 2 | C >> 6)))
+        res.append(chr(0x20 +  (C & 0x3F)))
+
+    res.append('\n')
+    return space.wrap(res.build())
+b2a_uu.unwrap_spec = [ObjSpace, 'bufferstr']

Added: pypy/trunk/pypy/module/binascii/test/test_binascii.py
==============================================================================
--- (empty file)
+++ pypy/trunk/pypy/module/binascii/test/test_binascii.py	Mon Nov 29 10:59:15 2010
@@ -0,0 +1,418 @@
+from pypy.conftest import gettestobjspace
+
+
+class AppTestBinascii(object):
+
+    def setup_class(cls):
+        """
+        Create a space with the binascii module and import it for use by the
+        tests.
+        """
+        cls.space = gettestobjspace()
+        cls.w_binascii = cls.space.appexec([], """():
+            import binascii
+            return binascii
+        """)
+
+    def test_a2b_uu(self):
+        # obscure case, for compability with CPython
+        assert self.binascii.a2b_uu("") == "\x00" * 0x20
+        #
+        for input, expected in [
+            (" ", ""),
+            ("!", "\x00"),
+            ("!6", "X"),
+            ('"6', "X\x00"),
+            ('"W', "\xdc\x00"),
+            ('"WA', "\xde\x10"),
+            ('"WAX', "\xde\x1e"),
+            ('#WAX', "\xde\x1e\x00"),
+            ('#WAXR', "\xde\x1e2"),
+            ('$WAXR', "\xde\x1e2\x00"),
+            ('$WAXR6', "\xde\x1e2X"),
+            ('%WAXR6U', "\xde\x1e2[P"),
+            ('&WAXR6UB', "\xde\x1e2[X\x80"),
+            ("'WAXR6UBA3", "\xde\x1e2[X\xa1L"),
+            ('(WAXR6UBA3#', "\xde\x1e2[X\xa1L0"),
+            (')WAXR6UBA3#Q', "\xde\x1e2[X\xa1L<@"),
+            ('*WAXR6UBA3#Q!5', "\xde\x1e2[X\xa1L<AT"),
+            ]:
+            assert self.binascii.a2b_uu(input) == expected
+            assert self.binascii.a2b_uu(input + ' ') == expected
+            assert self.binascii.a2b_uu(input + '  ') == expected
+            assert self.binascii.a2b_uu(input + '   ') == expected
+            assert self.binascii.a2b_uu(input + '    ') == expected
+            assert self.binascii.a2b_uu(input + '\n') == expected
+            assert self.binascii.a2b_uu(input + '\r\n') == expected
+            assert self.binascii.a2b_uu(input + '  \r\n') == expected
+            assert self.binascii.a2b_uu(input + '    \r\n') == expected
+        #
+        for bogus in [
+            "!w",
+            "! w",
+            "!  w",
+            "!   w",
+            "!    w",
+            "!     w",
+            "!      w",
+            "#a",
+            '"WAXR',
+            ]:
+            raises(self.binascii.Error, self.binascii.a2b_uu, bogus)
+            raises(self.binascii.Error, self.binascii.a2b_uu, bogus + ' ')
+            raises(self.binascii.Error, self.binascii.a2b_uu, bogus + '  ')
+            raises(self.binascii.Error, self.binascii.a2b_uu, bogus + '   ')
+            raises(self.binascii.Error, self.binascii.a2b_uu, bogus + '    ')
+            raises(self.binascii.Error, self.binascii.a2b_uu, bogus + '\n')
+            raises(self.binascii.Error, self.binascii.a2b_uu, bogus + '\r\n')
+            raises(self.binascii.Error, self.binascii.a2b_uu, bogus + '  \r\n')
+
+    def test_b2a_uu(self):
+        for input, expected in [
+            ("", " "),
+            ("\x00", "!    "),
+            ("X", "!6   "),
+            ("X\x00", '"6   '),
+            ("\xdc\x00", '"W   '),
+            ("\xde\x10", '"WA  '),
+            ("\xde\x1e", '"WAX '),
+            ("\xde\x1e\x00", '#WAX '),
+            ("\xde\x1e2", '#WAXR'),
+            ("\xde\x1e2\x00", '$WAXR    '),
+            ("\xde\x1e2X", '$WAXR6   '),
+            ("\xde\x1e2[P", '%WAXR6U  '),
+            ("\xde\x1e2[X\x80", '&WAXR6UB '),
+            ("\xde\x1e2[X\xa1L", "'WAXR6UBA3   "),
+            ("\xde\x1e2[X\xa1L0", '(WAXR6UBA3#  '),
+            ("\xde\x1e2[X\xa1L<@", ')WAXR6UBA3#Q '),
+            ("\xde\x1e2[X\xa1L<AT", '*WAXR6UBA3#Q!5   '),
+            ]:
+            assert self.binascii.b2a_uu(input) == expected + '\n'
+
+    def test_a2b_base64(self):
+        for input, expected in [
+            ("", ""),
+            ("\n", ""),
+            ("Yg==\n", "b"),
+            ("Y g = \n = \r", "b"),     # random spaces
+            ("Y\x80g\xff=\xc4=", "b"),  # random junk chars, >= 0x80
+            ("abcd", "i\xb7\x1d"),
+            ("abcdef==", "i\xb7\x1dy"),
+            ("abcdefg=", "i\xb7\x1dy\xf8"),
+            ("abcdefgh", "i\xb7\x1dy\xf8!"),
+            ("abcdef==FINISHED", "i\xb7\x1dy"),
+            ("abcdef=   \n   =FINISHED", "i\xb7\x1dy"),
+            ("abcdefg=FINISHED", "i\xb7\x1dy\xf8"),
+            ("abcd=efgh", "i\xb7\x1dy\xf8!"),
+            ("abcde=fgh", "i\xb7\x1dy\xf8!"),
+            ("abcdef=gh", "i\xb7\x1dy\xf8!"),
+            ]:
+            assert self.binascii.a2b_base64(input) == expected
+        #
+        for bogus in [
+            "abcde",
+            "abcde=",
+            "abcde==",
+            "abcde===",
+            "abcdef",
+            "abcdef=",
+            "abcdefg",
+            ]:
+            raises(self.binascii.Error, self.binascii.a2b_base64, bogus)
+
+    def test_b2a_base64(self):
+        for input, expected in [
+            ("", ""),
+            ("b", "Yg=="),
+            ("i\xb7\x1d", "abcd"),
+            ("i\xb7\x1dy", "abcdeQ=="),
+            ("i\xb7\x1dy\xf8", "abcdefg="),
+            ("i\xb7\x1dy\xf8!", "abcdefgh"),
+            ("i\xb7\x1d" * 345, "abcd" * 345),
+            ]:
+            assert self.binascii.b2a_base64(input) == expected + '\n'
+
+    def test_a2b_qp(self):
+        for input, expected in [
+            # these are the tests from CPython 2.7
+            ("= ", "= "),
+            ("==", "="),
+            ("=AX", "=AX"),
+            ("=00\r\n=00", "\x00\r\n\x00"),
+            # more tests follow
+            ("=", ""),
+            ("abc=", "abc"),
+            ("ab=\ncd", "abcd"),
+            ("ab=\r\ncd", "abcd"),
+            (''.join(["=%02x" % n for n in range(256)]),
+                          ''.join(map(chr, range(256)))),
+            (''.join(["=%02X" % n for n in range(256)]),
+                          ''.join(map(chr, range(256)))),
+            ]:
+            assert self.binascii.a2b_qp(input) == expected
+        #
+        for input, expected in [
+            ("xyz", "xyz"),
+            ("__", "  "),
+            ("a_b", "a b"),
+            ]:
+            assert self.binascii.a2b_qp(input, header=True) == expected
+
+    def test_b2a_qp(self):
+        for input, flags, expected in [
+            # these are the tests from CPython 2.7
+            ("\xff\r\n\xff\n\xff", {}, "=FF\r\n=FF\r\n=FF"),
+            ("0"*75+"\xff\r\n\xff\r\n\xff",{},"0"*75+"=\r\n=FF\r\n=FF\r\n=FF"),
+            ('\0\n', {}, '=00\n'),
+            ('\0\n', {'quotetabs': True}, '=00\n'),
+            ('foo\tbar\t\n', {}, 'foo\tbar=09\n'),
+            ('foo\tbar\t\n', {'quotetabs': True}, 'foo=09bar=09\n'),
+            ('.', {}, '=2E'),
+            ('.\n', {}, '=2E\n'),
+            ('a.\n', {}, 'a.\n'),
+            # more tests follow
+            ('_', {}, '_'),
+            ('_', {'header': True}, '=5F'),
+            ('.x', {}, '.x'),
+            ('.\r\nn', {}, '=2E\r\nn'),
+            ('\nn', {}, '\nn'),
+            ('\r\nn', {}, '\r\nn'),
+            ('\nn', {'istext': False}, '=0An'),
+            ('\r\nn', {'istext': False}, '=0D=0An'),
+            (' ', {}, '=20'),
+            ('\t', {}, '=09'),
+            (' x', {}, ' x'),
+            ('\tx', {}, '\tx'),
+            ('\x16x', {}, '=16x'),
+            (' x', {'quotetabs': True}, '=20x'),
+            ('\tx', {'quotetabs': True}, '=09x'),
+            (' \nn', {}, '=20\nn'),
+            ('\t\nn', {}, '=09\nn'),
+            ('x\nn', {}, 'x\nn'),
+            (' \r\nn', {}, '=20\r\nn'),
+            ('\t\r\nn', {}, '=09\r\nn'),
+            ('x\r\nn', {}, 'x\r\nn'),
+            ('x\nn', {'istext': False}, 'x=0An'),
+            ('   ', {}, '  =20'),
+            ('   ', {'header': True}, '__=20'),
+            ('   \nn', {}, '  =20\nn'),
+            ('   \nn', {'header': True}, '___\nn'),
+            ('   ', {}, '  =20'),
+            ('\t\t\t', {'header': True}, '\t\t=09'),
+            ('\t\t\t\nn', {}, '\t\t=09\nn'),
+            ('\t\t\t\nn', {'header': True}, '\t\t=09\nn'),
+            ]:
+            assert self.binascii.b2a_qp(input, **flags) == expected
+
+    def test_a2b_hqx(self):
+        for input, expected, done in [
+            ("", "", 0),
+            ("AAAA", "]u\xd7", 0),
+            ("A\nA\rAA", "]u\xd7", 0),
+            (":", "", 1),
+            ("A:", "", 1),
+            ("AA:", "]", 1),
+            ("AAA:", "]u", 1),
+            ("AAAA:", "]u\xd7", 1),
+            ("AAAA:foobarbaz", "]u\xd7", 1),
+            ("41-CZ:", "D\xe3\x19", 1),
+            ("41-CZl:", "D\xe3\x19\xbb", 1),
+            ("41-CZlm:", "D\xe3\x19\xbb\xbf", 1),
+            ("41-CZlm@:", "D\xe3\x19\xbb\xbf\x16", 1),
+            ]:
+            assert self.binascii.a2b_hqx(input) == (expected, done)
+        #
+        for incomplete in [
+            "A",
+            "AA",
+            "AAA",
+            "12345",
+            "123456",
+            "1234560",
+            ]:
+            raises(self.binascii.Incomplete, self.binascii.a2b_hqx, incomplete)
+        #
+        for bogus in [
+            "\x00",
+            ".",
+            "AAA AAAAAA:",
+            ]:
+            raises(self.binascii.Error, self.binascii.a2b_hqx, bogus)
+
+    def test_b2a_hqx(self):
+        for input, expected in [
+            ("", ""),
+            ("A", "33"),
+            ("AB", "38)"),
+            ("ABC", "38*$"),
+            ("ABCD", "38*$4!"),
+            ("ABCDE", "38*$4%8"),
+            ("ABCDEF", "38*$4%9'"),
+            ("ABCDEFG", "38*$4%9'4`"),
+            ("]u\xd7", "AAAA"),
+            ]:
+            assert self.binascii.b2a_hqx(input) == expected
+
+    def test_rledecode_hqx(self):
+        for input, expected in [
+            ("", ""),
+            ("hello world", "hello world"),
+            ("\x90\x00", "\x90"),
+            ("a\x90\x05", "a" * 5),
+            ("a\x90\xff", "a" * 0xFF),
+            ("abc\x90\x01def", "abcdef"),
+            ("abc\x90\x02def", "abccdef"),
+            ("abc\x90\x03def", "abcccdef"),
+            ("abc\x90\xa1def", "ab" + "c" * 0xA1 + "def"),
+            ("abc\x90\x03\x90\x02def", "abccccdef"),
+            ("abc\x90\x00\x90\x03def", "abc\x90\x90\x90def"),
+            ("abc\x90\x03\x90\x00def", "abccc\x90def"),
+            ]:
+            assert self.binascii.rledecode_hqx(input) == expected
+        #
+        for input in [
+            "\x90",
+            "a\x90",
+            "hello world\x90",
+            ]:
+            raises(self.binascii.Incomplete, self.binascii.rledecode_hqx,
+                   input)
+        #
+        raises(self.binascii.Error, self.binascii.rledecode_hqx, "\x90\x01")
+        raises(self.binascii.Error, self.binascii.rledecode_hqx, "\x90\x02")
+        raises(self.binascii.Error, self.binascii.rledecode_hqx, "\x90\xff")
+
+    def test_rlecode_hqx(self):
+        for input, expected in [
+            ("", ""),
+            ("hello world", "hello world"),
+            ("helllo world", "helllo world"),
+            ("hellllo world", "hel\x90\x04o world"),
+            ("helllllo world", "hel\x90\x05o world"),
+            ("aaa", "aaa"),
+            ("aaaa", "a\x90\x04"),
+            ("a" * 0xff, "a\x90\xff"),
+            ("a" * 0x100, "a\x90\xffa"),
+            ("a" * 0x101, "a\x90\xffaa"),
+            ("a" * 0x102, "a\x90\xffaaa"),      # see comments in the source
+            ("a" * 0x103, "a\x90\xffa\x90\x04"),
+            ("a" * 0x1fe, "a\x90\xffa\x90\xff"),
+            ("a" * 0x1ff, "a\x90\xffa\x90\xffa"),
+            ("\x90", "\x90\x00"),
+            ("\x90" * 2, "\x90\x00" * 2),
+            ("\x90" * 3, "\x90\x00" * 3),       # see comments in the source
+            ("\x90" * 345, "\x90\x00" * 345),
+            ]:
+            assert self.binascii.rlecode_hqx(input) == expected
+
+    def test_crc_hqx(self):
+        for input, initial, expected in [
+            ("", 0, 0),
+            ("", 123, 123),
+            ("hello", 321, 28955),
+            ("world", 65535, 12911),
+            ("uh", 40102, 37544),
+            ('a', 10000, 14338),
+            ('b', 10000, 2145),
+            ('c', 10000, 6208),
+            ('d', 10000, 26791),
+            ('e', 10000, 30854),
+            ('f', 10000, 18661),
+            ('g', 10000, 22724),
+            ('h', 10000, 43307),
+            ('i', 10000, 47370),
+            ('j', 10000, 35177),
+            ('k', 10000, 39240),
+            ('l', 10000, 59823),
+            ('m', 10000, 63886),
+            ('n', 10000, 51693),
+            ('o', 10000, 55756),
+            ('p', 10000, 14866),
+            ('q', 10000, 10803),
+            ('r', 10000, 6736),
+            ('s', 10000, 2673),
+            ('t', 10000, 31382),
+            ('u', 10000, 27319),
+            ('v', 10000, 23252),
+            ('w', 10000, 19189),
+            ('x', 10000, 47898),
+            ('y', 10000, 43835),
+            ('z', 10000, 39768),
+            ]:
+            assert self.binascii.crc_hqx(input, initial) == expected
+
+    def test_crc32(self):
+        for input, initial, expected in [
+            ("", 0, 0),
+            ("", 123, 123),
+            ("hello", 321, -348147686),
+            ("world", -2147483648, 32803080),
+            ("world", 2147483647, 942244330),
+            ('a', 10000, -184504832),
+            ('b', 10000, 1812594618),
+            ('c', 10000, 453955372),
+            ('d', 10000, -2056627569),
+            ('e', 10000, -227710439),
+            ('f', 10000, 1801730979),
+            ('g', 10000, 476252981),
+            ('h', 10000, -1931733340),
+            ('i', 10000, -69523918),
+            ('j', 10000, 1657960328),
+            ('k', 10000, 366298910),
+            ('l', 10000, -1951280451),
+            ('m', 10000, -55123413),
+            ('n', 10000, 1707062161),
+            ('o', 10000, 314082055),
+            ('p', 10000, -1615819022),
+            ('q', 10000, -390611356),
+            ('r', 10000, 1908338654),
+            ('s', 10000, 112844616),
+            ('t', 10000, -1730327829),
+            ('u', 10000, -270894467),
+            ('v', 10000, 1993550791),
+            ('w', 10000, 30677841),
+            ('x', 10000, -1855256896),
+            ('y', 10000, -429115818),
+            ('z', 10000, 2137352172),
+            ]:
+            assert self.binascii.crc32(input, initial) == expected
+
+    def test_hexlify(self):
+        for input, expected in [
+            ("", ""),
+            ("0", "30"),
+            ("1", "31"),
+            ("2", "32"),
+            ("8", "38"),
+            ("9", "39"),
+            ("A", "41"),
+            ("O", "4f"),
+            ("\xde", "de"),
+            ("ABC", "414243"),
+            ("\x00\x00\x00\xff\x00\x00", "000000ff0000"),
+            ("\x28\x9c\xc8\xc0\x3d\x8e", "289cc8c03d8e"),
+            ]:
+            assert self.binascii.hexlify(input) == expected
+            assert self.binascii.b2a_hex(input) == expected
+
+    def test_unhexlify(self):
+        for input, expected in [
+            ("", ""),
+            ("30", "0"),
+            ("31", "1"),
+            ("32", "2"),
+            ("38", "8"),
+            ("39", "9"),
+            ("41", "A"),
+            ("4F", "O"),
+            ("4f", "O"),
+            ("DE", "\xde"),
+            ("De", "\xde"),
+            ("dE", "\xde"),
+            ("de", "\xde"),
+            ("414243", "ABC"),
+            ("000000FF0000", "\x00\x00\x00\xff\x00\x00"),
+            ("289cc8C03d8e", "\x28\x9c\xc8\xc0\x3d\x8e"),
+            ]:
+            assert self.binascii.unhexlify(input) == expected
+            assert self.binascii.a2b_hex(input) == expected



More information about the Pypy-commit mailing list