Index: Lib/test/test_binascii.py =================================================================== --- Lib/test/test_binascii.py (revision 77513) +++ Lib/test/test_binascii.py (working copy) @@ -5,13 +5,21 @@ import binascii import array +# Note: "*_hex" functions are aliases for "(un)hexlify" +b2a_functions = ['b2a_base64', 'b2a_hex', 'b2a_hqx', 'b2a_qp', 'b2a_uu', + 'hexlify', 'rlecode_hqx'] +a2b_functions = ['a2b_base64', 'a2b_hex', 'a2b_hqx', 'a2b_qp', 'a2b_uu', + 'unhexlify', 'rledecode_hqx'] +all_functions = a2b_functions + b2a_functions + ['crc32', 'crc_hqx'] + + class BinASCIITest(unittest.TestCase): type2test = str # Create binary test data rawdata = "The quick brown fox jumps over the lazy dog.\r\n" # Be slow so we don't depend on other modules - rawdata += "".join(map(chr, xrange(256))) + rawdata += str(bytearray(xrange(256))) rawdata += "\r\nHello world.\n" def setUp(self): @@ -24,31 +32,46 @@ def test_functions(self): # Check presence of all functions - funcs = [] - for suffix in "base64", "hqx", "uu", "hex": - prefixes = ["a2b_", "b2a_"] - if suffix == "hqx": - prefixes.extend(["crc_", "rlecode_", "rledecode_"]) - for prefix in prefixes: - name = prefix + suffix - self.assertTrue(hasattr(getattr(binascii, name), '__call__')) - self.assertRaises(TypeError, getattr(binascii, name)) - for name in ("hexlify", "unhexlify"): - self.assertTrue(hasattr(getattr(binascii, name), '__call__')) - self.assertRaises(TypeError, getattr(binascii, name)) + for func in all_functions: + self.assertTrue(hasattr(getattr(binascii, func), '__call__')) + self.assertRaises(TypeError, getattr(binascii, func)) + + def test_returned_value(self): + # Limit to the minimum of all limits (b2a_uu) + MAX_ALL = 45 + raw = self.rawdata[:MAX_ALL] + for fa, fb in zip(a2b_functions, b2a_functions): + a2b = getattr(binascii, fa) + b2a = getattr(binascii, fb) + try: + a = b2a(self.type2test(raw)) + res = a2b(self.type2test(a)) + except Exception, err: + self.fail("{}/{} conversion raises {!r}".format(fb, fa, err)) + if fb == 'b2a_hqx': + # b2a_hqx returns a tuple + res = res[0] + self.assertEqual(res, raw, "{}/{} conversion: " + "{!r} != {!r}".format(fb, fa, res, raw)) + self.assertIsInstance(res, str) + self.assertIsInstance(a, str) + self.assertLess(max(ord(c) for c in a), 128) + self.assertIsInstance(binascii.crc_hqx(raw, 0), int) + self.assertIsInstance(binascii.crc32(raw), int) def test_base64valid(self): # Test base64 with valid data MAX_BASE64 = 57 lines = [] - for i in range(0, len(self.data), MAX_BASE64): - b = self.data[i:i+MAX_BASE64] + for i in range(0, len(self.rawdata), MAX_BASE64): + b = self.type2test(self.rawdata[i:i+MAX_BASE64]) a = binascii.b2a_base64(b) lines.append(a) res = "" for line in lines: - b = binascii.a2b_base64(line) - res = res + b + a = self.type2test(line) + b = binascii.a2b_base64(a) + res += b self.assertEqual(res, self.rawdata) def test_base64invalid(self): @@ -57,47 +80,49 @@ MAX_BASE64 = 57 lines = [] for i in range(0, len(self.data), MAX_BASE64): - b = self.data[i:i+MAX_BASE64] + b = self.type2test(self.rawdata[i:i+MAX_BASE64]) a = binascii.b2a_base64(b) lines.append(a) - fillers = "" + fillers = bytearray() valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/" for i in xrange(256): c = chr(i) if c not in valid: - fillers += c + fillers.append(i) def addnoise(line): noise = fillers ratio = len(line) // len(noise) - res = "" + res = bytearray() while line and noise: if len(line) // len(noise) > ratio: c, line = line[0], line[1:] else: c, noise = noise[0], noise[1:] - res += c + res.append(c) return res + noise + line - res = "" + res = bytearray() for line in map(addnoise, lines): - b = binascii.a2b_base64(line) + a = self.type2test(str(line)) + b = binascii.a2b_base64(a) res += b self.assertEqual(res, self.rawdata) # Test base64 with just invalid characters, which should return # empty strings. TBD: shouldn't it raise an exception instead ? - self.assertEqual(binascii.a2b_base64(fillers), '') + self.assertEqual(binascii.a2b_base64(self.type2test(str(fillers))), '') def test_uu(self): MAX_UU = 45 lines = [] for i in range(0, len(self.data), MAX_UU): - b = self.data[i:i+MAX_UU] + b = self.type2test(self.rawdata[i:i+MAX_UU]) a = binascii.b2a_uu(b) lines.append(a) res = "" for line in lines: - b = binascii.a2b_uu(line) + a = self.type2test(line) + b = binascii.a2b_uu(a) res += b self.assertEqual(res, self.rawdata) @@ -113,19 +138,27 @@ self.assertEqual(binascii.b2a_uu('x'), '!> \n') def test_crc32(self): - crc = binascii.crc32("Test the CRC-32 of") - crc = binascii.crc32(" this string.", crc) + crc = binascii.crc32(self.type2test("Test the CRC-32 of")) + crc = binascii.crc32(self.type2test(" this string."), crc) self.assertEqual(crc, 1571220330) self.assertRaises(TypeError, binascii.crc32) - # The hqx test is in test_binhex.py + def test_hqx(self): + # Perform binhex4 style RLE-compression + # Then calculate the hexbin4 binary-to-ASCII translation + rle = binascii.rlecode_hqx(self.data) + a = binascii.b2a_hqx(self.type2test(rle)) + b, _ = binascii.a2b_hqx(self.type2test(a)) + res = binascii.rledecode_hqx(b) + + self.assertEqual(res, self.rawdata) def test_hex(self): # test hexlification s = '{s\005\000\000\000worldi\002\000\000\000s\005\000\000\000helloi\001\000\000\0000' - t = binascii.b2a_hex(s) - u = binascii.a2b_hex(t) + t = binascii.b2a_hex(self.type2test(s)) + u = binascii.a2b_hex(self.type2test(t)) self.assertEqual(s, u) self.assertRaises(TypeError, binascii.a2b_hex, t[:-1]) self.assertRaises(TypeError, binascii.a2b_hex, t[:-1] + 'q') @@ -137,11 +170,11 @@ def test_qp(self): # A test for SF bug 534347 (segfaults without the proper fix) try: - binascii.a2b_qp("", **{1:1}) + binascii.a2b_qp("", **{1: 1}) except TypeError: pass else: - self.fail("binascii.a2b_qp(**{1:1}) didn't raise TypeError") + self.fail("binascii.a2b_qp(**{1: 1}) didn't raise TypeError") self.assertEqual(binascii.a2b_qp("= "), "= ") self.assertEqual(binascii.a2b_qp("=="), "=") self.assertEqual(binascii.a2b_qp("=AX"), "=AX") @@ -167,13 +200,19 @@ def test_empty_string(self): # A test for SF bug #1022953. Make sure SystemError is not raised. - for n in ['b2a_qp', 'a2b_hex', 'b2a_base64', 'a2b_uu', 'a2b_qp', - 'b2a_hex', 'unhexlify', 'hexlify', 'crc32', 'b2a_hqx', - 'a2b_hqx', 'a2b_base64', 'rlecode_hqx', 'b2a_uu', - 'rledecode_hqx']: - f = getattr(binascii, n) - f('') - binascii.crc_hqx('', 0) + empty = self.type2test('') + for func in all_functions: + if func == 'crc_hqx': + # crc_hqx needs 2 arguments + binascii.crc_hqx(empty, 0) + continue + f = getattr(binascii, func) + try: + f(empty) + except SystemError, err: + self.fail("{}({!r}) raises SystemError: {}".format(func, empty, err)) + except Exception, err: + self.fail("{}({!r}) raises {!r}".format(func, empty, err)) class ArrayBinASCIITest(BinASCIITest): @@ -181,6 +220,10 @@ return array.array('c', s) +class BytearrayBinASCIITest(BinASCIITest): + type2test = bytearray + + class MemoryviewBinASCIITest(BinASCIITest): type2test = memoryview @@ -188,6 +231,7 @@ def test_main(): test_support.run_unittest(BinASCIITest, ArrayBinASCIITest, + BytearrayBinASCIITest, MemoryviewBinASCIITest) if __name__ == "__main__": Index: Modules/binascii.c =================================================================== --- Modules/binascii.c (revision 77513) +++ Modules/binascii.c (working copy) @@ -537,6 +537,7 @@ static PyObject * binascii_a2b_hqx(PyObject *self, PyObject *args) { + Py_buffer pascii; unsigned char *ascii_data, *bin_data; int leftbits = 0; unsigned char this_ch; @@ -545,19 +546,25 @@ Py_ssize_t len; int done = 0; - if ( !PyArg_ParseTuple(args, "t#:a2b_hqx", &ascii_data, &len) ) + if ( !PyArg_ParseTuple(args, "s*:a2b_hqx", &pascii) ) return NULL; + ascii_data = pascii.buf; + len = pascii.len; assert(len >= 0); - if (len > PY_SSIZE_T_MAX - 2) + if (len > PY_SSIZE_T_MAX - 2) { + PyBuffer_Release(&pascii); return PyErr_NoMemory(); + } /* Allocate a string that is too big (fixed later) Add two to the initial length to prevent interning which would preclude subsequent resizing. */ - if ( (rv=PyString_FromStringAndSize(NULL, len+2)) == NULL ) + if ( (rv=PyString_FromStringAndSize(NULL, len+2)) == NULL ) { + PyBuffer_Release(&pascii); return NULL; + } bin_data = (unsigned char *)PyString_AS_STRING(rv); for( ; len > 0 ; len--, ascii_data++ ) { @@ -567,6 +574,7 @@ continue; if ( this_ch == FAIL ) { PyErr_SetString(Error, "Illegal char"); + PyBuffer_Release(&pascii); Py_DECREF(rv); return NULL; } @@ -589,21 +597,25 @@ if ( leftbits && !done ) { PyErr_SetString(Incomplete, "String has incomplete number of bytes"); + PyBuffer_Release(&pascii); Py_DECREF(rv); return NULL; } if (_PyString_Resize(&rv, (bin_data - (unsigned char *)PyString_AS_STRING(rv))) < 0) { + PyBuffer_Release(&pascii); Py_DECREF(rv); rv = NULL; } if (rv) { PyObject *rrv = Py_BuildValue("Oi", rv, done); + PyBuffer_Release(&pascii); Py_DECREF(rv); return rrv; } + PyBuffer_Release(&pascii); return NULL; }