| 831 | |
| 832 | |
| 833 | class AbstractUnpickleTests: |
| 834 | # Subclass must define self.loads. |
| 835 | |
| 836 | _testdata = create_data() |
| 837 | |
| 838 | def assert_is_copy(self, obj, objcopy, msg=None): |
| 839 | """Utility method to verify if two objects are copies of each others. |
| 840 | """ |
| 841 | if msg is None: |
| 842 | msg = "{!r} is not a copy of {!r}".format(obj, objcopy) |
| 843 | self.assertEqual(obj, objcopy, msg=msg) |
| 844 | self.assertIs(type(obj), type(objcopy), msg=msg) |
| 845 | if hasattr(obj, '__dict__'): |
| 846 | self.assertDictEqual(obj.__dict__, objcopy.__dict__, msg=msg) |
| 847 | self.assertIsNot(obj.__dict__, objcopy.__dict__, msg=msg) |
| 848 | if hasattr(obj, '__slots__'): |
| 849 | self.assertListEqual(obj.__slots__, objcopy.__slots__, msg=msg) |
| 850 | for slot in obj.__slots__: |
| 851 | self.assertEqual( |
| 852 | hasattr(obj, slot), hasattr(objcopy, slot), msg=msg) |
| 853 | self.assertEqual(getattr(obj, slot, None), |
| 854 | getattr(objcopy, slot, None), msg=msg) |
| 855 | |
| 856 | def check_unpickling_error(self, errors, data): |
| 857 | with self.subTest(data=data), \ |
| 858 | self.assertRaises(errors): |
| 859 | try: |
| 860 | self.loads(data) |
| 861 | except BaseException as exc: |
| 862 | if support.verbose > 1: |
| 863 | print('%-32r - %s: %s' % |
| 864 | (data, exc.__class__.__name__, exc)) |
| 865 | raise |
| 866 | |
| 867 | def test_load_from_data0(self): |
| 868 | self.assert_is_copy(self._testdata, self.loads(DATA0)) |
| 869 | |
| 870 | def test_load_from_data1(self): |
| 871 | self.assert_is_copy(self._testdata, self.loads(DATA1)) |
| 872 | |
| 873 | def test_load_from_data2(self): |
| 874 | self.assert_is_copy(self._testdata, self.loads(DATA2)) |
| 875 | |
| 876 | def test_load_from_data3(self): |
| 877 | self.assert_is_copy(self._testdata, self.loads(DATA3)) |
| 878 | |
| 879 | def test_load_from_data4(self): |
| 880 | self.assert_is_copy(self._testdata, self.loads(DATA4)) |
| 881 | |
| 882 | def test_load_classic_instance(self): |
| 883 | # See issue5180. Test loading 2.x pickles that |
| 884 | # contain an instance of old style class. |
| 885 | for X, args in [(C, ()), (D, ('x',)), (E, ())]: |
| 886 | xname = X.__name__.encode('ascii') |
| 887 | # Protocol 0 (text mode pickle): |
| 888 | """ |
| 889 | 0: ( MARK |
| 890 | 1: i INST '__main__ X' (MARK at 0) |
nothing calls this directly
no test coverage detected