Index: Modules/binascii.c =================================================================== --- Modules/binascii.c (revision 76839) +++ Modules/binascii.c (working copy) @@ -739,7 +739,7 @@ PyObject *rv; Py_ssize_t in_len, out_len, out_len_left; - if ( !PyArg_ParseTuple(args, "s*:rledecode_hqx", &pin) ) + if ( !PyArg_ParseTuple(args, "y*:rledecode_hqx", &pin) ) return NULL; in_data = pin.buf; in_len = pin.len; @@ -1110,7 +1110,7 @@ char* retbuf; Py_ssize_t i, j; - if (!PyArg_ParseTuple(args, "s*:a2b_hex", &parg)) + if (!PyArg_ParseTuple(args, "y*:a2b_hex", &parg)) return NULL; argbuf = parg.buf; arglen = parg.len; @@ -1188,7 +1188,7 @@ static char *kwlist[] = {"data", "header", NULL}; int header = 0; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s*|i", kwlist, &pdata, + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y*|i", kwlist, &pdata, &header)) return NULL; data = pdata.buf; Index: Lib/email/base64mime.py =================================================================== --- Lib/email/base64mime.py (revision 76839) +++ Lib/email/base64mime.py (working copy) @@ -74,12 +74,12 @@ def body_encode(s, maxlinelen=76, eol=NL): - """Encode a string with base64. + r"""Encode a string with base64. Each line will be wrapped at, at most, maxlinelen characters (defaults to 76 characters). - Each line of encoded text will end with eol, which defaults to "\\n". Set + Each line of encoded text will end with eol, which defaults to "\n". Set this to "\r\n" if you will be using the result of this function directly in an email. """ Index: Lib/email/message.py =================================================================== --- Lib/email/message.py (revision 76839) +++ Lib/email/message.py (working copy) @@ -198,6 +198,8 @@ return None cte = self.get('content-transfer-encoding', '').lower() if cte == 'quoted-printable': + if isinstance(payload, str): + payload = payload.encode('raw-unicode-escape') return utils._qdecode(payload) elif cte == 'base64': try: Index: Lib/pickle.py =================================================================== --- Lib/pickle.py (revision 76839) +++ Lib/pickle.py (working copy) @@ -1311,7 +1311,7 @@ else: ashex = ashex[2:] assert len(ashex) & 1 == 0, (x, ashex) - binary = _binascii.unhexlify(ashex) + binary = _binascii.unhexlify(ashex.encode('ascii')) return bytes(binary[::-1]) def decode_long(data): Index: Lib/test/test_binascii.py =================================================================== --- Lib/test/test_binascii.py (revision 76839) +++ Lib/test/test_binascii.py (working copy) @@ -4,6 +4,17 @@ import unittest import binascii +all_functions = [ + 'a2b_base64', 'b2a_base64', + 'a2b_hex', 'b2a_hex', + 'a2b_hqx', 'b2a_hqx', + 'a2b_qp', 'b2a_qp', + 'a2b_uu', 'b2a_uu', + 'crc32', 'crc_hqx', + 'hexlify', 'unhexlify', + 'rlecode_hqx', 'rledecode_hqx', +] + class BinASCIITest(unittest.TestCase): # Create binary test data @@ -19,18 +30,9 @@ 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_base64valid(self): # Test base64 with valid data @@ -126,7 +128,7 @@ def test_qp(self): # A test for SF bug 534347 (segfaults without the proper fix) try: - binascii.a2b_qp("", **{1:1}) + binascii.a2b_qp(b"", **{1:1}) except TypeError: pass else: @@ -157,24 +159,21 @@ 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) + for func in all_functions: + if func == 'crc_hqx': + binascii.crc_hqx(b'', 0) + continue + f = getattr(binascii, func) try: f(b'') except SystemError as err: self.fail("%s(b'') raises SystemError: %s" % (n, err)) - binascii.crc_hqx(b'', 0) def test_no_binary_strings(self): - # b2a_ must not accept strings - for f in (binascii.b2a_uu, binascii.b2a_base64, - binascii.b2a_hqx, binascii.b2a_qp, - binascii.hexlify, binascii.rlecode_hqx, - binascii.crc_hqx, binascii.crc32): - self.assertRaises(TypeError, f, "test") + # Unencoded strings are not accepted. + for func in all_functions: + self.assertRaises(TypeError, getattr(binascii, func), "test") + self.assertRaises(TypeError, binascii.crc_hqx, "test", 0) def test_main(): support.run_unittest(BinASCIITest) Index: Lib/test/test_struct.py =================================================================== --- Lib/test/test_struct.py (revision 76839) +++ Lib/test/test_struct.py (working copy) @@ -196,6 +196,7 @@ expected = hex(expected)[2:] # chop "0x" if len(expected) & 1: expected = "0" + expected + expected = expected.encode('ascii') expected = unhexlify(expected) expected = b"\x00" * (self.bytesize - len(expected)) + expected @@ -242,6 +243,7 @@ expected = hex(expected)[2:] # chop "0x" if len(expected) & 1: expected = "0" + expected + expected = expected.encode('ascii') expected = unhexlify(expected) expected = b"\x00" * (self.bytesize - len(expected)) + expected