Collin Winter | 771d834 | 2009-04-16 03:18:06 +0000 | [diff] [blame] | 1 | import io |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 2 | import unittest |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 3 | import pickle |
Tim Peters | 31f119e | 2003-02-03 16:20:13 +0000 | [diff] [blame] | 4 | import pickletools |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 5 | import copyreg |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6 | from http.cookies import SimpleCookie |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 7 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 8 | from test.support import TestFailed, TESTFN, run_with_locale |
Tim Peters | e089c68 | 2001-04-10 03:41:41 +0000 | [diff] [blame] | 9 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 10 | from pickle import bytes_types |
| 11 | |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 12 | # Tests that try a number of pickle protocols should have a |
| 13 | # for proto in protocols: |
Tim Peters | 8587b3c | 2003-02-13 15:44:41 +0000 | [diff] [blame] | 14 | # kind of outer loop. |
Tim Peters | 8587b3c | 2003-02-13 15:44:41 +0000 | [diff] [blame] | 15 | protocols = range(pickle.HIGHEST_PROTOCOL + 1) |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 16 | |
Tim Peters | 22e7171 | 2003-02-03 22:27:38 +0000 | [diff] [blame] | 17 | |
| 18 | # Return True if opcode code appears in the pickle, else False. |
| 19 | def opcode_in_pickle(code, pickle): |
| 20 | for op, dummy, dummy in pickletools.genops(pickle): |
Guido van Rossum | cfe5f20 | 2007-05-08 21:26:54 +0000 | [diff] [blame] | 21 | if op.code == code.decode("latin-1"): |
Tim Peters | 22e7171 | 2003-02-03 22:27:38 +0000 | [diff] [blame] | 22 | return True |
| 23 | return False |
| 24 | |
Tim Peters | 8d2613a | 2003-02-11 16:40:16 +0000 | [diff] [blame] | 25 | # Return the number of times opcode code appears in pickle. |
| 26 | def count_opcode(code, pickle): |
| 27 | n = 0 |
| 28 | for op, dummy, dummy in pickletools.genops(pickle): |
Guido van Rossum | cfe5f20 | 2007-05-08 21:26:54 +0000 | [diff] [blame] | 29 | if op.code == code.decode("latin-1"): |
Tim Peters | 8d2613a | 2003-02-11 16:40:16 +0000 | [diff] [blame] | 30 | n += 1 |
| 31 | return n |
| 32 | |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 33 | # We can't very well test the extension registry without putting known stuff |
| 34 | # in it, but we have to be careful to restore its original state. Code |
| 35 | # should do this: |
| 36 | # |
| 37 | # e = ExtensionSaver(extension_code) |
| 38 | # try: |
| 39 | # fiddle w/ the extension registry's stuff for extension_code |
| 40 | # finally: |
| 41 | # e.restore() |
| 42 | |
| 43 | class ExtensionSaver: |
| 44 | # Remember current registration for code (if any), and remove it (if |
| 45 | # there is one). |
| 46 | def __init__(self, code): |
| 47 | self.code = code |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 48 | if code in copyreg._inverted_registry: |
| 49 | self.pair = copyreg._inverted_registry[code] |
| 50 | copyreg.remove_extension(self.pair[0], self.pair[1], code) |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 51 | else: |
| 52 | self.pair = None |
| 53 | |
| 54 | # Restore previous registration for code. |
| 55 | def restore(self): |
| 56 | code = self.code |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 57 | curpair = copyreg._inverted_registry.get(code) |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 58 | if curpair is not None: |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 59 | copyreg.remove_extension(curpair[0], curpair[1], code) |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 60 | pair = self.pair |
| 61 | if pair is not None: |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 62 | copyreg.add_extension(pair[0], pair[1], code) |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 63 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 64 | class C: |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 65 | def __eq__(self, other): |
| 66 | return self.__dict__ == other.__dict__ |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 67 | |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 68 | class D(C): |
| 69 | def __init__(self, arg): |
| 70 | pass |
| 71 | |
| 72 | class E(C): |
| 73 | def __getinitargs__(self): |
| 74 | return () |
| 75 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 76 | import __main__ |
| 77 | __main__.C = C |
| 78 | C.__module__ = "__main__" |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 79 | __main__.D = D |
| 80 | D.__module__ = "__main__" |
| 81 | __main__.E = E |
| 82 | E.__module__ = "__main__" |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 83 | |
| 84 | class myint(int): |
| 85 | def __init__(self, x): |
| 86 | self.str = str(x) |
| 87 | |
| 88 | class initarg(C): |
Guido van Rossum | 1444f67 | 2001-12-19 16:38:29 +0000 | [diff] [blame] | 89 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 90 | def __init__(self, a, b): |
| 91 | self.a = a |
| 92 | self.b = b |
| 93 | |
| 94 | def __getinitargs__(self): |
| 95 | return self.a, self.b |
| 96 | |
Guido van Rossum | 04a8661 | 2001-12-19 16:58:54 +0000 | [diff] [blame] | 97 | class metaclass(type): |
| 98 | pass |
| 99 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 100 | class use_metaclass(object, metaclass=metaclass): |
| 101 | pass |
Guido van Rossum | 04a8661 | 2001-12-19 16:58:54 +0000 | [diff] [blame] | 102 | |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 103 | # DATA0 .. DATA2 are the pickles we expect under the various protocols, for |
| 104 | # the object returned by create_data(). |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 105 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 106 | DATA0 = ( |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 107 | b'(lp0\nL0L\naL1L\naF2.0\nac' |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 108 | b'builtins\ncomplex\n' |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 109 | b'p1\n(F3.0\nF0.0\ntp2\nRp' |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 110 | b'3\naL1L\naL-1L\naL255L\naL-' |
| 111 | b'255L\naL-256L\naL65535L\na' |
| 112 | b'L-65535L\naL-65536L\naL2' |
| 113 | b'147483647L\naL-2147483' |
| 114 | b'647L\naL-2147483648L\na(' |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 115 | b'Vabc\np4\ng4\nccopyreg' |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 116 | b'\n_reconstructor\np5\n(' |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 117 | b'c__main__\nC\np6\ncbu' |
| 118 | b'iltins\nobject\np7\nNt' |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 119 | b'p8\nRp9\n(dp10\nVfoo\np1' |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 120 | b'1\nL1L\nsVbar\np12\nL2L\nsb' |
| 121 | b'g9\ntp13\nag13\naL5L\na.' |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 122 | ) |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 123 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 124 | # Disassembly of DATA0 |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 125 | DATA0_DIS = """\ |
| 126 | 0: ( MARK |
| 127 | 1: l LIST (MARK at 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 128 | 2: p PUT 0 |
| 129 | 5: L LONG 0 |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 130 | 9: a APPEND |
| 131 | 10: L LONG 1 |
| 132 | 14: a APPEND |
| 133 | 15: F FLOAT 2.0 |
| 134 | 20: a APPEND |
| 135 | 21: c GLOBAL 'builtins complex' |
| 136 | 39: p PUT 1 |
| 137 | 42: ( MARK |
| 138 | 43: F FLOAT 3.0 |
| 139 | 48: F FLOAT 0.0 |
| 140 | 53: t TUPLE (MARK at 42) |
| 141 | 54: p PUT 2 |
| 142 | 57: R REDUCE |
| 143 | 58: p PUT 3 |
| 144 | 61: a APPEND |
| 145 | 62: L LONG 1 |
| 146 | 66: a APPEND |
| 147 | 67: L LONG -1 |
| 148 | 72: a APPEND |
| 149 | 73: L LONG 255 |
| 150 | 79: a APPEND |
| 151 | 80: L LONG -255 |
| 152 | 87: a APPEND |
| 153 | 88: L LONG -256 |
| 154 | 95: a APPEND |
| 155 | 96: L LONG 65535 |
| 156 | 104: a APPEND |
| 157 | 105: L LONG -65535 |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 158 | 114: a APPEND |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 159 | 115: L LONG -65536 |
| 160 | 124: a APPEND |
| 161 | 125: L LONG 2147483647 |
| 162 | 138: a APPEND |
| 163 | 139: L LONG -2147483647 |
| 164 | 153: a APPEND |
| 165 | 154: L LONG -2147483648 |
| 166 | 168: a APPEND |
| 167 | 169: ( MARK |
| 168 | 170: V UNICODE 'abc' |
| 169 | 175: p PUT 4 |
| 170 | 178: g GET 4 |
| 171 | 181: c GLOBAL 'copyreg _reconstructor' |
| 172 | 205: p PUT 5 |
| 173 | 208: ( MARK |
| 174 | 209: c GLOBAL '__main__ C' |
| 175 | 221: p PUT 6 |
| 176 | 224: c GLOBAL 'builtins object' |
| 177 | 241: p PUT 7 |
| 178 | 244: N NONE |
| 179 | 245: t TUPLE (MARK at 208) |
| 180 | 246: p PUT 8 |
| 181 | 249: R REDUCE |
| 182 | 250: p PUT 9 |
| 183 | 253: ( MARK |
| 184 | 254: d DICT (MARK at 253) |
| 185 | 255: p PUT 10 |
| 186 | 259: V UNICODE 'foo' |
| 187 | 264: p PUT 11 |
| 188 | 268: L LONG 1 |
| 189 | 272: s SETITEM |
| 190 | 273: V UNICODE 'bar' |
| 191 | 278: p PUT 12 |
| 192 | 282: L LONG 2 |
| 193 | 286: s SETITEM |
| 194 | 287: b BUILD |
| 195 | 288: g GET 9 |
| 196 | 291: t TUPLE (MARK at 169) |
| 197 | 292: p PUT 13 |
| 198 | 296: a APPEND |
| 199 | 297: g GET 13 |
| 200 | 301: a APPEND |
| 201 | 302: L LONG 5 |
| 202 | 306: a APPEND |
| 203 | 307: . STOP |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 204 | highest protocol among opcodes = 0 |
| 205 | """ |
| 206 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 207 | DATA1 = ( |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 208 | b']q\x00(K\x00K\x01G@\x00\x00\x00\x00\x00\x00\x00c' |
| 209 | b'builtins\ncomplex\nq\x01' |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 210 | b'(G@\x08\x00\x00\x00\x00\x00\x00G\x00\x00\x00\x00\x00\x00\x00\x00t' |
| 211 | b'q\x02Rq\x03K\x01J\xff\xff\xff\xffK\xffJ\x01\xff\xff\xffJ' |
| 212 | b'\x00\xff\xff\xffM\xff\xffJ\x01\x00\xff\xffJ\x00\x00\xff\xffJ\xff\xff' |
| 213 | b'\xff\x7fJ\x01\x00\x00\x80J\x00\x00\x00\x80(X\x03\x00\x00\x00ab' |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 214 | b'cq\x04h\x04ccopyreg\n_reco' |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 215 | b'nstructor\nq\x05(c__main' |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 216 | b'__\nC\nq\x06cbuiltins\n' |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 217 | b'object\nq\x07Ntq\x08Rq\t}q\n(' |
| 218 | b'X\x03\x00\x00\x00fooq\x0bK\x01X\x03\x00\x00\x00bar' |
| 219 | b'q\x0cK\x02ubh\ttq\rh\rK\x05e.' |
| 220 | ) |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 221 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 222 | # Disassembly of DATA1 |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 223 | DATA1_DIS = """\ |
| 224 | 0: ] EMPTY_LIST |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 225 | 1: q BINPUT 0 |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 226 | 3: ( MARK |
| 227 | 4: K BININT1 0 |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 228 | 6: K BININT1 1 |
| 229 | 8: G BINFLOAT 2.0 |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 230 | 17: c GLOBAL 'builtins complex' |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 231 | 35: q BINPUT 1 |
| 232 | 37: ( MARK |
| 233 | 38: G BINFLOAT 3.0 |
| 234 | 47: G BINFLOAT 0.0 |
| 235 | 56: t TUPLE (MARK at 37) |
| 236 | 57: q BINPUT 2 |
| 237 | 59: R REDUCE |
| 238 | 60: q BINPUT 3 |
| 239 | 62: K BININT1 1 |
| 240 | 64: J BININT -1 |
| 241 | 69: K BININT1 255 |
| 242 | 71: J BININT -255 |
| 243 | 76: J BININT -256 |
| 244 | 81: M BININT2 65535 |
| 245 | 84: J BININT -65535 |
| 246 | 89: J BININT -65536 |
| 247 | 94: J BININT 2147483647 |
| 248 | 99: J BININT -2147483647 |
| 249 | 104: J BININT -2147483648 |
| 250 | 109: ( MARK |
| 251 | 110: X BINUNICODE 'abc' |
| 252 | 118: q BINPUT 4 |
| 253 | 120: h BINGET 4 |
| 254 | 122: c GLOBAL 'copyreg _reconstructor' |
| 255 | 146: q BINPUT 5 |
| 256 | 148: ( MARK |
| 257 | 149: c GLOBAL '__main__ C' |
| 258 | 161: q BINPUT 6 |
| 259 | 163: c GLOBAL 'builtins object' |
| 260 | 180: q BINPUT 7 |
| 261 | 182: N NONE |
| 262 | 183: t TUPLE (MARK at 148) |
| 263 | 184: q BINPUT 8 |
| 264 | 186: R REDUCE |
| 265 | 187: q BINPUT 9 |
| 266 | 189: } EMPTY_DICT |
| 267 | 190: q BINPUT 10 |
| 268 | 192: ( MARK |
| 269 | 193: X BINUNICODE 'foo' |
| 270 | 201: q BINPUT 11 |
| 271 | 203: K BININT1 1 |
| 272 | 205: X BINUNICODE 'bar' |
| 273 | 213: q BINPUT 12 |
| 274 | 215: K BININT1 2 |
| 275 | 217: u SETITEMS (MARK at 192) |
| 276 | 218: b BUILD |
| 277 | 219: h BINGET 9 |
| 278 | 221: t TUPLE (MARK at 109) |
| 279 | 222: q BINPUT 13 |
| 280 | 224: h BINGET 13 |
| 281 | 226: K BININT1 5 |
| 282 | 228: e APPENDS (MARK at 3) |
| 283 | 229: . STOP |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 284 | highest protocol among opcodes = 1 |
| 285 | """ |
Tim Peters | e0c446b | 2001-10-18 21:57:37 +0000 | [diff] [blame] | 286 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 287 | DATA2 = ( |
| 288 | b'\x80\x02]q\x00(K\x00K\x01G@\x00\x00\x00\x00\x00\x00\x00c' |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 289 | b'builtins\ncomplex\n' |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 290 | b'q\x01G@\x08\x00\x00\x00\x00\x00\x00G\x00\x00\x00\x00\x00\x00\x00\x00' |
| 291 | b'\x86q\x02Rq\x03K\x01J\xff\xff\xff\xffK\xffJ\x01\xff\xff\xff' |
| 292 | b'J\x00\xff\xff\xffM\xff\xffJ\x01\x00\xff\xffJ\x00\x00\xff\xffJ\xff' |
| 293 | b'\xff\xff\x7fJ\x01\x00\x00\x80J\x00\x00\x00\x80(X\x03\x00\x00\x00a' |
| 294 | b'bcq\x04h\x04c__main__\nC\nq\x05' |
| 295 | b')\x81q\x06}q\x07(X\x03\x00\x00\x00fooq\x08K\x01' |
| 296 | b'X\x03\x00\x00\x00barq\tK\x02ubh\x06tq\nh' |
| 297 | b'\nK\x05e.' |
| 298 | ) |
Tim Peters | fc27375 | 2003-03-02 04:54:24 +0000 | [diff] [blame] | 299 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 300 | # Disassembly of DATA2 |
Tim Peters | fc27375 | 2003-03-02 04:54:24 +0000 | [diff] [blame] | 301 | DATA2_DIS = """\ |
| 302 | 0: \x80 PROTO 2 |
| 303 | 2: ] EMPTY_LIST |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 304 | 3: q BINPUT 0 |
Tim Peters | fc27375 | 2003-03-02 04:54:24 +0000 | [diff] [blame] | 305 | 5: ( MARK |
| 306 | 6: K BININT1 0 |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 307 | 8: K BININT1 1 |
| 308 | 10: G BINFLOAT 2.0 |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 309 | 19: c GLOBAL 'builtins complex' |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 310 | 37: q BINPUT 1 |
| 311 | 39: G BINFLOAT 3.0 |
| 312 | 48: G BINFLOAT 0.0 |
| 313 | 57: \x86 TUPLE2 |
| 314 | 58: q BINPUT 2 |
| 315 | 60: R REDUCE |
| 316 | 61: q BINPUT 3 |
| 317 | 63: K BININT1 1 |
| 318 | 65: J BININT -1 |
| 319 | 70: K BININT1 255 |
| 320 | 72: J BININT -255 |
| 321 | 77: J BININT -256 |
| 322 | 82: M BININT2 65535 |
| 323 | 85: J BININT -65535 |
| 324 | 90: J BININT -65536 |
| 325 | 95: J BININT 2147483647 |
| 326 | 100: J BININT -2147483647 |
| 327 | 105: J BININT -2147483648 |
| 328 | 110: ( MARK |
| 329 | 111: X BINUNICODE 'abc' |
| 330 | 119: q BINPUT 4 |
| 331 | 121: h BINGET 4 |
| 332 | 123: c GLOBAL '__main__ C' |
| 333 | 135: q BINPUT 5 |
| 334 | 137: ) EMPTY_TUPLE |
| 335 | 138: \x81 NEWOBJ |
| 336 | 139: q BINPUT 6 |
| 337 | 141: } EMPTY_DICT |
| 338 | 142: q BINPUT 7 |
| 339 | 144: ( MARK |
| 340 | 145: X BINUNICODE 'foo' |
| 341 | 153: q BINPUT 8 |
| 342 | 155: K BININT1 1 |
| 343 | 157: X BINUNICODE 'bar' |
| 344 | 165: q BINPUT 9 |
| 345 | 167: K BININT1 2 |
| 346 | 169: u SETITEMS (MARK at 144) |
| 347 | 170: b BUILD |
| 348 | 171: h BINGET 6 |
| 349 | 173: t TUPLE (MARK at 110) |
| 350 | 174: q BINPUT 10 |
| 351 | 176: h BINGET 10 |
| 352 | 178: K BININT1 5 |
| 353 | 180: e APPENDS (MARK at 5) |
| 354 | 181: . STOP |
Tim Peters | fc27375 | 2003-03-02 04:54:24 +0000 | [diff] [blame] | 355 | highest protocol among opcodes = 2 |
| 356 | """ |
| 357 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 358 | # set([1,2]) pickled from 2.x with protocol 2 |
| 359 | DATA3 = b'\x80\x02c__builtin__\nset\nq\x00]q\x01(K\x01K\x02e\x85q\x02Rq\x03.' |
| 360 | |
| 361 | # xrange(5) pickled from 2.x with protocol 2 |
| 362 | DATA4 = b'\x80\x02c__builtin__\nxrange\nq\x00K\x00K\x05K\x01\x87q\x01Rq\x02.' |
| 363 | |
| 364 | # a SimpleCookie() object pickled from 2.x with protocol 2 |
| 365 | DATA5 = (b'\x80\x02cCookie\nSimpleCookie\nq\x00)\x81q\x01U\x03key' |
| 366 | b'q\x02cCookie\nMorsel\nq\x03)\x81q\x04(U\x07commentq\x05U' |
| 367 | b'\x00q\x06U\x06domainq\x07h\x06U\x06secureq\x08h\x06U\x07' |
| 368 | b'expiresq\th\x06U\x07max-ageq\nh\x06U\x07versionq\x0bh\x06U' |
| 369 | b'\x04pathq\x0ch\x06U\x08httponlyq\rh\x06u}q\x0e(U\x0b' |
| 370 | b'coded_valueq\x0fU\x05valueq\x10h\x10h\x10h\x02h\x02ubs}q\x11b.') |
| 371 | |
| 372 | # set([3]) pickled from 2.x with protocol 2 |
| 373 | DATA6 = b'\x80\x02c__builtin__\nset\nq\x00]q\x01K\x03a\x85q\x02Rq\x03.' |
| 374 | |
| 375 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 376 | def create_data(): |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 377 | c = C() |
| 378 | c.foo = 1 |
| 379 | c.bar = 2 |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 380 | x = [0, 1, 2.0, 3.0+0j] |
Tim Peters | 461922a | 2001-04-09 20:07:05 +0000 | [diff] [blame] | 381 | # Append some integer test cases at cPickle.c's internal size |
| 382 | # cutoffs. |
| 383 | uint1max = 0xff |
| 384 | uint2max = 0xffff |
| 385 | int4max = 0x7fffffff |
| 386 | x.extend([1, -1, |
| 387 | uint1max, -uint1max, -uint1max-1, |
| 388 | uint2max, -uint2max, -uint2max-1, |
| 389 | int4max, -int4max, -int4max-1]) |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 390 | y = ('abc', 'abc', c, c) |
| 391 | x.append(y) |
| 392 | x.append(y) |
| 393 | x.append(5) |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 394 | return x |
Tim Peters | c58440f | 2001-04-09 17:16:31 +0000 | [diff] [blame] | 395 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 396 | class AbstractPickleTests(unittest.TestCase): |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 397 | # Subclass must define self.dumps, self.loads. |
Tim Peters | c58440f | 2001-04-09 17:16:31 +0000 | [diff] [blame] | 398 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 399 | _testdata = create_data() |
Tim Peters | c58440f | 2001-04-09 17:16:31 +0000 | [diff] [blame] | 400 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 401 | def setUp(self): |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 402 | pass |
Tim Peters | c58440f | 2001-04-09 17:16:31 +0000 | [diff] [blame] | 403 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 404 | def test_misc(self): |
| 405 | # test various datatypes not tested by testdata |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 406 | for proto in protocols: |
| 407 | x = myint(4) |
| 408 | s = self.dumps(x, proto) |
| 409 | y = self.loads(s) |
| 410 | self.assertEqual(x, y) |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 411 | |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 412 | x = (1, ()) |
| 413 | s = self.dumps(x, proto) |
| 414 | y = self.loads(s) |
| 415 | self.assertEqual(x, y) |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 416 | |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 417 | x = initarg(1, x) |
| 418 | s = self.dumps(x, proto) |
| 419 | y = self.loads(s) |
| 420 | self.assertEqual(x, y) |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 421 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 422 | # XXX test __reduce__ protocol? |
| 423 | |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 424 | def test_roundtrip_equality(self): |
| 425 | expected = self._testdata |
| 426 | for proto in protocols: |
| 427 | s = self.dumps(expected, proto) |
| 428 | got = self.loads(s) |
| 429 | self.assertEqual(expected, got) |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 430 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 431 | def test_load_from_data0(self): |
| 432 | self.assertEqual(self._testdata, self.loads(DATA0)) |
| 433 | |
| 434 | def test_load_from_data1(self): |
| 435 | self.assertEqual(self._testdata, self.loads(DATA1)) |
| 436 | |
| 437 | def test_load_from_data2(self): |
| 438 | self.assertEqual(self._testdata, self.loads(DATA2)) |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 439 | |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 440 | def test_load_classic_instance(self): |
| 441 | # See issue5180. Test loading 2.x pickles that |
| 442 | # contain an instance of old style class. |
| 443 | for X, args in [(C, ()), (D, ('x',)), (E, ())]: |
| 444 | xname = X.__name__.encode('ascii') |
| 445 | # Protocol 0 (text mode pickle): |
| 446 | """ |
| 447 | 0: ( MARK |
| 448 | 1: i INST '__main__ X' (MARK at 0) |
| 449 | 15: p PUT 0 |
| 450 | 18: ( MARK |
| 451 | 19: d DICT (MARK at 18) |
| 452 | 20: p PUT 1 |
| 453 | 23: b BUILD |
| 454 | 24: . STOP |
| 455 | """ |
| 456 | pickle0 = (b"(i__main__\n" |
| 457 | b"X\n" |
| 458 | b"p0\n" |
| 459 | b"(dp1\nb.").replace(b'X', xname) |
| 460 | self.assertEqual(X(*args), self.loads(pickle0)) |
| 461 | |
| 462 | # Protocol 1 (binary mode pickle) |
| 463 | """ |
| 464 | 0: ( MARK |
| 465 | 1: c GLOBAL '__main__ X' |
| 466 | 15: q BINPUT 0 |
| 467 | 17: o OBJ (MARK at 0) |
| 468 | 18: q BINPUT 1 |
| 469 | 20: } EMPTY_DICT |
| 470 | 21: q BINPUT 2 |
| 471 | 23: b BUILD |
| 472 | 24: . STOP |
| 473 | """ |
| 474 | pickle1 = (b'(c__main__\n' |
| 475 | b'X\n' |
| 476 | b'q\x00oq\x01}q\x02b.').replace(b'X', xname) |
| 477 | self.assertEqual(X(*args), self.loads(pickle1)) |
| 478 | |
| 479 | # Protocol 2 (pickle2 = b'\x80\x02' + pickle1) |
| 480 | """ |
| 481 | 0: \x80 PROTO 2 |
| 482 | 2: ( MARK |
| 483 | 3: c GLOBAL '__main__ X' |
| 484 | 17: q BINPUT 0 |
| 485 | 19: o OBJ (MARK at 2) |
| 486 | 20: q BINPUT 1 |
| 487 | 22: } EMPTY_DICT |
| 488 | 23: q BINPUT 2 |
| 489 | 25: b BUILD |
| 490 | 26: . STOP |
| 491 | """ |
| 492 | pickle2 = (b'\x80\x02(c__main__\n' |
| 493 | b'X\n' |
| 494 | b'q\x00oq\x01}q\x02b.').replace(b'X', xname) |
| 495 | self.assertEqual(X(*args), self.loads(pickle2)) |
| 496 | |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 497 | # There are gratuitous differences between pickles produced by |
| 498 | # pickle and cPickle, largely because cPickle starts PUT indices at |
| 499 | # 1 and pickle starts them at 0. See XXX comment in cPickle's put2() -- |
| 500 | # there's a comment with an exclamation point there whose meaning |
| 501 | # is a mystery. cPickle also suppresses PUT for objects with a refcount |
| 502 | # of 1. |
| 503 | def dont_test_disassembly(self): |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 504 | from io import StringIO |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 505 | from pickletools import dis |
| 506 | |
| 507 | for proto, expected in (0, DATA0_DIS), (1, DATA1_DIS): |
| 508 | s = self.dumps(self._testdata, proto) |
| 509 | filelike = StringIO() |
| 510 | dis(s, out=filelike) |
| 511 | got = filelike.getvalue() |
| 512 | self.assertEqual(expected, got) |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 513 | |
| 514 | def test_recursive_list(self): |
| 515 | l = [] |
| 516 | l.append(l) |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 517 | for proto in protocols: |
| 518 | s = self.dumps(l, proto) |
| 519 | x = self.loads(s) |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 520 | self.assertEqual(len(x), 1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 521 | self.assertTrue(x is x[0]) |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 522 | |
Collin Winter | 8ca69de | 2009-05-26 16:53:41 +0000 | [diff] [blame] | 523 | def test_recursive_tuple(self): |
| 524 | t = ([],) |
| 525 | t[0].append(t) |
| 526 | for proto in protocols: |
| 527 | s = self.dumps(t, proto) |
| 528 | x = self.loads(s) |
| 529 | self.assertEqual(len(x), 1) |
| 530 | self.assertEqual(len(x[0]), 1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 531 | self.assertTrue(x is x[0][0]) |
Collin Winter | 8ca69de | 2009-05-26 16:53:41 +0000 | [diff] [blame] | 532 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 533 | def test_recursive_dict(self): |
| 534 | d = {} |
| 535 | d[1] = d |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 536 | for proto in protocols: |
| 537 | s = self.dumps(d, proto) |
| 538 | x = self.loads(s) |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 539 | self.assertEqual(list(x.keys()), [1]) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 540 | self.assertTrue(x[1] is x) |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 541 | |
| 542 | def test_recursive_inst(self): |
| 543 | i = C() |
| 544 | i.attr = i |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 545 | for proto in protocols: |
| 546 | s = self.dumps(i, 2) |
| 547 | x = self.loads(s) |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 548 | self.assertEqual(dir(x), dir(i)) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 549 | self.assertTrue(x.attr is x) |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 550 | |
| 551 | def test_recursive_multi(self): |
| 552 | l = [] |
| 553 | d = {1:l} |
| 554 | i = C() |
| 555 | i.attr = d |
| 556 | l.append(i) |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 557 | for proto in protocols: |
| 558 | s = self.dumps(l, proto) |
| 559 | x = self.loads(s) |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 560 | self.assertEqual(len(x), 1) |
| 561 | self.assertEqual(dir(x[0]), dir(i)) |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 562 | self.assertEqual(list(x[0].attr.keys()), [1]) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 563 | self.assertTrue(x[0].attr[1] is x) |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 564 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 565 | def test_get(self): |
| 566 | self.assertRaises(KeyError, self.loads, b'g0\np0') |
| 567 | self.assertEquals(self.loads(b'((Kdtp0\nh\x00l.))'), [(100,), (100,)]) |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 568 | |
| 569 | def test_insecure_strings(self): |
Guido van Rossum | 39478e8 | 2007-08-27 17:23:59 +0000 | [diff] [blame] | 570 | # XXX Some of these tests are temporarily disabled |
| 571 | insecure = [b"abc", b"2 + 2", # not quoted |
| 572 | ## b"'abc' + 'def'", # not a single quoted string |
| 573 | b"'abc", # quote is not closed |
| 574 | b"'abc\"", # open quote and close quote don't match |
| 575 | b"'abc' ?", # junk after close quote |
| 576 | b"'\\'", # trailing backslash |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 577 | # some tests of the quoting rules |
Guido van Rossum | 39478e8 | 2007-08-27 17:23:59 +0000 | [diff] [blame] | 578 | ## b"'abc\"\''", |
| 579 | ## b"'\\\\a\'\'\'\\\'\\\\\''", |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 580 | ] |
Guido van Rossum | 39478e8 | 2007-08-27 17:23:59 +0000 | [diff] [blame] | 581 | for b in insecure: |
| 582 | buf = b"S" + b + b"\012p0\012." |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 583 | self.assertRaises(ValueError, self.loads, buf) |
| 584 | |
Walter Dörwald | 9b77553 | 2007-06-08 14:30:53 +0000 | [diff] [blame] | 585 | def test_unicode(self): |
Benjamin Peterson | d148630 | 2008-12-27 16:58:50 +0000 | [diff] [blame] | 586 | endcases = ['', '<\\u>', '<\\\u1234>', '<\n>', |
Victor Stinner | 485fb56 | 2010-04-13 11:07:24 +0000 | [diff] [blame] | 587 | '<\\>', '<\\\U00012345>', |
| 588 | # surrogates |
| 589 | '<\udc80>'] |
Walter Dörwald | 9b77553 | 2007-06-08 14:30:53 +0000 | [diff] [blame] | 590 | for proto in protocols: |
| 591 | for u in endcases: |
| 592 | p = self.dumps(u, proto) |
| 593 | u2 = self.loads(p) |
| 594 | self.assertEqual(u2, u) |
Tim Peters | e089c68 | 2001-04-10 03:41:41 +0000 | [diff] [blame] | 595 | |
Alexandre Vassalotti | 554d878 | 2008-12-27 07:32:41 +0000 | [diff] [blame] | 596 | def test_unicode_high_plane(self): |
| 597 | t = '\U00012345' |
| 598 | for proto in protocols: |
| 599 | p = self.dumps(t, proto) |
| 600 | t2 = self.loads(p) |
| 601 | self.assertEqual(t2, t) |
| 602 | |
Guido van Rossum | f416981 | 2008-03-17 22:56:06 +0000 | [diff] [blame] | 603 | def test_bytes(self): |
| 604 | for proto in protocols: |
| 605 | for u in b'', b'xyz', b'xyz'*100: |
| 606 | p = self.dumps(u) |
| 607 | self.assertEqual(self.loads(p), u) |
| 608 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 609 | def test_ints(self): |
| 610 | import sys |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 611 | for proto in protocols: |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 612 | n = sys.maxsize |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 613 | while n: |
| 614 | for expected in (-n, n): |
| 615 | s = self.dumps(expected, proto) |
| 616 | n2 = self.loads(s) |
| 617 | self.assertEqual(expected, n2) |
| 618 | n = n >> 1 |
Tim Peters | 19ef62d | 2001-08-28 22:21:18 +0000 | [diff] [blame] | 619 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 620 | def test_maxint64(self): |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 621 | maxint64 = (1 << 63) - 1 |
Guido van Rossum | 39478e8 | 2007-08-27 17:23:59 +0000 | [diff] [blame] | 622 | data = b'I' + str(maxint64).encode("ascii") + b'\n.' |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 623 | got = self.loads(data) |
| 624 | self.assertEqual(got, maxint64) |
| 625 | |
| 626 | # Try too with a bogus literal. |
Guido van Rossum | 39478e8 | 2007-08-27 17:23:59 +0000 | [diff] [blame] | 627 | data = b'I' + str(maxint64).encode("ascii") + b'JUNK\n.' |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 628 | self.assertRaises(ValueError, self.loads, data) |
| 629 | |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 630 | def test_long(self): |
| 631 | for proto in protocols: |
Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 632 | # 256 bytes is where LONG4 begins. |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 633 | for nbits in 1, 8, 8*254, 8*255, 8*256, 8*257: |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 634 | nbase = 1 << nbits |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 635 | for npos in nbase-1, nbase, nbase+1: |
| 636 | for n in npos, -npos: |
| 637 | pickle = self.dumps(n, proto) |
| 638 | got = self.loads(pickle) |
| 639 | self.assertEqual(n, got) |
| 640 | # Try a monster. This is quadratic-time in protos 0 & 1, so don't |
| 641 | # bother with those. |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 642 | nbase = int("deadbeeffeedface", 16) |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 643 | nbase += nbase << 1000000 |
| 644 | for n in nbase, -nbase: |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 645 | p = self.dumps(n, 2) |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 646 | got = self.loads(p) |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 647 | self.assertEqual(n, got) |
| 648 | |
Mark Dickinson | cddcf44 | 2009-01-24 21:46:33 +0000 | [diff] [blame] | 649 | def test_float(self): |
| 650 | test_values = [0.0, 4.94e-324, 1e-310, 7e-308, 6.626e-34, 0.1, 0.5, |
| 651 | 3.14, 263.44582062374053, 6.022e23, 1e30] |
| 652 | test_values = test_values + [-x for x in test_values] |
| 653 | for proto in protocols: |
| 654 | for value in test_values: |
| 655 | pickle = self.dumps(value, proto) |
| 656 | got = self.loads(pickle) |
| 657 | self.assertEqual(value, got) |
| 658 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 659 | @run_with_locale('LC_ALL', 'de_DE', 'fr_FR') |
| 660 | def test_float_format(self): |
Guido van Rossum | f416981 | 2008-03-17 22:56:06 +0000 | [diff] [blame] | 661 | # make sure that floats are formatted locale independent with proto 0 |
| 662 | self.assertEqual(self.dumps(1.2, 0)[0:3], b'F1.') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 663 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 664 | def test_reduce(self): |
Tim Peters | 19ef62d | 2001-08-28 22:21:18 +0000 | [diff] [blame] | 665 | pass |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 666 | |
| 667 | def test_getinitargs(self): |
| 668 | pass |
| 669 | |
Guido van Rossum | 04a8661 | 2001-12-19 16:58:54 +0000 | [diff] [blame] | 670 | def test_metaclass(self): |
| 671 | a = use_metaclass() |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 672 | for proto in protocols: |
| 673 | s = self.dumps(a, proto) |
| 674 | b = self.loads(s) |
| 675 | self.assertEqual(a.__class__, b.__class__) |
Guido van Rossum | 04a8661 | 2001-12-19 16:58:54 +0000 | [diff] [blame] | 676 | |
Michael W. Hudson | 7bb466a | 2002-03-05 13:27:58 +0000 | [diff] [blame] | 677 | def test_structseq(self): |
| 678 | import time |
Michael W. Hudson | 0e02530 | 2002-03-06 17:11:18 +0000 | [diff] [blame] | 679 | import os |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 680 | |
| 681 | t = time.localtime() |
| 682 | for proto in protocols: |
| 683 | s = self.dumps(t, proto) |
Michael W. Hudson | 0e02530 | 2002-03-06 17:11:18 +0000 | [diff] [blame] | 684 | u = self.loads(s) |
| 685 | self.assertEqual(t, u) |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 686 | if hasattr(os, "stat"): |
| 687 | t = os.stat(os.curdir) |
| 688 | s = self.dumps(t, proto) |
| 689 | u = self.loads(s) |
| 690 | self.assertEqual(t, u) |
| 691 | if hasattr(os, "statvfs"): |
| 692 | t = os.statvfs(os.curdir) |
| 693 | s = self.dumps(t, proto) |
| 694 | u = self.loads(s) |
| 695 | self.assertEqual(t, u) |
Michael W. Hudson | 7bb466a | 2002-03-05 13:27:58 +0000 | [diff] [blame] | 696 | |
Guido van Rossum | d6c9e63 | 2003-01-28 03:49:52 +0000 | [diff] [blame] | 697 | # Tests for protocol 2 |
| 698 | |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 699 | def test_proto(self): |
| 700 | build_none = pickle.NONE + pickle.STOP |
| 701 | for proto in protocols: |
| 702 | expected = build_none |
| 703 | if proto >= 2: |
Guido van Rossum | cfe5f20 | 2007-05-08 21:26:54 +0000 | [diff] [blame] | 704 | expected = pickle.PROTO + bytes([proto]) + expected |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 705 | p = self.dumps(None, proto) |
| 706 | self.assertEqual(p, expected) |
| 707 | |
| 708 | oob = protocols[-1] + 1 # a future protocol |
Guido van Rossum | cfe5f20 | 2007-05-08 21:26:54 +0000 | [diff] [blame] | 709 | badpickle = pickle.PROTO + bytes([oob]) + build_none |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 710 | try: |
| 711 | self.loads(badpickle) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 712 | except ValueError as detail: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 713 | self.assertTrue(str(detail).startswith( |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 714 | "unsupported pickle protocol")) |
| 715 | else: |
| 716 | self.fail("expected bad protocol number to raise ValueError") |
| 717 | |
Guido van Rossum | d6c9e63 | 2003-01-28 03:49:52 +0000 | [diff] [blame] | 718 | def test_long1(self): |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 719 | x = 12345678910111213141516178920 |
Tim Peters | 61bf257 | 2003-02-03 21:31:22 +0000 | [diff] [blame] | 720 | for proto in protocols: |
| 721 | s = self.dumps(x, proto) |
| 722 | y = self.loads(s) |
| 723 | self.assertEqual(x, y) |
Tim Peters | 22e7171 | 2003-02-03 22:27:38 +0000 | [diff] [blame] | 724 | self.assertEqual(opcode_in_pickle(pickle.LONG1, s), proto >= 2) |
Guido van Rossum | d6c9e63 | 2003-01-28 03:49:52 +0000 | [diff] [blame] | 725 | |
| 726 | def test_long4(self): |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 727 | x = 12345678910111213141516178920 << (256*8) |
Tim Peters | 61bf257 | 2003-02-03 21:31:22 +0000 | [diff] [blame] | 728 | for proto in protocols: |
| 729 | s = self.dumps(x, proto) |
| 730 | y = self.loads(s) |
| 731 | self.assertEqual(x, y) |
Tim Peters | 22e7171 | 2003-02-03 22:27:38 +0000 | [diff] [blame] | 732 | self.assertEqual(opcode_in_pickle(pickle.LONG4, s), proto >= 2) |
Guido van Rossum | d6c9e63 | 2003-01-28 03:49:52 +0000 | [diff] [blame] | 733 | |
Guido van Rossum | 44f0ea5 | 2003-01-28 04:14:51 +0000 | [diff] [blame] | 734 | def test_short_tuples(self): |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 735 | # Map (proto, len(tuple)) to expected opcode. |
| 736 | expected_opcode = {(0, 0): pickle.TUPLE, |
| 737 | (0, 1): pickle.TUPLE, |
| 738 | (0, 2): pickle.TUPLE, |
| 739 | (0, 3): pickle.TUPLE, |
| 740 | (0, 4): pickle.TUPLE, |
| 741 | |
| 742 | (1, 0): pickle.EMPTY_TUPLE, |
| 743 | (1, 1): pickle.TUPLE, |
| 744 | (1, 2): pickle.TUPLE, |
| 745 | (1, 3): pickle.TUPLE, |
| 746 | (1, 4): pickle.TUPLE, |
| 747 | |
| 748 | (2, 0): pickle.EMPTY_TUPLE, |
| 749 | (2, 1): pickle.TUPLE1, |
| 750 | (2, 2): pickle.TUPLE2, |
| 751 | (2, 3): pickle.TUPLE3, |
| 752 | (2, 4): pickle.TUPLE, |
Guido van Rossum | f416981 | 2008-03-17 22:56:06 +0000 | [diff] [blame] | 753 | |
| 754 | (3, 0): pickle.EMPTY_TUPLE, |
| 755 | (3, 1): pickle.TUPLE1, |
| 756 | (3, 2): pickle.TUPLE2, |
| 757 | (3, 3): pickle.TUPLE3, |
| 758 | (3, 4): pickle.TUPLE, |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 759 | } |
Guido van Rossum | 44f0ea5 | 2003-01-28 04:14:51 +0000 | [diff] [blame] | 760 | a = () |
Guido van Rossum | 025bc2f | 2003-01-28 04:20:02 +0000 | [diff] [blame] | 761 | b = (1,) |
| 762 | c = (1, 2) |
| 763 | d = (1, 2, 3) |
| 764 | e = (1, 2, 3, 4) |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 765 | for proto in protocols: |
Guido van Rossum | 44f0ea5 | 2003-01-28 04:14:51 +0000 | [diff] [blame] | 766 | for x in a, b, c, d, e: |
| 767 | s = self.dumps(x, proto) |
| 768 | y = self.loads(s) |
| 769 | self.assertEqual(x, y, (proto, x, s, y)) |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 770 | expected = expected_opcode[proto, len(x)] |
Tim Peters | 22e7171 | 2003-02-03 22:27:38 +0000 | [diff] [blame] | 771 | self.assertEqual(opcode_in_pickle(expected, s), True) |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 772 | |
Guido van Rossum | 7d97d31 | 2003-01-28 04:25:27 +0000 | [diff] [blame] | 773 | def test_singletons(self): |
Tim Peters | 61bf257 | 2003-02-03 21:31:22 +0000 | [diff] [blame] | 774 | # Map (proto, singleton) to expected opcode. |
| 775 | expected_opcode = {(0, None): pickle.NONE, |
| 776 | (1, None): pickle.NONE, |
| 777 | (2, None): pickle.NONE, |
Guido van Rossum | f416981 | 2008-03-17 22:56:06 +0000 | [diff] [blame] | 778 | (3, None): pickle.NONE, |
Tim Peters | 61bf257 | 2003-02-03 21:31:22 +0000 | [diff] [blame] | 779 | |
| 780 | (0, True): pickle.INT, |
| 781 | (1, True): pickle.INT, |
| 782 | (2, True): pickle.NEWTRUE, |
Guido van Rossum | f416981 | 2008-03-17 22:56:06 +0000 | [diff] [blame] | 783 | (3, True): pickle.NEWTRUE, |
Tim Peters | 61bf257 | 2003-02-03 21:31:22 +0000 | [diff] [blame] | 784 | |
| 785 | (0, False): pickle.INT, |
| 786 | (1, False): pickle.INT, |
| 787 | (2, False): pickle.NEWFALSE, |
Guido van Rossum | f416981 | 2008-03-17 22:56:06 +0000 | [diff] [blame] | 788 | (3, False): pickle.NEWFALSE, |
Tim Peters | 61bf257 | 2003-02-03 21:31:22 +0000 | [diff] [blame] | 789 | } |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 790 | for proto in protocols: |
Guido van Rossum | 7d97d31 | 2003-01-28 04:25:27 +0000 | [diff] [blame] | 791 | for x in None, False, True: |
| 792 | s = self.dumps(x, proto) |
| 793 | y = self.loads(s) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 794 | self.assertTrue(x is y, (proto, x, s, y)) |
Tim Peters | 61bf257 | 2003-02-03 21:31:22 +0000 | [diff] [blame] | 795 | expected = expected_opcode[proto, x] |
Tim Peters | 22e7171 | 2003-02-03 22:27:38 +0000 | [diff] [blame] | 796 | self.assertEqual(opcode_in_pickle(expected, s), True) |
Tim Peters | 3c67d79 | 2003-02-02 17:59:11 +0000 | [diff] [blame] | 797 | |
Guido van Rossum | 533dbcf | 2003-01-28 17:55:05 +0000 | [diff] [blame] | 798 | def test_newobj_tuple(self): |
Guido van Rossum | 3d8c01b | 2003-01-28 19:48:18 +0000 | [diff] [blame] | 799 | x = MyTuple([1, 2, 3]) |
| 800 | x.foo = 42 |
| 801 | x.bar = "hello" |
Tim Peters | 894453a | 2003-02-03 22:32:18 +0000 | [diff] [blame] | 802 | for proto in protocols: |
| 803 | s = self.dumps(x, proto) |
| 804 | y = self.loads(s) |
| 805 | self.assertEqual(tuple(x), tuple(y)) |
| 806 | self.assertEqual(x.__dict__, y.__dict__) |
Guido van Rossum | 533dbcf | 2003-01-28 17:55:05 +0000 | [diff] [blame] | 807 | |
| 808 | def test_newobj_list(self): |
Guido van Rossum | 3d8c01b | 2003-01-28 19:48:18 +0000 | [diff] [blame] | 809 | x = MyList([1, 2, 3]) |
| 810 | x.foo = 42 |
| 811 | x.bar = "hello" |
Tim Peters | 894453a | 2003-02-03 22:32:18 +0000 | [diff] [blame] | 812 | for proto in protocols: |
| 813 | s = self.dumps(x, proto) |
| 814 | y = self.loads(s) |
| 815 | self.assertEqual(list(x), list(y)) |
| 816 | self.assertEqual(x.__dict__, y.__dict__) |
Guido van Rossum | 533dbcf | 2003-01-28 17:55:05 +0000 | [diff] [blame] | 817 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 818 | def test_newobj_generic(self): |
Tim Peters | 5013bd9 | 2003-02-03 22:28:41 +0000 | [diff] [blame] | 819 | for proto in protocols: |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 820 | for C in myclasses: |
| 821 | B = C.__base__ |
| 822 | x = C(C.sample) |
| 823 | x.foo = 42 |
| 824 | s = self.dumps(x, proto) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 825 | y = self.loads(s) |
| 826 | detail = (proto, C, B, x, y, type(y)) |
| 827 | self.assertEqual(B(x), B(y), detail) |
| 828 | self.assertEqual(x.__dict__, y.__dict__, detail) |
| 829 | |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 830 | # Register a type with copyreg, with extension code extcode. Pickle |
Tim Peters | 22e7171 | 2003-02-03 22:27:38 +0000 | [diff] [blame] | 831 | # an object of that type. Check that the resulting pickle uses opcode |
| 832 | # (EXT[124]) under proto 2, and not in proto 1. |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 833 | |
Tim Peters | 22e7171 | 2003-02-03 22:27:38 +0000 | [diff] [blame] | 834 | def produce_global_ext(self, extcode, opcode): |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 835 | e = ExtensionSaver(extcode) |
Guido van Rossum | 0322d0f | 2003-01-29 06:12:46 +0000 | [diff] [blame] | 836 | try: |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 837 | copyreg.add_extension(__name__, "MyList", extcode) |
Guido van Rossum | 0322d0f | 2003-01-29 06:12:46 +0000 | [diff] [blame] | 838 | x = MyList([1, 2, 3]) |
| 839 | x.foo = 42 |
| 840 | x.bar = "hello" |
| 841 | |
Tim Peters | 22e7171 | 2003-02-03 22:27:38 +0000 | [diff] [blame] | 842 | # Dump using protocol 1 for comparison. |
Guido van Rossum | 0322d0f | 2003-01-29 06:12:46 +0000 | [diff] [blame] | 843 | s1 = self.dumps(x, 1) |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 844 | self.assertIn(__name__.encode("utf-8"), s1) |
| 845 | self.assertIn(b"MyList", s1) |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 846 | self.assertEqual(opcode_in_pickle(opcode, s1), False) |
| 847 | |
Guido van Rossum | 0322d0f | 2003-01-29 06:12:46 +0000 | [diff] [blame] | 848 | y = self.loads(s1) |
| 849 | self.assertEqual(list(x), list(y)) |
| 850 | self.assertEqual(x.__dict__, y.__dict__) |
Guido van Rossum | 0322d0f | 2003-01-29 06:12:46 +0000 | [diff] [blame] | 851 | |
Tim Peters | 22e7171 | 2003-02-03 22:27:38 +0000 | [diff] [blame] | 852 | # Dump using protocol 2 for test. |
Guido van Rossum | 0322d0f | 2003-01-29 06:12:46 +0000 | [diff] [blame] | 853 | s2 = self.dumps(x, 2) |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 854 | self.assertNotIn(__name__.encode("utf-8"), s2) |
| 855 | self.assertNotIn(b"MyList", s2) |
Guido van Rossum | cfe5f20 | 2007-05-08 21:26:54 +0000 | [diff] [blame] | 856 | self.assertEqual(opcode_in_pickle(opcode, s2), True, repr(s2)) |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 857 | |
Guido van Rossum | 0322d0f | 2003-01-29 06:12:46 +0000 | [diff] [blame] | 858 | y = self.loads(s2) |
| 859 | self.assertEqual(list(x), list(y)) |
| 860 | self.assertEqual(x.__dict__, y.__dict__) |
Guido van Rossum | 0322d0f | 2003-01-29 06:12:46 +0000 | [diff] [blame] | 861 | |
| 862 | finally: |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 863 | e.restore() |
Tim Peters | 22e7171 | 2003-02-03 22:27:38 +0000 | [diff] [blame] | 864 | |
| 865 | def test_global_ext1(self): |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 866 | self.produce_global_ext(0x00000001, pickle.EXT1) # smallest EXT1 code |
| 867 | self.produce_global_ext(0x000000ff, pickle.EXT1) # largest EXT1 code |
Guido van Rossum | 0322d0f | 2003-01-29 06:12:46 +0000 | [diff] [blame] | 868 | |
| 869 | def test_global_ext2(self): |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 870 | self.produce_global_ext(0x00000100, pickle.EXT2) # smallest EXT2 code |
| 871 | self.produce_global_ext(0x0000ffff, pickle.EXT2) # largest EXT2 code |
| 872 | self.produce_global_ext(0x0000abcd, pickle.EXT2) # check endianness |
Guido van Rossum | 0322d0f | 2003-01-29 06:12:46 +0000 | [diff] [blame] | 873 | |
| 874 | def test_global_ext4(self): |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 875 | self.produce_global_ext(0x00010000, pickle.EXT4) # smallest EXT4 code |
| 876 | self.produce_global_ext(0x7fffffff, pickle.EXT4) # largest EXT4 code |
| 877 | self.produce_global_ext(0x12abcdef, pickle.EXT4) # check endianness |
| 878 | |
Tim Peters | 8d2613a | 2003-02-11 16:40:16 +0000 | [diff] [blame] | 879 | def test_list_chunking(self): |
| 880 | n = 10 # too small to chunk |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 881 | x = list(range(n)) |
Tim Peters | 8d2613a | 2003-02-11 16:40:16 +0000 | [diff] [blame] | 882 | for proto in protocols: |
| 883 | s = self.dumps(x, proto) |
| 884 | y = self.loads(s) |
| 885 | self.assertEqual(x, y) |
| 886 | num_appends = count_opcode(pickle.APPENDS, s) |
| 887 | self.assertEqual(num_appends, proto > 0) |
| 888 | |
| 889 | n = 2500 # expect at least two chunks when proto > 0 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 890 | x = list(range(n)) |
Tim Peters | 8d2613a | 2003-02-11 16:40:16 +0000 | [diff] [blame] | 891 | for proto in protocols: |
| 892 | s = self.dumps(x, proto) |
| 893 | y = self.loads(s) |
| 894 | self.assertEqual(x, y) |
| 895 | num_appends = count_opcode(pickle.APPENDS, s) |
| 896 | if proto == 0: |
| 897 | self.assertEqual(num_appends, 0) |
| 898 | else: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 899 | self.assertTrue(num_appends >= 2) |
Tim Peters | 8d2613a | 2003-02-11 16:40:16 +0000 | [diff] [blame] | 900 | |
| 901 | def test_dict_chunking(self): |
| 902 | n = 10 # too small to chunk |
| 903 | x = dict.fromkeys(range(n)) |
| 904 | for proto in protocols: |
| 905 | s = self.dumps(x, proto) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 906 | self.assertIsInstance(s, bytes_types) |
Tim Peters | 8d2613a | 2003-02-11 16:40:16 +0000 | [diff] [blame] | 907 | y = self.loads(s) |
| 908 | self.assertEqual(x, y) |
| 909 | num_setitems = count_opcode(pickle.SETITEMS, s) |
| 910 | self.assertEqual(num_setitems, proto > 0) |
| 911 | |
| 912 | n = 2500 # expect at least two chunks when proto > 0 |
| 913 | x = dict.fromkeys(range(n)) |
| 914 | for proto in protocols: |
| 915 | s = self.dumps(x, proto) |
| 916 | y = self.loads(s) |
| 917 | self.assertEqual(x, y) |
| 918 | num_setitems = count_opcode(pickle.SETITEMS, s) |
| 919 | if proto == 0: |
| 920 | self.assertEqual(num_setitems, 0) |
| 921 | else: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 922 | self.assertTrue(num_setitems >= 2) |
Guido van Rossum | 0322d0f | 2003-01-29 06:12:46 +0000 | [diff] [blame] | 923 | |
Tim Peters | e9ef203 | 2003-02-13 18:42:00 +0000 | [diff] [blame] | 924 | def test_simple_newobj(self): |
| 925 | x = object.__new__(SimpleNewObj) # avoid __init__ |
| 926 | x.abc = 666 |
| 927 | for proto in protocols: |
| 928 | s = self.dumps(x, proto) |
| 929 | self.assertEqual(opcode_in_pickle(pickle.NEWOBJ, s), proto >= 2) |
| 930 | y = self.loads(s) # will raise TypeError if __init__ called |
| 931 | self.assertEqual(y.abc, 666) |
| 932 | self.assertEqual(x.__dict__, y.__dict__) |
| 933 | |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 934 | def test_newobj_list_slots(self): |
| 935 | x = SlotList([1, 2, 3]) |
| 936 | x.foo = 42 |
| 937 | x.bar = "hello" |
| 938 | s = self.dumps(x, 2) |
| 939 | y = self.loads(s) |
| 940 | self.assertEqual(list(x), list(y)) |
| 941 | self.assertEqual(x.__dict__, y.__dict__) |
| 942 | self.assertEqual(x.foo, y.foo) |
| 943 | self.assertEqual(x.bar, y.bar) |
| 944 | |
Guido van Rossum | 2a30b21 | 2003-02-18 22:41:24 +0000 | [diff] [blame] | 945 | def test_reduce_overrides_default_reduce_ex(self): |
Collin Winter | 771d834 | 2009-04-16 03:18:06 +0000 | [diff] [blame] | 946 | for proto in protocols: |
Guido van Rossum | 2a30b21 | 2003-02-18 22:41:24 +0000 | [diff] [blame] | 947 | x = REX_one() |
| 948 | self.assertEqual(x._reduce_called, 0) |
| 949 | s = self.dumps(x, proto) |
| 950 | self.assertEqual(x._reduce_called, 1) |
| 951 | y = self.loads(s) |
| 952 | self.assertEqual(y._reduce_called, 0) |
| 953 | |
| 954 | def test_reduce_ex_called(self): |
Collin Winter | 771d834 | 2009-04-16 03:18:06 +0000 | [diff] [blame] | 955 | for proto in protocols: |
Guido van Rossum | 2a30b21 | 2003-02-18 22:41:24 +0000 | [diff] [blame] | 956 | x = REX_two() |
| 957 | self.assertEqual(x._proto, None) |
| 958 | s = self.dumps(x, proto) |
| 959 | self.assertEqual(x._proto, proto) |
| 960 | y = self.loads(s) |
| 961 | self.assertEqual(y._proto, None) |
| 962 | |
| 963 | def test_reduce_ex_overrides_reduce(self): |
Collin Winter | 771d834 | 2009-04-16 03:18:06 +0000 | [diff] [blame] | 964 | for proto in protocols: |
Guido van Rossum | 2a30b21 | 2003-02-18 22:41:24 +0000 | [diff] [blame] | 965 | x = REX_three() |
| 966 | self.assertEqual(x._proto, None) |
| 967 | s = self.dumps(x, proto) |
| 968 | self.assertEqual(x._proto, proto) |
| 969 | y = self.loads(s) |
| 970 | self.assertEqual(y._proto, None) |
| 971 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 972 | def test_reduce_ex_calls_base(self): |
Collin Winter | 771d834 | 2009-04-16 03:18:06 +0000 | [diff] [blame] | 973 | for proto in protocols: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 974 | x = REX_four() |
| 975 | self.assertEqual(x._proto, None) |
| 976 | s = self.dumps(x, proto) |
| 977 | self.assertEqual(x._proto, proto) |
| 978 | y = self.loads(s) |
| 979 | self.assertEqual(y._proto, proto) |
| 980 | |
| 981 | def test_reduce_calls_base(self): |
Collin Winter | 771d834 | 2009-04-16 03:18:06 +0000 | [diff] [blame] | 982 | for proto in protocols: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 983 | x = REX_five() |
| 984 | self.assertEqual(x._reduce_called, 0) |
| 985 | s = self.dumps(x, proto) |
| 986 | self.assertEqual(x._reduce_called, 1) |
| 987 | y = self.loads(s) |
| 988 | self.assertEqual(y._reduce_called, 1) |
| 989 | |
Alexandre Vassalotti | 1f9d907 | 2008-08-15 03:07:47 +0000 | [diff] [blame] | 990 | def test_bad_getattr(self): |
| 991 | x = BadGetattr() |
| 992 | for proto in 0, 1: |
| 993 | self.assertRaises(RuntimeError, self.dumps, x, proto) |
| 994 | # protocol 2 don't raise a RuntimeError. |
| 995 | d = self.dumps(x, 2) |
| 996 | self.assertRaises(RuntimeError, self.loads, d) |
| 997 | |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 998 | def test_reduce_bad_iterator(self): |
| 999 | # Issue4176: crash when 4th and 5th items of __reduce__() |
| 1000 | # are not iterators |
| 1001 | class C(object): |
| 1002 | def __reduce__(self): |
| 1003 | # 4th item is not an iterator |
| 1004 | return list, (), None, [], None |
| 1005 | class D(object): |
| 1006 | def __reduce__(self): |
| 1007 | # 5th item is not an iterator |
| 1008 | return dict, (), None, None, [] |
| 1009 | |
Amaury Forgeot d'Arc | 6285ffd | 2008-10-31 17:52:47 +0000 | [diff] [blame] | 1010 | # Protocol 0 is less strict and also accept iterables. |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 1011 | for proto in protocols: |
Amaury Forgeot d'Arc | 6285ffd | 2008-10-31 17:52:47 +0000 | [diff] [blame] | 1012 | try: |
| 1013 | self.dumps(C(), proto) |
| 1014 | except (pickle.PickleError): |
| 1015 | pass |
| 1016 | try: |
| 1017 | self.dumps(D(), proto) |
| 1018 | except (pickle.PickleError): |
| 1019 | pass |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 1020 | |
Collin Winter | 771d834 | 2009-04-16 03:18:06 +0000 | [diff] [blame] | 1021 | def test_many_puts_and_gets(self): |
| 1022 | # Test that internal data structures correctly deal with lots of |
| 1023 | # puts/gets. |
| 1024 | keys = ("aaa" + str(i) for i in range(100)) |
| 1025 | large_dict = dict((k, [4, 5, 6]) for k in keys) |
| 1026 | obj = [dict(large_dict), dict(large_dict), dict(large_dict)] |
| 1027 | |
| 1028 | for proto in protocols: |
| 1029 | dumped = self.dumps(obj, proto) |
| 1030 | loaded = self.loads(dumped) |
| 1031 | self.assertEqual(loaded, obj, |
| 1032 | "Failed protocol %d: %r != %r" |
| 1033 | % (proto, obj, loaded)) |
| 1034 | |
Antoine Pitrou | a9f48a0 | 2009-05-02 21:41:14 +0000 | [diff] [blame] | 1035 | def test_attribute_name_interning(self): |
| 1036 | # Test that attribute names of pickled objects are interned when |
| 1037 | # unpickling. |
| 1038 | for proto in protocols: |
| 1039 | x = C() |
| 1040 | x.foo = 42 |
| 1041 | x.bar = "hello" |
| 1042 | s = self.dumps(x, proto) |
| 1043 | y = self.loads(s) |
| 1044 | x_keys = sorted(x.__dict__) |
| 1045 | y_keys = sorted(y.__dict__) |
| 1046 | for x_key, y_key in zip(x_keys, y_keys): |
| 1047 | self.assertIs(x_key, y_key) |
| 1048 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 1049 | def test_unpickle_from_2x(self): |
| 1050 | # Unpickle non-trivial data from Python 2.x. |
| 1051 | loaded = self.loads(DATA3) |
| 1052 | self.assertEqual(loaded, set([1, 2])) |
| 1053 | loaded = self.loads(DATA4) |
| 1054 | self.assertEqual(type(loaded), type(range(0))) |
| 1055 | self.assertEqual(list(loaded), list(range(5))) |
| 1056 | loaded = self.loads(DATA5) |
| 1057 | self.assertEqual(type(loaded), SimpleCookie) |
| 1058 | self.assertEqual(list(loaded.keys()), ["key"]) |
| 1059 | self.assertEqual(loaded["key"].value, "Set-Cookie: key=value") |
| 1060 | |
| 1061 | def test_pickle_to_2x(self): |
| 1062 | # Pickle non-trivial data with protocol 2, expecting that it yields |
| 1063 | # the same result as Python 2.x did. |
| 1064 | # NOTE: this test is a bit too strong since we can produce different |
| 1065 | # bytecode that 2.x will still understand. |
| 1066 | dumped = self.dumps(range(5), 2) |
| 1067 | self.assertEqual(dumped, DATA4) |
| 1068 | dumped = self.dumps(set([3]), 2) |
| 1069 | self.assertEqual(dumped, DATA6) |
| 1070 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1071 | def test_large_pickles(self): |
| 1072 | # Test the correctness of internal buffering routines when handling |
| 1073 | # large data. |
| 1074 | for proto in protocols: |
| 1075 | data = (1, b'x' * (256 * 1024)) |
| 1076 | dumped = self.dumps(data, proto) |
| 1077 | loaded = self.loads(dumped) |
| 1078 | self.assertEqual(loaded, data) |
| 1079 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 1080 | |
Guido van Rossum | 2a30b21 | 2003-02-18 22:41:24 +0000 | [diff] [blame] | 1081 | # Test classes for reduce_ex |
| 1082 | |
| 1083 | class REX_one(object): |
| 1084 | _reduce_called = 0 |
| 1085 | def __reduce__(self): |
| 1086 | self._reduce_called = 1 |
| 1087 | return REX_one, () |
| 1088 | # No __reduce_ex__ here, but inheriting it from object |
| 1089 | |
| 1090 | class REX_two(object): |
| 1091 | _proto = None |
| 1092 | def __reduce_ex__(self, proto): |
| 1093 | self._proto = proto |
| 1094 | return REX_two, () |
| 1095 | # No __reduce__ here, but inheriting it from object |
| 1096 | |
| 1097 | class REX_three(object): |
| 1098 | _proto = None |
| 1099 | def __reduce_ex__(self, proto): |
| 1100 | self._proto = proto |
| 1101 | return REX_two, () |
| 1102 | def __reduce__(self): |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 1103 | raise TestFailed("This __reduce__ shouldn't be called") |
Guido van Rossum | 2a30b21 | 2003-02-18 22:41:24 +0000 | [diff] [blame] | 1104 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1105 | class REX_four(object): |
| 1106 | _proto = None |
| 1107 | def __reduce_ex__(self, proto): |
| 1108 | self._proto = proto |
| 1109 | return object.__reduce_ex__(self, proto) |
| 1110 | # Calling base class method should succeed |
| 1111 | |
| 1112 | class REX_five(object): |
| 1113 | _reduce_called = 0 |
| 1114 | def __reduce__(self): |
| 1115 | self._reduce_called = 1 |
| 1116 | return object.__reduce__(self) |
| 1117 | # This one used to fail with infinite recursion |
| 1118 | |
Guido van Rossum | 2a30b21 | 2003-02-18 22:41:24 +0000 | [diff] [blame] | 1119 | # Test classes for newobj |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 1120 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 1121 | class MyInt(int): |
| 1122 | sample = 1 |
| 1123 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 1124 | class MyFloat(float): |
| 1125 | sample = 1.0 |
| 1126 | |
| 1127 | class MyComplex(complex): |
| 1128 | sample = 1.0 + 0.0j |
| 1129 | |
| 1130 | class MyStr(str): |
| 1131 | sample = "hello" |
| 1132 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1133 | class MyUnicode(str): |
| 1134 | sample = "hello \u1234" |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 1135 | |
Guido van Rossum | 533dbcf | 2003-01-28 17:55:05 +0000 | [diff] [blame] | 1136 | class MyTuple(tuple): |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 1137 | sample = (1, 2, 3) |
Guido van Rossum | 533dbcf | 2003-01-28 17:55:05 +0000 | [diff] [blame] | 1138 | |
| 1139 | class MyList(list): |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 1140 | sample = [1, 2, 3] |
| 1141 | |
| 1142 | class MyDict(dict): |
| 1143 | sample = {"a": 1, "b": 2} |
| 1144 | |
Mark Dickinson | 5c2db37 | 2009-12-05 20:28:34 +0000 | [diff] [blame] | 1145 | myclasses = [MyInt, MyFloat, |
Guido van Rossum | 206b9a7 | 2003-03-02 13:53:18 +0000 | [diff] [blame] | 1146 | MyComplex, |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 1147 | MyStr, MyUnicode, |
| 1148 | MyTuple, MyList, MyDict] |
| 1149 | |
Guido van Rossum | 533dbcf | 2003-01-28 17:55:05 +0000 | [diff] [blame] | 1150 | |
Guido van Rossum | c8d6ef5 | 2003-01-28 22:02:31 +0000 | [diff] [blame] | 1151 | class SlotList(MyList): |
| 1152 | __slots__ = ["foo"] |
| 1153 | |
Tim Peters | e9ef203 | 2003-02-13 18:42:00 +0000 | [diff] [blame] | 1154 | class SimpleNewObj(object): |
| 1155 | def __init__(self, a, b, c): |
| 1156 | # raise an error, to make sure this isn't called |
| 1157 | raise TypeError("SimpleNewObj.__init__() didn't expect to get called") |
| 1158 | |
Alexandre Vassalotti | 1f9d907 | 2008-08-15 03:07:47 +0000 | [diff] [blame] | 1159 | class BadGetattr: |
| 1160 | def __getattr__(self, key): |
| 1161 | self.foo |
| 1162 | |
Collin Winter | 771d834 | 2009-04-16 03:18:06 +0000 | [diff] [blame] | 1163 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 1164 | class AbstractPickleModuleTests(unittest.TestCase): |
| 1165 | |
| 1166 | def test_dump_closed_file(self): |
Guido van Rossum | 3b0a329 | 2002-08-09 16:38:32 +0000 | [diff] [blame] | 1167 | import os |
Walter Dörwald | 11b41f6 | 2007-06-20 12:46:31 +0000 | [diff] [blame] | 1168 | f = open(TESTFN, "wb") |
Guido van Rossum | 3b0a329 | 2002-08-09 16:38:32 +0000 | [diff] [blame] | 1169 | try: |
| 1170 | f.close() |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1171 | self.assertRaises(ValueError, pickle.dump, 123, f) |
Guido van Rossum | 3b0a329 | 2002-08-09 16:38:32 +0000 | [diff] [blame] | 1172 | finally: |
| 1173 | os.remove(TESTFN) |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 1174 | |
| 1175 | def test_load_closed_file(self): |
Guido van Rossum | 3b0a329 | 2002-08-09 16:38:32 +0000 | [diff] [blame] | 1176 | import os |
Walter Dörwald | 11b41f6 | 2007-06-20 12:46:31 +0000 | [diff] [blame] | 1177 | f = open(TESTFN, "wb") |
Guido van Rossum | 3b0a329 | 2002-08-09 16:38:32 +0000 | [diff] [blame] | 1178 | try: |
| 1179 | f.close() |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1180 | self.assertRaises(ValueError, pickle.dump, 123, f) |
Guido van Rossum | 3b0a329 | 2002-08-09 16:38:32 +0000 | [diff] [blame] | 1181 | finally: |
| 1182 | os.remove(TESTFN) |
Jeremy Hylton | 4c8be85 | 2002-11-13 22:10:47 +0000 | [diff] [blame] | 1183 | |
Collin Winter | 771d834 | 2009-04-16 03:18:06 +0000 | [diff] [blame] | 1184 | def test_load_from_and_dump_to_file(self): |
| 1185 | stream = io.BytesIO() |
| 1186 | data = [123, {}, 124] |
| 1187 | pickle.dump(data, stream) |
| 1188 | stream.seek(0) |
| 1189 | unpickled = pickle.load(stream) |
| 1190 | self.assertEqual(unpickled, data) |
| 1191 | |
Tim Peters | c0c9370 | 2003-02-13 19:30:57 +0000 | [diff] [blame] | 1192 | def test_highest_protocol(self): |
| 1193 | # Of course this needs to be changed when HIGHEST_PROTOCOL changes. |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1194 | self.assertEqual(pickle.HIGHEST_PROTOCOL, 3) |
Tim Peters | c0c9370 | 2003-02-13 19:30:57 +0000 | [diff] [blame] | 1195 | |
Martin v. Löwis | 544f119 | 2004-07-27 05:22:33 +0000 | [diff] [blame] | 1196 | def test_callapi(self): |
Collin Winter | 771d834 | 2009-04-16 03:18:06 +0000 | [diff] [blame] | 1197 | f = io.BytesIO() |
Martin v. Löwis | 544f119 | 2004-07-27 05:22:33 +0000 | [diff] [blame] | 1198 | # With and without keyword arguments |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1199 | pickle.dump(123, f, -1) |
| 1200 | pickle.dump(123, file=f, protocol=-1) |
| 1201 | pickle.dumps(123, -1) |
| 1202 | pickle.dumps(123, protocol=-1) |
| 1203 | pickle.Pickler(f, -1) |
| 1204 | pickle.Pickler(f, protocol=-1) |
Tim Peters | c0c9370 | 2003-02-13 19:30:57 +0000 | [diff] [blame] | 1205 | |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 1206 | def test_bad_init(self): |
| 1207 | # Test issue3664 (pickle can segfault from a badly initialized Pickler). |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 1208 | # Override initialization without calling __init__() of the superclass. |
| 1209 | class BadPickler(pickle.Pickler): |
| 1210 | def __init__(self): pass |
| 1211 | |
| 1212 | class BadUnpickler(pickle.Unpickler): |
| 1213 | def __init__(self): pass |
| 1214 | |
| 1215 | self.assertRaises(pickle.PicklingError, BadPickler().dump, 0) |
| 1216 | self.assertRaises(pickle.UnpicklingError, BadUnpickler().load) |
| 1217 | |
Amaury Forgeot d'Arc | 3e4e72f | 2008-11-11 20:05:06 +0000 | [diff] [blame] | 1218 | def test_bad_input(self): |
| 1219 | # Test issue4298 |
| 1220 | s = bytes([0x58, 0, 0, 0, 0x54]) |
| 1221 | self.assertRaises(EOFError, pickle.loads, s) |
Antoine Pitrou | 01a15ea | 2010-01-07 17:57:31 +0000 | [diff] [blame] | 1222 | # Test issue7455 |
| 1223 | s = b'0' |
| 1224 | self.assertRaises(pickle.UnpicklingError, pickle.loads, s) |
Amaury Forgeot d'Arc | 3e4e72f | 2008-11-11 20:05:06 +0000 | [diff] [blame] | 1225 | |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 1226 | |
Jeremy Hylton | 4c8be85 | 2002-11-13 22:10:47 +0000 | [diff] [blame] | 1227 | class AbstractPersistentPicklerTests(unittest.TestCase): |
| 1228 | |
| 1229 | # This class defines persistent_id() and persistent_load() |
| 1230 | # functions that should be used by the pickler. All even integers |
| 1231 | # are pickled using persistent ids. |
| 1232 | |
| 1233 | def persistent_id(self, object): |
| 1234 | if isinstance(object, int) and object % 2 == 0: |
| 1235 | self.id_count += 1 |
| 1236 | return str(object) |
| 1237 | else: |
| 1238 | return None |
| 1239 | |
| 1240 | def persistent_load(self, oid): |
| 1241 | self.load_count += 1 |
| 1242 | object = int(oid) |
| 1243 | assert object % 2 == 0 |
| 1244 | return object |
| 1245 | |
| 1246 | def test_persistence(self): |
| 1247 | self.id_count = 0 |
| 1248 | self.load_count = 0 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1249 | L = list(range(10)) |
Jeremy Hylton | 4c8be85 | 2002-11-13 22:10:47 +0000 | [diff] [blame] | 1250 | self.assertEqual(self.loads(self.dumps(L)), L) |
| 1251 | self.assertEqual(self.id_count, 5) |
| 1252 | self.assertEqual(self.load_count, 5) |
| 1253 | |
| 1254 | def test_bin_persistence(self): |
| 1255 | self.id_count = 0 |
| 1256 | self.load_count = 0 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1257 | L = list(range(10)) |
Jeremy Hylton | 4c8be85 | 2002-11-13 22:10:47 +0000 | [diff] [blame] | 1258 | self.assertEqual(self.loads(self.dumps(L, 1)), L) |
| 1259 | self.assertEqual(self.id_count, 5) |
| 1260 | self.assertEqual(self.load_count, 5) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1261 | |
Collin Winter | 771d834 | 2009-04-16 03:18:06 +0000 | [diff] [blame] | 1262 | |
| 1263 | class AbstractPicklerUnpicklerObjectTests(unittest.TestCase): |
| 1264 | |
| 1265 | pickler_class = None |
| 1266 | unpickler_class = None |
| 1267 | |
| 1268 | def setUp(self): |
| 1269 | assert self.pickler_class |
| 1270 | assert self.unpickler_class |
| 1271 | |
| 1272 | def test_clear_pickler_memo(self): |
| 1273 | # To test whether clear_memo() has any effect, we pickle an object, |
| 1274 | # then pickle it again without clearing the memo; the two serialized |
| 1275 | # forms should be different. If we clear_memo() and then pickle the |
| 1276 | # object again, the third serialized form should be identical to the |
| 1277 | # first one we obtained. |
| 1278 | data = ["abcdefg", "abcdefg", 44] |
| 1279 | f = io.BytesIO() |
| 1280 | pickler = self.pickler_class(f) |
| 1281 | |
| 1282 | pickler.dump(data) |
| 1283 | first_pickled = f.getvalue() |
| 1284 | |
| 1285 | # Reset StringIO object. |
| 1286 | f.seek(0) |
| 1287 | f.truncate() |
| 1288 | |
| 1289 | pickler.dump(data) |
| 1290 | second_pickled = f.getvalue() |
| 1291 | |
| 1292 | # Reset the Pickler and StringIO objects. |
| 1293 | pickler.clear_memo() |
| 1294 | f.seek(0) |
| 1295 | f.truncate() |
| 1296 | |
| 1297 | pickler.dump(data) |
| 1298 | third_pickled = f.getvalue() |
| 1299 | |
| 1300 | self.assertNotEqual(first_pickled, second_pickled) |
| 1301 | self.assertEqual(first_pickled, third_pickled) |
| 1302 | |
| 1303 | def test_priming_pickler_memo(self): |
| 1304 | # Verify that we can set the Pickler's memo attribute. |
| 1305 | data = ["abcdefg", "abcdefg", 44] |
| 1306 | f = io.BytesIO() |
| 1307 | pickler = self.pickler_class(f) |
| 1308 | |
| 1309 | pickler.dump(data) |
| 1310 | first_pickled = f.getvalue() |
| 1311 | |
| 1312 | f = io.BytesIO() |
| 1313 | primed = self.pickler_class(f) |
| 1314 | primed.memo = pickler.memo |
| 1315 | |
| 1316 | primed.dump(data) |
| 1317 | primed_pickled = f.getvalue() |
| 1318 | |
| 1319 | self.assertNotEqual(first_pickled, primed_pickled) |
| 1320 | |
| 1321 | def test_priming_unpickler_memo(self): |
| 1322 | # Verify that we can set the Unpickler's memo attribute. |
| 1323 | data = ["abcdefg", "abcdefg", 44] |
| 1324 | f = io.BytesIO() |
| 1325 | pickler = self.pickler_class(f) |
| 1326 | |
| 1327 | pickler.dump(data) |
| 1328 | first_pickled = f.getvalue() |
| 1329 | |
| 1330 | f = io.BytesIO() |
| 1331 | primed = self.pickler_class(f) |
| 1332 | primed.memo = pickler.memo |
| 1333 | |
| 1334 | primed.dump(data) |
| 1335 | primed_pickled = f.getvalue() |
| 1336 | |
| 1337 | unpickler = self.unpickler_class(io.BytesIO(first_pickled)) |
| 1338 | unpickled_data1 = unpickler.load() |
| 1339 | |
| 1340 | self.assertEqual(unpickled_data1, data) |
| 1341 | |
| 1342 | primed = self.unpickler_class(io.BytesIO(primed_pickled)) |
| 1343 | primed.memo = unpickler.memo |
| 1344 | unpickled_data2 = primed.load() |
| 1345 | |
| 1346 | primed.memo.clear() |
| 1347 | |
| 1348 | self.assertEqual(unpickled_data2, data) |
| 1349 | self.assertTrue(unpickled_data2 is unpickled_data1) |
| 1350 | |
| 1351 | def test_reusing_unpickler_objects(self): |
| 1352 | data1 = ["abcdefg", "abcdefg", 44] |
| 1353 | f = io.BytesIO() |
| 1354 | pickler = self.pickler_class(f) |
| 1355 | pickler.dump(data1) |
| 1356 | pickled1 = f.getvalue() |
| 1357 | |
| 1358 | data2 = ["abcdefg", 44, 44] |
| 1359 | f = io.BytesIO() |
| 1360 | pickler = self.pickler_class(f) |
| 1361 | pickler.dump(data2) |
| 1362 | pickled2 = f.getvalue() |
| 1363 | |
| 1364 | f = io.BytesIO() |
| 1365 | f.write(pickled1) |
| 1366 | f.seek(0) |
| 1367 | unpickler = self.unpickler_class(f) |
| 1368 | self.assertEqual(unpickler.load(), data1) |
| 1369 | |
| 1370 | f.seek(0) |
| 1371 | f.truncate() |
| 1372 | f.write(pickled2) |
| 1373 | f.seek(0) |
| 1374 | self.assertEqual(unpickler.load(), data2) |
| 1375 | |
| 1376 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1377 | if __name__ == "__main__": |
| 1378 | # Print some stuff that can be used to rewrite DATA{0,1,2} |
| 1379 | from pickletools import dis |
| 1380 | x = create_data() |
| 1381 | for i in range(3): |
| 1382 | p = pickle.dumps(x, i) |
| 1383 | print("DATA{0} = (".format(i)) |
| 1384 | for j in range(0, len(p), 20): |
| 1385 | b = bytes(p[j:j+20]) |
| 1386 | print(" {0!r}".format(b)) |
| 1387 | print(")") |
| 1388 | print() |
| 1389 | print("# Disassembly of DATA{0}".format(i)) |
| 1390 | print("DATA{0}_DIS = \"\"\"\\".format(i)) |
| 1391 | dis(p) |
| 1392 | print("\"\"\"") |
| 1393 | print() |