Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1 | #include "Python.h" |
| 2 | #include "cStringIO.h" |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3 | #include "structmember.h" |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5 | PyDoc_STRVAR(cPickle_module_documentation, |
Tim Peters | 64c04d1 | 2003-02-01 06:27:59 +0000 | [diff] [blame] | 6 | "C implementation and optimization of the Python pickle module."); |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 8 | #ifndef Py_eval_input |
| 9 | #include <graminit.h> |
| 10 | #define Py_eval_input eval_input |
Guido van Rossum | c6ef204 | 1997-08-21 02:30:45 +0000 | [diff] [blame] | 11 | #endif /* Py_eval_input */ |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 12 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 13 | #define DEL_LIST_SLICE(list, from, to) (PyList_SetSlice(list, from, to, NULL)) |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 14 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 15 | #define WRITE_BUF_SIZE 256 |
| 16 | |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 17 | /* Bump this when new opcodes are added to the pickle protocol. */ |
Tim Peters | 8587b3c | 2003-02-13 15:44:41 +0000 | [diff] [blame] | 18 | #define HIGHEST_PROTOCOL 2 |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 19 | |
Tim Peters | 797ec24 | 2003-02-01 06:22:36 +0000 | [diff] [blame] | 20 | /* |
Martin v. Löwis | 9ac4927 | 2009-01-02 20:40:14 +0000 | [diff] [blame] | 21 | * Note: The UNICODE macro controls the TCHAR meaning of the win32 API. Since |
| 22 | * all headers have already been included here, we can safely redefine it. |
| 23 | */ |
| 24 | #ifdef UNICODE |
| 25 | # undef UNICODE |
| 26 | #endif |
| 27 | |
| 28 | /* |
Tim Peters | 797ec24 | 2003-02-01 06:22:36 +0000 | [diff] [blame] | 29 | * Pickle opcodes. These must be kept in synch with pickle.py. Extensive |
| 30 | * docs are in pickletools.py. |
| 31 | */ |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 32 | #define MARK '(' |
| 33 | #define STOP '.' |
| 34 | #define POP '0' |
| 35 | #define POP_MARK '1' |
| 36 | #define DUP '2' |
| 37 | #define FLOAT 'F' |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 38 | #define BINFLOAT 'G' |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 39 | #define INT 'I' |
| 40 | #define BININT 'J' |
| 41 | #define BININT1 'K' |
| 42 | #define LONG 'L' |
| 43 | #define BININT2 'M' |
| 44 | #define NONE 'N' |
| 45 | #define PERSID 'P' |
| 46 | #define BINPERSID 'Q' |
| 47 | #define REDUCE 'R' |
| 48 | #define STRING 'S' |
| 49 | #define BINSTRING 'T' |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 50 | #define SHORT_BINSTRING 'U' |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 51 | #define UNICODE 'V' |
| 52 | #define BINUNICODE 'X' |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 53 | #define APPEND 'a' |
| 54 | #define BUILD 'b' |
| 55 | #define GLOBAL 'c' |
| 56 | #define DICT 'd' |
| 57 | #define EMPTY_DICT '}' |
| 58 | #define APPENDS 'e' |
| 59 | #define GET 'g' |
| 60 | #define BINGET 'h' |
| 61 | #define INST 'i' |
| 62 | #define LONG_BINGET 'j' |
| 63 | #define LIST 'l' |
| 64 | #define EMPTY_LIST ']' |
| 65 | #define OBJ 'o' |
| 66 | #define PUT 'p' |
| 67 | #define BINPUT 'q' |
| 68 | #define LONG_BINPUT 'r' |
| 69 | #define SETITEM 's' |
| 70 | #define TUPLE 't' |
| 71 | #define EMPTY_TUPLE ')' |
| 72 | #define SETITEMS 'u' |
Tim Peters | 797ec24 | 2003-02-01 06:22:36 +0000 | [diff] [blame] | 73 | |
| 74 | /* Protocol 2. */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 75 | #define PROTO '\x80' /* identify pickle protocol */ |
Tim Peters | 797ec24 | 2003-02-01 06:22:36 +0000 | [diff] [blame] | 76 | #define NEWOBJ '\x81' /* build object by applying cls.__new__ to argtuple */ |
| 77 | #define EXT1 '\x82' /* push object from extension registry; 1-byte index */ |
| 78 | #define EXT2 '\x83' /* ditto, but 2-byte index */ |
| 79 | #define EXT4 '\x84' /* ditto, but 4-byte index */ |
| 80 | #define TUPLE1 '\x85' /* build 1-tuple from stack top */ |
| 81 | #define TUPLE2 '\x86' /* build 2-tuple from two topmost stack items */ |
| 82 | #define TUPLE3 '\x87' /* build 3-tuple from three topmost stack items */ |
| 83 | #define NEWTRUE '\x88' /* push True */ |
| 84 | #define NEWFALSE '\x89' /* push False */ |
| 85 | #define LONG1 '\x8a' /* push long from < 256 bytes */ |
| 86 | #define LONG4 '\x8b' /* push really big long */ |
| 87 | |
| 88 | /* There aren't opcodes -- they're ways to pickle bools before protocol 2, |
| 89 | * so that unpicklers written before bools were introduced unpickle them |
| 90 | * as ints, but unpicklers after can recognize that bools were intended. |
| 91 | * Note that protocol 2 added direct ways to pickle bools. |
| 92 | */ |
Jack Jansen | 3a96702 | 2002-06-26 20:40:42 +0000 | [diff] [blame] | 93 | #undef TRUE |
Guido van Rossum | e276339 | 2002-04-05 19:30:08 +0000 | [diff] [blame] | 94 | #define TRUE "I01\n" |
Jack Jansen | 3a96702 | 2002-06-26 20:40:42 +0000 | [diff] [blame] | 95 | #undef FALSE |
Guido van Rossum | e276339 | 2002-04-05 19:30:08 +0000 | [diff] [blame] | 96 | #define FALSE "I00\n" |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 97 | |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 98 | /* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 99 | * batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will |
| 100 | * break if this gets out of synch with pickle.py, but it's unclear that |
| 101 | * would help anything either. |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 102 | */ |
| 103 | #define BATCHSIZE 1000 |
| 104 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 105 | static char MARKv = MARK; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 106 | |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 107 | static PyObject *PickleError; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 108 | static PyObject *PicklingError; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 109 | static PyObject *UnpickleableError; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 110 | static PyObject *UnpicklingError; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 111 | static PyObject *BadPickleGet; |
| 112 | |
Tim Peters | 5b7da39 | 2003-02-04 00:21:07 +0000 | [diff] [blame] | 113 | /* As the name says, an empty tuple. */ |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 114 | static PyObject *empty_tuple; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 115 | |
Georg Brandl | dffbf5f | 2008-05-20 07:49:57 +0000 | [diff] [blame] | 116 | /* copy_reg.dispatch_table, {type_object: pickling_function} */ |
Tim Peters | 5b7da39 | 2003-02-04 00:21:07 +0000 | [diff] [blame] | 117 | static PyObject *dispatch_table; |
| 118 | |
| 119 | /* For EXT[124] opcodes. */ |
Georg Brandl | dffbf5f | 2008-05-20 07:49:57 +0000 | [diff] [blame] | 120 | /* copy_reg._extension_registry, {(module_name, function_name): code} */ |
Tim Peters | 5b7da39 | 2003-02-04 00:21:07 +0000 | [diff] [blame] | 121 | static PyObject *extension_registry; |
Georg Brandl | dffbf5f | 2008-05-20 07:49:57 +0000 | [diff] [blame] | 122 | /* copy_reg._inverted_registry, {code: (module_name, function_name)} */ |
Tim Peters | 5b7da39 | 2003-02-04 00:21:07 +0000 | [diff] [blame] | 123 | static PyObject *inverted_registry; |
Georg Brandl | dffbf5f | 2008-05-20 07:49:57 +0000 | [diff] [blame] | 124 | /* copy_reg._extension_cache, {code: object} */ |
Tim Peters | 5b7da39 | 2003-02-04 00:21:07 +0000 | [diff] [blame] | 125 | static PyObject *extension_cache; |
| 126 | |
Georg Brandl | dffbf5f | 2008-05-20 07:49:57 +0000 | [diff] [blame] | 127 | /* For looking up name pairs in copy_reg._extension_registry. */ |
Tim Peters | 731098b | 2003-02-04 20:56:09 +0000 | [diff] [blame] | 128 | static PyObject *two_tuple; |
| 129 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 130 | static PyObject *__class___str, *__getinitargs___str, *__dict___str, |
| 131 | *__getstate___str, *__setstate___str, *__name___str, *__reduce___str, |
Guido van Rossum | b289b87 | 2003-02-19 01:45:13 +0000 | [diff] [blame] | 132 | *__reduce_ex___str, |
Tim Peters | 1f1b2d2 | 2003-02-01 02:16:37 +0000 | [diff] [blame] | 133 | *write_str, *append_str, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 134 | *read_str, *readline_str, *__main___str, |
Alexandre Vassalotti | 8b2d713 | 2009-11-24 17:53:23 +0000 | [diff] [blame] | 135 | *dispatch_table_str; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 136 | |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 137 | /************************************************************************* |
| 138 | Internal Data type for pickle data. */ |
| 139 | |
| 140 | typedef struct { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 141 | PyObject_HEAD |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 142 | Py_ssize_t length; /* number of initial slots in data currently used */ |
| 143 | Py_ssize_t size; /* number of slots in data allocated */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 144 | PyObject **data; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 145 | } Pdata; |
| 146 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 147 | static void |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 148 | Pdata_dealloc(Pdata *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 149 | { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 150 | Py_ssize_t i; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 151 | PyObject **p; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 152 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 153 | for (i = self->length, p = self->data; --i >= 0; p++) { |
| 154 | Py_DECREF(*p); |
| 155 | } |
| 156 | if (self->data) |
| 157 | free(self->data); |
| 158 | PyObject_Del(self); |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | static PyTypeObject PdataType = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 162 | PyVarObject_HEAD_INIT(NULL, 0) "cPickle.Pdata", sizeof(Pdata), 0, |
| 163 | (destructor)Pdata_dealloc, |
| 164 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0L,0L,0L,0L, "" |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 165 | }; |
| 166 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 167 | #define Pdata_Check(O) (Py_TYPE(O) == &PdataType) |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 168 | |
| 169 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 170 | Pdata_New(void) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 171 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 172 | Pdata *self; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 173 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 174 | if (!(self = PyObject_New(Pdata, &PdataType))) |
| 175 | return NULL; |
| 176 | self->size = 8; |
| 177 | self->length = 0; |
| 178 | self->data = malloc(self->size * sizeof(PyObject*)); |
| 179 | if (self->data) |
| 180 | return (PyObject*)self; |
| 181 | Py_DECREF(self); |
| 182 | return PyErr_NoMemory(); |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 185 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 186 | stackUnderflow(void) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 187 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 188 | PyErr_SetString(UnpicklingError, "unpickling stack underflow"); |
| 189 | return -1; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 192 | /* Retain only the initial clearto items. If clearto >= the current |
| 193 | * number of items, this is a (non-erroneous) NOP. |
| 194 | */ |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 195 | static int |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 196 | Pdata_clear(Pdata *self, Py_ssize_t clearto) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 197 | { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 198 | Py_ssize_t i; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 199 | PyObject **p; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 200 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 201 | if (clearto < 0) return stackUnderflow(); |
| 202 | if (clearto >= self->length) return 0; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 203 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 204 | for (i = self->length, p = self->data + clearto; |
| 205 | --i >= clearto; |
| 206 | p++) { |
| 207 | Py_CLEAR(*p); |
| 208 | } |
| 209 | self->length = clearto; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 210 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 211 | return 0; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 214 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 215 | Pdata_grow(Pdata *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 216 | { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 217 | Py_ssize_t bigger; |
| 218 | Py_ssize_t nbytes; |
| 219 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 220 | PyObject **tmp; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 221 | |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 222 | if (self->size > (PY_SSIZE_T_MAX >> 1)) |
| 223 | goto nomemory; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 224 | bigger = self->size << 1; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 225 | if (bigger > (PY_SSIZE_T_MAX / sizeof(PyObject *))) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 226 | goto nomemory; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 227 | nbytes = bigger * sizeof(PyObject *); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 228 | tmp = realloc(self->data, nbytes); |
| 229 | if (tmp == NULL) |
| 230 | goto nomemory; |
| 231 | self->data = tmp; |
| 232 | self->size = bigger; |
| 233 | return 0; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 234 | |
| 235 | nomemory: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 236 | PyErr_NoMemory(); |
| 237 | return -1; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 238 | } |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 239 | |
Tim Peters | e0a3907 | 2003-02-03 15:45:56 +0000 | [diff] [blame] | 240 | /* D is a Pdata*. Pop the topmost element and store it into V, which |
| 241 | * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 242 | * is raised and V is set to NULL. D and V may be evaluated several times. |
| 243 | */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 244 | #define PDATA_POP(D, V) { \ |
| 245 | if ((D)->length) \ |
| 246 | (V) = (D)->data[--((D)->length)]; \ |
| 247 | else { \ |
| 248 | PyErr_SetString(UnpicklingError, "bad pickle data"); \ |
| 249 | (V) = NULL; \ |
| 250 | } \ |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Tim Peters | e0a3907 | 2003-02-03 15:45:56 +0000 | [diff] [blame] | 253 | /* PDATA_PUSH and PDATA_APPEND both push rvalue PyObject* O on to Pdata* |
| 254 | * D. If the Pdata stack can't be grown to hold the new value, both |
| 255 | * raise MemoryError and execute "return ER". The difference is in ownership |
| 256 | * of O after: _PUSH transfers ownership of O from the caller to the stack |
| 257 | * (no incref of O is done, and in case of error O is decrefed), while |
| 258 | * _APPEND pushes a new reference. |
| 259 | */ |
| 260 | |
| 261 | /* Push O on stack D, giving ownership of O to the stack. */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 262 | #define PDATA_PUSH(D, O, ER) { \ |
| 263 | if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \ |
| 264 | Pdata_grow((Pdata*)(D)) < 0) { \ |
| 265 | Py_DECREF(O); \ |
| 266 | return ER; \ |
| 267 | } \ |
| 268 | ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \ |
Tim Peters | e0a3907 | 2003-02-03 15:45:56 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /* Push O on stack D, pushing a new reference. */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 272 | #define PDATA_APPEND(D, O, ER) { \ |
| 273 | if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \ |
| 274 | Pdata_grow((Pdata*)(D)) < 0) \ |
| 275 | return ER; \ |
| 276 | Py_INCREF(O); \ |
| 277 | ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \ |
Tim Peters | e0a3907 | 2003-02-03 15:45:56 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 281 | static PyObject * |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 282 | Pdata_popTuple(Pdata *self, Py_ssize_t start) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 283 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 284 | PyObject *r; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 285 | Py_ssize_t i, j, l; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 286 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 287 | l = self->length-start; |
| 288 | r = PyTuple_New(l); |
| 289 | if (r == NULL) |
| 290 | return NULL; |
| 291 | for (i = start, j = 0 ; j < l; i++, j++) |
| 292 | PyTuple_SET_ITEM(r, j, self->data[i]); |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 293 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 294 | self->length = start; |
| 295 | return r; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | static PyObject * |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 299 | Pdata_popList(Pdata *self, Py_ssize_t start) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 300 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 301 | PyObject *r; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 302 | Py_ssize_t i, j, l; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 303 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 304 | l=self->length-start; |
| 305 | if (!( r=PyList_New(l))) return NULL; |
| 306 | for (i=start, j=0 ; j < l; i++, j++) |
| 307 | PyList_SET_ITEM(r, j, self->data[i]); |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 308 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 309 | self->length=start; |
| 310 | return r; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 313 | /*************************************************************************/ |
| 314 | |
| 315 | #define ARG_TUP(self, o) { \ |
| 316 | if (self->arg || (self->arg=PyTuple_New(1))) { \ |
| 317 | Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \ |
| 318 | PyTuple_SET_ITEM(self->arg,0,o); \ |
| 319 | } \ |
| 320 | else { \ |
| 321 | Py_DECREF(o); \ |
| 322 | } \ |
| 323 | } |
| 324 | |
| 325 | #define FREE_ARG_TUP(self) { \ |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 326 | if (Py_REFCNT(self->arg) > 1) { \ |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 327 | Py_DECREF(self->arg); \ |
| 328 | self->arg=NULL; \ |
| 329 | } \ |
| 330 | } |
| 331 | |
Thomas Wouters | 3b6448f | 2000-07-22 23:56:07 +0000 | [diff] [blame] | 332 | typedef struct Picklerobject { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 333 | PyObject_HEAD |
| 334 | FILE *fp; |
| 335 | PyObject *write; |
| 336 | PyObject *file; |
| 337 | PyObject *memo; |
| 338 | PyObject *arg; |
| 339 | PyObject *pers_func; |
| 340 | PyObject *inst_pers_func; |
Tim Peters | 797ec24 | 2003-02-01 06:22:36 +0000 | [diff] [blame] | 341 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 342 | /* pickle protocol number, >= 0 */ |
| 343 | int proto; |
Tim Peters | 797ec24 | 2003-02-01 06:22:36 +0000 | [diff] [blame] | 344 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 345 | /* bool, true if proto > 0 */ |
| 346 | int bin; |
Tim Peters | 797ec24 | 2003-02-01 06:22:36 +0000 | [diff] [blame] | 347 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 348 | int fast; /* Fast mode doesn't save in memo, don't use if circ ref */ |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 349 | Py_ssize_t (*write_func)(struct Picklerobject *, const char *, Py_ssize_t); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 350 | char *write_buf; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 351 | Py_ssize_t buf_size; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 352 | PyObject *dispatch_table; |
| 353 | int fast_container; /* count nested container dumps */ |
| 354 | PyObject *fast_memo; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 355 | } Picklerobject; |
| 356 | |
Barry Warsaw | 52acb49 | 2001-12-21 20:04:22 +0000 | [diff] [blame] | 357 | #ifndef PY_CPICKLE_FAST_LIMIT |
| 358 | #define PY_CPICKLE_FAST_LIMIT 50 |
| 359 | #endif |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 360 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 361 | static PyTypeObject Picklertype; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 362 | |
Thomas Wouters | 3b6448f | 2000-07-22 23:56:07 +0000 | [diff] [blame] | 363 | typedef struct Unpicklerobject { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 364 | PyObject_HEAD |
| 365 | FILE *fp; |
| 366 | PyObject *file; |
| 367 | PyObject *readline; |
| 368 | PyObject *read; |
| 369 | PyObject *memo; |
| 370 | PyObject *arg; |
| 371 | Pdata *stack; |
| 372 | PyObject *mark; |
| 373 | PyObject *pers_func; |
| 374 | PyObject *last_string; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 375 | Py_ssize_t *marks; |
| 376 | Py_ssize_t num_marks; |
| 377 | Py_ssize_t marks_size; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 378 | Py_ssize_t (*read_func)(struct Unpicklerobject *, char **, Py_ssize_t); |
| 379 | Py_ssize_t (*readline_func)(struct Unpicklerobject *, char **); |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 380 | Py_ssize_t buf_size; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 381 | char *buf; |
| 382 | PyObject *find_class; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 383 | } Unpicklerobject; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 384 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 385 | static PyTypeObject Unpicklertype; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 386 | |
Thomas Wouters | 4789b3a | 2000-07-24 11:36:47 +0000 | [diff] [blame] | 387 | /* Forward decls that need the above structs */ |
| 388 | static int save(Picklerobject *, PyObject *, int); |
| 389 | static int put2(Picklerobject *, PyObject *); |
| 390 | |
Guido van Rossum | d385d59 | 1997-04-09 17:47:47 +0000 | [diff] [blame] | 391 | static |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 392 | PyObject * |
Thomas Wouters | 3b6448f | 2000-07-22 23:56:07 +0000 | [diff] [blame] | 393 | cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...) |
| 394 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 395 | va_list va; |
| 396 | PyObject *args=0, *retval=0; |
| 397 | va_start(va, format); |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 398 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 399 | if (format) args = Py_VaBuildValue(format, va); |
| 400 | va_end(va); |
| 401 | if (format && ! args) return NULL; |
| 402 | if (stringformat && !(retval=PyString_FromString(stringformat))) |
| 403 | return NULL; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 404 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 405 | if (retval) { |
| 406 | if (args) { |
| 407 | PyObject *v; |
| 408 | v=PyString_Format(retval, args); |
| 409 | Py_DECREF(retval); |
| 410 | Py_DECREF(args); |
| 411 | if (! v) return NULL; |
| 412 | retval=v; |
| 413 | } |
| 414 | } |
| 415 | else |
| 416 | if (args) retval=args; |
| 417 | else { |
| 418 | PyErr_SetObject(ErrType,Py_None); |
| 419 | return NULL; |
| 420 | } |
| 421 | PyErr_SetObject(ErrType,retval); |
| 422 | Py_DECREF(retval); |
| 423 | return NULL; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 424 | } |
| 425 | |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 426 | static Py_ssize_t |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 427 | write_file(Picklerobject *self, const char *s, Py_ssize_t n) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 428 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 429 | size_t nbyteswritten; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 430 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 431 | if (s == NULL) { |
| 432 | return 0; |
| 433 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 434 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 435 | PyFile_IncUseCount((PyFileObject *)self->file); |
| 436 | Py_BEGIN_ALLOW_THREADS |
| 437 | nbyteswritten = fwrite(s, sizeof(char), n, self->fp); |
| 438 | Py_END_ALLOW_THREADS |
| 439 | PyFile_DecUseCount((PyFileObject *)self->file); |
| 440 | if (nbyteswritten != (size_t)n) { |
| 441 | PyErr_SetFromErrno(PyExc_IOError); |
| 442 | return -1; |
| 443 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 444 | |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 445 | return n; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 448 | static Py_ssize_t |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 449 | write_cStringIO(Picklerobject *self, const char *s, Py_ssize_t n) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 450 | { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 451 | Py_ssize_t len = n; |
| 452 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 453 | if (s == NULL) { |
| 454 | return 0; |
| 455 | } |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 456 | |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 457 | while (n > INT_MAX) { |
| 458 | if (PycStringIO->cwrite((PyObject *)self->file, s, INT_MAX) != INT_MAX) { |
| 459 | return -1; |
| 460 | } |
| 461 | n -= INT_MAX; |
| 462 | } |
| 463 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 464 | if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) { |
| 465 | return -1; |
| 466 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 467 | |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 468 | return len; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 469 | } |
| 470 | |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 471 | static Py_ssize_t |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 472 | write_none(Picklerobject *self, const char *s, Py_ssize_t n) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 473 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 474 | if (s == NULL) return 0; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 475 | return n; |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 478 | static Py_ssize_t |
| 479 | write_other(Picklerobject *self, const char *s, Py_ssize_t n) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 480 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 481 | PyObject *py_str = 0, *junk = 0; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 482 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 483 | if (s == NULL) { |
| 484 | if (!( self->buf_size )) return 0; |
| 485 | py_str = PyString_FromStringAndSize(self->write_buf, |
| 486 | self->buf_size); |
| 487 | if (!py_str) |
| 488 | return -1; |
| 489 | } |
| 490 | else { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 491 | if (self->buf_size && n > WRITE_BUF_SIZE - self->buf_size) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 492 | if (write_other(self, NULL, 0) < 0) |
| 493 | return -1; |
| 494 | } |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 495 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 496 | if (n > WRITE_BUF_SIZE) { |
| 497 | if (!( py_str = |
| 498 | PyString_FromStringAndSize(s, n))) |
| 499 | return -1; |
| 500 | } |
| 501 | else { |
| 502 | memcpy(self->write_buf + self->buf_size, s, n); |
| 503 | self->buf_size += n; |
| 504 | return n; |
| 505 | } |
| 506 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 507 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 508 | if (self->write) { |
| 509 | /* object with write method */ |
| 510 | ARG_TUP(self, py_str); |
| 511 | if (self->arg) { |
| 512 | junk = PyObject_Call(self->write, self->arg, NULL); |
| 513 | FREE_ARG_TUP(self); |
| 514 | } |
| 515 | if (junk) Py_DECREF(junk); |
| 516 | else return -1; |
| 517 | } |
| 518 | else |
| 519 | PDATA_PUSH(self->file, py_str, -1); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 520 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 521 | self->buf_size = 0; |
| 522 | return n; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 526 | static Py_ssize_t |
| 527 | read_file(Unpicklerobject *self, char **s, Py_ssize_t n) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 528 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 529 | size_t nbytesread; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 530 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 531 | if (self->buf_size == 0) { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 532 | Py_ssize_t size; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 533 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 534 | size = ((n < 32) ? 32 : n); |
| 535 | if (!( self->buf = (char *)malloc(size))) { |
| 536 | PyErr_NoMemory(); |
| 537 | return -1; |
| 538 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 539 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 540 | self->buf_size = size; |
| 541 | } |
| 542 | else if (n > self->buf_size) { |
| 543 | char *newbuf = (char *)realloc(self->buf, n); |
| 544 | if (!newbuf) { |
| 545 | PyErr_NoMemory(); |
| 546 | return -1; |
| 547 | } |
| 548 | self->buf = newbuf; |
| 549 | self->buf_size = n; |
| 550 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 551 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 552 | PyFile_IncUseCount((PyFileObject *)self->file); |
| 553 | Py_BEGIN_ALLOW_THREADS |
| 554 | nbytesread = fread(self->buf, sizeof(char), n, self->fp); |
| 555 | Py_END_ALLOW_THREADS |
| 556 | PyFile_DecUseCount((PyFileObject *)self->file); |
| 557 | if (nbytesread != (size_t)n) { |
| 558 | if (feof(self->fp)) { |
| 559 | PyErr_SetNone(PyExc_EOFError); |
| 560 | return -1; |
| 561 | } |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 562 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 563 | PyErr_SetFromErrno(PyExc_IOError); |
| 564 | return -1; |
| 565 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 566 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 567 | *s = self->buf; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 568 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 569 | return n; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 573 | static Py_ssize_t |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 574 | readline_file(Unpicklerobject *self, char **s) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 575 | { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 576 | Py_ssize_t i; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 577 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 578 | if (self->buf_size == 0) { |
| 579 | if (!( self->buf = (char *)malloc(40))) { |
| 580 | PyErr_NoMemory(); |
| 581 | return -1; |
| 582 | } |
| 583 | self->buf_size = 40; |
| 584 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 585 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 586 | i = 0; |
| 587 | while (1) { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 588 | Py_ssize_t bigger; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 589 | char *newbuf; |
| 590 | for (; i < (self->buf_size - 1); i++) { |
| 591 | if (feof(self->fp) || |
| 592 | (self->buf[i] = getc(self->fp)) == '\n') { |
| 593 | self->buf[i + 1] = '\0'; |
| 594 | *s = self->buf; |
| 595 | return i + 1; |
| 596 | } |
| 597 | } |
Serhiy Storchaka | d36d4e0 | 2013-02-26 10:07:36 +0200 | [diff] [blame] | 598 | if (self->buf_size > (PY_SSIZE_T_MAX >> 1)) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 599 | PyErr_NoMemory(); |
| 600 | return -1; |
| 601 | } |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 602 | bigger = self->buf_size << 1; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 603 | newbuf = (char *)realloc(self->buf, bigger); |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 604 | if (newbuf == NULL) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 605 | PyErr_NoMemory(); |
| 606 | return -1; |
| 607 | } |
| 608 | self->buf = newbuf; |
| 609 | self->buf_size = bigger; |
| 610 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 611 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 612 | |
| 613 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 614 | static Py_ssize_t |
| 615 | read_cStringIO(Unpicklerobject *self, char **s, Py_ssize_t n) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 616 | { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 617 | Py_ssize_t len = n; |
| 618 | char *start, *end = NULL; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 619 | |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 620 | while (1) { |
| 621 | int k; |
| 622 | char *ptr; |
| 623 | if (n > INT_MAX) |
| 624 | k = INT_MAX; |
| 625 | else |
| 626 | k = (int)n; |
| 627 | if (PycStringIO->cread((PyObject *)self->file, &ptr, k) != k) { |
| 628 | PyErr_SetNone(PyExc_EOFError); |
| 629 | return -1; |
| 630 | } |
| 631 | if (end == NULL) |
| 632 | start = ptr; |
| 633 | else if (ptr != end) { |
| 634 | /* non-continuous area */ |
| 635 | return -1; |
| 636 | } |
| 637 | if (n <= INT_MAX) |
| 638 | break; |
| 639 | end = ptr + INT_MAX; |
| 640 | n -= INT_MAX; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 641 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 642 | |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 643 | *s = start; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 644 | |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 645 | return len; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 649 | static Py_ssize_t |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 650 | readline_cStringIO(Unpicklerobject *self, char **s) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 651 | { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 652 | Py_ssize_t n = 0; |
| 653 | char *start = NULL, *end = NULL; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 654 | |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 655 | while (1) { |
| 656 | int k; |
| 657 | char *ptr; |
| 658 | if ((k = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) { |
| 659 | return -1; |
| 660 | } |
| 661 | n += k; |
| 662 | if (end == NULL) |
| 663 | start = ptr; |
| 664 | else if (ptr != end) { |
| 665 | /* non-continuous area */ |
| 666 | return -1; |
| 667 | } |
| 668 | if (k == 0 || ptr[k - 1] == '\n') |
| 669 | break; |
| 670 | end = ptr + k; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 671 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 672 | |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 673 | *s = start; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 674 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 675 | return n; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 679 | static Py_ssize_t |
| 680 | read_other(Unpicklerobject *self, char **s, Py_ssize_t n) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 681 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 682 | PyObject *bytes, *str=0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 683 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 684 | if (!( bytes = PyInt_FromSsize_t(n))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 685 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 686 | ARG_TUP(self, bytes); |
| 687 | if (self->arg) { |
| 688 | str = PyObject_Call(self->read, self->arg, NULL); |
| 689 | FREE_ARG_TUP(self); |
| 690 | } |
| 691 | if (! str) return -1; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 692 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 693 | Py_XDECREF(self->last_string); |
| 694 | self->last_string = str; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 695 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 696 | if (! (*s = PyString_AsString(str))) return -1; |
Amaury Forgeot d'Arc | 74b3016 | 2009-07-23 19:26:02 +0000 | [diff] [blame] | 697 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 698 | if (PyString_GET_SIZE(str) != n) { |
| 699 | PyErr_SetNone(PyExc_EOFError); |
| 700 | return -1; |
| 701 | } |
Amaury Forgeot d'Arc | 74b3016 | 2009-07-23 19:26:02 +0000 | [diff] [blame] | 702 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 703 | return n; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 707 | static Py_ssize_t |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 708 | readline_other(Unpicklerobject *self, char **s) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 709 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 710 | PyObject *str; |
| 711 | Py_ssize_t str_size; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 712 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 713 | if (!( str = PyObject_CallObject(self->readline, empty_tuple))) { |
| 714 | return -1; |
| 715 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 716 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 717 | if ((str_size = PyString_Size(str)) < 0) |
| 718 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 719 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 720 | Py_XDECREF(self->last_string); |
| 721 | self->last_string = str; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 722 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 723 | if (! (*s = PyString_AsString(str))) |
| 724 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 725 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 726 | return str_size; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 729 | /* Copy the first n bytes from s into newly malloc'ed memory, plus a |
| 730 | * trailing 0 byte. Return a pointer to that, or NULL if out of memory. |
| 731 | * The caller is responsible for free()'ing the return value. |
| 732 | */ |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 733 | static char * |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 734 | pystrndup(const char *s, Py_ssize_t n) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 735 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 736 | char *r = (char *)malloc(n+1); |
| 737 | if (r == NULL) |
| 738 | return (char*)PyErr_NoMemory(); |
| 739 | memcpy(r, s, n); |
| 740 | r[n] = 0; |
| 741 | return r; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | |
| 745 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 746 | get(Picklerobject *self, PyObject *id) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 747 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 748 | PyObject *value, *mv; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 749 | Py_ssize_t c_value; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 750 | char s[30]; |
| 751 | size_t len; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 752 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 753 | if (!( mv = PyDict_GetItem(self->memo, id))) { |
| 754 | PyErr_SetObject(PyExc_KeyError, id); |
| 755 | return -1; |
| 756 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 757 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 758 | if (!( value = PyTuple_GetItem(mv, 0))) |
| 759 | return -1; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 760 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 761 | if (!( PyInt_Check(value))) { |
| 762 | PyErr_SetString(PicklingError, "no int where int expected in memo"); |
| 763 | return -1; |
| 764 | } |
| 765 | c_value = PyInt_AS_LONG((PyIntObject*)value); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 766 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 767 | if (!self->bin) { |
| 768 | s[0] = GET; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 769 | PyOS_snprintf(s + 1, sizeof(s) - 1, |
| 770 | "%" PY_FORMAT_SIZE_T "d\n", c_value); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 771 | len = strlen(s); |
| 772 | } |
| 773 | else if (Pdata_Check(self->file)) { |
| 774 | if (write_other(self, NULL, 0) < 0) return -1; |
| 775 | PDATA_APPEND(self->file, mv, -1); |
| 776 | return 0; |
| 777 | } |
| 778 | else { |
| 779 | if (c_value < 256) { |
| 780 | s[0] = BINGET; |
| 781 | s[1] = (int)(c_value & 0xff); |
| 782 | len = 2; |
| 783 | } |
| 784 | else { |
| 785 | s[0] = LONG_BINGET; |
| 786 | s[1] = (int)(c_value & 0xff); |
| 787 | s[2] = (int)((c_value >> 8) & 0xff); |
| 788 | s[3] = (int)((c_value >> 16) & 0xff); |
| 789 | s[4] = (int)((c_value >> 24) & 0xff); |
| 790 | len = 5; |
| 791 | } |
| 792 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 793 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 794 | if (self->write_func(self, s, len) < 0) |
| 795 | return -1; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 796 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 797 | return 0; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 798 | } |
Jeremy Hylton | ce616e4 | 1998-08-13 23:13:52 +0000 | [diff] [blame] | 799 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 800 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 801 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 802 | put(Picklerobject *self, PyObject *ob) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 803 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 804 | if (Py_REFCNT(ob) < 2 || self->fast) |
| 805 | return 0; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 806 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 807 | return put2(self, ob); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 808 | } |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 809 | |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 810 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 811 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 812 | put2(Picklerobject *self, PyObject *ob) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 813 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 814 | char c_str[30]; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 815 | Py_ssize_t len, p; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 816 | int res = -1; |
| 817 | PyObject *py_ob_id = 0, *memo_len = 0, *t = 0; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 818 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 819 | if (self->fast) |
| 820 | return 0; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 821 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 822 | if ((p = PyDict_Size(self->memo)) < 0) |
| 823 | goto finally; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 824 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 825 | /* Make sure memo keys are positive! */ |
| 826 | /* XXX Why? |
| 827 | * XXX And does "positive" really mean non-negative? |
| 828 | * XXX pickle.py starts with PUT index 0, not 1. This makes for |
| 829 | * XXX gratuitous differences between the pickling modules. |
| 830 | */ |
| 831 | p++; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 832 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 833 | if (!( py_ob_id = PyLong_FromVoidPtr(ob))) |
| 834 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 835 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 836 | if (!( memo_len = PyInt_FromLong(p))) |
| 837 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 838 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 839 | if (!( t = PyTuple_New(2))) |
| 840 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 841 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 842 | PyTuple_SET_ITEM(t, 0, memo_len); |
| 843 | Py_INCREF(memo_len); |
| 844 | PyTuple_SET_ITEM(t, 1, ob); |
| 845 | Py_INCREF(ob); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 846 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 847 | if (PyDict_SetItem(self->memo, py_ob_id, t) < 0) |
| 848 | goto finally; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 849 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 850 | if (!self->bin) { |
| 851 | c_str[0] = PUT; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 852 | PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, |
| 853 | "%" PY_FORMAT_SIZE_T "d\n", p); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 854 | len = strlen(c_str); |
| 855 | } |
| 856 | else if (Pdata_Check(self->file)) { |
| 857 | if (write_other(self, NULL, 0) < 0) return -1; |
| 858 | PDATA_APPEND(self->file, memo_len, -1); |
| 859 | res=0; /* Job well done ;) */ |
| 860 | goto finally; |
| 861 | } |
| 862 | else { |
| 863 | if (p >= 256) { |
| 864 | c_str[0] = LONG_BINPUT; |
| 865 | c_str[1] = (int)(p & 0xff); |
| 866 | c_str[2] = (int)((p >> 8) & 0xff); |
| 867 | c_str[3] = (int)((p >> 16) & 0xff); |
| 868 | c_str[4] = (int)((p >> 24) & 0xff); |
| 869 | len = 5; |
| 870 | } |
| 871 | else { |
| 872 | c_str[0] = BINPUT; |
| 873 | c_str[1] = p; |
| 874 | len = 2; |
| 875 | } |
| 876 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 877 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 878 | if (self->write_func(self, c_str, len) < 0) |
| 879 | goto finally; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 880 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 881 | res = 0; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 882 | |
| 883 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 884 | Py_XDECREF(py_ob_id); |
| 885 | Py_XDECREF(memo_len); |
| 886 | Py_XDECREF(t); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 887 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 888 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 889 | } |
| 890 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 891 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 892 | whichmodule(PyObject *global, PyObject *global_name) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 893 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 894 | Py_ssize_t i, j; |
| 895 | PyObject *module = 0, *modules_dict = 0, |
| 896 | *global_name_attr = 0, *name = 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 897 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 898 | module = PyObject_GetAttrString(global, "__module__"); |
| 899 | if (module) |
| 900 | return module; |
| 901 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 902 | PyErr_Clear(); |
| 903 | else |
| 904 | return NULL; |
Guido van Rossum | 4518823 | 1997-09-28 05:38:51 +0000 | [diff] [blame] | 905 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 906 | if (!( modules_dict = PySys_GetObject("modules"))) |
| 907 | return NULL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 908 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 909 | i = 0; |
| 910 | while ((j = PyDict_Next(modules_dict, &i, &name, &module))) { |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 911 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 912 | if (PyObject_Compare(name, __main___str)==0) continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 913 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 914 | global_name_attr = PyObject_GetAttr(module, global_name); |
| 915 | if (!global_name_attr) { |
| 916 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 917 | PyErr_Clear(); |
| 918 | else |
| 919 | return NULL; |
| 920 | continue; |
| 921 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 922 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 923 | if (global_name_attr != global) { |
| 924 | Py_DECREF(global_name_attr); |
| 925 | continue; |
| 926 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 927 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 928 | Py_DECREF(global_name_attr); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 929 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 930 | break; |
| 931 | } |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 932 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 933 | /* The following implements the rule in pickle.py added in 1.5 |
| 934 | that used __main__ if no module is found. I don't actually |
| 935 | like this rule. jlf |
| 936 | */ |
| 937 | if (!j) { |
| 938 | name=__main___str; |
| 939 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 940 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 941 | Py_INCREF(name); |
| 942 | return name; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 946 | static int |
Jeremy Hylton | 499ab6a | 2001-10-15 21:37:58 +0000 | [diff] [blame] | 947 | fast_save_enter(Picklerobject *self, PyObject *obj) |
| 948 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 949 | /* if fast_container < 0, we're doing an error exit. */ |
| 950 | if (++self->fast_container >= PY_CPICKLE_FAST_LIMIT) { |
| 951 | PyObject *key = NULL; |
| 952 | if (self->fast_memo == NULL) { |
| 953 | self->fast_memo = PyDict_New(); |
| 954 | if (self->fast_memo == NULL) { |
| 955 | self->fast_container = -1; |
| 956 | return 0; |
| 957 | } |
| 958 | } |
| 959 | key = PyLong_FromVoidPtr(obj); |
| 960 | if (key == NULL) |
| 961 | return 0; |
| 962 | if (PyDict_GetItem(self->fast_memo, key)) { |
| 963 | Py_DECREF(key); |
| 964 | PyErr_Format(PyExc_ValueError, |
| 965 | "fast mode: can't pickle cyclic objects " |
| 966 | "including object type %s at %p", |
| 967 | Py_TYPE(obj)->tp_name, obj); |
| 968 | self->fast_container = -1; |
| 969 | return 0; |
| 970 | } |
| 971 | if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) { |
| 972 | Py_DECREF(key); |
| 973 | self->fast_container = -1; |
| 974 | return 0; |
| 975 | } |
| 976 | Py_DECREF(key); |
| 977 | } |
| 978 | return 1; |
Jeremy Hylton | 499ab6a | 2001-10-15 21:37:58 +0000 | [diff] [blame] | 979 | } |
| 980 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 981 | int |
Jeremy Hylton | 499ab6a | 2001-10-15 21:37:58 +0000 | [diff] [blame] | 982 | fast_save_leave(Picklerobject *self, PyObject *obj) |
| 983 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 984 | if (self->fast_container-- >= PY_CPICKLE_FAST_LIMIT) { |
| 985 | PyObject *key = PyLong_FromVoidPtr(obj); |
| 986 | if (key == NULL) |
| 987 | return 0; |
| 988 | if (PyDict_DelItem(self->fast_memo, key) < 0) { |
| 989 | Py_DECREF(key); |
| 990 | return 0; |
| 991 | } |
| 992 | Py_DECREF(key); |
| 993 | } |
| 994 | return 1; |
Jeremy Hylton | 499ab6a | 2001-10-15 21:37:58 +0000 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 998 | save_none(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 999 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1000 | static char none = NONE; |
| 1001 | if (self->write_func(self, &none, 1) < 0) |
| 1002 | return -1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1003 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1004 | return 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1005 | } |
| 1006 | |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 1007 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1008 | save_bool(Picklerobject *self, PyObject *args) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 1009 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1010 | static const char *buf[2] = {FALSE, TRUE}; |
| 1011 | static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1}; |
| 1012 | long l = PyInt_AS_LONG((PyIntObject *)args); |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 1013 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1014 | if (self->proto >= 2) { |
| 1015 | char opcode = l ? NEWTRUE : NEWFALSE; |
| 1016 | if (self->write_func(self, &opcode, 1) < 0) |
| 1017 | return -1; |
| 1018 | } |
| 1019 | else if (self->write_func(self, buf[l], len[l]) < 0) |
| 1020 | return -1; |
| 1021 | return 0; |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 1022 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 1023 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1024 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1025 | save_int(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1026 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1027 | char c_str[32]; |
| 1028 | long l = PyInt_AS_LONG((PyIntObject *)args); |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 1029 | Py_ssize_t len = 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1030 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1031 | if (!self->bin |
Guido van Rossum | de8d6d7 | 1997-05-13 18:00:44 +0000 | [diff] [blame] | 1032 | #if SIZEOF_LONG > 4 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1033 | || l > 0x7fffffffL |
| 1034 | || l < -0x80000000L |
Guido van Rossum | de8d6d7 | 1997-05-13 18:00:44 +0000 | [diff] [blame] | 1035 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1036 | ) { |
| 1037 | /* Text-mode pickle, or long too big to fit in the 4-byte |
| 1038 | * signed BININT format: store as a string. |
| 1039 | */ |
| 1040 | c_str[0] = INT; |
| 1041 | PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%ld\n", l); |
| 1042 | if (self->write_func(self, c_str, strlen(c_str)) < 0) |
| 1043 | return -1; |
| 1044 | } |
| 1045 | else { |
| 1046 | /* Binary pickle and l fits in a signed 4-byte int. */ |
| 1047 | c_str[1] = (int)( l & 0xff); |
| 1048 | c_str[2] = (int)((l >> 8) & 0xff); |
| 1049 | c_str[3] = (int)((l >> 16) & 0xff); |
| 1050 | c_str[4] = (int)((l >> 24) & 0xff); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1051 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1052 | if ((c_str[4] == 0) && (c_str[3] == 0)) { |
| 1053 | if (c_str[2] == 0) { |
| 1054 | c_str[0] = BININT1; |
| 1055 | len = 2; |
| 1056 | } |
| 1057 | else { |
| 1058 | c_str[0] = BININT2; |
| 1059 | len = 3; |
| 1060 | } |
| 1061 | } |
| 1062 | else { |
| 1063 | c_str[0] = BININT; |
| 1064 | len = 5; |
| 1065 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1066 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1067 | if (self->write_func(self, c_str, len) < 0) |
| 1068 | return -1; |
| 1069 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1070 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1071 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | |
| 1075 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1076 | save_long(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1077 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1078 | Py_ssize_t size; |
| 1079 | int res = -1; |
| 1080 | PyObject *repr = NULL; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1081 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1082 | static char l = LONG; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1083 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1084 | if (self->proto >= 2) { |
| 1085 | /* Linear-time pickling. */ |
| 1086 | size_t nbits; |
| 1087 | size_t nbytes; |
| 1088 | unsigned char *pdata; |
| 1089 | char c_str[5]; |
| 1090 | int i; |
| 1091 | int sign = _PyLong_Sign(args); |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 1092 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1093 | if (sign == 0) { |
| 1094 | /* It's 0 -- an empty bytestring. */ |
| 1095 | c_str[0] = LONG1; |
| 1096 | c_str[1] = 0; |
| 1097 | i = self->write_func(self, c_str, 2); |
| 1098 | if (i < 0) goto finally; |
| 1099 | res = 0; |
| 1100 | goto finally; |
| 1101 | } |
| 1102 | nbits = _PyLong_NumBits(args); |
| 1103 | if (nbits == (size_t)-1 && PyErr_Occurred()) |
| 1104 | goto finally; |
| 1105 | /* How many bytes do we need? There are nbits >> 3 full |
| 1106 | * bytes of data, and nbits & 7 leftover bits. If there |
| 1107 | * are any leftover bits, then we clearly need another |
| 1108 | * byte. Wnat's not so obvious is that we *probably* |
| 1109 | * need another byte even if there aren't any leftovers: |
| 1110 | * the most-significant bit of the most-significant byte |
| 1111 | * acts like a sign bit, and it's usually got a sense |
| 1112 | * opposite of the one we need. The exception is longs |
| 1113 | * of the form -(2**(8*j-1)) for j > 0. Such a long is |
| 1114 | * its own 256's-complement, so has the right sign bit |
| 1115 | * even without the extra byte. That's a pain to check |
| 1116 | * for in advance, though, so we always grab an extra |
| 1117 | * byte at the start, and cut it back later if possible. |
| 1118 | */ |
| 1119 | nbytes = (nbits >> 3) + 1; |
| 1120 | if (nbytes > INT_MAX) { |
| 1121 | PyErr_SetString(PyExc_OverflowError, "long too large " |
| 1122 | "to pickle"); |
| 1123 | goto finally; |
| 1124 | } |
| 1125 | repr = PyString_FromStringAndSize(NULL, (int)nbytes); |
| 1126 | if (repr == NULL) goto finally; |
| 1127 | pdata = (unsigned char *)PyString_AS_STRING(repr); |
| 1128 | i = _PyLong_AsByteArray((PyLongObject *)args, |
| 1129 | pdata, nbytes, |
| 1130 | 1 /* little endian */, 1 /* signed */); |
| 1131 | if (i < 0) goto finally; |
| 1132 | /* If the long is negative, this may be a byte more than |
| 1133 | * needed. This is so iff the MSB is all redundant sign |
| 1134 | * bits. |
| 1135 | */ |
| 1136 | if (sign < 0 && nbytes > 1 && pdata[nbytes - 1] == 0xff && |
| 1137 | (pdata[nbytes - 2] & 0x80) != 0) |
| 1138 | --nbytes; |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 1139 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1140 | if (nbytes < 256) { |
| 1141 | c_str[0] = LONG1; |
| 1142 | c_str[1] = (char)nbytes; |
| 1143 | size = 2; |
| 1144 | } |
| 1145 | else { |
| 1146 | c_str[0] = LONG4; |
| 1147 | size = (int)nbytes; |
| 1148 | for (i = 1; i < 5; i++) { |
| 1149 | c_str[i] = (char)(size & 0xff); |
| 1150 | size >>= 8; |
| 1151 | } |
| 1152 | size = 5; |
| 1153 | } |
| 1154 | i = self->write_func(self, c_str, size); |
| 1155 | if (i < 0) goto finally; |
| 1156 | i = self->write_func(self, (char *)pdata, (int)nbytes); |
| 1157 | if (i < 0) goto finally; |
| 1158 | res = 0; |
| 1159 | goto finally; |
| 1160 | } |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 1161 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1162 | /* proto < 2: write the repr and newline. This is quadratic-time |
| 1163 | * (in the number of digits), in both directions. |
| 1164 | */ |
| 1165 | if (!( repr = PyObject_Repr(args))) |
| 1166 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1167 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1168 | if ((size = PyString_Size(repr)) < 0) |
| 1169 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1170 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1171 | if (self->write_func(self, &l, 1) < 0) |
| 1172 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1173 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1174 | if (self->write_func(self, |
| 1175 | PyString_AS_STRING((PyStringObject *)repr), |
| 1176 | size) < 0) |
| 1177 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1178 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1179 | if (self->write_func(self, "\n", 1) < 0) |
| 1180 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1181 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1182 | res = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1183 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1184 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1185 | Py_XDECREF(repr); |
| 1186 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | |
| 1190 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1191 | save_float(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1192 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1193 | double x = PyFloat_AS_DOUBLE((PyFloatObject *)args); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1194 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1195 | if (self->bin) { |
| 1196 | char str[9]; |
| 1197 | str[0] = BINFLOAT; |
| 1198 | if (_PyFloat_Pack8(x, (unsigned char *)&str[1], 0) < 0) |
| 1199 | return -1; |
| 1200 | if (self->write_func(self, str, 9) < 0) |
| 1201 | return -1; |
| 1202 | } |
| 1203 | else { |
| 1204 | int result = -1; |
| 1205 | char *buf = NULL; |
| 1206 | char op = FLOAT; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1207 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1208 | if (self->write_func(self, &op, 1) < 0) |
| 1209 | goto done; |
Eric Smith | b05d3be | 2009-10-26 15:06:39 +0000 | [diff] [blame] | 1210 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1211 | buf = PyOS_double_to_string(x, 'g', 17, 0, NULL); |
| 1212 | if (!buf) { |
| 1213 | PyErr_NoMemory(); |
| 1214 | goto done; |
| 1215 | } |
Eric Smith | b05d3be | 2009-10-26 15:06:39 +0000 | [diff] [blame] | 1216 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1217 | if (self->write_func(self, buf, strlen(buf)) < 0) |
| 1218 | goto done; |
Eric Smith | b05d3be | 2009-10-26 15:06:39 +0000 | [diff] [blame] | 1219 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1220 | if (self->write_func(self, "\n", 1) < 0) |
| 1221 | goto done; |
Eric Smith | b05d3be | 2009-10-26 15:06:39 +0000 | [diff] [blame] | 1222 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1223 | result = 0; |
Eric Smith | b05d3be | 2009-10-26 15:06:39 +0000 | [diff] [blame] | 1224 | done: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1225 | PyMem_Free(buf); |
| 1226 | return result; |
| 1227 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1228 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1229 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | |
| 1233 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1234 | save_string(Picklerobject *self, PyObject *args, int doput) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1235 | { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 1236 | Py_ssize_t size, len; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1237 | PyObject *repr=0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1238 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1239 | if ((size = PyString_Size(args)) < 0) |
| 1240 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1241 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1242 | if (!self->bin) { |
| 1243 | char *repr_str; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1244 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1245 | static char string = STRING; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1246 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1247 | if (!( repr = PyObject_Repr(args))) |
| 1248 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1249 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1250 | if ((len = PyString_Size(repr)) < 0) |
| 1251 | goto err; |
| 1252 | repr_str = PyString_AS_STRING((PyStringObject *)repr); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1253 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1254 | if (self->write_func(self, &string, 1) < 0) |
| 1255 | goto err; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1256 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1257 | if (self->write_func(self, repr_str, len) < 0) |
| 1258 | goto err; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1259 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1260 | if (self->write_func(self, "\n", 1) < 0) |
| 1261 | goto err; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1262 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1263 | Py_XDECREF(repr); |
| 1264 | } |
| 1265 | else { |
| 1266 | int i; |
| 1267 | char c_str[5]; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1268 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1269 | if (size < 256) { |
| 1270 | c_str[0] = SHORT_BINSTRING; |
| 1271 | c_str[1] = size; |
| 1272 | len = 2; |
| 1273 | } |
| 1274 | else if (size <= INT_MAX) { |
| 1275 | c_str[0] = BINSTRING; |
| 1276 | for (i = 1; i < 5; i++) |
| 1277 | c_str[i] = (int)(size >> ((i - 1) * 8)); |
| 1278 | len = 5; |
| 1279 | } |
| 1280 | else |
| 1281 | return -1; /* string too large */ |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1282 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1283 | if (self->write_func(self, c_str, len) < 0) |
| 1284 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1285 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1286 | if (size > 128 && Pdata_Check(self->file)) { |
| 1287 | if (write_other(self, NULL, 0) < 0) return -1; |
| 1288 | PDATA_APPEND(self->file, args, -1); |
| 1289 | } |
| 1290 | else { |
| 1291 | if (self->write_func(self, |
| 1292 | PyString_AS_STRING( |
| 1293 | (PyStringObject *)args), |
| 1294 | size) < 0) |
| 1295 | return -1; |
| 1296 | } |
| 1297 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1298 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1299 | if (doput) |
| 1300 | if (put(self, args) < 0) |
| 1301 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1302 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1303 | return 0; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 1304 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1305 | err: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1306 | Py_XDECREF(repr); |
| 1307 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1311 | #ifdef Py_USING_UNICODE |
Guido van Rossum | fb10c3f | 2000-12-19 02:08:38 +0000 | [diff] [blame] | 1312 | /* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates |
| 1313 | backslash and newline characters to \uXXXX escapes. */ |
| 1314 | static PyObject * |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1315 | modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, Py_ssize_t size) |
Guido van Rossum | fb10c3f | 2000-12-19 02:08:38 +0000 | [diff] [blame] | 1316 | { |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1317 | PyObject *repr; |
| 1318 | char *p; |
| 1319 | char *q; |
Guido van Rossum | fb10c3f | 2000-12-19 02:08:38 +0000 | [diff] [blame] | 1320 | |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1321 | static const char *hexdigit = "0123456789abcdef"; |
| 1322 | #ifdef Py_UNICODE_WIDE |
| 1323 | const Py_ssize_t expandsize = 10; |
| 1324 | #else |
| 1325 | const Py_ssize_t expandsize = 6; |
| 1326 | #endif |
Guido van Rossum | fb10c3f | 2000-12-19 02:08:38 +0000 | [diff] [blame] | 1327 | |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1328 | if (size > PY_SSIZE_T_MAX / expandsize) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1329 | return PyErr_NoMemory(); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1330 | |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1331 | repr = PyString_FromStringAndSize(NULL, expandsize * size); |
| 1332 | if (repr == NULL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1333 | return NULL; |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1334 | if (size == 0) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1335 | return repr; |
Guido van Rossum | fb10c3f | 2000-12-19 02:08:38 +0000 | [diff] [blame] | 1336 | |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1337 | p = q = PyString_AS_STRING(repr); |
| 1338 | while (size-- > 0) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1339 | Py_UNICODE ch = *s++; |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1340 | #ifdef Py_UNICODE_WIDE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1341 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 1342 | if (ch >= 0x10000) { |
| 1343 | *p++ = '\\'; |
| 1344 | *p++ = 'U'; |
| 1345 | *p++ = hexdigit[(ch >> 28) & 0xf]; |
| 1346 | *p++ = hexdigit[(ch >> 24) & 0xf]; |
| 1347 | *p++ = hexdigit[(ch >> 20) & 0xf]; |
| 1348 | *p++ = hexdigit[(ch >> 16) & 0xf]; |
| 1349 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 1350 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 1351 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 1352 | *p++ = hexdigit[ch & 15]; |
| 1353 | } |
| 1354 | else |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1355 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1356 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 1357 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 1358 | Py_UNICODE ch2; |
| 1359 | Py_UCS4 ucs; |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1360 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1361 | ch2 = *s++; |
| 1362 | size--; |
| 1363 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 1364 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 1365 | *p++ = '\\'; |
| 1366 | *p++ = 'U'; |
| 1367 | *p++ = hexdigit[(ucs >> 28) & 0xf]; |
| 1368 | *p++ = hexdigit[(ucs >> 24) & 0xf]; |
| 1369 | *p++ = hexdigit[(ucs >> 20) & 0xf]; |
| 1370 | *p++ = hexdigit[(ucs >> 16) & 0xf]; |
| 1371 | *p++ = hexdigit[(ucs >> 12) & 0xf]; |
| 1372 | *p++ = hexdigit[(ucs >> 8) & 0xf]; |
| 1373 | *p++ = hexdigit[(ucs >> 4) & 0xf]; |
| 1374 | *p++ = hexdigit[ucs & 0xf]; |
| 1375 | continue; |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1376 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1377 | /* Fall through: isolated surrogates are copied as-is */ |
| 1378 | s--; |
| 1379 | size++; |
| 1380 | } |
| 1381 | #endif |
| 1382 | /* Map 16-bit characters to '\uxxxx' */ |
| 1383 | if (ch >= 256 || ch == '\\' || ch == '\n') { |
| 1384 | *p++ = '\\'; |
| 1385 | *p++ = 'u'; |
| 1386 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 1387 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 1388 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 1389 | *p++ = hexdigit[ch & 15]; |
| 1390 | } |
| 1391 | /* Copy everything else as-is */ |
| 1392 | else |
| 1393 | *p++ = (char) ch; |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1394 | } |
| 1395 | *p = '\0'; |
| 1396 | _PyString_Resize(&repr, p - q); |
| 1397 | return repr; |
| 1398 | } |
Guido van Rossum | fb10c3f | 2000-12-19 02:08:38 +0000 | [diff] [blame] | 1399 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1400 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1401 | save_unicode(Picklerobject *self, PyObject *args, int doput) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1402 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1403 | Py_ssize_t size, len; |
| 1404 | PyObject *repr=0; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1405 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1406 | if (!PyUnicode_Check(args)) |
| 1407 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1408 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1409 | if (!self->bin) { |
| 1410 | char *repr_str; |
| 1411 | static char string = UNICODE; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1412 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1413 | repr = modified_EncodeRawUnicodeEscape( |
| 1414 | PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args)); |
| 1415 | if (!repr) |
| 1416 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1417 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1418 | if ((len = PyString_Size(repr)) < 0) |
| 1419 | goto err; |
| 1420 | repr_str = PyString_AS_STRING((PyStringObject *)repr); |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1421 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1422 | if (self->write_func(self, &string, 1) < 0) |
| 1423 | goto err; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1424 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1425 | if (self->write_func(self, repr_str, len) < 0) |
| 1426 | goto err; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1427 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1428 | if (self->write_func(self, "\n", 1) < 0) |
| 1429 | goto err; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1430 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1431 | Py_XDECREF(repr); |
| 1432 | } |
| 1433 | else { |
| 1434 | int i; |
| 1435 | char c_str[5]; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1436 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1437 | if (!( repr = PyUnicode_AsUTF8String(args))) |
| 1438 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1439 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1440 | if ((size = PyString_Size(repr)) < 0) |
| 1441 | goto err; |
| 1442 | if (size > INT_MAX) |
| 1443 | return -1; /* string too large */ |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1444 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1445 | c_str[0] = BINUNICODE; |
| 1446 | for (i = 1; i < 5; i++) |
| 1447 | c_str[i] = (int)(size >> ((i - 1) * 8)); |
| 1448 | len = 5; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1449 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1450 | if (self->write_func(self, c_str, len) < 0) |
| 1451 | goto err; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1452 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1453 | if (size > 128 && Pdata_Check(self->file)) { |
| 1454 | if (write_other(self, NULL, 0) < 0) |
| 1455 | goto err; |
| 1456 | PDATA_APPEND(self->file, repr, -1); |
| 1457 | } |
| 1458 | else { |
| 1459 | if (self->write_func(self, PyString_AS_STRING(repr), |
| 1460 | size) < 0) |
| 1461 | goto err; |
| 1462 | } |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1463 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1464 | Py_DECREF(repr); |
| 1465 | } |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1466 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1467 | if (doput) |
| 1468 | if (put(self, args) < 0) |
| 1469 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1470 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1471 | return 0; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1472 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1473 | err: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1474 | Py_XDECREF(repr); |
| 1475 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1476 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1477 | #endif |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1478 | |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1479 | /* A helper for save_tuple. Push the len elements in tuple t on the stack. */ |
| 1480 | static int |
Tim Peters | 6792014 | 2003-02-05 03:46:17 +0000 | [diff] [blame] | 1481 | store_tuple_elements(Picklerobject *self, PyObject *t, int len) |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1482 | { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 1483 | Py_ssize_t i; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1484 | int res = -1; /* guilty until proved innocent */ |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1485 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1486 | assert(PyTuple_Size(t) == len); |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1487 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1488 | for (i = 0; i < len; i++) { |
| 1489 | PyObject *element = PyTuple_GET_ITEM(t, i); |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1490 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1491 | if (element == NULL) |
| 1492 | goto finally; |
| 1493 | if (save(self, element, 0) < 0) |
| 1494 | goto finally; |
| 1495 | } |
| 1496 | res = 0; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1497 | |
| 1498 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1499 | return res; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
| 1502 | /* Tuples are ubiquitous in the pickle protocols, so many techniques are |
| 1503 | * used across protocols to minimize the space needed to pickle them. |
Tim Peters | 6792014 | 2003-02-05 03:46:17 +0000 | [diff] [blame] | 1504 | * Tuples are also the only builtin immutable type that can be recursive |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1505 | * (a tuple can be reached from itself), and that requires some subtle |
| 1506 | * magic so that it works in all cases. IOW, this is a long routine. |
| 1507 | */ |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1508 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1509 | save_tuple(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1510 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1511 | PyObject *py_tuple_id = NULL; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 1512 | Py_ssize_t len, i; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1513 | int res = -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1514 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1515 | static char tuple = TUPLE; |
| 1516 | static char pop = POP; |
| 1517 | static char pop_mark = POP_MARK; |
| 1518 | static char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3}; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1519 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1520 | if ((len = PyTuple_Size(args)) < 0) |
| 1521 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1522 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1523 | if (len == 0) { |
| 1524 | char c_str[2]; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 1525 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1526 | if (self->proto) { |
| 1527 | c_str[0] = EMPTY_TUPLE; |
| 1528 | len = 1; |
| 1529 | } |
| 1530 | else { |
| 1531 | c_str[0] = MARK; |
| 1532 | c_str[1] = TUPLE; |
| 1533 | len = 2; |
| 1534 | } |
| 1535 | if (self->write_func(self, c_str, len) >= 0) |
| 1536 | res = 0; |
| 1537 | /* Don't memoize an empty tuple. */ |
| 1538 | goto finally; |
| 1539 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1540 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1541 | /* A non-empty tuple. */ |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1542 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1543 | /* id(tuple) isn't in the memo now. If it shows up there after |
| 1544 | * saving the tuple elements, the tuple must be recursive, in |
| 1545 | * which case we'll pop everything we put on the stack, and fetch |
| 1546 | * its value from the memo. |
| 1547 | */ |
| 1548 | py_tuple_id = PyLong_FromVoidPtr(args); |
| 1549 | if (py_tuple_id == NULL) |
| 1550 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1551 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1552 | if (len <= 3 && self->proto >= 2) { |
| 1553 | /* Use TUPLE{1,2,3} opcodes. */ |
| 1554 | if (store_tuple_elements(self, args, len) < 0) |
| 1555 | goto finally; |
| 1556 | if (PyDict_GetItem(self->memo, py_tuple_id)) { |
| 1557 | /* pop the len elements */ |
| 1558 | for (i = 0; i < len; ++i) |
| 1559 | if (self->write_func(self, &pop, 1) < 0) |
| 1560 | goto finally; |
| 1561 | /* fetch from memo */ |
| 1562 | if (get(self, py_tuple_id) < 0) |
| 1563 | goto finally; |
| 1564 | res = 0; |
| 1565 | goto finally; |
| 1566 | } |
| 1567 | /* Not recursive. */ |
| 1568 | if (self->write_func(self, len2opcode + len, 1) < 0) |
| 1569 | goto finally; |
| 1570 | goto memoize; |
| 1571 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1572 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1573 | /* proto < 2 and len > 0, or proto >= 2 and len > 3. |
| 1574 | * Generate MARK elt1 elt2 ... TUPLE |
| 1575 | */ |
| 1576 | if (self->write_func(self, &MARKv, 1) < 0) |
| 1577 | goto finally; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1578 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1579 | if (store_tuple_elements(self, args, len) < 0) |
| 1580 | goto finally; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1581 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1582 | if (PyDict_GetItem(self->memo, py_tuple_id)) { |
| 1583 | /* pop the stack stuff we pushed */ |
| 1584 | if (self->bin) { |
| 1585 | if (self->write_func(self, &pop_mark, 1) < 0) |
| 1586 | goto finally; |
| 1587 | } |
| 1588 | else { |
| 1589 | /* Note that we pop one more than len, to remove |
| 1590 | * the MARK too. |
| 1591 | */ |
| 1592 | for (i = 0; i <= len; i++) |
| 1593 | if (self->write_func(self, &pop, 1) < 0) |
| 1594 | goto finally; |
| 1595 | } |
| 1596 | /* fetch from memo */ |
| 1597 | if (get(self, py_tuple_id) >= 0) |
| 1598 | res = 0; |
| 1599 | goto finally; |
| 1600 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1601 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1602 | /* Not recursive. */ |
| 1603 | if (self->write_func(self, &tuple, 1) < 0) |
| 1604 | goto finally; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 1605 | |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1606 | memoize: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1607 | if (put(self, args) >= 0) |
| 1608 | res = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1609 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1610 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1611 | Py_XDECREF(py_tuple_id); |
| 1612 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1613 | } |
| 1614 | |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1615 | /* iter is an iterator giving items, and we batch up chunks of |
| 1616 | * MARK item item ... item APPENDS |
| 1617 | * opcode sequences. Calling code should have arranged to first create an |
| 1618 | * empty list, or list-like object, for the APPENDS to operate on. |
| 1619 | * Returns 0 on success, <0 on error. |
| 1620 | */ |
| 1621 | static int |
| 1622 | batch_list(Picklerobject *self, PyObject *iter) |
| 1623 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1624 | PyObject *obj = NULL; |
| 1625 | PyObject *firstitem = NULL; |
| 1626 | int i, n; |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1627 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1628 | static char append = APPEND; |
| 1629 | static char appends = APPENDS; |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1630 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1631 | assert(iter != NULL); |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1632 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1633 | if (self->proto == 0) { |
| 1634 | /* APPENDS isn't available; do one at a time. */ |
| 1635 | for (;;) { |
| 1636 | obj = PyIter_Next(iter); |
| 1637 | if (obj == NULL) { |
| 1638 | if (PyErr_Occurred()) |
| 1639 | return -1; |
| 1640 | break; |
| 1641 | } |
| 1642 | i = save(self, obj, 0); |
| 1643 | Py_DECREF(obj); |
| 1644 | if (i < 0) |
| 1645 | return -1; |
| 1646 | if (self->write_func(self, &append, 1) < 0) |
| 1647 | return -1; |
| 1648 | } |
| 1649 | return 0; |
| 1650 | } |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1651 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1652 | /* proto > 0: write in batches of BATCHSIZE. */ |
| 1653 | do { |
| 1654 | /* Get first item */ |
| 1655 | firstitem = PyIter_Next(iter); |
| 1656 | if (firstitem == NULL) { |
| 1657 | if (PyErr_Occurred()) |
| 1658 | goto BatchFailed; |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1659 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1660 | /* nothing more to add */ |
| 1661 | break; |
| 1662 | } |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1663 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1664 | /* Try to get a second item */ |
| 1665 | obj = PyIter_Next(iter); |
| 1666 | if (obj == NULL) { |
| 1667 | if (PyErr_Occurred()) |
| 1668 | goto BatchFailed; |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1669 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1670 | /* Only one item to write */ |
| 1671 | if (save(self, firstitem, 0) < 0) |
| 1672 | goto BatchFailed; |
| 1673 | if (self->write_func(self, &append, 1) < 0) |
| 1674 | goto BatchFailed; |
| 1675 | Py_CLEAR(firstitem); |
| 1676 | break; |
| 1677 | } |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1678 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1679 | /* More than one item to write */ |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1680 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1681 | /* Pump out MARK, items, APPENDS. */ |
| 1682 | if (self->write_func(self, &MARKv, 1) < 0) |
| 1683 | goto BatchFailed; |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1684 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1685 | if (save(self, firstitem, 0) < 0) |
| 1686 | goto BatchFailed; |
| 1687 | Py_CLEAR(firstitem); |
| 1688 | n = 1; |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1689 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1690 | /* Fetch and save up to BATCHSIZE items */ |
| 1691 | while (obj) { |
| 1692 | if (save(self, obj, 0) < 0) |
| 1693 | goto BatchFailed; |
| 1694 | Py_CLEAR(obj); |
| 1695 | n += 1; |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1696 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1697 | if (n == BATCHSIZE) |
| 1698 | break; |
| 1699 | |
| 1700 | obj = PyIter_Next(iter); |
| 1701 | if (obj == NULL) { |
| 1702 | if (PyErr_Occurred()) |
| 1703 | goto BatchFailed; |
| 1704 | break; |
| 1705 | } |
| 1706 | } |
| 1707 | |
| 1708 | if (self->write_func(self, &appends, 1) < 0) |
| 1709 | goto BatchFailed; |
| 1710 | |
| 1711 | } while (n == BATCHSIZE); |
| 1712 | return 0; |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1713 | |
| 1714 | BatchFailed: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1715 | Py_XDECREF(firstitem); |
| 1716 | Py_XDECREF(obj); |
| 1717 | return -1; |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1718 | } |
| 1719 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1720 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1721 | save_list(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1722 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1723 | int res = -1; |
| 1724 | char s[3]; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 1725 | Py_ssize_t len; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1726 | PyObject *iter; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1727 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1728 | if (self->fast && !fast_save_enter(self, args)) |
| 1729 | goto finally; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 1730 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1731 | /* Create an empty list. */ |
| 1732 | if (self->bin) { |
| 1733 | s[0] = EMPTY_LIST; |
| 1734 | len = 1; |
| 1735 | } |
| 1736 | else { |
| 1737 | s[0] = MARK; |
| 1738 | s[1] = LIST; |
| 1739 | len = 2; |
| 1740 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1741 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1742 | if (self->write_func(self, s, len) < 0) |
| 1743 | goto finally; |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1744 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1745 | /* Get list length, and bow out early if empty. */ |
| 1746 | if ((len = PyList_Size(args)) < 0) |
| 1747 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1748 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1749 | /* Memoize. */ |
| 1750 | if (len == 0) { |
| 1751 | if (put(self, args) >= 0) |
| 1752 | res = 0; |
| 1753 | goto finally; |
| 1754 | } |
| 1755 | if (put2(self, args) < 0) |
| 1756 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1757 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1758 | /* Materialize the list elements. */ |
| 1759 | iter = PyObject_GetIter(args); |
| 1760 | if (iter == NULL) |
| 1761 | goto finally; |
Facundo Batista | 763d309 | 2008-06-30 01:10:55 +0000 | [diff] [blame] | 1762 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1763 | if (Py_EnterRecursiveCall(" while pickling an object") == 0) |
| 1764 | { |
| 1765 | res = batch_list(self, iter); |
| 1766 | Py_LeaveRecursiveCall(); |
| 1767 | } |
| 1768 | Py_DECREF(iter); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1769 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1770 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1771 | if (self->fast && !fast_save_leave(self, args)) |
| 1772 | res = -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1773 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1774 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1775 | } |
| 1776 | |
| 1777 | |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1778 | /* iter is an iterator giving (key, value) pairs, and we batch up chunks of |
| 1779 | * MARK key value ... key value SETITEMS |
| 1780 | * opcode sequences. Calling code should have arranged to first create an |
| 1781 | * empty dict, or dict-like object, for the SETITEMS to operate on. |
| 1782 | * Returns 0 on success, <0 on error. |
| 1783 | * |
| 1784 | * This is very much like batch_list(). The difference between saving |
| 1785 | * elements directly, and picking apart two-tuples, is so long-winded at |
| 1786 | * the C level, though, that attempts to combine these routines were too |
| 1787 | * ugly to bear. |
| 1788 | */ |
| 1789 | static int |
| 1790 | batch_dict(Picklerobject *self, PyObject *iter) |
| 1791 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1792 | PyObject *p = NULL; |
| 1793 | PyObject *firstitem = NULL; |
| 1794 | int i, n; |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1795 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1796 | static char setitem = SETITEM; |
| 1797 | static char setitems = SETITEMS; |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1798 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1799 | assert(iter != NULL); |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1800 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1801 | if (self->proto == 0) { |
| 1802 | /* SETITEMS isn't available; do one at a time. */ |
| 1803 | for (;;) { |
| 1804 | p = PyIter_Next(iter); |
| 1805 | if (p == NULL) { |
| 1806 | if (PyErr_Occurred()) |
| 1807 | return -1; |
| 1808 | break; |
| 1809 | } |
| 1810 | if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) { |
| 1811 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 1812 | "iterator must return 2-tuples"); |
| 1813 | return -1; |
| 1814 | } |
| 1815 | i = save(self, PyTuple_GET_ITEM(p, 0), 0); |
| 1816 | if (i >= 0) |
| 1817 | i = save(self, PyTuple_GET_ITEM(p, 1), 0); |
| 1818 | Py_DECREF(p); |
| 1819 | if (i < 0) |
| 1820 | return -1; |
| 1821 | if (self->write_func(self, &setitem, 1) < 0) |
| 1822 | return -1; |
| 1823 | } |
| 1824 | return 0; |
| 1825 | } |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1826 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1827 | /* proto > 0: write in batches of BATCHSIZE. */ |
| 1828 | do { |
| 1829 | /* Get first item */ |
| 1830 | firstitem = PyIter_Next(iter); |
| 1831 | if (firstitem == NULL) { |
| 1832 | if (PyErr_Occurred()) |
| 1833 | goto BatchFailed; |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1834 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1835 | /* nothing more to add */ |
| 1836 | break; |
| 1837 | } |
| 1838 | if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) { |
| 1839 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 1840 | "iterator must return 2-tuples"); |
| 1841 | goto BatchFailed; |
| 1842 | } |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1843 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1844 | /* Try to get a second item */ |
| 1845 | p = PyIter_Next(iter); |
| 1846 | if (p == NULL) { |
| 1847 | if (PyErr_Occurred()) |
| 1848 | goto BatchFailed; |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1849 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1850 | /* Only one item to write */ |
| 1851 | if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0) |
| 1852 | goto BatchFailed; |
| 1853 | if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0) |
| 1854 | goto BatchFailed; |
| 1855 | if (self->write_func(self, &setitem, 1) < 0) |
| 1856 | goto BatchFailed; |
| 1857 | Py_CLEAR(firstitem); |
| 1858 | break; |
| 1859 | } |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1860 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1861 | /* More than one item to write */ |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1862 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1863 | /* Pump out MARK, items, SETITEMS. */ |
| 1864 | if (self->write_func(self, &MARKv, 1) < 0) |
| 1865 | goto BatchFailed; |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1866 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1867 | if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0) |
| 1868 | goto BatchFailed; |
| 1869 | if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0) |
| 1870 | goto BatchFailed; |
| 1871 | Py_CLEAR(firstitem); |
| 1872 | n = 1; |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1873 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1874 | /* Fetch and save up to BATCHSIZE items */ |
| 1875 | while (p) { |
| 1876 | if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) { |
| 1877 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 1878 | "iterator must return 2-tuples"); |
| 1879 | goto BatchFailed; |
| 1880 | } |
| 1881 | if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0) |
| 1882 | goto BatchFailed; |
| 1883 | if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0) |
| 1884 | goto BatchFailed; |
| 1885 | Py_CLEAR(p); |
| 1886 | n += 1; |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1887 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1888 | if (n == BATCHSIZE) |
| 1889 | break; |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1890 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1891 | p = PyIter_Next(iter); |
| 1892 | if (p == NULL) { |
| 1893 | if (PyErr_Occurred()) |
| 1894 | goto BatchFailed; |
| 1895 | break; |
| 1896 | } |
| 1897 | } |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1898 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1899 | if (self->write_func(self, &setitems, 1) < 0) |
| 1900 | goto BatchFailed; |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1901 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1902 | } while (n == BATCHSIZE); |
| 1903 | return 0; |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1904 | |
| 1905 | BatchFailed: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1906 | Py_XDECREF(firstitem); |
| 1907 | Py_XDECREF(p); |
| 1908 | return -1; |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1909 | } |
| 1910 | |
Collin Winter | 179bf21 | 2009-05-25 04:34:39 +0000 | [diff] [blame] | 1911 | /* This is a variant of batch_dict() above that specializes for dicts, with no |
| 1912 | * support for dict subclasses. Like batch_dict(), we batch up chunks of |
| 1913 | * MARK key value ... key value SETITEMS |
| 1914 | * opcode sequences. Calling code should have arranged to first create an |
| 1915 | * empty dict, or dict-like object, for the SETITEMS to operate on. |
| 1916 | * Returns 0 on success, -1 on error. |
| 1917 | * |
| 1918 | * Note that this currently doesn't work for protocol 0. |
| 1919 | */ |
| 1920 | static int |
| 1921 | batch_dict_exact(Picklerobject *self, PyObject *obj) |
| 1922 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1923 | PyObject *key = NULL, *value = NULL; |
| 1924 | int i; |
| 1925 | Py_ssize_t dict_size, ppos = 0; |
Collin Winter | 179bf21 | 2009-05-25 04:34:39 +0000 | [diff] [blame] | 1926 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1927 | static char setitem = SETITEM; |
| 1928 | static char setitems = SETITEMS; |
Collin Winter | 179bf21 | 2009-05-25 04:34:39 +0000 | [diff] [blame] | 1929 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1930 | assert(obj != NULL); |
| 1931 | assert(self->proto > 0); |
Collin Winter | 179bf21 | 2009-05-25 04:34:39 +0000 | [diff] [blame] | 1932 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1933 | dict_size = PyDict_Size(obj); |
Collin Winter | 179bf21 | 2009-05-25 04:34:39 +0000 | [diff] [blame] | 1934 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1935 | /* Special-case len(d) == 1 to save space. */ |
| 1936 | if (dict_size == 1) { |
| 1937 | PyDict_Next(obj, &ppos, &key, &value); |
| 1938 | if (save(self, key, 0) < 0) |
| 1939 | return -1; |
| 1940 | if (save(self, value, 0) < 0) |
| 1941 | return -1; |
| 1942 | if (self->write_func(self, &setitem, 1) < 0) |
| 1943 | return -1; |
| 1944 | return 0; |
| 1945 | } |
Collin Winter | 179bf21 | 2009-05-25 04:34:39 +0000 | [diff] [blame] | 1946 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1947 | /* Write in batches of BATCHSIZE. */ |
| 1948 | do { |
| 1949 | i = 0; |
| 1950 | if (self->write_func(self, &MARKv, 1) < 0) |
| 1951 | return -1; |
| 1952 | while (PyDict_Next(obj, &ppos, &key, &value)) { |
| 1953 | if (save(self, key, 0) < 0) |
| 1954 | return -1; |
| 1955 | if (save(self, value, 0) < 0) |
| 1956 | return -1; |
| 1957 | if (++i == BATCHSIZE) |
| 1958 | break; |
| 1959 | } |
| 1960 | if (self->write_func(self, &setitems, 1) < 0) |
| 1961 | return -1; |
| 1962 | if (PyDict_Size(obj) != dict_size) { |
| 1963 | PyErr_Format( |
| 1964 | PyExc_RuntimeError, |
| 1965 | "dictionary changed size during iteration"); |
| 1966 | return -1; |
| 1967 | } |
Collin Winter | 179bf21 | 2009-05-25 04:34:39 +0000 | [diff] [blame] | 1968 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1969 | } while (i == BATCHSIZE); |
| 1970 | return 0; |
Collin Winter | 179bf21 | 2009-05-25 04:34:39 +0000 | [diff] [blame] | 1971 | } |
| 1972 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1973 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1974 | save_dict(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1975 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1976 | int res = -1; |
| 1977 | char s[3]; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 1978 | Py_ssize_t len; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1979 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1980 | if (self->fast && !fast_save_enter(self, args)) |
| 1981 | goto finally; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 1982 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1983 | /* Create an empty dict. */ |
| 1984 | if (self->bin) { |
| 1985 | s[0] = EMPTY_DICT; |
| 1986 | len = 1; |
| 1987 | } |
| 1988 | else { |
| 1989 | s[0] = MARK; |
| 1990 | s[1] = DICT; |
| 1991 | len = 2; |
| 1992 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1993 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1994 | if (self->write_func(self, s, len) < 0) |
| 1995 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1996 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1997 | /* Get dict size, and bow out early if empty. */ |
| 1998 | if ((len = PyDict_Size(args)) < 0) |
| 1999 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2000 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2001 | if (len == 0) { |
| 2002 | if (put(self, args) >= 0) |
| 2003 | res = 0; |
| 2004 | goto finally; |
| 2005 | } |
| 2006 | if (put2(self, args) < 0) |
| 2007 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2008 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2009 | /* Materialize the dict items. */ |
| 2010 | if (PyDict_CheckExact(args) && self->proto > 0) { |
| 2011 | /* We can take certain shortcuts if we know this is a dict and |
| 2012 | not a dict subclass. */ |
| 2013 | if (Py_EnterRecursiveCall(" while pickling an object") == 0) { |
| 2014 | res = batch_dict_exact(self, args); |
| 2015 | Py_LeaveRecursiveCall(); |
| 2016 | } |
| 2017 | } else { |
| 2018 | PyObject *iter = PyObject_CallMethod(args, "iteritems", "()"); |
| 2019 | if (iter == NULL) |
| 2020 | goto finally; |
| 2021 | if (Py_EnterRecursiveCall(" while pickling an object") == 0) { |
| 2022 | res = batch_dict(self, iter); |
| 2023 | Py_LeaveRecursiveCall(); |
| 2024 | } |
| 2025 | Py_DECREF(iter); |
| 2026 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2027 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2028 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2029 | if (self->fast && !fast_save_leave(self, args)) |
| 2030 | res = -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2031 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2032 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2033 | } |
| 2034 | |
| 2035 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2036 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2037 | save_inst(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2038 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2039 | PyObject *class = 0, *module = 0, *name = 0, *state = 0, |
| 2040 | *getinitargs_func = 0, *getstate_func = 0, *class_args = 0; |
| 2041 | char *module_str, *name_str; |
| 2042 | int module_size, name_size, res = -1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2043 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2044 | static char inst = INST, obj = OBJ, build = BUILD; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2045 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2046 | if (self->fast && !fast_save_enter(self, args)) |
| 2047 | goto finally; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 2048 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2049 | if (self->write_func(self, &MARKv, 1) < 0) |
| 2050 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2051 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2052 | if (!( class = PyObject_GetAttr(args, __class___str))) |
| 2053 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2054 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2055 | if (self->bin) { |
| 2056 | if (save(self, class, 0) < 0) |
| 2057 | goto finally; |
| 2058 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2059 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2060 | if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) { |
| 2061 | PyObject *element = 0; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 2062 | Py_ssize_t i, len; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2063 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2064 | if (!( class_args = |
| 2065 | PyObject_Call(getinitargs_func, empty_tuple, NULL))) |
| 2066 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2067 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2068 | if ((len = PyObject_Size(class_args)) < 0) |
| 2069 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2070 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2071 | for (i = 0; i < len; i++) { |
| 2072 | if (!( element = PySequence_GetItem(class_args, i))) |
| 2073 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2074 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2075 | if (save(self, element, 0) < 0) { |
| 2076 | Py_DECREF(element); |
| 2077 | goto finally; |
| 2078 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2079 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2080 | Py_DECREF(element); |
| 2081 | } |
| 2082 | } |
| 2083 | else { |
| 2084 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 2085 | PyErr_Clear(); |
| 2086 | else |
| 2087 | goto finally; |
| 2088 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2089 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2090 | if (!self->bin) { |
| 2091 | if (!( name = ((PyClassObject *)class)->cl_name )) { |
| 2092 | PyErr_SetString(PicklingError, "class has no name"); |
| 2093 | goto finally; |
| 2094 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2095 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2096 | if (!( module = whichmodule(class, name))) |
| 2097 | goto finally; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 2098 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2099 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2100 | if ((module_size = PyString_Size(module)) < 0 || |
| 2101 | (name_size = PyString_Size(name)) < 0) |
| 2102 | goto finally; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 2103 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2104 | module_str = PyString_AS_STRING((PyStringObject *)module); |
| 2105 | name_str = PyString_AS_STRING((PyStringObject *)name); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2106 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2107 | if (self->write_func(self, &inst, 1) < 0) |
| 2108 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2109 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2110 | if (self->write_func(self, module_str, module_size) < 0) |
| 2111 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2112 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2113 | if (self->write_func(self, "\n", 1) < 0) |
| 2114 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2115 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2116 | if (self->write_func(self, name_str, name_size) < 0) |
| 2117 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2118 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2119 | if (self->write_func(self, "\n", 1) < 0) |
| 2120 | goto finally; |
| 2121 | } |
| 2122 | else if (self->write_func(self, &obj, 1) < 0) { |
| 2123 | goto finally; |
| 2124 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2125 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2126 | if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) { |
| 2127 | state = PyObject_Call(getstate_func, empty_tuple, NULL); |
| 2128 | if (!state) |
| 2129 | goto finally; |
| 2130 | } |
| 2131 | else { |
| 2132 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 2133 | PyErr_Clear(); |
| 2134 | else |
| 2135 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2136 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2137 | if (!( state = PyObject_GetAttr(args, __dict___str))) { |
| 2138 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 2139 | PyErr_Clear(); |
| 2140 | else |
| 2141 | goto finally; |
| 2142 | res = 0; |
| 2143 | goto finally; |
| 2144 | } |
| 2145 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2146 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2147 | if (!PyDict_Check(state)) { |
| 2148 | if (put2(self, args) < 0) |
| 2149 | goto finally; |
| 2150 | } |
| 2151 | else { |
| 2152 | if (put(self, args) < 0) |
| 2153 | goto finally; |
| 2154 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2155 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2156 | if (save(self, state, 0) < 0) |
| 2157 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2158 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2159 | if (self->write_func(self, &build, 1) < 0) |
| 2160 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2161 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2162 | res = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2163 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2164 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2165 | if (self->fast && !fast_save_leave(self, args)) |
| 2166 | res = -1; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 2167 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2168 | Py_XDECREF(module); |
| 2169 | Py_XDECREF(class); |
| 2170 | Py_XDECREF(state); |
| 2171 | Py_XDECREF(getinitargs_func); |
| 2172 | Py_XDECREF(getstate_func); |
| 2173 | Py_XDECREF(class_args); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2174 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2175 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2176 | } |
| 2177 | |
| 2178 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2179 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2180 | save_global(Picklerobject *self, PyObject *args, PyObject *name) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2181 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2182 | PyObject *global_name = 0, *module = 0, *mod = 0, *klass = 0; |
| 2183 | char *name_str, *module_str; |
| 2184 | int module_size, name_size, res = -1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2185 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2186 | static char global = GLOBAL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2187 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2188 | if (name) { |
| 2189 | global_name = name; |
| 2190 | Py_INCREF(global_name); |
| 2191 | } |
| 2192 | else { |
| 2193 | if (!( global_name = PyObject_GetAttr(args, __name___str))) |
| 2194 | goto finally; |
| 2195 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2196 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2197 | if (!( module = whichmodule(args, global_name))) |
| 2198 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2199 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2200 | if ((module_size = PyString_Size(module)) < 0 || |
| 2201 | (name_size = PyString_Size(global_name)) < 0) |
| 2202 | goto finally; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 2203 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2204 | module_str = PyString_AS_STRING((PyStringObject *)module); |
| 2205 | name_str = PyString_AS_STRING((PyStringObject *)global_name); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2206 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2207 | /* XXX This can be doing a relative import. Clearly it shouldn't, |
| 2208 | but I don't know how to stop it. :-( */ |
| 2209 | mod = PyImport_ImportModule(module_str); |
| 2210 | if (mod == NULL) { |
| 2211 | cPickle_ErrFormat(PicklingError, |
| 2212 | "Can't pickle %s: import of module %s " |
| 2213 | "failed", |
| 2214 | "OS", args, module); |
| 2215 | goto finally; |
| 2216 | } |
| 2217 | klass = PyObject_GetAttrString(mod, name_str); |
| 2218 | if (klass == NULL) { |
| 2219 | cPickle_ErrFormat(PicklingError, |
| 2220 | "Can't pickle %s: attribute lookup %s.%s " |
| 2221 | "failed", |
| 2222 | "OSS", args, module, global_name); |
| 2223 | goto finally; |
| 2224 | } |
| 2225 | if (klass != args) { |
| 2226 | Py_DECREF(klass); |
| 2227 | cPickle_ErrFormat(PicklingError, |
| 2228 | "Can't pickle %s: it's not the same object " |
| 2229 | "as %s.%s", |
| 2230 | "OSS", args, module, global_name); |
| 2231 | goto finally; |
| 2232 | } |
| 2233 | Py_DECREF(klass); |
Guido van Rossum | a92d16a | 2001-08-18 21:22:07 +0000 | [diff] [blame] | 2234 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2235 | if (self->proto >= 2) { |
| 2236 | /* See whether this is in the extension registry, and if |
| 2237 | * so generate an EXT opcode. |
| 2238 | */ |
| 2239 | PyObject *py_code; /* extension code as Python object */ |
| 2240 | long code; /* extension code as C value */ |
| 2241 | char c_str[5]; |
| 2242 | int n; |
Tim Peters | 731098b | 2003-02-04 20:56:09 +0000 | [diff] [blame] | 2243 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2244 | PyTuple_SET_ITEM(two_tuple, 0, module); |
| 2245 | PyTuple_SET_ITEM(two_tuple, 1, global_name); |
| 2246 | py_code = PyDict_GetItem(extension_registry, two_tuple); |
| 2247 | if (py_code == NULL) |
| 2248 | goto gen_global; /* not registered */ |
Tim Peters | 731098b | 2003-02-04 20:56:09 +0000 | [diff] [blame] | 2249 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2250 | /* Verify py_code has the right type and value. */ |
| 2251 | if (!PyInt_Check(py_code)) { |
| 2252 | cPickle_ErrFormat(PicklingError, "Can't pickle %s: " |
| 2253 | "extension code %s isn't an integer", |
| 2254 | "OO", args, py_code); |
| 2255 | goto finally; |
| 2256 | } |
| 2257 | code = PyInt_AS_LONG(py_code); |
| 2258 | if (code <= 0 || code > 0x7fffffffL) { |
| 2259 | cPickle_ErrFormat(PicklingError, "Can't pickle %s: " |
| 2260 | "extension code %ld is out of range", |
| 2261 | "Ol", args, code); |
| 2262 | goto finally; |
| 2263 | } |
Tim Peters | 731098b | 2003-02-04 20:56:09 +0000 | [diff] [blame] | 2264 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2265 | /* Generate an EXT opcode. */ |
| 2266 | if (code <= 0xff) { |
| 2267 | c_str[0] = EXT1; |
| 2268 | c_str[1] = (char)code; |
| 2269 | n = 2; |
| 2270 | } |
| 2271 | else if (code <= 0xffff) { |
| 2272 | c_str[0] = EXT2; |
| 2273 | c_str[1] = (char)(code & 0xff); |
| 2274 | c_str[2] = (char)((code >> 8) & 0xff); |
| 2275 | n = 3; |
| 2276 | } |
| 2277 | else { |
| 2278 | c_str[0] = EXT4; |
| 2279 | c_str[1] = (char)(code & 0xff); |
| 2280 | c_str[2] = (char)((code >> 8) & 0xff); |
| 2281 | c_str[3] = (char)((code >> 16) & 0xff); |
| 2282 | c_str[4] = (char)((code >> 24) & 0xff); |
| 2283 | n = 5; |
| 2284 | } |
Tim Peters | 731098b | 2003-02-04 20:56:09 +0000 | [diff] [blame] | 2285 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2286 | if (self->write_func(self, c_str, n) >= 0) |
| 2287 | res = 0; |
| 2288 | goto finally; /* and don't memoize */ |
| 2289 | } |
Tim Peters | 731098b | 2003-02-04 20:56:09 +0000 | [diff] [blame] | 2290 | |
| 2291 | gen_global: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2292 | if (self->write_func(self, &global, 1) < 0) |
| 2293 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2294 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2295 | if (self->write_func(self, module_str, module_size) < 0) |
| 2296 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2297 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2298 | if (self->write_func(self, "\n", 1) < 0) |
| 2299 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2300 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2301 | if (self->write_func(self, name_str, name_size) < 0) |
| 2302 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2303 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2304 | if (self->write_func(self, "\n", 1) < 0) |
| 2305 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2306 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2307 | if (put(self, args) < 0) |
| 2308 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2309 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2310 | res = 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2311 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2312 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2313 | Py_XDECREF(module); |
| 2314 | Py_XDECREF(global_name); |
| 2315 | Py_XDECREF(mod); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2316 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2317 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2318 | } |
| 2319 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2320 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2321 | save_pers(Picklerobject *self, PyObject *args, PyObject *f) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2322 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2323 | PyObject *pid = 0; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 2324 | Py_ssize_t size; |
| 2325 | int res = -1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2326 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2327 | static char persid = PERSID, binpersid = BINPERSID; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2328 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2329 | Py_INCREF(args); |
| 2330 | ARG_TUP(self, args); |
| 2331 | if (self->arg) { |
| 2332 | pid = PyObject_Call(f, self->arg, NULL); |
| 2333 | FREE_ARG_TUP(self); |
| 2334 | } |
| 2335 | if (! pid) return -1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2336 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2337 | if (pid != Py_None) { |
| 2338 | if (!self->bin) { |
| 2339 | if (!PyString_Check(pid)) { |
| 2340 | PyErr_SetString(PicklingError, |
| 2341 | "persistent id must be string"); |
| 2342 | goto finally; |
| 2343 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2344 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2345 | if (self->write_func(self, &persid, 1) < 0) |
| 2346 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2347 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2348 | if ((size = PyString_Size(pid)) < 0) |
| 2349 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2350 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2351 | if (self->write_func(self, |
| 2352 | PyString_AS_STRING( |
| 2353 | (PyStringObject *)pid), |
| 2354 | size) < 0) |
| 2355 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2356 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2357 | if (self->write_func(self, "\n", 1) < 0) |
| 2358 | goto finally; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2359 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2360 | res = 1; |
| 2361 | goto finally; |
| 2362 | } |
| 2363 | else if (save(self, pid, 1) >= 0) { |
| 2364 | if (self->write_func(self, &binpersid, 1) < 0) |
| 2365 | res = -1; |
| 2366 | else |
| 2367 | res = 1; |
| 2368 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2369 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2370 | goto finally; |
| 2371 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2372 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2373 | res = 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2374 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2375 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2376 | Py_XDECREF(pid); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2377 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2378 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2379 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2380 | |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2381 | /* We're saving ob, and args is the 2-thru-5 tuple returned by the |
| 2382 | * appropriate __reduce__ method for ob. |
| 2383 | */ |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2384 | static int |
Amaury Forgeot d'Arc | 69a9c5b | 2008-10-30 21:18:34 +0000 | [diff] [blame] | 2385 | save_reduce(Picklerobject *self, PyObject *args, PyObject *fn, PyObject *ob) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2386 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2387 | PyObject *callable; |
| 2388 | PyObject *argtup; |
| 2389 | PyObject *state = NULL; |
| 2390 | PyObject *listitems = Py_None; |
| 2391 | PyObject *dictitems = Py_None; |
| 2392 | Py_ssize_t size; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2393 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2394 | int use_newobj = self->proto >= 2; |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2395 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2396 | static char reduce = REDUCE; |
| 2397 | static char build = BUILD; |
| 2398 | static char newobj = NEWOBJ; |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2399 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2400 | size = PyTuple_Size(args); |
| 2401 | if (size < 2 || size > 5) { |
| 2402 | cPickle_ErrFormat(PicklingError, "tuple returned by " |
| 2403 | "%s must contain 2 through 5 elements", |
| 2404 | "O", fn); |
| 2405 | return -1; |
| 2406 | } |
Amaury Forgeot d'Arc | 69a9c5b | 2008-10-30 21:18:34 +0000 | [diff] [blame] | 2407 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2408 | if (! PyArg_UnpackTuple(args, "save_reduce", 2, 5, |
| 2409 | &callable, |
| 2410 | &argtup, |
| 2411 | &state, |
| 2412 | &listitems, |
| 2413 | &dictitems)) |
| 2414 | return -1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2415 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2416 | if (!PyTuple_Check(argtup)) { |
| 2417 | cPickle_ErrFormat(PicklingError, "Second element of " |
| 2418 | "tuple returned by %s must be a tuple", |
| 2419 | "O", fn); |
| 2420 | return -1; |
| 2421 | } |
Raymond Hettinger | a6b45cc | 2004-12-07 07:05:57 +0000 | [diff] [blame] | 2422 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2423 | if (state == Py_None) |
| 2424 | state = NULL; |
Amaury Forgeot d'Arc | 69a9c5b | 2008-10-30 21:18:34 +0000 | [diff] [blame] | 2425 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2426 | if (listitems == Py_None) |
| 2427 | listitems = NULL; |
| 2428 | else if (!PyIter_Check(listitems)) { |
| 2429 | cPickle_ErrFormat(PicklingError, "Fourth element of " |
| 2430 | "tuple returned by %s must be an iterator, not %s", |
| 2431 | "Os", fn, Py_TYPE(listitems)->tp_name); |
| 2432 | return -1; |
| 2433 | } |
Amaury Forgeot d'Arc | 69a9c5b | 2008-10-30 21:18:34 +0000 | [diff] [blame] | 2434 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2435 | if (dictitems == Py_None) |
| 2436 | dictitems = NULL; |
| 2437 | else if (!PyIter_Check(dictitems)) { |
| 2438 | cPickle_ErrFormat(PicklingError, "Fifth element of " |
| 2439 | "tuple returned by %s must be an iterator, not %s", |
| 2440 | "Os", fn, Py_TYPE(dictitems)->tp_name); |
| 2441 | return -1; |
| 2442 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2443 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2444 | /* Protocol 2 special case: if callable's name is __newobj__, use |
| 2445 | * NEWOBJ. This consumes a lot of code. |
| 2446 | */ |
| 2447 | if (use_newobj) { |
| 2448 | PyObject *temp = PyObject_GetAttr(callable, __name___str); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2449 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2450 | if (temp == NULL) { |
| 2451 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 2452 | PyErr_Clear(); |
| 2453 | else |
| 2454 | return -1; |
| 2455 | use_newobj = 0; |
| 2456 | } |
| 2457 | else { |
| 2458 | use_newobj = PyString_Check(temp) && |
| 2459 | strcmp(PyString_AS_STRING(temp), |
| 2460 | "__newobj__") == 0; |
| 2461 | Py_DECREF(temp); |
| 2462 | } |
| 2463 | } |
| 2464 | if (use_newobj) { |
| 2465 | PyObject *cls; |
| 2466 | PyObject *newargtup; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 2467 | Py_ssize_t n, i; |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2468 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2469 | /* Sanity checks. */ |
| 2470 | n = PyTuple_Size(argtup); |
| 2471 | if (n < 1) { |
| 2472 | PyErr_SetString(PicklingError, "__newobj__ arglist " |
| 2473 | "is empty"); |
| 2474 | return -1; |
| 2475 | } |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2476 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2477 | cls = PyTuple_GET_ITEM(argtup, 0); |
| 2478 | if (! PyObject_HasAttrString(cls, "__new__")) { |
| 2479 | PyErr_SetString(PicklingError, "args[0] from " |
| 2480 | "__newobj__ args has no __new__"); |
| 2481 | return -1; |
| 2482 | } |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2483 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2484 | /* XXX How could ob be NULL? */ |
| 2485 | if (ob != NULL) { |
| 2486 | PyObject *ob_dot_class; |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2487 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2488 | ob_dot_class = PyObject_GetAttr(ob, __class___str); |
| 2489 | if (ob_dot_class == NULL) { |
| 2490 | if (PyErr_ExceptionMatches( |
| 2491 | PyExc_AttributeError)) |
| 2492 | PyErr_Clear(); |
| 2493 | else |
| 2494 | return -1; |
| 2495 | } |
| 2496 | i = ob_dot_class != cls; /* true iff a problem */ |
| 2497 | Py_XDECREF(ob_dot_class); |
| 2498 | if (i) { |
| 2499 | PyErr_SetString(PicklingError, "args[0] from " |
| 2500 | "__newobj__ args has the wrong class"); |
| 2501 | return -1; |
| 2502 | } |
| 2503 | } |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2504 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2505 | /* Save the class and its __new__ arguments. */ |
| 2506 | if (save(self, cls, 0) < 0) |
| 2507 | return -1; |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2508 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2509 | newargtup = PyTuple_New(n-1); /* argtup[1:] */ |
| 2510 | if (newargtup == NULL) |
| 2511 | return -1; |
| 2512 | for (i = 1; i < n; ++i) { |
| 2513 | PyObject *temp = PyTuple_GET_ITEM(argtup, i); |
| 2514 | Py_INCREF(temp); |
| 2515 | PyTuple_SET_ITEM(newargtup, i-1, temp); |
| 2516 | } |
| 2517 | i = save(self, newargtup, 0); |
| 2518 | Py_DECREF(newargtup); |
| 2519 | if (i < 0) |
| 2520 | return -1; |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2521 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2522 | /* Add NEWOBJ opcode. */ |
| 2523 | if (self->write_func(self, &newobj, 1) < 0) |
| 2524 | return -1; |
| 2525 | } |
| 2526 | else { |
| 2527 | /* Not using NEWOBJ. */ |
| 2528 | if (save(self, callable, 0) < 0 || |
| 2529 | save(self, argtup, 0) < 0 || |
| 2530 | self->write_func(self, &reduce, 1) < 0) |
| 2531 | return -1; |
| 2532 | } |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2533 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2534 | /* Memoize. */ |
| 2535 | /* XXX How can ob be NULL? */ |
| 2536 | if (ob != NULL) { |
| 2537 | if (state && !PyDict_Check(state)) { |
| 2538 | if (put2(self, ob) < 0) |
| 2539 | return -1; |
| 2540 | } |
| 2541 | else if (put(self, ob) < 0) |
| 2542 | return -1; |
| 2543 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2544 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2545 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2546 | if (listitems && batch_list(self, listitems) < 0) |
| 2547 | return -1; |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2548 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2549 | if (dictitems && batch_dict(self, dictitems) < 0) |
| 2550 | return -1; |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2551 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2552 | if (state) { |
| 2553 | if (save(self, state, 0) < 0 || |
| 2554 | self->write_func(self, &build, 1) < 0) |
| 2555 | return -1; |
| 2556 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2557 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2558 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2559 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2560 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2561 | static int |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 2562 | save(Picklerobject *self, PyObject *args, int pers_save) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2563 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2564 | PyTypeObject *type; |
| 2565 | PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0; |
| 2566 | int res = -1; |
| 2567 | int tmp; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2568 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2569 | if (Py_EnterRecursiveCall(" while pickling an object")) |
| 2570 | return -1; |
Martin v. Löwis | 5a39530 | 2002-08-04 08:20:23 +0000 | [diff] [blame] | 2571 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2572 | if (!pers_save && self->pers_func) { |
| 2573 | if ((tmp = save_pers(self, args, self->pers_func)) != 0) { |
| 2574 | res = tmp; |
| 2575 | goto finally; |
| 2576 | } |
| 2577 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2578 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2579 | if (args == Py_None) { |
| 2580 | res = save_none(self, args); |
| 2581 | goto finally; |
| 2582 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2583 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2584 | type = Py_TYPE(args); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2585 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2586 | switch (type->tp_name[0]) { |
| 2587 | case 'b': |
| 2588 | if (args == Py_False || args == Py_True) { |
| 2589 | res = save_bool(self, args); |
| 2590 | goto finally; |
| 2591 | } |
| 2592 | break; |
| 2593 | case 'i': |
| 2594 | if (type == &PyInt_Type) { |
| 2595 | res = save_int(self, args); |
| 2596 | goto finally; |
| 2597 | } |
| 2598 | break; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2599 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2600 | case 'l': |
| 2601 | if (type == &PyLong_Type) { |
| 2602 | res = save_long(self, args); |
| 2603 | goto finally; |
| 2604 | } |
| 2605 | break; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2606 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2607 | case 'f': |
| 2608 | if (type == &PyFloat_Type) { |
| 2609 | res = save_float(self, args); |
| 2610 | goto finally; |
| 2611 | } |
| 2612 | break; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2613 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2614 | case 't': |
| 2615 | if (type == &PyTuple_Type && PyTuple_Size(args) == 0) { |
| 2616 | res = save_tuple(self, args); |
| 2617 | goto finally; |
| 2618 | } |
| 2619 | break; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2620 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2621 | case 's': |
| 2622 | if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) { |
| 2623 | res = save_string(self, args, 0); |
| 2624 | goto finally; |
| 2625 | } |
| 2626 | break; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 2627 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2628 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2629 | case 'u': |
| 2630 | if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) { |
| 2631 | res = save_unicode(self, args, 0); |
| 2632 | goto finally; |
| 2633 | } |
| 2634 | break; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2635 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2636 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2637 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2638 | if (Py_REFCNT(args) > 1) { |
| 2639 | if (!( py_ob_id = PyLong_FromVoidPtr(args))) |
| 2640 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2641 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2642 | if (PyDict_GetItem(self->memo, py_ob_id)) { |
| 2643 | if (get(self, py_ob_id) < 0) |
| 2644 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2645 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2646 | res = 0; |
| 2647 | goto finally; |
| 2648 | } |
| 2649 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2650 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2651 | switch (type->tp_name[0]) { |
| 2652 | case 's': |
| 2653 | if (type == &PyString_Type) { |
| 2654 | res = save_string(self, args, 1); |
| 2655 | goto finally; |
| 2656 | } |
| 2657 | break; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2658 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2659 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2660 | case 'u': |
| 2661 | if (type == &PyUnicode_Type) { |
| 2662 | res = save_unicode(self, args, 1); |
| 2663 | goto finally; |
| 2664 | } |
| 2665 | break; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2666 | #endif |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 2667 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2668 | case 't': |
| 2669 | if (type == &PyTuple_Type) { |
| 2670 | res = save_tuple(self, args); |
| 2671 | goto finally; |
| 2672 | } |
| 2673 | if (type == &PyType_Type) { |
Alexandre Vassalotti | df9460f | 2013-11-30 17:43:42 -0800 | [diff] [blame] | 2674 | res = save_global(self, args, NULL); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2675 | goto finally; |
| 2676 | } |
| 2677 | break; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2678 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2679 | case 'l': |
| 2680 | if (type == &PyList_Type) { |
| 2681 | res = save_list(self, args); |
| 2682 | goto finally; |
| 2683 | } |
| 2684 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2685 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2686 | case 'd': |
| 2687 | if (type == &PyDict_Type) { |
| 2688 | res = save_dict(self, args); |
| 2689 | goto finally; |
| 2690 | } |
| 2691 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2692 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2693 | case 'i': |
| 2694 | if (type == &PyInstance_Type) { |
| 2695 | res = save_inst(self, args); |
| 2696 | goto finally; |
| 2697 | } |
| 2698 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2699 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2700 | case 'c': |
| 2701 | if (type == &PyClass_Type) { |
| 2702 | res = save_global(self, args, NULL); |
| 2703 | goto finally; |
| 2704 | } |
| 2705 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2706 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2707 | case 'f': |
| 2708 | if (type == &PyFunction_Type) { |
| 2709 | res = save_global(self, args, NULL); |
| 2710 | if (res && PyErr_ExceptionMatches(PickleError)) { |
| 2711 | /* fall back to reduce */ |
| 2712 | PyErr_Clear(); |
| 2713 | break; |
| 2714 | } |
| 2715 | goto finally; |
| 2716 | } |
| 2717 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2718 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2719 | case 'b': |
| 2720 | if (type == &PyCFunction_Type) { |
| 2721 | res = save_global(self, args, NULL); |
| 2722 | goto finally; |
| 2723 | } |
| 2724 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2725 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2726 | if (!pers_save && self->inst_pers_func) { |
| 2727 | if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) { |
| 2728 | res = tmp; |
| 2729 | goto finally; |
| 2730 | } |
| 2731 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2732 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2733 | /* Get a reduction callable, and call it. This may come from |
| 2734 | * copy_reg.dispatch_table, the object's __reduce_ex__ method, |
| 2735 | * or the object's __reduce__ method. |
| 2736 | */ |
| 2737 | __reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type); |
| 2738 | if (__reduce__ != NULL) { |
| 2739 | Py_INCREF(__reduce__); |
| 2740 | Py_INCREF(args); |
| 2741 | ARG_TUP(self, args); |
| 2742 | if (self->arg) { |
| 2743 | t = PyObject_Call(__reduce__, self->arg, NULL); |
| 2744 | FREE_ARG_TUP(self); |
| 2745 | } |
| 2746 | } |
| 2747 | else { |
Antoine Pitrou | 561a821 | 2011-10-04 09:34:48 +0200 | [diff] [blame] | 2748 | if (PyType_IsSubtype(type, &PyType_Type)) { |
| 2749 | res = save_global(self, args, NULL); |
| 2750 | goto finally; |
| 2751 | } |
| 2752 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2753 | /* Check for a __reduce_ex__ method. */ |
| 2754 | __reduce__ = PyObject_GetAttr(args, __reduce_ex___str); |
| 2755 | if (__reduce__ != NULL) { |
| 2756 | t = PyInt_FromLong(self->proto); |
| 2757 | if (t != NULL) { |
| 2758 | ARG_TUP(self, t); |
| 2759 | t = NULL; |
| 2760 | if (self->arg) { |
| 2761 | t = PyObject_Call(__reduce__, |
| 2762 | self->arg, NULL); |
| 2763 | FREE_ARG_TUP(self); |
| 2764 | } |
| 2765 | } |
| 2766 | } |
| 2767 | else { |
| 2768 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 2769 | PyErr_Clear(); |
| 2770 | else |
| 2771 | goto finally; |
| 2772 | /* Check for a __reduce__ method. */ |
| 2773 | __reduce__ = PyObject_GetAttr(args, __reduce___str); |
| 2774 | if (__reduce__ != NULL) { |
| 2775 | t = PyObject_Call(__reduce__, |
| 2776 | empty_tuple, NULL); |
| 2777 | } |
| 2778 | else { |
| 2779 | PyErr_SetObject(UnpickleableError, args); |
| 2780 | goto finally; |
| 2781 | } |
| 2782 | } |
| 2783 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2784 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2785 | if (t == NULL) |
| 2786 | goto finally; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2787 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2788 | if (PyString_Check(t)) { |
| 2789 | res = save_global(self, args, t); |
| 2790 | goto finally; |
| 2791 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2792 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2793 | if (!PyTuple_Check(t)) { |
| 2794 | cPickle_ErrFormat(PicklingError, "Value returned by " |
| 2795 | "%s must be string or tuple", |
| 2796 | "O", __reduce__); |
| 2797 | goto finally; |
| 2798 | } |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2799 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2800 | res = save_reduce(self, t, __reduce__, args); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2801 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2802 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2803 | Py_LeaveRecursiveCall(); |
| 2804 | Py_XDECREF(py_ob_id); |
| 2805 | Py_XDECREF(__reduce__); |
| 2806 | Py_XDECREF(t); |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2807 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2808 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2809 | } |
| 2810 | |
| 2811 | |
| 2812 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2813 | dump(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2814 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2815 | static char stop = STOP; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2816 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2817 | if (self->proto >= 2) { |
| 2818 | char bytes[2]; |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 2819 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2820 | bytes[0] = PROTO; |
| 2821 | assert(self->proto >= 0 && self->proto < 256); |
| 2822 | bytes[1] = (char)self->proto; |
| 2823 | if (self->write_func(self, bytes, 2) < 0) |
| 2824 | return -1; |
| 2825 | } |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 2826 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2827 | if (save(self, args, 0) < 0) |
| 2828 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2829 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2830 | if (self->write_func(self, &stop, 1) < 0) |
| 2831 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2832 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2833 | if (self->write_func(self, NULL, 0) < 0) |
| 2834 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2835 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2836 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2837 | } |
| 2838 | |
| 2839 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2840 | Pickle_clear_memo(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2841 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2842 | if (self->memo) |
| 2843 | PyDict_Clear(self->memo); |
| 2844 | Py_INCREF(Py_None); |
| 2845 | return Py_None; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2846 | } |
| 2847 | |
| 2848 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2849 | Pickle_getvalue(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2850 | { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 2851 | Py_ssize_t l, i, rsize, ssize, clear=1, lm; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2852 | long ik; |
| 2853 | PyObject *k, *r; |
| 2854 | char *s, *p, *have_get; |
| 2855 | Pdata *data; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2856 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2857 | /* Can be called by Python code or C code */ |
| 2858 | if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear)) |
| 2859 | return NULL; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2860 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2861 | /* Check to make sure we are based on a list */ |
| 2862 | if (! Pdata_Check(self->file)) { |
| 2863 | PyErr_SetString(PicklingError, |
| 2864 | "Attempt to getvalue() a non-list-based pickler"); |
| 2865 | return NULL; |
| 2866 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2867 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2868 | /* flush write buffer */ |
| 2869 | if (write_other(self, NULL, 0) < 0) return NULL; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2870 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2871 | data=(Pdata*)self->file; |
| 2872 | l=data->length; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2873 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2874 | /* set up an array to hold get/put status */ |
| 2875 | lm = PyDict_Size(self->memo); |
| 2876 | if (lm < 0) return NULL; |
| 2877 | lm++; |
| 2878 | have_get = malloc(lm); |
| 2879 | if (have_get == NULL) return PyErr_NoMemory(); |
| 2880 | memset(have_get, 0, lm); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2881 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2882 | /* Scan for gets. */ |
| 2883 | for (rsize = 0, i = l; --i >= 0; ) { |
| 2884 | k = data->data[i]; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2885 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2886 | if (PyString_Check(k)) |
| 2887 | rsize += PyString_GET_SIZE(k); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2888 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2889 | else if (PyInt_Check(k)) { /* put */ |
| 2890 | ik = PyInt_AS_LONG((PyIntObject*)k); |
| 2891 | if (ik >= lm || ik == 0) { |
| 2892 | PyErr_SetString(PicklingError, |
| 2893 | "Invalid get data"); |
| 2894 | goto err; |
| 2895 | } |
| 2896 | if (have_get[ik]) /* with matching get */ |
| 2897 | rsize += ik < 256 ? 2 : 5; |
| 2898 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2899 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2900 | else if (! (PyTuple_Check(k) && |
| 2901 | PyTuple_GET_SIZE(k) == 2 && |
| 2902 | PyInt_Check((k = PyTuple_GET_ITEM(k, 0)))) |
| 2903 | ) { |
| 2904 | PyErr_SetString(PicklingError, |
| 2905 | "Unexpected data in internal list"); |
| 2906 | goto err; |
| 2907 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2908 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2909 | else { /* put */ |
| 2910 | ik = PyInt_AS_LONG((PyIntObject *)k); |
| 2911 | if (ik >= lm || ik == 0) { |
| 2912 | PyErr_SetString(PicklingError, |
| 2913 | "Invalid get data"); |
| 2914 | return NULL; |
| 2915 | } |
| 2916 | have_get[ik] = 1; |
| 2917 | rsize += ik < 256 ? 2 : 5; |
| 2918 | } |
| 2919 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2920 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2921 | /* Now generate the result */ |
| 2922 | r = PyString_FromStringAndSize(NULL, rsize); |
| 2923 | if (r == NULL) goto err; |
| 2924 | s = PyString_AS_STRING((PyStringObject *)r); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2925 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2926 | for (i = 0; i < l; i++) { |
| 2927 | k = data->data[i]; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2928 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2929 | if (PyString_Check(k)) { |
| 2930 | ssize = PyString_GET_SIZE(k); |
| 2931 | if (ssize) { |
| 2932 | p=PyString_AS_STRING((PyStringObject *)k); |
| 2933 | while (--ssize >= 0) |
| 2934 | *s++ = *p++; |
| 2935 | } |
| 2936 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2937 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2938 | else if (PyTuple_Check(k)) { /* get */ |
| 2939 | ik = PyInt_AS_LONG((PyIntObject *) |
| 2940 | PyTuple_GET_ITEM(k, 0)); |
| 2941 | if (ik < 256) { |
| 2942 | *s++ = BINGET; |
| 2943 | *s++ = (int)(ik & 0xff); |
| 2944 | } |
| 2945 | else { |
| 2946 | *s++ = LONG_BINGET; |
| 2947 | *s++ = (int)(ik & 0xff); |
| 2948 | *s++ = (int)((ik >> 8) & 0xff); |
| 2949 | *s++ = (int)((ik >> 16) & 0xff); |
| 2950 | *s++ = (int)((ik >> 24) & 0xff); |
| 2951 | } |
| 2952 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2953 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2954 | else { /* put */ |
| 2955 | ik = PyInt_AS_LONG((PyIntObject*)k); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2956 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2957 | if (have_get[ik]) { /* with matching get */ |
| 2958 | if (ik < 256) { |
| 2959 | *s++ = BINPUT; |
| 2960 | *s++ = (int)(ik & 0xff); |
| 2961 | } |
| 2962 | else { |
| 2963 | *s++ = LONG_BINPUT; |
| 2964 | *s++ = (int)(ik & 0xff); |
| 2965 | *s++ = (int)((ik >> 8) & 0xff); |
| 2966 | *s++ = (int)((ik >> 16) & 0xff); |
| 2967 | *s++ = (int)((ik >> 24) & 0xff); |
| 2968 | } |
| 2969 | } |
| 2970 | } |
| 2971 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2972 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2973 | if (clear) { |
| 2974 | PyDict_Clear(self->memo); |
| 2975 | Pdata_clear(data, 0); |
| 2976 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2977 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2978 | free(have_get); |
| 2979 | return r; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2980 | err: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2981 | free(have_get); |
| 2982 | return NULL; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 2983 | } |
| 2984 | |
| 2985 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2986 | Pickler_dump(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2987 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2988 | PyObject *ob; |
| 2989 | int get=0; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 2990 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2991 | if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get))) |
| 2992 | return NULL; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 2993 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2994 | if (dump(self, ob) < 0) |
| 2995 | return NULL; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 2996 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2997 | if (get) return Pickle_getvalue(self, NULL); |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 2998 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2999 | /* XXX Why does dump() return self? */ |
| 3000 | Py_INCREF(self); |
| 3001 | return (PyObject*)self; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3002 | } |
| 3003 | |
| 3004 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3005 | static struct PyMethodDef Pickler_methods[] = |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3006 | { |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 3007 | {"dump", (PyCFunction)Pickler_dump, METH_VARARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 3008 | PyDoc_STR("dump(object) -- " |
| 3009 | "Write an object in pickle format to the object's pickle stream")}, |
Fred Drake | 0ebacc8 | 2002-05-01 20:36:39 +0000 | [diff] [blame] | 3010 | {"clear_memo", (PyCFunction)Pickle_clear_memo, METH_NOARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 3011 | PyDoc_STR("clear_memo() -- Clear the picklers memo")}, |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 3012 | {"getvalue", (PyCFunction)Pickle_getvalue, METH_VARARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 3013 | PyDoc_STR("getvalue() -- Finish picking a list-based pickle")}, |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3014 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3015 | }; |
| 3016 | |
| 3017 | |
| 3018 | static Picklerobject * |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 3019 | newPicklerobject(PyObject *file, int proto) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3020 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3021 | Picklerobject *self; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3022 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3023 | if (proto < 0) |
| 3024 | proto = HIGHEST_PROTOCOL; |
| 3025 | if (proto > HIGHEST_PROTOCOL) { |
| 3026 | PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; " |
| 3027 | "the highest available protocol is %d", |
| 3028 | proto, HIGHEST_PROTOCOL); |
| 3029 | return NULL; |
| 3030 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3031 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3032 | self = PyObject_GC_New(Picklerobject, &Picklertype); |
| 3033 | if (self == NULL) |
| 3034 | return NULL; |
| 3035 | self->proto = proto; |
| 3036 | self->bin = proto > 0; |
| 3037 | self->fp = NULL; |
| 3038 | self->write = NULL; |
| 3039 | self->memo = NULL; |
| 3040 | self->arg = NULL; |
| 3041 | self->pers_func = NULL; |
| 3042 | self->inst_pers_func = NULL; |
| 3043 | self->write_buf = NULL; |
| 3044 | self->fast = 0; |
| 3045 | self->fast_container = 0; |
| 3046 | self->fast_memo = NULL; |
| 3047 | self->buf_size = 0; |
| 3048 | self->dispatch_table = NULL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3049 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3050 | self->file = NULL; |
| 3051 | if (file) |
| 3052 | Py_INCREF(file); |
| 3053 | else { |
| 3054 | file = Pdata_New(); |
| 3055 | if (file == NULL) |
| 3056 | goto err; |
| 3057 | } |
| 3058 | self->file = file; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 3059 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3060 | if (!( self->memo = PyDict_New())) |
| 3061 | goto err; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3062 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3063 | if (PyFile_Check(file)) { |
| 3064 | self->fp = PyFile_AsFile(file); |
| 3065 | if (self->fp == NULL) { |
| 3066 | PyErr_SetString(PyExc_ValueError, |
| 3067 | "I/O operation on closed file"); |
| 3068 | goto err; |
| 3069 | } |
| 3070 | self->write_func = write_file; |
| 3071 | } |
| 3072 | else if (PycStringIO_OutputCheck(file)) { |
| 3073 | self->write_func = write_cStringIO; |
| 3074 | } |
| 3075 | else if (file == Py_None) { |
| 3076 | self->write_func = write_none; |
| 3077 | } |
| 3078 | else { |
| 3079 | self->write_func = write_other; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3080 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3081 | if (! Pdata_Check(file)) { |
| 3082 | self->write = PyObject_GetAttr(file, write_str); |
| 3083 | if (!self->write) { |
| 3084 | PyErr_Clear(); |
| 3085 | PyErr_SetString(PyExc_TypeError, |
| 3086 | "argument must have 'write' " |
| 3087 | "attribute"); |
| 3088 | goto err; |
| 3089 | } |
| 3090 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3091 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3092 | self->write_buf = (char *)PyMem_Malloc(WRITE_BUF_SIZE); |
| 3093 | if (self->write_buf == NULL) { |
| 3094 | PyErr_NoMemory(); |
| 3095 | goto err; |
| 3096 | } |
| 3097 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3098 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3099 | if (PyEval_GetRestricted()) { |
| 3100 | /* Restricted execution, get private tables */ |
| 3101 | PyObject *m = PyImport_ImportModule("copy_reg"); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3102 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3103 | if (m == NULL) |
| 3104 | goto err; |
| 3105 | self->dispatch_table = PyObject_GetAttr(m, dispatch_table_str); |
| 3106 | Py_DECREF(m); |
| 3107 | if (self->dispatch_table == NULL) |
| 3108 | goto err; |
| 3109 | } |
| 3110 | else { |
| 3111 | self->dispatch_table = dispatch_table; |
| 3112 | Py_INCREF(dispatch_table); |
| 3113 | } |
| 3114 | PyObject_GC_Track(self); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3115 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3116 | return self; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3117 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3118 | err: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3119 | Py_DECREF(self); |
| 3120 | return NULL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3121 | } |
| 3122 | |
| 3123 | |
| 3124 | static PyObject * |
Martin v. Löwis | 544f119 | 2004-07-27 05:22:33 +0000 | [diff] [blame] | 3125 | get_Pickler(PyObject *self, PyObject *args, PyObject *kwds) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3126 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3127 | static char *kwlist[] = {"file", "protocol", NULL}; |
| 3128 | PyObject *file = NULL; |
| 3129 | int proto = 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3130 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3131 | /* XXX |
| 3132 | * The documented signature is Pickler(file, protocol=0), but this |
| 3133 | * accepts Pickler() and Pickler(integer) too. The meaning then |
| 3134 | * is clear as mud, undocumented, and not supported by pickle.py. |
| 3135 | * I'm told Zope uses this, but I haven't traced into this code |
| 3136 | * far enough to figure out what it means. |
| 3137 | */ |
| 3138 | if (!PyArg_ParseTuple(args, "|i:Pickler", &proto)) { |
| 3139 | PyErr_Clear(); |
| 3140 | proto = 0; |
| 3141 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|i:Pickler", |
| 3142 | kwlist, &file, &proto)) |
| 3143 | return NULL; |
| 3144 | } |
| 3145 | return (PyObject *)newPicklerobject(file, proto); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3146 | } |
| 3147 | |
| 3148 | |
| 3149 | static void |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3150 | Pickler_dealloc(Picklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3151 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3152 | PyObject_GC_UnTrack(self); |
| 3153 | Py_XDECREF(self->write); |
| 3154 | Py_XDECREF(self->memo); |
| 3155 | Py_XDECREF(self->fast_memo); |
| 3156 | Py_XDECREF(self->arg); |
| 3157 | Py_XDECREF(self->file); |
| 3158 | Py_XDECREF(self->pers_func); |
| 3159 | Py_XDECREF(self->inst_pers_func); |
| 3160 | Py_XDECREF(self->dispatch_table); |
| 3161 | PyMem_Free(self->write_buf); |
| 3162 | Py_TYPE(self)->tp_free((PyObject *)self); |
Jeremy Hylton | 4cf6319 | 2003-04-09 21:05:12 +0000 | [diff] [blame] | 3163 | } |
| 3164 | |
| 3165 | static int |
| 3166 | Pickler_traverse(Picklerobject *self, visitproc visit, void *arg) |
| 3167 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3168 | Py_VISIT(self->write); |
| 3169 | Py_VISIT(self->memo); |
| 3170 | Py_VISIT(self->fast_memo); |
| 3171 | Py_VISIT(self->arg); |
| 3172 | Py_VISIT(self->file); |
| 3173 | Py_VISIT(self->pers_func); |
| 3174 | Py_VISIT(self->inst_pers_func); |
| 3175 | Py_VISIT(self->dispatch_table); |
| 3176 | return 0; |
Jeremy Hylton | 4cf6319 | 2003-04-09 21:05:12 +0000 | [diff] [blame] | 3177 | } |
| 3178 | |
| 3179 | static int |
| 3180 | Pickler_clear(Picklerobject *self) |
| 3181 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3182 | Py_CLEAR(self->write); |
| 3183 | Py_CLEAR(self->memo); |
| 3184 | Py_CLEAR(self->fast_memo); |
| 3185 | Py_CLEAR(self->arg); |
| 3186 | Py_CLEAR(self->file); |
| 3187 | Py_CLEAR(self->pers_func); |
| 3188 | Py_CLEAR(self->inst_pers_func); |
| 3189 | Py_CLEAR(self->dispatch_table); |
| 3190 | return 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3191 | } |
| 3192 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3193 | static PyObject * |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3194 | Pickler_get_pers_func(Picklerobject *p) |
| 3195 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3196 | if (p->pers_func == NULL) |
| 3197 | PyErr_SetString(PyExc_AttributeError, "persistent_id"); |
| 3198 | else |
| 3199 | Py_INCREF(p->pers_func); |
| 3200 | return p->pers_func; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3201 | } |
| 3202 | |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3203 | static int |
| 3204 | Pickler_set_pers_func(Picklerobject *p, PyObject *v) |
| 3205 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3206 | if (v == NULL) { |
| 3207 | PyErr_SetString(PyExc_TypeError, |
| 3208 | "attribute deletion is not supported"); |
| 3209 | return -1; |
| 3210 | } |
| 3211 | Py_XDECREF(p->pers_func); |
| 3212 | Py_INCREF(v); |
| 3213 | p->pers_func = v; |
| 3214 | return 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3215 | } |
| 3216 | |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3217 | static int |
| 3218 | Pickler_set_inst_pers_func(Picklerobject *p, PyObject *v) |
| 3219 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3220 | if (v == NULL) { |
| 3221 | PyErr_SetString(PyExc_TypeError, |
| 3222 | "attribute deletion is not supported"); |
| 3223 | return -1; |
| 3224 | } |
| 3225 | Py_XDECREF(p->inst_pers_func); |
| 3226 | Py_INCREF(v); |
| 3227 | p->inst_pers_func = v; |
| 3228 | return 0; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3229 | } |
| 3230 | |
| 3231 | static PyObject * |
| 3232 | Pickler_get_memo(Picklerobject *p) |
| 3233 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3234 | if (p->memo == NULL) |
| 3235 | PyErr_SetString(PyExc_AttributeError, "memo"); |
| 3236 | else |
| 3237 | Py_INCREF(p->memo); |
| 3238 | return p->memo; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3239 | } |
| 3240 | |
| 3241 | static int |
| 3242 | Pickler_set_memo(Picklerobject *p, PyObject *v) |
| 3243 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3244 | if (v == NULL) { |
| 3245 | PyErr_SetString(PyExc_TypeError, |
| 3246 | "attribute deletion is not supported"); |
| 3247 | return -1; |
| 3248 | } |
| 3249 | if (!PyDict_Check(v)) { |
| 3250 | PyErr_SetString(PyExc_TypeError, "memo must be a dictionary"); |
| 3251 | return -1; |
| 3252 | } |
| 3253 | Py_XDECREF(p->memo); |
| 3254 | Py_INCREF(v); |
| 3255 | p->memo = v; |
| 3256 | return 0; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3257 | } |
| 3258 | |
| 3259 | static PyObject * |
| 3260 | Pickler_get_error(Picklerobject *p) |
| 3261 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3262 | /* why is this an attribute on the Pickler? */ |
| 3263 | Py_INCREF(PicklingError); |
| 3264 | return PicklingError; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3265 | } |
| 3266 | |
| 3267 | static PyMemberDef Pickler_members[] = { |
| 3268 | {"binary", T_INT, offsetof(Picklerobject, bin)}, |
| 3269 | {"fast", T_INT, offsetof(Picklerobject, fast)}, |
Jeremy Hylton | 3eb46f3 | 2001-10-16 17:10:49 +0000 | [diff] [blame] | 3270 | {NULL} |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3271 | }; |
| 3272 | |
| 3273 | static PyGetSetDef Pickler_getsets[] = { |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3274 | {"persistent_id", (getter)Pickler_get_pers_func, |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3275 | (setter)Pickler_set_pers_func}, |
| 3276 | {"inst_persistent_id", NULL, (setter)Pickler_set_inst_pers_func}, |
| 3277 | {"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo}, |
Jeremy Hylton | 3eb46f3 | 2001-10-16 17:10:49 +0000 | [diff] [blame] | 3278 | {"PicklingError", (getter)Pickler_get_error, NULL}, |
| 3279 | {NULL} |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3280 | }; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3281 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3282 | PyDoc_STRVAR(Picklertype__doc__, |
| 3283 | "Objects that know how to pickle objects\n"); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3284 | |
Guido van Rossum | 9716aaa | 1997-12-08 15:15:16 +0000 | [diff] [blame] | 3285 | static PyTypeObject Picklertype = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 3286 | PyVarObject_HEAD_INIT(NULL, 0) |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 3287 | "cPickle.Pickler", /*tp_name*/ |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3288 | sizeof(Picklerobject), /*tp_basicsize*/ |
| 3289 | 0, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3290 | (destructor)Pickler_dealloc, /* tp_dealloc */ |
| 3291 | 0, /* tp_print */ |
| 3292 | 0, /* tp_getattr */ |
| 3293 | 0, /* tp_setattr */ |
| 3294 | 0, /* tp_compare */ |
| 3295 | 0, /* tp_repr */ |
| 3296 | 0, /* tp_as_number */ |
| 3297 | 0, /* tp_as_sequence */ |
| 3298 | 0, /* tp_as_mapping */ |
| 3299 | 0, /* tp_hash */ |
| 3300 | 0, /* tp_call */ |
| 3301 | 0, /* tp_str */ |
| 3302 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3303 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 3304 | 0, /* tp_as_buffer */ |
Jeremy Hylton | 4cf6319 | 2003-04-09 21:05:12 +0000 | [diff] [blame] | 3305 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3306 | Picklertype__doc__, /* tp_doc */ |
| 3307 | (traverseproc)Pickler_traverse, /* tp_traverse */ |
| 3308 | (inquiry)Pickler_clear, /* tp_clear */ |
| 3309 | 0, /* tp_richcompare */ |
| 3310 | 0, /* tp_weaklistoffset */ |
| 3311 | 0, /* tp_iter */ |
| 3312 | 0, /* tp_iternext */ |
| 3313 | Pickler_methods, /* tp_methods */ |
| 3314 | Pickler_members, /* tp_members */ |
| 3315 | Pickler_getsets, /* tp_getset */ |
Guido van Rossum | 9716aaa | 1997-12-08 15:15:16 +0000 | [diff] [blame] | 3316 | }; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3317 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3318 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3319 | find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3320 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3321 | PyObject *global = 0, *module; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3322 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3323 | if (fc) { |
| 3324 | if (fc==Py_None) { |
| 3325 | PyErr_SetString(UnpicklingError, "Global and instance " |
| 3326 | "pickles are not supported."); |
| 3327 | return NULL; |
| 3328 | } |
| 3329 | return PyObject_CallFunctionObjArgs(fc, py_module_name, |
| 3330 | py_global_name, NULL); |
| 3331 | } |
Guido van Rossum | 1b9e0aa | 1999-04-19 17:58:18 +0000 | [diff] [blame] | 3332 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3333 | module = PySys_GetObject("modules"); |
| 3334 | if (module == NULL) |
| 3335 | return NULL; |
Jeremy Hylton | d105523 | 1998-08-11 19:52:51 +0000 | [diff] [blame] | 3336 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3337 | module = PyDict_GetItem(module, py_module_name); |
| 3338 | if (module == NULL) { |
| 3339 | module = PyImport_Import(py_module_name); |
| 3340 | if (!module) |
| 3341 | return NULL; |
| 3342 | global = PyObject_GetAttr(module, py_global_name); |
| 3343 | Py_DECREF(module); |
| 3344 | } |
| 3345 | else |
| 3346 | global = PyObject_GetAttr(module, py_global_name); |
| 3347 | return global; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3348 | } |
| 3349 | |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 3350 | static Py_ssize_t |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3351 | marker(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3352 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3353 | if (self->num_marks < 1) { |
| 3354 | PyErr_SetString(UnpicklingError, "could not find MARK"); |
| 3355 | return -1; |
| 3356 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3357 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3358 | return self->marks[--self->num_marks]; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3359 | } |
| 3360 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3361 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3362 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3363 | load_none(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3364 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3365 | PDATA_APPEND(self->stack, Py_None, -1); |
| 3366 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3367 | } |
| 3368 | |
Guido van Rossum | f9ffb03 | 1999-02-04 14:54:04 +0000 | [diff] [blame] | 3369 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3370 | bad_readline(void) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3371 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3372 | PyErr_SetString(UnpicklingError, "pickle data was truncated"); |
| 3373 | return -1; |
Guido van Rossum | f9ffb03 | 1999-02-04 14:54:04 +0000 | [diff] [blame] | 3374 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3375 | |
| 3376 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3377 | load_int(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3378 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3379 | PyObject *py_int = 0; |
| 3380 | char *endptr, *s; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 3381 | Py_ssize_t len; |
| 3382 | int res = -1; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3383 | long l; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3384 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3385 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
| 3386 | if (len < 2) return bad_readline(); |
| 3387 | if (!( s=pystrndup(s,len))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3388 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3389 | errno = 0; |
| 3390 | l = strtol(s, &endptr, 0); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3391 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3392 | if (errno || (*endptr != '\n') || (endptr[1] != '\0')) { |
| 3393 | /* Hm, maybe we've got something long. Let's try reading |
| 3394 | it as a Python long object. */ |
| 3395 | errno = 0; |
| 3396 | py_int = PyLong_FromString(s, NULL, 0); |
| 3397 | if (py_int == NULL) { |
| 3398 | PyErr_SetString(PyExc_ValueError, |
| 3399 | "could not convert string to int"); |
| 3400 | goto finally; |
| 3401 | } |
| 3402 | } |
| 3403 | else { |
| 3404 | if (len == 3 && (l == 0 || l == 1)) { |
| 3405 | if (!( py_int = PyBool_FromLong(l))) goto finally; |
| 3406 | } |
| 3407 | else { |
| 3408 | if (!( py_int = PyInt_FromLong(l))) goto finally; |
| 3409 | } |
| 3410 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3411 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3412 | free(s); |
| 3413 | PDATA_PUSH(self->stack, py_int, -1); |
| 3414 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3415 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3416 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3417 | free(s); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3418 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3419 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3420 | } |
| 3421 | |
Tim Peters | 3c67d79 | 2003-02-02 17:59:11 +0000 | [diff] [blame] | 3422 | static int |
| 3423 | load_bool(Unpicklerobject *self, PyObject *boolean) |
| 3424 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3425 | assert(boolean == Py_True || boolean == Py_False); |
| 3426 | PDATA_APPEND(self->stack, boolean, -1); |
| 3427 | return 0; |
Tim Peters | 3c67d79 | 2003-02-02 17:59:11 +0000 | [diff] [blame] | 3428 | } |
| 3429 | |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 3430 | /* s contains x bytes of a little-endian integer. Return its value as a |
| 3431 | * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian |
| 3432 | * int, but when x is 4 it's a signed one. This is an historical source |
| 3433 | * of x-platform bugs. |
| 3434 | */ |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3435 | static long |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 3436 | calc_binint(char *s, int x) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3437 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3438 | unsigned char c; |
| 3439 | int i; |
| 3440 | long l; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3441 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3442 | for (i = 0, l = 0L; i < x; i++) { |
| 3443 | c = (unsigned char)s[i]; |
| 3444 | l |= (long)c << (i * 8); |
| 3445 | } |
Tim Peters | bfa18f7 | 2001-04-10 01:54:42 +0000 | [diff] [blame] | 3446 | #if SIZEOF_LONG > 4 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3447 | /* Unlike BININT1 and BININT2, BININT (more accurately BININT4) |
| 3448 | * is signed, so on a box with longs bigger than 4 bytes we need |
| 3449 | * to extend a BININT's sign bit to the full width. |
| 3450 | */ |
| 3451 | if (x == 4 && l & (1L << 31)) |
| 3452 | l |= (~0L) << 32; |
Tim Peters | bfa18f7 | 2001-04-10 01:54:42 +0000 | [diff] [blame] | 3453 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3454 | return l; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3455 | } |
| 3456 | |
| 3457 | |
| 3458 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3459 | load_binintx(Unpicklerobject *self, char *s, int x) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3460 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3461 | PyObject *py_int = 0; |
| 3462 | long l; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3463 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3464 | l = calc_binint(s, x); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3465 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3466 | if (!( py_int = PyInt_FromLong(l))) |
| 3467 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3468 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3469 | PDATA_PUSH(self->stack, py_int, -1); |
| 3470 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3471 | } |
| 3472 | |
| 3473 | |
| 3474 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3475 | load_binint(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3476 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3477 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3478 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3479 | if (self->read_func(self, &s, 4) < 0) |
| 3480 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3481 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3482 | return load_binintx(self, s, 4); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3483 | } |
| 3484 | |
| 3485 | |
| 3486 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3487 | load_binint1(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3488 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3489 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3490 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3491 | if (self->read_func(self, &s, 1) < 0) |
| 3492 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3493 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3494 | return load_binintx(self, s, 1); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3495 | } |
| 3496 | |
| 3497 | |
| 3498 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3499 | load_binint2(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3500 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3501 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3502 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3503 | if (self->read_func(self, &s, 2) < 0) |
| 3504 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3505 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3506 | return load_binintx(self, s, 2); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3507 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3508 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3509 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3510 | load_long(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3511 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3512 | PyObject *l = 0; |
| 3513 | char *end, *s; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 3514 | Py_ssize_t len; |
| 3515 | int res = -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3516 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3517 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
| 3518 | if (len < 2) return bad_readline(); |
| 3519 | if (!( s=pystrndup(s,len))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3520 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3521 | if (!( l = PyLong_FromString(s, &end, 0))) |
| 3522 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3523 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3524 | free(s); |
| 3525 | PDATA_PUSH(self->stack, l, -1); |
| 3526 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3527 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3528 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3529 | free(s); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3530 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3531 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3532 | } |
| 3533 | |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 3534 | /* 'size' bytes contain the # of bytes of little-endian 256's-complement |
| 3535 | * data following. |
| 3536 | */ |
| 3537 | static int |
| 3538 | load_counted_long(Unpicklerobject *self, int size) |
| 3539 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3540 | Py_ssize_t i; |
| 3541 | char *nbytes; |
| 3542 | unsigned char *pdata; |
| 3543 | PyObject *along; |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 3544 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3545 | assert(size == 1 || size == 4); |
| 3546 | i = self->read_func(self, &nbytes, size); |
| 3547 | if (i < 0) return -1; |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 3548 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3549 | size = calc_binint(nbytes, size); |
| 3550 | if (size < 0) { |
| 3551 | /* Corrupt or hostile pickle -- we never write one like |
| 3552 | * this. |
| 3553 | */ |
| 3554 | PyErr_SetString(UnpicklingError, "LONG pickle has negative " |
| 3555 | "byte count"); |
| 3556 | return -1; |
| 3557 | } |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 3558 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3559 | if (size == 0) |
| 3560 | along = PyLong_FromLong(0L); |
| 3561 | else { |
| 3562 | /* Read the raw little-endian bytes & convert. */ |
| 3563 | i = self->read_func(self, (char **)&pdata, size); |
| 3564 | if (i < 0) return -1; |
| 3565 | along = _PyLong_FromByteArray(pdata, (size_t)size, |
| 3566 | 1 /* little endian */, 1 /* signed */); |
| 3567 | } |
| 3568 | if (along == NULL) |
| 3569 | return -1; |
| 3570 | PDATA_PUSH(self->stack, along, -1); |
| 3571 | return 0; |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 3572 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3573 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3574 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3575 | load_float(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3576 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3577 | PyObject *py_float = 0; |
| 3578 | char *endptr, *s; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 3579 | Py_ssize_t len; |
| 3580 | int res = -1; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3581 | double d; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3582 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3583 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
| 3584 | if (len < 2) return bad_readline(); |
| 3585 | if (!( s=pystrndup(s,len))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3586 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3587 | d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3588 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3589 | if (d == -1.0 && PyErr_Occurred()) { |
| 3590 | goto finally; |
| 3591 | } else if ((endptr[0] != '\n') || (endptr[1] != '\0')) { |
| 3592 | PyErr_SetString(PyExc_ValueError, |
| 3593 | "could not convert string to float"); |
| 3594 | goto finally; |
| 3595 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3596 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3597 | if (!( py_float = PyFloat_FromDouble(d))) |
| 3598 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3599 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3600 | free(s); |
| 3601 | PDATA_PUSH(self->stack, py_float, -1); |
| 3602 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3603 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3604 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3605 | free(s); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3606 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3607 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3608 | } |
| 3609 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3610 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3611 | load_binfloat(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3612 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3613 | PyObject *py_float; |
| 3614 | double x; |
| 3615 | char *p; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3616 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3617 | if (self->read_func(self, &p, 8) < 0) |
| 3618 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3619 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3620 | x = _PyFloat_Unpack8((unsigned char *)p, 0); |
| 3621 | if (x == -1.0 && PyErr_Occurred()) |
| 3622 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3623 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3624 | py_float = PyFloat_FromDouble(x); |
| 3625 | if (py_float == NULL) |
| 3626 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3627 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3628 | PDATA_PUSH(self->stack, py_float, -1); |
| 3629 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3630 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3631 | |
| 3632 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3633 | load_string(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3634 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3635 | PyObject *str = 0; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 3636 | Py_ssize_t len; |
| 3637 | int res = -1; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3638 | char *s, *p; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3639 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3640 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
| 3641 | if (len < 2) return bad_readline(); |
| 3642 | if (!( s=pystrndup(s,len))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3643 | |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 3644 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3645 | /* Strip outermost quotes */ |
Antoine Pitrou | be92971 | 2013-04-15 21:35:25 +0200 | [diff] [blame] | 3646 | while (len > 0 && s[len-1] <= ' ') |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3647 | len--; |
Antoine Pitrou | be92971 | 2013-04-15 21:35:25 +0200 | [diff] [blame] | 3648 | if (len > 1 && s[0]=='"' && s[len-1]=='"') { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3649 | s[len-1] = '\0'; |
| 3650 | p = s + 1 ; |
| 3651 | len -= 2; |
Antoine Pitrou | be92971 | 2013-04-15 21:35:25 +0200 | [diff] [blame] | 3652 | } |
| 3653 | else if (len > 1 && s[0]=='\'' && s[len-1]=='\'') { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3654 | s[len-1] = '\0'; |
| 3655 | p = s + 1 ; |
| 3656 | len -= 2; |
Antoine Pitrou | be92971 | 2013-04-15 21:35:25 +0200 | [diff] [blame] | 3657 | } |
| 3658 | else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3659 | goto insecure; |
| 3660 | /********************************************/ |
Guido van Rossum | 9716aaa | 1997-12-08 15:15:16 +0000 | [diff] [blame] | 3661 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3662 | str = PyString_DecodeEscape(p, len, NULL, 0, NULL); |
| 3663 | free(s); |
| 3664 | if (str) { |
| 3665 | PDATA_PUSH(self->stack, str, -1); |
| 3666 | res = 0; |
| 3667 | } |
| 3668 | return res; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3669 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3670 | insecure: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3671 | free(s); |
| 3672 | PyErr_SetString(PyExc_ValueError,"insecure string pickle"); |
| 3673 | return -1; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3674 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3675 | |
| 3676 | |
| 3677 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3678 | load_binstring(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3679 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3680 | PyObject *py_string = 0; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 3681 | Py_ssize_t l; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3682 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3683 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3684 | if (self->read_func(self, &s, 4) < 0) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3685 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3686 | l = calc_binint(s, 4); |
| 3687 | if (l < 0) { |
| 3688 | /* Corrupt or hostile pickle -- we never write one like |
| 3689 | * this. |
| 3690 | */ |
| 3691 | PyErr_SetString(UnpicklingError, |
| 3692 | "BINSTRING pickle has negative byte count"); |
| 3693 | return -1; |
| 3694 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3695 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3696 | if (self->read_func(self, &s, l) < 0) |
| 3697 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3698 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3699 | if (!( py_string = PyString_FromStringAndSize(s, l))) |
| 3700 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3701 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3702 | PDATA_PUSH(self->stack, py_string, -1); |
| 3703 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3704 | } |
| 3705 | |
| 3706 | |
| 3707 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3708 | load_short_binstring(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3709 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3710 | PyObject *py_string = 0; |
| 3711 | unsigned char l; |
| 3712 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3713 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3714 | if (self->read_func(self, &s, 1) < 0) |
| 3715 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3716 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3717 | l = (unsigned char)s[0]; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3718 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3719 | if (self->read_func(self, &s, l) < 0) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3720 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3721 | if (!( py_string = PyString_FromStringAndSize(s, l))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3722 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3723 | PDATA_PUSH(self->stack, py_string, -1); |
| 3724 | return 0; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3725 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3726 | |
| 3727 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3728 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3729 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3730 | load_unicode(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3731 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3732 | PyObject *str = 0; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 3733 | Py_ssize_t len; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3734 | char *s; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3735 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3736 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
| 3737 | if (len < 1) return bad_readline(); |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3738 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3739 | if (!( str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL))) |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 3740 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3741 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3742 | PDATA_PUSH(self->stack, str, -1); |
| 3743 | return 0; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3744 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3745 | #endif |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3746 | |
| 3747 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3748 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3749 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3750 | load_binunicode(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3751 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3752 | PyObject *unicode; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 3753 | Py_ssize_t l; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3754 | char *s; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3755 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3756 | if (self->read_func(self, &s, 4) < 0) return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3757 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3758 | l = calc_binint(s, 4); |
| 3759 | if (l < 0) { |
| 3760 | /* Corrupt or hostile pickle -- we never write one like |
| 3761 | * this. |
| 3762 | */ |
| 3763 | PyErr_SetString(UnpicklingError, |
| 3764 | "BINUNICODE pickle has negative byte count"); |
| 3765 | return -1; |
| 3766 | } |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3767 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3768 | if (self->read_func(self, &s, l) < 0) |
| 3769 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3770 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3771 | if (!( unicode = PyUnicode_DecodeUTF8(s, l, NULL))) |
| 3772 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3773 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3774 | PDATA_PUSH(self->stack, unicode, -1); |
| 3775 | return 0; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3776 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3777 | #endif |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3778 | |
| 3779 | |
| 3780 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3781 | load_tuple(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3782 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3783 | PyObject *tup; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 3784 | Py_ssize_t i; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3785 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3786 | if ((i = marker(self)) < 0) return -1; |
| 3787 | if (!( tup=Pdata_popTuple(self->stack, i))) return -1; |
| 3788 | PDATA_PUSH(self->stack, tup, -1); |
| 3789 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3790 | } |
| 3791 | |
| 3792 | static int |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 3793 | load_counted_tuple(Unpicklerobject *self, int len) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3794 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3795 | PyObject *tup = PyTuple_New(len); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3796 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3797 | if (tup == NULL) |
| 3798 | return -1; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 3799 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3800 | while (--len >= 0) { |
| 3801 | PyObject *element; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 3802 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3803 | PDATA_POP(self->stack, element); |
| 3804 | if (element == NULL) |
| 3805 | return -1; |
| 3806 | PyTuple_SET_ITEM(tup, len, element); |
| 3807 | } |
| 3808 | PDATA_PUSH(self->stack, tup, -1); |
| 3809 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3810 | } |
| 3811 | |
| 3812 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3813 | load_empty_list(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3814 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3815 | PyObject *list; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3816 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3817 | if (!( list=PyList_New(0))) return -1; |
| 3818 | PDATA_PUSH(self->stack, list, -1); |
| 3819 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3820 | } |
| 3821 | |
| 3822 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3823 | load_empty_dict(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3824 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3825 | PyObject *dict; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3826 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3827 | if (!( dict=PyDict_New())) return -1; |
| 3828 | PDATA_PUSH(self->stack, dict, -1); |
| 3829 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3830 | } |
| 3831 | |
| 3832 | |
| 3833 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3834 | load_list(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3835 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3836 | PyObject *list = 0; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 3837 | Py_ssize_t i; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3838 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3839 | if ((i = marker(self)) < 0) return -1; |
| 3840 | if (!( list=Pdata_popList(self->stack, i))) return -1; |
| 3841 | PDATA_PUSH(self->stack, list, -1); |
| 3842 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3843 | } |
| 3844 | |
| 3845 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3846 | load_dict(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3847 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3848 | PyObject *dict, *key, *value; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 3849 | Py_ssize_t i, j, k; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3850 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3851 | if ((i = marker(self)) < 0) return -1; |
| 3852 | j=self->stack->length; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3853 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3854 | if (!( dict = PyDict_New())) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3855 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3856 | for (k = i+1; k < j; k += 2) { |
| 3857 | key =self->stack->data[k-1]; |
| 3858 | value=self->stack->data[k ]; |
| 3859 | if (PyDict_SetItem(dict, key, value) < 0) { |
| 3860 | Py_DECREF(dict); |
| 3861 | return -1; |
| 3862 | } |
| 3863 | } |
| 3864 | Pdata_clear(self->stack, i); |
| 3865 | PDATA_PUSH(self->stack, dict, -1); |
| 3866 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3867 | } |
| 3868 | |
| 3869 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3870 | Instance_New(PyObject *cls, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3871 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3872 | PyObject *r = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3873 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3874 | if (PyClass_Check(cls)) { |
| 3875 | int l; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3876 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3877 | if ((l=PyObject_Size(args)) < 0) goto err; |
| 3878 | if (!( l )) { |
| 3879 | PyObject *__getinitargs__; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3880 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3881 | __getinitargs__ = PyObject_GetAttr(cls, |
| 3882 | __getinitargs___str); |
| 3883 | if (!__getinitargs__) { |
| 3884 | /* We have a class with no __getinitargs__, |
| 3885 | so bypass usual construction */ |
| 3886 | PyObject *inst; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3887 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3888 | PyErr_Clear(); |
| 3889 | if (!( inst=PyInstance_NewRaw(cls, NULL))) |
| 3890 | goto err; |
| 3891 | return inst; |
| 3892 | } |
| 3893 | Py_DECREF(__getinitargs__); |
| 3894 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3895 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3896 | if ((r=PyInstance_New(cls, args, NULL))) return r; |
| 3897 | else goto err; |
| 3898 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3899 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3900 | if ((r=PyObject_CallObject(cls, args))) return r; |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 3901 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3902 | err: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3903 | { |
| 3904 | PyObject *tp, *v, *tb, *tmp_value; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3905 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3906 | PyErr_Fetch(&tp, &v, &tb); |
| 3907 | tmp_value = v; |
| 3908 | /* NULL occurs when there was a KeyboardInterrupt */ |
| 3909 | if (tmp_value == NULL) |
| 3910 | tmp_value = Py_None; |
| 3911 | if ((r = PyTuple_Pack(3, tmp_value, cls, args))) { |
| 3912 | Py_XDECREF(v); |
| 3913 | v=r; |
| 3914 | } |
| 3915 | PyErr_Restore(tp,v,tb); |
| 3916 | } |
| 3917 | return NULL; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3918 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3919 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3920 | |
| 3921 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3922 | load_obj(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3923 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3924 | PyObject *class, *tup, *obj=0; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 3925 | Py_ssize_t i; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3926 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3927 | if ((i = marker(self)) < 0) return -1; |
| 3928 | if (!( tup=Pdata_popTuple(self->stack, i+1))) return -1; |
| 3929 | PDATA_POP(self->stack, class); |
| 3930 | if (class) { |
| 3931 | obj = Instance_New(class, tup); |
| 3932 | Py_DECREF(class); |
| 3933 | } |
| 3934 | Py_DECREF(tup); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3935 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3936 | if (! obj) return -1; |
| 3937 | PDATA_PUSH(self->stack, obj, -1); |
| 3938 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3939 | } |
| 3940 | |
| 3941 | |
| 3942 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3943 | load_inst(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3944 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3945 | PyObject *tup, *class=0, *obj=0, *module_name, *class_name; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 3946 | Py_ssize_t i, len; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3947 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3948 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3949 | if ((i = marker(self)) < 0) return -1; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3950 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3951 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
| 3952 | if (len < 2) return bad_readline(); |
| 3953 | module_name = PyString_FromStringAndSize(s, len - 1); |
| 3954 | if (!module_name) return -1; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3955 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3956 | if ((len = self->readline_func(self, &s)) >= 0) { |
| 3957 | if (len < 2) return bad_readline(); |
| 3958 | if ((class_name = PyString_FromStringAndSize(s, len - 1))) { |
| 3959 | class = find_class(module_name, class_name, |
| 3960 | self->find_class); |
| 3961 | Py_DECREF(class_name); |
| 3962 | } |
| 3963 | } |
| 3964 | Py_DECREF(module_name); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3965 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3966 | if (! class) return -1; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3967 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3968 | if ((tup=Pdata_popTuple(self->stack, i))) { |
| 3969 | obj = Instance_New(class, tup); |
| 3970 | Py_DECREF(tup); |
| 3971 | } |
| 3972 | Py_DECREF(class); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3973 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3974 | if (! obj) return -1; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3975 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3976 | PDATA_PUSH(self->stack, obj, -1); |
| 3977 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3978 | } |
| 3979 | |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 3980 | static int |
| 3981 | load_newobj(Unpicklerobject *self) |
| 3982 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3983 | PyObject *args = NULL; |
| 3984 | PyObject *clsraw = NULL; |
| 3985 | PyTypeObject *cls; /* clsraw cast to its true type */ |
| 3986 | PyObject *obj; |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 3987 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3988 | /* Stack is ... cls argtuple, and we want to call |
| 3989 | * cls.__new__(cls, *argtuple). |
| 3990 | */ |
| 3991 | PDATA_POP(self->stack, args); |
| 3992 | if (args == NULL) goto Fail; |
| 3993 | if (! PyTuple_Check(args)) { |
| 3994 | PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg " |
| 3995 | "tuple."); |
| 3996 | goto Fail; |
| 3997 | } |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 3998 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3999 | PDATA_POP(self->stack, clsraw); |
| 4000 | cls = (PyTypeObject *)clsraw; |
| 4001 | if (cls == NULL) goto Fail; |
| 4002 | if (! PyType_Check(cls)) { |
| 4003 | PyErr_SetString(UnpicklingError, "NEWOBJ class argument " |
| 4004 | "isn't a type object"); |
| 4005 | goto Fail; |
| 4006 | } |
| 4007 | if (cls->tp_new == NULL) { |
| 4008 | PyErr_SetString(UnpicklingError, "NEWOBJ class argument " |
| 4009 | "has NULL tp_new"); |
| 4010 | goto Fail; |
| 4011 | } |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 4012 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4013 | /* Call __new__. */ |
| 4014 | obj = cls->tp_new(cls, args, NULL); |
| 4015 | if (obj == NULL) goto Fail; |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 4016 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4017 | Py_DECREF(args); |
| 4018 | Py_DECREF(clsraw); |
| 4019 | PDATA_PUSH(self->stack, obj, -1); |
| 4020 | return 0; |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 4021 | |
| 4022 | Fail: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4023 | Py_XDECREF(args); |
| 4024 | Py_XDECREF(clsraw); |
| 4025 | return -1; |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 4026 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4027 | |
| 4028 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4029 | load_global(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4030 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4031 | PyObject *class = 0, *module_name = 0, *class_name = 0; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4032 | Py_ssize_t len; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4033 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4034 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4035 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
| 4036 | if (len < 2) return bad_readline(); |
| 4037 | module_name = PyString_FromStringAndSize(s, len - 1); |
| 4038 | if (!module_name) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4039 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4040 | if ((len = self->readline_func(self, &s)) >= 0) { |
| 4041 | if (len < 2) { |
| 4042 | Py_DECREF(module_name); |
| 4043 | return bad_readline(); |
| 4044 | } |
| 4045 | if ((class_name = PyString_FromStringAndSize(s, len - 1))) { |
| 4046 | class = find_class(module_name, class_name, |
| 4047 | self->find_class); |
| 4048 | Py_DECREF(class_name); |
| 4049 | } |
| 4050 | } |
| 4051 | Py_DECREF(module_name); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4052 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4053 | if (! class) return -1; |
| 4054 | PDATA_PUSH(self->stack, class, -1); |
| 4055 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4056 | } |
| 4057 | |
| 4058 | |
| 4059 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4060 | load_persid(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4061 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4062 | PyObject *pid = 0; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4063 | Py_ssize_t len; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4064 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4065 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4066 | if (self->pers_func) { |
| 4067 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
| 4068 | if (len < 2) return bad_readline(); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4069 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4070 | pid = PyString_FromStringAndSize(s, len - 1); |
| 4071 | if (!pid) return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4072 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4073 | if (PyList_Check(self->pers_func)) { |
| 4074 | if (PyList_Append(self->pers_func, pid) < 0) { |
| 4075 | Py_DECREF(pid); |
| 4076 | return -1; |
| 4077 | } |
| 4078 | } |
| 4079 | else { |
| 4080 | ARG_TUP(self, pid); |
| 4081 | if (self->arg) { |
| 4082 | pid = PyObject_Call(self->pers_func, self->arg, |
| 4083 | NULL); |
| 4084 | FREE_ARG_TUP(self); |
| 4085 | } |
| 4086 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4087 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4088 | if (! pid) return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4089 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4090 | PDATA_PUSH(self->stack, pid, -1); |
| 4091 | return 0; |
| 4092 | } |
| 4093 | else { |
| 4094 | PyErr_SetString(UnpicklingError, |
| 4095 | "A load persistent id instruction was encountered,\n" |
| 4096 | "but no persistent_load function was specified."); |
| 4097 | return -1; |
| 4098 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4099 | } |
| 4100 | |
| 4101 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4102 | load_binpersid(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4103 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4104 | PyObject *pid = 0; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4105 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4106 | if (self->pers_func) { |
| 4107 | PDATA_POP(self->stack, pid); |
| 4108 | if (! pid) return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4109 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4110 | if (PyList_Check(self->pers_func)) { |
| 4111 | if (PyList_Append(self->pers_func, pid) < 0) { |
| 4112 | Py_DECREF(pid); |
| 4113 | return -1; |
| 4114 | } |
| 4115 | } |
| 4116 | else { |
| 4117 | ARG_TUP(self, pid); |
| 4118 | if (self->arg) { |
| 4119 | pid = PyObject_Call(self->pers_func, self->arg, |
| 4120 | NULL); |
| 4121 | FREE_ARG_TUP(self); |
| 4122 | } |
| 4123 | if (! pid) return -1; |
| 4124 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4125 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4126 | PDATA_PUSH(self->stack, pid, -1); |
| 4127 | return 0; |
| 4128 | } |
| 4129 | else { |
| 4130 | PyErr_SetString(UnpicklingError, |
| 4131 | "A load persistent id instruction was encountered,\n" |
| 4132 | "but no persistent_load function was specified."); |
| 4133 | return -1; |
| 4134 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4135 | } |
| 4136 | |
| 4137 | |
| 4138 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4139 | load_pop(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4140 | { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4141 | Py_ssize_t len = self->stack->length; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4142 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4143 | /* Note that we split the (pickle.py) stack into two stacks, |
| 4144 | an object stack and a mark stack. We have to be clever and |
| 4145 | pop the right one. We do this by looking at the top of the |
| 4146 | mark stack first, and only signalling a stack underflow if |
| 4147 | the object stack is empty and the mark stack doesn't match |
| 4148 | our expectations. |
| 4149 | */ |
| 4150 | if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) { |
| 4151 | self->num_marks--; |
| 4152 | } else if (len > 0) { |
| 4153 | len--; |
| 4154 | Py_DECREF(self->stack->data[len]); |
| 4155 | self->stack->length = len; |
| 4156 | } else { |
| 4157 | return stackUnderflow(); |
| 4158 | } |
| 4159 | return 0; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4160 | } |
| 4161 | |
| 4162 | |
| 4163 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4164 | load_pop_mark(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4165 | { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4166 | Py_ssize_t i; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4167 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4168 | if ((i = marker(self)) < 0) |
| 4169 | return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4170 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4171 | Pdata_clear(self->stack, i); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4172 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4173 | return 0; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4174 | } |
| 4175 | |
| 4176 | |
| 4177 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4178 | load_dup(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4179 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4180 | PyObject *last; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4181 | Py_ssize_t len; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4182 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4183 | if ((len = self->stack->length) <= 0) return stackUnderflow(); |
| 4184 | last=self->stack->data[len-1]; |
| 4185 | Py_INCREF(last); |
| 4186 | PDATA_PUSH(self->stack, last, -1); |
| 4187 | return 0; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4188 | } |
| 4189 | |
| 4190 | |
| 4191 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4192 | load_get(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4193 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4194 | PyObject *py_str = 0, *value = 0; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4195 | Py_ssize_t len; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4196 | char *s; |
| 4197 | int rc; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4198 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4199 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
| 4200 | if (len < 2) return bad_readline(); |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4201 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4202 | if (!( py_str = PyString_FromStringAndSize(s, len - 1))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4203 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4204 | value = PyDict_GetItem(self->memo, py_str); |
| 4205 | if (! value) { |
| 4206 | PyErr_SetObject(BadPickleGet, py_str); |
| 4207 | rc = -1; |
| 4208 | } |
| 4209 | else { |
| 4210 | PDATA_APPEND(self->stack, value, -1); |
| 4211 | rc = 0; |
| 4212 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4213 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4214 | Py_DECREF(py_str); |
| 4215 | return rc; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4216 | } |
| 4217 | |
| 4218 | |
| 4219 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4220 | load_binget(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4221 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4222 | PyObject *py_key = 0, *value = 0; |
| 4223 | unsigned char key; |
| 4224 | char *s; |
| 4225 | int rc; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4226 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4227 | if (self->read_func(self, &s, 1) < 0) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4228 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4229 | key = (unsigned char)s[0]; |
| 4230 | if (!( py_key = PyInt_FromLong((long)key))) return -1; |
Guido van Rossum | ea2b715 | 2000-05-09 18:14:50 +0000 | [diff] [blame] | 4231 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4232 | value = PyDict_GetItem(self->memo, py_key); |
| 4233 | if (! value) { |
| 4234 | PyErr_SetObject(BadPickleGet, py_key); |
| 4235 | rc = -1; |
| 4236 | } |
| 4237 | else { |
| 4238 | PDATA_APPEND(self->stack, value, -1); |
| 4239 | rc = 0; |
| 4240 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4241 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4242 | Py_DECREF(py_key); |
| 4243 | return rc; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4244 | } |
| 4245 | |
| 4246 | |
| 4247 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4248 | load_long_binget(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4249 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4250 | PyObject *py_key = 0, *value = 0; |
| 4251 | unsigned char c; |
| 4252 | char *s; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4253 | Py_ssize_t key; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4254 | int rc; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4255 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4256 | if (self->read_func(self, &s, 4) < 0) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4257 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4258 | c = (unsigned char)s[0]; |
| 4259 | key = (long)c; |
| 4260 | c = (unsigned char)s[1]; |
| 4261 | key |= (long)c << 8; |
| 4262 | c = (unsigned char)s[2]; |
| 4263 | key |= (long)c << 16; |
| 4264 | c = (unsigned char)s[3]; |
| 4265 | key |= (long)c << 24; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4266 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4267 | if (!( py_key = PyInt_FromLong((long)key))) return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4268 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4269 | value = PyDict_GetItem(self->memo, py_key); |
| 4270 | if (! value) { |
| 4271 | PyErr_SetObject(BadPickleGet, py_key); |
| 4272 | rc = -1; |
| 4273 | } |
| 4274 | else { |
| 4275 | PDATA_APPEND(self->stack, value, -1); |
| 4276 | rc = 0; |
| 4277 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4278 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4279 | Py_DECREF(py_key); |
| 4280 | return rc; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4281 | } |
| 4282 | |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 4283 | /* Push an object from the extension registry (EXT[124]). nbytes is |
| 4284 | * the number of bytes following the opcode, holding the index (code) value. |
| 4285 | */ |
| 4286 | static int |
| 4287 | load_extension(Unpicklerobject *self, int nbytes) |
| 4288 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4289 | char *codebytes; /* the nbytes bytes after the opcode */ |
| 4290 | long code; /* calc_binint returns long */ |
| 4291 | PyObject *py_code; /* code as a Python int */ |
| 4292 | PyObject *obj; /* the object to push */ |
| 4293 | PyObject *pair; /* (module_name, class_name) */ |
| 4294 | PyObject *module_name, *class_name; |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 4295 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4296 | assert(nbytes == 1 || nbytes == 2 || nbytes == 4); |
| 4297 | if (self->read_func(self, &codebytes, nbytes) < 0) return -1; |
| 4298 | code = calc_binint(codebytes, nbytes); |
| 4299 | if (code <= 0) { /* note that 0 is forbidden */ |
| 4300 | /* Corrupt or hostile pickle. */ |
| 4301 | PyErr_SetString(UnpicklingError, "EXT specifies code <= 0"); |
| 4302 | return -1; |
| 4303 | } |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 4304 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4305 | /* Look for the code in the cache. */ |
| 4306 | py_code = PyInt_FromLong(code); |
| 4307 | if (py_code == NULL) return -1; |
| 4308 | obj = PyDict_GetItem(extension_cache, py_code); |
| 4309 | if (obj != NULL) { |
| 4310 | /* Bingo. */ |
| 4311 | Py_DECREF(py_code); |
| 4312 | PDATA_APPEND(self->stack, obj, -1); |
| 4313 | return 0; |
| 4314 | } |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 4315 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4316 | /* Look up the (module_name, class_name) pair. */ |
| 4317 | pair = PyDict_GetItem(inverted_registry, py_code); |
| 4318 | if (pair == NULL) { |
| 4319 | Py_DECREF(py_code); |
| 4320 | PyErr_Format(PyExc_ValueError, "unregistered extension " |
| 4321 | "code %ld", code); |
| 4322 | return -1; |
| 4323 | } |
| 4324 | /* Since the extension registry is manipulable via Python code, |
| 4325 | * confirm that pair is really a 2-tuple of strings. |
| 4326 | */ |
| 4327 | if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 || |
| 4328 | !PyString_Check(module_name = PyTuple_GET_ITEM(pair, 0)) || |
| 4329 | !PyString_Check(class_name = PyTuple_GET_ITEM(pair, 1))) { |
| 4330 | Py_DECREF(py_code); |
| 4331 | PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] " |
| 4332 | "isn't a 2-tuple of strings", code); |
| 4333 | return -1; |
| 4334 | } |
| 4335 | /* Load the object. */ |
| 4336 | obj = find_class(module_name, class_name, self->find_class); |
| 4337 | if (obj == NULL) { |
| 4338 | Py_DECREF(py_code); |
| 4339 | return -1; |
| 4340 | } |
| 4341 | /* Cache code -> obj. */ |
| 4342 | code = PyDict_SetItem(extension_cache, py_code, obj); |
| 4343 | Py_DECREF(py_code); |
| 4344 | if (code < 0) { |
| 4345 | Py_DECREF(obj); |
| 4346 | return -1; |
| 4347 | } |
| 4348 | PDATA_PUSH(self->stack, obj, -1); |
| 4349 | return 0; |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 4350 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4351 | |
| 4352 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4353 | load_put(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4354 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4355 | PyObject *py_str = 0, *value = 0; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4356 | Py_ssize_t len, l; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4357 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4358 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4359 | if ((l = self->readline_func(self, &s)) < 0) return -1; |
| 4360 | if (l < 2) return bad_readline(); |
| 4361 | if (!( len=self->stack->length )) return stackUnderflow(); |
| 4362 | if (!( py_str = PyString_FromStringAndSize(s, l - 1))) return -1; |
| 4363 | value=self->stack->data[len-1]; |
| 4364 | l=PyDict_SetItem(self->memo, py_str, value); |
| 4365 | Py_DECREF(py_str); |
| 4366 | return l; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4367 | } |
| 4368 | |
| 4369 | |
| 4370 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4371 | load_binput(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4372 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4373 | PyObject *py_key = 0, *value = 0; |
| 4374 | unsigned char key; |
| 4375 | char *s; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4376 | Py_ssize_t len; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4377 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4378 | if (self->read_func(self, &s, 1) < 0) return -1; |
| 4379 | if (!( (len=self->stack->length) > 0 )) return stackUnderflow(); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4380 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4381 | key = (unsigned char)s[0]; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 4382 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4383 | if (!( py_key = PyInt_FromLong((long)key))) return -1; |
| 4384 | value=self->stack->data[len-1]; |
| 4385 | len=PyDict_SetItem(self->memo, py_key, value); |
| 4386 | Py_DECREF(py_key); |
| 4387 | return len; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4388 | } |
| 4389 | |
| 4390 | |
| 4391 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4392 | load_long_binput(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4393 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4394 | PyObject *py_key = 0, *value = 0; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4395 | Py_ssize_t key; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4396 | unsigned char c; |
| 4397 | char *s; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4398 | Py_ssize_t len; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4399 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4400 | if (self->read_func(self, &s, 4) < 0) return -1; |
| 4401 | if (!( len=self->stack->length )) return stackUnderflow(); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4402 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4403 | c = (unsigned char)s[0]; |
| 4404 | key = (long)c; |
| 4405 | c = (unsigned char)s[1]; |
| 4406 | key |= (long)c << 8; |
| 4407 | c = (unsigned char)s[2]; |
| 4408 | key |= (long)c << 16; |
| 4409 | c = (unsigned char)s[3]; |
| 4410 | key |= (long)c << 24; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4411 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4412 | if (!( py_key = PyInt_FromLong(key))) return -1; |
| 4413 | value=self->stack->data[len-1]; |
| 4414 | len=PyDict_SetItem(self->memo, py_key, value); |
| 4415 | Py_DECREF(py_key); |
| 4416 | return len; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4417 | } |
| 4418 | |
| 4419 | |
| 4420 | static int |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4421 | do_append(Unpicklerobject *self, Py_ssize_t x) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4422 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4423 | PyObject *value = 0, *list = 0, *append_method = 0; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4424 | Py_ssize_t len, i; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4425 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4426 | len=self->stack->length; |
| 4427 | if (!( len >= x && x > 0 )) return stackUnderflow(); |
| 4428 | /* nothing to do */ |
| 4429 | if (len==x) return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4430 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4431 | list=self->stack->data[x-1]; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4432 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4433 | if (PyList_Check(list)) { |
| 4434 | PyObject *slice; |
| 4435 | int list_len; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4436 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4437 | slice=Pdata_popList(self->stack, x); |
| 4438 | if (! slice) return -1; |
| 4439 | list_len = PyList_GET_SIZE(list); |
| 4440 | i=PyList_SetSlice(list, list_len, list_len, slice); |
| 4441 | Py_DECREF(slice); |
| 4442 | return i; |
| 4443 | } |
| 4444 | else { |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4445 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4446 | if (!( append_method = PyObject_GetAttr(list, append_str))) |
| 4447 | return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4448 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4449 | for (i = x; i < len; i++) { |
| 4450 | PyObject *junk; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4451 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4452 | value=self->stack->data[i]; |
| 4453 | junk=0; |
| 4454 | ARG_TUP(self, value); |
| 4455 | if (self->arg) { |
| 4456 | junk = PyObject_Call(append_method, self->arg, |
| 4457 | NULL); |
| 4458 | FREE_ARG_TUP(self); |
| 4459 | } |
| 4460 | if (! junk) { |
| 4461 | Pdata_clear(self->stack, i+1); |
| 4462 | self->stack->length=x; |
| 4463 | Py_DECREF(append_method); |
| 4464 | return -1; |
| 4465 | } |
| 4466 | Py_DECREF(junk); |
| 4467 | } |
| 4468 | self->stack->length=x; |
| 4469 | Py_DECREF(append_method); |
| 4470 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4471 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4472 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4473 | } |
| 4474 | |
| 4475 | |
| 4476 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4477 | load_append(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4478 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4479 | return do_append(self, self->stack->length - 1); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4480 | } |
| 4481 | |
| 4482 | |
| 4483 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4484 | load_appends(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4485 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4486 | return do_append(self, marker(self)); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4487 | } |
| 4488 | |
| 4489 | |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4490 | static Py_ssize_t |
| 4491 | do_setitems(Unpicklerobject *self, Py_ssize_t x) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4492 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4493 | PyObject *value = 0, *key = 0, *dict = 0; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4494 | Py_ssize_t len, i, r=0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4495 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4496 | if (!( (len=self->stack->length) >= x |
| 4497 | && x > 0 )) return stackUnderflow(); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4498 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4499 | dict=self->stack->data[x-1]; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4500 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4501 | for (i = x+1; i < len; i += 2) { |
| 4502 | key =self->stack->data[i-1]; |
| 4503 | value=self->stack->data[i ]; |
| 4504 | if (PyObject_SetItem(dict, key, value) < 0) { |
| 4505 | r=-1; |
| 4506 | break; |
| 4507 | } |
| 4508 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4509 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4510 | Pdata_clear(self->stack, x); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4511 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4512 | return r; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4513 | } |
| 4514 | |
| 4515 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4516 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4517 | load_setitem(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4518 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4519 | return do_setitems(self, self->stack->length - 2); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4520 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4521 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4522 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4523 | load_setitems(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4524 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4525 | return do_setitems(self, marker(self)); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4526 | } |
| 4527 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4528 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4529 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4530 | load_build(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4531 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4532 | PyObject *state, *inst, *slotstate; |
| 4533 | PyObject *__setstate__; |
| 4534 | PyObject *d_key, *d_value; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4535 | int res = -1; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4536 | Py_ssize_t i; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4537 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4538 | /* Stack is ... instance, state. We want to leave instance at |
| 4539 | * the stack top, possibly mutated via instance.__setstate__(state). |
| 4540 | */ |
| 4541 | if (self->stack->length < 2) |
| 4542 | return stackUnderflow(); |
| 4543 | PDATA_POP(self->stack, state); |
| 4544 | if (state == NULL) |
| 4545 | return -1; |
| 4546 | inst = self->stack->data[self->stack->length - 1]; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4547 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4548 | __setstate__ = PyObject_GetAttr(inst, __setstate___str); |
| 4549 | if (__setstate__ != NULL) { |
| 4550 | PyObject *junk = NULL; |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 4551 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4552 | /* The explicit __setstate__ is responsible for everything. */ |
| 4553 | ARG_TUP(self, state); |
| 4554 | if (self->arg) { |
| 4555 | junk = PyObject_Call(__setstate__, self->arg, NULL); |
| 4556 | FREE_ARG_TUP(self); |
| 4557 | } |
| 4558 | Py_DECREF(__setstate__); |
| 4559 | if (junk == NULL) |
| 4560 | return -1; |
| 4561 | Py_DECREF(junk); |
| 4562 | return 0; |
| 4563 | } |
| 4564 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 4565 | return -1; |
| 4566 | PyErr_Clear(); |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 4567 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4568 | /* A default __setstate__. First see whether state embeds a |
| 4569 | * slot state dict too (a proto 2 addition). |
| 4570 | */ |
| 4571 | if (PyTuple_Check(state) && PyTuple_Size(state) == 2) { |
| 4572 | PyObject *temp = state; |
| 4573 | state = PyTuple_GET_ITEM(temp, 0); |
| 4574 | slotstate = PyTuple_GET_ITEM(temp, 1); |
| 4575 | Py_INCREF(state); |
| 4576 | Py_INCREF(slotstate); |
| 4577 | Py_DECREF(temp); |
| 4578 | } |
| 4579 | else |
| 4580 | slotstate = NULL; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4581 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4582 | /* Set inst.__dict__ from the state dict (if any). */ |
| 4583 | if (state != Py_None) { |
| 4584 | PyObject *dict; |
| 4585 | if (! PyDict_Check(state)) { |
| 4586 | PyErr_SetString(UnpicklingError, "state is not a " |
| 4587 | "dictionary"); |
| 4588 | goto finally; |
| 4589 | } |
| 4590 | dict = PyObject_GetAttr(inst, __dict___str); |
| 4591 | if (dict == NULL) |
| 4592 | goto finally; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4593 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4594 | i = 0; |
| 4595 | while (PyDict_Next(state, &i, &d_key, &d_value)) { |
| 4596 | /* normally the keys for instance attributes are |
| 4597 | interned. we should try to do that here. */ |
| 4598 | Py_INCREF(d_key); |
| 4599 | if (PyString_CheckExact(d_key)) |
| 4600 | PyString_InternInPlace(&d_key); |
| 4601 | if (PyObject_SetItem(dict, d_key, d_value) < 0) { |
| 4602 | Py_DECREF(d_key); |
| 4603 | goto finally; |
| 4604 | } |
| 4605 | Py_DECREF(d_key); |
| 4606 | } |
| 4607 | Py_DECREF(dict); |
| 4608 | } |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 4609 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4610 | /* Also set instance attributes from the slotstate dict (if any). */ |
| 4611 | if (slotstate != NULL) { |
| 4612 | if (! PyDict_Check(slotstate)) { |
| 4613 | PyErr_SetString(UnpicklingError, "slot state is not " |
| 4614 | "a dictionary"); |
| 4615 | goto finally; |
| 4616 | } |
| 4617 | i = 0; |
| 4618 | while (PyDict_Next(slotstate, &i, &d_key, &d_value)) { |
| 4619 | if (PyObject_SetAttr(inst, d_key, d_value) < 0) |
| 4620 | goto finally; |
| 4621 | } |
| 4622 | } |
| 4623 | res = 0; |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 4624 | |
| 4625 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4626 | Py_DECREF(state); |
| 4627 | Py_XDECREF(slotstate); |
| 4628 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4629 | } |
| 4630 | |
| 4631 | |
| 4632 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4633 | load_mark(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4634 | { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4635 | Py_ssize_t s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4636 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4637 | /* Note that we split the (pickle.py) stack into two stacks, an |
| 4638 | object stack and a mark stack. Here we push a mark onto the |
| 4639 | mark stack. |
| 4640 | */ |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4641 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4642 | if ((self->num_marks + 1) >= self->marks_size) { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4643 | Py_ssize_t *marks; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4644 | s=self->marks_size+20; |
| 4645 | if (s <= self->num_marks) s=self->num_marks + 1; |
| 4646 | if (self->marks == NULL) |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4647 | marks=(Py_ssize_t *)malloc(s * sizeof(Py_ssize_t)); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4648 | else |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4649 | marks=(Py_ssize_t *)realloc(self->marks, |
| 4650 | s * sizeof(Py_ssize_t)); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4651 | if (!marks) { |
| 4652 | PyErr_NoMemory(); |
| 4653 | return -1; |
| 4654 | } |
| 4655 | self->marks = marks; |
| 4656 | self->marks_size = s; |
| 4657 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4658 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4659 | self->marks[self->num_marks++] = self->stack->length; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4660 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4661 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4662 | } |
| 4663 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4664 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4665 | load_reduce(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4666 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4667 | PyObject *callable = 0, *arg_tup = 0, *ob = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4668 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4669 | PDATA_POP(self->stack, arg_tup); |
| 4670 | if (! arg_tup) return -1; |
| 4671 | PDATA_POP(self->stack, callable); |
| 4672 | if (callable) { |
| 4673 | ob = Instance_New(callable, arg_tup); |
| 4674 | Py_DECREF(callable); |
| 4675 | } |
| 4676 | Py_DECREF(arg_tup); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4677 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4678 | if (! ob) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4679 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4680 | PDATA_PUSH(self->stack, ob, -1); |
| 4681 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4682 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4683 | |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 4684 | /* Just raises an error if we don't know the protocol specified. PROTO |
| 4685 | * is the first opcode for protocols >= 2. |
| 4686 | */ |
| 4687 | static int |
| 4688 | load_proto(Unpicklerobject *self) |
| 4689 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4690 | int i; |
| 4691 | char *protobyte; |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 4692 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4693 | i = self->read_func(self, &protobyte, 1); |
| 4694 | if (i < 0) |
| 4695 | return -1; |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 4696 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4697 | i = calc_binint(protobyte, 1); |
| 4698 | /* No point checking for < 0, since calc_binint returns an unsigned |
| 4699 | * int when chewing on 1 byte. |
| 4700 | */ |
| 4701 | assert(i >= 0); |
| 4702 | if (i <= HIGHEST_PROTOCOL) |
| 4703 | return 0; |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 4704 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4705 | PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i); |
| 4706 | return -1; |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 4707 | } |
| 4708 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4709 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4710 | load(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4711 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4712 | PyObject *err = 0, *val = 0; |
| 4713 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4714 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4715 | self->num_marks = 0; |
| 4716 | if (self->stack->length) Pdata_clear(self->stack, 0); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4717 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4718 | while (1) { |
| 4719 | if (self->read_func(self, &s, 1) < 0) |
| 4720 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4721 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4722 | switch (s[0]) { |
| 4723 | case NONE: |
| 4724 | if (load_none(self) < 0) |
| 4725 | break; |
| 4726 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4727 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4728 | case BININT: |
| 4729 | if (load_binint(self) < 0) |
| 4730 | break; |
| 4731 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4732 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4733 | case BININT1: |
| 4734 | if (load_binint1(self) < 0) |
| 4735 | break; |
| 4736 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4737 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4738 | case BININT2: |
| 4739 | if (load_binint2(self) < 0) |
| 4740 | break; |
| 4741 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4742 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4743 | case INT: |
| 4744 | if (load_int(self) < 0) |
| 4745 | break; |
| 4746 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4747 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4748 | case LONG: |
| 4749 | if (load_long(self) < 0) |
| 4750 | break; |
| 4751 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4752 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4753 | case LONG1: |
| 4754 | if (load_counted_long(self, 1) < 0) |
| 4755 | break; |
| 4756 | continue; |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 4757 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4758 | case LONG4: |
| 4759 | if (load_counted_long(self, 4) < 0) |
| 4760 | break; |
| 4761 | continue; |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 4762 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4763 | case FLOAT: |
| 4764 | if (load_float(self) < 0) |
| 4765 | break; |
| 4766 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4767 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4768 | case BINFLOAT: |
| 4769 | if (load_binfloat(self) < 0) |
| 4770 | break; |
| 4771 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4772 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4773 | case BINSTRING: |
| 4774 | if (load_binstring(self) < 0) |
| 4775 | break; |
| 4776 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4777 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4778 | case SHORT_BINSTRING: |
| 4779 | if (load_short_binstring(self) < 0) |
| 4780 | break; |
| 4781 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4782 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4783 | case STRING: |
| 4784 | if (load_string(self) < 0) |
| 4785 | break; |
| 4786 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4787 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4788 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4789 | case UNICODE: |
| 4790 | if (load_unicode(self) < 0) |
| 4791 | break; |
| 4792 | continue; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 4793 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4794 | case BINUNICODE: |
| 4795 | if (load_binunicode(self) < 0) |
| 4796 | break; |
| 4797 | continue; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4798 | #endif |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 4799 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4800 | case EMPTY_TUPLE: |
| 4801 | if (load_counted_tuple(self, 0) < 0) |
| 4802 | break; |
| 4803 | continue; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 4804 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4805 | case TUPLE1: |
| 4806 | if (load_counted_tuple(self, 1) < 0) |
| 4807 | break; |
| 4808 | continue; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 4809 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4810 | case TUPLE2: |
| 4811 | if (load_counted_tuple(self, 2) < 0) |
| 4812 | break; |
| 4813 | continue; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 4814 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4815 | case TUPLE3: |
| 4816 | if (load_counted_tuple(self, 3) < 0) |
| 4817 | break; |
| 4818 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4819 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4820 | case TUPLE: |
| 4821 | if (load_tuple(self) < 0) |
| 4822 | break; |
| 4823 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4824 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4825 | case EMPTY_LIST: |
| 4826 | if (load_empty_list(self) < 0) |
| 4827 | break; |
| 4828 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4829 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4830 | case LIST: |
| 4831 | if (load_list(self) < 0) |
| 4832 | break; |
| 4833 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4834 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4835 | case EMPTY_DICT: |
| 4836 | if (load_empty_dict(self) < 0) |
| 4837 | break; |
| 4838 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4839 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4840 | case DICT: |
| 4841 | if (load_dict(self) < 0) |
| 4842 | break; |
| 4843 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4844 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4845 | case OBJ: |
| 4846 | if (load_obj(self) < 0) |
| 4847 | break; |
| 4848 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4849 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4850 | case INST: |
| 4851 | if (load_inst(self) < 0) |
| 4852 | break; |
| 4853 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4854 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4855 | case NEWOBJ: |
| 4856 | if (load_newobj(self) < 0) |
| 4857 | break; |
| 4858 | continue; |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 4859 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4860 | case GLOBAL: |
| 4861 | if (load_global(self) < 0) |
| 4862 | break; |
| 4863 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4864 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4865 | case APPEND: |
| 4866 | if (load_append(self) < 0) |
| 4867 | break; |
| 4868 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4869 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4870 | case APPENDS: |
| 4871 | if (load_appends(self) < 0) |
| 4872 | break; |
| 4873 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4874 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4875 | case BUILD: |
| 4876 | if (load_build(self) < 0) |
| 4877 | break; |
| 4878 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4879 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4880 | case DUP: |
| 4881 | if (load_dup(self) < 0) |
| 4882 | break; |
| 4883 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4884 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4885 | case BINGET: |
| 4886 | if (load_binget(self) < 0) |
| 4887 | break; |
| 4888 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4889 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4890 | case LONG_BINGET: |
| 4891 | if (load_long_binget(self) < 0) |
| 4892 | break; |
| 4893 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4894 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4895 | case GET: |
| 4896 | if (load_get(self) < 0) |
| 4897 | break; |
| 4898 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4899 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4900 | case EXT1: |
| 4901 | if (load_extension(self, 1) < 0) |
| 4902 | break; |
| 4903 | continue; |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 4904 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4905 | case EXT2: |
| 4906 | if (load_extension(self, 2) < 0) |
| 4907 | break; |
| 4908 | continue; |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 4909 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4910 | case EXT4: |
| 4911 | if (load_extension(self, 4) < 0) |
| 4912 | break; |
| 4913 | continue; |
| 4914 | case MARK: |
| 4915 | if (load_mark(self) < 0) |
| 4916 | break; |
| 4917 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4918 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4919 | case BINPUT: |
| 4920 | if (load_binput(self) < 0) |
| 4921 | break; |
| 4922 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4923 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4924 | case LONG_BINPUT: |
| 4925 | if (load_long_binput(self) < 0) |
| 4926 | break; |
| 4927 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4928 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4929 | case PUT: |
| 4930 | if (load_put(self) < 0) |
| 4931 | break; |
| 4932 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4933 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4934 | case POP: |
| 4935 | if (load_pop(self) < 0) |
| 4936 | break; |
| 4937 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4938 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4939 | case POP_MARK: |
| 4940 | if (load_pop_mark(self) < 0) |
| 4941 | break; |
| 4942 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4943 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4944 | case SETITEM: |
| 4945 | if (load_setitem(self) < 0) |
| 4946 | break; |
| 4947 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4948 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4949 | case SETITEMS: |
| 4950 | if (load_setitems(self) < 0) |
| 4951 | break; |
| 4952 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4953 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4954 | case STOP: |
| 4955 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4956 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4957 | case PERSID: |
| 4958 | if (load_persid(self) < 0) |
| 4959 | break; |
| 4960 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4961 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4962 | case BINPERSID: |
| 4963 | if (load_binpersid(self) < 0) |
| 4964 | break; |
| 4965 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4966 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4967 | case REDUCE: |
| 4968 | if (load_reduce(self) < 0) |
| 4969 | break; |
| 4970 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4971 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4972 | case PROTO: |
| 4973 | if (load_proto(self) < 0) |
| 4974 | break; |
| 4975 | continue; |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 4976 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4977 | case NEWTRUE: |
| 4978 | if (load_bool(self, Py_True) < 0) |
| 4979 | break; |
| 4980 | continue; |
Tim Peters | 3c67d79 | 2003-02-02 17:59:11 +0000 | [diff] [blame] | 4981 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4982 | case NEWFALSE: |
| 4983 | if (load_bool(self, Py_False) < 0) |
| 4984 | break; |
| 4985 | continue; |
Tim Peters | 3c67d79 | 2003-02-02 17:59:11 +0000 | [diff] [blame] | 4986 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4987 | case '\0': |
| 4988 | /* end of file */ |
| 4989 | PyErr_SetNone(PyExc_EOFError); |
| 4990 | break; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4991 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4992 | default: |
| 4993 | cPickle_ErrFormat(UnpicklingError, |
| 4994 | "invalid load key, '%s'.", |
| 4995 | "c", s[0]); |
| 4996 | return NULL; |
| 4997 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4998 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4999 | break; |
| 5000 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5001 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5002 | if ((err = PyErr_Occurred())) { |
| 5003 | if (err == PyExc_EOFError) { |
| 5004 | PyErr_SetNone(PyExc_EOFError); |
| 5005 | } |
| 5006 | return NULL; |
| 5007 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5008 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5009 | PDATA_POP(self->stack, val); |
| 5010 | return val; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5011 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5012 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5013 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5014 | /* No-load functions to support noload, which is used to |
| 5015 | find persistent references. */ |
| 5016 | |
| 5017 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5018 | noload_obj(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5019 | { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 5020 | Py_ssize_t i; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5021 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5022 | if ((i = marker(self)) < 0) return -1; |
| 5023 | return Pdata_clear(self->stack, i+1); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5024 | } |
| 5025 | |
| 5026 | |
| 5027 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5028 | noload_inst(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5029 | { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 5030 | Py_ssize_t i; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5031 | char *s; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5032 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5033 | if ((i = marker(self)) < 0) return -1; |
| 5034 | Pdata_clear(self->stack, i); |
| 5035 | if (self->readline_func(self, &s) < 0) return -1; |
| 5036 | if (self->readline_func(self, &s) < 0) return -1; |
| 5037 | PDATA_APPEND(self->stack, Py_None, -1); |
| 5038 | return 0; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5039 | } |
| 5040 | |
| 5041 | static int |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 5042 | noload_newobj(Unpicklerobject *self) |
| 5043 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5044 | PyObject *obj; |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 5045 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5046 | PDATA_POP(self->stack, obj); /* pop argtuple */ |
| 5047 | if (obj == NULL) return -1; |
| 5048 | Py_DECREF(obj); |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 5049 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5050 | PDATA_POP(self->stack, obj); /* pop cls */ |
| 5051 | if (obj == NULL) return -1; |
| 5052 | Py_DECREF(obj); |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 5053 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5054 | PDATA_APPEND(self->stack, Py_None, -1); |
| 5055 | return 0; |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 5056 | } |
| 5057 | |
| 5058 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5059 | noload_global(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5060 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5061 | char *s; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5062 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5063 | if (self->readline_func(self, &s) < 0) return -1; |
| 5064 | if (self->readline_func(self, &s) < 0) return -1; |
| 5065 | PDATA_APPEND(self->stack, Py_None,-1); |
| 5066 | return 0; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5067 | } |
| 5068 | |
| 5069 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5070 | noload_reduce(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5071 | { |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5072 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5073 | if (self->stack->length < 2) return stackUnderflow(); |
| 5074 | Pdata_clear(self->stack, self->stack->length-2); |
| 5075 | PDATA_APPEND(self->stack, Py_None,-1); |
| 5076 | return 0; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5077 | } |
| 5078 | |
| 5079 | static int |
| 5080 | noload_build(Unpicklerobject *self) { |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5081 | |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 5082 | if (self->stack->length < 1) return stackUnderflow(); |
| 5083 | Pdata_clear(self->stack, self->stack->length-1); |
Guido van Rossum | 50f385c | 1998-12-04 18:48:44 +0000 | [diff] [blame] | 5084 | return 0; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5085 | } |
| 5086 | |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 5087 | static int |
| 5088 | noload_extension(Unpicklerobject *self, int nbytes) |
| 5089 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5090 | char *codebytes; |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 5091 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5092 | assert(nbytes == 1 || nbytes == 2 || nbytes == 4); |
| 5093 | if (self->read_func(self, &codebytes, nbytes) < 0) return -1; |
| 5094 | PDATA_APPEND(self->stack, Py_None, -1); |
| 5095 | return 0; |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 5096 | } |
| 5097 | |
Neil Schemenauer | 973f8b4 | 2009-10-14 19:33:31 +0000 | [diff] [blame] | 5098 | static int |
| 5099 | noload_append(Unpicklerobject *self) |
| 5100 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5101 | return Pdata_clear(self->stack, self->stack->length - 1); |
Neil Schemenauer | 973f8b4 | 2009-10-14 19:33:31 +0000 | [diff] [blame] | 5102 | } |
| 5103 | |
| 5104 | static int |
| 5105 | noload_appends(Unpicklerobject *self) |
| 5106 | { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 5107 | Py_ssize_t i; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5108 | if ((i = marker(self)) < 0) return -1; |
| 5109 | return Pdata_clear(self->stack, i); |
Neil Schemenauer | 973f8b4 | 2009-10-14 19:33:31 +0000 | [diff] [blame] | 5110 | } |
| 5111 | |
| 5112 | static int |
| 5113 | noload_setitem(Unpicklerobject *self) |
| 5114 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5115 | return Pdata_clear(self->stack, self->stack->length - 2); |
Neil Schemenauer | 973f8b4 | 2009-10-14 19:33:31 +0000 | [diff] [blame] | 5116 | } |
| 5117 | |
| 5118 | static int |
| 5119 | noload_setitems(Unpicklerobject *self) |
| 5120 | { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 5121 | Py_ssize_t i; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5122 | if ((i = marker(self)) < 0) return -1; |
| 5123 | return Pdata_clear(self->stack, i); |
Neil Schemenauer | 973f8b4 | 2009-10-14 19:33:31 +0000 | [diff] [blame] | 5124 | } |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5125 | |
| 5126 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5127 | noload(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5128 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5129 | PyObject *err = 0, *val = 0; |
| 5130 | char *s; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5131 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5132 | self->num_marks = 0; |
| 5133 | Pdata_clear(self->stack, 0); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5134 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5135 | while (1) { |
| 5136 | if (self->read_func(self, &s, 1) < 0) |
| 5137 | break; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5138 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5139 | switch (s[0]) { |
| 5140 | case NONE: |
| 5141 | if (load_none(self) < 0) |
| 5142 | break; |
| 5143 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5144 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5145 | case BININT: |
| 5146 | if (load_binint(self) < 0) |
| 5147 | break; |
| 5148 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5149 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5150 | case BININT1: |
| 5151 | if (load_binint1(self) < 0) |
| 5152 | break; |
| 5153 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5154 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5155 | case BININT2: |
| 5156 | if (load_binint2(self) < 0) |
| 5157 | break; |
| 5158 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5159 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5160 | case INT: |
| 5161 | if (load_int(self) < 0) |
| 5162 | break; |
| 5163 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5164 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5165 | case LONG: |
| 5166 | if (load_long(self) < 0) |
| 5167 | break; |
| 5168 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5169 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5170 | case LONG1: |
| 5171 | if (load_counted_long(self, 1) < 0) |
| 5172 | break; |
| 5173 | continue; |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 5174 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5175 | case LONG4: |
| 5176 | if (load_counted_long(self, 4) < 0) |
| 5177 | break; |
| 5178 | continue; |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 5179 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5180 | case FLOAT: |
| 5181 | if (load_float(self) < 0) |
| 5182 | break; |
| 5183 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5184 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5185 | case BINFLOAT: |
| 5186 | if (load_binfloat(self) < 0) |
| 5187 | break; |
| 5188 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5189 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5190 | case BINSTRING: |
| 5191 | if (load_binstring(self) < 0) |
| 5192 | break; |
| 5193 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5194 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5195 | case SHORT_BINSTRING: |
| 5196 | if (load_short_binstring(self) < 0) |
| 5197 | break; |
| 5198 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5199 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5200 | case STRING: |
| 5201 | if (load_string(self) < 0) |
| 5202 | break; |
| 5203 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5204 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 5205 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5206 | case UNICODE: |
| 5207 | if (load_unicode(self) < 0) |
| 5208 | break; |
| 5209 | continue; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 5210 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5211 | case BINUNICODE: |
| 5212 | if (load_binunicode(self) < 0) |
| 5213 | break; |
| 5214 | continue; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 5215 | #endif |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 5216 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5217 | case EMPTY_TUPLE: |
| 5218 | if (load_counted_tuple(self, 0) < 0) |
| 5219 | break; |
| 5220 | continue; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 5221 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5222 | case TUPLE1: |
| 5223 | if (load_counted_tuple(self, 1) < 0) |
| 5224 | break; |
| 5225 | continue; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 5226 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5227 | case TUPLE2: |
| 5228 | if (load_counted_tuple(self, 2) < 0) |
| 5229 | break; |
| 5230 | continue; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 5231 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5232 | case TUPLE3: |
| 5233 | if (load_counted_tuple(self, 3) < 0) |
| 5234 | break; |
| 5235 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5236 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5237 | case TUPLE: |
| 5238 | if (load_tuple(self) < 0) |
| 5239 | break; |
| 5240 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5241 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5242 | case EMPTY_LIST: |
| 5243 | if (load_empty_list(self) < 0) |
| 5244 | break; |
| 5245 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5246 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5247 | case LIST: |
| 5248 | if (load_list(self) < 0) |
| 5249 | break; |
| 5250 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5251 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5252 | case EMPTY_DICT: |
| 5253 | if (load_empty_dict(self) < 0) |
| 5254 | break; |
| 5255 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5256 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5257 | case DICT: |
| 5258 | if (load_dict(self) < 0) |
| 5259 | break; |
| 5260 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5261 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5262 | case OBJ: |
| 5263 | if (noload_obj(self) < 0) |
| 5264 | break; |
| 5265 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5266 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5267 | case INST: |
| 5268 | if (noload_inst(self) < 0) |
| 5269 | break; |
| 5270 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5271 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5272 | case NEWOBJ: |
| 5273 | if (noload_newobj(self) < 0) |
| 5274 | break; |
| 5275 | continue; |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 5276 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5277 | case GLOBAL: |
| 5278 | if (noload_global(self) < 0) |
| 5279 | break; |
| 5280 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5281 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5282 | case APPEND: |
| 5283 | if (noload_append(self) < 0) |
| 5284 | break; |
| 5285 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5286 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5287 | case APPENDS: |
| 5288 | if (noload_appends(self) < 0) |
| 5289 | break; |
| 5290 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5291 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5292 | case BUILD: |
| 5293 | if (noload_build(self) < 0) |
| 5294 | break; |
| 5295 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5296 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5297 | case DUP: |
| 5298 | if (load_dup(self) < 0) |
| 5299 | break; |
| 5300 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5301 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5302 | case BINGET: |
| 5303 | if (load_binget(self) < 0) |
| 5304 | break; |
| 5305 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5306 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5307 | case LONG_BINGET: |
| 5308 | if (load_long_binget(self) < 0) |
| 5309 | break; |
| 5310 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5311 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5312 | case GET: |
| 5313 | if (load_get(self) < 0) |
| 5314 | break; |
| 5315 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5316 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5317 | case EXT1: |
| 5318 | if (noload_extension(self, 1) < 0) |
| 5319 | break; |
| 5320 | continue; |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 5321 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5322 | case EXT2: |
| 5323 | if (noload_extension(self, 2) < 0) |
| 5324 | break; |
| 5325 | continue; |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 5326 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5327 | case EXT4: |
| 5328 | if (noload_extension(self, 4) < 0) |
| 5329 | break; |
| 5330 | continue; |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 5331 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5332 | case MARK: |
| 5333 | if (load_mark(self) < 0) |
| 5334 | break; |
| 5335 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5336 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5337 | case BINPUT: |
| 5338 | if (load_binput(self) < 0) |
| 5339 | break; |
| 5340 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5341 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5342 | case LONG_BINPUT: |
| 5343 | if (load_long_binput(self) < 0) |
| 5344 | break; |
| 5345 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5346 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5347 | case PUT: |
| 5348 | if (load_put(self) < 0) |
| 5349 | break; |
| 5350 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5351 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5352 | case POP: |
| 5353 | if (load_pop(self) < 0) |
| 5354 | break; |
| 5355 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5356 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5357 | case POP_MARK: |
| 5358 | if (load_pop_mark(self) < 0) |
| 5359 | break; |
| 5360 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5361 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5362 | case SETITEM: |
| 5363 | if (noload_setitem(self) < 0) |
| 5364 | break; |
| 5365 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5366 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5367 | case SETITEMS: |
| 5368 | if (noload_setitems(self) < 0) |
| 5369 | break; |
| 5370 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5371 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5372 | case STOP: |
| 5373 | break; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5374 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5375 | case PERSID: |
| 5376 | if (load_persid(self) < 0) |
| 5377 | break; |
| 5378 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5379 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5380 | case BINPERSID: |
| 5381 | if (load_binpersid(self) < 0) |
| 5382 | break; |
| 5383 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5384 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5385 | case REDUCE: |
| 5386 | if (noload_reduce(self) < 0) |
| 5387 | break; |
| 5388 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5389 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5390 | case PROTO: |
| 5391 | if (load_proto(self) < 0) |
| 5392 | break; |
| 5393 | continue; |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 5394 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5395 | case NEWTRUE: |
| 5396 | if (load_bool(self, Py_True) < 0) |
| 5397 | break; |
| 5398 | continue; |
Tim Peters | 3c67d79 | 2003-02-02 17:59:11 +0000 | [diff] [blame] | 5399 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5400 | case NEWFALSE: |
| 5401 | if (load_bool(self, Py_False) < 0) |
| 5402 | break; |
| 5403 | continue; |
| 5404 | default: |
| 5405 | cPickle_ErrFormat(UnpicklingError, |
| 5406 | "invalid load key, '%s'.", |
| 5407 | "c", s[0]); |
| 5408 | return NULL; |
| 5409 | } |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5410 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5411 | break; |
| 5412 | } |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5413 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5414 | if ((err = PyErr_Occurred())) { |
| 5415 | if (err == PyExc_EOFError) { |
| 5416 | PyErr_SetNone(PyExc_EOFError); |
| 5417 | } |
| 5418 | return NULL; |
| 5419 | } |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5420 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5421 | PDATA_POP(self->stack, val); |
| 5422 | return val; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5423 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5424 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5425 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5426 | static PyObject * |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 5427 | Unpickler_load(Unpicklerobject *self, PyObject *unused) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5428 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5429 | return load(self); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5430 | } |
| 5431 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5432 | static PyObject * |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 5433 | Unpickler_noload(Unpicklerobject *self, PyObject *unused) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5434 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5435 | return noload(self); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5436 | } |
| 5437 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5438 | |
| 5439 | static struct PyMethodDef Unpickler_methods[] = { |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 5440 | {"load", (PyCFunction)Unpickler_load, METH_NOARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 5441 | PyDoc_STR("load() -- Load a pickle") |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5442 | }, |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 5443 | {"noload", (PyCFunction)Unpickler_noload, METH_NOARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 5444 | PyDoc_STR( |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5445 | "noload() -- not load a pickle, but go through most of the motions\n" |
| 5446 | "\n" |
| 5447 | "This function can be used to read past a pickle without instantiating\n" |
| 5448 | "any objects or importing any modules. It can also be used to find all\n" |
| 5449 | "persistent references without instantiating any objects or importing\n" |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 5450 | "any modules.\n") |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5451 | }, |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5452 | {NULL, NULL} /* sentinel */ |
| 5453 | }; |
| 5454 | |
| 5455 | |
| 5456 | static Unpicklerobject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5457 | newUnpicklerobject(PyObject *f) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5458 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5459 | Unpicklerobject *self; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5460 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5461 | if (!( self = PyObject_GC_New(Unpicklerobject, &Unpicklertype))) |
| 5462 | return NULL; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5463 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5464 | self->file = NULL; |
| 5465 | self->arg = NULL; |
| 5466 | self->stack = (Pdata*)Pdata_New(); |
| 5467 | self->pers_func = NULL; |
| 5468 | self->last_string = NULL; |
| 5469 | self->marks = NULL; |
| 5470 | self->num_marks = 0; |
| 5471 | self->marks_size = 0; |
| 5472 | self->buf_size = 0; |
| 5473 | self->read = NULL; |
| 5474 | self->readline = NULL; |
| 5475 | self->find_class = NULL; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5476 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5477 | if (!( self->memo = PyDict_New())) |
| 5478 | goto err; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5479 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5480 | if (!self->stack) |
| 5481 | goto err; |
Neal Norwitz | b59d08c | 2006-07-22 16:20:49 +0000 | [diff] [blame] | 5482 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5483 | Py_INCREF(f); |
| 5484 | self->file = f; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5485 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5486 | /* Set read, readline based on type of f */ |
| 5487 | if (PyFile_Check(f)) { |
| 5488 | self->fp = PyFile_AsFile(f); |
| 5489 | if (self->fp == NULL) { |
| 5490 | PyErr_SetString(PyExc_ValueError, |
| 5491 | "I/O operation on closed file"); |
| 5492 | goto err; |
| 5493 | } |
| 5494 | self->read_func = read_file; |
| 5495 | self->readline_func = readline_file; |
| 5496 | } |
| 5497 | else if (PycStringIO_InputCheck(f)) { |
| 5498 | self->fp = NULL; |
| 5499 | self->read_func = read_cStringIO; |
| 5500 | self->readline_func = readline_cStringIO; |
| 5501 | } |
| 5502 | else { |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5503 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5504 | self->fp = NULL; |
| 5505 | self->read_func = read_other; |
| 5506 | self->readline_func = readline_other; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5507 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5508 | if (!( (self->readline = PyObject_GetAttr(f, readline_str)) && |
| 5509 | (self->read = PyObject_GetAttr(f, read_str)))) { |
| 5510 | PyErr_Clear(); |
| 5511 | PyErr_SetString( PyExc_TypeError, |
| 5512 | "argument must have 'read' and " |
| 5513 | "'readline' attributes" ); |
| 5514 | goto err; |
| 5515 | } |
| 5516 | } |
| 5517 | PyObject_GC_Track(self); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5518 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5519 | return self; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5520 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5521 | err: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5522 | Py_DECREF((PyObject *)self); |
| 5523 | return NULL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5524 | } |
| 5525 | |
| 5526 | |
| 5527 | static PyObject * |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 5528 | get_Unpickler(PyObject *self, PyObject *file) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5529 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5530 | return (PyObject *)newUnpicklerobject(file); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5531 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5532 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5533 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5534 | static void |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5535 | Unpickler_dealloc(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5536 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5537 | PyObject_GC_UnTrack((PyObject *)self); |
| 5538 | Py_XDECREF(self->readline); |
| 5539 | Py_XDECREF(self->read); |
| 5540 | Py_XDECREF(self->file); |
| 5541 | Py_XDECREF(self->memo); |
| 5542 | Py_XDECREF(self->stack); |
| 5543 | Py_XDECREF(self->pers_func); |
| 5544 | Py_XDECREF(self->arg); |
| 5545 | Py_XDECREF(self->last_string); |
| 5546 | Py_XDECREF(self->find_class); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5547 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5548 | if (self->marks) { |
| 5549 | free(self->marks); |
| 5550 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5551 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5552 | if (self->buf_size) { |
| 5553 | free(self->buf); |
| 5554 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5555 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5556 | Py_TYPE(self)->tp_free((PyObject *)self); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5557 | } |
| 5558 | |
Jeremy Hylton | 7b5ce7f | 2003-04-09 21:25:30 +0000 | [diff] [blame] | 5559 | static int |
| 5560 | Unpickler_traverse(Unpicklerobject *self, visitproc visit, void *arg) |
| 5561 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5562 | Py_VISIT(self->readline); |
| 5563 | Py_VISIT(self->read); |
| 5564 | Py_VISIT(self->file); |
| 5565 | Py_VISIT(self->memo); |
| 5566 | Py_VISIT(self->stack); |
| 5567 | Py_VISIT(self->pers_func); |
| 5568 | Py_VISIT(self->arg); |
| 5569 | Py_VISIT(self->last_string); |
| 5570 | Py_VISIT(self->find_class); |
| 5571 | return 0; |
Jeremy Hylton | 7b5ce7f | 2003-04-09 21:25:30 +0000 | [diff] [blame] | 5572 | } |
| 5573 | |
| 5574 | static int |
| 5575 | Unpickler_clear(Unpicklerobject *self) |
| 5576 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5577 | Py_CLEAR(self->readline); |
| 5578 | Py_CLEAR(self->read); |
| 5579 | Py_CLEAR(self->file); |
| 5580 | Py_CLEAR(self->memo); |
| 5581 | Py_CLEAR(self->stack); |
| 5582 | Py_CLEAR(self->pers_func); |
| 5583 | Py_CLEAR(self->arg); |
| 5584 | Py_CLEAR(self->last_string); |
| 5585 | Py_CLEAR(self->find_class); |
| 5586 | return 0; |
Jeremy Hylton | 7b5ce7f | 2003-04-09 21:25:30 +0000 | [diff] [blame] | 5587 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5588 | |
| 5589 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5590 | Unpickler_getattr(Unpicklerobject *self, char *name) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5591 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5592 | if (!strcmp(name, "persistent_load")) { |
| 5593 | if (!self->pers_func) { |
| 5594 | PyErr_SetString(PyExc_AttributeError, name); |
| 5595 | return NULL; |
| 5596 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5597 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5598 | Py_INCREF(self->pers_func); |
| 5599 | return self->pers_func; |
| 5600 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5601 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5602 | if (!strcmp(name, "find_global")) { |
| 5603 | if (!self->find_class) { |
| 5604 | PyErr_SetString(PyExc_AttributeError, name); |
| 5605 | return NULL; |
| 5606 | } |
Guido van Rossum | 1b9e0aa | 1999-04-19 17:58:18 +0000 | [diff] [blame] | 5607 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5608 | Py_INCREF(self->find_class); |
| 5609 | return self->find_class; |
| 5610 | } |
Guido van Rossum | 1b9e0aa | 1999-04-19 17:58:18 +0000 | [diff] [blame] | 5611 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5612 | if (!strcmp(name, "memo")) { |
| 5613 | if (!self->memo) { |
| 5614 | PyErr_SetString(PyExc_AttributeError, name); |
| 5615 | return NULL; |
| 5616 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5617 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5618 | Py_INCREF(self->memo); |
| 5619 | return self->memo; |
| 5620 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5621 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5622 | if (!strcmp(name, "UnpicklingError")) { |
| 5623 | Py_INCREF(UnpicklingError); |
| 5624 | return UnpicklingError; |
| 5625 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5626 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5627 | return Py_FindMethod(Unpickler_methods, (PyObject *)self, name); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5628 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5629 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5630 | |
| 5631 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5632 | Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5633 | { |
Jeremy Hylton | ce616e4 | 1998-08-13 23:13:52 +0000 | [diff] [blame] | 5634 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5635 | if (!strcmp(name, "persistent_load")) { |
| 5636 | Py_XDECREF(self->pers_func); |
| 5637 | self->pers_func = value; |
| 5638 | Py_XINCREF(value); |
| 5639 | return 0; |
| 5640 | } |
Guido van Rossum | 1b9e0aa | 1999-04-19 17:58:18 +0000 | [diff] [blame] | 5641 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5642 | if (!strcmp(name, "find_global")) { |
| 5643 | Py_XDECREF(self->find_class); |
| 5644 | self->find_class = value; |
| 5645 | Py_XINCREF(value); |
| 5646 | return 0; |
| 5647 | } |
Guido van Rossum | 1b9e0aa | 1999-04-19 17:58:18 +0000 | [diff] [blame] | 5648 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5649 | if (! value) { |
| 5650 | PyErr_SetString(PyExc_TypeError, |
| 5651 | "attribute deletion is not supported"); |
| 5652 | return -1; |
| 5653 | } |
Jeremy Hylton | ce616e4 | 1998-08-13 23:13:52 +0000 | [diff] [blame] | 5654 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5655 | if (strcmp(name, "memo") == 0) { |
| 5656 | if (!PyDict_Check(value)) { |
| 5657 | PyErr_SetString(PyExc_TypeError, |
| 5658 | "memo must be a dictionary"); |
| 5659 | return -1; |
| 5660 | } |
| 5661 | Py_XDECREF(self->memo); |
| 5662 | self->memo = value; |
| 5663 | Py_INCREF(value); |
| 5664 | return 0; |
| 5665 | } |
Jeremy Hylton | ce616e4 | 1998-08-13 23:13:52 +0000 | [diff] [blame] | 5666 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5667 | PyErr_SetString(PyExc_AttributeError, name); |
| 5668 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5669 | } |
| 5670 | |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5671 | /* --------------------------------------------------------------------------- |
| 5672 | * Module-level functions. |
| 5673 | */ |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5674 | |
Martin v. Löwis | 544f119 | 2004-07-27 05:22:33 +0000 | [diff] [blame] | 5675 | /* dump(obj, file, protocol=0). */ |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5676 | static PyObject * |
Martin v. Löwis | 544f119 | 2004-07-27 05:22:33 +0000 | [diff] [blame] | 5677 | cpm_dump(PyObject *self, PyObject *args, PyObject *kwds) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5678 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5679 | static char *kwlist[] = {"obj", "file", "protocol", NULL}; |
| 5680 | PyObject *ob, *file, *res = NULL; |
| 5681 | Picklerobject *pickler = 0; |
| 5682 | int proto = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5683 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5684 | if (!( PyArg_ParseTupleAndKeywords(args, kwds, "OO|i", kwlist, |
| 5685 | &ob, &file, &proto))) |
| 5686 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5687 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5688 | if (!( pickler = newPicklerobject(file, proto))) |
| 5689 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5690 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5691 | if (dump(pickler, ob) < 0) |
| 5692 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5693 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5694 | Py_INCREF(Py_None); |
| 5695 | res = Py_None; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5696 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5697 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5698 | Py_XDECREF(pickler); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5699 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5700 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5701 | } |
| 5702 | |
| 5703 | |
Martin v. Löwis | 544f119 | 2004-07-27 05:22:33 +0000 | [diff] [blame] | 5704 | /* dumps(obj, protocol=0). */ |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5705 | static PyObject * |
Martin v. Löwis | 544f119 | 2004-07-27 05:22:33 +0000 | [diff] [blame] | 5706 | cpm_dumps(PyObject *self, PyObject *args, PyObject *kwds) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5707 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5708 | static char *kwlist[] = {"obj", "protocol", NULL}; |
| 5709 | PyObject *ob, *file = 0, *res = NULL; |
| 5710 | Picklerobject *pickler = 0; |
| 5711 | int proto = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5712 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5713 | if (!( PyArg_ParseTupleAndKeywords(args, kwds, "O|i:dumps", kwlist, |
| 5714 | &ob, &proto))) |
| 5715 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5716 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5717 | if (!( file = PycStringIO->NewOutput(128))) |
| 5718 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5719 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5720 | if (!( pickler = newPicklerobject(file, proto))) |
| 5721 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5722 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5723 | if (dump(pickler, ob) < 0) |
| 5724 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5725 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5726 | res = PycStringIO->cgetvalue(file); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5727 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5728 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5729 | Py_XDECREF(pickler); |
| 5730 | Py_XDECREF(file); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5731 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5732 | return res; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5733 | } |
| 5734 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5735 | |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5736 | /* load(fileobj). */ |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5737 | static PyObject * |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 5738 | cpm_load(PyObject *self, PyObject *ob) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5739 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5740 | Unpicklerobject *unpickler = 0; |
| 5741 | PyObject *res = NULL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5742 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5743 | if (!( unpickler = newUnpicklerobject(ob))) |
| 5744 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5745 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5746 | res = load(unpickler); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5747 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5748 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5749 | Py_XDECREF(unpickler); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5750 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5751 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5752 | } |
| 5753 | |
| 5754 | |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5755 | /* loads(string) */ |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5756 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5757 | cpm_loads(PyObject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5758 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5759 | PyObject *ob, *file = 0, *res = NULL; |
| 5760 | Unpicklerobject *unpickler = 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5761 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5762 | if (!( PyArg_ParseTuple(args, "S:loads", &ob))) |
| 5763 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5764 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5765 | if (!( file = PycStringIO->NewInput(ob))) |
| 5766 | goto finally; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5767 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5768 | if (!( unpickler = newUnpicklerobject(file))) |
| 5769 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5770 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5771 | res = load(unpickler); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5772 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5773 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5774 | Py_XDECREF(file); |
| 5775 | Py_XDECREF(unpickler); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5776 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5777 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5778 | } |
| 5779 | |
| 5780 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5781 | PyDoc_STRVAR(Unpicklertype__doc__, |
| 5782 | "Objects that know how to unpickle"); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5783 | |
Guido van Rossum | 9716aaa | 1997-12-08 15:15:16 +0000 | [diff] [blame] | 5784 | static PyTypeObject Unpicklertype = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 5785 | PyVarObject_HEAD_INIT(NULL, 0) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5786 | "cPickle.Unpickler", /*tp_name*/ |
Jeremy Hylton | 7b5ce7f | 2003-04-09 21:25:30 +0000 | [diff] [blame] | 5787 | sizeof(Unpicklerobject), /*tp_basicsize*/ |
| 5788 | 0, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5789 | (destructor)Unpickler_dealloc, /* tp_dealloc */ |
| 5790 | 0, /* tp_print */ |
| 5791 | (getattrfunc)Unpickler_getattr, /* tp_getattr */ |
| 5792 | (setattrfunc)Unpickler_setattr, /* tp_setattr */ |
| 5793 | 0, /* tp_compare */ |
| 5794 | 0, /* tp_repr */ |
| 5795 | 0, /* tp_as_number */ |
| 5796 | 0, /* tp_as_sequence */ |
| 5797 | 0, /* tp_as_mapping */ |
| 5798 | 0, /* tp_hash */ |
| 5799 | 0, /* tp_call */ |
| 5800 | 0, /* tp_str */ |
| 5801 | 0, /* tp_getattro */ |
| 5802 | 0, /* tp_setattro */ |
| 5803 | 0, /* tp_as_buffer */ |
Jeremy Hylton | 7b5ce7f | 2003-04-09 21:25:30 +0000 | [diff] [blame] | 5804 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5805 | Unpicklertype__doc__, /* tp_doc */ |
| 5806 | (traverseproc)Unpickler_traverse, /* tp_traverse */ |
| 5807 | (inquiry)Unpickler_clear, /* tp_clear */ |
Guido van Rossum | 9716aaa | 1997-12-08 15:15:16 +0000 | [diff] [blame] | 5808 | }; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5809 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5810 | static struct PyMethodDef cPickle_methods[] = { |
Martin v. Löwis | 544f119 | 2004-07-27 05:22:33 +0000 | [diff] [blame] | 5811 | {"dump", (PyCFunction)cpm_dump, METH_VARARGS | METH_KEYWORDS, |
| 5812 | PyDoc_STR("dump(obj, file, protocol=0) -- " |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5813 | "Write an object in pickle format to the given file.\n" |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5814 | "\n" |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5815 | "See the Pickler docstring for the meaning of optional argument proto.") |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5816 | }, |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5817 | |
Martin v. Löwis | 544f119 | 2004-07-27 05:22:33 +0000 | [diff] [blame] | 5818 | {"dumps", (PyCFunction)cpm_dumps, METH_VARARGS | METH_KEYWORDS, |
| 5819 | PyDoc_STR("dumps(obj, protocol=0) -- " |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5820 | "Return a string containing an object in pickle format.\n" |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5821 | "\n" |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5822 | "See the Pickler docstring for the meaning of optional argument proto.") |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5823 | }, |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5824 | |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 5825 | {"load", (PyCFunction)cpm_load, METH_O, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 5826 | PyDoc_STR("load(file) -- Load a pickle from the given file")}, |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5827 | |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 5828 | {"loads", (PyCFunction)cpm_loads, METH_VARARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 5829 | PyDoc_STR("loads(string) -- Load a pickle from the given string")}, |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5830 | |
Martin v. Löwis | 544f119 | 2004-07-27 05:22:33 +0000 | [diff] [blame] | 5831 | {"Pickler", (PyCFunction)get_Pickler, METH_VARARGS | METH_KEYWORDS, |
| 5832 | PyDoc_STR("Pickler(file, protocol=0) -- Create a pickler.\n" |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5833 | "\n" |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5834 | "This takes a file-like object for writing a pickle data stream.\n" |
| 5835 | "The optional proto argument tells the pickler to use the given\n" |
| 5836 | "protocol; supported protocols are 0, 1, 2. The default\n" |
| 5837 | "protocol is 0, to be backwards compatible. (Protocol 0 is the\n" |
| 5838 | "only protocol that can be written to a file opened in text\n" |
| 5839 | "mode and read back successfully. When using a protocol higher\n" |
| 5840 | "than 0, make sure the file is opened in binary mode, both when\n" |
| 5841 | "pickling and unpickling.)\n" |
| 5842 | "\n" |
| 5843 | "Protocol 1 is more efficient than protocol 0; protocol 2 is\n" |
| 5844 | "more efficient than protocol 1.\n" |
| 5845 | "\n" |
| 5846 | "Specifying a negative protocol version selects the highest\n" |
| 5847 | "protocol version supported. The higher the protocol used, the\n" |
| 5848 | "more recent the version of Python needed to read the pickle\n" |
| 5849 | "produced.\n" |
| 5850 | "\n" |
| 5851 | "The file parameter must have a write() method that accepts a single\n" |
| 5852 | "string argument. It can thus be an open file object, a StringIO\n" |
| 5853 | "object, or any other custom object that meets this interface.\n") |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5854 | }, |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5855 | |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 5856 | {"Unpickler", (PyCFunction)get_Unpickler, METH_O, |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5857 | PyDoc_STR("Unpickler(file) -- Create an unpickler.")}, |
| 5858 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5859 | { NULL, NULL } |
| 5860 | }; |
| 5861 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5862 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5863 | init_stuff(PyObject *module_dict) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5864 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5865 | PyObject *copyreg, *t, *r; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5866 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 5867 | #define INIT_STR(S) if (!( S ## _str=PyString_InternFromString(#S))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5868 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5869 | if (PyType_Ready(&Unpicklertype) < 0) |
| 5870 | return -1; |
| 5871 | if (PyType_Ready(&Picklertype) < 0) |
| 5872 | return -1; |
Tim Peters | 3cfe754 | 2003-05-21 21:29:48 +0000 | [diff] [blame] | 5873 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5874 | INIT_STR(__class__); |
| 5875 | INIT_STR(__getinitargs__); |
| 5876 | INIT_STR(__dict__); |
| 5877 | INIT_STR(__getstate__); |
| 5878 | INIT_STR(__setstate__); |
| 5879 | INIT_STR(__name__); |
| 5880 | INIT_STR(__main__); |
| 5881 | INIT_STR(__reduce__); |
| 5882 | INIT_STR(__reduce_ex__); |
| 5883 | INIT_STR(write); |
| 5884 | INIT_STR(append); |
| 5885 | INIT_STR(read); |
| 5886 | INIT_STR(readline); |
| 5887 | INIT_STR(dispatch_table); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5888 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5889 | if (!( copyreg = PyImport_ImportModule("copy_reg"))) |
| 5890 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5891 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5892 | /* This is special because we want to use a different |
| 5893 | one in restricted mode. */ |
| 5894 | dispatch_table = PyObject_GetAttr(copyreg, dispatch_table_str); |
| 5895 | if (!dispatch_table) return -1; |
Tim Peters | 5b7da39 | 2003-02-04 00:21:07 +0000 | [diff] [blame] | 5896 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5897 | extension_registry = PyObject_GetAttrString(copyreg, |
| 5898 | "_extension_registry"); |
| 5899 | if (!extension_registry) return -1; |
Tim Peters | 5b7da39 | 2003-02-04 00:21:07 +0000 | [diff] [blame] | 5900 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5901 | inverted_registry = PyObject_GetAttrString(copyreg, |
| 5902 | "_inverted_registry"); |
| 5903 | if (!inverted_registry) return -1; |
Tim Peters | 5b7da39 | 2003-02-04 00:21:07 +0000 | [diff] [blame] | 5904 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5905 | extension_cache = PyObject_GetAttrString(copyreg, |
| 5906 | "_extension_cache"); |
| 5907 | if (!extension_cache) return -1; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5908 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5909 | Py_DECREF(copyreg); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5910 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5911 | if (!(empty_tuple = PyTuple_New(0))) |
| 5912 | return -1; |
Tim Peters | 731098b | 2003-02-04 20:56:09 +0000 | [diff] [blame] | 5913 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5914 | two_tuple = PyTuple_New(2); |
| 5915 | if (two_tuple == NULL) |
| 5916 | return -1; |
| 5917 | /* We use this temp container with no regard to refcounts, or to |
| 5918 | * keeping containees alive. Exempt from GC, because we don't |
| 5919 | * want anything looking at two_tuple() by magic. |
| 5920 | */ |
| 5921 | PyObject_GC_UnTrack(two_tuple); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5922 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5923 | /* Ugh */ |
| 5924 | if (!( t=PyImport_ImportModule("__builtin__"))) return -1; |
| 5925 | if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0) |
| 5926 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5927 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5928 | if (!( t=PyDict_New())) return -1; |
| 5929 | if (!( r=PyRun_String( |
| 5930 | "def __str__(self):\n" |
| 5931 | " return self.args and ('%s' % self.args[0]) or '(what)'\n", |
| 5932 | Py_file_input, |
| 5933 | module_dict, t) )) return -1; |
| 5934 | Py_DECREF(r); |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5935 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5936 | PickleError = PyErr_NewException("cPickle.PickleError", NULL, t); |
| 5937 | if (!PickleError) |
| 5938 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5939 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5940 | Py_DECREF(t); |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5941 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5942 | PicklingError = PyErr_NewException("cPickle.PicklingError", |
| 5943 | PickleError, NULL); |
| 5944 | if (!PicklingError) |
| 5945 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5946 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5947 | if (!( t=PyDict_New())) return -1; |
| 5948 | if (!( r=PyRun_String( |
| 5949 | "def __str__(self):\n" |
| 5950 | " a=self.args\n" |
| 5951 | " a=a and type(a[0]) or '(what)'\n" |
| 5952 | " return 'Cannot pickle %s objects' % a\n" |
| 5953 | , Py_file_input, |
| 5954 | module_dict, t) )) return -1; |
| 5955 | Py_DECREF(r); |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5956 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5957 | if (!( UnpickleableError = PyErr_NewException( |
| 5958 | "cPickle.UnpickleableError", PicklingError, t))) |
| 5959 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5960 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5961 | Py_DECREF(t); |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5962 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5963 | if (!( UnpicklingError = PyErr_NewException("cPickle.UnpicklingError", |
| 5964 | PickleError, NULL))) |
| 5965 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5966 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5967 | if (!( BadPickleGet = PyErr_NewException("cPickle.BadPickleGet", |
| 5968 | UnpicklingError, NULL))) |
| 5969 | return -1; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5970 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5971 | if (PyDict_SetItemString(module_dict, "PickleError", |
| 5972 | PickleError) < 0) |
| 5973 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5974 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5975 | if (PyDict_SetItemString(module_dict, "PicklingError", |
| 5976 | PicklingError) < 0) |
| 5977 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5978 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5979 | if (PyDict_SetItemString(module_dict, "UnpicklingError", |
| 5980 | UnpicklingError) < 0) |
| 5981 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5982 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5983 | if (PyDict_SetItemString(module_dict, "UnpickleableError", |
| 5984 | UnpickleableError) < 0) |
| 5985 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5986 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5987 | if (PyDict_SetItemString(module_dict, "BadPickleGet", |
| 5988 | BadPickleGet) < 0) |
| 5989 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5990 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5991 | PycString_IMPORT; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 5992 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5993 | return 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5994 | } |
| 5995 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5996 | #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */ |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 5997 | #define PyMODINIT_FUNC void |
Guido van Rossum | f9ffb03 | 1999-02-04 14:54:04 +0000 | [diff] [blame] | 5998 | #endif |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 5999 | PyMODINIT_FUNC |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 6000 | initcPickle(void) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 6001 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 6002 | PyObject *m, *d, *di, *v, *k; |
| 6003 | Py_ssize_t i; |
| 6004 | char *rev = "1.71"; /* XXX when does this change? */ |
| 6005 | PyObject *format_version; |
| 6006 | PyObject *compatible_formats; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 6007 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 6008 | Py_TYPE(&Picklertype) = &PyType_Type; |
| 6009 | Py_TYPE(&Unpicklertype) = &PyType_Type; |
| 6010 | Py_TYPE(&PdataType) = &PyType_Type; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 6011 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 6012 | /* Initialize some pieces. We need to do this before module creation, |
| 6013 | * so we're forced to use a temporary dictionary. :( |
| 6014 | */ |
| 6015 | di = PyDict_New(); |
| 6016 | if (!di) return; |
| 6017 | if (init_stuff(di) < 0) return; |
Guido van Rossum | ebba420 | 2000-09-07 14:35:37 +0000 | [diff] [blame] | 6018 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 6019 | /* Create the module and add the functions */ |
| 6020 | m = Py_InitModule4("cPickle", cPickle_methods, |
| 6021 | cPickle_module_documentation, |
| 6022 | (PyObject*)NULL,PYTHON_API_VERSION); |
| 6023 | if (m == NULL) |
| 6024 | return; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 6025 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 6026 | /* Add some symbolic constants to the module */ |
| 6027 | d = PyModule_GetDict(m); |
| 6028 | v = PyString_FromString(rev); |
| 6029 | PyDict_SetItemString(d, "__version__", v); |
| 6030 | Py_XDECREF(v); |
Barry Warsaw | 93d29b6 | 1997-01-14 17:45:08 +0000 | [diff] [blame] | 6031 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 6032 | /* Copy data from di. Waaa. */ |
| 6033 | for (i=0; PyDict_Next(di, &i, &k, &v); ) { |
| 6034 | if (PyObject_SetItem(d, k, v) < 0) { |
| 6035 | Py_DECREF(di); |
| 6036 | return; |
| 6037 | } |
| 6038 | } |
| 6039 | Py_DECREF(di); |
Guido van Rossum | ebba420 | 2000-09-07 14:35:37 +0000 | [diff] [blame] | 6040 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 6041 | i = PyModule_AddIntConstant(m, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL); |
| 6042 | if (i < 0) |
| 6043 | return; |
Tim Peters | 8587b3c | 2003-02-13 15:44:41 +0000 | [diff] [blame] | 6044 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 6045 | /* These are purely informational; no code uses them. */ |
| 6046 | /* File format version we write. */ |
| 6047 | format_version = PyString_FromString("2.0"); |
| 6048 | /* Format versions we can read. */ |
| 6049 | compatible_formats = Py_BuildValue("[sssss]", |
| 6050 | "1.0", /* Original protocol 0 */ |
| 6051 | "1.1", /* Protocol 0 + INST */ |
| 6052 | "1.2", /* Original protocol 1 */ |
| 6053 | "1.3", /* Protocol 1 + BINFLOAT */ |
| 6054 | "2.0"); /* Original protocol 2 */ |
| 6055 | PyDict_SetItemString(d, "format_version", format_version); |
| 6056 | PyDict_SetItemString(d, "compatible_formats", compatible_formats); |
| 6057 | Py_XDECREF(format_version); |
| 6058 | Py_XDECREF(compatible_formats); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 6059 | } |