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