10c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# test_pickle dumps and loads pickles via pickle.py.
20c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# test_cpickle does the same, but via the cPickle module.
30c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# This test covers the other two cases, making pickles with one module and
40c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# loading them via the other. It also tests backwards compatibility with
50c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# previous version of Python by bouncing pickled objects through Python 2.4
60c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# and Python 2.5 running this file.
70c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
80c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiimport cPickle
90c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiimport os
100c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiimport os.path
110c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiimport pickle
120c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiimport subprocess
130c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiimport sys
140c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiimport types
150c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiimport unittest
160c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
170c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yifrom test import test_support
180c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
190c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# Most distro-supplied Pythons don't include the tests
200c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# or test support files, and some don't include a way to get these back even if
210c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# you're will to install extra packages (like Ubuntu). Doing things like this
220c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# "provides" a pickletester module for older versions of Python that may be
230c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# installed without it. Note that one other design for this involves messing
240c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# with sys.path, which is less precise.
250c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yimod_path = os.path.abspath(os.path.join(os.path.dirname(__file__),
260c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                                        "pickletester.py"))
270c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yipickletester = types.ModuleType("test.pickletester")
280c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiexec compile(open(mod_path).read(), mod_path, 'exec') in pickletester.__dict__
290c5958b1636c47ed7c284f859c8e805fd06a0e6Bill YiAbstractPickleTests = pickletester.AbstractPickleTests
300c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiif pickletester.__name__ in sys.modules:
310c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    raise RuntimeError("Did not expect to find test.pickletester loaded")
320c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yisys.modules[pickletester.__name__] = pickletester
330c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
340c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
350c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiclass DumpCPickle_LoadPickle(AbstractPickleTests):
360c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
370c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    error = KeyError
380c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
390c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def dumps(self, arg, proto=0, fast=False):
400c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        # Ignore fast
410c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        return cPickle.dumps(arg, proto)
420c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
430c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def loads(self, buf):
440c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        # Ignore fast
450c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        return pickle.loads(buf)
460c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
470c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiclass DumpPickle_LoadCPickle(AbstractPickleTests):
480c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
490c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    error = cPickle.BadPickleGet
500c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
510c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def dumps(self, arg, proto=0, fast=False):
520c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        # Ignore fast
530c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        return pickle.dumps(arg, proto)
540c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
550c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def loads(self, buf):
560c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        # Ignore fast
570c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        return cPickle.loads(buf)
580c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
590c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yidef have_python_version(name):
600c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    """Check whether the given name is a valid Python binary and has
610c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    test.test_support.
620c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
630c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    This respects your PATH.
640c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
650c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    Args:
660c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        name: short string name of a Python binary such as "python2.4".
670c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
680c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    Returns:
690c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        True if the name is valid, False otherwise.
700c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    """
710c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    return os.system(name + " -c 'import test.test_support'") == 0
720c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
730c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
740c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiclass AbstractCompatTests(AbstractPickleTests):
750c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
760c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    module = None
770c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    python = None
780c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    error = None
790c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
800c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def setUp(self):
810c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertTrue(self.python)
820c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertTrue(self.module)
830c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertTrue(self.error)
840c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
850c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def send_to_worker(self, python, obj, proto):
860c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        """Bounce a pickled object through another version of Python.
870c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
880c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        This will pickle the object, send it to a child process where it will be
890c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        unpickled, then repickled and sent back to the parent process.
900c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
910c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        Args:
920c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            python: the name of the Python binary to start.
930c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            obj: object to pickle.
940c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            proto: pickle protocol number to use.
950c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
960c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        Returns:
970c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            The pickled data received from the child process.
980c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        """
990c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        # Prevent the subprocess from picking up invalid .pyc files.
1000c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        target = __file__
1010c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        if target[-1] in ("c", "o"):
1020c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            target = target[:-1]
1030c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1040c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        data = self.module.dumps((proto, obj), proto)
1050c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        worker = subprocess.Popen([python, target, "worker"],
1060c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                                  stdin=subprocess.PIPE,
1070c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                                  stdout=subprocess.PIPE,
1080c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                                  stderr=subprocess.PIPE)
1090c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        stdout, stderr = worker.communicate(data)
1100c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        if worker.returncode != 0:
1110c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            raise RuntimeError(stderr)
1120c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        return stdout
1130c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1140c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def dumps(self, arg, proto=0, fast=False):
1150c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        return self.send_to_worker(self.python, arg, proto)
1160c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1170c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def loads(self, input):
1180c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        return self.module.loads(input)
1190c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1200c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    # These tests are disabled because they require some special setup
1210c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    # on the worker that's hard to keep in sync.
1220c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def test_global_ext1(self):
1230c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        pass
1240c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1250c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def test_global_ext2(self):
1260c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        pass
1270c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1280c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def test_global_ext4(self):
1290c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        pass
1300c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1310c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    # This is a cut-down version of pickletester's test_float. Backwards
1320c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    # compatibility for the values in for_bin_protos was explicitly broken in
1330c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    # r68903 to fix a bug.
1340c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def test_float(self):
1350c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        for_bin_protos = [4.94e-324, 1e-310]
1360c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        neg_for_bin_protos = [-x for x in for_bin_protos]
1370c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        test_values = [0.0, 7e-308, 6.626e-34, 0.1, 0.5,
1380c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                       3.14, 263.44582062374053, 6.022e23, 1e30]
1390c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        test_proto0_values = test_values + [-x for x in test_values]
1400c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        test_values = test_proto0_values + for_bin_protos + neg_for_bin_protos
1410c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1420c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        for value in test_proto0_values:
1430c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            pickle = self.dumps(value, 0)
1440c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            got = self.loads(pickle)
1450c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            self.assertEqual(value, got)
1460c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1470c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        for proto in pickletester.protocols[1:]:
1480c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            for value in test_values:
1490c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                pickle = self.dumps(value, proto)
1500c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                got = self.loads(pickle)
1510c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                self.assertEqual(value, got)
1520c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1530c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    # Backwards compatibility was explicitly broken in r67934 to fix a bug.
1540c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def test_unicode_high_plane(self):
1550c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        pass
1560c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1570c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    # This tests a fix that's in 2.7 only
1580c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def test_dynamic_class(self):
1590c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        pass
1600c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1610c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    if test_support.have_unicode:
1620c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        # This is a cut-down version of pickletester's test_unicode. Backwards
1630c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        # compatibility was explicitly broken in r67934 to fix a bug.
1640c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        def test_unicode(self):
1650c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            endcases = [u'', u'<\\u>', u'<\\\u1234>', u'<\n>', u'<\\>']
1660c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            for proto in pickletester.protocols:
1670c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                for u in endcases:
1680c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                    p = self.dumps(u, proto)
1690c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                    u2 = self.loads(p)
1700c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                    self.assertEqual(u2, u)
1710c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1720c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1730c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yidef run_compat_test(python_name):
1740c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    return (test_support.is_resource_enabled("xpickle") and
1750c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            have_python_version(python_name))
1760c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1770c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1780c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# Test backwards compatibility with Python 2.4.
1790c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiif not run_compat_test("python2.4"):
1800c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    class CPicklePython24Compat(unittest.TestCase):
1810c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        pass
1820c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yielse:
1830c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    class CPicklePython24Compat(AbstractCompatTests):
1840c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1850c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        module = cPickle
1860c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        python = "python2.4"
1870c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        error = cPickle.BadPickleGet
1880c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1890c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        # Disable these tests for Python 2.4. Making them pass would require
1900c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        # nontrivially monkeypatching the pickletester module in the worker.
1910c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        def test_reduce_calls_base(self):
1920c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            pass
1930c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1940c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        def test_reduce_ex_calls_base(self):
1950c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            pass
1960c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1970c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiclass PicklePython24Compat(CPicklePython24Compat):
1980c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1990c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    module = pickle
2000c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    error = KeyError
2010c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
2020c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
2030c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# Test backwards compatibility with Python 2.5.
2040c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiif not run_compat_test("python2.5"):
2050c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    class CPicklePython25Compat(unittest.TestCase):
2060c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        pass
2070c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yielse:
2080c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    class CPicklePython25Compat(AbstractCompatTests):
2090c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
2100c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        module = cPickle
2110c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        python = "python2.5"
2120c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        error = cPickle.BadPickleGet
2130c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
2140c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiclass PicklePython25Compat(CPicklePython25Compat):
2150c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
2160c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    module = pickle
2170c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    error = KeyError
2180c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
2190c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
2200c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# Test backwards compatibility with Python 2.6.
2210c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiif not run_compat_test("python2.6"):
2220c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    class CPicklePython26Compat(unittest.TestCase):
2230c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        pass
2240c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yielse:
2250c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    class CPicklePython26Compat(AbstractCompatTests):
2260c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
2270c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        module = cPickle
2280c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        python = "python2.6"
2290c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        error = cPickle.BadPickleGet
2300c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
2310c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiclass PicklePython26Compat(CPicklePython26Compat):
2320c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
2330c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    module = pickle
2340c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    error = KeyError
2350c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
2360c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
2370c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yidef worker_main(in_stream, out_stream):
2380c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    message = cPickle.load(in_stream)
2390c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    protocol, obj = message
2400c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    cPickle.dump(obj, out_stream, protocol)
2410c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
2420c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
2430c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yidef test_main():
2440c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    if not test_support.is_resource_enabled("xpickle"):
2450c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        print >>sys.stderr, "test_xpickle -- skipping backwards compat tests."
2460c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        print >>sys.stderr, "Use 'regrtest.py -u xpickle' to run them."
2470c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        sys.stderr.flush()
2480c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
2490c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    test_support.run_unittest(
2500c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        DumpCPickle_LoadPickle,
2510c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        DumpPickle_LoadCPickle,
2520c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        CPicklePython24Compat,
2530c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        CPicklePython25Compat,
2540c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        CPicklePython26Compat,
2550c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        PicklePython24Compat,
2560c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        PicklePython25Compat,
2570c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        PicklePython26Compat,
2580c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    )
2590c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
2600c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiif __name__ == "__main__":
2610c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    if "worker" in sys.argv:
2620c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        worker_main(sys.stdin, sys.stdout)
2630c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    else:
2640c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        test_main()
265