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) { \ |
Serhiy Storchaka | 98a9722 | 2014-02-09 13:14:04 +0200 | [diff] [blame] | 327 | Py_CLEAR(self->arg); \ |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 328 | } \ |
| 329 | } |
| 330 | |
Thomas Wouters | 3b6448f | 2000-07-22 23:56:07 +0000 | [diff] [blame] | 331 | typedef struct Picklerobject { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 332 | PyObject_HEAD |
| 333 | FILE *fp; |
| 334 | PyObject *write; |
| 335 | PyObject *file; |
| 336 | PyObject *memo; |
| 337 | PyObject *arg; |
| 338 | PyObject *pers_func; |
| 339 | PyObject *inst_pers_func; |
Tim Peters | 797ec24 | 2003-02-01 06:22:36 +0000 | [diff] [blame] | 340 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 341 | /* pickle protocol number, >= 0 */ |
| 342 | int proto; |
Tim Peters | 797ec24 | 2003-02-01 06:22:36 +0000 | [diff] [blame] | 343 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 344 | /* bool, true if proto > 0 */ |
| 345 | int bin; |
Tim Peters | 797ec24 | 2003-02-01 06:22:36 +0000 | [diff] [blame] | 346 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 347 | 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] | 348 | Py_ssize_t (*write_func)(struct Picklerobject *, const char *, Py_ssize_t); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 349 | char *write_buf; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 350 | Py_ssize_t buf_size; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 351 | PyObject *dispatch_table; |
| 352 | int fast_container; /* count nested container dumps */ |
| 353 | PyObject *fast_memo; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 354 | } Picklerobject; |
| 355 | |
Barry Warsaw | 52acb49 | 2001-12-21 20:04:22 +0000 | [diff] [blame] | 356 | #ifndef PY_CPICKLE_FAST_LIMIT |
| 357 | #define PY_CPICKLE_FAST_LIMIT 50 |
| 358 | #endif |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 359 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 360 | static PyTypeObject Picklertype; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 361 | |
Thomas Wouters | 3b6448f | 2000-07-22 23:56:07 +0000 | [diff] [blame] | 362 | typedef struct Unpicklerobject { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 363 | PyObject_HEAD |
| 364 | FILE *fp; |
| 365 | PyObject *file; |
| 366 | PyObject *readline; |
| 367 | PyObject *read; |
| 368 | PyObject *memo; |
| 369 | PyObject *arg; |
| 370 | Pdata *stack; |
| 371 | PyObject *mark; |
| 372 | PyObject *pers_func; |
| 373 | PyObject *last_string; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 374 | Py_ssize_t *marks; |
| 375 | Py_ssize_t num_marks; |
| 376 | Py_ssize_t marks_size; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 377 | Py_ssize_t (*read_func)(struct Unpicklerobject *, char **, Py_ssize_t); |
| 378 | Py_ssize_t (*readline_func)(struct Unpicklerobject *, char **); |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 379 | Py_ssize_t buf_size; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 380 | char *buf; |
| 381 | PyObject *find_class; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 382 | } Unpicklerobject; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 383 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 384 | static PyTypeObject Unpicklertype; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 385 | |
Thomas Wouters | 4789b3a | 2000-07-24 11:36:47 +0000 | [diff] [blame] | 386 | /* Forward decls that need the above structs */ |
| 387 | static int save(Picklerobject *, PyObject *, int); |
| 388 | static int put2(Picklerobject *, PyObject *); |
| 389 | |
Guido van Rossum | d385d59 | 1997-04-09 17:47:47 +0000 | [diff] [blame] | 390 | static |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 391 | PyObject * |
Thomas Wouters | 3b6448f | 2000-07-22 23:56:07 +0000 | [diff] [blame] | 392 | cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...) |
| 393 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 394 | va_list va; |
| 395 | PyObject *args=0, *retval=0; |
| 396 | va_start(va, format); |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 397 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 398 | if (format) args = Py_VaBuildValue(format, va); |
| 399 | va_end(va); |
| 400 | if (format && ! args) return NULL; |
| 401 | if (stringformat && !(retval=PyString_FromString(stringformat))) |
| 402 | return NULL; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 403 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 404 | if (retval) { |
| 405 | if (args) { |
| 406 | PyObject *v; |
| 407 | v=PyString_Format(retval, args); |
| 408 | Py_DECREF(retval); |
| 409 | Py_DECREF(args); |
| 410 | if (! v) return NULL; |
| 411 | retval=v; |
| 412 | } |
| 413 | } |
| 414 | else |
| 415 | if (args) retval=args; |
| 416 | else { |
| 417 | PyErr_SetObject(ErrType,Py_None); |
| 418 | return NULL; |
| 419 | } |
| 420 | PyErr_SetObject(ErrType,retval); |
| 421 | Py_DECREF(retval); |
| 422 | return NULL; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 425 | static Py_ssize_t |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 426 | 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] | 427 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 428 | size_t nbyteswritten; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 429 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 430 | if (s == NULL) { |
| 431 | return 0; |
| 432 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 433 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 434 | PyFile_IncUseCount((PyFileObject *)self->file); |
| 435 | Py_BEGIN_ALLOW_THREADS |
| 436 | nbyteswritten = fwrite(s, sizeof(char), n, self->fp); |
| 437 | Py_END_ALLOW_THREADS |
| 438 | PyFile_DecUseCount((PyFileObject *)self->file); |
| 439 | if (nbyteswritten != (size_t)n) { |
| 440 | PyErr_SetFromErrno(PyExc_IOError); |
| 441 | return -1; |
| 442 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 443 | |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 444 | return n; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 447 | static Py_ssize_t |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 448 | 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] | 449 | { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 450 | Py_ssize_t len = n; |
| 451 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 452 | if (s == NULL) { |
| 453 | return 0; |
| 454 | } |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 455 | |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 456 | while (n > INT_MAX) { |
| 457 | if (PycStringIO->cwrite((PyObject *)self->file, s, INT_MAX) != INT_MAX) { |
| 458 | return -1; |
| 459 | } |
| 460 | n -= INT_MAX; |
| 461 | } |
| 462 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 463 | if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) { |
| 464 | return -1; |
| 465 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 466 | |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 467 | return len; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 468 | } |
| 469 | |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 470 | static Py_ssize_t |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 471 | 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] | 472 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 473 | if (s == NULL) return 0; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 474 | return n; |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 475 | } |
| 476 | |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 477 | static Py_ssize_t |
| 478 | 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] | 479 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 480 | PyObject *py_str = 0, *junk = 0; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 481 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 482 | if (s == NULL) { |
| 483 | if (!( self->buf_size )) return 0; |
| 484 | py_str = PyString_FromStringAndSize(self->write_buf, |
| 485 | self->buf_size); |
| 486 | if (!py_str) |
| 487 | return -1; |
| 488 | } |
| 489 | else { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 490 | if (self->buf_size && n > WRITE_BUF_SIZE - self->buf_size) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 491 | if (write_other(self, NULL, 0) < 0) |
| 492 | return -1; |
| 493 | } |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 494 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 495 | if (n > WRITE_BUF_SIZE) { |
| 496 | if (!( py_str = |
| 497 | PyString_FromStringAndSize(s, n))) |
| 498 | return -1; |
| 499 | } |
| 500 | else { |
| 501 | memcpy(self->write_buf + self->buf_size, s, n); |
| 502 | self->buf_size += n; |
| 503 | return n; |
| 504 | } |
| 505 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 506 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 507 | if (self->write) { |
| 508 | /* object with write method */ |
| 509 | ARG_TUP(self, py_str); |
| 510 | if (self->arg) { |
| 511 | junk = PyObject_Call(self->write, self->arg, NULL); |
| 512 | FREE_ARG_TUP(self); |
| 513 | } |
| 514 | if (junk) Py_DECREF(junk); |
| 515 | else return -1; |
| 516 | } |
| 517 | else |
| 518 | PDATA_PUSH(self->file, py_str, -1); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 519 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 520 | self->buf_size = 0; |
| 521 | return n; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 525 | static Py_ssize_t |
| 526 | read_file(Unpicklerobject *self, char **s, Py_ssize_t n) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 527 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 528 | size_t nbytesread; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 529 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 530 | if (self->buf_size == 0) { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 531 | Py_ssize_t size; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 532 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 533 | size = ((n < 32) ? 32 : n); |
| 534 | if (!( self->buf = (char *)malloc(size))) { |
| 535 | PyErr_NoMemory(); |
| 536 | return -1; |
| 537 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 538 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 539 | self->buf_size = size; |
| 540 | } |
| 541 | else if (n > self->buf_size) { |
| 542 | char *newbuf = (char *)realloc(self->buf, n); |
| 543 | if (!newbuf) { |
| 544 | PyErr_NoMemory(); |
| 545 | return -1; |
| 546 | } |
| 547 | self->buf = newbuf; |
| 548 | self->buf_size = n; |
| 549 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 550 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 551 | PyFile_IncUseCount((PyFileObject *)self->file); |
| 552 | Py_BEGIN_ALLOW_THREADS |
| 553 | nbytesread = fread(self->buf, sizeof(char), n, self->fp); |
| 554 | Py_END_ALLOW_THREADS |
| 555 | PyFile_DecUseCount((PyFileObject *)self->file); |
| 556 | if (nbytesread != (size_t)n) { |
| 557 | if (feof(self->fp)) { |
| 558 | PyErr_SetNone(PyExc_EOFError); |
| 559 | return -1; |
| 560 | } |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 561 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 562 | PyErr_SetFromErrno(PyExc_IOError); |
| 563 | return -1; |
| 564 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 565 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 566 | *s = self->buf; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 567 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 568 | return n; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 572 | static Py_ssize_t |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 573 | readline_file(Unpicklerobject *self, char **s) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 574 | { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 575 | Py_ssize_t i; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 576 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 577 | if (self->buf_size == 0) { |
| 578 | if (!( self->buf = (char *)malloc(40))) { |
| 579 | PyErr_NoMemory(); |
| 580 | return -1; |
| 581 | } |
| 582 | self->buf_size = 40; |
| 583 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 584 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 585 | i = 0; |
| 586 | while (1) { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 587 | Py_ssize_t bigger; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 588 | char *newbuf; |
| 589 | for (; i < (self->buf_size - 1); i++) { |
| 590 | if (feof(self->fp) || |
| 591 | (self->buf[i] = getc(self->fp)) == '\n') { |
| 592 | self->buf[i + 1] = '\0'; |
| 593 | *s = self->buf; |
| 594 | return i + 1; |
| 595 | } |
| 596 | } |
Serhiy Storchaka | d36d4e0 | 2013-02-26 10:07:36 +0200 | [diff] [blame] | 597 | if (self->buf_size > (PY_SSIZE_T_MAX >> 1)) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 598 | PyErr_NoMemory(); |
| 599 | return -1; |
| 600 | } |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 601 | bigger = self->buf_size << 1; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 602 | newbuf = (char *)realloc(self->buf, bigger); |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 603 | if (newbuf == NULL) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 604 | PyErr_NoMemory(); |
| 605 | return -1; |
| 606 | } |
| 607 | self->buf = newbuf; |
| 608 | self->buf_size = bigger; |
| 609 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 610 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 611 | |
| 612 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 613 | static Py_ssize_t |
| 614 | read_cStringIO(Unpicklerobject *self, char **s, Py_ssize_t n) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 615 | { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 616 | Py_ssize_t len = n; |
| 617 | char *start, *end = NULL; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 618 | |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 619 | while (1) { |
| 620 | int k; |
| 621 | char *ptr; |
| 622 | if (n > INT_MAX) |
| 623 | k = INT_MAX; |
| 624 | else |
| 625 | k = (int)n; |
| 626 | if (PycStringIO->cread((PyObject *)self->file, &ptr, k) != k) { |
| 627 | PyErr_SetNone(PyExc_EOFError); |
| 628 | return -1; |
| 629 | } |
| 630 | if (end == NULL) |
| 631 | start = ptr; |
| 632 | else if (ptr != end) { |
| 633 | /* non-continuous area */ |
| 634 | return -1; |
| 635 | } |
| 636 | if (n <= INT_MAX) |
| 637 | break; |
| 638 | end = ptr + INT_MAX; |
| 639 | n -= INT_MAX; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 640 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 641 | |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 642 | *s = start; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 643 | |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 644 | return len; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 648 | static Py_ssize_t |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 649 | readline_cStringIO(Unpicklerobject *self, char **s) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 650 | { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 651 | Py_ssize_t n = 0; |
| 652 | char *start = NULL, *end = NULL; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 653 | |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 654 | while (1) { |
| 655 | int k; |
| 656 | char *ptr; |
| 657 | if ((k = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) { |
| 658 | return -1; |
| 659 | } |
| 660 | n += k; |
| 661 | if (end == NULL) |
| 662 | start = ptr; |
| 663 | else if (ptr != end) { |
| 664 | /* non-continuous area */ |
| 665 | return -1; |
| 666 | } |
| 667 | if (k == 0 || ptr[k - 1] == '\n') |
| 668 | break; |
| 669 | end = ptr + k; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 670 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 671 | |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 672 | *s = start; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 673 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 674 | return n; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 678 | static Py_ssize_t |
| 679 | read_other(Unpicklerobject *self, char **s, Py_ssize_t n) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 680 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 681 | PyObject *bytes, *str=0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 682 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 683 | if (!( bytes = PyInt_FromSsize_t(n))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 684 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 685 | ARG_TUP(self, bytes); |
| 686 | if (self->arg) { |
| 687 | str = PyObject_Call(self->read, self->arg, NULL); |
| 688 | FREE_ARG_TUP(self); |
| 689 | } |
| 690 | if (! str) return -1; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 691 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 692 | Py_XDECREF(self->last_string); |
| 693 | self->last_string = str; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 694 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 695 | if (! (*s = PyString_AsString(str))) return -1; |
Amaury Forgeot d'Arc | 74b3016 | 2009-07-23 19:26:02 +0000 | [diff] [blame] | 696 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 697 | if (PyString_GET_SIZE(str) != n) { |
| 698 | PyErr_SetNone(PyExc_EOFError); |
| 699 | return -1; |
| 700 | } |
Amaury Forgeot d'Arc | 74b3016 | 2009-07-23 19:26:02 +0000 | [diff] [blame] | 701 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 702 | return n; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 706 | static Py_ssize_t |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 707 | readline_other(Unpicklerobject *self, char **s) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 708 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 709 | PyObject *str; |
| 710 | Py_ssize_t str_size; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 711 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 712 | if (!( str = PyObject_CallObject(self->readline, empty_tuple))) { |
| 713 | return -1; |
| 714 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 715 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 716 | if ((str_size = PyString_Size(str)) < 0) |
| 717 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 718 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 719 | Py_XDECREF(self->last_string); |
| 720 | self->last_string = str; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 721 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 722 | if (! (*s = PyString_AsString(str))) |
| 723 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 724 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 725 | return str_size; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 726 | } |
| 727 | |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 728 | /* Copy the first n bytes from s into newly malloc'ed memory, plus a |
| 729 | * trailing 0 byte. Return a pointer to that, or NULL if out of memory. |
| 730 | * The caller is responsible for free()'ing the return value. |
| 731 | */ |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 732 | static char * |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 733 | pystrndup(const char *s, Py_ssize_t n) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 734 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 735 | char *r = (char *)malloc(n+1); |
| 736 | if (r == NULL) |
| 737 | return (char*)PyErr_NoMemory(); |
| 738 | memcpy(r, s, n); |
| 739 | r[n] = 0; |
| 740 | return r; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | |
| 744 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 745 | get(Picklerobject *self, PyObject *id) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 746 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 747 | PyObject *value, *mv; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 748 | Py_ssize_t c_value; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 749 | char s[30]; |
| 750 | size_t len; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 751 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 752 | if (!( mv = PyDict_GetItem(self->memo, id))) { |
| 753 | PyErr_SetObject(PyExc_KeyError, id); |
| 754 | return -1; |
| 755 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 756 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 757 | if (!( value = PyTuple_GetItem(mv, 0))) |
| 758 | return -1; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 759 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 760 | if (!( PyInt_Check(value))) { |
| 761 | PyErr_SetString(PicklingError, "no int where int expected in memo"); |
| 762 | return -1; |
| 763 | } |
| 764 | c_value = PyInt_AS_LONG((PyIntObject*)value); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 765 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 766 | if (!self->bin) { |
| 767 | s[0] = GET; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 768 | PyOS_snprintf(s + 1, sizeof(s) - 1, |
| 769 | "%" PY_FORMAT_SIZE_T "d\n", c_value); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 770 | len = strlen(s); |
| 771 | } |
| 772 | else if (Pdata_Check(self->file)) { |
| 773 | if (write_other(self, NULL, 0) < 0) return -1; |
| 774 | PDATA_APPEND(self->file, mv, -1); |
| 775 | return 0; |
| 776 | } |
| 777 | else { |
| 778 | if (c_value < 256) { |
| 779 | s[0] = BINGET; |
| 780 | s[1] = (int)(c_value & 0xff); |
| 781 | len = 2; |
| 782 | } |
| 783 | else { |
| 784 | s[0] = LONG_BINGET; |
| 785 | s[1] = (int)(c_value & 0xff); |
| 786 | s[2] = (int)((c_value >> 8) & 0xff); |
| 787 | s[3] = (int)((c_value >> 16) & 0xff); |
| 788 | s[4] = (int)((c_value >> 24) & 0xff); |
| 789 | len = 5; |
| 790 | } |
| 791 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 792 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 793 | if (self->write_func(self, s, len) < 0) |
| 794 | return -1; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 795 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 796 | return 0; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 797 | } |
Jeremy Hylton | ce616e4 | 1998-08-13 23:13:52 +0000 | [diff] [blame] | 798 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 799 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 800 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 801 | put(Picklerobject *self, PyObject *ob) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 802 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 803 | if (Py_REFCNT(ob) < 2 || self->fast) |
| 804 | return 0; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 805 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 806 | return put2(self, ob); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 807 | } |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 808 | |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 809 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 810 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 811 | put2(Picklerobject *self, PyObject *ob) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 812 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 813 | char c_str[30]; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 814 | Py_ssize_t len, p; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 815 | int res = -1; |
| 816 | PyObject *py_ob_id = 0, *memo_len = 0, *t = 0; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 817 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 818 | if (self->fast) |
| 819 | return 0; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 820 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 821 | if ((p = PyDict_Size(self->memo)) < 0) |
| 822 | goto finally; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 823 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 824 | /* Make sure memo keys are positive! */ |
| 825 | /* XXX Why? |
| 826 | * XXX And does "positive" really mean non-negative? |
| 827 | * XXX pickle.py starts with PUT index 0, not 1. This makes for |
| 828 | * XXX gratuitous differences between the pickling modules. |
| 829 | */ |
| 830 | p++; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 831 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 832 | if (!( py_ob_id = PyLong_FromVoidPtr(ob))) |
| 833 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 834 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 835 | if (!( memo_len = PyInt_FromLong(p))) |
| 836 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 837 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 838 | if (!( t = PyTuple_New(2))) |
| 839 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 840 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 841 | PyTuple_SET_ITEM(t, 0, memo_len); |
| 842 | Py_INCREF(memo_len); |
| 843 | PyTuple_SET_ITEM(t, 1, ob); |
| 844 | Py_INCREF(ob); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 845 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 846 | if (PyDict_SetItem(self->memo, py_ob_id, t) < 0) |
| 847 | goto finally; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 848 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 849 | if (!self->bin) { |
| 850 | c_str[0] = PUT; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 851 | PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, |
| 852 | "%" PY_FORMAT_SIZE_T "d\n", p); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 853 | len = strlen(c_str); |
| 854 | } |
| 855 | else if (Pdata_Check(self->file)) { |
| 856 | if (write_other(self, NULL, 0) < 0) return -1; |
| 857 | PDATA_APPEND(self->file, memo_len, -1); |
| 858 | res=0; /* Job well done ;) */ |
| 859 | goto finally; |
| 860 | } |
| 861 | else { |
| 862 | if (p >= 256) { |
| 863 | c_str[0] = LONG_BINPUT; |
| 864 | c_str[1] = (int)(p & 0xff); |
| 865 | c_str[2] = (int)((p >> 8) & 0xff); |
| 866 | c_str[3] = (int)((p >> 16) & 0xff); |
| 867 | c_str[4] = (int)((p >> 24) & 0xff); |
| 868 | len = 5; |
| 869 | } |
| 870 | else { |
| 871 | c_str[0] = BINPUT; |
| 872 | c_str[1] = p; |
| 873 | len = 2; |
| 874 | } |
| 875 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 876 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 877 | if (self->write_func(self, c_str, len) < 0) |
| 878 | goto finally; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 879 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 880 | res = 0; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 881 | |
| 882 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 883 | Py_XDECREF(py_ob_id); |
| 884 | Py_XDECREF(memo_len); |
| 885 | Py_XDECREF(t); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 886 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 887 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 888 | } |
| 889 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 890 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 891 | whichmodule(PyObject *global, PyObject *global_name) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 892 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 893 | Py_ssize_t i, j; |
| 894 | PyObject *module = 0, *modules_dict = 0, |
| 895 | *global_name_attr = 0, *name = 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 896 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 897 | module = PyObject_GetAttrString(global, "__module__"); |
| 898 | if (module) |
| 899 | return module; |
| 900 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 901 | PyErr_Clear(); |
| 902 | else |
| 903 | return NULL; |
Guido van Rossum | 4518823 | 1997-09-28 05:38:51 +0000 | [diff] [blame] | 904 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 905 | if (!( modules_dict = PySys_GetObject("modules"))) |
| 906 | return NULL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 907 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 908 | i = 0; |
| 909 | while ((j = PyDict_Next(modules_dict, &i, &name, &module))) { |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 910 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 911 | if (PyObject_Compare(name, __main___str)==0) continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 912 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 913 | global_name_attr = PyObject_GetAttr(module, global_name); |
| 914 | if (!global_name_attr) { |
| 915 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 916 | PyErr_Clear(); |
| 917 | else |
| 918 | return NULL; |
| 919 | continue; |
| 920 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 921 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 922 | if (global_name_attr != global) { |
| 923 | Py_DECREF(global_name_attr); |
| 924 | continue; |
| 925 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 926 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 927 | Py_DECREF(global_name_attr); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 928 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 929 | break; |
| 930 | } |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 931 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 932 | /* The following implements the rule in pickle.py added in 1.5 |
| 933 | that used __main__ if no module is found. I don't actually |
| 934 | like this rule. jlf |
| 935 | */ |
| 936 | if (!j) { |
| 937 | name=__main___str; |
| 938 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 939 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 940 | Py_INCREF(name); |
| 941 | return name; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 945 | static int |
Jeremy Hylton | 499ab6a | 2001-10-15 21:37:58 +0000 | [diff] [blame] | 946 | fast_save_enter(Picklerobject *self, PyObject *obj) |
| 947 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 948 | /* if fast_container < 0, we're doing an error exit. */ |
| 949 | if (++self->fast_container >= PY_CPICKLE_FAST_LIMIT) { |
| 950 | PyObject *key = NULL; |
| 951 | if (self->fast_memo == NULL) { |
| 952 | self->fast_memo = PyDict_New(); |
| 953 | if (self->fast_memo == NULL) { |
| 954 | self->fast_container = -1; |
| 955 | return 0; |
| 956 | } |
| 957 | } |
| 958 | key = PyLong_FromVoidPtr(obj); |
| 959 | if (key == NULL) |
| 960 | return 0; |
| 961 | if (PyDict_GetItem(self->fast_memo, key)) { |
| 962 | Py_DECREF(key); |
| 963 | PyErr_Format(PyExc_ValueError, |
| 964 | "fast mode: can't pickle cyclic objects " |
| 965 | "including object type %s at %p", |
| 966 | Py_TYPE(obj)->tp_name, obj); |
| 967 | self->fast_container = -1; |
| 968 | return 0; |
| 969 | } |
| 970 | if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) { |
| 971 | Py_DECREF(key); |
| 972 | self->fast_container = -1; |
| 973 | return 0; |
| 974 | } |
| 975 | Py_DECREF(key); |
| 976 | } |
| 977 | return 1; |
Jeremy Hylton | 499ab6a | 2001-10-15 21:37:58 +0000 | [diff] [blame] | 978 | } |
| 979 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 980 | int |
Jeremy Hylton | 499ab6a | 2001-10-15 21:37:58 +0000 | [diff] [blame] | 981 | fast_save_leave(Picklerobject *self, PyObject *obj) |
| 982 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 983 | if (self->fast_container-- >= PY_CPICKLE_FAST_LIMIT) { |
| 984 | PyObject *key = PyLong_FromVoidPtr(obj); |
| 985 | if (key == NULL) |
| 986 | return 0; |
| 987 | if (PyDict_DelItem(self->fast_memo, key) < 0) { |
| 988 | Py_DECREF(key); |
| 989 | return 0; |
| 990 | } |
| 991 | Py_DECREF(key); |
| 992 | } |
| 993 | return 1; |
Jeremy Hylton | 499ab6a | 2001-10-15 21:37:58 +0000 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 997 | save_none(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 998 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 999 | static char none = NONE; |
| 1000 | if (self->write_func(self, &none, 1) < 0) |
| 1001 | return -1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1002 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1003 | return 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1004 | } |
| 1005 | |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 1006 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1007 | save_bool(Picklerobject *self, PyObject *args) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 1008 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1009 | static const char *buf[2] = {FALSE, TRUE}; |
| 1010 | static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1}; |
| 1011 | long l = PyInt_AS_LONG((PyIntObject *)args); |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 1012 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1013 | if (self->proto >= 2) { |
| 1014 | char opcode = l ? NEWTRUE : NEWFALSE; |
| 1015 | if (self->write_func(self, &opcode, 1) < 0) |
| 1016 | return -1; |
| 1017 | } |
| 1018 | else if (self->write_func(self, buf[l], len[l]) < 0) |
| 1019 | return -1; |
| 1020 | return 0; |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 1021 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 1022 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1023 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1024 | save_int(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1025 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1026 | char c_str[32]; |
| 1027 | long l = PyInt_AS_LONG((PyIntObject *)args); |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 1028 | Py_ssize_t len = 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1029 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1030 | if (!self->bin |
Guido van Rossum | de8d6d7 | 1997-05-13 18:00:44 +0000 | [diff] [blame] | 1031 | #if SIZEOF_LONG > 4 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1032 | || l > 0x7fffffffL |
| 1033 | || l < -0x80000000L |
Guido van Rossum | de8d6d7 | 1997-05-13 18:00:44 +0000 | [diff] [blame] | 1034 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1035 | ) { |
| 1036 | /* Text-mode pickle, or long too big to fit in the 4-byte |
| 1037 | * signed BININT format: store as a string. |
| 1038 | */ |
| 1039 | c_str[0] = INT; |
| 1040 | PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%ld\n", l); |
| 1041 | if (self->write_func(self, c_str, strlen(c_str)) < 0) |
| 1042 | return -1; |
| 1043 | } |
| 1044 | else { |
| 1045 | /* Binary pickle and l fits in a signed 4-byte int. */ |
| 1046 | c_str[1] = (int)( l & 0xff); |
| 1047 | c_str[2] = (int)((l >> 8) & 0xff); |
| 1048 | c_str[3] = (int)((l >> 16) & 0xff); |
| 1049 | c_str[4] = (int)((l >> 24) & 0xff); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1050 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1051 | if ((c_str[4] == 0) && (c_str[3] == 0)) { |
| 1052 | if (c_str[2] == 0) { |
| 1053 | c_str[0] = BININT1; |
| 1054 | len = 2; |
| 1055 | } |
| 1056 | else { |
| 1057 | c_str[0] = BININT2; |
| 1058 | len = 3; |
| 1059 | } |
| 1060 | } |
| 1061 | else { |
| 1062 | c_str[0] = BININT; |
| 1063 | len = 5; |
| 1064 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1065 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1066 | if (self->write_func(self, c_str, len) < 0) |
| 1067 | return -1; |
| 1068 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1069 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1070 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | |
| 1074 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1075 | save_long(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1076 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1077 | Py_ssize_t size; |
| 1078 | int res = -1; |
| 1079 | PyObject *repr = NULL; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1080 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1081 | static char l = LONG; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1082 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1083 | if (self->proto >= 2) { |
| 1084 | /* Linear-time pickling. */ |
| 1085 | size_t nbits; |
| 1086 | size_t nbytes; |
| 1087 | unsigned char *pdata; |
| 1088 | char c_str[5]; |
| 1089 | int i; |
| 1090 | int sign = _PyLong_Sign(args); |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 1091 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1092 | if (sign == 0) { |
| 1093 | /* It's 0 -- an empty bytestring. */ |
| 1094 | c_str[0] = LONG1; |
| 1095 | c_str[1] = 0; |
| 1096 | i = self->write_func(self, c_str, 2); |
| 1097 | if (i < 0) goto finally; |
| 1098 | res = 0; |
| 1099 | goto finally; |
| 1100 | } |
| 1101 | nbits = _PyLong_NumBits(args); |
| 1102 | if (nbits == (size_t)-1 && PyErr_Occurred()) |
| 1103 | goto finally; |
| 1104 | /* How many bytes do we need? There are nbits >> 3 full |
| 1105 | * bytes of data, and nbits & 7 leftover bits. If there |
| 1106 | * are any leftover bits, then we clearly need another |
| 1107 | * byte. Wnat's not so obvious is that we *probably* |
| 1108 | * need another byte even if there aren't any leftovers: |
| 1109 | * the most-significant bit of the most-significant byte |
| 1110 | * acts like a sign bit, and it's usually got a sense |
| 1111 | * opposite of the one we need. The exception is longs |
| 1112 | * of the form -(2**(8*j-1)) for j > 0. Such a long is |
| 1113 | * its own 256's-complement, so has the right sign bit |
| 1114 | * even without the extra byte. That's a pain to check |
| 1115 | * for in advance, though, so we always grab an extra |
| 1116 | * byte at the start, and cut it back later if possible. |
| 1117 | */ |
| 1118 | nbytes = (nbits >> 3) + 1; |
| 1119 | if (nbytes > INT_MAX) { |
| 1120 | PyErr_SetString(PyExc_OverflowError, "long too large " |
| 1121 | "to pickle"); |
| 1122 | goto finally; |
| 1123 | } |
| 1124 | repr = PyString_FromStringAndSize(NULL, (int)nbytes); |
| 1125 | if (repr == NULL) goto finally; |
| 1126 | pdata = (unsigned char *)PyString_AS_STRING(repr); |
| 1127 | i = _PyLong_AsByteArray((PyLongObject *)args, |
| 1128 | pdata, nbytes, |
| 1129 | 1 /* little endian */, 1 /* signed */); |
| 1130 | if (i < 0) goto finally; |
| 1131 | /* If the long is negative, this may be a byte more than |
| 1132 | * needed. This is so iff the MSB is all redundant sign |
| 1133 | * bits. |
| 1134 | */ |
| 1135 | if (sign < 0 && nbytes > 1 && pdata[nbytes - 1] == 0xff && |
| 1136 | (pdata[nbytes - 2] & 0x80) != 0) |
| 1137 | --nbytes; |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 1138 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1139 | if (nbytes < 256) { |
| 1140 | c_str[0] = LONG1; |
| 1141 | c_str[1] = (char)nbytes; |
| 1142 | size = 2; |
| 1143 | } |
| 1144 | else { |
| 1145 | c_str[0] = LONG4; |
| 1146 | size = (int)nbytes; |
| 1147 | for (i = 1; i < 5; i++) { |
| 1148 | c_str[i] = (char)(size & 0xff); |
| 1149 | size >>= 8; |
| 1150 | } |
| 1151 | size = 5; |
| 1152 | } |
| 1153 | i = self->write_func(self, c_str, size); |
| 1154 | if (i < 0) goto finally; |
| 1155 | i = self->write_func(self, (char *)pdata, (int)nbytes); |
| 1156 | if (i < 0) goto finally; |
| 1157 | res = 0; |
| 1158 | goto finally; |
| 1159 | } |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 1160 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1161 | /* proto < 2: write the repr and newline. This is quadratic-time |
| 1162 | * (in the number of digits), in both directions. |
| 1163 | */ |
| 1164 | if (!( repr = PyObject_Repr(args))) |
| 1165 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1166 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1167 | if ((size = PyString_Size(repr)) < 0) |
| 1168 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1169 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1170 | if (self->write_func(self, &l, 1) < 0) |
| 1171 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1172 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1173 | if (self->write_func(self, |
| 1174 | PyString_AS_STRING((PyStringObject *)repr), |
| 1175 | size) < 0) |
| 1176 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1177 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1178 | if (self->write_func(self, "\n", 1) < 0) |
| 1179 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1180 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1181 | res = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1182 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1183 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1184 | Py_XDECREF(repr); |
| 1185 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | |
| 1189 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1190 | save_float(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1191 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1192 | double x = PyFloat_AS_DOUBLE((PyFloatObject *)args); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1193 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1194 | if (self->bin) { |
| 1195 | char str[9]; |
| 1196 | str[0] = BINFLOAT; |
| 1197 | if (_PyFloat_Pack8(x, (unsigned char *)&str[1], 0) < 0) |
| 1198 | return -1; |
| 1199 | if (self->write_func(self, str, 9) < 0) |
| 1200 | return -1; |
| 1201 | } |
| 1202 | else { |
| 1203 | int result = -1; |
| 1204 | char *buf = NULL; |
| 1205 | char op = FLOAT; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1206 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1207 | if (self->write_func(self, &op, 1) < 0) |
| 1208 | goto done; |
Eric Smith | b05d3be | 2009-10-26 15:06:39 +0000 | [diff] [blame] | 1209 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1210 | buf = PyOS_double_to_string(x, 'g', 17, 0, NULL); |
| 1211 | if (!buf) { |
| 1212 | PyErr_NoMemory(); |
| 1213 | goto done; |
| 1214 | } |
Eric Smith | b05d3be | 2009-10-26 15:06:39 +0000 | [diff] [blame] | 1215 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1216 | if (self->write_func(self, buf, strlen(buf)) < 0) |
| 1217 | goto done; |
Eric Smith | b05d3be | 2009-10-26 15:06:39 +0000 | [diff] [blame] | 1218 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1219 | if (self->write_func(self, "\n", 1) < 0) |
| 1220 | goto done; |
Eric Smith | b05d3be | 2009-10-26 15:06:39 +0000 | [diff] [blame] | 1221 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1222 | result = 0; |
Eric Smith | b05d3be | 2009-10-26 15:06:39 +0000 | [diff] [blame] | 1223 | done: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1224 | PyMem_Free(buf); |
| 1225 | return result; |
| 1226 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1227 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1228 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1229 | } |
| 1230 | |
| 1231 | |
| 1232 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1233 | save_string(Picklerobject *self, PyObject *args, int doput) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1234 | { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 1235 | Py_ssize_t size, len; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1236 | PyObject *repr=0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1237 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1238 | if ((size = PyString_Size(args)) < 0) |
| 1239 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1240 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1241 | if (!self->bin) { |
| 1242 | char *repr_str; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1243 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1244 | static char string = STRING; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1245 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1246 | if (!( repr = PyObject_Repr(args))) |
| 1247 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1248 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1249 | if ((len = PyString_Size(repr)) < 0) |
| 1250 | goto err; |
| 1251 | repr_str = PyString_AS_STRING((PyStringObject *)repr); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1252 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1253 | if (self->write_func(self, &string, 1) < 0) |
| 1254 | goto err; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1255 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1256 | if (self->write_func(self, repr_str, len) < 0) |
| 1257 | goto err; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1258 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1259 | if (self->write_func(self, "\n", 1) < 0) |
| 1260 | goto err; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1261 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1262 | Py_XDECREF(repr); |
| 1263 | } |
| 1264 | else { |
| 1265 | int i; |
| 1266 | char c_str[5]; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1267 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1268 | if (size < 256) { |
| 1269 | c_str[0] = SHORT_BINSTRING; |
| 1270 | c_str[1] = size; |
| 1271 | len = 2; |
| 1272 | } |
| 1273 | else if (size <= INT_MAX) { |
| 1274 | c_str[0] = BINSTRING; |
| 1275 | for (i = 1; i < 5; i++) |
| 1276 | c_str[i] = (int)(size >> ((i - 1) * 8)); |
| 1277 | len = 5; |
| 1278 | } |
| 1279 | else |
| 1280 | return -1; /* string too large */ |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1281 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1282 | if (self->write_func(self, c_str, len) < 0) |
| 1283 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1284 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1285 | if (size > 128 && Pdata_Check(self->file)) { |
| 1286 | if (write_other(self, NULL, 0) < 0) return -1; |
| 1287 | PDATA_APPEND(self->file, args, -1); |
| 1288 | } |
| 1289 | else { |
| 1290 | if (self->write_func(self, |
| 1291 | PyString_AS_STRING( |
| 1292 | (PyStringObject *)args), |
| 1293 | size) < 0) |
| 1294 | return -1; |
| 1295 | } |
| 1296 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1297 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1298 | if (doput) |
| 1299 | if (put(self, args) < 0) |
| 1300 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1301 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1302 | return 0; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 1303 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1304 | err: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1305 | Py_XDECREF(repr); |
| 1306 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
| 1309 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1310 | #ifdef Py_USING_UNICODE |
Guido van Rossum | fb10c3f | 2000-12-19 02:08:38 +0000 | [diff] [blame] | 1311 | /* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates |
| 1312 | backslash and newline characters to \uXXXX escapes. */ |
| 1313 | static PyObject * |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1314 | modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, Py_ssize_t size) |
Guido van Rossum | fb10c3f | 2000-12-19 02:08:38 +0000 | [diff] [blame] | 1315 | { |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1316 | PyObject *repr; |
| 1317 | char *p; |
| 1318 | char *q; |
Guido van Rossum | fb10c3f | 2000-12-19 02:08:38 +0000 | [diff] [blame] | 1319 | |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1320 | static const char *hexdigit = "0123456789abcdef"; |
| 1321 | #ifdef Py_UNICODE_WIDE |
| 1322 | const Py_ssize_t expandsize = 10; |
| 1323 | #else |
| 1324 | const Py_ssize_t expandsize = 6; |
| 1325 | #endif |
Guido van Rossum | fb10c3f | 2000-12-19 02:08:38 +0000 | [diff] [blame] | 1326 | |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1327 | if (size > PY_SSIZE_T_MAX / expandsize) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1328 | return PyErr_NoMemory(); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1329 | |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1330 | repr = PyString_FromStringAndSize(NULL, expandsize * size); |
| 1331 | if (repr == NULL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1332 | return NULL; |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1333 | if (size == 0) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1334 | return repr; |
Guido van Rossum | fb10c3f | 2000-12-19 02:08:38 +0000 | [diff] [blame] | 1335 | |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1336 | p = q = PyString_AS_STRING(repr); |
| 1337 | while (size-- > 0) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1338 | Py_UNICODE ch = *s++; |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1339 | #ifdef Py_UNICODE_WIDE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1340 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 1341 | if (ch >= 0x10000) { |
| 1342 | *p++ = '\\'; |
| 1343 | *p++ = 'U'; |
| 1344 | *p++ = hexdigit[(ch >> 28) & 0xf]; |
| 1345 | *p++ = hexdigit[(ch >> 24) & 0xf]; |
| 1346 | *p++ = hexdigit[(ch >> 20) & 0xf]; |
| 1347 | *p++ = hexdigit[(ch >> 16) & 0xf]; |
| 1348 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 1349 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 1350 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 1351 | *p++ = hexdigit[ch & 15]; |
| 1352 | } |
| 1353 | else |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1354 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1355 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 1356 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 1357 | Py_UNICODE ch2; |
| 1358 | Py_UCS4 ucs; |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1359 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1360 | ch2 = *s++; |
| 1361 | size--; |
| 1362 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 1363 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 1364 | *p++ = '\\'; |
| 1365 | *p++ = 'U'; |
| 1366 | *p++ = hexdigit[(ucs >> 28) & 0xf]; |
| 1367 | *p++ = hexdigit[(ucs >> 24) & 0xf]; |
| 1368 | *p++ = hexdigit[(ucs >> 20) & 0xf]; |
| 1369 | *p++ = hexdigit[(ucs >> 16) & 0xf]; |
| 1370 | *p++ = hexdigit[(ucs >> 12) & 0xf]; |
| 1371 | *p++ = hexdigit[(ucs >> 8) & 0xf]; |
| 1372 | *p++ = hexdigit[(ucs >> 4) & 0xf]; |
| 1373 | *p++ = hexdigit[ucs & 0xf]; |
| 1374 | continue; |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1375 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1376 | /* Fall through: isolated surrogates are copied as-is */ |
| 1377 | s--; |
| 1378 | size++; |
| 1379 | } |
| 1380 | #endif |
| 1381 | /* Map 16-bit characters to '\uxxxx' */ |
| 1382 | if (ch >= 256 || ch == '\\' || ch == '\n') { |
| 1383 | *p++ = '\\'; |
| 1384 | *p++ = 'u'; |
| 1385 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 1386 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 1387 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 1388 | *p++ = hexdigit[ch & 15]; |
| 1389 | } |
| 1390 | /* Copy everything else as-is */ |
| 1391 | else |
| 1392 | *p++ = (char) ch; |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1393 | } |
| 1394 | *p = '\0'; |
| 1395 | _PyString_Resize(&repr, p - q); |
| 1396 | return repr; |
| 1397 | } |
Guido van Rossum | fb10c3f | 2000-12-19 02:08:38 +0000 | [diff] [blame] | 1398 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1399 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1400 | save_unicode(Picklerobject *self, PyObject *args, int doput) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1401 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1402 | Py_ssize_t size, len; |
| 1403 | PyObject *repr=0; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1404 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1405 | if (!PyUnicode_Check(args)) |
| 1406 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1407 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1408 | if (!self->bin) { |
| 1409 | char *repr_str; |
| 1410 | static char string = UNICODE; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1411 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1412 | repr = modified_EncodeRawUnicodeEscape( |
| 1413 | PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args)); |
| 1414 | if (!repr) |
| 1415 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1416 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1417 | if ((len = PyString_Size(repr)) < 0) |
| 1418 | goto err; |
| 1419 | repr_str = PyString_AS_STRING((PyStringObject *)repr); |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1420 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1421 | if (self->write_func(self, &string, 1) < 0) |
| 1422 | goto err; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1423 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1424 | if (self->write_func(self, repr_str, len) < 0) |
| 1425 | goto err; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1426 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1427 | if (self->write_func(self, "\n", 1) < 0) |
| 1428 | goto err; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1429 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1430 | Py_XDECREF(repr); |
| 1431 | } |
| 1432 | else { |
| 1433 | int i; |
| 1434 | char c_str[5]; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1435 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1436 | if (!( repr = PyUnicode_AsUTF8String(args))) |
| 1437 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1438 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1439 | if ((size = PyString_Size(repr)) < 0) |
| 1440 | goto err; |
| 1441 | if (size > INT_MAX) |
| 1442 | return -1; /* string too large */ |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1443 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1444 | c_str[0] = BINUNICODE; |
| 1445 | for (i = 1; i < 5; i++) |
| 1446 | c_str[i] = (int)(size >> ((i - 1) * 8)); |
| 1447 | len = 5; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1448 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1449 | if (self->write_func(self, c_str, len) < 0) |
| 1450 | goto err; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1451 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1452 | if (size > 128 && Pdata_Check(self->file)) { |
| 1453 | if (write_other(self, NULL, 0) < 0) |
| 1454 | goto err; |
| 1455 | PDATA_APPEND(self->file, repr, -1); |
| 1456 | } |
| 1457 | else { |
| 1458 | if (self->write_func(self, PyString_AS_STRING(repr), |
| 1459 | size) < 0) |
| 1460 | goto err; |
| 1461 | } |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1462 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1463 | Py_DECREF(repr); |
| 1464 | } |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1465 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1466 | if (doput) |
| 1467 | if (put(self, args) < 0) |
| 1468 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1469 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1470 | return 0; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1471 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1472 | err: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1473 | Py_XDECREF(repr); |
| 1474 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1475 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1476 | #endif |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1477 | |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1478 | /* A helper for save_tuple. Push the len elements in tuple t on the stack. */ |
| 1479 | static int |
Tim Peters | 6792014 | 2003-02-05 03:46:17 +0000 | [diff] [blame] | 1480 | store_tuple_elements(Picklerobject *self, PyObject *t, int len) |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1481 | { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 1482 | Py_ssize_t i; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1483 | int res = -1; /* guilty until proved innocent */ |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1484 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1485 | assert(PyTuple_Size(t) == len); |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1486 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1487 | for (i = 0; i < len; i++) { |
| 1488 | PyObject *element = PyTuple_GET_ITEM(t, i); |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1489 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1490 | if (element == NULL) |
| 1491 | goto finally; |
| 1492 | if (save(self, element, 0) < 0) |
| 1493 | goto finally; |
| 1494 | } |
| 1495 | res = 0; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1496 | |
| 1497 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1498 | return res; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1499 | } |
| 1500 | |
| 1501 | /* Tuples are ubiquitous in the pickle protocols, so many techniques are |
| 1502 | * used across protocols to minimize the space needed to pickle them. |
Tim Peters | 6792014 | 2003-02-05 03:46:17 +0000 | [diff] [blame] | 1503 | * Tuples are also the only builtin immutable type that can be recursive |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1504 | * (a tuple can be reached from itself), and that requires some subtle |
| 1505 | * magic so that it works in all cases. IOW, this is a long routine. |
| 1506 | */ |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1507 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1508 | save_tuple(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1509 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1510 | PyObject *py_tuple_id = NULL; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 1511 | Py_ssize_t len, i; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1512 | int res = -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1513 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1514 | static char tuple = TUPLE; |
| 1515 | static char pop = POP; |
| 1516 | static char pop_mark = POP_MARK; |
| 1517 | static char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3}; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1518 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1519 | if ((len = PyTuple_Size(args)) < 0) |
| 1520 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1521 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1522 | if (len == 0) { |
| 1523 | char c_str[2]; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 1524 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1525 | if (self->proto) { |
| 1526 | c_str[0] = EMPTY_TUPLE; |
| 1527 | len = 1; |
| 1528 | } |
| 1529 | else { |
| 1530 | c_str[0] = MARK; |
| 1531 | c_str[1] = TUPLE; |
| 1532 | len = 2; |
| 1533 | } |
| 1534 | if (self->write_func(self, c_str, len) >= 0) |
| 1535 | res = 0; |
| 1536 | /* Don't memoize an empty tuple. */ |
| 1537 | goto finally; |
| 1538 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1539 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1540 | /* A non-empty tuple. */ |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1541 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1542 | /* id(tuple) isn't in the memo now. If it shows up there after |
| 1543 | * saving the tuple elements, the tuple must be recursive, in |
| 1544 | * which case we'll pop everything we put on the stack, and fetch |
| 1545 | * its value from the memo. |
| 1546 | */ |
| 1547 | py_tuple_id = PyLong_FromVoidPtr(args); |
| 1548 | if (py_tuple_id == NULL) |
| 1549 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1550 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1551 | if (len <= 3 && self->proto >= 2) { |
| 1552 | /* Use TUPLE{1,2,3} opcodes. */ |
| 1553 | if (store_tuple_elements(self, args, len) < 0) |
| 1554 | goto finally; |
| 1555 | if (PyDict_GetItem(self->memo, py_tuple_id)) { |
| 1556 | /* pop the len elements */ |
| 1557 | for (i = 0; i < len; ++i) |
| 1558 | if (self->write_func(self, &pop, 1) < 0) |
| 1559 | goto finally; |
| 1560 | /* fetch from memo */ |
| 1561 | if (get(self, py_tuple_id) < 0) |
| 1562 | goto finally; |
| 1563 | res = 0; |
| 1564 | goto finally; |
| 1565 | } |
| 1566 | /* Not recursive. */ |
| 1567 | if (self->write_func(self, len2opcode + len, 1) < 0) |
| 1568 | goto finally; |
| 1569 | goto memoize; |
| 1570 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1571 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1572 | /* proto < 2 and len > 0, or proto >= 2 and len > 3. |
| 1573 | * Generate MARK elt1 elt2 ... TUPLE |
| 1574 | */ |
| 1575 | if (self->write_func(self, &MARKv, 1) < 0) |
| 1576 | goto finally; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1577 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1578 | if (store_tuple_elements(self, args, len) < 0) |
| 1579 | goto finally; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1580 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1581 | if (PyDict_GetItem(self->memo, py_tuple_id)) { |
| 1582 | /* pop the stack stuff we pushed */ |
| 1583 | if (self->bin) { |
| 1584 | if (self->write_func(self, &pop_mark, 1) < 0) |
| 1585 | goto finally; |
| 1586 | } |
| 1587 | else { |
| 1588 | /* Note that we pop one more than len, to remove |
| 1589 | * the MARK too. |
| 1590 | */ |
| 1591 | for (i = 0; i <= len; i++) |
| 1592 | if (self->write_func(self, &pop, 1) < 0) |
| 1593 | goto finally; |
| 1594 | } |
| 1595 | /* fetch from memo */ |
| 1596 | if (get(self, py_tuple_id) >= 0) |
| 1597 | res = 0; |
| 1598 | goto finally; |
| 1599 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1600 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1601 | /* Not recursive. */ |
| 1602 | if (self->write_func(self, &tuple, 1) < 0) |
| 1603 | goto finally; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 1604 | |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1605 | memoize: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1606 | if (put(self, args) >= 0) |
| 1607 | res = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1608 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1609 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1610 | Py_XDECREF(py_tuple_id); |
| 1611 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1612 | } |
| 1613 | |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1614 | /* iter is an iterator giving items, and we batch up chunks of |
| 1615 | * MARK item item ... item APPENDS |
| 1616 | * opcode sequences. Calling code should have arranged to first create an |
| 1617 | * empty list, or list-like object, for the APPENDS to operate on. |
| 1618 | * Returns 0 on success, <0 on error. |
| 1619 | */ |
| 1620 | static int |
| 1621 | batch_list(Picklerobject *self, PyObject *iter) |
| 1622 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1623 | PyObject *obj = NULL; |
| 1624 | PyObject *firstitem = NULL; |
| 1625 | int i, n; |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1626 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1627 | static char append = APPEND; |
| 1628 | static char appends = APPENDS; |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1629 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1630 | assert(iter != NULL); |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1631 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1632 | if (self->proto == 0) { |
| 1633 | /* APPENDS isn't available; do one at a time. */ |
| 1634 | for (;;) { |
| 1635 | obj = PyIter_Next(iter); |
| 1636 | if (obj == NULL) { |
| 1637 | if (PyErr_Occurred()) |
| 1638 | return -1; |
| 1639 | break; |
| 1640 | } |
| 1641 | i = save(self, obj, 0); |
| 1642 | Py_DECREF(obj); |
| 1643 | if (i < 0) |
| 1644 | return -1; |
| 1645 | if (self->write_func(self, &append, 1) < 0) |
| 1646 | return -1; |
| 1647 | } |
| 1648 | return 0; |
| 1649 | } |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1650 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1651 | /* proto > 0: write in batches of BATCHSIZE. */ |
| 1652 | do { |
| 1653 | /* Get first item */ |
| 1654 | firstitem = PyIter_Next(iter); |
| 1655 | if (firstitem == NULL) { |
| 1656 | if (PyErr_Occurred()) |
| 1657 | goto BatchFailed; |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1658 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1659 | /* nothing more to add */ |
| 1660 | break; |
| 1661 | } |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1662 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1663 | /* Try to get a second item */ |
| 1664 | obj = PyIter_Next(iter); |
| 1665 | if (obj == NULL) { |
| 1666 | if (PyErr_Occurred()) |
| 1667 | goto BatchFailed; |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1668 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1669 | /* Only one item to write */ |
| 1670 | if (save(self, firstitem, 0) < 0) |
| 1671 | goto BatchFailed; |
| 1672 | if (self->write_func(self, &append, 1) < 0) |
| 1673 | goto BatchFailed; |
| 1674 | Py_CLEAR(firstitem); |
| 1675 | break; |
| 1676 | } |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1677 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1678 | /* More than one item to write */ |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1679 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1680 | /* Pump out MARK, items, APPENDS. */ |
| 1681 | if (self->write_func(self, &MARKv, 1) < 0) |
| 1682 | goto BatchFailed; |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1683 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1684 | if (save(self, firstitem, 0) < 0) |
| 1685 | goto BatchFailed; |
| 1686 | Py_CLEAR(firstitem); |
| 1687 | n = 1; |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1688 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1689 | /* Fetch and save up to BATCHSIZE items */ |
| 1690 | while (obj) { |
| 1691 | if (save(self, obj, 0) < 0) |
| 1692 | goto BatchFailed; |
| 1693 | Py_CLEAR(obj); |
| 1694 | n += 1; |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1695 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1696 | if (n == BATCHSIZE) |
| 1697 | break; |
| 1698 | |
| 1699 | obj = PyIter_Next(iter); |
| 1700 | if (obj == NULL) { |
| 1701 | if (PyErr_Occurred()) |
| 1702 | goto BatchFailed; |
| 1703 | break; |
| 1704 | } |
| 1705 | } |
| 1706 | |
| 1707 | if (self->write_func(self, &appends, 1) < 0) |
| 1708 | goto BatchFailed; |
| 1709 | |
| 1710 | } while (n == BATCHSIZE); |
| 1711 | return 0; |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1712 | |
| 1713 | BatchFailed: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1714 | Py_XDECREF(firstitem); |
| 1715 | Py_XDECREF(obj); |
| 1716 | return -1; |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1717 | } |
| 1718 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1719 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1720 | save_list(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1721 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1722 | int res = -1; |
| 1723 | char s[3]; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 1724 | Py_ssize_t len; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1725 | PyObject *iter; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1726 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1727 | if (self->fast && !fast_save_enter(self, args)) |
| 1728 | goto finally; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 1729 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1730 | /* Create an empty list. */ |
| 1731 | if (self->bin) { |
| 1732 | s[0] = EMPTY_LIST; |
| 1733 | len = 1; |
| 1734 | } |
| 1735 | else { |
| 1736 | s[0] = MARK; |
| 1737 | s[1] = LIST; |
| 1738 | len = 2; |
| 1739 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1740 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1741 | if (self->write_func(self, s, len) < 0) |
| 1742 | goto finally; |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1743 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1744 | /* Get list length, and bow out early if empty. */ |
| 1745 | if ((len = PyList_Size(args)) < 0) |
| 1746 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1747 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1748 | /* Memoize. */ |
| 1749 | if (len == 0) { |
| 1750 | if (put(self, args) >= 0) |
| 1751 | res = 0; |
| 1752 | goto finally; |
| 1753 | } |
| 1754 | if (put2(self, args) < 0) |
| 1755 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1756 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1757 | /* Materialize the list elements. */ |
| 1758 | iter = PyObject_GetIter(args); |
| 1759 | if (iter == NULL) |
| 1760 | goto finally; |
Facundo Batista | 763d309 | 2008-06-30 01:10:55 +0000 | [diff] [blame] | 1761 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1762 | if (Py_EnterRecursiveCall(" while pickling an object") == 0) |
| 1763 | { |
| 1764 | res = batch_list(self, iter); |
| 1765 | Py_LeaveRecursiveCall(); |
| 1766 | } |
| 1767 | Py_DECREF(iter); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1768 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1769 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1770 | if (self->fast && !fast_save_leave(self, args)) |
| 1771 | res = -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1772 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1773 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1774 | } |
| 1775 | |
| 1776 | |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1777 | /* iter is an iterator giving (key, value) pairs, and we batch up chunks of |
| 1778 | * MARK key value ... key value SETITEMS |
| 1779 | * opcode sequences. Calling code should have arranged to first create an |
| 1780 | * empty dict, or dict-like object, for the SETITEMS to operate on. |
| 1781 | * Returns 0 on success, <0 on error. |
| 1782 | * |
| 1783 | * This is very much like batch_list(). The difference between saving |
| 1784 | * elements directly, and picking apart two-tuples, is so long-winded at |
| 1785 | * the C level, though, that attempts to combine these routines were too |
| 1786 | * ugly to bear. |
| 1787 | */ |
| 1788 | static int |
| 1789 | batch_dict(Picklerobject *self, PyObject *iter) |
| 1790 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1791 | PyObject *p = NULL; |
| 1792 | PyObject *firstitem = NULL; |
| 1793 | int i, n; |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1794 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1795 | static char setitem = SETITEM; |
| 1796 | static char setitems = SETITEMS; |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1797 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1798 | assert(iter != NULL); |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1799 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1800 | if (self->proto == 0) { |
| 1801 | /* SETITEMS isn't available; do one at a time. */ |
| 1802 | for (;;) { |
| 1803 | p = PyIter_Next(iter); |
| 1804 | if (p == NULL) { |
| 1805 | if (PyErr_Occurred()) |
| 1806 | return -1; |
| 1807 | break; |
| 1808 | } |
| 1809 | if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) { |
| 1810 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 1811 | "iterator must return 2-tuples"); |
| 1812 | return -1; |
| 1813 | } |
| 1814 | i = save(self, PyTuple_GET_ITEM(p, 0), 0); |
| 1815 | if (i >= 0) |
| 1816 | i = save(self, PyTuple_GET_ITEM(p, 1), 0); |
| 1817 | Py_DECREF(p); |
| 1818 | if (i < 0) |
| 1819 | return -1; |
| 1820 | if (self->write_func(self, &setitem, 1) < 0) |
| 1821 | return -1; |
| 1822 | } |
| 1823 | return 0; |
| 1824 | } |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1825 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1826 | /* proto > 0: write in batches of BATCHSIZE. */ |
| 1827 | do { |
| 1828 | /* Get first item */ |
| 1829 | firstitem = PyIter_Next(iter); |
| 1830 | if (firstitem == NULL) { |
| 1831 | if (PyErr_Occurred()) |
| 1832 | goto BatchFailed; |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1833 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1834 | /* nothing more to add */ |
| 1835 | break; |
| 1836 | } |
| 1837 | if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) { |
| 1838 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 1839 | "iterator must return 2-tuples"); |
| 1840 | goto BatchFailed; |
| 1841 | } |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1842 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1843 | /* Try to get a second item */ |
| 1844 | p = PyIter_Next(iter); |
| 1845 | if (p == NULL) { |
| 1846 | if (PyErr_Occurred()) |
| 1847 | goto BatchFailed; |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1848 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1849 | /* Only one item to write */ |
| 1850 | if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0) |
| 1851 | goto BatchFailed; |
| 1852 | if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0) |
| 1853 | goto BatchFailed; |
| 1854 | if (self->write_func(self, &setitem, 1) < 0) |
| 1855 | goto BatchFailed; |
| 1856 | Py_CLEAR(firstitem); |
| 1857 | break; |
| 1858 | } |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1859 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1860 | /* More than one item to write */ |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1861 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1862 | /* Pump out MARK, items, SETITEMS. */ |
| 1863 | if (self->write_func(self, &MARKv, 1) < 0) |
| 1864 | goto BatchFailed; |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1865 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1866 | if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0) |
| 1867 | goto BatchFailed; |
| 1868 | if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0) |
| 1869 | goto BatchFailed; |
| 1870 | Py_CLEAR(firstitem); |
| 1871 | n = 1; |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1872 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1873 | /* Fetch and save up to BATCHSIZE items */ |
| 1874 | while (p) { |
| 1875 | if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) { |
| 1876 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 1877 | "iterator must return 2-tuples"); |
| 1878 | goto BatchFailed; |
| 1879 | } |
| 1880 | if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0) |
| 1881 | goto BatchFailed; |
| 1882 | if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0) |
| 1883 | goto BatchFailed; |
| 1884 | Py_CLEAR(p); |
| 1885 | n += 1; |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1886 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1887 | if (n == BATCHSIZE) |
| 1888 | break; |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1889 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1890 | p = PyIter_Next(iter); |
| 1891 | if (p == NULL) { |
| 1892 | if (PyErr_Occurred()) |
| 1893 | goto BatchFailed; |
| 1894 | break; |
| 1895 | } |
| 1896 | } |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1897 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1898 | if (self->write_func(self, &setitems, 1) < 0) |
| 1899 | goto BatchFailed; |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1900 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1901 | } while (n == BATCHSIZE); |
| 1902 | return 0; |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1903 | |
| 1904 | BatchFailed: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1905 | Py_XDECREF(firstitem); |
| 1906 | Py_XDECREF(p); |
| 1907 | return -1; |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1908 | } |
| 1909 | |
Collin Winter | 179bf21 | 2009-05-25 04:34:39 +0000 | [diff] [blame] | 1910 | /* This is a variant of batch_dict() above that specializes for dicts, with no |
| 1911 | * support for dict subclasses. Like batch_dict(), we batch up chunks of |
| 1912 | * MARK key value ... key value SETITEMS |
| 1913 | * opcode sequences. Calling code should have arranged to first create an |
| 1914 | * empty dict, or dict-like object, for the SETITEMS to operate on. |
| 1915 | * Returns 0 on success, -1 on error. |
| 1916 | * |
| 1917 | * Note that this currently doesn't work for protocol 0. |
| 1918 | */ |
| 1919 | static int |
| 1920 | batch_dict_exact(Picklerobject *self, PyObject *obj) |
| 1921 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1922 | PyObject *key = NULL, *value = NULL; |
| 1923 | int i; |
| 1924 | Py_ssize_t dict_size, ppos = 0; |
Collin Winter | 179bf21 | 2009-05-25 04:34:39 +0000 | [diff] [blame] | 1925 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1926 | static char setitem = SETITEM; |
| 1927 | static char setitems = SETITEMS; |
Collin Winter | 179bf21 | 2009-05-25 04:34:39 +0000 | [diff] [blame] | 1928 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1929 | assert(obj != NULL); |
| 1930 | assert(self->proto > 0); |
Collin Winter | 179bf21 | 2009-05-25 04:34:39 +0000 | [diff] [blame] | 1931 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1932 | dict_size = PyDict_Size(obj); |
Collin Winter | 179bf21 | 2009-05-25 04:34:39 +0000 | [diff] [blame] | 1933 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1934 | /* Special-case len(d) == 1 to save space. */ |
| 1935 | if (dict_size == 1) { |
| 1936 | PyDict_Next(obj, &ppos, &key, &value); |
| 1937 | if (save(self, key, 0) < 0) |
| 1938 | return -1; |
| 1939 | if (save(self, value, 0) < 0) |
| 1940 | return -1; |
| 1941 | if (self->write_func(self, &setitem, 1) < 0) |
| 1942 | return -1; |
| 1943 | return 0; |
| 1944 | } |
Collin Winter | 179bf21 | 2009-05-25 04:34:39 +0000 | [diff] [blame] | 1945 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1946 | /* Write in batches of BATCHSIZE. */ |
| 1947 | do { |
| 1948 | i = 0; |
| 1949 | if (self->write_func(self, &MARKv, 1) < 0) |
| 1950 | return -1; |
| 1951 | while (PyDict_Next(obj, &ppos, &key, &value)) { |
| 1952 | if (save(self, key, 0) < 0) |
| 1953 | return -1; |
| 1954 | if (save(self, value, 0) < 0) |
| 1955 | return -1; |
| 1956 | if (++i == BATCHSIZE) |
| 1957 | break; |
| 1958 | } |
| 1959 | if (self->write_func(self, &setitems, 1) < 0) |
| 1960 | return -1; |
| 1961 | if (PyDict_Size(obj) != dict_size) { |
| 1962 | PyErr_Format( |
| 1963 | PyExc_RuntimeError, |
| 1964 | "dictionary changed size during iteration"); |
| 1965 | return -1; |
| 1966 | } |
Collin Winter | 179bf21 | 2009-05-25 04:34:39 +0000 | [diff] [blame] | 1967 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1968 | } while (i == BATCHSIZE); |
| 1969 | return 0; |
Collin Winter | 179bf21 | 2009-05-25 04:34:39 +0000 | [diff] [blame] | 1970 | } |
| 1971 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1972 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1973 | save_dict(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1974 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1975 | int res = -1; |
| 1976 | char s[3]; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 1977 | Py_ssize_t len; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1978 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1979 | if (self->fast && !fast_save_enter(self, args)) |
| 1980 | goto finally; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 1981 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1982 | /* Create an empty dict. */ |
| 1983 | if (self->bin) { |
| 1984 | s[0] = EMPTY_DICT; |
| 1985 | len = 1; |
| 1986 | } |
| 1987 | else { |
| 1988 | s[0] = MARK; |
| 1989 | s[1] = DICT; |
| 1990 | len = 2; |
| 1991 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1992 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1993 | if (self->write_func(self, s, len) < 0) |
| 1994 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1995 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1996 | /* Get dict size, and bow out early if empty. */ |
| 1997 | if ((len = PyDict_Size(args)) < 0) |
| 1998 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1999 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2000 | if (len == 0) { |
| 2001 | if (put(self, args) >= 0) |
| 2002 | res = 0; |
| 2003 | goto finally; |
| 2004 | } |
| 2005 | if (put2(self, args) < 0) |
| 2006 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2007 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2008 | /* Materialize the dict items. */ |
| 2009 | if (PyDict_CheckExact(args) && self->proto > 0) { |
| 2010 | /* We can take certain shortcuts if we know this is a dict and |
| 2011 | not a dict subclass. */ |
| 2012 | if (Py_EnterRecursiveCall(" while pickling an object") == 0) { |
| 2013 | res = batch_dict_exact(self, args); |
| 2014 | Py_LeaveRecursiveCall(); |
| 2015 | } |
| 2016 | } else { |
| 2017 | PyObject *iter = PyObject_CallMethod(args, "iteritems", "()"); |
| 2018 | if (iter == NULL) |
| 2019 | goto finally; |
| 2020 | if (Py_EnterRecursiveCall(" while pickling an object") == 0) { |
| 2021 | res = batch_dict(self, iter); |
| 2022 | Py_LeaveRecursiveCall(); |
| 2023 | } |
| 2024 | Py_DECREF(iter); |
| 2025 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2026 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2027 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2028 | if (self->fast && !fast_save_leave(self, args)) |
| 2029 | res = -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2030 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2031 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2032 | } |
| 2033 | |
| 2034 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2035 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2036 | save_inst(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2037 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2038 | PyObject *class = 0, *module = 0, *name = 0, *state = 0, |
| 2039 | *getinitargs_func = 0, *getstate_func = 0, *class_args = 0; |
| 2040 | char *module_str, *name_str; |
| 2041 | int module_size, name_size, res = -1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2042 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2043 | static char inst = INST, obj = OBJ, build = BUILD; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2044 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2045 | if (self->fast && !fast_save_enter(self, args)) |
| 2046 | goto finally; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 2047 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2048 | if (self->write_func(self, &MARKv, 1) < 0) |
| 2049 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2050 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2051 | if (!( class = PyObject_GetAttr(args, __class___str))) |
| 2052 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2053 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2054 | if (self->bin) { |
| 2055 | if (save(self, class, 0) < 0) |
| 2056 | goto finally; |
| 2057 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2058 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2059 | if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) { |
| 2060 | PyObject *element = 0; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 2061 | Py_ssize_t i, len; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2062 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2063 | if (!( class_args = |
| 2064 | PyObject_Call(getinitargs_func, empty_tuple, NULL))) |
| 2065 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2066 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2067 | if ((len = PyObject_Size(class_args)) < 0) |
| 2068 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2069 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2070 | for (i = 0; i < len; i++) { |
| 2071 | if (!( element = PySequence_GetItem(class_args, i))) |
| 2072 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2073 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2074 | if (save(self, element, 0) < 0) { |
| 2075 | Py_DECREF(element); |
| 2076 | goto finally; |
| 2077 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2078 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2079 | Py_DECREF(element); |
| 2080 | } |
| 2081 | } |
| 2082 | else { |
| 2083 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 2084 | PyErr_Clear(); |
| 2085 | else |
| 2086 | goto finally; |
| 2087 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2088 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2089 | if (!self->bin) { |
| 2090 | if (!( name = ((PyClassObject *)class)->cl_name )) { |
| 2091 | PyErr_SetString(PicklingError, "class has no name"); |
| 2092 | goto finally; |
| 2093 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2094 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2095 | if (!( module = whichmodule(class, name))) |
| 2096 | goto finally; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 2097 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2098 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2099 | if ((module_size = PyString_Size(module)) < 0 || |
| 2100 | (name_size = PyString_Size(name)) < 0) |
| 2101 | goto finally; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 2102 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2103 | module_str = PyString_AS_STRING((PyStringObject *)module); |
| 2104 | name_str = PyString_AS_STRING((PyStringObject *)name); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2105 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2106 | if (self->write_func(self, &inst, 1) < 0) |
| 2107 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2108 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2109 | if (self->write_func(self, module_str, module_size) < 0) |
| 2110 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2111 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2112 | if (self->write_func(self, "\n", 1) < 0) |
| 2113 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2114 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2115 | if (self->write_func(self, name_str, name_size) < 0) |
| 2116 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2117 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2118 | if (self->write_func(self, "\n", 1) < 0) |
| 2119 | goto finally; |
| 2120 | } |
| 2121 | else if (self->write_func(self, &obj, 1) < 0) { |
| 2122 | goto finally; |
| 2123 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2124 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2125 | if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) { |
| 2126 | state = PyObject_Call(getstate_func, empty_tuple, NULL); |
| 2127 | if (!state) |
| 2128 | goto finally; |
| 2129 | } |
| 2130 | else { |
| 2131 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 2132 | PyErr_Clear(); |
| 2133 | else |
| 2134 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2135 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2136 | if (!( state = PyObject_GetAttr(args, __dict___str))) { |
| 2137 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 2138 | PyErr_Clear(); |
| 2139 | else |
| 2140 | goto finally; |
| 2141 | res = 0; |
| 2142 | goto finally; |
| 2143 | } |
| 2144 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2145 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2146 | if (!PyDict_Check(state)) { |
| 2147 | if (put2(self, args) < 0) |
| 2148 | goto finally; |
| 2149 | } |
| 2150 | else { |
| 2151 | if (put(self, args) < 0) |
| 2152 | goto finally; |
| 2153 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2154 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2155 | if (save(self, state, 0) < 0) |
| 2156 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2157 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2158 | if (self->write_func(self, &build, 1) < 0) |
| 2159 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2160 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2161 | res = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2162 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2163 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2164 | if (self->fast && !fast_save_leave(self, args)) |
| 2165 | res = -1; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 2166 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2167 | Py_XDECREF(module); |
| 2168 | Py_XDECREF(class); |
| 2169 | Py_XDECREF(state); |
| 2170 | Py_XDECREF(getinitargs_func); |
| 2171 | Py_XDECREF(getstate_func); |
| 2172 | Py_XDECREF(class_args); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2173 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2174 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2175 | } |
| 2176 | |
| 2177 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2178 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2179 | save_global(Picklerobject *self, PyObject *args, PyObject *name) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2180 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2181 | PyObject *global_name = 0, *module = 0, *mod = 0, *klass = 0; |
| 2182 | char *name_str, *module_str; |
| 2183 | int module_size, name_size, res = -1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2184 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2185 | static char global = GLOBAL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2186 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2187 | if (name) { |
| 2188 | global_name = name; |
| 2189 | Py_INCREF(global_name); |
| 2190 | } |
| 2191 | else { |
| 2192 | if (!( global_name = PyObject_GetAttr(args, __name___str))) |
| 2193 | goto finally; |
| 2194 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2195 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2196 | if (!( module = whichmodule(args, global_name))) |
| 2197 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2198 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2199 | if ((module_size = PyString_Size(module)) < 0 || |
| 2200 | (name_size = PyString_Size(global_name)) < 0) |
| 2201 | goto finally; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 2202 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2203 | module_str = PyString_AS_STRING((PyStringObject *)module); |
| 2204 | name_str = PyString_AS_STRING((PyStringObject *)global_name); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2205 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2206 | /* XXX This can be doing a relative import. Clearly it shouldn't, |
| 2207 | but I don't know how to stop it. :-( */ |
| 2208 | mod = PyImport_ImportModule(module_str); |
| 2209 | if (mod == NULL) { |
| 2210 | cPickle_ErrFormat(PicklingError, |
| 2211 | "Can't pickle %s: import of module %s " |
| 2212 | "failed", |
| 2213 | "OS", args, module); |
| 2214 | goto finally; |
| 2215 | } |
| 2216 | klass = PyObject_GetAttrString(mod, name_str); |
| 2217 | if (klass == NULL) { |
| 2218 | cPickle_ErrFormat(PicklingError, |
| 2219 | "Can't pickle %s: attribute lookup %s.%s " |
| 2220 | "failed", |
| 2221 | "OSS", args, module, global_name); |
| 2222 | goto finally; |
| 2223 | } |
| 2224 | if (klass != args) { |
| 2225 | Py_DECREF(klass); |
| 2226 | cPickle_ErrFormat(PicklingError, |
| 2227 | "Can't pickle %s: it's not the same object " |
| 2228 | "as %s.%s", |
| 2229 | "OSS", args, module, global_name); |
| 2230 | goto finally; |
| 2231 | } |
| 2232 | Py_DECREF(klass); |
Guido van Rossum | a92d16a | 2001-08-18 21:22:07 +0000 | [diff] [blame] | 2233 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2234 | if (self->proto >= 2) { |
| 2235 | /* See whether this is in the extension registry, and if |
| 2236 | * so generate an EXT opcode. |
| 2237 | */ |
| 2238 | PyObject *py_code; /* extension code as Python object */ |
| 2239 | long code; /* extension code as C value */ |
| 2240 | char c_str[5]; |
| 2241 | int n; |
Tim Peters | 731098b | 2003-02-04 20:56:09 +0000 | [diff] [blame] | 2242 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2243 | PyTuple_SET_ITEM(two_tuple, 0, module); |
| 2244 | PyTuple_SET_ITEM(two_tuple, 1, global_name); |
| 2245 | py_code = PyDict_GetItem(extension_registry, two_tuple); |
| 2246 | if (py_code == NULL) |
| 2247 | goto gen_global; /* not registered */ |
Tim Peters | 731098b | 2003-02-04 20:56:09 +0000 | [diff] [blame] | 2248 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2249 | /* Verify py_code has the right type and value. */ |
| 2250 | if (!PyInt_Check(py_code)) { |
| 2251 | cPickle_ErrFormat(PicklingError, "Can't pickle %s: " |
| 2252 | "extension code %s isn't an integer", |
| 2253 | "OO", args, py_code); |
| 2254 | goto finally; |
| 2255 | } |
| 2256 | code = PyInt_AS_LONG(py_code); |
| 2257 | if (code <= 0 || code > 0x7fffffffL) { |
| 2258 | cPickle_ErrFormat(PicklingError, "Can't pickle %s: " |
| 2259 | "extension code %ld is out of range", |
| 2260 | "Ol", args, code); |
| 2261 | goto finally; |
| 2262 | } |
Tim Peters | 731098b | 2003-02-04 20:56:09 +0000 | [diff] [blame] | 2263 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2264 | /* Generate an EXT opcode. */ |
| 2265 | if (code <= 0xff) { |
| 2266 | c_str[0] = EXT1; |
| 2267 | c_str[1] = (char)code; |
| 2268 | n = 2; |
| 2269 | } |
| 2270 | else if (code <= 0xffff) { |
| 2271 | c_str[0] = EXT2; |
| 2272 | c_str[1] = (char)(code & 0xff); |
| 2273 | c_str[2] = (char)((code >> 8) & 0xff); |
| 2274 | n = 3; |
| 2275 | } |
| 2276 | else { |
| 2277 | c_str[0] = EXT4; |
| 2278 | c_str[1] = (char)(code & 0xff); |
| 2279 | c_str[2] = (char)((code >> 8) & 0xff); |
| 2280 | c_str[3] = (char)((code >> 16) & 0xff); |
| 2281 | c_str[4] = (char)((code >> 24) & 0xff); |
| 2282 | n = 5; |
| 2283 | } |
Tim Peters | 731098b | 2003-02-04 20:56:09 +0000 | [diff] [blame] | 2284 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2285 | if (self->write_func(self, c_str, n) >= 0) |
| 2286 | res = 0; |
| 2287 | goto finally; /* and don't memoize */ |
| 2288 | } |
Tim Peters | 731098b | 2003-02-04 20:56:09 +0000 | [diff] [blame] | 2289 | |
| 2290 | gen_global: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2291 | if (self->write_func(self, &global, 1) < 0) |
| 2292 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2293 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2294 | if (self->write_func(self, module_str, module_size) < 0) |
| 2295 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2296 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2297 | if (self->write_func(self, "\n", 1) < 0) |
| 2298 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2299 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2300 | if (self->write_func(self, name_str, name_size) < 0) |
| 2301 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2302 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2303 | if (self->write_func(self, "\n", 1) < 0) |
| 2304 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2305 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2306 | if (put(self, args) < 0) |
| 2307 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2308 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2309 | res = 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2310 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2311 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2312 | Py_XDECREF(module); |
| 2313 | Py_XDECREF(global_name); |
| 2314 | Py_XDECREF(mod); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2315 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2316 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2317 | } |
| 2318 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2319 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2320 | save_pers(Picklerobject *self, PyObject *args, PyObject *f) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2321 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2322 | PyObject *pid = 0; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 2323 | Py_ssize_t size; |
| 2324 | int res = -1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2325 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2326 | static char persid = PERSID, binpersid = BINPERSID; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2327 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2328 | Py_INCREF(args); |
| 2329 | ARG_TUP(self, args); |
| 2330 | if (self->arg) { |
| 2331 | pid = PyObject_Call(f, self->arg, NULL); |
| 2332 | FREE_ARG_TUP(self); |
| 2333 | } |
| 2334 | if (! pid) return -1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2335 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2336 | if (pid != Py_None) { |
| 2337 | if (!self->bin) { |
| 2338 | if (!PyString_Check(pid)) { |
| 2339 | PyErr_SetString(PicklingError, |
| 2340 | "persistent id must be string"); |
| 2341 | goto finally; |
| 2342 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2343 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2344 | if (self->write_func(self, &persid, 1) < 0) |
| 2345 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2346 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2347 | if ((size = PyString_Size(pid)) < 0) |
| 2348 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2349 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2350 | if (self->write_func(self, |
| 2351 | PyString_AS_STRING( |
| 2352 | (PyStringObject *)pid), |
| 2353 | size) < 0) |
| 2354 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2355 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2356 | if (self->write_func(self, "\n", 1) < 0) |
| 2357 | goto finally; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2358 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2359 | res = 1; |
| 2360 | goto finally; |
| 2361 | } |
| 2362 | else if (save(self, pid, 1) >= 0) { |
| 2363 | if (self->write_func(self, &binpersid, 1) < 0) |
| 2364 | res = -1; |
| 2365 | else |
| 2366 | res = 1; |
| 2367 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2368 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2369 | goto finally; |
| 2370 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2371 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2372 | res = 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2373 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2374 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2375 | Py_XDECREF(pid); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2376 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2377 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2378 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2379 | |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2380 | /* We're saving ob, and args is the 2-thru-5 tuple returned by the |
| 2381 | * appropriate __reduce__ method for ob. |
| 2382 | */ |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2383 | static int |
Amaury Forgeot d'Arc | 69a9c5b | 2008-10-30 21:18:34 +0000 | [diff] [blame] | 2384 | save_reduce(Picklerobject *self, PyObject *args, PyObject *fn, PyObject *ob) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2385 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2386 | PyObject *callable; |
| 2387 | PyObject *argtup; |
| 2388 | PyObject *state = NULL; |
| 2389 | PyObject *listitems = Py_None; |
| 2390 | PyObject *dictitems = Py_None; |
| 2391 | Py_ssize_t size; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2392 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2393 | int use_newobj = self->proto >= 2; |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2394 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2395 | static char reduce = REDUCE; |
| 2396 | static char build = BUILD; |
| 2397 | static char newobj = NEWOBJ; |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2398 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2399 | size = PyTuple_Size(args); |
| 2400 | if (size < 2 || size > 5) { |
| 2401 | cPickle_ErrFormat(PicklingError, "tuple returned by " |
| 2402 | "%s must contain 2 through 5 elements", |
| 2403 | "O", fn); |
| 2404 | return -1; |
| 2405 | } |
Amaury Forgeot d'Arc | 69a9c5b | 2008-10-30 21:18:34 +0000 | [diff] [blame] | 2406 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2407 | if (! PyArg_UnpackTuple(args, "save_reduce", 2, 5, |
| 2408 | &callable, |
| 2409 | &argtup, |
| 2410 | &state, |
| 2411 | &listitems, |
| 2412 | &dictitems)) |
| 2413 | return -1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2414 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2415 | if (!PyTuple_Check(argtup)) { |
| 2416 | cPickle_ErrFormat(PicklingError, "Second element of " |
| 2417 | "tuple returned by %s must be a tuple", |
| 2418 | "O", fn); |
| 2419 | return -1; |
| 2420 | } |
Raymond Hettinger | a6b45cc | 2004-12-07 07:05:57 +0000 | [diff] [blame] | 2421 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2422 | if (state == Py_None) |
| 2423 | state = NULL; |
Amaury Forgeot d'Arc | 69a9c5b | 2008-10-30 21:18:34 +0000 | [diff] [blame] | 2424 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2425 | if (listitems == Py_None) |
| 2426 | listitems = NULL; |
| 2427 | else if (!PyIter_Check(listitems)) { |
| 2428 | cPickle_ErrFormat(PicklingError, "Fourth element of " |
| 2429 | "tuple returned by %s must be an iterator, not %s", |
| 2430 | "Os", fn, Py_TYPE(listitems)->tp_name); |
| 2431 | return -1; |
| 2432 | } |
Amaury Forgeot d'Arc | 69a9c5b | 2008-10-30 21:18:34 +0000 | [diff] [blame] | 2433 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2434 | if (dictitems == Py_None) |
| 2435 | dictitems = NULL; |
| 2436 | else if (!PyIter_Check(dictitems)) { |
| 2437 | cPickle_ErrFormat(PicklingError, "Fifth element of " |
| 2438 | "tuple returned by %s must be an iterator, not %s", |
| 2439 | "Os", fn, Py_TYPE(dictitems)->tp_name); |
| 2440 | return -1; |
| 2441 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2442 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2443 | /* Protocol 2 special case: if callable's name is __newobj__, use |
| 2444 | * NEWOBJ. This consumes a lot of code. |
| 2445 | */ |
| 2446 | if (use_newobj) { |
| 2447 | PyObject *temp = PyObject_GetAttr(callable, __name___str); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2448 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2449 | if (temp == NULL) { |
| 2450 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 2451 | PyErr_Clear(); |
| 2452 | else |
| 2453 | return -1; |
| 2454 | use_newobj = 0; |
| 2455 | } |
| 2456 | else { |
| 2457 | use_newobj = PyString_Check(temp) && |
| 2458 | strcmp(PyString_AS_STRING(temp), |
| 2459 | "__newobj__") == 0; |
| 2460 | Py_DECREF(temp); |
| 2461 | } |
| 2462 | } |
| 2463 | if (use_newobj) { |
| 2464 | PyObject *cls; |
| 2465 | PyObject *newargtup; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 2466 | Py_ssize_t n, i; |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2467 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2468 | /* Sanity checks. */ |
| 2469 | n = PyTuple_Size(argtup); |
| 2470 | if (n < 1) { |
| 2471 | PyErr_SetString(PicklingError, "__newobj__ arglist " |
| 2472 | "is empty"); |
| 2473 | return -1; |
| 2474 | } |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2475 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2476 | cls = PyTuple_GET_ITEM(argtup, 0); |
| 2477 | if (! PyObject_HasAttrString(cls, "__new__")) { |
| 2478 | PyErr_SetString(PicklingError, "args[0] from " |
| 2479 | "__newobj__ args has no __new__"); |
| 2480 | return -1; |
| 2481 | } |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2482 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2483 | /* XXX How could ob be NULL? */ |
| 2484 | if (ob != NULL) { |
| 2485 | PyObject *ob_dot_class; |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2486 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2487 | ob_dot_class = PyObject_GetAttr(ob, __class___str); |
| 2488 | if (ob_dot_class == NULL) { |
| 2489 | if (PyErr_ExceptionMatches( |
| 2490 | PyExc_AttributeError)) |
| 2491 | PyErr_Clear(); |
| 2492 | else |
| 2493 | return -1; |
| 2494 | } |
| 2495 | i = ob_dot_class != cls; /* true iff a problem */ |
| 2496 | Py_XDECREF(ob_dot_class); |
| 2497 | if (i) { |
| 2498 | PyErr_SetString(PicklingError, "args[0] from " |
| 2499 | "__newobj__ args has the wrong class"); |
| 2500 | return -1; |
| 2501 | } |
| 2502 | } |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2503 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2504 | /* Save the class and its __new__ arguments. */ |
| 2505 | if (save(self, cls, 0) < 0) |
| 2506 | return -1; |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2507 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2508 | newargtup = PyTuple_New(n-1); /* argtup[1:] */ |
| 2509 | if (newargtup == NULL) |
| 2510 | return -1; |
| 2511 | for (i = 1; i < n; ++i) { |
| 2512 | PyObject *temp = PyTuple_GET_ITEM(argtup, i); |
| 2513 | Py_INCREF(temp); |
| 2514 | PyTuple_SET_ITEM(newargtup, i-1, temp); |
| 2515 | } |
| 2516 | i = save(self, newargtup, 0); |
| 2517 | Py_DECREF(newargtup); |
| 2518 | if (i < 0) |
| 2519 | return -1; |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2520 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2521 | /* Add NEWOBJ opcode. */ |
| 2522 | if (self->write_func(self, &newobj, 1) < 0) |
| 2523 | return -1; |
| 2524 | } |
| 2525 | else { |
| 2526 | /* Not using NEWOBJ. */ |
| 2527 | if (save(self, callable, 0) < 0 || |
| 2528 | save(self, argtup, 0) < 0 || |
| 2529 | self->write_func(self, &reduce, 1) < 0) |
| 2530 | return -1; |
| 2531 | } |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2532 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2533 | /* Memoize. */ |
| 2534 | /* XXX How can ob be NULL? */ |
| 2535 | if (ob != NULL) { |
| 2536 | if (state && !PyDict_Check(state)) { |
| 2537 | if (put2(self, ob) < 0) |
| 2538 | return -1; |
| 2539 | } |
| 2540 | else if (put(self, ob) < 0) |
| 2541 | return -1; |
| 2542 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2543 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2544 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2545 | if (listitems && batch_list(self, listitems) < 0) |
| 2546 | return -1; |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2547 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2548 | if (dictitems && batch_dict(self, dictitems) < 0) |
| 2549 | return -1; |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2550 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2551 | if (state) { |
| 2552 | if (save(self, state, 0) < 0 || |
| 2553 | self->write_func(self, &build, 1) < 0) |
| 2554 | return -1; |
| 2555 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2556 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2557 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2558 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2559 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2560 | static int |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 2561 | save(Picklerobject *self, PyObject *args, int pers_save) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2562 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2563 | PyTypeObject *type; |
| 2564 | PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0; |
| 2565 | int res = -1; |
| 2566 | int tmp; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2567 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2568 | if (Py_EnterRecursiveCall(" while pickling an object")) |
| 2569 | return -1; |
Martin v. Löwis | 5a39530 | 2002-08-04 08:20:23 +0000 | [diff] [blame] | 2570 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2571 | if (!pers_save && self->pers_func) { |
| 2572 | if ((tmp = save_pers(self, args, self->pers_func)) != 0) { |
| 2573 | res = tmp; |
| 2574 | goto finally; |
| 2575 | } |
| 2576 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2577 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2578 | if (args == Py_None) { |
| 2579 | res = save_none(self, args); |
| 2580 | goto finally; |
| 2581 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2582 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2583 | type = Py_TYPE(args); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2584 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2585 | switch (type->tp_name[0]) { |
| 2586 | case 'b': |
| 2587 | if (args == Py_False || args == Py_True) { |
| 2588 | res = save_bool(self, args); |
| 2589 | goto finally; |
| 2590 | } |
| 2591 | break; |
| 2592 | case 'i': |
| 2593 | if (type == &PyInt_Type) { |
| 2594 | res = save_int(self, args); |
| 2595 | goto finally; |
| 2596 | } |
| 2597 | break; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2598 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2599 | case 'l': |
| 2600 | if (type == &PyLong_Type) { |
| 2601 | res = save_long(self, args); |
| 2602 | goto finally; |
| 2603 | } |
| 2604 | break; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2605 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2606 | case 'f': |
| 2607 | if (type == &PyFloat_Type) { |
| 2608 | res = save_float(self, args); |
| 2609 | goto finally; |
| 2610 | } |
| 2611 | break; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2612 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2613 | case 't': |
| 2614 | if (type == &PyTuple_Type && PyTuple_Size(args) == 0) { |
| 2615 | res = save_tuple(self, args); |
| 2616 | goto finally; |
| 2617 | } |
| 2618 | break; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2619 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2620 | case 's': |
| 2621 | if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) { |
| 2622 | res = save_string(self, args, 0); |
| 2623 | goto finally; |
| 2624 | } |
| 2625 | break; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 2626 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2627 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2628 | case 'u': |
| 2629 | if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) { |
| 2630 | res = save_unicode(self, args, 0); |
| 2631 | goto finally; |
| 2632 | } |
| 2633 | break; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2634 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2635 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2636 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2637 | if (Py_REFCNT(args) > 1) { |
| 2638 | if (!( py_ob_id = PyLong_FromVoidPtr(args))) |
| 2639 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2640 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2641 | if (PyDict_GetItem(self->memo, py_ob_id)) { |
| 2642 | if (get(self, py_ob_id) < 0) |
| 2643 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2644 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2645 | res = 0; |
| 2646 | goto finally; |
| 2647 | } |
| 2648 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2649 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2650 | switch (type->tp_name[0]) { |
| 2651 | case 's': |
| 2652 | if (type == &PyString_Type) { |
| 2653 | res = save_string(self, args, 1); |
| 2654 | goto finally; |
| 2655 | } |
| 2656 | break; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2657 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2658 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2659 | case 'u': |
| 2660 | if (type == &PyUnicode_Type) { |
| 2661 | res = save_unicode(self, args, 1); |
| 2662 | goto finally; |
| 2663 | } |
| 2664 | break; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2665 | #endif |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 2666 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2667 | case 't': |
| 2668 | if (type == &PyTuple_Type) { |
| 2669 | res = save_tuple(self, args); |
| 2670 | goto finally; |
| 2671 | } |
| 2672 | if (type == &PyType_Type) { |
Alexandre Vassalotti | df9460f | 2013-11-30 17:43:42 -0800 | [diff] [blame] | 2673 | res = save_global(self, args, NULL); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2674 | goto finally; |
| 2675 | } |
| 2676 | break; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2677 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2678 | case 'l': |
| 2679 | if (type == &PyList_Type) { |
| 2680 | res = save_list(self, args); |
| 2681 | goto finally; |
| 2682 | } |
| 2683 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2684 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2685 | case 'd': |
| 2686 | if (type == &PyDict_Type) { |
| 2687 | res = save_dict(self, args); |
| 2688 | goto finally; |
| 2689 | } |
| 2690 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2691 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2692 | case 'i': |
| 2693 | if (type == &PyInstance_Type) { |
| 2694 | res = save_inst(self, args); |
| 2695 | goto finally; |
| 2696 | } |
| 2697 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2698 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2699 | case 'c': |
| 2700 | if (type == &PyClass_Type) { |
| 2701 | res = save_global(self, args, NULL); |
| 2702 | goto finally; |
| 2703 | } |
| 2704 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2705 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2706 | case 'f': |
| 2707 | if (type == &PyFunction_Type) { |
| 2708 | res = save_global(self, args, NULL); |
| 2709 | if (res && PyErr_ExceptionMatches(PickleError)) { |
| 2710 | /* fall back to reduce */ |
| 2711 | PyErr_Clear(); |
| 2712 | break; |
| 2713 | } |
| 2714 | goto finally; |
| 2715 | } |
| 2716 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2717 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2718 | case 'b': |
| 2719 | if (type == &PyCFunction_Type) { |
| 2720 | res = save_global(self, args, NULL); |
| 2721 | goto finally; |
| 2722 | } |
| 2723 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2724 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2725 | if (!pers_save && self->inst_pers_func) { |
| 2726 | if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) { |
| 2727 | res = tmp; |
| 2728 | goto finally; |
| 2729 | } |
| 2730 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2731 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2732 | /* Get a reduction callable, and call it. This may come from |
| 2733 | * copy_reg.dispatch_table, the object's __reduce_ex__ method, |
| 2734 | * or the object's __reduce__ method. |
| 2735 | */ |
| 2736 | __reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type); |
| 2737 | if (__reduce__ != NULL) { |
| 2738 | Py_INCREF(__reduce__); |
| 2739 | Py_INCREF(args); |
| 2740 | ARG_TUP(self, args); |
| 2741 | if (self->arg) { |
| 2742 | t = PyObject_Call(__reduce__, self->arg, NULL); |
| 2743 | FREE_ARG_TUP(self); |
| 2744 | } |
| 2745 | } |
| 2746 | else { |
Antoine Pitrou | 561a821 | 2011-10-04 09:34:48 +0200 | [diff] [blame] | 2747 | if (PyType_IsSubtype(type, &PyType_Type)) { |
| 2748 | res = save_global(self, args, NULL); |
| 2749 | goto finally; |
| 2750 | } |
| 2751 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2752 | /* Check for a __reduce_ex__ method. */ |
| 2753 | __reduce__ = PyObject_GetAttr(args, __reduce_ex___str); |
| 2754 | if (__reduce__ != NULL) { |
| 2755 | t = PyInt_FromLong(self->proto); |
| 2756 | if (t != NULL) { |
| 2757 | ARG_TUP(self, t); |
| 2758 | t = NULL; |
| 2759 | if (self->arg) { |
| 2760 | t = PyObject_Call(__reduce__, |
| 2761 | self->arg, NULL); |
| 2762 | FREE_ARG_TUP(self); |
| 2763 | } |
| 2764 | } |
| 2765 | } |
| 2766 | else { |
| 2767 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 2768 | PyErr_Clear(); |
| 2769 | else |
| 2770 | goto finally; |
| 2771 | /* Check for a __reduce__ method. */ |
| 2772 | __reduce__ = PyObject_GetAttr(args, __reduce___str); |
| 2773 | if (__reduce__ != NULL) { |
| 2774 | t = PyObject_Call(__reduce__, |
| 2775 | empty_tuple, NULL); |
| 2776 | } |
| 2777 | else { |
| 2778 | PyErr_SetObject(UnpickleableError, args); |
| 2779 | goto finally; |
| 2780 | } |
| 2781 | } |
| 2782 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2783 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2784 | if (t == NULL) |
| 2785 | goto finally; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2786 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2787 | if (PyString_Check(t)) { |
| 2788 | res = save_global(self, args, t); |
| 2789 | goto finally; |
| 2790 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2791 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2792 | if (!PyTuple_Check(t)) { |
| 2793 | cPickle_ErrFormat(PicklingError, "Value returned by " |
| 2794 | "%s must be string or tuple", |
| 2795 | "O", __reduce__); |
| 2796 | goto finally; |
| 2797 | } |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2798 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2799 | res = save_reduce(self, t, __reduce__, args); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2800 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2801 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2802 | Py_LeaveRecursiveCall(); |
| 2803 | Py_XDECREF(py_ob_id); |
| 2804 | Py_XDECREF(__reduce__); |
| 2805 | Py_XDECREF(t); |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2806 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2807 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2808 | } |
| 2809 | |
| 2810 | |
| 2811 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2812 | dump(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2813 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2814 | static char stop = STOP; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2815 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2816 | if (self->proto >= 2) { |
| 2817 | char bytes[2]; |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 2818 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2819 | bytes[0] = PROTO; |
| 2820 | assert(self->proto >= 0 && self->proto < 256); |
| 2821 | bytes[1] = (char)self->proto; |
| 2822 | if (self->write_func(self, bytes, 2) < 0) |
| 2823 | return -1; |
| 2824 | } |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 2825 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2826 | if (save(self, args, 0) < 0) |
| 2827 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2828 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2829 | if (self->write_func(self, &stop, 1) < 0) |
| 2830 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2831 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2832 | if (self->write_func(self, NULL, 0) < 0) |
| 2833 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2834 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2835 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2836 | } |
| 2837 | |
| 2838 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2839 | Pickle_clear_memo(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2840 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2841 | if (self->memo) |
| 2842 | PyDict_Clear(self->memo); |
| 2843 | Py_INCREF(Py_None); |
| 2844 | return Py_None; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2845 | } |
| 2846 | |
| 2847 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2848 | Pickle_getvalue(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2849 | { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 2850 | Py_ssize_t l, i, rsize, ssize, clear=1, lm; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2851 | long ik; |
| 2852 | PyObject *k, *r; |
| 2853 | char *s, *p, *have_get; |
| 2854 | Pdata *data; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2855 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2856 | /* Can be called by Python code or C code */ |
| 2857 | if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear)) |
| 2858 | return NULL; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2859 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2860 | /* Check to make sure we are based on a list */ |
| 2861 | if (! Pdata_Check(self->file)) { |
| 2862 | PyErr_SetString(PicklingError, |
| 2863 | "Attempt to getvalue() a non-list-based pickler"); |
| 2864 | return NULL; |
| 2865 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2866 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2867 | /* flush write buffer */ |
| 2868 | if (write_other(self, NULL, 0) < 0) return NULL; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2869 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2870 | data=(Pdata*)self->file; |
| 2871 | l=data->length; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2872 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2873 | /* set up an array to hold get/put status */ |
| 2874 | lm = PyDict_Size(self->memo); |
| 2875 | if (lm < 0) return NULL; |
| 2876 | lm++; |
| 2877 | have_get = malloc(lm); |
| 2878 | if (have_get == NULL) return PyErr_NoMemory(); |
| 2879 | memset(have_get, 0, lm); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2880 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2881 | /* Scan for gets. */ |
| 2882 | for (rsize = 0, i = l; --i >= 0; ) { |
| 2883 | k = data->data[i]; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2884 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2885 | if (PyString_Check(k)) |
| 2886 | rsize += PyString_GET_SIZE(k); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2887 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2888 | else if (PyInt_Check(k)) { /* put */ |
| 2889 | ik = PyInt_AS_LONG((PyIntObject*)k); |
| 2890 | if (ik >= lm || ik == 0) { |
| 2891 | PyErr_SetString(PicklingError, |
| 2892 | "Invalid get data"); |
| 2893 | goto err; |
| 2894 | } |
| 2895 | if (have_get[ik]) /* with matching get */ |
| 2896 | rsize += ik < 256 ? 2 : 5; |
| 2897 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2898 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2899 | else if (! (PyTuple_Check(k) && |
| 2900 | PyTuple_GET_SIZE(k) == 2 && |
| 2901 | PyInt_Check((k = PyTuple_GET_ITEM(k, 0)))) |
| 2902 | ) { |
| 2903 | PyErr_SetString(PicklingError, |
| 2904 | "Unexpected data in internal list"); |
| 2905 | goto err; |
| 2906 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2907 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2908 | else { /* put */ |
| 2909 | ik = PyInt_AS_LONG((PyIntObject *)k); |
| 2910 | if (ik >= lm || ik == 0) { |
| 2911 | PyErr_SetString(PicklingError, |
| 2912 | "Invalid get data"); |
| 2913 | return NULL; |
| 2914 | } |
| 2915 | have_get[ik] = 1; |
| 2916 | rsize += ik < 256 ? 2 : 5; |
| 2917 | } |
| 2918 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2919 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2920 | /* Now generate the result */ |
| 2921 | r = PyString_FromStringAndSize(NULL, rsize); |
| 2922 | if (r == NULL) goto err; |
| 2923 | s = PyString_AS_STRING((PyStringObject *)r); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2924 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2925 | for (i = 0; i < l; i++) { |
| 2926 | k = data->data[i]; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2927 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2928 | if (PyString_Check(k)) { |
| 2929 | ssize = PyString_GET_SIZE(k); |
| 2930 | if (ssize) { |
| 2931 | p=PyString_AS_STRING((PyStringObject *)k); |
| 2932 | while (--ssize >= 0) |
| 2933 | *s++ = *p++; |
| 2934 | } |
| 2935 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2936 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2937 | else if (PyTuple_Check(k)) { /* get */ |
| 2938 | ik = PyInt_AS_LONG((PyIntObject *) |
| 2939 | PyTuple_GET_ITEM(k, 0)); |
| 2940 | if (ik < 256) { |
| 2941 | *s++ = BINGET; |
| 2942 | *s++ = (int)(ik & 0xff); |
| 2943 | } |
| 2944 | else { |
| 2945 | *s++ = LONG_BINGET; |
| 2946 | *s++ = (int)(ik & 0xff); |
| 2947 | *s++ = (int)((ik >> 8) & 0xff); |
| 2948 | *s++ = (int)((ik >> 16) & 0xff); |
| 2949 | *s++ = (int)((ik >> 24) & 0xff); |
| 2950 | } |
| 2951 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2952 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2953 | else { /* put */ |
| 2954 | ik = PyInt_AS_LONG((PyIntObject*)k); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2955 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2956 | if (have_get[ik]) { /* with matching get */ |
| 2957 | if (ik < 256) { |
| 2958 | *s++ = BINPUT; |
| 2959 | *s++ = (int)(ik & 0xff); |
| 2960 | } |
| 2961 | else { |
| 2962 | *s++ = LONG_BINPUT; |
| 2963 | *s++ = (int)(ik & 0xff); |
| 2964 | *s++ = (int)((ik >> 8) & 0xff); |
| 2965 | *s++ = (int)((ik >> 16) & 0xff); |
| 2966 | *s++ = (int)((ik >> 24) & 0xff); |
| 2967 | } |
| 2968 | } |
| 2969 | } |
| 2970 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2971 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2972 | if (clear) { |
| 2973 | PyDict_Clear(self->memo); |
| 2974 | Pdata_clear(data, 0); |
| 2975 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2976 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2977 | free(have_get); |
| 2978 | return r; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2979 | err: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2980 | free(have_get); |
| 2981 | return NULL; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 2982 | } |
| 2983 | |
| 2984 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2985 | Pickler_dump(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2986 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2987 | PyObject *ob; |
| 2988 | int get=0; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 2989 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2990 | if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get))) |
| 2991 | return NULL; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 2992 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2993 | if (dump(self, ob) < 0) |
| 2994 | return NULL; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 2995 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2996 | if (get) return Pickle_getvalue(self, NULL); |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 2997 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2998 | /* XXX Why does dump() return self? */ |
| 2999 | Py_INCREF(self); |
| 3000 | return (PyObject*)self; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3001 | } |
| 3002 | |
| 3003 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3004 | static struct PyMethodDef Pickler_methods[] = |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3005 | { |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 3006 | {"dump", (PyCFunction)Pickler_dump, METH_VARARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 3007 | PyDoc_STR("dump(object) -- " |
| 3008 | "Write an object in pickle format to the object's pickle stream")}, |
Fred Drake | 0ebacc8 | 2002-05-01 20:36:39 +0000 | [diff] [blame] | 3009 | {"clear_memo", (PyCFunction)Pickle_clear_memo, METH_NOARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 3010 | PyDoc_STR("clear_memo() -- Clear the picklers memo")}, |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 3011 | {"getvalue", (PyCFunction)Pickle_getvalue, METH_VARARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 3012 | PyDoc_STR("getvalue() -- Finish picking a list-based pickle")}, |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3013 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3014 | }; |
| 3015 | |
| 3016 | |
| 3017 | static Picklerobject * |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 3018 | newPicklerobject(PyObject *file, int proto) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3019 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3020 | Picklerobject *self; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3021 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3022 | if (proto < 0) |
| 3023 | proto = HIGHEST_PROTOCOL; |
| 3024 | if (proto > HIGHEST_PROTOCOL) { |
| 3025 | PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; " |
| 3026 | "the highest available protocol is %d", |
| 3027 | proto, HIGHEST_PROTOCOL); |
| 3028 | return NULL; |
| 3029 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3030 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3031 | self = PyObject_GC_New(Picklerobject, &Picklertype); |
| 3032 | if (self == NULL) |
| 3033 | return NULL; |
| 3034 | self->proto = proto; |
| 3035 | self->bin = proto > 0; |
| 3036 | self->fp = NULL; |
| 3037 | self->write = NULL; |
| 3038 | self->memo = NULL; |
| 3039 | self->arg = NULL; |
| 3040 | self->pers_func = NULL; |
| 3041 | self->inst_pers_func = NULL; |
| 3042 | self->write_buf = NULL; |
| 3043 | self->fast = 0; |
| 3044 | self->fast_container = 0; |
| 3045 | self->fast_memo = NULL; |
| 3046 | self->buf_size = 0; |
| 3047 | self->dispatch_table = NULL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3048 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3049 | self->file = NULL; |
| 3050 | if (file) |
| 3051 | Py_INCREF(file); |
| 3052 | else { |
| 3053 | file = Pdata_New(); |
| 3054 | if (file == NULL) |
| 3055 | goto err; |
| 3056 | } |
| 3057 | self->file = file; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 3058 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3059 | if (!( self->memo = PyDict_New())) |
| 3060 | goto err; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3061 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3062 | if (PyFile_Check(file)) { |
| 3063 | self->fp = PyFile_AsFile(file); |
| 3064 | if (self->fp == NULL) { |
| 3065 | PyErr_SetString(PyExc_ValueError, |
| 3066 | "I/O operation on closed file"); |
| 3067 | goto err; |
| 3068 | } |
| 3069 | self->write_func = write_file; |
| 3070 | } |
| 3071 | else if (PycStringIO_OutputCheck(file)) { |
| 3072 | self->write_func = write_cStringIO; |
| 3073 | } |
| 3074 | else if (file == Py_None) { |
| 3075 | self->write_func = write_none; |
| 3076 | } |
| 3077 | else { |
| 3078 | self->write_func = write_other; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3079 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3080 | if (! Pdata_Check(file)) { |
| 3081 | self->write = PyObject_GetAttr(file, write_str); |
| 3082 | if (!self->write) { |
| 3083 | PyErr_Clear(); |
| 3084 | PyErr_SetString(PyExc_TypeError, |
| 3085 | "argument must have 'write' " |
| 3086 | "attribute"); |
| 3087 | goto err; |
| 3088 | } |
| 3089 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3090 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3091 | self->write_buf = (char *)PyMem_Malloc(WRITE_BUF_SIZE); |
| 3092 | if (self->write_buf == NULL) { |
| 3093 | PyErr_NoMemory(); |
| 3094 | goto err; |
| 3095 | } |
| 3096 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3097 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3098 | if (PyEval_GetRestricted()) { |
| 3099 | /* Restricted execution, get private tables */ |
| 3100 | PyObject *m = PyImport_ImportModule("copy_reg"); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3101 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3102 | if (m == NULL) |
| 3103 | goto err; |
| 3104 | self->dispatch_table = PyObject_GetAttr(m, dispatch_table_str); |
| 3105 | Py_DECREF(m); |
| 3106 | if (self->dispatch_table == NULL) |
| 3107 | goto err; |
| 3108 | } |
| 3109 | else { |
| 3110 | self->dispatch_table = dispatch_table; |
| 3111 | Py_INCREF(dispatch_table); |
| 3112 | } |
| 3113 | PyObject_GC_Track(self); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3114 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3115 | return self; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3116 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3117 | err: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3118 | Py_DECREF(self); |
| 3119 | return NULL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3120 | } |
| 3121 | |
| 3122 | |
| 3123 | static PyObject * |
Martin v. Löwis | 544f119 | 2004-07-27 05:22:33 +0000 | [diff] [blame] | 3124 | get_Pickler(PyObject *self, PyObject *args, PyObject *kwds) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3125 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3126 | static char *kwlist[] = {"file", "protocol", NULL}; |
| 3127 | PyObject *file = NULL; |
| 3128 | int proto = 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3129 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3130 | /* XXX |
| 3131 | * The documented signature is Pickler(file, protocol=0), but this |
| 3132 | * accepts Pickler() and Pickler(integer) too. The meaning then |
| 3133 | * is clear as mud, undocumented, and not supported by pickle.py. |
| 3134 | * I'm told Zope uses this, but I haven't traced into this code |
| 3135 | * far enough to figure out what it means. |
| 3136 | */ |
| 3137 | if (!PyArg_ParseTuple(args, "|i:Pickler", &proto)) { |
| 3138 | PyErr_Clear(); |
| 3139 | proto = 0; |
| 3140 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|i:Pickler", |
| 3141 | kwlist, &file, &proto)) |
| 3142 | return NULL; |
| 3143 | } |
| 3144 | return (PyObject *)newPicklerobject(file, proto); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3145 | } |
| 3146 | |
| 3147 | |
| 3148 | static void |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3149 | Pickler_dealloc(Picklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3150 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3151 | PyObject_GC_UnTrack(self); |
| 3152 | Py_XDECREF(self->write); |
| 3153 | Py_XDECREF(self->memo); |
| 3154 | Py_XDECREF(self->fast_memo); |
| 3155 | Py_XDECREF(self->arg); |
| 3156 | Py_XDECREF(self->file); |
| 3157 | Py_XDECREF(self->pers_func); |
| 3158 | Py_XDECREF(self->inst_pers_func); |
| 3159 | Py_XDECREF(self->dispatch_table); |
| 3160 | PyMem_Free(self->write_buf); |
| 3161 | Py_TYPE(self)->tp_free((PyObject *)self); |
Jeremy Hylton | 4cf6319 | 2003-04-09 21:05:12 +0000 | [diff] [blame] | 3162 | } |
| 3163 | |
| 3164 | static int |
| 3165 | Pickler_traverse(Picklerobject *self, visitproc visit, void *arg) |
| 3166 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3167 | Py_VISIT(self->write); |
| 3168 | Py_VISIT(self->memo); |
| 3169 | Py_VISIT(self->fast_memo); |
| 3170 | Py_VISIT(self->arg); |
| 3171 | Py_VISIT(self->file); |
| 3172 | Py_VISIT(self->pers_func); |
| 3173 | Py_VISIT(self->inst_pers_func); |
| 3174 | Py_VISIT(self->dispatch_table); |
| 3175 | return 0; |
Jeremy Hylton | 4cf6319 | 2003-04-09 21:05:12 +0000 | [diff] [blame] | 3176 | } |
| 3177 | |
| 3178 | static int |
| 3179 | Pickler_clear(Picklerobject *self) |
| 3180 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3181 | Py_CLEAR(self->write); |
| 3182 | Py_CLEAR(self->memo); |
| 3183 | Py_CLEAR(self->fast_memo); |
| 3184 | Py_CLEAR(self->arg); |
| 3185 | Py_CLEAR(self->file); |
| 3186 | Py_CLEAR(self->pers_func); |
| 3187 | Py_CLEAR(self->inst_pers_func); |
| 3188 | Py_CLEAR(self->dispatch_table); |
| 3189 | return 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3190 | } |
| 3191 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3192 | static PyObject * |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3193 | Pickler_get_pers_func(Picklerobject *p) |
| 3194 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3195 | if (p->pers_func == NULL) |
| 3196 | PyErr_SetString(PyExc_AttributeError, "persistent_id"); |
| 3197 | else |
| 3198 | Py_INCREF(p->pers_func); |
| 3199 | return p->pers_func; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3200 | } |
| 3201 | |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3202 | static int |
| 3203 | Pickler_set_pers_func(Picklerobject *p, PyObject *v) |
| 3204 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3205 | if (v == NULL) { |
| 3206 | PyErr_SetString(PyExc_TypeError, |
| 3207 | "attribute deletion is not supported"); |
| 3208 | return -1; |
| 3209 | } |
| 3210 | Py_XDECREF(p->pers_func); |
| 3211 | Py_INCREF(v); |
| 3212 | p->pers_func = v; |
| 3213 | return 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3214 | } |
| 3215 | |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3216 | static int |
| 3217 | Pickler_set_inst_pers_func(Picklerobject *p, PyObject *v) |
| 3218 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3219 | if (v == NULL) { |
| 3220 | PyErr_SetString(PyExc_TypeError, |
| 3221 | "attribute deletion is not supported"); |
| 3222 | return -1; |
| 3223 | } |
| 3224 | Py_XDECREF(p->inst_pers_func); |
| 3225 | Py_INCREF(v); |
| 3226 | p->inst_pers_func = v; |
| 3227 | return 0; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3228 | } |
| 3229 | |
| 3230 | static PyObject * |
| 3231 | Pickler_get_memo(Picklerobject *p) |
| 3232 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3233 | if (p->memo == NULL) |
| 3234 | PyErr_SetString(PyExc_AttributeError, "memo"); |
| 3235 | else |
| 3236 | Py_INCREF(p->memo); |
| 3237 | return p->memo; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3238 | } |
| 3239 | |
| 3240 | static int |
| 3241 | Pickler_set_memo(Picklerobject *p, PyObject *v) |
| 3242 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3243 | if (v == NULL) { |
| 3244 | PyErr_SetString(PyExc_TypeError, |
| 3245 | "attribute deletion is not supported"); |
| 3246 | return -1; |
| 3247 | } |
| 3248 | if (!PyDict_Check(v)) { |
| 3249 | PyErr_SetString(PyExc_TypeError, "memo must be a dictionary"); |
| 3250 | return -1; |
| 3251 | } |
| 3252 | Py_XDECREF(p->memo); |
| 3253 | Py_INCREF(v); |
| 3254 | p->memo = v; |
| 3255 | return 0; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3256 | } |
| 3257 | |
| 3258 | static PyObject * |
| 3259 | Pickler_get_error(Picklerobject *p) |
| 3260 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3261 | /* why is this an attribute on the Pickler? */ |
| 3262 | Py_INCREF(PicklingError); |
| 3263 | return PicklingError; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3264 | } |
| 3265 | |
| 3266 | static PyMemberDef Pickler_members[] = { |
| 3267 | {"binary", T_INT, offsetof(Picklerobject, bin)}, |
| 3268 | {"fast", T_INT, offsetof(Picklerobject, fast)}, |
Jeremy Hylton | 3eb46f3 | 2001-10-16 17:10:49 +0000 | [diff] [blame] | 3269 | {NULL} |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3270 | }; |
| 3271 | |
| 3272 | static PyGetSetDef Pickler_getsets[] = { |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3273 | {"persistent_id", (getter)Pickler_get_pers_func, |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3274 | (setter)Pickler_set_pers_func}, |
| 3275 | {"inst_persistent_id", NULL, (setter)Pickler_set_inst_pers_func}, |
| 3276 | {"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo}, |
Jeremy Hylton | 3eb46f3 | 2001-10-16 17:10:49 +0000 | [diff] [blame] | 3277 | {"PicklingError", (getter)Pickler_get_error, NULL}, |
| 3278 | {NULL} |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3279 | }; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3280 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3281 | PyDoc_STRVAR(Picklertype__doc__, |
| 3282 | "Objects that know how to pickle objects\n"); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3283 | |
Guido van Rossum | 9716aaa | 1997-12-08 15:15:16 +0000 | [diff] [blame] | 3284 | static PyTypeObject Picklertype = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 3285 | PyVarObject_HEAD_INIT(NULL, 0) |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 3286 | "cPickle.Pickler", /*tp_name*/ |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3287 | sizeof(Picklerobject), /*tp_basicsize*/ |
| 3288 | 0, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3289 | (destructor)Pickler_dealloc, /* tp_dealloc */ |
| 3290 | 0, /* tp_print */ |
| 3291 | 0, /* tp_getattr */ |
| 3292 | 0, /* tp_setattr */ |
| 3293 | 0, /* tp_compare */ |
| 3294 | 0, /* tp_repr */ |
| 3295 | 0, /* tp_as_number */ |
| 3296 | 0, /* tp_as_sequence */ |
| 3297 | 0, /* tp_as_mapping */ |
| 3298 | 0, /* tp_hash */ |
| 3299 | 0, /* tp_call */ |
| 3300 | 0, /* tp_str */ |
| 3301 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3302 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 3303 | 0, /* tp_as_buffer */ |
Jeremy Hylton | 4cf6319 | 2003-04-09 21:05:12 +0000 | [diff] [blame] | 3304 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3305 | Picklertype__doc__, /* tp_doc */ |
| 3306 | (traverseproc)Pickler_traverse, /* tp_traverse */ |
| 3307 | (inquiry)Pickler_clear, /* tp_clear */ |
| 3308 | 0, /* tp_richcompare */ |
| 3309 | 0, /* tp_weaklistoffset */ |
| 3310 | 0, /* tp_iter */ |
| 3311 | 0, /* tp_iternext */ |
| 3312 | Pickler_methods, /* tp_methods */ |
| 3313 | Pickler_members, /* tp_members */ |
| 3314 | Pickler_getsets, /* tp_getset */ |
Guido van Rossum | 9716aaa | 1997-12-08 15:15:16 +0000 | [diff] [blame] | 3315 | }; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3316 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3317 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3318 | 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] | 3319 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3320 | PyObject *global = 0, *module; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3321 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3322 | if (fc) { |
| 3323 | if (fc==Py_None) { |
| 3324 | PyErr_SetString(UnpicklingError, "Global and instance " |
| 3325 | "pickles are not supported."); |
| 3326 | return NULL; |
| 3327 | } |
| 3328 | return PyObject_CallFunctionObjArgs(fc, py_module_name, |
| 3329 | py_global_name, NULL); |
| 3330 | } |
Guido van Rossum | 1b9e0aa | 1999-04-19 17:58:18 +0000 | [diff] [blame] | 3331 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3332 | module = PySys_GetObject("modules"); |
| 3333 | if (module == NULL) |
| 3334 | return NULL; |
Jeremy Hylton | d105523 | 1998-08-11 19:52:51 +0000 | [diff] [blame] | 3335 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3336 | module = PyDict_GetItem(module, py_module_name); |
| 3337 | if (module == NULL) { |
| 3338 | module = PyImport_Import(py_module_name); |
| 3339 | if (!module) |
| 3340 | return NULL; |
| 3341 | global = PyObject_GetAttr(module, py_global_name); |
| 3342 | Py_DECREF(module); |
| 3343 | } |
| 3344 | else |
| 3345 | global = PyObject_GetAttr(module, py_global_name); |
| 3346 | return global; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3347 | } |
| 3348 | |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 3349 | static Py_ssize_t |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3350 | marker(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3351 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3352 | if (self->num_marks < 1) { |
| 3353 | PyErr_SetString(UnpicklingError, "could not find MARK"); |
| 3354 | return -1; |
| 3355 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3356 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3357 | return self->marks[--self->num_marks]; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3358 | } |
| 3359 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3360 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3361 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3362 | load_none(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3363 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3364 | PDATA_APPEND(self->stack, Py_None, -1); |
| 3365 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3366 | } |
| 3367 | |
Guido van Rossum | f9ffb03 | 1999-02-04 14:54:04 +0000 | [diff] [blame] | 3368 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3369 | bad_readline(void) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3370 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3371 | PyErr_SetString(UnpicklingError, "pickle data was truncated"); |
| 3372 | return -1; |
Guido van Rossum | f9ffb03 | 1999-02-04 14:54:04 +0000 | [diff] [blame] | 3373 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3374 | |
| 3375 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3376 | load_int(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3377 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3378 | PyObject *py_int = 0; |
| 3379 | char *endptr, *s; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 3380 | Py_ssize_t len; |
| 3381 | int res = -1; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3382 | long l; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3383 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3384 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
| 3385 | if (len < 2) return bad_readline(); |
| 3386 | if (!( s=pystrndup(s,len))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3387 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3388 | errno = 0; |
| 3389 | l = strtol(s, &endptr, 0); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3390 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3391 | if (errno || (*endptr != '\n') || (endptr[1] != '\0')) { |
| 3392 | /* Hm, maybe we've got something long. Let's try reading |
| 3393 | it as a Python long object. */ |
| 3394 | errno = 0; |
| 3395 | py_int = PyLong_FromString(s, NULL, 0); |
| 3396 | if (py_int == NULL) { |
| 3397 | PyErr_SetString(PyExc_ValueError, |
| 3398 | "could not convert string to int"); |
| 3399 | goto finally; |
| 3400 | } |
| 3401 | } |
| 3402 | else { |
| 3403 | if (len == 3 && (l == 0 || l == 1)) { |
| 3404 | if (!( py_int = PyBool_FromLong(l))) goto finally; |
| 3405 | } |
| 3406 | else { |
| 3407 | if (!( py_int = PyInt_FromLong(l))) goto finally; |
| 3408 | } |
| 3409 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3410 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3411 | free(s); |
| 3412 | PDATA_PUSH(self->stack, py_int, -1); |
| 3413 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3414 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3415 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3416 | free(s); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3417 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3418 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3419 | } |
| 3420 | |
Tim Peters | 3c67d79 | 2003-02-02 17:59:11 +0000 | [diff] [blame] | 3421 | static int |
| 3422 | load_bool(Unpicklerobject *self, PyObject *boolean) |
| 3423 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3424 | assert(boolean == Py_True || boolean == Py_False); |
| 3425 | PDATA_APPEND(self->stack, boolean, -1); |
| 3426 | return 0; |
Tim Peters | 3c67d79 | 2003-02-02 17:59:11 +0000 | [diff] [blame] | 3427 | } |
| 3428 | |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 3429 | /* s contains x bytes of a little-endian integer. Return its value as a |
| 3430 | * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian |
| 3431 | * int, but when x is 4 it's a signed one. This is an historical source |
| 3432 | * of x-platform bugs. |
| 3433 | */ |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3434 | static long |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 3435 | calc_binint(char *s, int x) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3436 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3437 | unsigned char c; |
| 3438 | int i; |
| 3439 | long l; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3440 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3441 | for (i = 0, l = 0L; i < x; i++) { |
| 3442 | c = (unsigned char)s[i]; |
| 3443 | l |= (long)c << (i * 8); |
| 3444 | } |
Tim Peters | bfa18f7 | 2001-04-10 01:54:42 +0000 | [diff] [blame] | 3445 | #if SIZEOF_LONG > 4 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3446 | /* Unlike BININT1 and BININT2, BININT (more accurately BININT4) |
| 3447 | * is signed, so on a box with longs bigger than 4 bytes we need |
| 3448 | * to extend a BININT's sign bit to the full width. |
| 3449 | */ |
| 3450 | if (x == 4 && l & (1L << 31)) |
| 3451 | l |= (~0L) << 32; |
Tim Peters | bfa18f7 | 2001-04-10 01:54:42 +0000 | [diff] [blame] | 3452 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3453 | return l; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3454 | } |
| 3455 | |
| 3456 | |
| 3457 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3458 | load_binintx(Unpicklerobject *self, char *s, int x) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3459 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3460 | PyObject *py_int = 0; |
| 3461 | long l; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3462 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3463 | l = calc_binint(s, x); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3464 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3465 | if (!( py_int = PyInt_FromLong(l))) |
| 3466 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3467 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3468 | PDATA_PUSH(self->stack, py_int, -1); |
| 3469 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3470 | } |
| 3471 | |
| 3472 | |
| 3473 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3474 | load_binint(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3475 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3476 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3477 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3478 | if (self->read_func(self, &s, 4) < 0) |
| 3479 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3480 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3481 | return load_binintx(self, s, 4); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3482 | } |
| 3483 | |
| 3484 | |
| 3485 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3486 | load_binint1(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3487 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3488 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3489 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3490 | if (self->read_func(self, &s, 1) < 0) |
| 3491 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3492 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3493 | return load_binintx(self, s, 1); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3494 | } |
| 3495 | |
| 3496 | |
| 3497 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3498 | load_binint2(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3499 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3500 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3501 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3502 | if (self->read_func(self, &s, 2) < 0) |
| 3503 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3504 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3505 | return load_binintx(self, s, 2); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3506 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3507 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3508 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3509 | load_long(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3510 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3511 | PyObject *l = 0; |
| 3512 | char *end, *s; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 3513 | Py_ssize_t len; |
| 3514 | int res = -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3515 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3516 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
| 3517 | if (len < 2) return bad_readline(); |
| 3518 | if (!( s=pystrndup(s,len))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3519 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3520 | if (!( l = PyLong_FromString(s, &end, 0))) |
| 3521 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3522 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3523 | free(s); |
| 3524 | PDATA_PUSH(self->stack, l, -1); |
| 3525 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3526 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3527 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3528 | free(s); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3529 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3530 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3531 | } |
| 3532 | |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 3533 | /* 'size' bytes contain the # of bytes of little-endian 256's-complement |
| 3534 | * data following. |
| 3535 | */ |
| 3536 | static int |
| 3537 | load_counted_long(Unpicklerobject *self, int size) |
| 3538 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3539 | Py_ssize_t i; |
| 3540 | char *nbytes; |
| 3541 | unsigned char *pdata; |
| 3542 | PyObject *along; |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 3543 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3544 | assert(size == 1 || size == 4); |
| 3545 | i = self->read_func(self, &nbytes, size); |
| 3546 | if (i < 0) return -1; |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 3547 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3548 | size = calc_binint(nbytes, size); |
| 3549 | if (size < 0) { |
| 3550 | /* Corrupt or hostile pickle -- we never write one like |
| 3551 | * this. |
| 3552 | */ |
| 3553 | PyErr_SetString(UnpicklingError, "LONG pickle has negative " |
| 3554 | "byte count"); |
| 3555 | return -1; |
| 3556 | } |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 3557 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3558 | if (size == 0) |
| 3559 | along = PyLong_FromLong(0L); |
| 3560 | else { |
| 3561 | /* Read the raw little-endian bytes & convert. */ |
| 3562 | i = self->read_func(self, (char **)&pdata, size); |
| 3563 | if (i < 0) return -1; |
| 3564 | along = _PyLong_FromByteArray(pdata, (size_t)size, |
| 3565 | 1 /* little endian */, 1 /* signed */); |
| 3566 | } |
| 3567 | if (along == NULL) |
| 3568 | return -1; |
| 3569 | PDATA_PUSH(self->stack, along, -1); |
| 3570 | return 0; |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 3571 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3572 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3573 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3574 | load_float(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3575 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3576 | PyObject *py_float = 0; |
| 3577 | char *endptr, *s; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 3578 | Py_ssize_t len; |
| 3579 | int res = -1; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3580 | double d; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3581 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3582 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
| 3583 | if (len < 2) return bad_readline(); |
| 3584 | if (!( s=pystrndup(s,len))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3585 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3586 | d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3587 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3588 | if (d == -1.0 && PyErr_Occurred()) { |
| 3589 | goto finally; |
| 3590 | } else if ((endptr[0] != '\n') || (endptr[1] != '\0')) { |
| 3591 | PyErr_SetString(PyExc_ValueError, |
| 3592 | "could not convert string to float"); |
| 3593 | goto finally; |
| 3594 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3595 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3596 | if (!( py_float = PyFloat_FromDouble(d))) |
| 3597 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3598 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3599 | free(s); |
| 3600 | PDATA_PUSH(self->stack, py_float, -1); |
| 3601 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3602 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3603 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3604 | free(s); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3605 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3606 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3607 | } |
| 3608 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3609 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3610 | load_binfloat(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3611 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3612 | PyObject *py_float; |
| 3613 | double x; |
| 3614 | char *p; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3615 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3616 | if (self->read_func(self, &p, 8) < 0) |
| 3617 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3618 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3619 | x = _PyFloat_Unpack8((unsigned char *)p, 0); |
| 3620 | if (x == -1.0 && PyErr_Occurred()) |
| 3621 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3622 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3623 | py_float = PyFloat_FromDouble(x); |
| 3624 | if (py_float == NULL) |
| 3625 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3626 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3627 | PDATA_PUSH(self->stack, py_float, -1); |
| 3628 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3629 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3630 | |
| 3631 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3632 | load_string(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3633 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3634 | PyObject *str = 0; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 3635 | Py_ssize_t len; |
| 3636 | int res = -1; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3637 | char *s, *p; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3638 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3639 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
| 3640 | if (len < 2) return bad_readline(); |
| 3641 | if (!( s=pystrndup(s,len))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3642 | |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 3643 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3644 | /* Strip outermost quotes */ |
Antoine Pitrou | be92971 | 2013-04-15 21:35:25 +0200 | [diff] [blame] | 3645 | while (len > 0 && s[len-1] <= ' ') |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3646 | len--; |
Antoine Pitrou | be92971 | 2013-04-15 21:35:25 +0200 | [diff] [blame] | 3647 | if (len > 1 && s[0]=='"' && s[len-1]=='"') { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3648 | s[len-1] = '\0'; |
| 3649 | p = s + 1 ; |
| 3650 | len -= 2; |
Antoine Pitrou | be92971 | 2013-04-15 21:35:25 +0200 | [diff] [blame] | 3651 | } |
| 3652 | else if (len > 1 && s[0]=='\'' && s[len-1]=='\'') { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3653 | s[len-1] = '\0'; |
| 3654 | p = s + 1 ; |
| 3655 | len -= 2; |
Antoine Pitrou | be92971 | 2013-04-15 21:35:25 +0200 | [diff] [blame] | 3656 | } |
| 3657 | else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3658 | goto insecure; |
| 3659 | /********************************************/ |
Guido van Rossum | 9716aaa | 1997-12-08 15:15:16 +0000 | [diff] [blame] | 3660 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3661 | str = PyString_DecodeEscape(p, len, NULL, 0, NULL); |
| 3662 | free(s); |
| 3663 | if (str) { |
| 3664 | PDATA_PUSH(self->stack, str, -1); |
| 3665 | res = 0; |
| 3666 | } |
| 3667 | return res; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3668 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3669 | insecure: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3670 | free(s); |
| 3671 | PyErr_SetString(PyExc_ValueError,"insecure string pickle"); |
| 3672 | return -1; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3673 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3674 | |
| 3675 | |
| 3676 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3677 | load_binstring(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3678 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3679 | PyObject *py_string = 0; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 3680 | Py_ssize_t l; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3681 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3682 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3683 | if (self->read_func(self, &s, 4) < 0) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3684 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3685 | l = calc_binint(s, 4); |
| 3686 | if (l < 0) { |
| 3687 | /* Corrupt or hostile pickle -- we never write one like |
| 3688 | * this. |
| 3689 | */ |
| 3690 | PyErr_SetString(UnpicklingError, |
| 3691 | "BINSTRING pickle has negative byte count"); |
| 3692 | return -1; |
| 3693 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3694 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3695 | if (self->read_func(self, &s, l) < 0) |
| 3696 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3697 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3698 | if (!( py_string = PyString_FromStringAndSize(s, l))) |
| 3699 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3700 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3701 | PDATA_PUSH(self->stack, py_string, -1); |
| 3702 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3703 | } |
| 3704 | |
| 3705 | |
| 3706 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3707 | load_short_binstring(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3708 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3709 | PyObject *py_string = 0; |
| 3710 | unsigned char l; |
| 3711 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3712 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3713 | if (self->read_func(self, &s, 1) < 0) |
| 3714 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3715 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3716 | l = (unsigned char)s[0]; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3717 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3718 | if (self->read_func(self, &s, l) < 0) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3719 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3720 | if (!( py_string = PyString_FromStringAndSize(s, l))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3721 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3722 | PDATA_PUSH(self->stack, py_string, -1); |
| 3723 | return 0; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3724 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3725 | |
| 3726 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3727 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3728 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3729 | load_unicode(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3730 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3731 | PyObject *str = 0; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 3732 | Py_ssize_t len; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3733 | char *s; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3734 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3735 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
| 3736 | if (len < 1) return bad_readline(); |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3737 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3738 | if (!( str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL))) |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 3739 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3740 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3741 | PDATA_PUSH(self->stack, str, -1); |
| 3742 | return 0; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3743 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3744 | #endif |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3745 | |
| 3746 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3747 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3748 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3749 | load_binunicode(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3750 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3751 | PyObject *unicode; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 3752 | Py_ssize_t l; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3753 | char *s; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3754 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3755 | if (self->read_func(self, &s, 4) < 0) return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3756 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3757 | l = calc_binint(s, 4); |
| 3758 | if (l < 0) { |
| 3759 | /* Corrupt or hostile pickle -- we never write one like |
| 3760 | * this. |
| 3761 | */ |
| 3762 | PyErr_SetString(UnpicklingError, |
| 3763 | "BINUNICODE pickle has negative byte count"); |
| 3764 | return -1; |
| 3765 | } |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3766 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3767 | if (self->read_func(self, &s, l) < 0) |
| 3768 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3769 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3770 | if (!( unicode = PyUnicode_DecodeUTF8(s, l, NULL))) |
| 3771 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3772 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3773 | PDATA_PUSH(self->stack, unicode, -1); |
| 3774 | return 0; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3775 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3776 | #endif |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3777 | |
| 3778 | |
| 3779 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3780 | load_tuple(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3781 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3782 | PyObject *tup; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 3783 | Py_ssize_t i; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3784 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3785 | if ((i = marker(self)) < 0) return -1; |
| 3786 | if (!( tup=Pdata_popTuple(self->stack, i))) return -1; |
| 3787 | PDATA_PUSH(self->stack, tup, -1); |
| 3788 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3789 | } |
| 3790 | |
| 3791 | static int |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 3792 | load_counted_tuple(Unpicklerobject *self, int len) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3793 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3794 | PyObject *tup = PyTuple_New(len); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3795 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3796 | if (tup == NULL) |
| 3797 | return -1; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 3798 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3799 | while (--len >= 0) { |
| 3800 | PyObject *element; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 3801 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3802 | PDATA_POP(self->stack, element); |
| 3803 | if (element == NULL) |
| 3804 | return -1; |
| 3805 | PyTuple_SET_ITEM(tup, len, element); |
| 3806 | } |
| 3807 | PDATA_PUSH(self->stack, tup, -1); |
| 3808 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3809 | } |
| 3810 | |
| 3811 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3812 | load_empty_list(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3813 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3814 | PyObject *list; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3815 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3816 | if (!( list=PyList_New(0))) return -1; |
| 3817 | PDATA_PUSH(self->stack, list, -1); |
| 3818 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3819 | } |
| 3820 | |
| 3821 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3822 | load_empty_dict(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3823 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3824 | PyObject *dict; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3825 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3826 | if (!( dict=PyDict_New())) return -1; |
| 3827 | PDATA_PUSH(self->stack, dict, -1); |
| 3828 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3829 | } |
| 3830 | |
| 3831 | |
| 3832 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3833 | load_list(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3834 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3835 | PyObject *list = 0; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 3836 | Py_ssize_t i; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3837 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3838 | if ((i = marker(self)) < 0) return -1; |
| 3839 | if (!( list=Pdata_popList(self->stack, i))) return -1; |
| 3840 | PDATA_PUSH(self->stack, list, -1); |
| 3841 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3842 | } |
| 3843 | |
| 3844 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3845 | load_dict(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3846 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3847 | PyObject *dict, *key, *value; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 3848 | Py_ssize_t i, j, k; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3849 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3850 | if ((i = marker(self)) < 0) return -1; |
| 3851 | j=self->stack->length; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3852 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3853 | if (!( dict = PyDict_New())) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3854 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3855 | for (k = i+1; k < j; k += 2) { |
| 3856 | key =self->stack->data[k-1]; |
| 3857 | value=self->stack->data[k ]; |
| 3858 | if (PyDict_SetItem(dict, key, value) < 0) { |
| 3859 | Py_DECREF(dict); |
| 3860 | return -1; |
| 3861 | } |
| 3862 | } |
| 3863 | Pdata_clear(self->stack, i); |
| 3864 | PDATA_PUSH(self->stack, dict, -1); |
| 3865 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3866 | } |
| 3867 | |
| 3868 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3869 | Instance_New(PyObject *cls, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3870 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3871 | PyObject *r = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3872 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3873 | if (PyClass_Check(cls)) { |
| 3874 | int l; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3875 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3876 | if ((l=PyObject_Size(args)) < 0) goto err; |
| 3877 | if (!( l )) { |
| 3878 | PyObject *__getinitargs__; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3879 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3880 | __getinitargs__ = PyObject_GetAttr(cls, |
| 3881 | __getinitargs___str); |
| 3882 | if (!__getinitargs__) { |
| 3883 | /* We have a class with no __getinitargs__, |
| 3884 | so bypass usual construction */ |
| 3885 | PyObject *inst; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3886 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3887 | PyErr_Clear(); |
| 3888 | if (!( inst=PyInstance_NewRaw(cls, NULL))) |
| 3889 | goto err; |
| 3890 | return inst; |
| 3891 | } |
| 3892 | Py_DECREF(__getinitargs__); |
| 3893 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3894 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3895 | if ((r=PyInstance_New(cls, args, NULL))) return r; |
| 3896 | else goto err; |
| 3897 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3898 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3899 | if ((r=PyObject_CallObject(cls, args))) return r; |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 3900 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3901 | err: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3902 | { |
| 3903 | PyObject *tp, *v, *tb, *tmp_value; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3904 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3905 | PyErr_Fetch(&tp, &v, &tb); |
| 3906 | tmp_value = v; |
| 3907 | /* NULL occurs when there was a KeyboardInterrupt */ |
| 3908 | if (tmp_value == NULL) |
| 3909 | tmp_value = Py_None; |
| 3910 | if ((r = PyTuple_Pack(3, tmp_value, cls, args))) { |
| 3911 | Py_XDECREF(v); |
| 3912 | v=r; |
| 3913 | } |
| 3914 | PyErr_Restore(tp,v,tb); |
| 3915 | } |
| 3916 | return NULL; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3917 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3918 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3919 | |
| 3920 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3921 | load_obj(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3922 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3923 | PyObject *class, *tup, *obj=0; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 3924 | Py_ssize_t i; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3925 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3926 | if ((i = marker(self)) < 0) return -1; |
| 3927 | if (!( tup=Pdata_popTuple(self->stack, i+1))) return -1; |
| 3928 | PDATA_POP(self->stack, class); |
| 3929 | if (class) { |
| 3930 | obj = Instance_New(class, tup); |
| 3931 | Py_DECREF(class); |
| 3932 | } |
| 3933 | Py_DECREF(tup); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3934 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3935 | if (! obj) return -1; |
| 3936 | PDATA_PUSH(self->stack, obj, -1); |
| 3937 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3938 | } |
| 3939 | |
| 3940 | |
| 3941 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3942 | load_inst(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3943 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3944 | PyObject *tup, *class=0, *obj=0, *module_name, *class_name; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 3945 | Py_ssize_t i, len; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3946 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3947 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3948 | if ((i = marker(self)) < 0) return -1; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3949 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3950 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
| 3951 | if (len < 2) return bad_readline(); |
| 3952 | module_name = PyString_FromStringAndSize(s, len - 1); |
| 3953 | if (!module_name) return -1; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3954 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3955 | if ((len = self->readline_func(self, &s)) >= 0) { |
| 3956 | if (len < 2) return bad_readline(); |
| 3957 | if ((class_name = PyString_FromStringAndSize(s, len - 1))) { |
| 3958 | class = find_class(module_name, class_name, |
| 3959 | self->find_class); |
| 3960 | Py_DECREF(class_name); |
| 3961 | } |
| 3962 | } |
| 3963 | Py_DECREF(module_name); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3964 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3965 | if (! class) return -1; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3966 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3967 | if ((tup=Pdata_popTuple(self->stack, i))) { |
| 3968 | obj = Instance_New(class, tup); |
| 3969 | Py_DECREF(tup); |
| 3970 | } |
| 3971 | Py_DECREF(class); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3972 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3973 | if (! obj) return -1; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3974 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3975 | PDATA_PUSH(self->stack, obj, -1); |
| 3976 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3977 | } |
| 3978 | |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 3979 | static int |
| 3980 | load_newobj(Unpicklerobject *self) |
| 3981 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3982 | PyObject *args = NULL; |
| 3983 | PyObject *clsraw = NULL; |
| 3984 | PyTypeObject *cls; /* clsraw cast to its true type */ |
| 3985 | PyObject *obj; |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 3986 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3987 | /* Stack is ... cls argtuple, and we want to call |
| 3988 | * cls.__new__(cls, *argtuple). |
| 3989 | */ |
| 3990 | PDATA_POP(self->stack, args); |
| 3991 | if (args == NULL) goto Fail; |
| 3992 | if (! PyTuple_Check(args)) { |
| 3993 | PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg " |
| 3994 | "tuple."); |
| 3995 | goto Fail; |
| 3996 | } |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 3997 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3998 | PDATA_POP(self->stack, clsraw); |
| 3999 | cls = (PyTypeObject *)clsraw; |
| 4000 | if (cls == NULL) goto Fail; |
| 4001 | if (! PyType_Check(cls)) { |
| 4002 | PyErr_SetString(UnpicklingError, "NEWOBJ class argument " |
| 4003 | "isn't a type object"); |
| 4004 | goto Fail; |
| 4005 | } |
| 4006 | if (cls->tp_new == NULL) { |
| 4007 | PyErr_SetString(UnpicklingError, "NEWOBJ class argument " |
| 4008 | "has NULL tp_new"); |
| 4009 | goto Fail; |
| 4010 | } |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 4011 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4012 | /* Call __new__. */ |
| 4013 | obj = cls->tp_new(cls, args, NULL); |
| 4014 | if (obj == NULL) goto Fail; |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 4015 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4016 | Py_DECREF(args); |
| 4017 | Py_DECREF(clsraw); |
| 4018 | PDATA_PUSH(self->stack, obj, -1); |
| 4019 | return 0; |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 4020 | |
| 4021 | Fail: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4022 | Py_XDECREF(args); |
| 4023 | Py_XDECREF(clsraw); |
| 4024 | return -1; |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 4025 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4026 | |
| 4027 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4028 | load_global(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4029 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4030 | PyObject *class = 0, *module_name = 0, *class_name = 0; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4031 | Py_ssize_t len; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4032 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4033 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4034 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
| 4035 | if (len < 2) return bad_readline(); |
| 4036 | module_name = PyString_FromStringAndSize(s, len - 1); |
| 4037 | if (!module_name) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4038 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4039 | if ((len = self->readline_func(self, &s)) >= 0) { |
| 4040 | if (len < 2) { |
| 4041 | Py_DECREF(module_name); |
| 4042 | return bad_readline(); |
| 4043 | } |
| 4044 | if ((class_name = PyString_FromStringAndSize(s, len - 1))) { |
| 4045 | class = find_class(module_name, class_name, |
| 4046 | self->find_class); |
| 4047 | Py_DECREF(class_name); |
| 4048 | } |
| 4049 | } |
| 4050 | Py_DECREF(module_name); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4051 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4052 | if (! class) return -1; |
| 4053 | PDATA_PUSH(self->stack, class, -1); |
| 4054 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4055 | } |
| 4056 | |
| 4057 | |
| 4058 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4059 | load_persid(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4060 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4061 | PyObject *pid = 0; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4062 | Py_ssize_t len; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4063 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4064 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4065 | if (self->pers_func) { |
| 4066 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
| 4067 | if (len < 2) return bad_readline(); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4068 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4069 | pid = PyString_FromStringAndSize(s, len - 1); |
| 4070 | if (!pid) return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4071 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4072 | if (PyList_Check(self->pers_func)) { |
| 4073 | if (PyList_Append(self->pers_func, pid) < 0) { |
| 4074 | Py_DECREF(pid); |
| 4075 | return -1; |
| 4076 | } |
| 4077 | } |
| 4078 | else { |
| 4079 | ARG_TUP(self, pid); |
| 4080 | if (self->arg) { |
| 4081 | pid = PyObject_Call(self->pers_func, self->arg, |
| 4082 | NULL); |
| 4083 | FREE_ARG_TUP(self); |
| 4084 | } |
| 4085 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4086 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4087 | if (! pid) return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4088 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4089 | PDATA_PUSH(self->stack, pid, -1); |
| 4090 | return 0; |
| 4091 | } |
| 4092 | else { |
| 4093 | PyErr_SetString(UnpicklingError, |
| 4094 | "A load persistent id instruction was encountered,\n" |
| 4095 | "but no persistent_load function was specified."); |
| 4096 | return -1; |
| 4097 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4098 | } |
| 4099 | |
| 4100 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4101 | load_binpersid(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4102 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4103 | PyObject *pid = 0; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4104 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4105 | if (self->pers_func) { |
| 4106 | PDATA_POP(self->stack, pid); |
| 4107 | if (! pid) return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4108 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4109 | if (PyList_Check(self->pers_func)) { |
| 4110 | if (PyList_Append(self->pers_func, pid) < 0) { |
| 4111 | Py_DECREF(pid); |
| 4112 | return -1; |
| 4113 | } |
| 4114 | } |
| 4115 | else { |
| 4116 | ARG_TUP(self, pid); |
| 4117 | if (self->arg) { |
| 4118 | pid = PyObject_Call(self->pers_func, self->arg, |
| 4119 | NULL); |
| 4120 | FREE_ARG_TUP(self); |
| 4121 | } |
| 4122 | if (! pid) return -1; |
| 4123 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4124 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4125 | PDATA_PUSH(self->stack, pid, -1); |
| 4126 | return 0; |
| 4127 | } |
| 4128 | else { |
| 4129 | PyErr_SetString(UnpicklingError, |
| 4130 | "A load persistent id instruction was encountered,\n" |
| 4131 | "but no persistent_load function was specified."); |
| 4132 | return -1; |
| 4133 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4134 | } |
| 4135 | |
| 4136 | |
| 4137 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4138 | load_pop(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4139 | { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4140 | Py_ssize_t len = self->stack->length; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4141 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4142 | /* Note that we split the (pickle.py) stack into two stacks, |
| 4143 | an object stack and a mark stack. We have to be clever and |
| 4144 | pop the right one. We do this by looking at the top of the |
| 4145 | mark stack first, and only signalling a stack underflow if |
| 4146 | the object stack is empty and the mark stack doesn't match |
| 4147 | our expectations. |
| 4148 | */ |
| 4149 | if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) { |
| 4150 | self->num_marks--; |
| 4151 | } else if (len > 0) { |
| 4152 | len--; |
| 4153 | Py_DECREF(self->stack->data[len]); |
| 4154 | self->stack->length = len; |
| 4155 | } else { |
| 4156 | return stackUnderflow(); |
| 4157 | } |
| 4158 | return 0; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4159 | } |
| 4160 | |
| 4161 | |
| 4162 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4163 | load_pop_mark(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4164 | { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4165 | Py_ssize_t i; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4166 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4167 | if ((i = marker(self)) < 0) |
| 4168 | return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4169 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4170 | Pdata_clear(self->stack, i); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4171 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4172 | return 0; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4173 | } |
| 4174 | |
| 4175 | |
| 4176 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4177 | load_dup(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4178 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4179 | PyObject *last; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4180 | Py_ssize_t len; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4181 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4182 | if ((len = self->stack->length) <= 0) return stackUnderflow(); |
| 4183 | last=self->stack->data[len-1]; |
| 4184 | Py_INCREF(last); |
| 4185 | PDATA_PUSH(self->stack, last, -1); |
| 4186 | return 0; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4187 | } |
| 4188 | |
| 4189 | |
| 4190 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4191 | load_get(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4192 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4193 | PyObject *py_str = 0, *value = 0; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4194 | Py_ssize_t len; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4195 | char *s; |
| 4196 | int rc; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4197 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4198 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
| 4199 | if (len < 2) return bad_readline(); |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4200 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4201 | if (!( py_str = PyString_FromStringAndSize(s, len - 1))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4202 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4203 | value = PyDict_GetItem(self->memo, py_str); |
| 4204 | if (! value) { |
| 4205 | PyErr_SetObject(BadPickleGet, py_str); |
| 4206 | rc = -1; |
| 4207 | } |
| 4208 | else { |
| 4209 | PDATA_APPEND(self->stack, value, -1); |
| 4210 | rc = 0; |
| 4211 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4212 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4213 | Py_DECREF(py_str); |
| 4214 | return rc; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4215 | } |
| 4216 | |
| 4217 | |
| 4218 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4219 | load_binget(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4220 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4221 | PyObject *py_key = 0, *value = 0; |
| 4222 | unsigned char key; |
| 4223 | char *s; |
| 4224 | int rc; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4225 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4226 | if (self->read_func(self, &s, 1) < 0) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4227 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4228 | key = (unsigned char)s[0]; |
| 4229 | if (!( py_key = PyInt_FromLong((long)key))) return -1; |
Guido van Rossum | ea2b715 | 2000-05-09 18:14:50 +0000 | [diff] [blame] | 4230 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4231 | value = PyDict_GetItem(self->memo, py_key); |
| 4232 | if (! value) { |
| 4233 | PyErr_SetObject(BadPickleGet, py_key); |
| 4234 | rc = -1; |
| 4235 | } |
| 4236 | else { |
| 4237 | PDATA_APPEND(self->stack, value, -1); |
| 4238 | rc = 0; |
| 4239 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4240 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4241 | Py_DECREF(py_key); |
| 4242 | return rc; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4243 | } |
| 4244 | |
| 4245 | |
| 4246 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4247 | load_long_binget(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4248 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4249 | PyObject *py_key = 0, *value = 0; |
| 4250 | unsigned char c; |
| 4251 | char *s; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4252 | Py_ssize_t key; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4253 | int rc; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4254 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4255 | if (self->read_func(self, &s, 4) < 0) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4256 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4257 | c = (unsigned char)s[0]; |
| 4258 | key = (long)c; |
| 4259 | c = (unsigned char)s[1]; |
| 4260 | key |= (long)c << 8; |
| 4261 | c = (unsigned char)s[2]; |
| 4262 | key |= (long)c << 16; |
| 4263 | c = (unsigned char)s[3]; |
| 4264 | key |= (long)c << 24; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4265 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4266 | if (!( py_key = PyInt_FromLong((long)key))) return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4267 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4268 | value = PyDict_GetItem(self->memo, py_key); |
| 4269 | if (! value) { |
| 4270 | PyErr_SetObject(BadPickleGet, py_key); |
| 4271 | rc = -1; |
| 4272 | } |
| 4273 | else { |
| 4274 | PDATA_APPEND(self->stack, value, -1); |
| 4275 | rc = 0; |
| 4276 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4277 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4278 | Py_DECREF(py_key); |
| 4279 | return rc; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4280 | } |
| 4281 | |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 4282 | /* Push an object from the extension registry (EXT[124]). nbytes is |
| 4283 | * the number of bytes following the opcode, holding the index (code) value. |
| 4284 | */ |
| 4285 | static int |
| 4286 | load_extension(Unpicklerobject *self, int nbytes) |
| 4287 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4288 | char *codebytes; /* the nbytes bytes after the opcode */ |
| 4289 | long code; /* calc_binint returns long */ |
| 4290 | PyObject *py_code; /* code as a Python int */ |
| 4291 | PyObject *obj; /* the object to push */ |
| 4292 | PyObject *pair; /* (module_name, class_name) */ |
| 4293 | PyObject *module_name, *class_name; |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 4294 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4295 | assert(nbytes == 1 || nbytes == 2 || nbytes == 4); |
| 4296 | if (self->read_func(self, &codebytes, nbytes) < 0) return -1; |
| 4297 | code = calc_binint(codebytes, nbytes); |
| 4298 | if (code <= 0) { /* note that 0 is forbidden */ |
| 4299 | /* Corrupt or hostile pickle. */ |
| 4300 | PyErr_SetString(UnpicklingError, "EXT specifies code <= 0"); |
| 4301 | return -1; |
| 4302 | } |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 4303 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4304 | /* Look for the code in the cache. */ |
| 4305 | py_code = PyInt_FromLong(code); |
| 4306 | if (py_code == NULL) return -1; |
| 4307 | obj = PyDict_GetItem(extension_cache, py_code); |
| 4308 | if (obj != NULL) { |
| 4309 | /* Bingo. */ |
| 4310 | Py_DECREF(py_code); |
| 4311 | PDATA_APPEND(self->stack, obj, -1); |
| 4312 | return 0; |
| 4313 | } |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 4314 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4315 | /* Look up the (module_name, class_name) pair. */ |
| 4316 | pair = PyDict_GetItem(inverted_registry, py_code); |
| 4317 | if (pair == NULL) { |
| 4318 | Py_DECREF(py_code); |
| 4319 | PyErr_Format(PyExc_ValueError, "unregistered extension " |
| 4320 | "code %ld", code); |
| 4321 | return -1; |
| 4322 | } |
| 4323 | /* Since the extension registry is manipulable via Python code, |
| 4324 | * confirm that pair is really a 2-tuple of strings. |
| 4325 | */ |
| 4326 | if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 || |
| 4327 | !PyString_Check(module_name = PyTuple_GET_ITEM(pair, 0)) || |
| 4328 | !PyString_Check(class_name = PyTuple_GET_ITEM(pair, 1))) { |
| 4329 | Py_DECREF(py_code); |
| 4330 | PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] " |
| 4331 | "isn't a 2-tuple of strings", code); |
| 4332 | return -1; |
| 4333 | } |
| 4334 | /* Load the object. */ |
| 4335 | obj = find_class(module_name, class_name, self->find_class); |
| 4336 | if (obj == NULL) { |
| 4337 | Py_DECREF(py_code); |
| 4338 | return -1; |
| 4339 | } |
| 4340 | /* Cache code -> obj. */ |
| 4341 | code = PyDict_SetItem(extension_cache, py_code, obj); |
| 4342 | Py_DECREF(py_code); |
| 4343 | if (code < 0) { |
| 4344 | Py_DECREF(obj); |
| 4345 | return -1; |
| 4346 | } |
| 4347 | PDATA_PUSH(self->stack, obj, -1); |
| 4348 | return 0; |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 4349 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4350 | |
| 4351 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4352 | load_put(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4353 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4354 | PyObject *py_str = 0, *value = 0; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4355 | Py_ssize_t len, l; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4356 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4357 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4358 | if ((l = self->readline_func(self, &s)) < 0) return -1; |
| 4359 | if (l < 2) return bad_readline(); |
| 4360 | if (!( len=self->stack->length )) return stackUnderflow(); |
| 4361 | if (!( py_str = PyString_FromStringAndSize(s, l - 1))) return -1; |
| 4362 | value=self->stack->data[len-1]; |
| 4363 | l=PyDict_SetItem(self->memo, py_str, value); |
| 4364 | Py_DECREF(py_str); |
| 4365 | return l; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4366 | } |
| 4367 | |
| 4368 | |
| 4369 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4370 | load_binput(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4371 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4372 | PyObject *py_key = 0, *value = 0; |
| 4373 | unsigned char key; |
| 4374 | char *s; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4375 | Py_ssize_t len; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4376 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4377 | if (self->read_func(self, &s, 1) < 0) return -1; |
| 4378 | if (!( (len=self->stack->length) > 0 )) return stackUnderflow(); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4379 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4380 | key = (unsigned char)s[0]; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 4381 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4382 | if (!( py_key = PyInt_FromLong((long)key))) return -1; |
| 4383 | value=self->stack->data[len-1]; |
| 4384 | len=PyDict_SetItem(self->memo, py_key, value); |
| 4385 | Py_DECREF(py_key); |
| 4386 | return len; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4387 | } |
| 4388 | |
| 4389 | |
| 4390 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4391 | load_long_binput(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4392 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4393 | PyObject *py_key = 0, *value = 0; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4394 | Py_ssize_t key; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4395 | unsigned char c; |
| 4396 | char *s; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4397 | Py_ssize_t len; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4398 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4399 | if (self->read_func(self, &s, 4) < 0) return -1; |
| 4400 | if (!( len=self->stack->length )) return stackUnderflow(); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4401 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4402 | c = (unsigned char)s[0]; |
| 4403 | key = (long)c; |
| 4404 | c = (unsigned char)s[1]; |
| 4405 | key |= (long)c << 8; |
| 4406 | c = (unsigned char)s[2]; |
| 4407 | key |= (long)c << 16; |
| 4408 | c = (unsigned char)s[3]; |
| 4409 | key |= (long)c << 24; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4410 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4411 | if (!( py_key = PyInt_FromLong(key))) return -1; |
| 4412 | value=self->stack->data[len-1]; |
| 4413 | len=PyDict_SetItem(self->memo, py_key, value); |
| 4414 | Py_DECREF(py_key); |
| 4415 | return len; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4416 | } |
| 4417 | |
| 4418 | |
| 4419 | static int |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4420 | do_append(Unpicklerobject *self, Py_ssize_t x) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4421 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4422 | PyObject *value = 0, *list = 0, *append_method = 0; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4423 | Py_ssize_t len, i; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4424 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4425 | len=self->stack->length; |
| 4426 | if (!( len >= x && x > 0 )) return stackUnderflow(); |
| 4427 | /* nothing to do */ |
| 4428 | if (len==x) return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4429 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4430 | list=self->stack->data[x-1]; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4431 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4432 | if (PyList_Check(list)) { |
| 4433 | PyObject *slice; |
| 4434 | int list_len; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4435 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4436 | slice=Pdata_popList(self->stack, x); |
| 4437 | if (! slice) return -1; |
| 4438 | list_len = PyList_GET_SIZE(list); |
| 4439 | i=PyList_SetSlice(list, list_len, list_len, slice); |
| 4440 | Py_DECREF(slice); |
| 4441 | return i; |
| 4442 | } |
| 4443 | else { |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4444 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4445 | if (!( append_method = PyObject_GetAttr(list, append_str))) |
| 4446 | return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4447 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4448 | for (i = x; i < len; i++) { |
| 4449 | PyObject *junk; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4450 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4451 | value=self->stack->data[i]; |
| 4452 | junk=0; |
| 4453 | ARG_TUP(self, value); |
| 4454 | if (self->arg) { |
| 4455 | junk = PyObject_Call(append_method, self->arg, |
| 4456 | NULL); |
| 4457 | FREE_ARG_TUP(self); |
| 4458 | } |
| 4459 | if (! junk) { |
| 4460 | Pdata_clear(self->stack, i+1); |
| 4461 | self->stack->length=x; |
| 4462 | Py_DECREF(append_method); |
| 4463 | return -1; |
| 4464 | } |
| 4465 | Py_DECREF(junk); |
| 4466 | } |
| 4467 | self->stack->length=x; |
| 4468 | Py_DECREF(append_method); |
| 4469 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4470 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4471 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4472 | } |
| 4473 | |
| 4474 | |
| 4475 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4476 | load_append(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4477 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4478 | return do_append(self, self->stack->length - 1); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4479 | } |
| 4480 | |
| 4481 | |
| 4482 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4483 | load_appends(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4484 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4485 | return do_append(self, marker(self)); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4486 | } |
| 4487 | |
| 4488 | |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4489 | static Py_ssize_t |
| 4490 | do_setitems(Unpicklerobject *self, Py_ssize_t x) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4491 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4492 | PyObject *value = 0, *key = 0, *dict = 0; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4493 | Py_ssize_t len, i, r=0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4494 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4495 | if (!( (len=self->stack->length) >= x |
| 4496 | && x > 0 )) return stackUnderflow(); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4497 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4498 | dict=self->stack->data[x-1]; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4499 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4500 | for (i = x+1; i < len; i += 2) { |
| 4501 | key =self->stack->data[i-1]; |
| 4502 | value=self->stack->data[i ]; |
| 4503 | if (PyObject_SetItem(dict, key, value) < 0) { |
| 4504 | r=-1; |
| 4505 | break; |
| 4506 | } |
| 4507 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4508 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4509 | Pdata_clear(self->stack, x); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4510 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4511 | return r; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4512 | } |
| 4513 | |
| 4514 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4515 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4516 | load_setitem(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4517 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4518 | return do_setitems(self, self->stack->length - 2); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4519 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4520 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4521 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4522 | load_setitems(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4523 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4524 | return do_setitems(self, marker(self)); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4525 | } |
| 4526 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4527 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4528 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4529 | load_build(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4530 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4531 | PyObject *state, *inst, *slotstate; |
| 4532 | PyObject *__setstate__; |
| 4533 | PyObject *d_key, *d_value; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4534 | int res = -1; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4535 | Py_ssize_t i; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4536 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4537 | /* Stack is ... instance, state. We want to leave instance at |
| 4538 | * the stack top, possibly mutated via instance.__setstate__(state). |
| 4539 | */ |
| 4540 | if (self->stack->length < 2) |
| 4541 | return stackUnderflow(); |
| 4542 | PDATA_POP(self->stack, state); |
| 4543 | if (state == NULL) |
| 4544 | return -1; |
| 4545 | inst = self->stack->data[self->stack->length - 1]; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4546 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4547 | __setstate__ = PyObject_GetAttr(inst, __setstate___str); |
| 4548 | if (__setstate__ != NULL) { |
| 4549 | PyObject *junk = NULL; |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 4550 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4551 | /* The explicit __setstate__ is responsible for everything. */ |
| 4552 | ARG_TUP(self, state); |
| 4553 | if (self->arg) { |
| 4554 | junk = PyObject_Call(__setstate__, self->arg, NULL); |
| 4555 | FREE_ARG_TUP(self); |
| 4556 | } |
| 4557 | Py_DECREF(__setstate__); |
| 4558 | if (junk == NULL) |
| 4559 | return -1; |
| 4560 | Py_DECREF(junk); |
| 4561 | return 0; |
| 4562 | } |
| 4563 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 4564 | return -1; |
| 4565 | PyErr_Clear(); |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 4566 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4567 | /* A default __setstate__. First see whether state embeds a |
| 4568 | * slot state dict too (a proto 2 addition). |
| 4569 | */ |
| 4570 | if (PyTuple_Check(state) && PyTuple_Size(state) == 2) { |
| 4571 | PyObject *temp = state; |
| 4572 | state = PyTuple_GET_ITEM(temp, 0); |
| 4573 | slotstate = PyTuple_GET_ITEM(temp, 1); |
| 4574 | Py_INCREF(state); |
| 4575 | Py_INCREF(slotstate); |
| 4576 | Py_DECREF(temp); |
| 4577 | } |
| 4578 | else |
| 4579 | slotstate = NULL; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4580 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4581 | /* Set inst.__dict__ from the state dict (if any). */ |
| 4582 | if (state != Py_None) { |
| 4583 | PyObject *dict; |
| 4584 | if (! PyDict_Check(state)) { |
| 4585 | PyErr_SetString(UnpicklingError, "state is not a " |
| 4586 | "dictionary"); |
| 4587 | goto finally; |
| 4588 | } |
| 4589 | dict = PyObject_GetAttr(inst, __dict___str); |
| 4590 | if (dict == NULL) |
| 4591 | goto finally; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4592 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4593 | i = 0; |
| 4594 | while (PyDict_Next(state, &i, &d_key, &d_value)) { |
| 4595 | /* normally the keys for instance attributes are |
| 4596 | interned. we should try to do that here. */ |
| 4597 | Py_INCREF(d_key); |
| 4598 | if (PyString_CheckExact(d_key)) |
| 4599 | PyString_InternInPlace(&d_key); |
| 4600 | if (PyObject_SetItem(dict, d_key, d_value) < 0) { |
| 4601 | Py_DECREF(d_key); |
| 4602 | goto finally; |
| 4603 | } |
| 4604 | Py_DECREF(d_key); |
| 4605 | } |
| 4606 | Py_DECREF(dict); |
| 4607 | } |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 4608 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4609 | /* Also set instance attributes from the slotstate dict (if any). */ |
| 4610 | if (slotstate != NULL) { |
| 4611 | if (! PyDict_Check(slotstate)) { |
| 4612 | PyErr_SetString(UnpicklingError, "slot state is not " |
| 4613 | "a dictionary"); |
| 4614 | goto finally; |
| 4615 | } |
| 4616 | i = 0; |
| 4617 | while (PyDict_Next(slotstate, &i, &d_key, &d_value)) { |
| 4618 | if (PyObject_SetAttr(inst, d_key, d_value) < 0) |
| 4619 | goto finally; |
| 4620 | } |
| 4621 | } |
| 4622 | res = 0; |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 4623 | |
| 4624 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4625 | Py_DECREF(state); |
| 4626 | Py_XDECREF(slotstate); |
| 4627 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4628 | } |
| 4629 | |
| 4630 | |
| 4631 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4632 | load_mark(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4633 | { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4634 | Py_ssize_t s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4635 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4636 | /* Note that we split the (pickle.py) stack into two stacks, an |
| 4637 | object stack and a mark stack. Here we push a mark onto the |
| 4638 | mark stack. |
| 4639 | */ |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4640 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4641 | if ((self->num_marks + 1) >= self->marks_size) { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4642 | Py_ssize_t *marks; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4643 | s=self->marks_size+20; |
| 4644 | if (s <= self->num_marks) s=self->num_marks + 1; |
| 4645 | if (self->marks == NULL) |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4646 | marks=(Py_ssize_t *)malloc(s * sizeof(Py_ssize_t)); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4647 | else |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4648 | marks=(Py_ssize_t *)realloc(self->marks, |
| 4649 | s * sizeof(Py_ssize_t)); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4650 | if (!marks) { |
| 4651 | PyErr_NoMemory(); |
| 4652 | return -1; |
| 4653 | } |
| 4654 | self->marks = marks; |
| 4655 | self->marks_size = s; |
| 4656 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4657 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4658 | self->marks[self->num_marks++] = self->stack->length; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4659 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4660 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4661 | } |
| 4662 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4663 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4664 | load_reduce(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4665 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4666 | PyObject *callable = 0, *arg_tup = 0, *ob = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4667 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4668 | PDATA_POP(self->stack, arg_tup); |
| 4669 | if (! arg_tup) return -1; |
| 4670 | PDATA_POP(self->stack, callable); |
| 4671 | if (callable) { |
| 4672 | ob = Instance_New(callable, arg_tup); |
| 4673 | Py_DECREF(callable); |
| 4674 | } |
| 4675 | Py_DECREF(arg_tup); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4676 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4677 | if (! ob) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4678 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4679 | PDATA_PUSH(self->stack, ob, -1); |
| 4680 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4681 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4682 | |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 4683 | /* Just raises an error if we don't know the protocol specified. PROTO |
| 4684 | * is the first opcode for protocols >= 2. |
| 4685 | */ |
| 4686 | static int |
| 4687 | load_proto(Unpicklerobject *self) |
| 4688 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4689 | int i; |
| 4690 | char *protobyte; |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 4691 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4692 | i = self->read_func(self, &protobyte, 1); |
| 4693 | if (i < 0) |
| 4694 | return -1; |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 4695 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4696 | i = calc_binint(protobyte, 1); |
| 4697 | /* No point checking for < 0, since calc_binint returns an unsigned |
| 4698 | * int when chewing on 1 byte. |
| 4699 | */ |
| 4700 | assert(i >= 0); |
| 4701 | if (i <= HIGHEST_PROTOCOL) |
| 4702 | return 0; |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 4703 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4704 | PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i); |
| 4705 | return -1; |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 4706 | } |
| 4707 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4708 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4709 | load(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4710 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4711 | PyObject *err = 0, *val = 0; |
| 4712 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4713 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4714 | self->num_marks = 0; |
| 4715 | if (self->stack->length) Pdata_clear(self->stack, 0); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4716 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4717 | while (1) { |
| 4718 | if (self->read_func(self, &s, 1) < 0) |
| 4719 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4720 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4721 | switch (s[0]) { |
| 4722 | case NONE: |
| 4723 | if (load_none(self) < 0) |
| 4724 | break; |
| 4725 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4726 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4727 | case BININT: |
| 4728 | if (load_binint(self) < 0) |
| 4729 | break; |
| 4730 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4731 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4732 | case BININT1: |
| 4733 | if (load_binint1(self) < 0) |
| 4734 | break; |
| 4735 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4736 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4737 | case BININT2: |
| 4738 | if (load_binint2(self) < 0) |
| 4739 | break; |
| 4740 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4741 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4742 | case INT: |
| 4743 | if (load_int(self) < 0) |
| 4744 | break; |
| 4745 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4746 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4747 | case LONG: |
| 4748 | if (load_long(self) < 0) |
| 4749 | break; |
| 4750 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4751 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4752 | case LONG1: |
| 4753 | if (load_counted_long(self, 1) < 0) |
| 4754 | break; |
| 4755 | continue; |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 4756 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4757 | case LONG4: |
| 4758 | if (load_counted_long(self, 4) < 0) |
| 4759 | break; |
| 4760 | continue; |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 4761 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4762 | case FLOAT: |
| 4763 | if (load_float(self) < 0) |
| 4764 | break; |
| 4765 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4766 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4767 | case BINFLOAT: |
| 4768 | if (load_binfloat(self) < 0) |
| 4769 | break; |
| 4770 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4771 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4772 | case BINSTRING: |
| 4773 | if (load_binstring(self) < 0) |
| 4774 | break; |
| 4775 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4776 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4777 | case SHORT_BINSTRING: |
| 4778 | if (load_short_binstring(self) < 0) |
| 4779 | break; |
| 4780 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4781 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4782 | case STRING: |
| 4783 | if (load_string(self) < 0) |
| 4784 | break; |
| 4785 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4786 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4787 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4788 | case UNICODE: |
| 4789 | if (load_unicode(self) < 0) |
| 4790 | break; |
| 4791 | continue; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 4792 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4793 | case BINUNICODE: |
| 4794 | if (load_binunicode(self) < 0) |
| 4795 | break; |
| 4796 | continue; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4797 | #endif |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 4798 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4799 | case EMPTY_TUPLE: |
| 4800 | if (load_counted_tuple(self, 0) < 0) |
| 4801 | break; |
| 4802 | continue; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 4803 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4804 | case TUPLE1: |
| 4805 | if (load_counted_tuple(self, 1) < 0) |
| 4806 | break; |
| 4807 | continue; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 4808 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4809 | case TUPLE2: |
| 4810 | if (load_counted_tuple(self, 2) < 0) |
| 4811 | break; |
| 4812 | continue; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 4813 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4814 | case TUPLE3: |
| 4815 | if (load_counted_tuple(self, 3) < 0) |
| 4816 | break; |
| 4817 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4818 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4819 | case TUPLE: |
| 4820 | if (load_tuple(self) < 0) |
| 4821 | break; |
| 4822 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4823 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4824 | case EMPTY_LIST: |
| 4825 | if (load_empty_list(self) < 0) |
| 4826 | break; |
| 4827 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4828 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4829 | case LIST: |
| 4830 | if (load_list(self) < 0) |
| 4831 | break; |
| 4832 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4833 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4834 | case EMPTY_DICT: |
| 4835 | if (load_empty_dict(self) < 0) |
| 4836 | break; |
| 4837 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4838 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4839 | case DICT: |
| 4840 | if (load_dict(self) < 0) |
| 4841 | break; |
| 4842 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4843 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4844 | case OBJ: |
| 4845 | if (load_obj(self) < 0) |
| 4846 | break; |
| 4847 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4848 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4849 | case INST: |
| 4850 | if (load_inst(self) < 0) |
| 4851 | break; |
| 4852 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4853 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4854 | case NEWOBJ: |
| 4855 | if (load_newobj(self) < 0) |
| 4856 | break; |
| 4857 | continue; |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 4858 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4859 | case GLOBAL: |
| 4860 | if (load_global(self) < 0) |
| 4861 | break; |
| 4862 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4863 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4864 | case APPEND: |
| 4865 | if (load_append(self) < 0) |
| 4866 | break; |
| 4867 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4868 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4869 | case APPENDS: |
| 4870 | if (load_appends(self) < 0) |
| 4871 | break; |
| 4872 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4873 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4874 | case BUILD: |
| 4875 | if (load_build(self) < 0) |
| 4876 | break; |
| 4877 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4878 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4879 | case DUP: |
| 4880 | if (load_dup(self) < 0) |
| 4881 | break; |
| 4882 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4883 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4884 | case BINGET: |
| 4885 | if (load_binget(self) < 0) |
| 4886 | break; |
| 4887 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4888 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4889 | case LONG_BINGET: |
| 4890 | if (load_long_binget(self) < 0) |
| 4891 | break; |
| 4892 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4893 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4894 | case GET: |
| 4895 | if (load_get(self) < 0) |
| 4896 | break; |
| 4897 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4898 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4899 | case EXT1: |
| 4900 | if (load_extension(self, 1) < 0) |
| 4901 | break; |
| 4902 | continue; |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 4903 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4904 | case EXT2: |
| 4905 | if (load_extension(self, 2) < 0) |
| 4906 | break; |
| 4907 | continue; |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 4908 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4909 | case EXT4: |
| 4910 | if (load_extension(self, 4) < 0) |
| 4911 | break; |
| 4912 | continue; |
| 4913 | case MARK: |
| 4914 | if (load_mark(self) < 0) |
| 4915 | break; |
| 4916 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4917 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4918 | case BINPUT: |
| 4919 | if (load_binput(self) < 0) |
| 4920 | break; |
| 4921 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4922 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4923 | case LONG_BINPUT: |
| 4924 | if (load_long_binput(self) < 0) |
| 4925 | break; |
| 4926 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4927 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4928 | case PUT: |
| 4929 | if (load_put(self) < 0) |
| 4930 | break; |
| 4931 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4932 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4933 | case POP: |
| 4934 | if (load_pop(self) < 0) |
| 4935 | break; |
| 4936 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4937 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4938 | case POP_MARK: |
| 4939 | if (load_pop_mark(self) < 0) |
| 4940 | break; |
| 4941 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4942 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4943 | case SETITEM: |
| 4944 | if (load_setitem(self) < 0) |
| 4945 | break; |
| 4946 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4947 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4948 | case SETITEMS: |
| 4949 | if (load_setitems(self) < 0) |
| 4950 | break; |
| 4951 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4952 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4953 | case STOP: |
| 4954 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4955 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4956 | case PERSID: |
| 4957 | if (load_persid(self) < 0) |
| 4958 | break; |
| 4959 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4960 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4961 | case BINPERSID: |
| 4962 | if (load_binpersid(self) < 0) |
| 4963 | break; |
| 4964 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4965 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4966 | case REDUCE: |
| 4967 | if (load_reduce(self) < 0) |
| 4968 | break; |
| 4969 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4970 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4971 | case PROTO: |
| 4972 | if (load_proto(self) < 0) |
| 4973 | break; |
| 4974 | continue; |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 4975 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4976 | case NEWTRUE: |
| 4977 | if (load_bool(self, Py_True) < 0) |
| 4978 | break; |
| 4979 | continue; |
Tim Peters | 3c67d79 | 2003-02-02 17:59:11 +0000 | [diff] [blame] | 4980 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4981 | case NEWFALSE: |
| 4982 | if (load_bool(self, Py_False) < 0) |
| 4983 | break; |
| 4984 | continue; |
Tim Peters | 3c67d79 | 2003-02-02 17:59:11 +0000 | [diff] [blame] | 4985 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4986 | case '\0': |
| 4987 | /* end of file */ |
| 4988 | PyErr_SetNone(PyExc_EOFError); |
| 4989 | break; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4990 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4991 | default: |
| 4992 | cPickle_ErrFormat(UnpicklingError, |
| 4993 | "invalid load key, '%s'.", |
| 4994 | "c", s[0]); |
| 4995 | return NULL; |
| 4996 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4997 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4998 | break; |
| 4999 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5000 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5001 | if ((err = PyErr_Occurred())) { |
| 5002 | if (err == PyExc_EOFError) { |
| 5003 | PyErr_SetNone(PyExc_EOFError); |
| 5004 | } |
| 5005 | return NULL; |
| 5006 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5007 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5008 | PDATA_POP(self->stack, val); |
| 5009 | return val; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5010 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5011 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5012 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5013 | /* No-load functions to support noload, which is used to |
| 5014 | find persistent references. */ |
| 5015 | |
| 5016 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5017 | noload_obj(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5018 | { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 5019 | Py_ssize_t i; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5020 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5021 | if ((i = marker(self)) < 0) return -1; |
| 5022 | return Pdata_clear(self->stack, i+1); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5023 | } |
| 5024 | |
| 5025 | |
| 5026 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5027 | noload_inst(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5028 | { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 5029 | Py_ssize_t i; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5030 | char *s; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5031 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5032 | if ((i = marker(self)) < 0) return -1; |
| 5033 | Pdata_clear(self->stack, i); |
| 5034 | if (self->readline_func(self, &s) < 0) return -1; |
| 5035 | if (self->readline_func(self, &s) < 0) return -1; |
| 5036 | PDATA_APPEND(self->stack, Py_None, -1); |
| 5037 | return 0; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5038 | } |
| 5039 | |
| 5040 | static int |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 5041 | noload_newobj(Unpicklerobject *self) |
| 5042 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5043 | PyObject *obj; |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 5044 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5045 | PDATA_POP(self->stack, obj); /* pop argtuple */ |
| 5046 | if (obj == NULL) return -1; |
| 5047 | Py_DECREF(obj); |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 5048 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5049 | PDATA_POP(self->stack, obj); /* pop cls */ |
| 5050 | if (obj == NULL) return -1; |
| 5051 | Py_DECREF(obj); |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 5052 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5053 | PDATA_APPEND(self->stack, Py_None, -1); |
| 5054 | return 0; |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 5055 | } |
| 5056 | |
| 5057 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5058 | noload_global(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5059 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5060 | char *s; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5061 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5062 | if (self->readline_func(self, &s) < 0) return -1; |
| 5063 | if (self->readline_func(self, &s) < 0) return -1; |
| 5064 | PDATA_APPEND(self->stack, Py_None,-1); |
| 5065 | return 0; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5066 | } |
| 5067 | |
| 5068 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5069 | noload_reduce(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5070 | { |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5071 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5072 | if (self->stack->length < 2) return stackUnderflow(); |
| 5073 | Pdata_clear(self->stack, self->stack->length-2); |
| 5074 | PDATA_APPEND(self->stack, Py_None,-1); |
| 5075 | return 0; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5076 | } |
| 5077 | |
| 5078 | static int |
| 5079 | noload_build(Unpicklerobject *self) { |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5080 | |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 5081 | if (self->stack->length < 1) return stackUnderflow(); |
| 5082 | Pdata_clear(self->stack, self->stack->length-1); |
Guido van Rossum | 50f385c | 1998-12-04 18:48:44 +0000 | [diff] [blame] | 5083 | return 0; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5084 | } |
| 5085 | |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 5086 | static int |
| 5087 | noload_extension(Unpicklerobject *self, int nbytes) |
| 5088 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5089 | char *codebytes; |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 5090 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5091 | assert(nbytes == 1 || nbytes == 2 || nbytes == 4); |
| 5092 | if (self->read_func(self, &codebytes, nbytes) < 0) return -1; |
| 5093 | PDATA_APPEND(self->stack, Py_None, -1); |
| 5094 | return 0; |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 5095 | } |
| 5096 | |
Neil Schemenauer | 973f8b4 | 2009-10-14 19:33:31 +0000 | [diff] [blame] | 5097 | static int |
| 5098 | noload_append(Unpicklerobject *self) |
| 5099 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5100 | return Pdata_clear(self->stack, self->stack->length - 1); |
Neil Schemenauer | 973f8b4 | 2009-10-14 19:33:31 +0000 | [diff] [blame] | 5101 | } |
| 5102 | |
| 5103 | static int |
| 5104 | noload_appends(Unpicklerobject *self) |
| 5105 | { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 5106 | Py_ssize_t i; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5107 | if ((i = marker(self)) < 0) return -1; |
| 5108 | return Pdata_clear(self->stack, i); |
Neil Schemenauer | 973f8b4 | 2009-10-14 19:33:31 +0000 | [diff] [blame] | 5109 | } |
| 5110 | |
| 5111 | static int |
| 5112 | noload_setitem(Unpicklerobject *self) |
| 5113 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5114 | return Pdata_clear(self->stack, self->stack->length - 2); |
Neil Schemenauer | 973f8b4 | 2009-10-14 19:33:31 +0000 | [diff] [blame] | 5115 | } |
| 5116 | |
| 5117 | static int |
| 5118 | noload_setitems(Unpicklerobject *self) |
| 5119 | { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 5120 | Py_ssize_t i; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5121 | if ((i = marker(self)) < 0) return -1; |
| 5122 | return Pdata_clear(self->stack, i); |
Neil Schemenauer | 973f8b4 | 2009-10-14 19:33:31 +0000 | [diff] [blame] | 5123 | } |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5124 | |
| 5125 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5126 | noload(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5127 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5128 | PyObject *err = 0, *val = 0; |
| 5129 | char *s; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5130 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5131 | self->num_marks = 0; |
| 5132 | Pdata_clear(self->stack, 0); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5133 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5134 | while (1) { |
| 5135 | if (self->read_func(self, &s, 1) < 0) |
| 5136 | break; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5137 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5138 | switch (s[0]) { |
| 5139 | case NONE: |
| 5140 | if (load_none(self) < 0) |
| 5141 | break; |
| 5142 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5143 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5144 | case BININT: |
| 5145 | if (load_binint(self) < 0) |
| 5146 | break; |
| 5147 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5148 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5149 | case BININT1: |
| 5150 | if (load_binint1(self) < 0) |
| 5151 | break; |
| 5152 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5153 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5154 | case BININT2: |
| 5155 | if (load_binint2(self) < 0) |
| 5156 | break; |
| 5157 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5158 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5159 | case INT: |
| 5160 | if (load_int(self) < 0) |
| 5161 | break; |
| 5162 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5163 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5164 | case LONG: |
| 5165 | if (load_long(self) < 0) |
| 5166 | break; |
| 5167 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5168 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5169 | case LONG1: |
| 5170 | if (load_counted_long(self, 1) < 0) |
| 5171 | break; |
| 5172 | continue; |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 5173 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5174 | case LONG4: |
| 5175 | if (load_counted_long(self, 4) < 0) |
| 5176 | break; |
| 5177 | continue; |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 5178 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5179 | case FLOAT: |
| 5180 | if (load_float(self) < 0) |
| 5181 | break; |
| 5182 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5183 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5184 | case BINFLOAT: |
| 5185 | if (load_binfloat(self) < 0) |
| 5186 | break; |
| 5187 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5188 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5189 | case BINSTRING: |
| 5190 | if (load_binstring(self) < 0) |
| 5191 | break; |
| 5192 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5193 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5194 | case SHORT_BINSTRING: |
| 5195 | if (load_short_binstring(self) < 0) |
| 5196 | break; |
| 5197 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5198 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5199 | case STRING: |
| 5200 | if (load_string(self) < 0) |
| 5201 | break; |
| 5202 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5203 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 5204 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5205 | case UNICODE: |
| 5206 | if (load_unicode(self) < 0) |
| 5207 | break; |
| 5208 | continue; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 5209 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5210 | case BINUNICODE: |
| 5211 | if (load_binunicode(self) < 0) |
| 5212 | break; |
| 5213 | continue; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 5214 | #endif |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 5215 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5216 | case EMPTY_TUPLE: |
| 5217 | if (load_counted_tuple(self, 0) < 0) |
| 5218 | break; |
| 5219 | continue; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 5220 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5221 | case TUPLE1: |
| 5222 | if (load_counted_tuple(self, 1) < 0) |
| 5223 | break; |
| 5224 | continue; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 5225 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5226 | case TUPLE2: |
| 5227 | if (load_counted_tuple(self, 2) < 0) |
| 5228 | break; |
| 5229 | continue; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 5230 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5231 | case TUPLE3: |
| 5232 | if (load_counted_tuple(self, 3) < 0) |
| 5233 | break; |
| 5234 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5235 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5236 | case TUPLE: |
| 5237 | if (load_tuple(self) < 0) |
| 5238 | break; |
| 5239 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5240 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5241 | case EMPTY_LIST: |
| 5242 | if (load_empty_list(self) < 0) |
| 5243 | break; |
| 5244 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5245 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5246 | case LIST: |
| 5247 | if (load_list(self) < 0) |
| 5248 | break; |
| 5249 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5250 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5251 | case EMPTY_DICT: |
| 5252 | if (load_empty_dict(self) < 0) |
| 5253 | break; |
| 5254 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5255 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5256 | case DICT: |
| 5257 | if (load_dict(self) < 0) |
| 5258 | break; |
| 5259 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5260 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5261 | case OBJ: |
| 5262 | if (noload_obj(self) < 0) |
| 5263 | break; |
| 5264 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5265 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5266 | case INST: |
| 5267 | if (noload_inst(self) < 0) |
| 5268 | break; |
| 5269 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5270 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5271 | case NEWOBJ: |
| 5272 | if (noload_newobj(self) < 0) |
| 5273 | break; |
| 5274 | continue; |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 5275 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5276 | case GLOBAL: |
| 5277 | if (noload_global(self) < 0) |
| 5278 | break; |
| 5279 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5280 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5281 | case APPEND: |
| 5282 | if (noload_append(self) < 0) |
| 5283 | break; |
| 5284 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5285 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5286 | case APPENDS: |
| 5287 | if (noload_appends(self) < 0) |
| 5288 | break; |
| 5289 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5290 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5291 | case BUILD: |
| 5292 | if (noload_build(self) < 0) |
| 5293 | break; |
| 5294 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5295 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5296 | case DUP: |
| 5297 | if (load_dup(self) < 0) |
| 5298 | break; |
| 5299 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5300 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5301 | case BINGET: |
| 5302 | if (load_binget(self) < 0) |
| 5303 | break; |
| 5304 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5305 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5306 | case LONG_BINGET: |
| 5307 | if (load_long_binget(self) < 0) |
| 5308 | break; |
| 5309 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5310 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5311 | case GET: |
| 5312 | if (load_get(self) < 0) |
| 5313 | break; |
| 5314 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5315 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5316 | case EXT1: |
| 5317 | if (noload_extension(self, 1) < 0) |
| 5318 | break; |
| 5319 | continue; |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 5320 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5321 | case EXT2: |
| 5322 | if (noload_extension(self, 2) < 0) |
| 5323 | break; |
| 5324 | continue; |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 5325 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5326 | case EXT4: |
| 5327 | if (noload_extension(self, 4) < 0) |
| 5328 | break; |
| 5329 | continue; |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 5330 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5331 | case MARK: |
| 5332 | if (load_mark(self) < 0) |
| 5333 | break; |
| 5334 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5335 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5336 | case BINPUT: |
| 5337 | if (load_binput(self) < 0) |
| 5338 | break; |
| 5339 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5340 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5341 | case LONG_BINPUT: |
| 5342 | if (load_long_binput(self) < 0) |
| 5343 | break; |
| 5344 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5345 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5346 | case PUT: |
| 5347 | if (load_put(self) < 0) |
| 5348 | break; |
| 5349 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5350 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5351 | case POP: |
| 5352 | if (load_pop(self) < 0) |
| 5353 | break; |
| 5354 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5355 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5356 | case POP_MARK: |
| 5357 | if (load_pop_mark(self) < 0) |
| 5358 | break; |
| 5359 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5360 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5361 | case SETITEM: |
| 5362 | if (noload_setitem(self) < 0) |
| 5363 | break; |
| 5364 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5365 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5366 | case SETITEMS: |
| 5367 | if (noload_setitems(self) < 0) |
| 5368 | break; |
| 5369 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5370 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5371 | case STOP: |
| 5372 | break; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5373 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5374 | case PERSID: |
| 5375 | if (load_persid(self) < 0) |
| 5376 | break; |
| 5377 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5378 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5379 | case BINPERSID: |
| 5380 | if (load_binpersid(self) < 0) |
| 5381 | break; |
| 5382 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5383 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5384 | case REDUCE: |
| 5385 | if (noload_reduce(self) < 0) |
| 5386 | break; |
| 5387 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5388 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5389 | case PROTO: |
| 5390 | if (load_proto(self) < 0) |
| 5391 | break; |
| 5392 | continue; |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 5393 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5394 | case NEWTRUE: |
| 5395 | if (load_bool(self, Py_True) < 0) |
| 5396 | break; |
| 5397 | continue; |
Tim Peters | 3c67d79 | 2003-02-02 17:59:11 +0000 | [diff] [blame] | 5398 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5399 | case NEWFALSE: |
| 5400 | if (load_bool(self, Py_False) < 0) |
| 5401 | break; |
| 5402 | continue; |
| 5403 | default: |
| 5404 | cPickle_ErrFormat(UnpicklingError, |
| 5405 | "invalid load key, '%s'.", |
| 5406 | "c", s[0]); |
| 5407 | return NULL; |
| 5408 | } |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5409 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5410 | break; |
| 5411 | } |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5412 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5413 | if ((err = PyErr_Occurred())) { |
| 5414 | if (err == PyExc_EOFError) { |
| 5415 | PyErr_SetNone(PyExc_EOFError); |
| 5416 | } |
| 5417 | return NULL; |
| 5418 | } |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5419 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5420 | PDATA_POP(self->stack, val); |
| 5421 | return val; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5422 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5423 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5424 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5425 | static PyObject * |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 5426 | Unpickler_load(Unpicklerobject *self, PyObject *unused) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5427 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5428 | return load(self); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5429 | } |
| 5430 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5431 | static PyObject * |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 5432 | Unpickler_noload(Unpicklerobject *self, PyObject *unused) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5433 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5434 | return noload(self); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5435 | } |
| 5436 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5437 | |
| 5438 | static struct PyMethodDef Unpickler_methods[] = { |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 5439 | {"load", (PyCFunction)Unpickler_load, METH_NOARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 5440 | PyDoc_STR("load() -- Load a pickle") |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5441 | }, |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 5442 | {"noload", (PyCFunction)Unpickler_noload, METH_NOARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 5443 | PyDoc_STR( |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5444 | "noload() -- not load a pickle, but go through most of the motions\n" |
| 5445 | "\n" |
| 5446 | "This function can be used to read past a pickle without instantiating\n" |
| 5447 | "any objects or importing any modules. It can also be used to find all\n" |
| 5448 | "persistent references without instantiating any objects or importing\n" |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 5449 | "any modules.\n") |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5450 | }, |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5451 | {NULL, NULL} /* sentinel */ |
| 5452 | }; |
| 5453 | |
| 5454 | |
| 5455 | static Unpicklerobject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5456 | newUnpicklerobject(PyObject *f) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5457 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5458 | Unpicklerobject *self; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5459 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5460 | if (!( self = PyObject_GC_New(Unpicklerobject, &Unpicklertype))) |
| 5461 | return NULL; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5462 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5463 | self->file = NULL; |
| 5464 | self->arg = NULL; |
| 5465 | self->stack = (Pdata*)Pdata_New(); |
| 5466 | self->pers_func = NULL; |
| 5467 | self->last_string = NULL; |
| 5468 | self->marks = NULL; |
| 5469 | self->num_marks = 0; |
| 5470 | self->marks_size = 0; |
| 5471 | self->buf_size = 0; |
| 5472 | self->read = NULL; |
| 5473 | self->readline = NULL; |
| 5474 | self->find_class = NULL; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5475 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5476 | if (!( self->memo = PyDict_New())) |
| 5477 | goto err; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5478 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5479 | if (!self->stack) |
| 5480 | goto err; |
Neal Norwitz | b59d08c | 2006-07-22 16:20:49 +0000 | [diff] [blame] | 5481 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5482 | Py_INCREF(f); |
| 5483 | self->file = f; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5484 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5485 | /* Set read, readline based on type of f */ |
| 5486 | if (PyFile_Check(f)) { |
| 5487 | self->fp = PyFile_AsFile(f); |
| 5488 | if (self->fp == NULL) { |
| 5489 | PyErr_SetString(PyExc_ValueError, |
| 5490 | "I/O operation on closed file"); |
| 5491 | goto err; |
| 5492 | } |
| 5493 | self->read_func = read_file; |
| 5494 | self->readline_func = readline_file; |
| 5495 | } |
| 5496 | else if (PycStringIO_InputCheck(f)) { |
| 5497 | self->fp = NULL; |
| 5498 | self->read_func = read_cStringIO; |
| 5499 | self->readline_func = readline_cStringIO; |
| 5500 | } |
| 5501 | else { |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5502 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5503 | self->fp = NULL; |
| 5504 | self->read_func = read_other; |
| 5505 | self->readline_func = readline_other; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5506 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5507 | if (!( (self->readline = PyObject_GetAttr(f, readline_str)) && |
| 5508 | (self->read = PyObject_GetAttr(f, read_str)))) { |
| 5509 | PyErr_Clear(); |
| 5510 | PyErr_SetString( PyExc_TypeError, |
| 5511 | "argument must have 'read' and " |
| 5512 | "'readline' attributes" ); |
| 5513 | goto err; |
| 5514 | } |
| 5515 | } |
| 5516 | PyObject_GC_Track(self); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5517 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5518 | return self; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5519 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5520 | err: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5521 | Py_DECREF((PyObject *)self); |
| 5522 | return NULL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5523 | } |
| 5524 | |
| 5525 | |
| 5526 | static PyObject * |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 5527 | get_Unpickler(PyObject *self, PyObject *file) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5528 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5529 | return (PyObject *)newUnpicklerobject(file); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5530 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5531 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5532 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5533 | static void |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5534 | Unpickler_dealloc(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5535 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5536 | PyObject_GC_UnTrack((PyObject *)self); |
| 5537 | Py_XDECREF(self->readline); |
| 5538 | Py_XDECREF(self->read); |
| 5539 | Py_XDECREF(self->file); |
| 5540 | Py_XDECREF(self->memo); |
| 5541 | Py_XDECREF(self->stack); |
| 5542 | Py_XDECREF(self->pers_func); |
| 5543 | Py_XDECREF(self->arg); |
| 5544 | Py_XDECREF(self->last_string); |
| 5545 | Py_XDECREF(self->find_class); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5546 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5547 | if (self->marks) { |
| 5548 | free(self->marks); |
| 5549 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5550 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5551 | if (self->buf_size) { |
| 5552 | free(self->buf); |
| 5553 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5554 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5555 | Py_TYPE(self)->tp_free((PyObject *)self); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5556 | } |
| 5557 | |
Jeremy Hylton | 7b5ce7f | 2003-04-09 21:25:30 +0000 | [diff] [blame] | 5558 | static int |
| 5559 | Unpickler_traverse(Unpicklerobject *self, visitproc visit, void *arg) |
| 5560 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5561 | Py_VISIT(self->readline); |
| 5562 | Py_VISIT(self->read); |
| 5563 | Py_VISIT(self->file); |
| 5564 | Py_VISIT(self->memo); |
| 5565 | Py_VISIT(self->stack); |
| 5566 | Py_VISIT(self->pers_func); |
| 5567 | Py_VISIT(self->arg); |
| 5568 | Py_VISIT(self->last_string); |
| 5569 | Py_VISIT(self->find_class); |
| 5570 | return 0; |
Jeremy Hylton | 7b5ce7f | 2003-04-09 21:25:30 +0000 | [diff] [blame] | 5571 | } |
| 5572 | |
| 5573 | static int |
| 5574 | Unpickler_clear(Unpicklerobject *self) |
| 5575 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5576 | Py_CLEAR(self->readline); |
| 5577 | Py_CLEAR(self->read); |
| 5578 | Py_CLEAR(self->file); |
| 5579 | Py_CLEAR(self->memo); |
| 5580 | Py_CLEAR(self->stack); |
| 5581 | Py_CLEAR(self->pers_func); |
| 5582 | Py_CLEAR(self->arg); |
| 5583 | Py_CLEAR(self->last_string); |
| 5584 | Py_CLEAR(self->find_class); |
| 5585 | return 0; |
Jeremy Hylton | 7b5ce7f | 2003-04-09 21:25:30 +0000 | [diff] [blame] | 5586 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5587 | |
| 5588 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5589 | Unpickler_getattr(Unpicklerobject *self, char *name) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5590 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5591 | if (!strcmp(name, "persistent_load")) { |
| 5592 | if (!self->pers_func) { |
| 5593 | PyErr_SetString(PyExc_AttributeError, name); |
| 5594 | return NULL; |
| 5595 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5596 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5597 | Py_INCREF(self->pers_func); |
| 5598 | return self->pers_func; |
| 5599 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5600 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5601 | if (!strcmp(name, "find_global")) { |
| 5602 | if (!self->find_class) { |
| 5603 | PyErr_SetString(PyExc_AttributeError, name); |
| 5604 | return NULL; |
| 5605 | } |
Guido van Rossum | 1b9e0aa | 1999-04-19 17:58:18 +0000 | [diff] [blame] | 5606 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5607 | Py_INCREF(self->find_class); |
| 5608 | return self->find_class; |
| 5609 | } |
Guido van Rossum | 1b9e0aa | 1999-04-19 17:58:18 +0000 | [diff] [blame] | 5610 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5611 | if (!strcmp(name, "memo")) { |
| 5612 | if (!self->memo) { |
| 5613 | PyErr_SetString(PyExc_AttributeError, name); |
| 5614 | return NULL; |
| 5615 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5616 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5617 | Py_INCREF(self->memo); |
| 5618 | return self->memo; |
| 5619 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5620 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5621 | if (!strcmp(name, "UnpicklingError")) { |
| 5622 | Py_INCREF(UnpicklingError); |
| 5623 | return UnpicklingError; |
| 5624 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5625 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5626 | return Py_FindMethod(Unpickler_methods, (PyObject *)self, name); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5627 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5628 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5629 | |
| 5630 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5631 | Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5632 | { |
Jeremy Hylton | ce616e4 | 1998-08-13 23:13:52 +0000 | [diff] [blame] | 5633 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5634 | if (!strcmp(name, "persistent_load")) { |
| 5635 | Py_XDECREF(self->pers_func); |
| 5636 | self->pers_func = value; |
| 5637 | Py_XINCREF(value); |
| 5638 | return 0; |
| 5639 | } |
Guido van Rossum | 1b9e0aa | 1999-04-19 17:58:18 +0000 | [diff] [blame] | 5640 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5641 | if (!strcmp(name, "find_global")) { |
| 5642 | Py_XDECREF(self->find_class); |
| 5643 | self->find_class = value; |
| 5644 | Py_XINCREF(value); |
| 5645 | return 0; |
| 5646 | } |
Guido van Rossum | 1b9e0aa | 1999-04-19 17:58:18 +0000 | [diff] [blame] | 5647 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5648 | if (! value) { |
| 5649 | PyErr_SetString(PyExc_TypeError, |
| 5650 | "attribute deletion is not supported"); |
| 5651 | return -1; |
| 5652 | } |
Jeremy Hylton | ce616e4 | 1998-08-13 23:13:52 +0000 | [diff] [blame] | 5653 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5654 | if (strcmp(name, "memo") == 0) { |
| 5655 | if (!PyDict_Check(value)) { |
| 5656 | PyErr_SetString(PyExc_TypeError, |
| 5657 | "memo must be a dictionary"); |
| 5658 | return -1; |
| 5659 | } |
| 5660 | Py_XDECREF(self->memo); |
| 5661 | self->memo = value; |
| 5662 | Py_INCREF(value); |
| 5663 | return 0; |
| 5664 | } |
Jeremy Hylton | ce616e4 | 1998-08-13 23:13:52 +0000 | [diff] [blame] | 5665 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5666 | PyErr_SetString(PyExc_AttributeError, name); |
| 5667 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5668 | } |
| 5669 | |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5670 | /* --------------------------------------------------------------------------- |
| 5671 | * Module-level functions. |
| 5672 | */ |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5673 | |
Martin v. Löwis | 544f119 | 2004-07-27 05:22:33 +0000 | [diff] [blame] | 5674 | /* dump(obj, file, protocol=0). */ |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5675 | static PyObject * |
Martin v. Löwis | 544f119 | 2004-07-27 05:22:33 +0000 | [diff] [blame] | 5676 | cpm_dump(PyObject *self, PyObject *args, PyObject *kwds) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5677 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5678 | static char *kwlist[] = {"obj", "file", "protocol", NULL}; |
| 5679 | PyObject *ob, *file, *res = NULL; |
| 5680 | Picklerobject *pickler = 0; |
| 5681 | int proto = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5682 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5683 | if (!( PyArg_ParseTupleAndKeywords(args, kwds, "OO|i", kwlist, |
| 5684 | &ob, &file, &proto))) |
| 5685 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5686 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5687 | if (!( pickler = newPicklerobject(file, proto))) |
| 5688 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5689 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5690 | if (dump(pickler, ob) < 0) |
| 5691 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5692 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5693 | Py_INCREF(Py_None); |
| 5694 | res = Py_None; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5695 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5696 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5697 | Py_XDECREF(pickler); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5698 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5699 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5700 | } |
| 5701 | |
| 5702 | |
Martin v. Löwis | 544f119 | 2004-07-27 05:22:33 +0000 | [diff] [blame] | 5703 | /* dumps(obj, protocol=0). */ |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5704 | static PyObject * |
Martin v. Löwis | 544f119 | 2004-07-27 05:22:33 +0000 | [diff] [blame] | 5705 | cpm_dumps(PyObject *self, PyObject *args, PyObject *kwds) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5706 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5707 | static char *kwlist[] = {"obj", "protocol", NULL}; |
| 5708 | PyObject *ob, *file = 0, *res = NULL; |
| 5709 | Picklerobject *pickler = 0; |
| 5710 | int proto = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5711 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5712 | if (!( PyArg_ParseTupleAndKeywords(args, kwds, "O|i:dumps", kwlist, |
| 5713 | &ob, &proto))) |
| 5714 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5715 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5716 | if (!( file = PycStringIO->NewOutput(128))) |
| 5717 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5718 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5719 | if (!( pickler = newPicklerobject(file, proto))) |
| 5720 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5721 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5722 | if (dump(pickler, ob) < 0) |
| 5723 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5724 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5725 | res = PycStringIO->cgetvalue(file); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5726 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5727 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5728 | Py_XDECREF(pickler); |
| 5729 | Py_XDECREF(file); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5730 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5731 | return res; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5732 | } |
| 5733 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5734 | |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5735 | /* load(fileobj). */ |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5736 | static PyObject * |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 5737 | cpm_load(PyObject *self, PyObject *ob) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5738 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5739 | Unpicklerobject *unpickler = 0; |
| 5740 | PyObject *res = NULL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5741 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5742 | if (!( unpickler = newUnpicklerobject(ob))) |
| 5743 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5744 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5745 | res = load(unpickler); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5746 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5747 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5748 | Py_XDECREF(unpickler); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5749 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5750 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5751 | } |
| 5752 | |
| 5753 | |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5754 | /* loads(string) */ |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5755 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5756 | cpm_loads(PyObject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5757 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5758 | PyObject *ob, *file = 0, *res = NULL; |
| 5759 | Unpicklerobject *unpickler = 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5760 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5761 | if (!( PyArg_ParseTuple(args, "S:loads", &ob))) |
| 5762 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5763 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5764 | if (!( file = PycStringIO->NewInput(ob))) |
| 5765 | goto finally; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5766 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5767 | if (!( unpickler = newUnpicklerobject(file))) |
| 5768 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5769 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5770 | res = load(unpickler); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5771 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5772 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5773 | Py_XDECREF(file); |
| 5774 | Py_XDECREF(unpickler); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5775 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5776 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5777 | } |
| 5778 | |
| 5779 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5780 | PyDoc_STRVAR(Unpicklertype__doc__, |
| 5781 | "Objects that know how to unpickle"); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5782 | |
Guido van Rossum | 9716aaa | 1997-12-08 15:15:16 +0000 | [diff] [blame] | 5783 | static PyTypeObject Unpicklertype = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 5784 | PyVarObject_HEAD_INIT(NULL, 0) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5785 | "cPickle.Unpickler", /*tp_name*/ |
Jeremy Hylton | 7b5ce7f | 2003-04-09 21:25:30 +0000 | [diff] [blame] | 5786 | sizeof(Unpicklerobject), /*tp_basicsize*/ |
| 5787 | 0, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5788 | (destructor)Unpickler_dealloc, /* tp_dealloc */ |
| 5789 | 0, /* tp_print */ |
| 5790 | (getattrfunc)Unpickler_getattr, /* tp_getattr */ |
| 5791 | (setattrfunc)Unpickler_setattr, /* tp_setattr */ |
| 5792 | 0, /* tp_compare */ |
| 5793 | 0, /* tp_repr */ |
| 5794 | 0, /* tp_as_number */ |
| 5795 | 0, /* tp_as_sequence */ |
| 5796 | 0, /* tp_as_mapping */ |
| 5797 | 0, /* tp_hash */ |
| 5798 | 0, /* tp_call */ |
| 5799 | 0, /* tp_str */ |
| 5800 | 0, /* tp_getattro */ |
| 5801 | 0, /* tp_setattro */ |
| 5802 | 0, /* tp_as_buffer */ |
Jeremy Hylton | 7b5ce7f | 2003-04-09 21:25:30 +0000 | [diff] [blame] | 5803 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5804 | Unpicklertype__doc__, /* tp_doc */ |
| 5805 | (traverseproc)Unpickler_traverse, /* tp_traverse */ |
| 5806 | (inquiry)Unpickler_clear, /* tp_clear */ |
Guido van Rossum | 9716aaa | 1997-12-08 15:15:16 +0000 | [diff] [blame] | 5807 | }; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5808 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5809 | static struct PyMethodDef cPickle_methods[] = { |
Martin v. Löwis | 544f119 | 2004-07-27 05:22:33 +0000 | [diff] [blame] | 5810 | {"dump", (PyCFunction)cpm_dump, METH_VARARGS | METH_KEYWORDS, |
| 5811 | PyDoc_STR("dump(obj, file, protocol=0) -- " |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5812 | "Write an object in pickle format to the given file.\n" |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5813 | "\n" |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5814 | "See the Pickler docstring for the meaning of optional argument proto.") |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5815 | }, |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5816 | |
Martin v. Löwis | 544f119 | 2004-07-27 05:22:33 +0000 | [diff] [blame] | 5817 | {"dumps", (PyCFunction)cpm_dumps, METH_VARARGS | METH_KEYWORDS, |
| 5818 | PyDoc_STR("dumps(obj, protocol=0) -- " |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5819 | "Return a string containing an object in pickle format.\n" |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5820 | "\n" |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5821 | "See the Pickler docstring for the meaning of optional argument proto.") |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5822 | }, |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5823 | |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 5824 | {"load", (PyCFunction)cpm_load, METH_O, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 5825 | PyDoc_STR("load(file) -- Load a pickle from the given file")}, |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5826 | |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 5827 | {"loads", (PyCFunction)cpm_loads, METH_VARARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 5828 | PyDoc_STR("loads(string) -- Load a pickle from the given string")}, |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5829 | |
Martin v. Löwis | 544f119 | 2004-07-27 05:22:33 +0000 | [diff] [blame] | 5830 | {"Pickler", (PyCFunction)get_Pickler, METH_VARARGS | METH_KEYWORDS, |
| 5831 | PyDoc_STR("Pickler(file, protocol=0) -- Create a pickler.\n" |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5832 | "\n" |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5833 | "This takes a file-like object for writing a pickle data stream.\n" |
| 5834 | "The optional proto argument tells the pickler to use the given\n" |
| 5835 | "protocol; supported protocols are 0, 1, 2. The default\n" |
| 5836 | "protocol is 0, to be backwards compatible. (Protocol 0 is the\n" |
| 5837 | "only protocol that can be written to a file opened in text\n" |
| 5838 | "mode and read back successfully. When using a protocol higher\n" |
| 5839 | "than 0, make sure the file is opened in binary mode, both when\n" |
| 5840 | "pickling and unpickling.)\n" |
| 5841 | "\n" |
| 5842 | "Protocol 1 is more efficient than protocol 0; protocol 2 is\n" |
| 5843 | "more efficient than protocol 1.\n" |
| 5844 | "\n" |
| 5845 | "Specifying a negative protocol version selects the highest\n" |
| 5846 | "protocol version supported. The higher the protocol used, the\n" |
| 5847 | "more recent the version of Python needed to read the pickle\n" |
| 5848 | "produced.\n" |
| 5849 | "\n" |
| 5850 | "The file parameter must have a write() method that accepts a single\n" |
| 5851 | "string argument. It can thus be an open file object, a StringIO\n" |
| 5852 | "object, or any other custom object that meets this interface.\n") |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5853 | }, |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5854 | |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 5855 | {"Unpickler", (PyCFunction)get_Unpickler, METH_O, |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5856 | PyDoc_STR("Unpickler(file) -- Create an unpickler.")}, |
| 5857 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5858 | { NULL, NULL } |
| 5859 | }; |
| 5860 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5861 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5862 | init_stuff(PyObject *module_dict) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5863 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5864 | PyObject *copyreg, *t, *r; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5865 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 5866 | #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] | 5867 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5868 | if (PyType_Ready(&Unpicklertype) < 0) |
| 5869 | return -1; |
| 5870 | if (PyType_Ready(&Picklertype) < 0) |
| 5871 | return -1; |
Tim Peters | 3cfe754 | 2003-05-21 21:29:48 +0000 | [diff] [blame] | 5872 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5873 | INIT_STR(__class__); |
| 5874 | INIT_STR(__getinitargs__); |
| 5875 | INIT_STR(__dict__); |
| 5876 | INIT_STR(__getstate__); |
| 5877 | INIT_STR(__setstate__); |
| 5878 | INIT_STR(__name__); |
| 5879 | INIT_STR(__main__); |
| 5880 | INIT_STR(__reduce__); |
| 5881 | INIT_STR(__reduce_ex__); |
| 5882 | INIT_STR(write); |
| 5883 | INIT_STR(append); |
| 5884 | INIT_STR(read); |
| 5885 | INIT_STR(readline); |
| 5886 | INIT_STR(dispatch_table); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5887 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5888 | if (!( copyreg = PyImport_ImportModule("copy_reg"))) |
| 5889 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5890 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5891 | /* This is special because we want to use a different |
| 5892 | one in restricted mode. */ |
| 5893 | dispatch_table = PyObject_GetAttr(copyreg, dispatch_table_str); |
| 5894 | if (!dispatch_table) return -1; |
Tim Peters | 5b7da39 | 2003-02-04 00:21:07 +0000 | [diff] [blame] | 5895 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5896 | extension_registry = PyObject_GetAttrString(copyreg, |
| 5897 | "_extension_registry"); |
| 5898 | if (!extension_registry) return -1; |
Tim Peters | 5b7da39 | 2003-02-04 00:21:07 +0000 | [diff] [blame] | 5899 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5900 | inverted_registry = PyObject_GetAttrString(copyreg, |
| 5901 | "_inverted_registry"); |
| 5902 | if (!inverted_registry) return -1; |
Tim Peters | 5b7da39 | 2003-02-04 00:21:07 +0000 | [diff] [blame] | 5903 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5904 | extension_cache = PyObject_GetAttrString(copyreg, |
| 5905 | "_extension_cache"); |
| 5906 | if (!extension_cache) return -1; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5907 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5908 | Py_DECREF(copyreg); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5909 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5910 | if (!(empty_tuple = PyTuple_New(0))) |
| 5911 | return -1; |
Tim Peters | 731098b | 2003-02-04 20:56:09 +0000 | [diff] [blame] | 5912 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5913 | two_tuple = PyTuple_New(2); |
| 5914 | if (two_tuple == NULL) |
| 5915 | return -1; |
| 5916 | /* We use this temp container with no regard to refcounts, or to |
| 5917 | * keeping containees alive. Exempt from GC, because we don't |
| 5918 | * want anything looking at two_tuple() by magic. |
| 5919 | */ |
| 5920 | PyObject_GC_UnTrack(two_tuple); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5921 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5922 | /* Ugh */ |
| 5923 | if (!( t=PyImport_ImportModule("__builtin__"))) return -1; |
| 5924 | if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0) |
| 5925 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5926 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5927 | if (!( t=PyDict_New())) return -1; |
| 5928 | if (!( r=PyRun_String( |
| 5929 | "def __str__(self):\n" |
| 5930 | " return self.args and ('%s' % self.args[0]) or '(what)'\n", |
| 5931 | Py_file_input, |
| 5932 | module_dict, t) )) return -1; |
| 5933 | Py_DECREF(r); |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5934 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5935 | PickleError = PyErr_NewException("cPickle.PickleError", NULL, t); |
| 5936 | if (!PickleError) |
| 5937 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5938 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5939 | Py_DECREF(t); |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5940 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5941 | PicklingError = PyErr_NewException("cPickle.PicklingError", |
| 5942 | PickleError, NULL); |
| 5943 | if (!PicklingError) |
| 5944 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5945 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5946 | if (!( t=PyDict_New())) return -1; |
| 5947 | if (!( r=PyRun_String( |
| 5948 | "def __str__(self):\n" |
| 5949 | " a=self.args\n" |
| 5950 | " a=a and type(a[0]) or '(what)'\n" |
| 5951 | " return 'Cannot pickle %s objects' % a\n" |
| 5952 | , Py_file_input, |
| 5953 | module_dict, t) )) return -1; |
| 5954 | Py_DECREF(r); |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5955 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5956 | if (!( UnpickleableError = PyErr_NewException( |
| 5957 | "cPickle.UnpickleableError", PicklingError, t))) |
| 5958 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5959 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5960 | Py_DECREF(t); |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5961 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5962 | if (!( UnpicklingError = PyErr_NewException("cPickle.UnpicklingError", |
| 5963 | PickleError, NULL))) |
| 5964 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5965 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5966 | if (!( BadPickleGet = PyErr_NewException("cPickle.BadPickleGet", |
| 5967 | UnpicklingError, NULL))) |
| 5968 | return -1; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5969 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5970 | if (PyDict_SetItemString(module_dict, "PickleError", |
| 5971 | PickleError) < 0) |
| 5972 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5973 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5974 | if (PyDict_SetItemString(module_dict, "PicklingError", |
| 5975 | PicklingError) < 0) |
| 5976 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5977 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5978 | if (PyDict_SetItemString(module_dict, "UnpicklingError", |
| 5979 | UnpicklingError) < 0) |
| 5980 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5981 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5982 | if (PyDict_SetItemString(module_dict, "UnpickleableError", |
| 5983 | UnpickleableError) < 0) |
| 5984 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5985 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5986 | if (PyDict_SetItemString(module_dict, "BadPickleGet", |
| 5987 | BadPickleGet) < 0) |
| 5988 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5989 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5990 | PycString_IMPORT; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 5991 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5992 | return 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5993 | } |
| 5994 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5995 | #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */ |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 5996 | #define PyMODINIT_FUNC void |
Guido van Rossum | f9ffb03 | 1999-02-04 14:54:04 +0000 | [diff] [blame] | 5997 | #endif |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 5998 | PyMODINIT_FUNC |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5999 | initcPickle(void) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 6000 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 6001 | PyObject *m, *d, *di, *v, *k; |
| 6002 | Py_ssize_t i; |
| 6003 | char *rev = "1.71"; /* XXX when does this change? */ |
| 6004 | PyObject *format_version; |
| 6005 | PyObject *compatible_formats; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 6006 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 6007 | Py_TYPE(&Picklertype) = &PyType_Type; |
| 6008 | Py_TYPE(&Unpicklertype) = &PyType_Type; |
| 6009 | Py_TYPE(&PdataType) = &PyType_Type; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 6010 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 6011 | /* Initialize some pieces. We need to do this before module creation, |
| 6012 | * so we're forced to use a temporary dictionary. :( |
| 6013 | */ |
| 6014 | di = PyDict_New(); |
| 6015 | if (!di) return; |
| 6016 | if (init_stuff(di) < 0) return; |
Guido van Rossum | ebba420 | 2000-09-07 14:35:37 +0000 | [diff] [blame] | 6017 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 6018 | /* Create the module and add the functions */ |
| 6019 | m = Py_InitModule4("cPickle", cPickle_methods, |
| 6020 | cPickle_module_documentation, |
| 6021 | (PyObject*)NULL,PYTHON_API_VERSION); |
| 6022 | if (m == NULL) |
| 6023 | return; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 6024 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 6025 | /* Add some symbolic constants to the module */ |
| 6026 | d = PyModule_GetDict(m); |
| 6027 | v = PyString_FromString(rev); |
| 6028 | PyDict_SetItemString(d, "__version__", v); |
| 6029 | Py_XDECREF(v); |
Barry Warsaw | 93d29b6 | 1997-01-14 17:45:08 +0000 | [diff] [blame] | 6030 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 6031 | /* Copy data from di. Waaa. */ |
| 6032 | for (i=0; PyDict_Next(di, &i, &k, &v); ) { |
| 6033 | if (PyObject_SetItem(d, k, v) < 0) { |
| 6034 | Py_DECREF(di); |
| 6035 | return; |
| 6036 | } |
| 6037 | } |
| 6038 | Py_DECREF(di); |
Guido van Rossum | ebba420 | 2000-09-07 14:35:37 +0000 | [diff] [blame] | 6039 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 6040 | i = PyModule_AddIntConstant(m, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL); |
| 6041 | if (i < 0) |
| 6042 | return; |
Tim Peters | 8587b3c | 2003-02-13 15:44:41 +0000 | [diff] [blame] | 6043 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 6044 | /* These are purely informational; no code uses them. */ |
| 6045 | /* File format version we write. */ |
| 6046 | format_version = PyString_FromString("2.0"); |
| 6047 | /* Format versions we can read. */ |
| 6048 | compatible_formats = Py_BuildValue("[sssss]", |
| 6049 | "1.0", /* Original protocol 0 */ |
| 6050 | "1.1", /* Protocol 0 + INST */ |
| 6051 | "1.2", /* Original protocol 1 */ |
| 6052 | "1.3", /* Protocol 1 + BINFLOAT */ |
| 6053 | "2.0"); /* Original protocol 2 */ |
| 6054 | PyDict_SetItemString(d, "format_version", format_version); |
| 6055 | PyDict_SetItemString(d, "compatible_formats", compatible_formats); |
| 6056 | Py_XDECREF(format_version); |
| 6057 | Py_XDECREF(compatible_formats); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 6058 | } |