diff -r 39ddcc5c7fb9 Lib/test/pickletester.py --- a/Lib/test/pickletester.py Sat Feb 25 19:26:39 2012 +0200 +++ b/Lib/test/pickletester.py Sat Mar 17 17:03:45 2012 +0100 @@ -1189,6 +1189,59 @@ dumped = b'\x80\x03X\x01\x00\x00\x00ar\xff\xff\xff\xff.' self.assertRaises(ValueError, self.loads, dumped) +class AbstractBytestrTests(unittest.TestCase): + def unpickleEqual(self, data, unpickled): + loaded = self.loads(data, encoding="bytes") + self.assertEqual(loaded, unpickled) + + def test_load_str_protocol_0(self): + """ Test str from protocol=0 + python 2: pickle.dumps('bytestring \x00\xa0', protocol=0) """ + self.unpickleEqual( + b"S'bytestring \\x00\\xa0'\np0\n.", + b'bytestring \x00\xa0') + + def test_load_str_protocol_1(self): + """ Test str from protocol=1 + python 2: pickle.dumps('bytestring \x00\xa0', protocol=1) """ + self.unpickleEqual( + b'U\rbytestring \x00\xa0q\x00.', + b'bytestring \x00\xa0') + + def test_load_str_protocol_2(self): + """ Test str from protocol=2 + python 2: pickle.dumps('bytestring \x00\xa0', protocol=2) """ + self.unpickleEqual( + b'\x80\x02U\rbytestring \x00\xa0q\x00.', + b'bytestring \x00\xa0') + + def test_load_unicode_protocol_0(self): + """ Test unicode with protocol=0 + python 2: pickle.dumps(u"\u041a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440", protocol=0) """ + self.unpickleEqual( + b'V\\u041a\\u043e\\u043c\\u043f\\u044c\\u044e\\u0442\\u0435\\u0440\np0\n.', + '\u041a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440') + + def test_load_unicode_protocol_1(self): + """ Test unicode with protocol=1 + python 2: pickle.dumps(u"\u041a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440", protocol=1) """ + self.unpickleEqual( + b'X\x12\x00\x00\x00\xd0\x9a\xd0\xbe\xd0\xbc\xd0\xbf\xd1\x8c\xd1\x8e\xd1\x82\xd0\xb5\xd1\x80q\x00.', + '\u041a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440') + + def test_load_unicode_protocol_2(self): + """ Test unicode with protocol=1 + python 2: pickle.dumps(u"\u041a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440", protocol=2) """ + self.unpickleEqual( + b'\x80\x02X\x12\x00\x00\x00\xd0\x9a\xd0\xbe\xd0\xbc\xd0\xbf\xd1\x8c\xd1\x8e\xd1\x82\xd0\xb5\xd1\x80q\x00.', + '\u041a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440') + + def test_load_long_str_protocol_1(self): + """ Test long str with protocol=1 + python 2: pickle.dumps('x'*300, protocol=1) """ + self.unpickleEqual( + b'T,\x01\x00\x00xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxq\x00.', + b'x'*300) class BigmemPickleTests(unittest.TestCase): diff -r 39ddcc5c7fb9 Lib/test/test_pickle.py --- a/Lib/test/test_pickle.py Sat Feb 25 19:26:39 2012 +0200 +++ b/Lib/test/test_pickle.py Sat Mar 17 17:03:45 2012 +0100 @@ -8,6 +8,7 @@ from test.pickletester import AbstractPersistentPicklerTests from test.pickletester import AbstractPicklerUnpicklerObjectTests from test.pickletester import BigmemPickleTests +from test.pickletester import AbstractBytestrTests try: import _pickle @@ -19,15 +20,13 @@ class PickleTests(AbstractPickleModuleTests): pass - -class PyPicklerTests(AbstractPickleTests): - +class PyPicklerBase: pickler = pickle._Pickler unpickler = pickle._Unpickler - def dumps(self, arg, proto=None): + def dumps(self, arg, proto=None, **kwds): f = io.BytesIO() - p = self.pickler(f, proto) + p = self.pickler(f, proto, **kwds) p.dump(arg) f.seek(0) return bytes(f.read()) @@ -37,6 +36,11 @@ u = self.unpickler(f, **kwds) return u.load() +class PyPicklerTests(PyPicklerBase, AbstractPickleTests): + pass + +class PyPicklerBytestrTests(PyPicklerBase, AbstractBytestrTests): + pass class InMemoryPickleTests(AbstractPickleTests, BigmemPickleTests): @@ -85,6 +89,10 @@ pickler = _pickle.Pickler unpickler = _pickle.Unpickler + class CPicklerBytestrTests(PyPicklerBytestrTests): + pickler = _pickle.Pickler + unpickler = _pickle.Unpickler + class CPersPicklerTests(PyPersPicklerTests): pickler = _pickle.Pickler unpickler = _pickle.Unpickler @@ -103,9 +111,9 @@ def test_main(): - tests = [PickleTests, PyPicklerTests, PyPersPicklerTests] + tests = [PickleTests, PyPicklerTests, PyPersPicklerTests, PyPicklerBytestrTests] if has_c_implementation: - tests.extend([CPicklerTests, CPersPicklerTests, + tests.extend([CPicklerTests, CPicklerBytestrTests, CPersPicklerTests, CDumpPickle_LoadPickle, DumpPickle_CLoadPickle, PyPicklerUnpicklerObjectTests, CPicklerUnpicklerObjectTests,