|
25 | 25 | import sys
|
26 | 26 | import tempfile
|
27 | 27 | import uuid
|
| 28 | +import pickle |
28 | 29 |
|
29 | 30 | from collections import abc, OrderedDict
|
30 | 31 | from io import BytesIO
|
@@ -1053,6 +1054,73 @@ def test_unicode_decode_error_handler(self):
|
1053 | 1054 | self.assertRaises(InvalidBSON, decode, invalid_both, CodecOptions(
|
1054 | 1055 | unicode_decode_error_handler="junk"))
|
1055 | 1056 |
|
| 1057 | + def round_trip_pickle(self, obj, pickled_with_older): |
| 1058 | + pickled_with_older_obj = pickle.loads(pickled_with_older) |
| 1059 | + for protocol in range(pickle.HIGHEST_PROTOCOL + 1): |
| 1060 | + pkl = pickle.dumps(obj, protocol=protocol) |
| 1061 | + obj2 = pickle.loads(pkl) |
| 1062 | + self.assertEqual(obj, obj2) |
| 1063 | + self.assertEqual(pickled_with_older_obj, obj2) |
| 1064 | + |
| 1065 | + def test_regex_pickling(self): |
| 1066 | + reg = Regex(".?") |
| 1067 | + pickled_with_3 = (b'\x80\x04\x959\x00\x00\x00\x00\x00\x00\x00\x8c\n' |
| 1068 | + b'bson.regex\x94\x8c\x05Regex\x94\x93\x94)\x81\x94}' |
| 1069 | + b'\x94(\x8c\x07pattern\x94\x8c\x02.?\x94\x8c\x05flag' |
| 1070 | + b's\x94K\x00ub.') |
| 1071 | + self.round_trip_pickle(reg, pickled_with_3) |
| 1072 | + |
| 1073 | + def test_timestamp_pickling(self): |
| 1074 | + ts = Timestamp(0, 1) |
| 1075 | + pickled_with_3 = (b'\x80\x04\x95Q\x00\x00\x00\x00\x00\x00\x00\x8c' |
| 1076 | + b'\x0ebson.timestamp\x94\x8c\tTimestamp\x94\x93\x94)' |
| 1077 | + b'\x81\x94}\x94(' |
| 1078 | + b'\x8c\x10_Timestamp__time\x94K\x00\x8c' |
| 1079 | + b'\x0f_Timestamp__inc\x94K\x01ub.') |
| 1080 | + self.round_trip_pickle(ts, pickled_with_3) |
| 1081 | + |
| 1082 | + def test_dbref_pickling(self): |
| 1083 | + dbr = DBRef("foo", 5) |
| 1084 | + pickled_with_3 = (b'\x80\x04\x95q\x00\x00\x00\x00\x00\x00\x00\x8c\n' |
| 1085 | + b'bson.dbref\x94\x8c\x05DBRef\x94\x93\x94)\x81\x94}' |
| 1086 | + b'\x94(\x8c\x12_DBRef__collection\x94\x8c\x03foo\x94' |
| 1087 | + b'\x8c\n_DBRef__id\x94K\x05\x8c\x10_DBRef__database' |
| 1088 | + b'\x94N\x8c\x0e_DBRef__kwargs\x94}\x94ub.') |
| 1089 | + self.round_trip_pickle(dbr, pickled_with_3) |
| 1090 | + |
| 1091 | + dbr = DBRef("foo", 5, database='db', kwargs1=None) |
| 1092 | + pickled_with_3 = (b'\x80\x04\x95\x81\x00\x00\x00\x00\x00\x00\x00\x8c' |
| 1093 | + b'\nbson.dbref\x94\x8c\x05DBRef\x94\x93\x94)\x81\x94}' |
| 1094 | + b'\x94(\x8c\x12_DBRef__collection\x94\x8c\x03foo\x94' |
| 1095 | + b'\x8c\n_DBRef__id\x94K\x05\x8c\x10_DBRef__database' |
| 1096 | + b'\x94\x8c\x02db\x94\x8c\x0e_DBRef__kwargs\x94}\x94' |
| 1097 | + b'\x8c\x07kwargs1\x94Nsub.') |
| 1098 | + |
| 1099 | + self.round_trip_pickle(dbr, pickled_with_3) |
| 1100 | + |
| 1101 | + def test_minkey_pickling(self): |
| 1102 | + mink = MinKey() |
| 1103 | + pickled_with_3 = (b'\x80\x04\x95\x1e\x00\x00\x00\x00\x00\x00\x00\x8c' |
| 1104 | + b'\x0cbson.min_key\x94\x8c\x06MinKey\x94\x93\x94)' |
| 1105 | + b'\x81\x94.') |
| 1106 | + |
| 1107 | + self.round_trip_pickle(mink, pickled_with_3) |
| 1108 | + |
| 1109 | + def test_maxkey_pickling(self): |
| 1110 | + maxk = MaxKey() |
| 1111 | + pickled_with_3 = (b'\x80\x04\x95\x1e\x00\x00\x00\x00\x00\x00\x00\x8c' |
| 1112 | + b'\x0cbson.max_key\x94\x8c\x06MaxKey\x94\x93\x94)' |
| 1113 | + b'\x81\x94.') |
| 1114 | + |
| 1115 | + self.round_trip_pickle(maxk, pickled_with_3) |
| 1116 | + |
| 1117 | + def test_int64_pickling(self): |
| 1118 | + i64 = Int64(9) |
| 1119 | + pickled_with_3 = (b'\x80\x04\x95\x1e\x00\x00\x00\x00\x00\x00\x00\x8c\n' |
| 1120 | + b'bson.int64\x94\x8c\x05Int64\x94\x93\x94K\t\x85\x94' |
| 1121 | + b'\x81\x94.') |
| 1122 | + self.round_trip_pickle(i64, pickled_with_3) |
| 1123 | + |
1056 | 1124 |
|
1057 | 1125 | if __name__ == "__main__":
|
1058 | 1126 | unittest.main()
|
0 commit comments