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 | |
Serhiy Storchaka | bc62af1 | 2016-04-06 09:51:18 +0300 | [diff] [blame] | 692 | Py_XSETREF(self->last_string, str); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 693 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 694 | if (! (*s = PyString_AsString(str))) return -1; |
Amaury Forgeot d'Arc | 74b3016 | 2009-07-23 19:26:02 +0000 | [diff] [blame] | 695 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 696 | if (PyString_GET_SIZE(str) != n) { |
| 697 | PyErr_SetNone(PyExc_EOFError); |
| 698 | return -1; |
| 699 | } |
Amaury Forgeot d'Arc | 74b3016 | 2009-07-23 19:26:02 +0000 | [diff] [blame] | 700 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 701 | return n; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 705 | static Py_ssize_t |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 706 | readline_other(Unpicklerobject *self, char **s) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 707 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 708 | PyObject *str; |
| 709 | Py_ssize_t str_size; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 710 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 711 | if (!( str = PyObject_CallObject(self->readline, empty_tuple))) { |
| 712 | return -1; |
| 713 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 714 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 715 | if ((str_size = PyString_Size(str)) < 0) |
| 716 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 717 | |
Serhiy Storchaka | bc62af1 | 2016-04-06 09:51:18 +0300 | [diff] [blame] | 718 | Py_XSETREF(self->last_string, str); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 719 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 720 | if (! (*s = PyString_AsString(str))) |
| 721 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 722 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 723 | return str_size; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 724 | } |
| 725 | |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 726 | /* Copy the first n bytes from s into newly malloc'ed memory, plus a |
| 727 | * trailing 0 byte. Return a pointer to that, or NULL if out of memory. |
| 728 | * The caller is responsible for free()'ing the return value. |
| 729 | */ |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 730 | static char * |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 731 | pystrndup(const char *s, Py_ssize_t n) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 732 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 733 | char *r = (char *)malloc(n+1); |
| 734 | if (r == NULL) |
| 735 | return (char*)PyErr_NoMemory(); |
| 736 | memcpy(r, s, n); |
| 737 | r[n] = 0; |
| 738 | return r; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | |
| 742 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 743 | get(Picklerobject *self, PyObject *id) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 744 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 745 | PyObject *value, *mv; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 746 | Py_ssize_t c_value; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 747 | char s[30]; |
| 748 | size_t len; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 749 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 750 | if (!( mv = PyDict_GetItem(self->memo, id))) { |
| 751 | PyErr_SetObject(PyExc_KeyError, id); |
| 752 | return -1; |
| 753 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 754 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 755 | if (!( value = PyTuple_GetItem(mv, 0))) |
| 756 | return -1; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 757 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 758 | if (!( PyInt_Check(value))) { |
| 759 | PyErr_SetString(PicklingError, "no int where int expected in memo"); |
| 760 | return -1; |
| 761 | } |
| 762 | c_value = PyInt_AS_LONG((PyIntObject*)value); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 763 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 764 | if (!self->bin) { |
| 765 | s[0] = GET; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 766 | PyOS_snprintf(s + 1, sizeof(s) - 1, |
| 767 | "%" PY_FORMAT_SIZE_T "d\n", c_value); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 768 | len = strlen(s); |
| 769 | } |
| 770 | else if (Pdata_Check(self->file)) { |
| 771 | if (write_other(self, NULL, 0) < 0) return -1; |
| 772 | PDATA_APPEND(self->file, mv, -1); |
| 773 | return 0; |
| 774 | } |
| 775 | else { |
| 776 | if (c_value < 256) { |
| 777 | s[0] = BINGET; |
| 778 | s[1] = (int)(c_value & 0xff); |
| 779 | len = 2; |
| 780 | } |
| 781 | else { |
| 782 | s[0] = LONG_BINGET; |
| 783 | s[1] = (int)(c_value & 0xff); |
| 784 | s[2] = (int)((c_value >> 8) & 0xff); |
| 785 | s[3] = (int)((c_value >> 16) & 0xff); |
| 786 | s[4] = (int)((c_value >> 24) & 0xff); |
| 787 | len = 5; |
| 788 | } |
| 789 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 790 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 791 | if (self->write_func(self, s, len) < 0) |
| 792 | return -1; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 793 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 794 | return 0; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 795 | } |
Jeremy Hylton | ce616e4 | 1998-08-13 23:13:52 +0000 | [diff] [blame] | 796 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 797 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 798 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 799 | put(Picklerobject *self, PyObject *ob) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 800 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 801 | if (Py_REFCNT(ob) < 2 || self->fast) |
| 802 | return 0; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 803 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 804 | return put2(self, ob); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 805 | } |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 806 | |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 807 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 808 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 809 | put2(Picklerobject *self, PyObject *ob) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 810 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 811 | char c_str[30]; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 812 | Py_ssize_t len, p; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 813 | int res = -1; |
| 814 | PyObject *py_ob_id = 0, *memo_len = 0, *t = 0; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 815 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 816 | if (self->fast) |
| 817 | return 0; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 818 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 819 | if ((p = PyDict_Size(self->memo)) < 0) |
| 820 | goto finally; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 821 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 822 | /* Make sure memo keys are positive! */ |
| 823 | /* XXX Why? |
| 824 | * XXX And does "positive" really mean non-negative? |
| 825 | * XXX pickle.py starts with PUT index 0, not 1. This makes for |
| 826 | * XXX gratuitous differences between the pickling modules. |
| 827 | */ |
| 828 | p++; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 829 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 830 | if (!( py_ob_id = PyLong_FromVoidPtr(ob))) |
| 831 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 832 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 833 | if (!( memo_len = PyInt_FromLong(p))) |
| 834 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 835 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 836 | if (!( t = PyTuple_New(2))) |
| 837 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 838 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 839 | PyTuple_SET_ITEM(t, 0, memo_len); |
| 840 | Py_INCREF(memo_len); |
| 841 | PyTuple_SET_ITEM(t, 1, ob); |
| 842 | Py_INCREF(ob); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 843 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 844 | if (PyDict_SetItem(self->memo, py_ob_id, t) < 0) |
| 845 | goto finally; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 846 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 847 | if (!self->bin) { |
| 848 | c_str[0] = PUT; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 849 | PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, |
| 850 | "%" PY_FORMAT_SIZE_T "d\n", p); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 851 | len = strlen(c_str); |
| 852 | } |
| 853 | else if (Pdata_Check(self->file)) { |
| 854 | if (write_other(self, NULL, 0) < 0) return -1; |
| 855 | PDATA_APPEND(self->file, memo_len, -1); |
| 856 | res=0; /* Job well done ;) */ |
| 857 | goto finally; |
| 858 | } |
| 859 | else { |
| 860 | if (p >= 256) { |
| 861 | c_str[0] = LONG_BINPUT; |
| 862 | c_str[1] = (int)(p & 0xff); |
| 863 | c_str[2] = (int)((p >> 8) & 0xff); |
| 864 | c_str[3] = (int)((p >> 16) & 0xff); |
| 865 | c_str[4] = (int)((p >> 24) & 0xff); |
| 866 | len = 5; |
| 867 | } |
| 868 | else { |
| 869 | c_str[0] = BINPUT; |
| 870 | c_str[1] = p; |
| 871 | len = 2; |
| 872 | } |
| 873 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 874 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 875 | if (self->write_func(self, c_str, len) < 0) |
| 876 | goto finally; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 877 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 878 | res = 0; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 879 | |
| 880 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 881 | Py_XDECREF(py_ob_id); |
| 882 | Py_XDECREF(memo_len); |
| 883 | Py_XDECREF(t); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 884 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 885 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 886 | } |
| 887 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 888 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 889 | whichmodule(PyObject *global, PyObject *global_name) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 890 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 891 | Py_ssize_t i, j; |
| 892 | PyObject *module = 0, *modules_dict = 0, |
| 893 | *global_name_attr = 0, *name = 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 894 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 895 | module = PyObject_GetAttrString(global, "__module__"); |
| 896 | if (module) |
| 897 | return module; |
| 898 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 899 | PyErr_Clear(); |
| 900 | else |
| 901 | return NULL; |
Guido van Rossum | 4518823 | 1997-09-28 05:38:51 +0000 | [diff] [blame] | 902 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 903 | if (!( modules_dict = PySys_GetObject("modules"))) |
| 904 | return NULL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 905 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 906 | i = 0; |
| 907 | while ((j = PyDict_Next(modules_dict, &i, &name, &module))) { |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 908 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 909 | if (PyObject_Compare(name, __main___str)==0) continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 910 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 911 | global_name_attr = PyObject_GetAttr(module, global_name); |
| 912 | if (!global_name_attr) { |
| 913 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 914 | PyErr_Clear(); |
| 915 | else |
| 916 | return NULL; |
| 917 | continue; |
| 918 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 919 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 920 | if (global_name_attr != global) { |
| 921 | Py_DECREF(global_name_attr); |
| 922 | continue; |
| 923 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 924 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 925 | Py_DECREF(global_name_attr); |
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 | break; |
| 928 | } |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 929 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 930 | /* The following implements the rule in pickle.py added in 1.5 |
| 931 | that used __main__ if no module is found. I don't actually |
| 932 | like this rule. jlf |
| 933 | */ |
| 934 | if (!j) { |
| 935 | name=__main___str; |
| 936 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 937 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 938 | Py_INCREF(name); |
| 939 | return name; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 943 | static int |
Jeremy Hylton | 499ab6a | 2001-10-15 21:37:58 +0000 | [diff] [blame] | 944 | fast_save_enter(Picklerobject *self, PyObject *obj) |
| 945 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 946 | /* if fast_container < 0, we're doing an error exit. */ |
| 947 | if (++self->fast_container >= PY_CPICKLE_FAST_LIMIT) { |
| 948 | PyObject *key = NULL; |
| 949 | if (self->fast_memo == NULL) { |
| 950 | self->fast_memo = PyDict_New(); |
| 951 | if (self->fast_memo == NULL) { |
| 952 | self->fast_container = -1; |
| 953 | return 0; |
| 954 | } |
| 955 | } |
| 956 | key = PyLong_FromVoidPtr(obj); |
| 957 | if (key == NULL) |
| 958 | return 0; |
| 959 | if (PyDict_GetItem(self->fast_memo, key)) { |
| 960 | Py_DECREF(key); |
| 961 | PyErr_Format(PyExc_ValueError, |
| 962 | "fast mode: can't pickle cyclic objects " |
| 963 | "including object type %s at %p", |
| 964 | Py_TYPE(obj)->tp_name, obj); |
| 965 | self->fast_container = -1; |
| 966 | return 0; |
| 967 | } |
| 968 | if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) { |
| 969 | Py_DECREF(key); |
| 970 | self->fast_container = -1; |
| 971 | return 0; |
| 972 | } |
| 973 | Py_DECREF(key); |
| 974 | } |
| 975 | return 1; |
Jeremy Hylton | 499ab6a | 2001-10-15 21:37:58 +0000 | [diff] [blame] | 976 | } |
| 977 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 978 | int |
Jeremy Hylton | 499ab6a | 2001-10-15 21:37:58 +0000 | [diff] [blame] | 979 | fast_save_leave(Picklerobject *self, PyObject *obj) |
| 980 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 981 | if (self->fast_container-- >= PY_CPICKLE_FAST_LIMIT) { |
| 982 | PyObject *key = PyLong_FromVoidPtr(obj); |
| 983 | if (key == NULL) |
| 984 | return 0; |
| 985 | if (PyDict_DelItem(self->fast_memo, key) < 0) { |
| 986 | Py_DECREF(key); |
| 987 | return 0; |
| 988 | } |
| 989 | Py_DECREF(key); |
| 990 | } |
| 991 | return 1; |
Jeremy Hylton | 499ab6a | 2001-10-15 21:37:58 +0000 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 995 | save_none(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 996 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 997 | static char none = NONE; |
| 998 | if (self->write_func(self, &none, 1) < 0) |
| 999 | return -1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1000 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1001 | return 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 1004 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1005 | save_bool(Picklerobject *self, PyObject *args) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 1006 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1007 | static const char *buf[2] = {FALSE, TRUE}; |
| 1008 | static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1}; |
| 1009 | long l = PyInt_AS_LONG((PyIntObject *)args); |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 1010 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1011 | if (self->proto >= 2) { |
| 1012 | char opcode = l ? NEWTRUE : NEWFALSE; |
| 1013 | if (self->write_func(self, &opcode, 1) < 0) |
| 1014 | return -1; |
| 1015 | } |
| 1016 | else if (self->write_func(self, buf[l], len[l]) < 0) |
| 1017 | return -1; |
| 1018 | return 0; |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 1019 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 1020 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1021 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1022 | save_int(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1023 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1024 | char c_str[32]; |
| 1025 | long l = PyInt_AS_LONG((PyIntObject *)args); |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 1026 | Py_ssize_t len = 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1027 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1028 | if (!self->bin |
Guido van Rossum | de8d6d7 | 1997-05-13 18:00:44 +0000 | [diff] [blame] | 1029 | #if SIZEOF_LONG > 4 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1030 | || l > 0x7fffffffL |
| 1031 | || l < -0x80000000L |
Guido van Rossum | de8d6d7 | 1997-05-13 18:00:44 +0000 | [diff] [blame] | 1032 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1033 | ) { |
| 1034 | /* Text-mode pickle, or long too big to fit in the 4-byte |
| 1035 | * signed BININT format: store as a string. |
| 1036 | */ |
| 1037 | c_str[0] = INT; |
| 1038 | PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%ld\n", l); |
| 1039 | if (self->write_func(self, c_str, strlen(c_str)) < 0) |
| 1040 | return -1; |
| 1041 | } |
| 1042 | else { |
| 1043 | /* Binary pickle and l fits in a signed 4-byte int. */ |
| 1044 | c_str[1] = (int)( l & 0xff); |
| 1045 | c_str[2] = (int)((l >> 8) & 0xff); |
| 1046 | c_str[3] = (int)((l >> 16) & 0xff); |
| 1047 | c_str[4] = (int)((l >> 24) & 0xff); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1048 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1049 | if ((c_str[4] == 0) && (c_str[3] == 0)) { |
| 1050 | if (c_str[2] == 0) { |
| 1051 | c_str[0] = BININT1; |
| 1052 | len = 2; |
| 1053 | } |
| 1054 | else { |
| 1055 | c_str[0] = BININT2; |
| 1056 | len = 3; |
| 1057 | } |
| 1058 | } |
| 1059 | else { |
| 1060 | c_str[0] = BININT; |
| 1061 | len = 5; |
| 1062 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1063 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1064 | if (self->write_func(self, c_str, len) < 0) |
| 1065 | return -1; |
| 1066 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1067 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1068 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1069 | } |
| 1070 | |
| 1071 | |
| 1072 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1073 | save_long(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1074 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1075 | Py_ssize_t size; |
| 1076 | int res = -1; |
| 1077 | PyObject *repr = NULL; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1078 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1079 | static char l = LONG; |
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 | if (self->proto >= 2) { |
| 1082 | /* Linear-time pickling. */ |
| 1083 | size_t nbits; |
| 1084 | size_t nbytes; |
| 1085 | unsigned char *pdata; |
| 1086 | char c_str[5]; |
| 1087 | int i; |
| 1088 | int sign = _PyLong_Sign(args); |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 1089 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1090 | if (sign == 0) { |
| 1091 | /* It's 0 -- an empty bytestring. */ |
| 1092 | c_str[0] = LONG1; |
| 1093 | c_str[1] = 0; |
| 1094 | i = self->write_func(self, c_str, 2); |
| 1095 | if (i < 0) goto finally; |
| 1096 | res = 0; |
| 1097 | goto finally; |
| 1098 | } |
| 1099 | nbits = _PyLong_NumBits(args); |
| 1100 | if (nbits == (size_t)-1 && PyErr_Occurred()) |
| 1101 | goto finally; |
| 1102 | /* How many bytes do we need? There are nbits >> 3 full |
| 1103 | * bytes of data, and nbits & 7 leftover bits. If there |
| 1104 | * are any leftover bits, then we clearly need another |
| 1105 | * byte. Wnat's not so obvious is that we *probably* |
| 1106 | * need another byte even if there aren't any leftovers: |
| 1107 | * the most-significant bit of the most-significant byte |
| 1108 | * acts like a sign bit, and it's usually got a sense |
| 1109 | * opposite of the one we need. The exception is longs |
| 1110 | * of the form -(2**(8*j-1)) for j > 0. Such a long is |
| 1111 | * its own 256's-complement, so has the right sign bit |
| 1112 | * even without the extra byte. That's a pain to check |
| 1113 | * for in advance, though, so we always grab an extra |
| 1114 | * byte at the start, and cut it back later if possible. |
| 1115 | */ |
| 1116 | nbytes = (nbits >> 3) + 1; |
| 1117 | if (nbytes > INT_MAX) { |
| 1118 | PyErr_SetString(PyExc_OverflowError, "long too large " |
| 1119 | "to pickle"); |
| 1120 | goto finally; |
| 1121 | } |
| 1122 | repr = PyString_FromStringAndSize(NULL, (int)nbytes); |
| 1123 | if (repr == NULL) goto finally; |
| 1124 | pdata = (unsigned char *)PyString_AS_STRING(repr); |
| 1125 | i = _PyLong_AsByteArray((PyLongObject *)args, |
| 1126 | pdata, nbytes, |
| 1127 | 1 /* little endian */, 1 /* signed */); |
| 1128 | if (i < 0) goto finally; |
| 1129 | /* If the long is negative, this may be a byte more than |
| 1130 | * needed. This is so iff the MSB is all redundant sign |
| 1131 | * bits. |
| 1132 | */ |
| 1133 | if (sign < 0 && nbytes > 1 && pdata[nbytes - 1] == 0xff && |
| 1134 | (pdata[nbytes - 2] & 0x80) != 0) |
| 1135 | --nbytes; |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 1136 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1137 | if (nbytes < 256) { |
| 1138 | c_str[0] = LONG1; |
| 1139 | c_str[1] = (char)nbytes; |
| 1140 | size = 2; |
| 1141 | } |
| 1142 | else { |
| 1143 | c_str[0] = LONG4; |
| 1144 | size = (int)nbytes; |
| 1145 | for (i = 1; i < 5; i++) { |
| 1146 | c_str[i] = (char)(size & 0xff); |
| 1147 | size >>= 8; |
| 1148 | } |
| 1149 | size = 5; |
| 1150 | } |
| 1151 | i = self->write_func(self, c_str, size); |
| 1152 | if (i < 0) goto finally; |
| 1153 | i = self->write_func(self, (char *)pdata, (int)nbytes); |
| 1154 | if (i < 0) goto finally; |
| 1155 | res = 0; |
| 1156 | goto finally; |
| 1157 | } |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 1158 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1159 | /* proto < 2: write the repr and newline. This is quadratic-time |
| 1160 | * (in the number of digits), in both directions. |
| 1161 | */ |
| 1162 | if (!( repr = PyObject_Repr(args))) |
| 1163 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1164 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1165 | if ((size = PyString_Size(repr)) < 0) |
| 1166 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1167 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1168 | if (self->write_func(self, &l, 1) < 0) |
| 1169 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1170 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1171 | if (self->write_func(self, |
| 1172 | PyString_AS_STRING((PyStringObject *)repr), |
| 1173 | size) < 0) |
| 1174 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1175 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1176 | if (self->write_func(self, "\n", 1) < 0) |
| 1177 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1178 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1179 | res = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1180 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1181 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1182 | Py_XDECREF(repr); |
| 1183 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | |
| 1187 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1188 | save_float(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1189 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1190 | double x = PyFloat_AS_DOUBLE((PyFloatObject *)args); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1191 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1192 | if (self->bin) { |
| 1193 | char str[9]; |
| 1194 | str[0] = BINFLOAT; |
| 1195 | if (_PyFloat_Pack8(x, (unsigned char *)&str[1], 0) < 0) |
| 1196 | return -1; |
| 1197 | if (self->write_func(self, str, 9) < 0) |
| 1198 | return -1; |
| 1199 | } |
| 1200 | else { |
| 1201 | int result = -1; |
| 1202 | char *buf = NULL; |
| 1203 | char op = FLOAT; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1204 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1205 | if (self->write_func(self, &op, 1) < 0) |
| 1206 | goto done; |
Eric Smith | b05d3be | 2009-10-26 15:06:39 +0000 | [diff] [blame] | 1207 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1208 | buf = PyOS_double_to_string(x, 'g', 17, 0, NULL); |
| 1209 | if (!buf) { |
| 1210 | PyErr_NoMemory(); |
| 1211 | goto done; |
| 1212 | } |
Eric Smith | b05d3be | 2009-10-26 15:06:39 +0000 | [diff] [blame] | 1213 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1214 | if (self->write_func(self, buf, strlen(buf)) < 0) |
| 1215 | goto done; |
Eric Smith | b05d3be | 2009-10-26 15:06:39 +0000 | [diff] [blame] | 1216 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1217 | if (self->write_func(self, "\n", 1) < 0) |
| 1218 | goto done; |
Eric Smith | b05d3be | 2009-10-26 15:06:39 +0000 | [diff] [blame] | 1219 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1220 | result = 0; |
Eric Smith | b05d3be | 2009-10-26 15:06:39 +0000 | [diff] [blame] | 1221 | done: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1222 | PyMem_Free(buf); |
| 1223 | return result; |
| 1224 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1225 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1226 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | |
| 1230 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1231 | save_string(Picklerobject *self, PyObject *args, int doput) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1232 | { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 1233 | Py_ssize_t size, len; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1234 | PyObject *repr=0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1235 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1236 | if ((size = PyString_Size(args)) < 0) |
| 1237 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1238 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1239 | if (!self->bin) { |
| 1240 | char *repr_str; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1241 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1242 | static char string = STRING; |
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 | if (!( repr = PyObject_Repr(args))) |
| 1245 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1246 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1247 | if ((len = PyString_Size(repr)) < 0) |
| 1248 | goto err; |
| 1249 | repr_str = PyString_AS_STRING((PyStringObject *)repr); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1250 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1251 | if (self->write_func(self, &string, 1) < 0) |
| 1252 | goto err; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1253 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1254 | if (self->write_func(self, repr_str, len) < 0) |
| 1255 | goto err; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1256 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1257 | if (self->write_func(self, "\n", 1) < 0) |
| 1258 | goto err; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1259 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1260 | Py_XDECREF(repr); |
| 1261 | } |
| 1262 | else { |
| 1263 | int i; |
| 1264 | char c_str[5]; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1265 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1266 | if (size < 256) { |
| 1267 | c_str[0] = SHORT_BINSTRING; |
| 1268 | c_str[1] = size; |
| 1269 | len = 2; |
| 1270 | } |
| 1271 | else if (size <= INT_MAX) { |
| 1272 | c_str[0] = BINSTRING; |
| 1273 | for (i = 1; i < 5; i++) |
| 1274 | c_str[i] = (int)(size >> ((i - 1) * 8)); |
| 1275 | len = 5; |
| 1276 | } |
| 1277 | else |
| 1278 | return -1; /* string too large */ |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1279 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1280 | if (self->write_func(self, c_str, len) < 0) |
| 1281 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1282 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1283 | if (size > 128 && Pdata_Check(self->file)) { |
| 1284 | if (write_other(self, NULL, 0) < 0) return -1; |
| 1285 | PDATA_APPEND(self->file, args, -1); |
| 1286 | } |
| 1287 | else { |
| 1288 | if (self->write_func(self, |
| 1289 | PyString_AS_STRING( |
| 1290 | (PyStringObject *)args), |
| 1291 | size) < 0) |
| 1292 | return -1; |
| 1293 | } |
| 1294 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1295 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1296 | if (doput) |
| 1297 | if (put(self, args) < 0) |
| 1298 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1299 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1300 | return 0; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 1301 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1302 | err: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1303 | Py_XDECREF(repr); |
| 1304 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1305 | } |
| 1306 | |
| 1307 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1308 | #ifdef Py_USING_UNICODE |
Guido van Rossum | fb10c3f | 2000-12-19 02:08:38 +0000 | [diff] [blame] | 1309 | /* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates |
| 1310 | backslash and newline characters to \uXXXX escapes. */ |
| 1311 | static PyObject * |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1312 | modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, Py_ssize_t size) |
Guido van Rossum | fb10c3f | 2000-12-19 02:08:38 +0000 | [diff] [blame] | 1313 | { |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1314 | PyObject *repr; |
| 1315 | char *p; |
| 1316 | char *q; |
Guido van Rossum | fb10c3f | 2000-12-19 02:08:38 +0000 | [diff] [blame] | 1317 | |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1318 | static const char *hexdigit = "0123456789abcdef"; |
| 1319 | #ifdef Py_UNICODE_WIDE |
| 1320 | const Py_ssize_t expandsize = 10; |
| 1321 | #else |
| 1322 | const Py_ssize_t expandsize = 6; |
| 1323 | #endif |
Guido van Rossum | fb10c3f | 2000-12-19 02:08:38 +0000 | [diff] [blame] | 1324 | |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1325 | if (size > PY_SSIZE_T_MAX / expandsize) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1326 | return PyErr_NoMemory(); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1327 | |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1328 | repr = PyString_FromStringAndSize(NULL, expandsize * size); |
| 1329 | if (repr == NULL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1330 | return NULL; |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1331 | if (size == 0) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1332 | return repr; |
Guido van Rossum | fb10c3f | 2000-12-19 02:08:38 +0000 | [diff] [blame] | 1333 | |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1334 | p = q = PyString_AS_STRING(repr); |
| 1335 | while (size-- > 0) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1336 | Py_UNICODE ch = *s++; |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1337 | #ifdef Py_UNICODE_WIDE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1338 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 1339 | if (ch >= 0x10000) { |
| 1340 | *p++ = '\\'; |
| 1341 | *p++ = 'U'; |
| 1342 | *p++ = hexdigit[(ch >> 28) & 0xf]; |
| 1343 | *p++ = hexdigit[(ch >> 24) & 0xf]; |
| 1344 | *p++ = hexdigit[(ch >> 20) & 0xf]; |
| 1345 | *p++ = hexdigit[(ch >> 16) & 0xf]; |
| 1346 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 1347 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 1348 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 1349 | *p++ = hexdigit[ch & 15]; |
| 1350 | } |
| 1351 | else |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1352 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1353 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 1354 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 1355 | Py_UNICODE ch2; |
| 1356 | Py_UCS4 ucs; |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1357 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1358 | ch2 = *s++; |
| 1359 | size--; |
| 1360 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 1361 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 1362 | *p++ = '\\'; |
| 1363 | *p++ = 'U'; |
| 1364 | *p++ = hexdigit[(ucs >> 28) & 0xf]; |
| 1365 | *p++ = hexdigit[(ucs >> 24) & 0xf]; |
| 1366 | *p++ = hexdigit[(ucs >> 20) & 0xf]; |
| 1367 | *p++ = hexdigit[(ucs >> 16) & 0xf]; |
| 1368 | *p++ = hexdigit[(ucs >> 12) & 0xf]; |
| 1369 | *p++ = hexdigit[(ucs >> 8) & 0xf]; |
| 1370 | *p++ = hexdigit[(ucs >> 4) & 0xf]; |
| 1371 | *p++ = hexdigit[ucs & 0xf]; |
| 1372 | continue; |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1373 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1374 | /* Fall through: isolated surrogates are copied as-is */ |
| 1375 | s--; |
| 1376 | size++; |
| 1377 | } |
| 1378 | #endif |
| 1379 | /* Map 16-bit characters to '\uxxxx' */ |
| 1380 | if (ch >= 256 || ch == '\\' || ch == '\n') { |
| 1381 | *p++ = '\\'; |
| 1382 | *p++ = 'u'; |
| 1383 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 1384 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 1385 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 1386 | *p++ = hexdigit[ch & 15]; |
| 1387 | } |
| 1388 | /* Copy everything else as-is */ |
| 1389 | else |
| 1390 | *p++ = (char) ch; |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1391 | } |
| 1392 | *p = '\0'; |
| 1393 | _PyString_Resize(&repr, p - q); |
| 1394 | return repr; |
| 1395 | } |
Guido van Rossum | fb10c3f | 2000-12-19 02:08:38 +0000 | [diff] [blame] | 1396 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1397 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1398 | save_unicode(Picklerobject *self, PyObject *args, int doput) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1399 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1400 | Py_ssize_t size, len; |
| 1401 | PyObject *repr=0; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1402 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1403 | if (!PyUnicode_Check(args)) |
| 1404 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1405 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1406 | if (!self->bin) { |
| 1407 | char *repr_str; |
| 1408 | static char string = UNICODE; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1409 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1410 | repr = modified_EncodeRawUnicodeEscape( |
| 1411 | PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args)); |
| 1412 | if (!repr) |
| 1413 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1414 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1415 | if ((len = PyString_Size(repr)) < 0) |
| 1416 | goto err; |
| 1417 | repr_str = PyString_AS_STRING((PyStringObject *)repr); |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1418 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1419 | if (self->write_func(self, &string, 1) < 0) |
| 1420 | goto err; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1421 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1422 | if (self->write_func(self, repr_str, len) < 0) |
| 1423 | goto err; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1424 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1425 | if (self->write_func(self, "\n", 1) < 0) |
| 1426 | goto err; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1427 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1428 | Py_XDECREF(repr); |
| 1429 | } |
| 1430 | else { |
| 1431 | int i; |
| 1432 | char c_str[5]; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1433 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1434 | if (!( repr = PyUnicode_AsUTF8String(args))) |
| 1435 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1436 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1437 | if ((size = PyString_Size(repr)) < 0) |
| 1438 | goto err; |
| 1439 | if (size > INT_MAX) |
| 1440 | return -1; /* string too large */ |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1441 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1442 | c_str[0] = BINUNICODE; |
| 1443 | for (i = 1; i < 5; i++) |
| 1444 | c_str[i] = (int)(size >> ((i - 1) * 8)); |
| 1445 | len = 5; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1446 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1447 | if (self->write_func(self, c_str, len) < 0) |
| 1448 | goto err; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1449 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1450 | if (size > 128 && Pdata_Check(self->file)) { |
| 1451 | if (write_other(self, NULL, 0) < 0) |
| 1452 | goto err; |
| 1453 | PDATA_APPEND(self->file, repr, -1); |
| 1454 | } |
| 1455 | else { |
| 1456 | if (self->write_func(self, PyString_AS_STRING(repr), |
| 1457 | size) < 0) |
| 1458 | goto err; |
| 1459 | } |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1460 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1461 | Py_DECREF(repr); |
| 1462 | } |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1463 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1464 | if (doput) |
| 1465 | if (put(self, args) < 0) |
| 1466 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1467 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1468 | return 0; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1469 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1470 | err: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1471 | Py_XDECREF(repr); |
| 1472 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1473 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1474 | #endif |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1475 | |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1476 | /* A helper for save_tuple. Push the len elements in tuple t on the stack. */ |
| 1477 | static int |
Tim Peters | 6792014 | 2003-02-05 03:46:17 +0000 | [diff] [blame] | 1478 | store_tuple_elements(Picklerobject *self, PyObject *t, int len) |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1479 | { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 1480 | Py_ssize_t i; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1481 | int res = -1; /* guilty until proved innocent */ |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1482 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1483 | assert(PyTuple_Size(t) == len); |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1484 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1485 | for (i = 0; i < len; i++) { |
| 1486 | PyObject *element = PyTuple_GET_ITEM(t, i); |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1487 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1488 | if (element == NULL) |
| 1489 | goto finally; |
| 1490 | if (save(self, element, 0) < 0) |
| 1491 | goto finally; |
| 1492 | } |
| 1493 | res = 0; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1494 | |
| 1495 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1496 | return res; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1497 | } |
| 1498 | |
| 1499 | /* Tuples are ubiquitous in the pickle protocols, so many techniques are |
| 1500 | * used across protocols to minimize the space needed to pickle them. |
Tim Peters | 6792014 | 2003-02-05 03:46:17 +0000 | [diff] [blame] | 1501 | * Tuples are also the only builtin immutable type that can be recursive |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1502 | * (a tuple can be reached from itself), and that requires some subtle |
| 1503 | * magic so that it works in all cases. IOW, this is a long routine. |
| 1504 | */ |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1505 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1506 | save_tuple(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1507 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1508 | PyObject *py_tuple_id = NULL; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 1509 | Py_ssize_t len, i; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1510 | int res = -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1511 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1512 | static char tuple = TUPLE; |
| 1513 | static char pop = POP; |
| 1514 | static char pop_mark = POP_MARK; |
| 1515 | static char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3}; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1516 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1517 | if ((len = PyTuple_Size(args)) < 0) |
| 1518 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1519 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1520 | if (len == 0) { |
| 1521 | char c_str[2]; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 1522 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1523 | if (self->proto) { |
| 1524 | c_str[0] = EMPTY_TUPLE; |
| 1525 | len = 1; |
| 1526 | } |
| 1527 | else { |
| 1528 | c_str[0] = MARK; |
| 1529 | c_str[1] = TUPLE; |
| 1530 | len = 2; |
| 1531 | } |
| 1532 | if (self->write_func(self, c_str, len) >= 0) |
| 1533 | res = 0; |
| 1534 | /* Don't memoize an empty tuple. */ |
| 1535 | goto finally; |
| 1536 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1537 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1538 | /* A non-empty tuple. */ |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1539 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1540 | /* id(tuple) isn't in the memo now. If it shows up there after |
| 1541 | * saving the tuple elements, the tuple must be recursive, in |
| 1542 | * which case we'll pop everything we put on the stack, and fetch |
| 1543 | * its value from the memo. |
| 1544 | */ |
| 1545 | py_tuple_id = PyLong_FromVoidPtr(args); |
| 1546 | if (py_tuple_id == NULL) |
| 1547 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1548 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1549 | if (len <= 3 && self->proto >= 2) { |
| 1550 | /* Use TUPLE{1,2,3} opcodes. */ |
| 1551 | if (store_tuple_elements(self, args, len) < 0) |
| 1552 | goto finally; |
| 1553 | if (PyDict_GetItem(self->memo, py_tuple_id)) { |
| 1554 | /* pop the len elements */ |
| 1555 | for (i = 0; i < len; ++i) |
| 1556 | if (self->write_func(self, &pop, 1) < 0) |
| 1557 | goto finally; |
| 1558 | /* fetch from memo */ |
| 1559 | if (get(self, py_tuple_id) < 0) |
| 1560 | goto finally; |
| 1561 | res = 0; |
| 1562 | goto finally; |
| 1563 | } |
| 1564 | /* Not recursive. */ |
| 1565 | if (self->write_func(self, len2opcode + len, 1) < 0) |
| 1566 | goto finally; |
| 1567 | goto memoize; |
| 1568 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1569 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1570 | /* proto < 2 and len > 0, or proto >= 2 and len > 3. |
| 1571 | * Generate MARK elt1 elt2 ... TUPLE |
| 1572 | */ |
| 1573 | if (self->write_func(self, &MARKv, 1) < 0) |
| 1574 | goto finally; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1575 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1576 | if (store_tuple_elements(self, args, len) < 0) |
| 1577 | goto finally; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1578 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1579 | if (PyDict_GetItem(self->memo, py_tuple_id)) { |
| 1580 | /* pop the stack stuff we pushed */ |
| 1581 | if (self->bin) { |
| 1582 | if (self->write_func(self, &pop_mark, 1) < 0) |
| 1583 | goto finally; |
| 1584 | } |
| 1585 | else { |
| 1586 | /* Note that we pop one more than len, to remove |
| 1587 | * the MARK too. |
| 1588 | */ |
| 1589 | for (i = 0; i <= len; i++) |
| 1590 | if (self->write_func(self, &pop, 1) < 0) |
| 1591 | goto finally; |
| 1592 | } |
| 1593 | /* fetch from memo */ |
| 1594 | if (get(self, py_tuple_id) >= 0) |
| 1595 | res = 0; |
| 1596 | goto finally; |
| 1597 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1598 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1599 | /* Not recursive. */ |
| 1600 | if (self->write_func(self, &tuple, 1) < 0) |
| 1601 | goto finally; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 1602 | |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1603 | memoize: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1604 | if (put(self, args) >= 0) |
| 1605 | res = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1606 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1607 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1608 | Py_XDECREF(py_tuple_id); |
| 1609 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1610 | } |
| 1611 | |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1612 | /* iter is an iterator giving items, and we batch up chunks of |
| 1613 | * MARK item item ... item APPENDS |
| 1614 | * opcode sequences. Calling code should have arranged to first create an |
| 1615 | * empty list, or list-like object, for the APPENDS to operate on. |
| 1616 | * Returns 0 on success, <0 on error. |
| 1617 | */ |
| 1618 | static int |
| 1619 | batch_list(Picklerobject *self, PyObject *iter) |
| 1620 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1621 | PyObject *obj = NULL; |
| 1622 | PyObject *firstitem = NULL; |
| 1623 | int i, n; |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1624 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1625 | static char append = APPEND; |
| 1626 | static char appends = APPENDS; |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1627 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1628 | assert(iter != NULL); |
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 | if (self->proto == 0) { |
| 1631 | /* APPENDS isn't available; do one at a time. */ |
| 1632 | for (;;) { |
| 1633 | obj = PyIter_Next(iter); |
| 1634 | if (obj == NULL) { |
| 1635 | if (PyErr_Occurred()) |
| 1636 | return -1; |
| 1637 | break; |
| 1638 | } |
| 1639 | i = save(self, obj, 0); |
| 1640 | Py_DECREF(obj); |
| 1641 | if (i < 0) |
| 1642 | return -1; |
| 1643 | if (self->write_func(self, &append, 1) < 0) |
| 1644 | return -1; |
| 1645 | } |
| 1646 | return 0; |
| 1647 | } |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1648 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1649 | /* proto > 0: write in batches of BATCHSIZE. */ |
| 1650 | do { |
| 1651 | /* Get first item */ |
| 1652 | firstitem = PyIter_Next(iter); |
| 1653 | if (firstitem == NULL) { |
| 1654 | if (PyErr_Occurred()) |
| 1655 | goto BatchFailed; |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1656 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1657 | /* nothing more to add */ |
| 1658 | break; |
| 1659 | } |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1660 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1661 | /* Try to get a second item */ |
| 1662 | obj = PyIter_Next(iter); |
| 1663 | if (obj == NULL) { |
| 1664 | if (PyErr_Occurred()) |
| 1665 | goto BatchFailed; |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1666 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1667 | /* Only one item to write */ |
| 1668 | if (save(self, firstitem, 0) < 0) |
| 1669 | goto BatchFailed; |
| 1670 | if (self->write_func(self, &append, 1) < 0) |
| 1671 | goto BatchFailed; |
| 1672 | Py_CLEAR(firstitem); |
| 1673 | break; |
| 1674 | } |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1675 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1676 | /* More than one item to write */ |
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 | /* Pump out MARK, items, APPENDS. */ |
| 1679 | if (self->write_func(self, &MARKv, 1) < 0) |
| 1680 | goto BatchFailed; |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1681 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1682 | if (save(self, firstitem, 0) < 0) |
| 1683 | goto BatchFailed; |
| 1684 | Py_CLEAR(firstitem); |
| 1685 | n = 1; |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1686 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1687 | /* Fetch and save up to BATCHSIZE items */ |
| 1688 | while (obj) { |
| 1689 | if (save(self, obj, 0) < 0) |
| 1690 | goto BatchFailed; |
| 1691 | Py_CLEAR(obj); |
| 1692 | n += 1; |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1693 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1694 | if (n == BATCHSIZE) |
| 1695 | break; |
| 1696 | |
| 1697 | obj = PyIter_Next(iter); |
| 1698 | if (obj == NULL) { |
| 1699 | if (PyErr_Occurred()) |
| 1700 | goto BatchFailed; |
| 1701 | break; |
| 1702 | } |
| 1703 | } |
| 1704 | |
| 1705 | if (self->write_func(self, &appends, 1) < 0) |
| 1706 | goto BatchFailed; |
| 1707 | |
| 1708 | } while (n == BATCHSIZE); |
| 1709 | return 0; |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1710 | |
| 1711 | BatchFailed: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1712 | Py_XDECREF(firstitem); |
| 1713 | Py_XDECREF(obj); |
| 1714 | return -1; |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1715 | } |
| 1716 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1717 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1718 | save_list(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1719 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1720 | int res = -1; |
| 1721 | char s[3]; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 1722 | Py_ssize_t len; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1723 | PyObject *iter; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1724 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1725 | if (self->fast && !fast_save_enter(self, args)) |
| 1726 | goto finally; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 1727 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1728 | /* Create an empty list. */ |
| 1729 | if (self->bin) { |
| 1730 | s[0] = EMPTY_LIST; |
| 1731 | len = 1; |
| 1732 | } |
| 1733 | else { |
| 1734 | s[0] = MARK; |
| 1735 | s[1] = LIST; |
| 1736 | len = 2; |
| 1737 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1738 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1739 | if (self->write_func(self, s, len) < 0) |
| 1740 | goto finally; |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1741 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1742 | /* Get list length, and bow out early if empty. */ |
| 1743 | if ((len = PyList_Size(args)) < 0) |
| 1744 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1745 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1746 | /* Memoize. */ |
| 1747 | if (len == 0) { |
| 1748 | if (put(self, args) >= 0) |
| 1749 | res = 0; |
| 1750 | goto finally; |
| 1751 | } |
| 1752 | if (put2(self, args) < 0) |
| 1753 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1754 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1755 | /* Materialize the list elements. */ |
| 1756 | iter = PyObject_GetIter(args); |
| 1757 | if (iter == NULL) |
| 1758 | goto finally; |
Facundo Batista | 763d309 | 2008-06-30 01:10:55 +0000 | [diff] [blame] | 1759 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1760 | if (Py_EnterRecursiveCall(" while pickling an object") == 0) |
| 1761 | { |
| 1762 | res = batch_list(self, iter); |
| 1763 | Py_LeaveRecursiveCall(); |
| 1764 | } |
| 1765 | Py_DECREF(iter); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1766 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1767 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1768 | if (self->fast && !fast_save_leave(self, args)) |
| 1769 | res = -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1770 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1771 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1772 | } |
| 1773 | |
| 1774 | |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1775 | /* iter is an iterator giving (key, value) pairs, and we batch up chunks of |
| 1776 | * MARK key value ... key value SETITEMS |
| 1777 | * opcode sequences. Calling code should have arranged to first create an |
| 1778 | * empty dict, or dict-like object, for the SETITEMS to operate on. |
| 1779 | * Returns 0 on success, <0 on error. |
| 1780 | * |
| 1781 | * This is very much like batch_list(). The difference between saving |
| 1782 | * elements directly, and picking apart two-tuples, is so long-winded at |
| 1783 | * the C level, though, that attempts to combine these routines were too |
| 1784 | * ugly to bear. |
| 1785 | */ |
| 1786 | static int |
| 1787 | batch_dict(Picklerobject *self, PyObject *iter) |
| 1788 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1789 | PyObject *p = NULL; |
| 1790 | PyObject *firstitem = NULL; |
| 1791 | int i, n; |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1792 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1793 | static char setitem = SETITEM; |
| 1794 | static char setitems = SETITEMS; |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1795 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1796 | assert(iter != NULL); |
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 | if (self->proto == 0) { |
| 1799 | /* SETITEMS isn't available; do one at a time. */ |
| 1800 | for (;;) { |
| 1801 | p = PyIter_Next(iter); |
| 1802 | if (p == NULL) { |
| 1803 | if (PyErr_Occurred()) |
| 1804 | return -1; |
| 1805 | break; |
| 1806 | } |
| 1807 | if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) { |
| 1808 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 1809 | "iterator must return 2-tuples"); |
| 1810 | return -1; |
| 1811 | } |
| 1812 | i = save(self, PyTuple_GET_ITEM(p, 0), 0); |
| 1813 | if (i >= 0) |
| 1814 | i = save(self, PyTuple_GET_ITEM(p, 1), 0); |
| 1815 | Py_DECREF(p); |
| 1816 | if (i < 0) |
| 1817 | return -1; |
| 1818 | if (self->write_func(self, &setitem, 1) < 0) |
| 1819 | return -1; |
| 1820 | } |
| 1821 | return 0; |
| 1822 | } |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1823 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1824 | /* proto > 0: write in batches of BATCHSIZE. */ |
| 1825 | do { |
| 1826 | /* Get first item */ |
| 1827 | firstitem = PyIter_Next(iter); |
| 1828 | if (firstitem == NULL) { |
| 1829 | if (PyErr_Occurred()) |
| 1830 | goto BatchFailed; |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1831 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1832 | /* nothing more to add */ |
| 1833 | break; |
| 1834 | } |
| 1835 | if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) { |
| 1836 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 1837 | "iterator must return 2-tuples"); |
| 1838 | goto BatchFailed; |
| 1839 | } |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1840 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1841 | /* Try to get a second item */ |
| 1842 | p = PyIter_Next(iter); |
| 1843 | if (p == NULL) { |
| 1844 | if (PyErr_Occurred()) |
| 1845 | goto BatchFailed; |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1846 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1847 | /* Only one item to write */ |
| 1848 | if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0) |
| 1849 | goto BatchFailed; |
| 1850 | if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0) |
| 1851 | goto BatchFailed; |
| 1852 | if (self->write_func(self, &setitem, 1) < 0) |
| 1853 | goto BatchFailed; |
| 1854 | Py_CLEAR(firstitem); |
| 1855 | break; |
| 1856 | } |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1857 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1858 | /* More than one item to write */ |
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 | /* Pump out MARK, items, SETITEMS. */ |
| 1861 | if (self->write_func(self, &MARKv, 1) < 0) |
| 1862 | goto BatchFailed; |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1863 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1864 | if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0) |
| 1865 | goto BatchFailed; |
| 1866 | if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0) |
| 1867 | goto BatchFailed; |
| 1868 | Py_CLEAR(firstitem); |
| 1869 | n = 1; |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1870 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1871 | /* Fetch and save up to BATCHSIZE items */ |
| 1872 | while (p) { |
| 1873 | if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) { |
| 1874 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 1875 | "iterator must return 2-tuples"); |
| 1876 | goto BatchFailed; |
| 1877 | } |
| 1878 | if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0) |
| 1879 | goto BatchFailed; |
| 1880 | if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0) |
| 1881 | goto BatchFailed; |
| 1882 | Py_CLEAR(p); |
| 1883 | n += 1; |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1884 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1885 | if (n == BATCHSIZE) |
| 1886 | break; |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1887 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1888 | p = PyIter_Next(iter); |
| 1889 | if (p == NULL) { |
| 1890 | if (PyErr_Occurred()) |
| 1891 | goto BatchFailed; |
| 1892 | break; |
| 1893 | } |
| 1894 | } |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1895 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1896 | if (self->write_func(self, &setitems, 1) < 0) |
| 1897 | goto BatchFailed; |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1898 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1899 | } while (n == BATCHSIZE); |
| 1900 | return 0; |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1901 | |
| 1902 | BatchFailed: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1903 | Py_XDECREF(firstitem); |
| 1904 | Py_XDECREF(p); |
| 1905 | return -1; |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1906 | } |
| 1907 | |
Collin Winter | 179bf21 | 2009-05-25 04:34:39 +0000 | [diff] [blame] | 1908 | /* This is a variant of batch_dict() above that specializes for dicts, with no |
| 1909 | * support for dict subclasses. Like batch_dict(), we batch up chunks of |
| 1910 | * MARK key value ... key value SETITEMS |
| 1911 | * opcode sequences. Calling code should have arranged to first create an |
| 1912 | * empty dict, or dict-like object, for the SETITEMS to operate on. |
| 1913 | * Returns 0 on success, -1 on error. |
| 1914 | * |
| 1915 | * Note that this currently doesn't work for protocol 0. |
| 1916 | */ |
| 1917 | static int |
| 1918 | batch_dict_exact(Picklerobject *self, PyObject *obj) |
| 1919 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1920 | PyObject *key = NULL, *value = NULL; |
| 1921 | int i; |
| 1922 | Py_ssize_t dict_size, ppos = 0; |
Collin Winter | 179bf21 | 2009-05-25 04:34:39 +0000 | [diff] [blame] | 1923 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1924 | static char setitem = SETITEM; |
| 1925 | static char setitems = SETITEMS; |
Collin Winter | 179bf21 | 2009-05-25 04:34:39 +0000 | [diff] [blame] | 1926 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1927 | assert(obj != NULL); |
| 1928 | assert(self->proto > 0); |
Collin Winter | 179bf21 | 2009-05-25 04:34:39 +0000 | [diff] [blame] | 1929 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1930 | dict_size = PyDict_Size(obj); |
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 | /* Special-case len(d) == 1 to save space. */ |
| 1933 | if (dict_size == 1) { |
| 1934 | PyDict_Next(obj, &ppos, &key, &value); |
| 1935 | if (save(self, key, 0) < 0) |
| 1936 | return -1; |
| 1937 | if (save(self, value, 0) < 0) |
| 1938 | return -1; |
| 1939 | if (self->write_func(self, &setitem, 1) < 0) |
| 1940 | return -1; |
| 1941 | return 0; |
| 1942 | } |
Collin Winter | 179bf21 | 2009-05-25 04:34:39 +0000 | [diff] [blame] | 1943 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1944 | /* Write in batches of BATCHSIZE. */ |
| 1945 | do { |
| 1946 | i = 0; |
| 1947 | if (self->write_func(self, &MARKv, 1) < 0) |
| 1948 | return -1; |
| 1949 | while (PyDict_Next(obj, &ppos, &key, &value)) { |
| 1950 | if (save(self, key, 0) < 0) |
| 1951 | return -1; |
| 1952 | if (save(self, value, 0) < 0) |
| 1953 | return -1; |
| 1954 | if (++i == BATCHSIZE) |
| 1955 | break; |
| 1956 | } |
| 1957 | if (self->write_func(self, &setitems, 1) < 0) |
| 1958 | return -1; |
| 1959 | if (PyDict_Size(obj) != dict_size) { |
| 1960 | PyErr_Format( |
| 1961 | PyExc_RuntimeError, |
| 1962 | "dictionary changed size during iteration"); |
| 1963 | return -1; |
| 1964 | } |
Collin Winter | 179bf21 | 2009-05-25 04:34:39 +0000 | [diff] [blame] | 1965 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1966 | } while (i == BATCHSIZE); |
| 1967 | return 0; |
Collin Winter | 179bf21 | 2009-05-25 04:34:39 +0000 | [diff] [blame] | 1968 | } |
| 1969 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1970 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1971 | save_dict(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1972 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1973 | int res = -1; |
| 1974 | char s[3]; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 1975 | Py_ssize_t len; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1976 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1977 | if (self->fast && !fast_save_enter(self, args)) |
| 1978 | goto finally; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 1979 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1980 | /* Create an empty dict. */ |
| 1981 | if (self->bin) { |
| 1982 | s[0] = EMPTY_DICT; |
| 1983 | len = 1; |
| 1984 | } |
| 1985 | else { |
| 1986 | s[0] = MARK; |
| 1987 | s[1] = DICT; |
| 1988 | len = 2; |
| 1989 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1990 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1991 | if (self->write_func(self, s, len) < 0) |
| 1992 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1993 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1994 | /* Get dict size, and bow out early if empty. */ |
| 1995 | if ((len = PyDict_Size(args)) < 0) |
| 1996 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1997 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1998 | if (len == 0) { |
| 1999 | if (put(self, args) >= 0) |
| 2000 | res = 0; |
| 2001 | goto finally; |
| 2002 | } |
| 2003 | if (put2(self, args) < 0) |
| 2004 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2005 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2006 | /* Materialize the dict items. */ |
| 2007 | if (PyDict_CheckExact(args) && self->proto > 0) { |
| 2008 | /* We can take certain shortcuts if we know this is a dict and |
| 2009 | not a dict subclass. */ |
| 2010 | if (Py_EnterRecursiveCall(" while pickling an object") == 0) { |
| 2011 | res = batch_dict_exact(self, args); |
| 2012 | Py_LeaveRecursiveCall(); |
| 2013 | } |
| 2014 | } else { |
| 2015 | PyObject *iter = PyObject_CallMethod(args, "iteritems", "()"); |
| 2016 | if (iter == NULL) |
| 2017 | goto finally; |
| 2018 | if (Py_EnterRecursiveCall(" while pickling an object") == 0) { |
| 2019 | res = batch_dict(self, iter); |
| 2020 | Py_LeaveRecursiveCall(); |
| 2021 | } |
| 2022 | Py_DECREF(iter); |
| 2023 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2024 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2025 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2026 | if (self->fast && !fast_save_leave(self, args)) |
| 2027 | res = -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2028 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2029 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2030 | } |
| 2031 | |
| 2032 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2033 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2034 | save_inst(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2035 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2036 | PyObject *class = 0, *module = 0, *name = 0, *state = 0, |
| 2037 | *getinitargs_func = 0, *getstate_func = 0, *class_args = 0; |
| 2038 | char *module_str, *name_str; |
| 2039 | int module_size, name_size, res = -1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2040 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2041 | static char inst = INST, obj = OBJ, build = BUILD; |
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 | if (self->fast && !fast_save_enter(self, args)) |
| 2044 | goto finally; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 2045 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2046 | if (self->write_func(self, &MARKv, 1) < 0) |
| 2047 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2048 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2049 | if (!( class = PyObject_GetAttr(args, __class___str))) |
| 2050 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2051 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2052 | if (self->bin) { |
| 2053 | if (save(self, class, 0) < 0) |
| 2054 | goto finally; |
| 2055 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2056 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2057 | if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) { |
| 2058 | PyObject *element = 0; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 2059 | Py_ssize_t i, len; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2060 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2061 | if (!( class_args = |
| 2062 | PyObject_Call(getinitargs_func, empty_tuple, NULL))) |
| 2063 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2064 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2065 | if ((len = PyObject_Size(class_args)) < 0) |
| 2066 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2067 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2068 | for (i = 0; i < len; i++) { |
| 2069 | if (!( element = PySequence_GetItem(class_args, i))) |
| 2070 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2071 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2072 | if (save(self, element, 0) < 0) { |
| 2073 | Py_DECREF(element); |
| 2074 | goto finally; |
| 2075 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2076 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2077 | Py_DECREF(element); |
| 2078 | } |
| 2079 | } |
| 2080 | else { |
| 2081 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 2082 | PyErr_Clear(); |
| 2083 | else |
| 2084 | goto finally; |
| 2085 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2086 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2087 | if (!self->bin) { |
| 2088 | if (!( name = ((PyClassObject *)class)->cl_name )) { |
| 2089 | PyErr_SetString(PicklingError, "class has no name"); |
| 2090 | goto finally; |
| 2091 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2092 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2093 | if (!( module = whichmodule(class, name))) |
| 2094 | goto finally; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 2095 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2096 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2097 | if ((module_size = PyString_Size(module)) < 0 || |
| 2098 | (name_size = PyString_Size(name)) < 0) |
| 2099 | goto finally; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 2100 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2101 | module_str = PyString_AS_STRING((PyStringObject *)module); |
| 2102 | name_str = PyString_AS_STRING((PyStringObject *)name); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2103 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2104 | if (self->write_func(self, &inst, 1) < 0) |
| 2105 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2106 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2107 | if (self->write_func(self, module_str, module_size) < 0) |
| 2108 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2109 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2110 | if (self->write_func(self, "\n", 1) < 0) |
| 2111 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2112 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2113 | if (self->write_func(self, name_str, name_size) < 0) |
| 2114 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2115 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2116 | if (self->write_func(self, "\n", 1) < 0) |
| 2117 | goto finally; |
| 2118 | } |
| 2119 | else if (self->write_func(self, &obj, 1) < 0) { |
| 2120 | goto finally; |
| 2121 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2122 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2123 | if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) { |
| 2124 | state = PyObject_Call(getstate_func, empty_tuple, NULL); |
| 2125 | if (!state) |
| 2126 | goto finally; |
| 2127 | } |
| 2128 | else { |
| 2129 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 2130 | PyErr_Clear(); |
| 2131 | else |
| 2132 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2133 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2134 | if (!( state = PyObject_GetAttr(args, __dict___str))) { |
| 2135 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 2136 | PyErr_Clear(); |
| 2137 | else |
| 2138 | goto finally; |
| 2139 | res = 0; |
| 2140 | goto finally; |
| 2141 | } |
| 2142 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2143 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2144 | if (!PyDict_Check(state)) { |
| 2145 | if (put2(self, args) < 0) |
| 2146 | goto finally; |
| 2147 | } |
| 2148 | else { |
| 2149 | if (put(self, args) < 0) |
| 2150 | goto finally; |
| 2151 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2152 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2153 | if (save(self, state, 0) < 0) |
| 2154 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2155 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2156 | if (self->write_func(self, &build, 1) < 0) |
| 2157 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2158 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2159 | res = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2160 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2161 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2162 | if (self->fast && !fast_save_leave(self, args)) |
| 2163 | res = -1; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 2164 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2165 | Py_XDECREF(module); |
| 2166 | Py_XDECREF(class); |
| 2167 | Py_XDECREF(state); |
| 2168 | Py_XDECREF(getinitargs_func); |
| 2169 | Py_XDECREF(getstate_func); |
| 2170 | Py_XDECREF(class_args); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2171 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2172 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2173 | } |
| 2174 | |
| 2175 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2176 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2177 | save_global(Picklerobject *self, PyObject *args, PyObject *name) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2178 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2179 | PyObject *global_name = 0, *module = 0, *mod = 0, *klass = 0; |
| 2180 | char *name_str, *module_str; |
| 2181 | int module_size, name_size, res = -1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2182 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2183 | static char global = GLOBAL; |
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 | if (name) { |
| 2186 | global_name = name; |
| 2187 | Py_INCREF(global_name); |
| 2188 | } |
| 2189 | else { |
| 2190 | if (!( global_name = PyObject_GetAttr(args, __name___str))) |
| 2191 | goto finally; |
| 2192 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2193 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2194 | if (!( module = whichmodule(args, global_name))) |
| 2195 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2196 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2197 | if ((module_size = PyString_Size(module)) < 0 || |
| 2198 | (name_size = PyString_Size(global_name)) < 0) |
| 2199 | goto finally; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 2200 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2201 | module_str = PyString_AS_STRING((PyStringObject *)module); |
| 2202 | name_str = PyString_AS_STRING((PyStringObject *)global_name); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2203 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2204 | /* XXX This can be doing a relative import. Clearly it shouldn't, |
| 2205 | but I don't know how to stop it. :-( */ |
| 2206 | mod = PyImport_ImportModule(module_str); |
| 2207 | if (mod == NULL) { |
| 2208 | cPickle_ErrFormat(PicklingError, |
| 2209 | "Can't pickle %s: import of module %s " |
| 2210 | "failed", |
| 2211 | "OS", args, module); |
| 2212 | goto finally; |
| 2213 | } |
| 2214 | klass = PyObject_GetAttrString(mod, name_str); |
| 2215 | if (klass == NULL) { |
| 2216 | cPickle_ErrFormat(PicklingError, |
| 2217 | "Can't pickle %s: attribute lookup %s.%s " |
| 2218 | "failed", |
| 2219 | "OSS", args, module, global_name); |
| 2220 | goto finally; |
| 2221 | } |
| 2222 | if (klass != args) { |
| 2223 | Py_DECREF(klass); |
| 2224 | cPickle_ErrFormat(PicklingError, |
| 2225 | "Can't pickle %s: it's not the same object " |
| 2226 | "as %s.%s", |
| 2227 | "OSS", args, module, global_name); |
| 2228 | goto finally; |
| 2229 | } |
| 2230 | Py_DECREF(klass); |
Guido van Rossum | a92d16a | 2001-08-18 21:22:07 +0000 | [diff] [blame] | 2231 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2232 | if (self->proto >= 2) { |
| 2233 | /* See whether this is in the extension registry, and if |
| 2234 | * so generate an EXT opcode. |
| 2235 | */ |
| 2236 | PyObject *py_code; /* extension code as Python object */ |
| 2237 | long code; /* extension code as C value */ |
| 2238 | char c_str[5]; |
| 2239 | int n; |
Tim Peters | 731098b | 2003-02-04 20:56:09 +0000 | [diff] [blame] | 2240 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2241 | PyTuple_SET_ITEM(two_tuple, 0, module); |
| 2242 | PyTuple_SET_ITEM(two_tuple, 1, global_name); |
| 2243 | py_code = PyDict_GetItem(extension_registry, two_tuple); |
| 2244 | if (py_code == NULL) |
| 2245 | goto gen_global; /* not registered */ |
Tim Peters | 731098b | 2003-02-04 20:56:09 +0000 | [diff] [blame] | 2246 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2247 | /* Verify py_code has the right type and value. */ |
| 2248 | if (!PyInt_Check(py_code)) { |
| 2249 | cPickle_ErrFormat(PicklingError, "Can't pickle %s: " |
| 2250 | "extension code %s isn't an integer", |
| 2251 | "OO", args, py_code); |
| 2252 | goto finally; |
| 2253 | } |
| 2254 | code = PyInt_AS_LONG(py_code); |
| 2255 | if (code <= 0 || code > 0x7fffffffL) { |
| 2256 | cPickle_ErrFormat(PicklingError, "Can't pickle %s: " |
| 2257 | "extension code %ld is out of range", |
| 2258 | "Ol", args, code); |
| 2259 | goto finally; |
| 2260 | } |
Tim Peters | 731098b | 2003-02-04 20:56:09 +0000 | [diff] [blame] | 2261 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2262 | /* Generate an EXT opcode. */ |
| 2263 | if (code <= 0xff) { |
| 2264 | c_str[0] = EXT1; |
| 2265 | c_str[1] = (char)code; |
| 2266 | n = 2; |
| 2267 | } |
| 2268 | else if (code <= 0xffff) { |
| 2269 | c_str[0] = EXT2; |
| 2270 | c_str[1] = (char)(code & 0xff); |
| 2271 | c_str[2] = (char)((code >> 8) & 0xff); |
| 2272 | n = 3; |
| 2273 | } |
| 2274 | else { |
| 2275 | c_str[0] = EXT4; |
| 2276 | c_str[1] = (char)(code & 0xff); |
| 2277 | c_str[2] = (char)((code >> 8) & 0xff); |
| 2278 | c_str[3] = (char)((code >> 16) & 0xff); |
| 2279 | c_str[4] = (char)((code >> 24) & 0xff); |
| 2280 | n = 5; |
| 2281 | } |
Tim Peters | 731098b | 2003-02-04 20:56:09 +0000 | [diff] [blame] | 2282 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2283 | if (self->write_func(self, c_str, n) >= 0) |
| 2284 | res = 0; |
| 2285 | goto finally; /* and don't memoize */ |
| 2286 | } |
Tim Peters | 731098b | 2003-02-04 20:56:09 +0000 | [diff] [blame] | 2287 | |
| 2288 | gen_global: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2289 | if (self->write_func(self, &global, 1) < 0) |
| 2290 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2291 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2292 | if (self->write_func(self, module_str, module_size) < 0) |
| 2293 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2294 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2295 | if (self->write_func(self, "\n", 1) < 0) |
| 2296 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2297 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2298 | if (self->write_func(self, name_str, name_size) < 0) |
| 2299 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2300 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2301 | if (self->write_func(self, "\n", 1) < 0) |
| 2302 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2303 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2304 | if (put(self, args) < 0) |
| 2305 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2306 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2307 | res = 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2308 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2309 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2310 | Py_XDECREF(module); |
| 2311 | Py_XDECREF(global_name); |
| 2312 | Py_XDECREF(mod); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2313 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2314 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2315 | } |
| 2316 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2317 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2318 | save_pers(Picklerobject *self, PyObject *args, PyObject *f) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2319 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2320 | PyObject *pid = 0; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 2321 | Py_ssize_t size; |
| 2322 | int res = -1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2323 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2324 | static char persid = PERSID, binpersid = BINPERSID; |
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 | Py_INCREF(args); |
| 2327 | ARG_TUP(self, args); |
| 2328 | if (self->arg) { |
| 2329 | pid = PyObject_Call(f, self->arg, NULL); |
| 2330 | FREE_ARG_TUP(self); |
| 2331 | } |
| 2332 | if (! pid) return -1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2333 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2334 | if (pid != Py_None) { |
| 2335 | if (!self->bin) { |
| 2336 | if (!PyString_Check(pid)) { |
| 2337 | PyErr_SetString(PicklingError, |
| 2338 | "persistent id must be string"); |
| 2339 | goto finally; |
| 2340 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2341 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2342 | if (self->write_func(self, &persid, 1) < 0) |
| 2343 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2344 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2345 | if ((size = PyString_Size(pid)) < 0) |
| 2346 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2347 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2348 | if (self->write_func(self, |
| 2349 | PyString_AS_STRING( |
| 2350 | (PyStringObject *)pid), |
| 2351 | size) < 0) |
| 2352 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2353 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2354 | if (self->write_func(self, "\n", 1) < 0) |
| 2355 | goto finally; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2356 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2357 | res = 1; |
| 2358 | goto finally; |
| 2359 | } |
| 2360 | else if (save(self, pid, 1) >= 0) { |
| 2361 | if (self->write_func(self, &binpersid, 1) < 0) |
| 2362 | res = -1; |
| 2363 | else |
| 2364 | res = 1; |
| 2365 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2366 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2367 | goto finally; |
| 2368 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2369 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2370 | res = 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2371 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2372 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2373 | Py_XDECREF(pid); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2374 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2375 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2376 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2377 | |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2378 | /* We're saving ob, and args is the 2-thru-5 tuple returned by the |
| 2379 | * appropriate __reduce__ method for ob. |
| 2380 | */ |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2381 | static int |
Amaury Forgeot d'Arc | 69a9c5b | 2008-10-30 21:18:34 +0000 | [diff] [blame] | 2382 | save_reduce(Picklerobject *self, PyObject *args, PyObject *fn, PyObject *ob) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2383 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2384 | PyObject *callable; |
| 2385 | PyObject *argtup; |
| 2386 | PyObject *state = NULL; |
| 2387 | PyObject *listitems = Py_None; |
| 2388 | PyObject *dictitems = Py_None; |
| 2389 | Py_ssize_t size; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2390 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2391 | int use_newobj = self->proto >= 2; |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2392 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2393 | static char reduce = REDUCE; |
| 2394 | static char build = BUILD; |
| 2395 | static char newobj = NEWOBJ; |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2396 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2397 | size = PyTuple_Size(args); |
| 2398 | if (size < 2 || size > 5) { |
| 2399 | cPickle_ErrFormat(PicklingError, "tuple returned by " |
| 2400 | "%s must contain 2 through 5 elements", |
| 2401 | "O", fn); |
| 2402 | return -1; |
| 2403 | } |
Amaury Forgeot d'Arc | 69a9c5b | 2008-10-30 21:18:34 +0000 | [diff] [blame] | 2404 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2405 | if (! PyArg_UnpackTuple(args, "save_reduce", 2, 5, |
| 2406 | &callable, |
| 2407 | &argtup, |
| 2408 | &state, |
| 2409 | &listitems, |
| 2410 | &dictitems)) |
| 2411 | return -1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2412 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2413 | if (!PyTuple_Check(argtup)) { |
| 2414 | cPickle_ErrFormat(PicklingError, "Second element of " |
| 2415 | "tuple returned by %s must be a tuple", |
| 2416 | "O", fn); |
| 2417 | return -1; |
| 2418 | } |
Raymond Hettinger | a6b45cc | 2004-12-07 07:05:57 +0000 | [diff] [blame] | 2419 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2420 | if (state == Py_None) |
| 2421 | state = NULL; |
Amaury Forgeot d'Arc | 69a9c5b | 2008-10-30 21:18:34 +0000 | [diff] [blame] | 2422 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2423 | if (listitems == Py_None) |
| 2424 | listitems = NULL; |
| 2425 | else if (!PyIter_Check(listitems)) { |
| 2426 | cPickle_ErrFormat(PicklingError, "Fourth element of " |
| 2427 | "tuple returned by %s must be an iterator, not %s", |
| 2428 | "Os", fn, Py_TYPE(listitems)->tp_name); |
| 2429 | return -1; |
| 2430 | } |
Amaury Forgeot d'Arc | 69a9c5b | 2008-10-30 21:18:34 +0000 | [diff] [blame] | 2431 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2432 | if (dictitems == Py_None) |
| 2433 | dictitems = NULL; |
| 2434 | else if (!PyIter_Check(dictitems)) { |
| 2435 | cPickle_ErrFormat(PicklingError, "Fifth element of " |
| 2436 | "tuple returned by %s must be an iterator, not %s", |
| 2437 | "Os", fn, Py_TYPE(dictitems)->tp_name); |
| 2438 | return -1; |
| 2439 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2440 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2441 | /* Protocol 2 special case: if callable's name is __newobj__, use |
| 2442 | * NEWOBJ. This consumes a lot of code. |
| 2443 | */ |
| 2444 | if (use_newobj) { |
| 2445 | PyObject *temp = PyObject_GetAttr(callable, __name___str); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2446 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2447 | if (temp == NULL) { |
| 2448 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 2449 | PyErr_Clear(); |
| 2450 | else |
| 2451 | return -1; |
| 2452 | use_newobj = 0; |
| 2453 | } |
| 2454 | else { |
| 2455 | use_newobj = PyString_Check(temp) && |
| 2456 | strcmp(PyString_AS_STRING(temp), |
| 2457 | "__newobj__") == 0; |
| 2458 | Py_DECREF(temp); |
| 2459 | } |
| 2460 | } |
| 2461 | if (use_newobj) { |
| 2462 | PyObject *cls; |
| 2463 | PyObject *newargtup; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 2464 | Py_ssize_t n, i; |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2465 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2466 | /* Sanity checks. */ |
| 2467 | n = PyTuple_Size(argtup); |
| 2468 | if (n < 1) { |
| 2469 | PyErr_SetString(PicklingError, "__newobj__ arglist " |
| 2470 | "is empty"); |
| 2471 | return -1; |
| 2472 | } |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2473 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2474 | cls = PyTuple_GET_ITEM(argtup, 0); |
| 2475 | if (! PyObject_HasAttrString(cls, "__new__")) { |
| 2476 | PyErr_SetString(PicklingError, "args[0] from " |
| 2477 | "__newobj__ args has no __new__"); |
| 2478 | return -1; |
| 2479 | } |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2480 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2481 | /* XXX How could ob be NULL? */ |
| 2482 | if (ob != NULL) { |
| 2483 | PyObject *ob_dot_class; |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2484 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2485 | ob_dot_class = PyObject_GetAttr(ob, __class___str); |
| 2486 | if (ob_dot_class == NULL) { |
| 2487 | if (PyErr_ExceptionMatches( |
| 2488 | PyExc_AttributeError)) |
| 2489 | PyErr_Clear(); |
| 2490 | else |
| 2491 | return -1; |
| 2492 | } |
| 2493 | i = ob_dot_class != cls; /* true iff a problem */ |
| 2494 | Py_XDECREF(ob_dot_class); |
| 2495 | if (i) { |
| 2496 | PyErr_SetString(PicklingError, "args[0] from " |
| 2497 | "__newobj__ args has the wrong class"); |
| 2498 | return -1; |
| 2499 | } |
| 2500 | } |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2501 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2502 | /* Save the class and its __new__ arguments. */ |
| 2503 | if (save(self, cls, 0) < 0) |
| 2504 | return -1; |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2505 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2506 | newargtup = PyTuple_New(n-1); /* argtup[1:] */ |
| 2507 | if (newargtup == NULL) |
| 2508 | return -1; |
| 2509 | for (i = 1; i < n; ++i) { |
| 2510 | PyObject *temp = PyTuple_GET_ITEM(argtup, i); |
| 2511 | Py_INCREF(temp); |
| 2512 | PyTuple_SET_ITEM(newargtup, i-1, temp); |
| 2513 | } |
| 2514 | i = save(self, newargtup, 0); |
| 2515 | Py_DECREF(newargtup); |
| 2516 | if (i < 0) |
| 2517 | return -1; |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2518 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2519 | /* Add NEWOBJ opcode. */ |
| 2520 | if (self->write_func(self, &newobj, 1) < 0) |
| 2521 | return -1; |
| 2522 | } |
| 2523 | else { |
| 2524 | /* Not using NEWOBJ. */ |
| 2525 | if (save(self, callable, 0) < 0 || |
| 2526 | save(self, argtup, 0) < 0 || |
| 2527 | self->write_func(self, &reduce, 1) < 0) |
| 2528 | return -1; |
| 2529 | } |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2530 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2531 | /* Memoize. */ |
| 2532 | /* XXX How can ob be NULL? */ |
| 2533 | if (ob != NULL) { |
Serhiy Storchaka | da87e45 | 2015-11-07 11:15:32 +0200 | [diff] [blame] | 2534 | /* If the object is already in the memo, this means it is |
| 2535 | recursive. In this case, throw away everything we put on the |
| 2536 | stack, and fetch the object back from the memo. */ |
| 2537 | if (Py_REFCNT(ob) > 1 && !self->fast) { |
| 2538 | PyObject *py_ob_id = PyLong_FromVoidPtr(ob); |
| 2539 | if (!py_ob_id) |
| 2540 | return -1; |
| 2541 | if (PyDict_GetItem(self->memo, py_ob_id)) { |
| 2542 | const char pop_op = POP; |
| 2543 | if (self->write_func(self, &pop_op, 1) < 0 || |
| 2544 | get(self, py_ob_id) < 0) { |
| 2545 | Py_DECREF(py_ob_id); |
| 2546 | return -1; |
| 2547 | } |
| 2548 | Py_DECREF(py_ob_id); |
| 2549 | return 0; |
| 2550 | } |
| 2551 | Py_DECREF(py_ob_id); |
| 2552 | if (PyErr_Occurred()) |
| 2553 | return -1; |
| 2554 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2555 | if (state && !PyDict_Check(state)) { |
| 2556 | if (put2(self, ob) < 0) |
| 2557 | return -1; |
| 2558 | } |
| 2559 | else if (put(self, ob) < 0) |
| 2560 | return -1; |
| 2561 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2562 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2563 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2564 | if (listitems && batch_list(self, listitems) < 0) |
| 2565 | return -1; |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2566 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2567 | if (dictitems && batch_dict(self, dictitems) < 0) |
| 2568 | return -1; |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2569 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2570 | if (state) { |
| 2571 | if (save(self, state, 0) < 0 || |
| 2572 | self->write_func(self, &build, 1) < 0) |
| 2573 | return -1; |
| 2574 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2575 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2576 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2577 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2578 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2579 | static int |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 2580 | save(Picklerobject *self, PyObject *args, int pers_save) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2581 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2582 | PyTypeObject *type; |
| 2583 | PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0; |
| 2584 | int res = -1; |
| 2585 | int tmp; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2586 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2587 | if (Py_EnterRecursiveCall(" while pickling an object")) |
| 2588 | return -1; |
Martin v. Löwis | 5a39530 | 2002-08-04 08:20:23 +0000 | [diff] [blame] | 2589 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2590 | if (!pers_save && self->pers_func) { |
| 2591 | if ((tmp = save_pers(self, args, self->pers_func)) != 0) { |
| 2592 | res = tmp; |
| 2593 | goto finally; |
| 2594 | } |
| 2595 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2596 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2597 | if (args == Py_None) { |
| 2598 | res = save_none(self, args); |
| 2599 | goto finally; |
| 2600 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2601 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2602 | type = Py_TYPE(args); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2603 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2604 | switch (type->tp_name[0]) { |
| 2605 | case 'b': |
| 2606 | if (args == Py_False || args == Py_True) { |
| 2607 | res = save_bool(self, args); |
| 2608 | goto finally; |
| 2609 | } |
| 2610 | break; |
| 2611 | case 'i': |
| 2612 | if (type == &PyInt_Type) { |
| 2613 | res = save_int(self, args); |
| 2614 | goto finally; |
| 2615 | } |
| 2616 | break; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2617 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2618 | case 'l': |
| 2619 | if (type == &PyLong_Type) { |
| 2620 | res = save_long(self, args); |
| 2621 | goto finally; |
| 2622 | } |
| 2623 | break; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2624 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2625 | case 'f': |
| 2626 | if (type == &PyFloat_Type) { |
| 2627 | res = save_float(self, args); |
| 2628 | goto finally; |
| 2629 | } |
| 2630 | break; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2631 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2632 | case 't': |
| 2633 | if (type == &PyTuple_Type && PyTuple_Size(args) == 0) { |
| 2634 | res = save_tuple(self, args); |
| 2635 | goto finally; |
| 2636 | } |
| 2637 | break; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2638 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2639 | case 's': |
| 2640 | if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) { |
| 2641 | res = save_string(self, args, 0); |
| 2642 | goto finally; |
| 2643 | } |
| 2644 | break; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 2645 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2646 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2647 | case 'u': |
| 2648 | if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) { |
| 2649 | res = save_unicode(self, args, 0); |
| 2650 | goto finally; |
| 2651 | } |
| 2652 | break; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2653 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2654 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2655 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2656 | if (Py_REFCNT(args) > 1) { |
| 2657 | if (!( py_ob_id = PyLong_FromVoidPtr(args))) |
| 2658 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2659 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2660 | if (PyDict_GetItem(self->memo, py_ob_id)) { |
| 2661 | if (get(self, py_ob_id) < 0) |
| 2662 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2663 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2664 | res = 0; |
| 2665 | goto finally; |
| 2666 | } |
| 2667 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2668 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2669 | switch (type->tp_name[0]) { |
| 2670 | case 's': |
| 2671 | if (type == &PyString_Type) { |
| 2672 | res = save_string(self, args, 1); |
| 2673 | goto finally; |
| 2674 | } |
| 2675 | break; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2676 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2677 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2678 | case 'u': |
| 2679 | if (type == &PyUnicode_Type) { |
| 2680 | res = save_unicode(self, args, 1); |
| 2681 | goto finally; |
| 2682 | } |
| 2683 | break; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2684 | #endif |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 2685 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2686 | case 't': |
| 2687 | if (type == &PyTuple_Type) { |
| 2688 | res = save_tuple(self, args); |
| 2689 | goto finally; |
| 2690 | } |
| 2691 | if (type == &PyType_Type) { |
Alexandre Vassalotti | df9460f | 2013-11-30 17:43:42 -0800 | [diff] [blame] | 2692 | res = save_global(self, args, NULL); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2693 | goto finally; |
| 2694 | } |
| 2695 | break; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2696 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2697 | case 'l': |
| 2698 | if (type == &PyList_Type) { |
| 2699 | res = save_list(self, args); |
| 2700 | goto finally; |
| 2701 | } |
| 2702 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2703 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2704 | case 'd': |
| 2705 | if (type == &PyDict_Type) { |
| 2706 | res = save_dict(self, args); |
| 2707 | goto finally; |
| 2708 | } |
| 2709 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2710 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2711 | case 'i': |
| 2712 | if (type == &PyInstance_Type) { |
| 2713 | res = save_inst(self, args); |
| 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 'c': |
| 2719 | if (type == &PyClass_Type) { |
| 2720 | res = save_global(self, args, NULL); |
| 2721 | goto finally; |
| 2722 | } |
| 2723 | break; |
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 | case 'f': |
| 2726 | if (type == &PyFunction_Type) { |
| 2727 | res = save_global(self, args, NULL); |
| 2728 | if (res && PyErr_ExceptionMatches(PickleError)) { |
| 2729 | /* fall back to reduce */ |
| 2730 | PyErr_Clear(); |
| 2731 | break; |
| 2732 | } |
| 2733 | goto finally; |
| 2734 | } |
| 2735 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2736 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2737 | case 'b': |
| 2738 | if (type == &PyCFunction_Type) { |
| 2739 | res = save_global(self, args, NULL); |
| 2740 | goto finally; |
| 2741 | } |
| 2742 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2743 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2744 | if (!pers_save && self->inst_pers_func) { |
| 2745 | if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) { |
| 2746 | res = tmp; |
| 2747 | goto finally; |
| 2748 | } |
| 2749 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2750 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2751 | /* Get a reduction callable, and call it. This may come from |
| 2752 | * copy_reg.dispatch_table, the object's __reduce_ex__ method, |
| 2753 | * or the object's __reduce__ method. |
| 2754 | */ |
| 2755 | __reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type); |
| 2756 | if (__reduce__ != NULL) { |
| 2757 | Py_INCREF(__reduce__); |
| 2758 | Py_INCREF(args); |
| 2759 | ARG_TUP(self, args); |
| 2760 | if (self->arg) { |
| 2761 | t = PyObject_Call(__reduce__, self->arg, NULL); |
| 2762 | FREE_ARG_TUP(self); |
| 2763 | } |
| 2764 | } |
| 2765 | else { |
Antoine Pitrou | 561a821 | 2011-10-04 09:34:48 +0200 | [diff] [blame] | 2766 | if (PyType_IsSubtype(type, &PyType_Type)) { |
| 2767 | res = save_global(self, args, NULL); |
| 2768 | goto finally; |
| 2769 | } |
| 2770 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2771 | /* Check for a __reduce_ex__ method. */ |
| 2772 | __reduce__ = PyObject_GetAttr(args, __reduce_ex___str); |
| 2773 | if (__reduce__ != NULL) { |
| 2774 | t = PyInt_FromLong(self->proto); |
| 2775 | if (t != NULL) { |
| 2776 | ARG_TUP(self, t); |
| 2777 | t = NULL; |
| 2778 | if (self->arg) { |
| 2779 | t = PyObject_Call(__reduce__, |
| 2780 | self->arg, NULL); |
| 2781 | FREE_ARG_TUP(self); |
| 2782 | } |
| 2783 | } |
| 2784 | } |
| 2785 | else { |
| 2786 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 2787 | PyErr_Clear(); |
| 2788 | else |
| 2789 | goto finally; |
| 2790 | /* Check for a __reduce__ method. */ |
| 2791 | __reduce__ = PyObject_GetAttr(args, __reduce___str); |
| 2792 | if (__reduce__ != NULL) { |
| 2793 | t = PyObject_Call(__reduce__, |
| 2794 | empty_tuple, NULL); |
| 2795 | } |
| 2796 | else { |
| 2797 | PyErr_SetObject(UnpickleableError, args); |
| 2798 | goto finally; |
| 2799 | } |
| 2800 | } |
| 2801 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2802 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2803 | if (t == NULL) |
| 2804 | goto finally; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2805 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2806 | if (PyString_Check(t)) { |
| 2807 | res = save_global(self, args, t); |
| 2808 | goto finally; |
| 2809 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2810 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2811 | if (!PyTuple_Check(t)) { |
| 2812 | cPickle_ErrFormat(PicklingError, "Value returned by " |
| 2813 | "%s must be string or tuple", |
| 2814 | "O", __reduce__); |
| 2815 | goto finally; |
| 2816 | } |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2817 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2818 | res = save_reduce(self, t, __reduce__, args); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2819 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2820 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2821 | Py_LeaveRecursiveCall(); |
| 2822 | Py_XDECREF(py_ob_id); |
| 2823 | Py_XDECREF(__reduce__); |
| 2824 | Py_XDECREF(t); |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2825 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2826 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2827 | } |
| 2828 | |
| 2829 | |
| 2830 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2831 | dump(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2832 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2833 | static char stop = STOP; |
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 | if (self->proto >= 2) { |
| 2836 | char bytes[2]; |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 2837 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2838 | bytes[0] = PROTO; |
| 2839 | assert(self->proto >= 0 && self->proto < 256); |
| 2840 | bytes[1] = (char)self->proto; |
| 2841 | if (self->write_func(self, bytes, 2) < 0) |
| 2842 | return -1; |
| 2843 | } |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 2844 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2845 | if (save(self, args, 0) < 0) |
| 2846 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2847 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2848 | if (self->write_func(self, &stop, 1) < 0) |
| 2849 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2850 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2851 | if (self->write_func(self, NULL, 0) < 0) |
| 2852 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2853 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2854 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2855 | } |
| 2856 | |
| 2857 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2858 | Pickle_clear_memo(Picklerobject *self, PyObject *args) |
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 | if (self->memo) |
| 2861 | PyDict_Clear(self->memo); |
| 2862 | Py_INCREF(Py_None); |
| 2863 | return Py_None; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2864 | } |
| 2865 | |
| 2866 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2867 | Pickle_getvalue(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2868 | { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 2869 | Py_ssize_t l, i, rsize, ssize, clear=1, lm; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2870 | long ik; |
| 2871 | PyObject *k, *r; |
| 2872 | char *s, *p, *have_get; |
| 2873 | Pdata *data; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2874 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2875 | /* Can be called by Python code or C code */ |
| 2876 | if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear)) |
| 2877 | return NULL; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2878 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2879 | /* Check to make sure we are based on a list */ |
| 2880 | if (! Pdata_Check(self->file)) { |
| 2881 | PyErr_SetString(PicklingError, |
| 2882 | "Attempt to getvalue() a non-list-based pickler"); |
| 2883 | return NULL; |
| 2884 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2885 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2886 | /* flush write buffer */ |
| 2887 | if (write_other(self, NULL, 0) < 0) return NULL; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2888 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2889 | data=(Pdata*)self->file; |
| 2890 | l=data->length; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2891 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2892 | /* set up an array to hold get/put status */ |
| 2893 | lm = PyDict_Size(self->memo); |
| 2894 | if (lm < 0) return NULL; |
| 2895 | lm++; |
| 2896 | have_get = malloc(lm); |
| 2897 | if (have_get == NULL) return PyErr_NoMemory(); |
| 2898 | memset(have_get, 0, lm); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2899 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2900 | /* Scan for gets. */ |
| 2901 | for (rsize = 0, i = l; --i >= 0; ) { |
| 2902 | k = data->data[i]; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2903 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2904 | if (PyString_Check(k)) |
| 2905 | rsize += PyString_GET_SIZE(k); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2906 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2907 | else if (PyInt_Check(k)) { /* put */ |
| 2908 | ik = PyInt_AS_LONG((PyIntObject*)k); |
| 2909 | if (ik >= lm || ik == 0) { |
| 2910 | PyErr_SetString(PicklingError, |
| 2911 | "Invalid get data"); |
| 2912 | goto err; |
| 2913 | } |
| 2914 | if (have_get[ik]) /* with matching get */ |
| 2915 | rsize += ik < 256 ? 2 : 5; |
| 2916 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2917 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2918 | else if (! (PyTuple_Check(k) && |
| 2919 | PyTuple_GET_SIZE(k) == 2 && |
| 2920 | PyInt_Check((k = PyTuple_GET_ITEM(k, 0)))) |
| 2921 | ) { |
| 2922 | PyErr_SetString(PicklingError, |
| 2923 | "Unexpected data in internal list"); |
| 2924 | goto err; |
| 2925 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2926 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2927 | else { /* put */ |
| 2928 | ik = PyInt_AS_LONG((PyIntObject *)k); |
| 2929 | if (ik >= lm || ik == 0) { |
| 2930 | PyErr_SetString(PicklingError, |
| 2931 | "Invalid get data"); |
Benjamin Peterson | 7f18ac4 | 2015-07-25 10:20:13 -0700 | [diff] [blame] | 2932 | goto err; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2933 | } |
| 2934 | have_get[ik] = 1; |
| 2935 | rsize += ik < 256 ? 2 : 5; |
| 2936 | } |
| 2937 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2938 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2939 | /* Now generate the result */ |
| 2940 | r = PyString_FromStringAndSize(NULL, rsize); |
| 2941 | if (r == NULL) goto err; |
| 2942 | s = PyString_AS_STRING((PyStringObject *)r); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2943 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2944 | for (i = 0; i < l; i++) { |
| 2945 | k = data->data[i]; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2946 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2947 | if (PyString_Check(k)) { |
| 2948 | ssize = PyString_GET_SIZE(k); |
| 2949 | if (ssize) { |
| 2950 | p=PyString_AS_STRING((PyStringObject *)k); |
| 2951 | while (--ssize >= 0) |
| 2952 | *s++ = *p++; |
| 2953 | } |
| 2954 | } |
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 | else if (PyTuple_Check(k)) { /* get */ |
| 2957 | ik = PyInt_AS_LONG((PyIntObject *) |
| 2958 | PyTuple_GET_ITEM(k, 0)); |
| 2959 | if (ik < 256) { |
| 2960 | *s++ = BINGET; |
| 2961 | *s++ = (int)(ik & 0xff); |
| 2962 | } |
| 2963 | else { |
| 2964 | *s++ = LONG_BINGET; |
| 2965 | *s++ = (int)(ik & 0xff); |
| 2966 | *s++ = (int)((ik >> 8) & 0xff); |
| 2967 | *s++ = (int)((ik >> 16) & 0xff); |
| 2968 | *s++ = (int)((ik >> 24) & 0xff); |
| 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 | else { /* put */ |
| 2973 | ik = PyInt_AS_LONG((PyIntObject*)k); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2974 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2975 | if (have_get[ik]) { /* with matching get */ |
| 2976 | if (ik < 256) { |
| 2977 | *s++ = BINPUT; |
| 2978 | *s++ = (int)(ik & 0xff); |
| 2979 | } |
| 2980 | else { |
| 2981 | *s++ = LONG_BINPUT; |
| 2982 | *s++ = (int)(ik & 0xff); |
| 2983 | *s++ = (int)((ik >> 8) & 0xff); |
| 2984 | *s++ = (int)((ik >> 16) & 0xff); |
| 2985 | *s++ = (int)((ik >> 24) & 0xff); |
| 2986 | } |
| 2987 | } |
| 2988 | } |
| 2989 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2990 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2991 | if (clear) { |
| 2992 | PyDict_Clear(self->memo); |
| 2993 | Pdata_clear(data, 0); |
| 2994 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2995 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2996 | free(have_get); |
| 2997 | return r; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2998 | err: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2999 | free(have_get); |
| 3000 | return NULL; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 3001 | } |
| 3002 | |
| 3003 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3004 | Pickler_dump(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3005 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3006 | PyObject *ob; |
| 3007 | int get=0; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 3008 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3009 | if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get))) |
| 3010 | return NULL; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 3011 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3012 | if (dump(self, ob) < 0) |
| 3013 | return NULL; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 3014 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3015 | if (get) return Pickle_getvalue(self, NULL); |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 3016 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3017 | /* XXX Why does dump() return self? */ |
| 3018 | Py_INCREF(self); |
| 3019 | return (PyObject*)self; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3020 | } |
| 3021 | |
| 3022 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3023 | static struct PyMethodDef Pickler_methods[] = |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3024 | { |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 3025 | {"dump", (PyCFunction)Pickler_dump, METH_VARARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 3026 | PyDoc_STR("dump(object) -- " |
| 3027 | "Write an object in pickle format to the object's pickle stream")}, |
Fred Drake | 0ebacc8 | 2002-05-01 20:36:39 +0000 | [diff] [blame] | 3028 | {"clear_memo", (PyCFunction)Pickle_clear_memo, METH_NOARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 3029 | PyDoc_STR("clear_memo() -- Clear the picklers memo")}, |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 3030 | {"getvalue", (PyCFunction)Pickle_getvalue, METH_VARARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 3031 | PyDoc_STR("getvalue() -- Finish picking a list-based pickle")}, |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3032 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3033 | }; |
| 3034 | |
| 3035 | |
| 3036 | static Picklerobject * |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 3037 | newPicklerobject(PyObject *file, int proto) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3038 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3039 | Picklerobject *self; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3040 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3041 | if (proto < 0) |
| 3042 | proto = HIGHEST_PROTOCOL; |
| 3043 | if (proto > HIGHEST_PROTOCOL) { |
| 3044 | PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; " |
| 3045 | "the highest available protocol is %d", |
| 3046 | proto, HIGHEST_PROTOCOL); |
| 3047 | return NULL; |
| 3048 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3049 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3050 | self = PyObject_GC_New(Picklerobject, &Picklertype); |
| 3051 | if (self == NULL) |
| 3052 | return NULL; |
| 3053 | self->proto = proto; |
| 3054 | self->bin = proto > 0; |
| 3055 | self->fp = NULL; |
| 3056 | self->write = NULL; |
| 3057 | self->memo = NULL; |
| 3058 | self->arg = NULL; |
| 3059 | self->pers_func = NULL; |
| 3060 | self->inst_pers_func = NULL; |
| 3061 | self->write_buf = NULL; |
| 3062 | self->fast = 0; |
| 3063 | self->fast_container = 0; |
| 3064 | self->fast_memo = NULL; |
| 3065 | self->buf_size = 0; |
| 3066 | self->dispatch_table = NULL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3067 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3068 | self->file = NULL; |
| 3069 | if (file) |
| 3070 | Py_INCREF(file); |
| 3071 | else { |
| 3072 | file = Pdata_New(); |
| 3073 | if (file == NULL) |
| 3074 | goto err; |
| 3075 | } |
| 3076 | self->file = file; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 3077 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3078 | if (!( self->memo = PyDict_New())) |
| 3079 | goto err; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3080 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3081 | if (PyFile_Check(file)) { |
| 3082 | self->fp = PyFile_AsFile(file); |
| 3083 | if (self->fp == NULL) { |
| 3084 | PyErr_SetString(PyExc_ValueError, |
| 3085 | "I/O operation on closed file"); |
| 3086 | goto err; |
| 3087 | } |
| 3088 | self->write_func = write_file; |
| 3089 | } |
| 3090 | else if (PycStringIO_OutputCheck(file)) { |
| 3091 | self->write_func = write_cStringIO; |
| 3092 | } |
| 3093 | else if (file == Py_None) { |
| 3094 | self->write_func = write_none; |
| 3095 | } |
| 3096 | else { |
| 3097 | self->write_func = write_other; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3098 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3099 | if (! Pdata_Check(file)) { |
| 3100 | self->write = PyObject_GetAttr(file, write_str); |
| 3101 | if (!self->write) { |
| 3102 | PyErr_Clear(); |
| 3103 | PyErr_SetString(PyExc_TypeError, |
| 3104 | "argument must have 'write' " |
| 3105 | "attribute"); |
| 3106 | goto err; |
| 3107 | } |
| 3108 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3109 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3110 | self->write_buf = (char *)PyMem_Malloc(WRITE_BUF_SIZE); |
| 3111 | if (self->write_buf == NULL) { |
| 3112 | PyErr_NoMemory(); |
| 3113 | goto err; |
| 3114 | } |
| 3115 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3116 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3117 | if (PyEval_GetRestricted()) { |
| 3118 | /* Restricted execution, get private tables */ |
| 3119 | PyObject *m = PyImport_ImportModule("copy_reg"); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3120 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3121 | if (m == NULL) |
| 3122 | goto err; |
| 3123 | self->dispatch_table = PyObject_GetAttr(m, dispatch_table_str); |
| 3124 | Py_DECREF(m); |
| 3125 | if (self->dispatch_table == NULL) |
| 3126 | goto err; |
| 3127 | } |
| 3128 | else { |
| 3129 | self->dispatch_table = dispatch_table; |
| 3130 | Py_INCREF(dispatch_table); |
| 3131 | } |
| 3132 | PyObject_GC_Track(self); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3133 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3134 | return self; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3135 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3136 | err: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3137 | Py_DECREF(self); |
| 3138 | return NULL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3139 | } |
| 3140 | |
| 3141 | |
| 3142 | static PyObject * |
Martin v. Löwis | 544f119 | 2004-07-27 05:22:33 +0000 | [diff] [blame] | 3143 | get_Pickler(PyObject *self, PyObject *args, PyObject *kwds) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3144 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3145 | static char *kwlist[] = {"file", "protocol", NULL}; |
| 3146 | PyObject *file = NULL; |
| 3147 | int proto = 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3148 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3149 | /* XXX |
| 3150 | * The documented signature is Pickler(file, protocol=0), but this |
| 3151 | * accepts Pickler() and Pickler(integer) too. The meaning then |
| 3152 | * is clear as mud, undocumented, and not supported by pickle.py. |
| 3153 | * I'm told Zope uses this, but I haven't traced into this code |
| 3154 | * far enough to figure out what it means. |
| 3155 | */ |
| 3156 | if (!PyArg_ParseTuple(args, "|i:Pickler", &proto)) { |
| 3157 | PyErr_Clear(); |
| 3158 | proto = 0; |
| 3159 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|i:Pickler", |
| 3160 | kwlist, &file, &proto)) |
| 3161 | return NULL; |
| 3162 | } |
| 3163 | return (PyObject *)newPicklerobject(file, proto); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3164 | } |
| 3165 | |
| 3166 | |
| 3167 | static void |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3168 | Pickler_dealloc(Picklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3169 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3170 | PyObject_GC_UnTrack(self); |
| 3171 | Py_XDECREF(self->write); |
| 3172 | Py_XDECREF(self->memo); |
| 3173 | Py_XDECREF(self->fast_memo); |
| 3174 | Py_XDECREF(self->arg); |
| 3175 | Py_XDECREF(self->file); |
| 3176 | Py_XDECREF(self->pers_func); |
| 3177 | Py_XDECREF(self->inst_pers_func); |
| 3178 | Py_XDECREF(self->dispatch_table); |
| 3179 | PyMem_Free(self->write_buf); |
| 3180 | Py_TYPE(self)->tp_free((PyObject *)self); |
Jeremy Hylton | 4cf6319 | 2003-04-09 21:05:12 +0000 | [diff] [blame] | 3181 | } |
| 3182 | |
| 3183 | static int |
| 3184 | Pickler_traverse(Picklerobject *self, visitproc visit, void *arg) |
| 3185 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3186 | Py_VISIT(self->write); |
| 3187 | Py_VISIT(self->memo); |
| 3188 | Py_VISIT(self->fast_memo); |
| 3189 | Py_VISIT(self->arg); |
| 3190 | Py_VISIT(self->file); |
| 3191 | Py_VISIT(self->pers_func); |
| 3192 | Py_VISIT(self->inst_pers_func); |
| 3193 | Py_VISIT(self->dispatch_table); |
| 3194 | return 0; |
Jeremy Hylton | 4cf6319 | 2003-04-09 21:05:12 +0000 | [diff] [blame] | 3195 | } |
| 3196 | |
| 3197 | static int |
| 3198 | Pickler_clear(Picklerobject *self) |
| 3199 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3200 | Py_CLEAR(self->write); |
| 3201 | Py_CLEAR(self->memo); |
| 3202 | Py_CLEAR(self->fast_memo); |
| 3203 | Py_CLEAR(self->arg); |
| 3204 | Py_CLEAR(self->file); |
| 3205 | Py_CLEAR(self->pers_func); |
| 3206 | Py_CLEAR(self->inst_pers_func); |
| 3207 | Py_CLEAR(self->dispatch_table); |
| 3208 | return 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3209 | } |
| 3210 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3211 | static PyObject * |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3212 | Pickler_get_pers_func(Picklerobject *p) |
| 3213 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3214 | if (p->pers_func == NULL) |
| 3215 | PyErr_SetString(PyExc_AttributeError, "persistent_id"); |
| 3216 | else |
| 3217 | Py_INCREF(p->pers_func); |
| 3218 | return p->pers_func; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3219 | } |
| 3220 | |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3221 | static int |
| 3222 | Pickler_set_pers_func(Picklerobject *p, PyObject *v) |
| 3223 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3224 | if (v == NULL) { |
| 3225 | PyErr_SetString(PyExc_TypeError, |
| 3226 | "attribute deletion is not supported"); |
| 3227 | return -1; |
| 3228 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3229 | Py_INCREF(v); |
Serhiy Storchaka | bc62af1 | 2016-04-06 09:51:18 +0300 | [diff] [blame] | 3230 | Py_XSETREF(p->pers_func, v); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3231 | return 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3232 | } |
| 3233 | |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3234 | static int |
| 3235 | Pickler_set_inst_pers_func(Picklerobject *p, PyObject *v) |
| 3236 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3237 | if (v == NULL) { |
| 3238 | PyErr_SetString(PyExc_TypeError, |
| 3239 | "attribute deletion is not supported"); |
| 3240 | return -1; |
| 3241 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3242 | Py_INCREF(v); |
Serhiy Storchaka | bc62af1 | 2016-04-06 09:51:18 +0300 | [diff] [blame] | 3243 | Py_XSETREF(p->inst_pers_func, v); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3244 | return 0; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3245 | } |
| 3246 | |
| 3247 | static PyObject * |
| 3248 | Pickler_get_memo(Picklerobject *p) |
| 3249 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3250 | if (p->memo == NULL) |
| 3251 | PyErr_SetString(PyExc_AttributeError, "memo"); |
| 3252 | else |
| 3253 | Py_INCREF(p->memo); |
| 3254 | return p->memo; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3255 | } |
| 3256 | |
| 3257 | static int |
| 3258 | Pickler_set_memo(Picklerobject *p, PyObject *v) |
| 3259 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3260 | if (v == NULL) { |
| 3261 | PyErr_SetString(PyExc_TypeError, |
| 3262 | "attribute deletion is not supported"); |
| 3263 | return -1; |
| 3264 | } |
| 3265 | if (!PyDict_Check(v)) { |
| 3266 | PyErr_SetString(PyExc_TypeError, "memo must be a dictionary"); |
| 3267 | return -1; |
| 3268 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3269 | Py_INCREF(v); |
Serhiy Storchaka | bc62af1 | 2016-04-06 09:51:18 +0300 | [diff] [blame] | 3270 | Py_XSETREF(p->memo, v); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3271 | return 0; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3272 | } |
| 3273 | |
| 3274 | static PyObject * |
| 3275 | Pickler_get_error(Picklerobject *p) |
| 3276 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3277 | /* why is this an attribute on the Pickler? */ |
| 3278 | Py_INCREF(PicklingError); |
| 3279 | return PicklingError; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3280 | } |
| 3281 | |
| 3282 | static PyMemberDef Pickler_members[] = { |
| 3283 | {"binary", T_INT, offsetof(Picklerobject, bin)}, |
| 3284 | {"fast", T_INT, offsetof(Picklerobject, fast)}, |
Jeremy Hylton | 3eb46f3 | 2001-10-16 17:10:49 +0000 | [diff] [blame] | 3285 | {NULL} |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3286 | }; |
| 3287 | |
| 3288 | static PyGetSetDef Pickler_getsets[] = { |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3289 | {"persistent_id", (getter)Pickler_get_pers_func, |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3290 | (setter)Pickler_set_pers_func}, |
| 3291 | {"inst_persistent_id", NULL, (setter)Pickler_set_inst_pers_func}, |
| 3292 | {"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo}, |
Jeremy Hylton | 3eb46f3 | 2001-10-16 17:10:49 +0000 | [diff] [blame] | 3293 | {"PicklingError", (getter)Pickler_get_error, NULL}, |
| 3294 | {NULL} |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3295 | }; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3296 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3297 | PyDoc_STRVAR(Picklertype__doc__, |
| 3298 | "Objects that know how to pickle objects\n"); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3299 | |
Guido van Rossum | 9716aaa | 1997-12-08 15:15:16 +0000 | [diff] [blame] | 3300 | static PyTypeObject Picklertype = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 3301 | PyVarObject_HEAD_INIT(NULL, 0) |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 3302 | "cPickle.Pickler", /*tp_name*/ |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3303 | sizeof(Picklerobject), /*tp_basicsize*/ |
| 3304 | 0, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3305 | (destructor)Pickler_dealloc, /* tp_dealloc */ |
| 3306 | 0, /* tp_print */ |
| 3307 | 0, /* tp_getattr */ |
| 3308 | 0, /* tp_setattr */ |
| 3309 | 0, /* tp_compare */ |
| 3310 | 0, /* tp_repr */ |
| 3311 | 0, /* tp_as_number */ |
| 3312 | 0, /* tp_as_sequence */ |
| 3313 | 0, /* tp_as_mapping */ |
| 3314 | 0, /* tp_hash */ |
| 3315 | 0, /* tp_call */ |
| 3316 | 0, /* tp_str */ |
| 3317 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3318 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 3319 | 0, /* tp_as_buffer */ |
Jeremy Hylton | 4cf6319 | 2003-04-09 21:05:12 +0000 | [diff] [blame] | 3320 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3321 | Picklertype__doc__, /* tp_doc */ |
| 3322 | (traverseproc)Pickler_traverse, /* tp_traverse */ |
| 3323 | (inquiry)Pickler_clear, /* tp_clear */ |
| 3324 | 0, /* tp_richcompare */ |
| 3325 | 0, /* tp_weaklistoffset */ |
| 3326 | 0, /* tp_iter */ |
| 3327 | 0, /* tp_iternext */ |
| 3328 | Pickler_methods, /* tp_methods */ |
| 3329 | Pickler_members, /* tp_members */ |
| 3330 | Pickler_getsets, /* tp_getset */ |
Guido van Rossum | 9716aaa | 1997-12-08 15:15:16 +0000 | [diff] [blame] | 3331 | }; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3332 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3333 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3334 | 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] | 3335 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3336 | PyObject *global = 0, *module; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3337 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3338 | if (fc) { |
| 3339 | if (fc==Py_None) { |
| 3340 | PyErr_SetString(UnpicklingError, "Global and instance " |
| 3341 | "pickles are not supported."); |
| 3342 | return NULL; |
| 3343 | } |
| 3344 | return PyObject_CallFunctionObjArgs(fc, py_module_name, |
| 3345 | py_global_name, NULL); |
| 3346 | } |
Guido van Rossum | 1b9e0aa | 1999-04-19 17:58:18 +0000 | [diff] [blame] | 3347 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3348 | module = PySys_GetObject("modules"); |
| 3349 | if (module == NULL) |
| 3350 | return NULL; |
Jeremy Hylton | d105523 | 1998-08-11 19:52:51 +0000 | [diff] [blame] | 3351 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3352 | module = PyDict_GetItem(module, py_module_name); |
| 3353 | if (module == NULL) { |
| 3354 | module = PyImport_Import(py_module_name); |
| 3355 | if (!module) |
| 3356 | return NULL; |
| 3357 | global = PyObject_GetAttr(module, py_global_name); |
| 3358 | Py_DECREF(module); |
| 3359 | } |
| 3360 | else |
| 3361 | global = PyObject_GetAttr(module, py_global_name); |
| 3362 | return global; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3363 | } |
| 3364 | |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 3365 | static Py_ssize_t |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3366 | marker(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3367 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3368 | if (self->num_marks < 1) { |
| 3369 | PyErr_SetString(UnpicklingError, "could not find MARK"); |
| 3370 | return -1; |
| 3371 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3372 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3373 | return self->marks[--self->num_marks]; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3374 | } |
| 3375 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3376 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3377 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3378 | load_none(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3379 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3380 | PDATA_APPEND(self->stack, Py_None, -1); |
| 3381 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3382 | } |
| 3383 | |
Guido van Rossum | f9ffb03 | 1999-02-04 14:54:04 +0000 | [diff] [blame] | 3384 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3385 | bad_readline(void) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3386 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3387 | PyErr_SetString(UnpicklingError, "pickle data was truncated"); |
| 3388 | return -1; |
Guido van Rossum | f9ffb03 | 1999-02-04 14:54:04 +0000 | [diff] [blame] | 3389 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3390 | |
| 3391 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3392 | load_int(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3393 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3394 | PyObject *py_int = 0; |
| 3395 | char *endptr, *s; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 3396 | Py_ssize_t len; |
| 3397 | int res = -1; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3398 | long l; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3399 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3400 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
| 3401 | if (len < 2) return bad_readline(); |
| 3402 | if (!( s=pystrndup(s,len))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3403 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3404 | errno = 0; |
| 3405 | l = strtol(s, &endptr, 0); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3406 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3407 | if (errno || (*endptr != '\n') || (endptr[1] != '\0')) { |
| 3408 | /* Hm, maybe we've got something long. Let's try reading |
| 3409 | it as a Python long object. */ |
| 3410 | errno = 0; |
| 3411 | py_int = PyLong_FromString(s, NULL, 0); |
| 3412 | if (py_int == NULL) { |
| 3413 | PyErr_SetString(PyExc_ValueError, |
| 3414 | "could not convert string to int"); |
| 3415 | goto finally; |
| 3416 | } |
| 3417 | } |
| 3418 | else { |
| 3419 | if (len == 3 && (l == 0 || l == 1)) { |
| 3420 | if (!( py_int = PyBool_FromLong(l))) goto finally; |
| 3421 | } |
| 3422 | else { |
| 3423 | if (!( py_int = PyInt_FromLong(l))) goto finally; |
| 3424 | } |
| 3425 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3426 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3427 | free(s); |
| 3428 | PDATA_PUSH(self->stack, py_int, -1); |
| 3429 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3430 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3431 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3432 | free(s); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3433 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3434 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3435 | } |
| 3436 | |
Tim Peters | 3c67d79 | 2003-02-02 17:59:11 +0000 | [diff] [blame] | 3437 | static int |
| 3438 | load_bool(Unpicklerobject *self, PyObject *boolean) |
| 3439 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3440 | assert(boolean == Py_True || boolean == Py_False); |
| 3441 | PDATA_APPEND(self->stack, boolean, -1); |
| 3442 | return 0; |
Tim Peters | 3c67d79 | 2003-02-02 17:59:11 +0000 | [diff] [blame] | 3443 | } |
| 3444 | |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 3445 | /* s contains x bytes of a little-endian integer. Return its value as a |
| 3446 | * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian |
Serhiy Storchaka | 9a118f1 | 2016-04-17 09:37:36 +0300 | [diff] [blame] | 3447 | * int, but when x is 4 it's a signed one. This is a historical source |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 3448 | * of x-platform bugs. |
| 3449 | */ |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3450 | static long |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 3451 | calc_binint(char *s, int x) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3452 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3453 | unsigned char c; |
| 3454 | int i; |
| 3455 | long l; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3456 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3457 | for (i = 0, l = 0L; i < x; i++) { |
| 3458 | c = (unsigned char)s[i]; |
| 3459 | l |= (long)c << (i * 8); |
| 3460 | } |
Tim Peters | bfa18f7 | 2001-04-10 01:54:42 +0000 | [diff] [blame] | 3461 | #if SIZEOF_LONG > 4 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3462 | /* Unlike BININT1 and BININT2, BININT (more accurately BININT4) |
| 3463 | * is signed, so on a box with longs bigger than 4 bytes we need |
| 3464 | * to extend a BININT's sign bit to the full width. |
| 3465 | */ |
| 3466 | if (x == 4 && l & (1L << 31)) |
Gregory P. Smith | 0a85728 | 2015-08-04 16:29:00 -0700 | [diff] [blame] | 3467 | l |= (~0UL) << 32; |
Tim Peters | bfa18f7 | 2001-04-10 01:54:42 +0000 | [diff] [blame] | 3468 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3469 | return l; |
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_binintx(Unpicklerobject *self, char *s, int x) |
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 | PyObject *py_int = 0; |
| 3477 | long l; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3478 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3479 | l = calc_binint(s, x); |
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 | if (!( py_int = PyInt_FromLong(l))) |
| 3482 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3483 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3484 | PDATA_PUSH(self->stack, py_int, -1); |
| 3485 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3486 | } |
| 3487 | |
| 3488 | |
| 3489 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3490 | load_binint(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3491 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3492 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3493 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3494 | if (self->read_func(self, &s, 4) < 0) |
| 3495 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3496 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3497 | return load_binintx(self, s, 4); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3498 | } |
| 3499 | |
| 3500 | |
| 3501 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3502 | load_binint1(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3503 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3504 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3505 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3506 | if (self->read_func(self, &s, 1) < 0) |
| 3507 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3508 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3509 | return load_binintx(self, s, 1); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3510 | } |
| 3511 | |
| 3512 | |
| 3513 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3514 | load_binint2(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3515 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3516 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3517 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3518 | if (self->read_func(self, &s, 2) < 0) |
| 3519 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3520 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3521 | return load_binintx(self, s, 2); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3522 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3523 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3524 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3525 | load_long(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3526 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3527 | PyObject *l = 0; |
| 3528 | char *end, *s; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 3529 | Py_ssize_t len; |
| 3530 | int res = -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3531 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3532 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
| 3533 | if (len < 2) return bad_readline(); |
| 3534 | if (!( s=pystrndup(s,len))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3535 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3536 | if (!( l = PyLong_FromString(s, &end, 0))) |
| 3537 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3538 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3539 | free(s); |
| 3540 | PDATA_PUSH(self->stack, l, -1); |
| 3541 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3542 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3543 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3544 | free(s); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3545 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3546 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3547 | } |
| 3548 | |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 3549 | /* 'size' bytes contain the # of bytes of little-endian 256's-complement |
| 3550 | * data following. |
| 3551 | */ |
| 3552 | static int |
| 3553 | load_counted_long(Unpicklerobject *self, int size) |
| 3554 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3555 | Py_ssize_t i; |
| 3556 | char *nbytes; |
| 3557 | unsigned char *pdata; |
| 3558 | PyObject *along; |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 3559 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3560 | assert(size == 1 || size == 4); |
| 3561 | i = self->read_func(self, &nbytes, size); |
| 3562 | if (i < 0) return -1; |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 3563 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3564 | size = calc_binint(nbytes, size); |
| 3565 | if (size < 0) { |
| 3566 | /* Corrupt or hostile pickle -- we never write one like |
| 3567 | * this. |
| 3568 | */ |
| 3569 | PyErr_SetString(UnpicklingError, "LONG pickle has negative " |
| 3570 | "byte count"); |
| 3571 | return -1; |
| 3572 | } |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 3573 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3574 | if (size == 0) |
| 3575 | along = PyLong_FromLong(0L); |
| 3576 | else { |
| 3577 | /* Read the raw little-endian bytes & convert. */ |
| 3578 | i = self->read_func(self, (char **)&pdata, size); |
| 3579 | if (i < 0) return -1; |
| 3580 | along = _PyLong_FromByteArray(pdata, (size_t)size, |
| 3581 | 1 /* little endian */, 1 /* signed */); |
| 3582 | } |
| 3583 | if (along == NULL) |
| 3584 | return -1; |
| 3585 | PDATA_PUSH(self->stack, along, -1); |
| 3586 | return 0; |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 3587 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3588 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3589 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3590 | load_float(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3591 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3592 | PyObject *py_float = 0; |
| 3593 | char *endptr, *s; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 3594 | Py_ssize_t len; |
| 3595 | int res = -1; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3596 | double d; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3597 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3598 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
| 3599 | if (len < 2) return bad_readline(); |
| 3600 | if (!( s=pystrndup(s,len))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3601 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3602 | d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3603 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3604 | if (d == -1.0 && PyErr_Occurred()) { |
| 3605 | goto finally; |
| 3606 | } else if ((endptr[0] != '\n') || (endptr[1] != '\0')) { |
| 3607 | PyErr_SetString(PyExc_ValueError, |
| 3608 | "could not convert string to float"); |
| 3609 | goto finally; |
| 3610 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3611 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3612 | if (!( py_float = PyFloat_FromDouble(d))) |
| 3613 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3614 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3615 | free(s); |
| 3616 | PDATA_PUSH(self->stack, py_float, -1); |
| 3617 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3618 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3619 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3620 | free(s); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3621 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3622 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3623 | } |
| 3624 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3625 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3626 | load_binfloat(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3627 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3628 | PyObject *py_float; |
| 3629 | double x; |
| 3630 | char *p; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3631 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3632 | if (self->read_func(self, &p, 8) < 0) |
| 3633 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3634 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3635 | x = _PyFloat_Unpack8((unsigned char *)p, 0); |
| 3636 | if (x == -1.0 && PyErr_Occurred()) |
| 3637 | return -1; |
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 | py_float = PyFloat_FromDouble(x); |
| 3640 | if (py_float == NULL) |
| 3641 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3642 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3643 | PDATA_PUSH(self->stack, py_float, -1); |
| 3644 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3645 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3646 | |
| 3647 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3648 | load_string(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3649 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3650 | PyObject *str = 0; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 3651 | Py_ssize_t len; |
| 3652 | int res = -1; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3653 | char *s, *p; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3654 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3655 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
| 3656 | if (len < 2) return bad_readline(); |
| 3657 | if (!( s=pystrndup(s,len))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3658 | |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 3659 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3660 | /* Strip outermost quotes */ |
Antoine Pitrou | be92971 | 2013-04-15 21:35:25 +0200 | [diff] [blame] | 3661 | while (len > 0 && s[len-1] <= ' ') |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3662 | len--; |
Antoine Pitrou | be92971 | 2013-04-15 21:35:25 +0200 | [diff] [blame] | 3663 | if (len > 1 && s[0]=='"' && s[len-1]=='"') { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3664 | s[len-1] = '\0'; |
| 3665 | p = s + 1 ; |
| 3666 | len -= 2; |
Antoine Pitrou | be92971 | 2013-04-15 21:35:25 +0200 | [diff] [blame] | 3667 | } |
| 3668 | else if (len > 1 && s[0]=='\'' && s[len-1]=='\'') { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3669 | s[len-1] = '\0'; |
| 3670 | p = s + 1 ; |
| 3671 | len -= 2; |
Antoine Pitrou | be92971 | 2013-04-15 21:35:25 +0200 | [diff] [blame] | 3672 | } |
| 3673 | else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3674 | goto insecure; |
| 3675 | /********************************************/ |
Guido van Rossum | 9716aaa | 1997-12-08 15:15:16 +0000 | [diff] [blame] | 3676 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3677 | str = PyString_DecodeEscape(p, len, NULL, 0, NULL); |
| 3678 | free(s); |
| 3679 | if (str) { |
| 3680 | PDATA_PUSH(self->stack, str, -1); |
| 3681 | res = 0; |
| 3682 | } |
| 3683 | return res; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3684 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3685 | insecure: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3686 | free(s); |
| 3687 | PyErr_SetString(PyExc_ValueError,"insecure string pickle"); |
| 3688 | return -1; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3689 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3690 | |
| 3691 | |
| 3692 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3693 | load_binstring(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3694 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3695 | PyObject *py_string = 0; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 3696 | Py_ssize_t l; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3697 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3698 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3699 | if (self->read_func(self, &s, 4) < 0) 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 | l = calc_binint(s, 4); |
| 3702 | if (l < 0) { |
| 3703 | /* Corrupt or hostile pickle -- we never write one like |
| 3704 | * this. |
| 3705 | */ |
| 3706 | PyErr_SetString(UnpicklingError, |
| 3707 | "BINSTRING pickle has negative byte count"); |
| 3708 | return -1; |
| 3709 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3710 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3711 | if (self->read_func(self, &s, l) < 0) |
| 3712 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3713 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3714 | if (!( py_string = PyString_FromStringAndSize(s, l))) |
| 3715 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3716 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3717 | PDATA_PUSH(self->stack, py_string, -1); |
| 3718 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3719 | } |
| 3720 | |
| 3721 | |
| 3722 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3723 | load_short_binstring(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3724 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3725 | PyObject *py_string = 0; |
| 3726 | unsigned char l; |
| 3727 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3728 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3729 | if (self->read_func(self, &s, 1) < 0) |
| 3730 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3731 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3732 | l = (unsigned char)s[0]; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3733 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3734 | if (self->read_func(self, &s, l) < 0) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3735 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3736 | if (!( py_string = PyString_FromStringAndSize(s, l))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3737 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3738 | PDATA_PUSH(self->stack, py_string, -1); |
| 3739 | return 0; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3740 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3741 | |
| 3742 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3743 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3744 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3745 | load_unicode(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3746 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3747 | PyObject *str = 0; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 3748 | Py_ssize_t len; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3749 | char *s; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3750 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3751 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
| 3752 | if (len < 1) return bad_readline(); |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3753 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3754 | if (!( str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL))) |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 3755 | 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 | PDATA_PUSH(self->stack, str, -1); |
| 3758 | return 0; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3759 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3760 | #endif |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3761 | |
| 3762 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3763 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3764 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3765 | load_binunicode(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3766 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3767 | PyObject *unicode; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 3768 | Py_ssize_t l; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3769 | char *s; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3770 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3771 | if (self->read_func(self, &s, 4) < 0) 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 | l = calc_binint(s, 4); |
| 3774 | if (l < 0) { |
| 3775 | /* Corrupt or hostile pickle -- we never write one like |
| 3776 | * this. |
| 3777 | */ |
| 3778 | PyErr_SetString(UnpicklingError, |
| 3779 | "BINUNICODE pickle has negative byte count"); |
| 3780 | return -1; |
| 3781 | } |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3782 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3783 | if (self->read_func(self, &s, l) < 0) |
| 3784 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3785 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3786 | if (!( unicode = PyUnicode_DecodeUTF8(s, l, NULL))) |
| 3787 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3788 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3789 | PDATA_PUSH(self->stack, unicode, -1); |
| 3790 | return 0; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3791 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3792 | #endif |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3793 | |
| 3794 | |
| 3795 | static int |
Serhiy Storchaka | 80767a3 | 2015-11-25 15:07:49 +0200 | [diff] [blame] | 3796 | load_counted_tuple(Unpicklerobject *self, int len) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3797 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3798 | PyObject *tup; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3799 | |
Serhiy Storchaka | 80767a3 | 2015-11-25 15:07:49 +0200 | [diff] [blame] | 3800 | if (self->stack->length < len) |
| 3801 | return stackUnderflow(); |
| 3802 | |
| 3803 | if (!(tup = Pdata_popTuple(self->stack, self->stack->length - len))) |
| 3804 | return -1; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3805 | PDATA_PUSH(self->stack, tup, -1); |
| 3806 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3807 | } |
| 3808 | |
| 3809 | static int |
Serhiy Storchaka | 80767a3 | 2015-11-25 15:07:49 +0200 | [diff] [blame] | 3810 | load_tuple(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3811 | { |
Serhiy Storchaka | 80767a3 | 2015-11-25 15:07:49 +0200 | [diff] [blame] | 3812 | Py_ssize_t i; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3813 | |
Serhiy Storchaka | 80767a3 | 2015-11-25 15:07:49 +0200 | [diff] [blame] | 3814 | if ((i = marker(self)) < 0) return -1; |
| 3815 | return load_counted_tuple(self, self->stack->length - i); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3816 | } |
| 3817 | |
| 3818 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3819 | load_empty_list(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3820 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3821 | PyObject *list; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3822 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3823 | if (!( list=PyList_New(0))) return -1; |
| 3824 | PDATA_PUSH(self->stack, list, -1); |
| 3825 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3826 | } |
| 3827 | |
| 3828 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3829 | load_empty_dict(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3830 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3831 | PyObject *dict; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3832 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3833 | if (!( dict=PyDict_New())) return -1; |
| 3834 | PDATA_PUSH(self->stack, dict, -1); |
| 3835 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3836 | } |
| 3837 | |
| 3838 | |
| 3839 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3840 | load_list(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3841 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3842 | PyObject *list = 0; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 3843 | Py_ssize_t i; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3844 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3845 | if ((i = marker(self)) < 0) return -1; |
| 3846 | if (!( list=Pdata_popList(self->stack, i))) return -1; |
| 3847 | PDATA_PUSH(self->stack, list, -1); |
| 3848 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3849 | } |
| 3850 | |
| 3851 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3852 | load_dict(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3853 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3854 | PyObject *dict, *key, *value; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 3855 | Py_ssize_t i, j, k; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3856 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3857 | if ((i = marker(self)) < 0) return -1; |
| 3858 | j=self->stack->length; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3859 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3860 | if (!( dict = PyDict_New())) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3861 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3862 | for (k = i+1; k < j; k += 2) { |
| 3863 | key =self->stack->data[k-1]; |
| 3864 | value=self->stack->data[k ]; |
| 3865 | if (PyDict_SetItem(dict, key, value) < 0) { |
| 3866 | Py_DECREF(dict); |
| 3867 | return -1; |
| 3868 | } |
| 3869 | } |
| 3870 | Pdata_clear(self->stack, i); |
| 3871 | PDATA_PUSH(self->stack, dict, -1); |
| 3872 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3873 | } |
| 3874 | |
| 3875 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3876 | Instance_New(PyObject *cls, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3877 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3878 | PyObject *r = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3879 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3880 | if (PyClass_Check(cls)) { |
| 3881 | int l; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3882 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3883 | if ((l=PyObject_Size(args)) < 0) goto err; |
| 3884 | if (!( l )) { |
| 3885 | PyObject *__getinitargs__; |
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 | __getinitargs__ = PyObject_GetAttr(cls, |
| 3888 | __getinitargs___str); |
| 3889 | if (!__getinitargs__) { |
| 3890 | /* We have a class with no __getinitargs__, |
| 3891 | so bypass usual construction */ |
| 3892 | PyObject *inst; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3893 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3894 | PyErr_Clear(); |
| 3895 | if (!( inst=PyInstance_NewRaw(cls, NULL))) |
| 3896 | goto err; |
| 3897 | return inst; |
| 3898 | } |
| 3899 | Py_DECREF(__getinitargs__); |
| 3900 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3901 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3902 | if ((r=PyInstance_New(cls, args, NULL))) return r; |
| 3903 | else goto err; |
| 3904 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3905 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3906 | if ((r=PyObject_CallObject(cls, args))) return r; |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 3907 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3908 | err: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3909 | { |
| 3910 | PyObject *tp, *v, *tb, *tmp_value; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3911 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3912 | PyErr_Fetch(&tp, &v, &tb); |
| 3913 | tmp_value = v; |
| 3914 | /* NULL occurs when there was a KeyboardInterrupt */ |
| 3915 | if (tmp_value == NULL) |
| 3916 | tmp_value = Py_None; |
| 3917 | if ((r = PyTuple_Pack(3, tmp_value, cls, args))) { |
| 3918 | Py_XDECREF(v); |
| 3919 | v=r; |
| 3920 | } |
| 3921 | PyErr_Restore(tp,v,tb); |
| 3922 | } |
| 3923 | return NULL; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3924 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3925 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3926 | |
| 3927 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3928 | load_obj(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3929 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3930 | PyObject *class, *tup, *obj=0; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 3931 | Py_ssize_t i; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3932 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3933 | if ((i = marker(self)) < 0) return -1; |
Serhiy Storchaka | 5c13766 | 2015-11-23 15:20:43 +0200 | [diff] [blame] | 3934 | |
| 3935 | if (self->stack->length - i < 1) |
| 3936 | return stackUnderflow(); |
| 3937 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3938 | if (!( tup=Pdata_popTuple(self->stack, i+1))) return -1; |
| 3939 | PDATA_POP(self->stack, class); |
| 3940 | if (class) { |
| 3941 | obj = Instance_New(class, tup); |
| 3942 | Py_DECREF(class); |
| 3943 | } |
| 3944 | Py_DECREF(tup); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3945 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3946 | if (! obj) return -1; |
| 3947 | PDATA_PUSH(self->stack, obj, -1); |
| 3948 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3949 | } |
| 3950 | |
| 3951 | |
| 3952 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3953 | load_inst(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3954 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3955 | PyObject *tup, *class=0, *obj=0, *module_name, *class_name; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 3956 | Py_ssize_t i, len; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3957 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3958 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3959 | if ((i = marker(self)) < 0) return -1; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3960 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3961 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
| 3962 | if (len < 2) return bad_readline(); |
| 3963 | module_name = PyString_FromStringAndSize(s, len - 1); |
| 3964 | if (!module_name) return -1; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3965 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3966 | if ((len = self->readline_func(self, &s)) >= 0) { |
Serhiy Storchaka | 048e107 | 2015-12-01 00:32:49 +0200 | [diff] [blame] | 3967 | if (len < 2) { |
| 3968 | Py_DECREF(module_name); |
| 3969 | return bad_readline(); |
| 3970 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3971 | if ((class_name = PyString_FromStringAndSize(s, len - 1))) { |
| 3972 | class = find_class(module_name, class_name, |
| 3973 | self->find_class); |
| 3974 | Py_DECREF(class_name); |
| 3975 | } |
| 3976 | } |
| 3977 | Py_DECREF(module_name); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3978 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3979 | if (! class) return -1; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3980 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3981 | if ((tup=Pdata_popTuple(self->stack, i))) { |
| 3982 | obj = Instance_New(class, tup); |
| 3983 | Py_DECREF(tup); |
| 3984 | } |
| 3985 | Py_DECREF(class); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3986 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3987 | if (! obj) return -1; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3988 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3989 | PDATA_PUSH(self->stack, obj, -1); |
| 3990 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3991 | } |
| 3992 | |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 3993 | static int |
| 3994 | load_newobj(Unpicklerobject *self) |
| 3995 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3996 | PyObject *args = NULL; |
| 3997 | PyObject *clsraw = NULL; |
| 3998 | PyTypeObject *cls; /* clsraw cast to its true type */ |
| 3999 | PyObject *obj; |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 4000 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4001 | /* Stack is ... cls argtuple, and we want to call |
| 4002 | * cls.__new__(cls, *argtuple). |
| 4003 | */ |
| 4004 | PDATA_POP(self->stack, args); |
| 4005 | if (args == NULL) goto Fail; |
| 4006 | if (! PyTuple_Check(args)) { |
| 4007 | PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg " |
| 4008 | "tuple."); |
| 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 | PDATA_POP(self->stack, clsraw); |
| 4013 | cls = (PyTypeObject *)clsraw; |
| 4014 | if (cls == NULL) goto Fail; |
| 4015 | if (! PyType_Check(cls)) { |
| 4016 | PyErr_SetString(UnpicklingError, "NEWOBJ class argument " |
| 4017 | "isn't a type object"); |
| 4018 | goto Fail; |
| 4019 | } |
| 4020 | if (cls->tp_new == NULL) { |
| 4021 | PyErr_SetString(UnpicklingError, "NEWOBJ class argument " |
| 4022 | "has NULL tp_new"); |
| 4023 | goto Fail; |
| 4024 | } |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 4025 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4026 | /* Call __new__. */ |
| 4027 | obj = cls->tp_new(cls, args, NULL); |
| 4028 | if (obj == NULL) goto Fail; |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 4029 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4030 | Py_DECREF(args); |
| 4031 | Py_DECREF(clsraw); |
| 4032 | PDATA_PUSH(self->stack, obj, -1); |
| 4033 | return 0; |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 4034 | |
| 4035 | Fail: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4036 | Py_XDECREF(args); |
| 4037 | Py_XDECREF(clsraw); |
| 4038 | return -1; |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 4039 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4040 | |
| 4041 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4042 | load_global(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4043 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4044 | PyObject *class = 0, *module_name = 0, *class_name = 0; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4045 | Py_ssize_t len; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4046 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4047 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4048 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
| 4049 | if (len < 2) return bad_readline(); |
| 4050 | module_name = PyString_FromStringAndSize(s, len - 1); |
| 4051 | if (!module_name) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4052 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4053 | if ((len = self->readline_func(self, &s)) >= 0) { |
| 4054 | if (len < 2) { |
| 4055 | Py_DECREF(module_name); |
| 4056 | return bad_readline(); |
| 4057 | } |
| 4058 | if ((class_name = PyString_FromStringAndSize(s, len - 1))) { |
| 4059 | class = find_class(module_name, class_name, |
| 4060 | self->find_class); |
| 4061 | Py_DECREF(class_name); |
| 4062 | } |
| 4063 | } |
| 4064 | Py_DECREF(module_name); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4065 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4066 | if (! class) return -1; |
| 4067 | PDATA_PUSH(self->stack, class, -1); |
| 4068 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4069 | } |
| 4070 | |
| 4071 | |
| 4072 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4073 | load_persid(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4074 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4075 | PyObject *pid = 0; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4076 | Py_ssize_t len; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4077 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4078 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4079 | if (self->pers_func) { |
| 4080 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
| 4081 | if (len < 2) return bad_readline(); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4082 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4083 | pid = PyString_FromStringAndSize(s, len - 1); |
| 4084 | if (!pid) return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4085 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4086 | if (PyList_Check(self->pers_func)) { |
| 4087 | if (PyList_Append(self->pers_func, pid) < 0) { |
| 4088 | Py_DECREF(pid); |
| 4089 | return -1; |
| 4090 | } |
| 4091 | } |
| 4092 | else { |
| 4093 | ARG_TUP(self, pid); |
| 4094 | if (self->arg) { |
| 4095 | pid = PyObject_Call(self->pers_func, self->arg, |
| 4096 | NULL); |
| 4097 | FREE_ARG_TUP(self); |
| 4098 | } |
| 4099 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4100 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4101 | if (! pid) return -1; |
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 | PDATA_PUSH(self->stack, pid, -1); |
| 4104 | return 0; |
| 4105 | } |
| 4106 | else { |
| 4107 | PyErr_SetString(UnpicklingError, |
| 4108 | "A load persistent id instruction was encountered,\n" |
| 4109 | "but no persistent_load function was specified."); |
| 4110 | return -1; |
| 4111 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4112 | } |
| 4113 | |
| 4114 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4115 | load_binpersid(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4116 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4117 | PyObject *pid = 0; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4118 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4119 | if (self->pers_func) { |
| 4120 | PDATA_POP(self->stack, pid); |
| 4121 | if (! pid) return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4122 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4123 | if (PyList_Check(self->pers_func)) { |
| 4124 | if (PyList_Append(self->pers_func, pid) < 0) { |
| 4125 | Py_DECREF(pid); |
| 4126 | return -1; |
| 4127 | } |
| 4128 | } |
| 4129 | else { |
| 4130 | ARG_TUP(self, pid); |
| 4131 | if (self->arg) { |
| 4132 | pid = PyObject_Call(self->pers_func, self->arg, |
| 4133 | NULL); |
| 4134 | FREE_ARG_TUP(self); |
| 4135 | } |
| 4136 | if (! pid) return -1; |
| 4137 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4138 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4139 | PDATA_PUSH(self->stack, pid, -1); |
| 4140 | return 0; |
| 4141 | } |
| 4142 | else { |
| 4143 | PyErr_SetString(UnpicklingError, |
| 4144 | "A load persistent id instruction was encountered,\n" |
| 4145 | "but no persistent_load function was specified."); |
| 4146 | return -1; |
| 4147 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4148 | } |
| 4149 | |
| 4150 | |
| 4151 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4152 | load_pop(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4153 | { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4154 | Py_ssize_t len = self->stack->length; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4155 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4156 | /* Note that we split the (pickle.py) stack into two stacks, |
| 4157 | an object stack and a mark stack. We have to be clever and |
| 4158 | pop the right one. We do this by looking at the top of the |
| 4159 | mark stack first, and only signalling a stack underflow if |
| 4160 | the object stack is empty and the mark stack doesn't match |
| 4161 | our expectations. |
| 4162 | */ |
| 4163 | if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) { |
| 4164 | self->num_marks--; |
| 4165 | } else if (len > 0) { |
| 4166 | len--; |
| 4167 | Py_DECREF(self->stack->data[len]); |
| 4168 | self->stack->length = len; |
| 4169 | } else { |
| 4170 | return stackUnderflow(); |
| 4171 | } |
| 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_pop_mark(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4178 | { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4179 | Py_ssize_t i; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4180 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4181 | if ((i = marker(self)) < 0) |
| 4182 | return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4183 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4184 | Pdata_clear(self->stack, i); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4185 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 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_dup(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 *last; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4194 | Py_ssize_t len; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4195 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4196 | if ((len = self->stack->length) <= 0) return stackUnderflow(); |
| 4197 | last=self->stack->data[len-1]; |
| 4198 | Py_INCREF(last); |
| 4199 | PDATA_PUSH(self->stack, last, -1); |
| 4200 | return 0; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4201 | } |
| 4202 | |
| 4203 | |
| 4204 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4205 | load_get(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4206 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4207 | PyObject *py_str = 0, *value = 0; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4208 | Py_ssize_t len; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4209 | char *s; |
| 4210 | int rc; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4211 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4212 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
| 4213 | if (len < 2) return bad_readline(); |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4214 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4215 | if (!( py_str = PyString_FromStringAndSize(s, len - 1))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4216 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4217 | value = PyDict_GetItem(self->memo, py_str); |
| 4218 | if (! value) { |
| 4219 | PyErr_SetObject(BadPickleGet, py_str); |
| 4220 | rc = -1; |
| 4221 | } |
| 4222 | else { |
| 4223 | PDATA_APPEND(self->stack, value, -1); |
| 4224 | rc = 0; |
| 4225 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4226 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4227 | Py_DECREF(py_str); |
| 4228 | return rc; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4229 | } |
| 4230 | |
| 4231 | |
| 4232 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4233 | load_binget(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4234 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4235 | PyObject *py_key = 0, *value = 0; |
| 4236 | unsigned char key; |
| 4237 | char *s; |
| 4238 | int rc; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4239 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4240 | if (self->read_func(self, &s, 1) < 0) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4241 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4242 | key = (unsigned char)s[0]; |
| 4243 | if (!( py_key = PyInt_FromLong((long)key))) return -1; |
Guido van Rossum | ea2b715 | 2000-05-09 18:14:50 +0000 | [diff] [blame] | 4244 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4245 | value = PyDict_GetItem(self->memo, py_key); |
| 4246 | if (! value) { |
| 4247 | PyErr_SetObject(BadPickleGet, py_key); |
| 4248 | rc = -1; |
| 4249 | } |
| 4250 | else { |
| 4251 | PDATA_APPEND(self->stack, value, -1); |
| 4252 | rc = 0; |
| 4253 | } |
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 | Py_DECREF(py_key); |
| 4256 | return rc; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4257 | } |
| 4258 | |
| 4259 | |
| 4260 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4261 | load_long_binget(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4262 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4263 | PyObject *py_key = 0, *value = 0; |
| 4264 | unsigned char c; |
| 4265 | char *s; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4266 | Py_ssize_t key; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4267 | int rc; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4268 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4269 | if (self->read_func(self, &s, 4) < 0) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4270 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4271 | c = (unsigned char)s[0]; |
| 4272 | key = (long)c; |
| 4273 | c = (unsigned char)s[1]; |
| 4274 | key |= (long)c << 8; |
| 4275 | c = (unsigned char)s[2]; |
| 4276 | key |= (long)c << 16; |
| 4277 | c = (unsigned char)s[3]; |
| 4278 | key |= (long)c << 24; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4279 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4280 | if (!( py_key = PyInt_FromLong((long)key))) return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4281 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4282 | value = PyDict_GetItem(self->memo, py_key); |
| 4283 | if (! value) { |
| 4284 | PyErr_SetObject(BadPickleGet, py_key); |
| 4285 | rc = -1; |
| 4286 | } |
| 4287 | else { |
| 4288 | PDATA_APPEND(self->stack, value, -1); |
| 4289 | rc = 0; |
| 4290 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4291 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4292 | Py_DECREF(py_key); |
| 4293 | return rc; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4294 | } |
| 4295 | |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 4296 | /* Push an object from the extension registry (EXT[124]). nbytes is |
| 4297 | * the number of bytes following the opcode, holding the index (code) value. |
| 4298 | */ |
| 4299 | static int |
| 4300 | load_extension(Unpicklerobject *self, int nbytes) |
| 4301 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4302 | char *codebytes; /* the nbytes bytes after the opcode */ |
| 4303 | long code; /* calc_binint returns long */ |
| 4304 | PyObject *py_code; /* code as a Python int */ |
| 4305 | PyObject *obj; /* the object to push */ |
| 4306 | PyObject *pair; /* (module_name, class_name) */ |
| 4307 | PyObject *module_name, *class_name; |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 4308 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4309 | assert(nbytes == 1 || nbytes == 2 || nbytes == 4); |
| 4310 | if (self->read_func(self, &codebytes, nbytes) < 0) return -1; |
| 4311 | code = calc_binint(codebytes, nbytes); |
| 4312 | if (code <= 0) { /* note that 0 is forbidden */ |
| 4313 | /* Corrupt or hostile pickle. */ |
| 4314 | PyErr_SetString(UnpicklingError, "EXT specifies code <= 0"); |
| 4315 | return -1; |
| 4316 | } |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 4317 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4318 | /* Look for the code in the cache. */ |
| 4319 | py_code = PyInt_FromLong(code); |
| 4320 | if (py_code == NULL) return -1; |
| 4321 | obj = PyDict_GetItem(extension_cache, py_code); |
| 4322 | if (obj != NULL) { |
| 4323 | /* Bingo. */ |
| 4324 | Py_DECREF(py_code); |
| 4325 | PDATA_APPEND(self->stack, obj, -1); |
| 4326 | return 0; |
| 4327 | } |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 4328 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4329 | /* Look up the (module_name, class_name) pair. */ |
| 4330 | pair = PyDict_GetItem(inverted_registry, py_code); |
| 4331 | if (pair == NULL) { |
| 4332 | Py_DECREF(py_code); |
| 4333 | PyErr_Format(PyExc_ValueError, "unregistered extension " |
| 4334 | "code %ld", code); |
| 4335 | return -1; |
| 4336 | } |
| 4337 | /* Since the extension registry is manipulable via Python code, |
| 4338 | * confirm that pair is really a 2-tuple of strings. |
| 4339 | */ |
| 4340 | if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 || |
| 4341 | !PyString_Check(module_name = PyTuple_GET_ITEM(pair, 0)) || |
| 4342 | !PyString_Check(class_name = PyTuple_GET_ITEM(pair, 1))) { |
| 4343 | Py_DECREF(py_code); |
| 4344 | PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] " |
| 4345 | "isn't a 2-tuple of strings", code); |
| 4346 | return -1; |
| 4347 | } |
| 4348 | /* Load the object. */ |
| 4349 | obj = find_class(module_name, class_name, self->find_class); |
| 4350 | if (obj == NULL) { |
| 4351 | Py_DECREF(py_code); |
| 4352 | return -1; |
| 4353 | } |
| 4354 | /* Cache code -> obj. */ |
| 4355 | code = PyDict_SetItem(extension_cache, py_code, obj); |
| 4356 | Py_DECREF(py_code); |
| 4357 | if (code < 0) { |
| 4358 | Py_DECREF(obj); |
| 4359 | return -1; |
| 4360 | } |
| 4361 | PDATA_PUSH(self->stack, obj, -1); |
| 4362 | return 0; |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 4363 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4364 | |
| 4365 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4366 | load_put(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4367 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4368 | PyObject *py_str = 0, *value = 0; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4369 | Py_ssize_t len, l; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4370 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4371 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4372 | if ((l = self->readline_func(self, &s)) < 0) return -1; |
| 4373 | if (l < 2) return bad_readline(); |
| 4374 | if (!( len=self->stack->length )) return stackUnderflow(); |
| 4375 | if (!( py_str = PyString_FromStringAndSize(s, l - 1))) return -1; |
| 4376 | value=self->stack->data[len-1]; |
| 4377 | l=PyDict_SetItem(self->memo, py_str, value); |
| 4378 | Py_DECREF(py_str); |
| 4379 | return l; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4380 | } |
| 4381 | |
| 4382 | |
| 4383 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4384 | load_binput(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4385 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4386 | PyObject *py_key = 0, *value = 0; |
| 4387 | unsigned char key; |
| 4388 | char *s; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4389 | Py_ssize_t len; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4390 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4391 | if (self->read_func(self, &s, 1) < 0) return -1; |
| 4392 | if (!( (len=self->stack->length) > 0 )) return stackUnderflow(); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4393 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4394 | key = (unsigned char)s[0]; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 4395 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4396 | if (!( py_key = PyInt_FromLong((long)key))) return -1; |
| 4397 | value=self->stack->data[len-1]; |
| 4398 | len=PyDict_SetItem(self->memo, py_key, value); |
| 4399 | Py_DECREF(py_key); |
| 4400 | return len; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4401 | } |
| 4402 | |
| 4403 | |
| 4404 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4405 | load_long_binput(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4406 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4407 | PyObject *py_key = 0, *value = 0; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4408 | Py_ssize_t key; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4409 | unsigned char c; |
| 4410 | char *s; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4411 | Py_ssize_t len; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4412 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4413 | if (self->read_func(self, &s, 4) < 0) return -1; |
| 4414 | if (!( len=self->stack->length )) return stackUnderflow(); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4415 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4416 | c = (unsigned char)s[0]; |
| 4417 | key = (long)c; |
| 4418 | c = (unsigned char)s[1]; |
| 4419 | key |= (long)c << 8; |
| 4420 | c = (unsigned char)s[2]; |
| 4421 | key |= (long)c << 16; |
| 4422 | c = (unsigned char)s[3]; |
| 4423 | key |= (long)c << 24; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4424 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4425 | if (!( py_key = PyInt_FromLong(key))) return -1; |
| 4426 | value=self->stack->data[len-1]; |
| 4427 | len=PyDict_SetItem(self->memo, py_key, value); |
| 4428 | Py_DECREF(py_key); |
| 4429 | return len; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4430 | } |
| 4431 | |
| 4432 | |
| 4433 | static int |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4434 | do_append(Unpicklerobject *self, Py_ssize_t x) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4435 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4436 | PyObject *value = 0, *list = 0, *append_method = 0; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4437 | Py_ssize_t len, i; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4438 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4439 | len=self->stack->length; |
| 4440 | if (!( len >= x && x > 0 )) return stackUnderflow(); |
| 4441 | /* nothing to do */ |
| 4442 | if (len==x) return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4443 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4444 | list=self->stack->data[x-1]; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4445 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4446 | if (PyList_Check(list)) { |
| 4447 | PyObject *slice; |
| 4448 | int list_len; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4449 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4450 | slice=Pdata_popList(self->stack, x); |
| 4451 | if (! slice) return -1; |
| 4452 | list_len = PyList_GET_SIZE(list); |
| 4453 | i=PyList_SetSlice(list, list_len, list_len, slice); |
| 4454 | Py_DECREF(slice); |
| 4455 | return i; |
| 4456 | } |
| 4457 | else { |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4458 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4459 | if (!( append_method = PyObject_GetAttr(list, append_str))) |
| 4460 | return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4461 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4462 | for (i = x; i < len; i++) { |
| 4463 | PyObject *junk; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4464 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4465 | value=self->stack->data[i]; |
| 4466 | junk=0; |
| 4467 | ARG_TUP(self, value); |
| 4468 | if (self->arg) { |
| 4469 | junk = PyObject_Call(append_method, self->arg, |
| 4470 | NULL); |
| 4471 | FREE_ARG_TUP(self); |
| 4472 | } |
| 4473 | if (! junk) { |
| 4474 | Pdata_clear(self->stack, i+1); |
| 4475 | self->stack->length=x; |
| 4476 | Py_DECREF(append_method); |
| 4477 | return -1; |
| 4478 | } |
| 4479 | Py_DECREF(junk); |
| 4480 | } |
| 4481 | self->stack->length=x; |
| 4482 | Py_DECREF(append_method); |
| 4483 | } |
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 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4486 | } |
| 4487 | |
| 4488 | |
| 4489 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4490 | load_append(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4491 | { |
Serhiy Storchaka | 5c13766 | 2015-11-23 15:20:43 +0200 | [diff] [blame] | 4492 | if (self->stack->length - 1 <= 0) |
| 4493 | return stackUnderflow(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4494 | return do_append(self, self->stack->length - 1); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4495 | } |
| 4496 | |
| 4497 | |
| 4498 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4499 | load_appends(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4500 | { |
Serhiy Storchaka | 5c13766 | 2015-11-23 15:20:43 +0200 | [diff] [blame] | 4501 | Py_ssize_t i = marker(self); |
| 4502 | if (i < 0) |
| 4503 | return -1; |
| 4504 | return do_append(self, i); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4505 | } |
| 4506 | |
| 4507 | |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4508 | static Py_ssize_t |
| 4509 | do_setitems(Unpicklerobject *self, Py_ssize_t 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 | PyObject *value = 0, *key = 0, *dict = 0; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4512 | Py_ssize_t len, i, r=0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4513 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4514 | if (!( (len=self->stack->length) >= x |
| 4515 | && x > 0 )) return stackUnderflow(); |
Serhiy Storchaka | 5c13766 | 2015-11-23 15:20:43 +0200 | [diff] [blame] | 4516 | if (len == x) /* nothing to do */ |
| 4517 | return 0; |
| 4518 | if ((len - x) % 2 != 0) { |
| 4519 | /* Currupt or hostile pickle -- we never write one like this. */ |
| 4520 | PyErr_SetString(UnpicklingError, |
| 4521 | "odd number of items for SETITEMS"); |
| 4522 | return -1; |
| 4523 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4524 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4525 | dict=self->stack->data[x-1]; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4526 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4527 | for (i = x+1; i < len; i += 2) { |
| 4528 | key =self->stack->data[i-1]; |
| 4529 | value=self->stack->data[i ]; |
| 4530 | if (PyObject_SetItem(dict, key, value) < 0) { |
| 4531 | r=-1; |
| 4532 | break; |
| 4533 | } |
| 4534 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4535 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4536 | Pdata_clear(self->stack, x); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4537 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4538 | return r; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4539 | } |
| 4540 | |
| 4541 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4542 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4543 | load_setitem(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4544 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4545 | return do_setitems(self, self->stack->length - 2); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4546 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4547 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4548 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4549 | load_setitems(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4550 | { |
Serhiy Storchaka | 5c13766 | 2015-11-23 15:20:43 +0200 | [diff] [blame] | 4551 | Py_ssize_t i = marker(self); |
| 4552 | if (i < 0) |
| 4553 | return -1; |
| 4554 | return do_setitems(self, i); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4555 | } |
| 4556 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4557 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4558 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4559 | load_build(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4560 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4561 | PyObject *state, *inst, *slotstate; |
| 4562 | PyObject *__setstate__; |
| 4563 | PyObject *d_key, *d_value; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4564 | int res = -1; |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4565 | Py_ssize_t i; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4566 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4567 | /* Stack is ... instance, state. We want to leave instance at |
| 4568 | * the stack top, possibly mutated via instance.__setstate__(state). |
| 4569 | */ |
| 4570 | if (self->stack->length < 2) |
| 4571 | return stackUnderflow(); |
| 4572 | PDATA_POP(self->stack, state); |
| 4573 | if (state == NULL) |
| 4574 | return -1; |
| 4575 | inst = self->stack->data[self->stack->length - 1]; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4576 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4577 | __setstate__ = PyObject_GetAttr(inst, __setstate___str); |
| 4578 | if (__setstate__ != NULL) { |
| 4579 | PyObject *junk = NULL; |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 4580 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4581 | /* The explicit __setstate__ is responsible for everything. */ |
| 4582 | ARG_TUP(self, state); |
| 4583 | if (self->arg) { |
| 4584 | junk = PyObject_Call(__setstate__, self->arg, NULL); |
| 4585 | FREE_ARG_TUP(self); |
| 4586 | } |
| 4587 | Py_DECREF(__setstate__); |
| 4588 | if (junk == NULL) |
| 4589 | return -1; |
| 4590 | Py_DECREF(junk); |
| 4591 | return 0; |
| 4592 | } |
| 4593 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 4594 | return -1; |
| 4595 | PyErr_Clear(); |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 4596 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4597 | /* A default __setstate__. First see whether state embeds a |
| 4598 | * slot state dict too (a proto 2 addition). |
| 4599 | */ |
| 4600 | if (PyTuple_Check(state) && PyTuple_Size(state) == 2) { |
| 4601 | PyObject *temp = state; |
| 4602 | state = PyTuple_GET_ITEM(temp, 0); |
| 4603 | slotstate = PyTuple_GET_ITEM(temp, 1); |
| 4604 | Py_INCREF(state); |
| 4605 | Py_INCREF(slotstate); |
| 4606 | Py_DECREF(temp); |
| 4607 | } |
| 4608 | else |
| 4609 | slotstate = NULL; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4610 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4611 | /* Set inst.__dict__ from the state dict (if any). */ |
| 4612 | if (state != Py_None) { |
| 4613 | PyObject *dict; |
| 4614 | if (! PyDict_Check(state)) { |
| 4615 | PyErr_SetString(UnpicklingError, "state is not a " |
| 4616 | "dictionary"); |
| 4617 | goto finally; |
| 4618 | } |
| 4619 | dict = PyObject_GetAttr(inst, __dict___str); |
| 4620 | if (dict == NULL) |
| 4621 | goto finally; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4622 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4623 | i = 0; |
| 4624 | while (PyDict_Next(state, &i, &d_key, &d_value)) { |
| 4625 | /* normally the keys for instance attributes are |
| 4626 | interned. we should try to do that here. */ |
| 4627 | Py_INCREF(d_key); |
| 4628 | if (PyString_CheckExact(d_key)) |
| 4629 | PyString_InternInPlace(&d_key); |
| 4630 | if (PyObject_SetItem(dict, d_key, d_value) < 0) { |
| 4631 | Py_DECREF(d_key); |
| 4632 | goto finally; |
| 4633 | } |
| 4634 | Py_DECREF(d_key); |
| 4635 | } |
| 4636 | Py_DECREF(dict); |
| 4637 | } |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 4638 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4639 | /* Also set instance attributes from the slotstate dict (if any). */ |
| 4640 | if (slotstate != NULL) { |
| 4641 | if (! PyDict_Check(slotstate)) { |
| 4642 | PyErr_SetString(UnpicklingError, "slot state is not " |
| 4643 | "a dictionary"); |
| 4644 | goto finally; |
| 4645 | } |
| 4646 | i = 0; |
| 4647 | while (PyDict_Next(slotstate, &i, &d_key, &d_value)) { |
| 4648 | if (PyObject_SetAttr(inst, d_key, d_value) < 0) |
| 4649 | goto finally; |
| 4650 | } |
| 4651 | } |
| 4652 | res = 0; |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 4653 | |
| 4654 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4655 | Py_DECREF(state); |
| 4656 | Py_XDECREF(slotstate); |
| 4657 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4658 | } |
| 4659 | |
| 4660 | |
| 4661 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4662 | load_mark(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4663 | { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4664 | Py_ssize_t s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4665 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4666 | /* Note that we split the (pickle.py) stack into two stacks, an |
| 4667 | object stack and a mark stack. Here we push a mark onto the |
| 4668 | mark stack. |
| 4669 | */ |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4670 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4671 | if ((self->num_marks + 1) >= self->marks_size) { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4672 | Py_ssize_t *marks; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4673 | s=self->marks_size+20; |
| 4674 | if (s <= self->num_marks) s=self->num_marks + 1; |
| 4675 | if (self->marks == NULL) |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4676 | marks=(Py_ssize_t *)malloc(s * sizeof(Py_ssize_t)); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4677 | else |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 4678 | marks=(Py_ssize_t *)realloc(self->marks, |
| 4679 | s * sizeof(Py_ssize_t)); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4680 | if (!marks) { |
| 4681 | PyErr_NoMemory(); |
| 4682 | return -1; |
| 4683 | } |
| 4684 | self->marks = marks; |
| 4685 | self->marks_size = s; |
| 4686 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4687 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4688 | self->marks[self->num_marks++] = self->stack->length; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4689 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4690 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4691 | } |
| 4692 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4693 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4694 | load_reduce(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4695 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4696 | PyObject *callable = 0, *arg_tup = 0, *ob = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4697 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4698 | PDATA_POP(self->stack, arg_tup); |
| 4699 | if (! arg_tup) return -1; |
| 4700 | PDATA_POP(self->stack, callable); |
| 4701 | if (callable) { |
| 4702 | ob = Instance_New(callable, arg_tup); |
| 4703 | Py_DECREF(callable); |
| 4704 | } |
| 4705 | Py_DECREF(arg_tup); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4706 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4707 | if (! ob) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4708 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4709 | PDATA_PUSH(self->stack, ob, -1); |
| 4710 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4711 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4712 | |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 4713 | /* Just raises an error if we don't know the protocol specified. PROTO |
| 4714 | * is the first opcode for protocols >= 2. |
| 4715 | */ |
| 4716 | static int |
| 4717 | load_proto(Unpicklerobject *self) |
| 4718 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4719 | int i; |
| 4720 | char *protobyte; |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 4721 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4722 | i = self->read_func(self, &protobyte, 1); |
| 4723 | if (i < 0) |
| 4724 | return -1; |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 4725 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4726 | i = calc_binint(protobyte, 1); |
| 4727 | /* No point checking for < 0, since calc_binint returns an unsigned |
| 4728 | * int when chewing on 1 byte. |
| 4729 | */ |
| 4730 | assert(i >= 0); |
| 4731 | if (i <= HIGHEST_PROTOCOL) |
| 4732 | return 0; |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 4733 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4734 | PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i); |
| 4735 | return -1; |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 4736 | } |
| 4737 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4738 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4739 | load(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4740 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4741 | PyObject *err = 0, *val = 0; |
| 4742 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4743 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4744 | self->num_marks = 0; |
| 4745 | if (self->stack->length) Pdata_clear(self->stack, 0); |
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 | while (1) { |
| 4748 | if (self->read_func(self, &s, 1) < 0) |
| 4749 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4750 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4751 | switch (s[0]) { |
| 4752 | case NONE: |
| 4753 | if (load_none(self) < 0) |
| 4754 | break; |
| 4755 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4756 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4757 | case BININT: |
| 4758 | if (load_binint(self) < 0) |
| 4759 | break; |
| 4760 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4761 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4762 | case BININT1: |
| 4763 | if (load_binint1(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 BININT2: |
| 4768 | if (load_binint2(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 INT: |
| 4773 | if (load_int(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 LONG: |
| 4778 | if (load_long(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 LONG1: |
| 4783 | if (load_counted_long(self, 1) < 0) |
| 4784 | break; |
| 4785 | continue; |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 4786 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4787 | case LONG4: |
| 4788 | if (load_counted_long(self, 4) < 0) |
| 4789 | break; |
| 4790 | continue; |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 4791 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4792 | case FLOAT: |
| 4793 | if (load_float(self) < 0) |
| 4794 | break; |
| 4795 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4796 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4797 | case BINFLOAT: |
| 4798 | if (load_binfloat(self) < 0) |
| 4799 | break; |
| 4800 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4801 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4802 | case BINSTRING: |
| 4803 | if (load_binstring(self) < 0) |
| 4804 | break; |
| 4805 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4806 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4807 | case SHORT_BINSTRING: |
| 4808 | if (load_short_binstring(self) < 0) |
| 4809 | break; |
| 4810 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4811 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4812 | case STRING: |
| 4813 | if (load_string(self) < 0) |
| 4814 | break; |
| 4815 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4816 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4817 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4818 | case UNICODE: |
| 4819 | if (load_unicode(self) < 0) |
| 4820 | break; |
| 4821 | continue; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 4822 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4823 | case BINUNICODE: |
| 4824 | if (load_binunicode(self) < 0) |
| 4825 | break; |
| 4826 | continue; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4827 | #endif |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 4828 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4829 | case EMPTY_TUPLE: |
| 4830 | if (load_counted_tuple(self, 0) < 0) |
| 4831 | break; |
| 4832 | continue; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 4833 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4834 | case TUPLE1: |
| 4835 | if (load_counted_tuple(self, 1) < 0) |
| 4836 | break; |
| 4837 | continue; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 4838 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4839 | case TUPLE2: |
| 4840 | if (load_counted_tuple(self, 2) < 0) |
| 4841 | break; |
| 4842 | continue; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 4843 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4844 | case TUPLE3: |
| 4845 | if (load_counted_tuple(self, 3) < 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 TUPLE: |
| 4850 | if (load_tuple(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 EMPTY_LIST: |
| 4855 | if (load_empty_list(self) < 0) |
| 4856 | break; |
| 4857 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4858 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4859 | case LIST: |
| 4860 | if (load_list(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 EMPTY_DICT: |
| 4865 | if (load_empty_dict(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 DICT: |
| 4870 | if (load_dict(self) < 0) |
| 4871 | break; |
| 4872 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4873 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4874 | case OBJ: |
| 4875 | if (load_obj(self) < 0) |
| 4876 | break; |
| 4877 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4878 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4879 | case INST: |
| 4880 | if (load_inst(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 NEWOBJ: |
| 4885 | if (load_newobj(self) < 0) |
| 4886 | break; |
| 4887 | continue; |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 4888 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4889 | case GLOBAL: |
| 4890 | if (load_global(self) < 0) |
| 4891 | break; |
| 4892 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4893 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4894 | case APPEND: |
| 4895 | if (load_append(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 APPENDS: |
| 4900 | if (load_appends(self) < 0) |
| 4901 | break; |
| 4902 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4903 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4904 | case BUILD: |
| 4905 | if (load_build(self) < 0) |
| 4906 | break; |
| 4907 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4908 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4909 | case DUP: |
| 4910 | if (load_dup(self) < 0) |
| 4911 | break; |
| 4912 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4913 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4914 | case BINGET: |
| 4915 | if (load_binget(self) < 0) |
| 4916 | break; |
| 4917 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4918 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4919 | case LONG_BINGET: |
| 4920 | if (load_long_binget(self) < 0) |
| 4921 | break; |
| 4922 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4923 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4924 | case GET: |
| 4925 | if (load_get(self) < 0) |
| 4926 | break; |
| 4927 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4928 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4929 | case EXT1: |
| 4930 | if (load_extension(self, 1) < 0) |
| 4931 | break; |
| 4932 | continue; |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 4933 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4934 | case EXT2: |
| 4935 | if (load_extension(self, 2) < 0) |
| 4936 | break; |
| 4937 | continue; |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 4938 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4939 | case EXT4: |
| 4940 | if (load_extension(self, 4) < 0) |
| 4941 | break; |
| 4942 | continue; |
| 4943 | case MARK: |
| 4944 | if (load_mark(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 BINPUT: |
| 4949 | if (load_binput(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 LONG_BINPUT: |
| 4954 | if (load_long_binput(self) < 0) |
| 4955 | break; |
| 4956 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4957 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4958 | case PUT: |
| 4959 | if (load_put(self) < 0) |
| 4960 | break; |
| 4961 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4962 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4963 | case POP: |
| 4964 | if (load_pop(self) < 0) |
| 4965 | break; |
| 4966 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4967 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4968 | case POP_MARK: |
| 4969 | if (load_pop_mark(self) < 0) |
| 4970 | break; |
| 4971 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4972 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4973 | case SETITEM: |
| 4974 | if (load_setitem(self) < 0) |
| 4975 | break; |
| 4976 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4977 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4978 | case SETITEMS: |
| 4979 | if (load_setitems(self) < 0) |
| 4980 | break; |
| 4981 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4982 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4983 | case STOP: |
| 4984 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4985 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4986 | case PERSID: |
| 4987 | if (load_persid(self) < 0) |
| 4988 | break; |
| 4989 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4990 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4991 | case BINPERSID: |
| 4992 | if (load_binpersid(self) < 0) |
| 4993 | break; |
| 4994 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4995 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4996 | case REDUCE: |
| 4997 | if (load_reduce(self) < 0) |
| 4998 | break; |
| 4999 | continue; |
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 | case PROTO: |
| 5002 | if (load_proto(self) < 0) |
| 5003 | break; |
| 5004 | continue; |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 5005 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5006 | case NEWTRUE: |
| 5007 | if (load_bool(self, Py_True) < 0) |
| 5008 | break; |
| 5009 | continue; |
Tim Peters | 3c67d79 | 2003-02-02 17:59:11 +0000 | [diff] [blame] | 5010 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5011 | case NEWFALSE: |
| 5012 | if (load_bool(self, Py_False) < 0) |
| 5013 | break; |
| 5014 | continue; |
Tim Peters | 3c67d79 | 2003-02-02 17:59:11 +0000 | [diff] [blame] | 5015 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5016 | case '\0': |
| 5017 | /* end of file */ |
| 5018 | PyErr_SetNone(PyExc_EOFError); |
| 5019 | break; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5020 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5021 | default: |
| 5022 | cPickle_ErrFormat(UnpicklingError, |
| 5023 | "invalid load key, '%s'.", |
| 5024 | "c", s[0]); |
| 5025 | return NULL; |
| 5026 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5027 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5028 | break; |
| 5029 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5030 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5031 | if ((err = PyErr_Occurred())) { |
| 5032 | if (err == PyExc_EOFError) { |
| 5033 | PyErr_SetNone(PyExc_EOFError); |
| 5034 | } |
| 5035 | return NULL; |
| 5036 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5037 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5038 | PDATA_POP(self->stack, val); |
| 5039 | return val; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5040 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5041 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5042 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5043 | /* No-load functions to support noload, which is used to |
| 5044 | find persistent references. */ |
| 5045 | |
| 5046 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5047 | noload_obj(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5048 | { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 5049 | Py_ssize_t i; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5050 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5051 | if ((i = marker(self)) < 0) return -1; |
| 5052 | return Pdata_clear(self->stack, i+1); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5053 | } |
| 5054 | |
| 5055 | |
| 5056 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5057 | noload_inst(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5058 | { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 5059 | Py_ssize_t i; |
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 ((i = marker(self)) < 0) return -1; |
| 5063 | Pdata_clear(self->stack, i); |
| 5064 | if (self->readline_func(self, &s) < 0) return -1; |
| 5065 | if (self->readline_func(self, &s) < 0) return -1; |
| 5066 | PDATA_APPEND(self->stack, Py_None, -1); |
| 5067 | return 0; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5068 | } |
| 5069 | |
| 5070 | static int |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 5071 | noload_newobj(Unpicklerobject *self) |
| 5072 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5073 | PyObject *obj; |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 5074 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5075 | PDATA_POP(self->stack, obj); /* pop argtuple */ |
| 5076 | if (obj == NULL) return -1; |
| 5077 | Py_DECREF(obj); |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 5078 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5079 | PDATA_POP(self->stack, obj); /* pop cls */ |
| 5080 | if (obj == NULL) return -1; |
| 5081 | Py_DECREF(obj); |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 5082 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5083 | PDATA_APPEND(self->stack, Py_None, -1); |
| 5084 | return 0; |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 5085 | } |
| 5086 | |
| 5087 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5088 | noload_global(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5089 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5090 | char *s; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5091 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5092 | if (self->readline_func(self, &s) < 0) return -1; |
| 5093 | if (self->readline_func(self, &s) < 0) return -1; |
| 5094 | PDATA_APPEND(self->stack, Py_None,-1); |
| 5095 | return 0; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5096 | } |
| 5097 | |
| 5098 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5099 | noload_reduce(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5100 | { |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5101 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5102 | if (self->stack->length < 2) return stackUnderflow(); |
| 5103 | Pdata_clear(self->stack, self->stack->length-2); |
| 5104 | PDATA_APPEND(self->stack, Py_None,-1); |
| 5105 | return 0; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5106 | } |
| 5107 | |
| 5108 | static int |
| 5109 | noload_build(Unpicklerobject *self) { |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5110 | |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 5111 | if (self->stack->length < 1) return stackUnderflow(); |
| 5112 | Pdata_clear(self->stack, self->stack->length-1); |
Guido van Rossum | 50f385c | 1998-12-04 18:48:44 +0000 | [diff] [blame] | 5113 | return 0; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5114 | } |
| 5115 | |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 5116 | static int |
| 5117 | noload_extension(Unpicklerobject *self, int nbytes) |
| 5118 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5119 | char *codebytes; |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 5120 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5121 | assert(nbytes == 1 || nbytes == 2 || nbytes == 4); |
| 5122 | if (self->read_func(self, &codebytes, nbytes) < 0) return -1; |
| 5123 | PDATA_APPEND(self->stack, Py_None, -1); |
| 5124 | return 0; |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 5125 | } |
| 5126 | |
Neil Schemenauer | 973f8b4 | 2009-10-14 19:33:31 +0000 | [diff] [blame] | 5127 | static int |
| 5128 | noload_append(Unpicklerobject *self) |
| 5129 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5130 | return Pdata_clear(self->stack, self->stack->length - 1); |
Neil Schemenauer | 973f8b4 | 2009-10-14 19:33:31 +0000 | [diff] [blame] | 5131 | } |
| 5132 | |
| 5133 | static int |
| 5134 | noload_appends(Unpicklerobject *self) |
| 5135 | { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 5136 | Py_ssize_t i; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5137 | if ((i = marker(self)) < 0) return -1; |
| 5138 | return Pdata_clear(self->stack, i); |
Neil Schemenauer | 973f8b4 | 2009-10-14 19:33:31 +0000 | [diff] [blame] | 5139 | } |
| 5140 | |
| 5141 | static int |
| 5142 | noload_setitem(Unpicklerobject *self) |
| 5143 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5144 | return Pdata_clear(self->stack, self->stack->length - 2); |
Neil Schemenauer | 973f8b4 | 2009-10-14 19:33:31 +0000 | [diff] [blame] | 5145 | } |
| 5146 | |
| 5147 | static int |
| 5148 | noload_setitems(Unpicklerobject *self) |
| 5149 | { |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 5150 | Py_ssize_t i; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5151 | if ((i = marker(self)) < 0) return -1; |
| 5152 | return Pdata_clear(self->stack, i); |
Neil Schemenauer | 973f8b4 | 2009-10-14 19:33:31 +0000 | [diff] [blame] | 5153 | } |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5154 | |
| 5155 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5156 | noload(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5157 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5158 | PyObject *err = 0, *val = 0; |
| 5159 | char *s; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5160 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5161 | self->num_marks = 0; |
| 5162 | Pdata_clear(self->stack, 0); |
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 | while (1) { |
| 5165 | if (self->read_func(self, &s, 1) < 0) |
| 5166 | break; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5167 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5168 | switch (s[0]) { |
| 5169 | case NONE: |
| 5170 | if (load_none(self) < 0) |
| 5171 | break; |
| 5172 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5173 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5174 | case BININT: |
| 5175 | if (load_binint(self) < 0) |
| 5176 | break; |
| 5177 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5178 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5179 | case BININT1: |
| 5180 | if (load_binint1(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 BININT2: |
| 5185 | if (load_binint2(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 INT: |
| 5190 | if (load_int(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 LONG: |
| 5195 | if (load_long(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 LONG1: |
| 5200 | if (load_counted_long(self, 1) < 0) |
| 5201 | break; |
| 5202 | continue; |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 5203 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5204 | case LONG4: |
| 5205 | if (load_counted_long(self, 4) < 0) |
| 5206 | break; |
| 5207 | continue; |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 5208 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5209 | case FLOAT: |
| 5210 | if (load_float(self) < 0) |
| 5211 | break; |
| 5212 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5213 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5214 | case BINFLOAT: |
| 5215 | if (load_binfloat(self) < 0) |
| 5216 | break; |
| 5217 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5218 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5219 | case BINSTRING: |
| 5220 | if (load_binstring(self) < 0) |
| 5221 | break; |
| 5222 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5223 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5224 | case SHORT_BINSTRING: |
| 5225 | if (load_short_binstring(self) < 0) |
| 5226 | break; |
| 5227 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5228 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5229 | case STRING: |
| 5230 | if (load_string(self) < 0) |
| 5231 | break; |
| 5232 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5233 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 5234 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5235 | case UNICODE: |
| 5236 | if (load_unicode(self) < 0) |
| 5237 | break; |
| 5238 | continue; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 5239 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5240 | case BINUNICODE: |
| 5241 | if (load_binunicode(self) < 0) |
| 5242 | break; |
| 5243 | continue; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 5244 | #endif |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 5245 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5246 | case EMPTY_TUPLE: |
| 5247 | if (load_counted_tuple(self, 0) < 0) |
| 5248 | break; |
| 5249 | continue; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 5250 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5251 | case TUPLE1: |
| 5252 | if (load_counted_tuple(self, 1) < 0) |
| 5253 | break; |
| 5254 | continue; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 5255 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5256 | case TUPLE2: |
| 5257 | if (load_counted_tuple(self, 2) < 0) |
| 5258 | break; |
| 5259 | continue; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 5260 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5261 | case TUPLE3: |
| 5262 | if (load_counted_tuple(self, 3) < 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 TUPLE: |
| 5267 | if (load_tuple(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 EMPTY_LIST: |
| 5272 | if (load_empty_list(self) < 0) |
| 5273 | break; |
| 5274 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5275 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5276 | case LIST: |
| 5277 | if (load_list(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 EMPTY_DICT: |
| 5282 | if (load_empty_dict(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 DICT: |
| 5287 | if (load_dict(self) < 0) |
| 5288 | break; |
| 5289 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5290 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5291 | case OBJ: |
| 5292 | if (noload_obj(self) < 0) |
| 5293 | break; |
| 5294 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5295 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5296 | case INST: |
| 5297 | if (noload_inst(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 NEWOBJ: |
| 5302 | if (noload_newobj(self) < 0) |
| 5303 | break; |
| 5304 | continue; |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 5305 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5306 | case GLOBAL: |
| 5307 | if (noload_global(self) < 0) |
| 5308 | break; |
| 5309 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5310 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5311 | case APPEND: |
| 5312 | if (noload_append(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 APPENDS: |
| 5317 | if (noload_appends(self) < 0) |
| 5318 | break; |
| 5319 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5320 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5321 | case BUILD: |
| 5322 | if (noload_build(self) < 0) |
| 5323 | break; |
| 5324 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5325 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5326 | case DUP: |
| 5327 | if (load_dup(self) < 0) |
| 5328 | break; |
| 5329 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5330 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5331 | case BINGET: |
| 5332 | if (load_binget(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 LONG_BINGET: |
| 5337 | if (load_long_binget(self) < 0) |
| 5338 | break; |
| 5339 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5340 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5341 | case GET: |
| 5342 | if (load_get(self) < 0) |
| 5343 | break; |
| 5344 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5345 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5346 | case EXT1: |
| 5347 | if (noload_extension(self, 1) < 0) |
| 5348 | break; |
| 5349 | continue; |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 5350 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5351 | case EXT2: |
| 5352 | if (noload_extension(self, 2) < 0) |
| 5353 | break; |
| 5354 | continue; |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 5355 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5356 | case EXT4: |
| 5357 | if (noload_extension(self, 4) < 0) |
| 5358 | break; |
| 5359 | continue; |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 5360 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5361 | case MARK: |
| 5362 | if (load_mark(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 BINPUT: |
| 5367 | if (load_binput(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 LONG_BINPUT: |
| 5372 | if (load_long_binput(self) < 0) |
| 5373 | break; |
| 5374 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5375 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5376 | case PUT: |
| 5377 | if (load_put(self) < 0) |
| 5378 | break; |
| 5379 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5380 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5381 | case POP: |
| 5382 | if (load_pop(self) < 0) |
| 5383 | break; |
| 5384 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5385 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5386 | case POP_MARK: |
| 5387 | if (load_pop_mark(self) < 0) |
| 5388 | break; |
| 5389 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5390 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5391 | case SETITEM: |
| 5392 | if (noload_setitem(self) < 0) |
| 5393 | break; |
| 5394 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5395 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5396 | case SETITEMS: |
| 5397 | if (noload_setitems(self) < 0) |
| 5398 | break; |
| 5399 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5400 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5401 | case STOP: |
| 5402 | break; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5403 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5404 | case PERSID: |
| 5405 | if (load_persid(self) < 0) |
| 5406 | break; |
| 5407 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5408 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5409 | case BINPERSID: |
| 5410 | if (load_binpersid(self) < 0) |
| 5411 | break; |
| 5412 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5413 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5414 | case REDUCE: |
| 5415 | if (noload_reduce(self) < 0) |
| 5416 | break; |
| 5417 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5418 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5419 | case PROTO: |
| 5420 | if (load_proto(self) < 0) |
| 5421 | break; |
| 5422 | continue; |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 5423 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5424 | case NEWTRUE: |
| 5425 | if (load_bool(self, Py_True) < 0) |
| 5426 | break; |
| 5427 | continue; |
Tim Peters | 3c67d79 | 2003-02-02 17:59:11 +0000 | [diff] [blame] | 5428 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5429 | case NEWFALSE: |
| 5430 | if (load_bool(self, Py_False) < 0) |
| 5431 | break; |
| 5432 | continue; |
| 5433 | default: |
| 5434 | cPickle_ErrFormat(UnpicklingError, |
| 5435 | "invalid load key, '%s'.", |
| 5436 | "c", s[0]); |
| 5437 | return NULL; |
| 5438 | } |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5439 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5440 | break; |
| 5441 | } |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5442 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5443 | if ((err = PyErr_Occurred())) { |
| 5444 | if (err == PyExc_EOFError) { |
| 5445 | PyErr_SetNone(PyExc_EOFError); |
| 5446 | } |
| 5447 | return NULL; |
| 5448 | } |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5449 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5450 | PDATA_POP(self->stack, val); |
| 5451 | return val; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5452 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5453 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5454 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5455 | static PyObject * |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 5456 | Unpickler_load(Unpicklerobject *self, PyObject *unused) |
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 | return load(self); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5459 | } |
| 5460 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5461 | static PyObject * |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 5462 | Unpickler_noload(Unpicklerobject *self, PyObject *unused) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5463 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5464 | return noload(self); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5465 | } |
| 5466 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5467 | |
| 5468 | static struct PyMethodDef Unpickler_methods[] = { |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 5469 | {"load", (PyCFunction)Unpickler_load, METH_NOARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 5470 | PyDoc_STR("load() -- Load a pickle") |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5471 | }, |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 5472 | {"noload", (PyCFunction)Unpickler_noload, METH_NOARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 5473 | PyDoc_STR( |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5474 | "noload() -- not load a pickle, but go through most of the motions\n" |
| 5475 | "\n" |
| 5476 | "This function can be used to read past a pickle without instantiating\n" |
| 5477 | "any objects or importing any modules. It can also be used to find all\n" |
| 5478 | "persistent references without instantiating any objects or importing\n" |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 5479 | "any modules.\n") |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5480 | }, |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5481 | {NULL, NULL} /* sentinel */ |
| 5482 | }; |
| 5483 | |
| 5484 | |
| 5485 | static Unpicklerobject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5486 | newUnpicklerobject(PyObject *f) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5487 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5488 | Unpicklerobject *self; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5489 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5490 | if (!( self = PyObject_GC_New(Unpicklerobject, &Unpicklertype))) |
| 5491 | return NULL; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5492 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5493 | self->file = NULL; |
| 5494 | self->arg = NULL; |
| 5495 | self->stack = (Pdata*)Pdata_New(); |
| 5496 | self->pers_func = NULL; |
| 5497 | self->last_string = NULL; |
| 5498 | self->marks = NULL; |
| 5499 | self->num_marks = 0; |
| 5500 | self->marks_size = 0; |
| 5501 | self->buf_size = 0; |
| 5502 | self->read = NULL; |
| 5503 | self->readline = NULL; |
| 5504 | self->find_class = NULL; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5505 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5506 | if (!( self->memo = PyDict_New())) |
| 5507 | goto err; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5508 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5509 | if (!self->stack) |
| 5510 | goto err; |
Neal Norwitz | b59d08c | 2006-07-22 16:20:49 +0000 | [diff] [blame] | 5511 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5512 | Py_INCREF(f); |
| 5513 | self->file = f; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5514 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5515 | /* Set read, readline based on type of f */ |
| 5516 | if (PyFile_Check(f)) { |
| 5517 | self->fp = PyFile_AsFile(f); |
| 5518 | if (self->fp == NULL) { |
| 5519 | PyErr_SetString(PyExc_ValueError, |
| 5520 | "I/O operation on closed file"); |
| 5521 | goto err; |
| 5522 | } |
| 5523 | self->read_func = read_file; |
| 5524 | self->readline_func = readline_file; |
| 5525 | } |
| 5526 | else if (PycStringIO_InputCheck(f)) { |
| 5527 | self->fp = NULL; |
| 5528 | self->read_func = read_cStringIO; |
| 5529 | self->readline_func = readline_cStringIO; |
| 5530 | } |
| 5531 | else { |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5532 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5533 | self->fp = NULL; |
| 5534 | self->read_func = read_other; |
| 5535 | self->readline_func = readline_other; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5536 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5537 | if (!( (self->readline = PyObject_GetAttr(f, readline_str)) && |
| 5538 | (self->read = PyObject_GetAttr(f, read_str)))) { |
| 5539 | PyErr_Clear(); |
| 5540 | PyErr_SetString( PyExc_TypeError, |
| 5541 | "argument must have 'read' and " |
| 5542 | "'readline' attributes" ); |
| 5543 | goto err; |
| 5544 | } |
| 5545 | } |
| 5546 | PyObject_GC_Track(self); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5547 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5548 | return self; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5549 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5550 | err: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5551 | Py_DECREF((PyObject *)self); |
| 5552 | return NULL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5553 | } |
| 5554 | |
| 5555 | |
| 5556 | static PyObject * |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 5557 | get_Unpickler(PyObject *self, PyObject *file) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5558 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5559 | return (PyObject *)newUnpicklerobject(file); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5560 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5561 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5562 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5563 | static void |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5564 | Unpickler_dealloc(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5565 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5566 | PyObject_GC_UnTrack((PyObject *)self); |
| 5567 | Py_XDECREF(self->readline); |
| 5568 | Py_XDECREF(self->read); |
| 5569 | Py_XDECREF(self->file); |
| 5570 | Py_XDECREF(self->memo); |
| 5571 | Py_XDECREF(self->stack); |
| 5572 | Py_XDECREF(self->pers_func); |
| 5573 | Py_XDECREF(self->arg); |
| 5574 | Py_XDECREF(self->last_string); |
| 5575 | Py_XDECREF(self->find_class); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5576 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5577 | if (self->marks) { |
| 5578 | free(self->marks); |
| 5579 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5580 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5581 | if (self->buf_size) { |
| 5582 | free(self->buf); |
| 5583 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5584 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5585 | Py_TYPE(self)->tp_free((PyObject *)self); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5586 | } |
| 5587 | |
Jeremy Hylton | 7b5ce7f | 2003-04-09 21:25:30 +0000 | [diff] [blame] | 5588 | static int |
| 5589 | Unpickler_traverse(Unpicklerobject *self, visitproc visit, void *arg) |
| 5590 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5591 | Py_VISIT(self->readline); |
| 5592 | Py_VISIT(self->read); |
| 5593 | Py_VISIT(self->file); |
| 5594 | Py_VISIT(self->memo); |
| 5595 | Py_VISIT(self->stack); |
| 5596 | Py_VISIT(self->pers_func); |
| 5597 | Py_VISIT(self->arg); |
| 5598 | Py_VISIT(self->last_string); |
| 5599 | Py_VISIT(self->find_class); |
| 5600 | return 0; |
Jeremy Hylton | 7b5ce7f | 2003-04-09 21:25:30 +0000 | [diff] [blame] | 5601 | } |
| 5602 | |
| 5603 | static int |
| 5604 | Unpickler_clear(Unpicklerobject *self) |
| 5605 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5606 | Py_CLEAR(self->readline); |
| 5607 | Py_CLEAR(self->read); |
| 5608 | Py_CLEAR(self->file); |
| 5609 | Py_CLEAR(self->memo); |
| 5610 | Py_CLEAR(self->stack); |
| 5611 | Py_CLEAR(self->pers_func); |
| 5612 | Py_CLEAR(self->arg); |
| 5613 | Py_CLEAR(self->last_string); |
| 5614 | Py_CLEAR(self->find_class); |
| 5615 | return 0; |
Jeremy Hylton | 7b5ce7f | 2003-04-09 21:25:30 +0000 | [diff] [blame] | 5616 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5617 | |
| 5618 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5619 | Unpickler_getattr(Unpicklerobject *self, char *name) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5620 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5621 | if (!strcmp(name, "persistent_load")) { |
| 5622 | if (!self->pers_func) { |
| 5623 | PyErr_SetString(PyExc_AttributeError, name); |
| 5624 | return NULL; |
| 5625 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5626 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5627 | Py_INCREF(self->pers_func); |
| 5628 | return self->pers_func; |
| 5629 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5630 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5631 | if (!strcmp(name, "find_global")) { |
| 5632 | if (!self->find_class) { |
| 5633 | PyErr_SetString(PyExc_AttributeError, name); |
| 5634 | return NULL; |
| 5635 | } |
Guido van Rossum | 1b9e0aa | 1999-04-19 17:58:18 +0000 | [diff] [blame] | 5636 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5637 | Py_INCREF(self->find_class); |
| 5638 | return self->find_class; |
| 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, "memo")) { |
| 5642 | if (!self->memo) { |
| 5643 | PyErr_SetString(PyExc_AttributeError, name); |
| 5644 | return NULL; |
| 5645 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5646 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5647 | Py_INCREF(self->memo); |
| 5648 | return self->memo; |
| 5649 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5650 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5651 | if (!strcmp(name, "UnpicklingError")) { |
| 5652 | Py_INCREF(UnpicklingError); |
| 5653 | return UnpicklingError; |
| 5654 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5655 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5656 | return Py_FindMethod(Unpickler_methods, (PyObject *)self, name); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5657 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5658 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5659 | |
| 5660 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5661 | Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5662 | { |
Jeremy Hylton | ce616e4 | 1998-08-13 23:13:52 +0000 | [diff] [blame] | 5663 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5664 | if (!strcmp(name, "persistent_load")) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5665 | Py_XINCREF(value); |
Serhiy Storchaka | bc62af1 | 2016-04-06 09:51:18 +0300 | [diff] [blame] | 5666 | Py_XSETREF(self->pers_func, value); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5667 | return 0; |
| 5668 | } |
Guido van Rossum | 1b9e0aa | 1999-04-19 17:58:18 +0000 | [diff] [blame] | 5669 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5670 | if (!strcmp(name, "find_global")) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5671 | Py_XINCREF(value); |
Serhiy Storchaka | bc62af1 | 2016-04-06 09:51:18 +0300 | [diff] [blame] | 5672 | Py_XSETREF(self->find_class, value); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5673 | return 0; |
| 5674 | } |
Guido van Rossum | 1b9e0aa | 1999-04-19 17:58:18 +0000 | [diff] [blame] | 5675 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5676 | if (! value) { |
| 5677 | PyErr_SetString(PyExc_TypeError, |
| 5678 | "attribute deletion is not supported"); |
| 5679 | return -1; |
| 5680 | } |
Jeremy Hylton | ce616e4 | 1998-08-13 23:13:52 +0000 | [diff] [blame] | 5681 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5682 | if (strcmp(name, "memo") == 0) { |
| 5683 | if (!PyDict_Check(value)) { |
| 5684 | PyErr_SetString(PyExc_TypeError, |
| 5685 | "memo must be a dictionary"); |
| 5686 | return -1; |
| 5687 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5688 | Py_INCREF(value); |
Serhiy Storchaka | bc62af1 | 2016-04-06 09:51:18 +0300 | [diff] [blame] | 5689 | Py_XSETREF(self->memo, value); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5690 | return 0; |
| 5691 | } |
Jeremy Hylton | ce616e4 | 1998-08-13 23:13:52 +0000 | [diff] [blame] | 5692 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5693 | PyErr_SetString(PyExc_AttributeError, name); |
| 5694 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5695 | } |
| 5696 | |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5697 | /* --------------------------------------------------------------------------- |
| 5698 | * Module-level functions. |
| 5699 | */ |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5700 | |
Martin v. Löwis | 544f119 | 2004-07-27 05:22:33 +0000 | [diff] [blame] | 5701 | /* dump(obj, file, protocol=0). */ |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5702 | static PyObject * |
Martin v. Löwis | 544f119 | 2004-07-27 05:22:33 +0000 | [diff] [blame] | 5703 | cpm_dump(PyObject *self, PyObject *args, PyObject *kwds) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5704 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5705 | static char *kwlist[] = {"obj", "file", "protocol", NULL}; |
| 5706 | PyObject *ob, *file, *res = NULL; |
| 5707 | Picklerobject *pickler = 0; |
| 5708 | int proto = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5709 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5710 | if (!( PyArg_ParseTupleAndKeywords(args, kwds, "OO|i", kwlist, |
| 5711 | &ob, &file, &proto))) |
| 5712 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5713 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5714 | if (!( pickler = newPicklerobject(file, proto))) |
| 5715 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5716 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5717 | if (dump(pickler, ob) < 0) |
| 5718 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5719 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5720 | Py_INCREF(Py_None); |
| 5721 | res = Py_None; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5722 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5723 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5724 | Py_XDECREF(pickler); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5725 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5726 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5727 | } |
| 5728 | |
| 5729 | |
Martin v. Löwis | 544f119 | 2004-07-27 05:22:33 +0000 | [diff] [blame] | 5730 | /* dumps(obj, protocol=0). */ |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5731 | static PyObject * |
Martin v. Löwis | 544f119 | 2004-07-27 05:22:33 +0000 | [diff] [blame] | 5732 | cpm_dumps(PyObject *self, PyObject *args, PyObject *kwds) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5733 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5734 | static char *kwlist[] = {"obj", "protocol", NULL}; |
| 5735 | PyObject *ob, *file = 0, *res = NULL; |
| 5736 | Picklerobject *pickler = 0; |
| 5737 | int proto = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5738 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5739 | if (!( PyArg_ParseTupleAndKeywords(args, kwds, "O|i:dumps", kwlist, |
| 5740 | &ob, &proto))) |
| 5741 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5742 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5743 | if (!( file = PycStringIO->NewOutput(128))) |
| 5744 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5745 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5746 | if (!( pickler = newPicklerobject(file, proto))) |
| 5747 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5748 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5749 | if (dump(pickler, ob) < 0) |
| 5750 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5751 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5752 | res = PycStringIO->cgetvalue(file); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5753 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5754 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5755 | Py_XDECREF(pickler); |
| 5756 | Py_XDECREF(file); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5757 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5758 | return res; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5759 | } |
| 5760 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5761 | |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5762 | /* load(fileobj). */ |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5763 | static PyObject * |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 5764 | cpm_load(PyObject *self, PyObject *ob) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5765 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5766 | Unpicklerobject *unpickler = 0; |
| 5767 | PyObject *res = NULL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5768 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5769 | if (!( unpickler = newUnpicklerobject(ob))) |
| 5770 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5771 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5772 | res = load(unpickler); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5773 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5774 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5775 | Py_XDECREF(unpickler); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5776 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5777 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5778 | } |
| 5779 | |
| 5780 | |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5781 | /* loads(string) */ |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5782 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5783 | cpm_loads(PyObject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5784 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5785 | PyObject *ob, *file = 0, *res = NULL; |
| 5786 | Unpicklerobject *unpickler = 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5787 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5788 | if (!( PyArg_ParseTuple(args, "S:loads", &ob))) |
| 5789 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5790 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5791 | if (!( file = PycStringIO->NewInput(ob))) |
| 5792 | goto finally; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5793 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5794 | if (!( unpickler = newUnpicklerobject(file))) |
| 5795 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5796 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5797 | res = load(unpickler); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5798 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5799 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5800 | Py_XDECREF(file); |
| 5801 | Py_XDECREF(unpickler); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5802 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5803 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5804 | } |
| 5805 | |
| 5806 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5807 | PyDoc_STRVAR(Unpicklertype__doc__, |
| 5808 | "Objects that know how to unpickle"); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5809 | |
Guido van Rossum | 9716aaa | 1997-12-08 15:15:16 +0000 | [diff] [blame] | 5810 | static PyTypeObject Unpicklertype = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 5811 | PyVarObject_HEAD_INIT(NULL, 0) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5812 | "cPickle.Unpickler", /*tp_name*/ |
Jeremy Hylton | 7b5ce7f | 2003-04-09 21:25:30 +0000 | [diff] [blame] | 5813 | sizeof(Unpicklerobject), /*tp_basicsize*/ |
| 5814 | 0, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5815 | (destructor)Unpickler_dealloc, /* tp_dealloc */ |
| 5816 | 0, /* tp_print */ |
| 5817 | (getattrfunc)Unpickler_getattr, /* tp_getattr */ |
| 5818 | (setattrfunc)Unpickler_setattr, /* tp_setattr */ |
| 5819 | 0, /* tp_compare */ |
| 5820 | 0, /* tp_repr */ |
| 5821 | 0, /* tp_as_number */ |
| 5822 | 0, /* tp_as_sequence */ |
| 5823 | 0, /* tp_as_mapping */ |
| 5824 | 0, /* tp_hash */ |
| 5825 | 0, /* tp_call */ |
| 5826 | 0, /* tp_str */ |
| 5827 | 0, /* tp_getattro */ |
| 5828 | 0, /* tp_setattro */ |
| 5829 | 0, /* tp_as_buffer */ |
Jeremy Hylton | 7b5ce7f | 2003-04-09 21:25:30 +0000 | [diff] [blame] | 5830 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5831 | Unpicklertype__doc__, /* tp_doc */ |
| 5832 | (traverseproc)Unpickler_traverse, /* tp_traverse */ |
| 5833 | (inquiry)Unpickler_clear, /* tp_clear */ |
Guido van Rossum | 9716aaa | 1997-12-08 15:15:16 +0000 | [diff] [blame] | 5834 | }; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5835 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5836 | static struct PyMethodDef cPickle_methods[] = { |
Martin v. Löwis | 544f119 | 2004-07-27 05:22:33 +0000 | [diff] [blame] | 5837 | {"dump", (PyCFunction)cpm_dump, METH_VARARGS | METH_KEYWORDS, |
| 5838 | PyDoc_STR("dump(obj, file, protocol=0) -- " |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5839 | "Write an object in pickle format to the given file.\n" |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5840 | "\n" |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5841 | "See the Pickler docstring for the meaning of optional argument proto.") |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5842 | }, |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5843 | |
Martin v. Löwis | 544f119 | 2004-07-27 05:22:33 +0000 | [diff] [blame] | 5844 | {"dumps", (PyCFunction)cpm_dumps, METH_VARARGS | METH_KEYWORDS, |
| 5845 | PyDoc_STR("dumps(obj, protocol=0) -- " |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5846 | "Return a string containing an object in pickle format.\n" |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5847 | "\n" |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5848 | "See the Pickler docstring for the meaning of optional argument proto.") |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5849 | }, |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5850 | |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 5851 | {"load", (PyCFunction)cpm_load, METH_O, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 5852 | PyDoc_STR("load(file) -- Load a pickle from the given file")}, |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5853 | |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 5854 | {"loads", (PyCFunction)cpm_loads, METH_VARARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 5855 | PyDoc_STR("loads(string) -- Load a pickle from the given string")}, |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5856 | |
Martin v. Löwis | 544f119 | 2004-07-27 05:22:33 +0000 | [diff] [blame] | 5857 | {"Pickler", (PyCFunction)get_Pickler, METH_VARARGS | METH_KEYWORDS, |
| 5858 | PyDoc_STR("Pickler(file, protocol=0) -- Create a pickler.\n" |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5859 | "\n" |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5860 | "This takes a file-like object for writing a pickle data stream.\n" |
| 5861 | "The optional proto argument tells the pickler to use the given\n" |
| 5862 | "protocol; supported protocols are 0, 1, 2. The default\n" |
| 5863 | "protocol is 0, to be backwards compatible. (Protocol 0 is the\n" |
| 5864 | "only protocol that can be written to a file opened in text\n" |
| 5865 | "mode and read back successfully. When using a protocol higher\n" |
| 5866 | "than 0, make sure the file is opened in binary mode, both when\n" |
| 5867 | "pickling and unpickling.)\n" |
| 5868 | "\n" |
| 5869 | "Protocol 1 is more efficient than protocol 0; protocol 2 is\n" |
| 5870 | "more efficient than protocol 1.\n" |
| 5871 | "\n" |
| 5872 | "Specifying a negative protocol version selects the highest\n" |
| 5873 | "protocol version supported. The higher the protocol used, the\n" |
| 5874 | "more recent the version of Python needed to read the pickle\n" |
| 5875 | "produced.\n" |
| 5876 | "\n" |
| 5877 | "The file parameter must have a write() method that accepts a single\n" |
| 5878 | "string argument. It can thus be an open file object, a StringIO\n" |
| 5879 | "object, or any other custom object that meets this interface.\n") |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5880 | }, |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5881 | |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 5882 | {"Unpickler", (PyCFunction)get_Unpickler, METH_O, |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5883 | PyDoc_STR("Unpickler(file) -- Create an unpickler.")}, |
| 5884 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5885 | { NULL, NULL } |
| 5886 | }; |
| 5887 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5888 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5889 | init_stuff(PyObject *module_dict) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5890 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5891 | PyObject *copyreg, *t, *r; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5892 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 5893 | #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] | 5894 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5895 | if (PyType_Ready(&Unpicklertype) < 0) |
| 5896 | return -1; |
| 5897 | if (PyType_Ready(&Picklertype) < 0) |
| 5898 | return -1; |
Tim Peters | 3cfe754 | 2003-05-21 21:29:48 +0000 | [diff] [blame] | 5899 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5900 | INIT_STR(__class__); |
| 5901 | INIT_STR(__getinitargs__); |
| 5902 | INIT_STR(__dict__); |
| 5903 | INIT_STR(__getstate__); |
| 5904 | INIT_STR(__setstate__); |
| 5905 | INIT_STR(__name__); |
| 5906 | INIT_STR(__main__); |
| 5907 | INIT_STR(__reduce__); |
| 5908 | INIT_STR(__reduce_ex__); |
| 5909 | INIT_STR(write); |
| 5910 | INIT_STR(append); |
| 5911 | INIT_STR(read); |
| 5912 | INIT_STR(readline); |
| 5913 | INIT_STR(dispatch_table); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5914 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5915 | if (!( copyreg = PyImport_ImportModule("copy_reg"))) |
| 5916 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5917 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5918 | /* This is special because we want to use a different |
| 5919 | one in restricted mode. */ |
| 5920 | dispatch_table = PyObject_GetAttr(copyreg, dispatch_table_str); |
| 5921 | if (!dispatch_table) return -1; |
Tim Peters | 5b7da39 | 2003-02-04 00:21:07 +0000 | [diff] [blame] | 5922 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5923 | extension_registry = PyObject_GetAttrString(copyreg, |
| 5924 | "_extension_registry"); |
| 5925 | if (!extension_registry) return -1; |
Tim Peters | 5b7da39 | 2003-02-04 00:21:07 +0000 | [diff] [blame] | 5926 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5927 | inverted_registry = PyObject_GetAttrString(copyreg, |
| 5928 | "_inverted_registry"); |
| 5929 | if (!inverted_registry) return -1; |
Tim Peters | 5b7da39 | 2003-02-04 00:21:07 +0000 | [diff] [blame] | 5930 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5931 | extension_cache = PyObject_GetAttrString(copyreg, |
| 5932 | "_extension_cache"); |
| 5933 | if (!extension_cache) return -1; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5934 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5935 | Py_DECREF(copyreg); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5936 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5937 | if (!(empty_tuple = PyTuple_New(0))) |
| 5938 | return -1; |
Tim Peters | 731098b | 2003-02-04 20:56:09 +0000 | [diff] [blame] | 5939 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5940 | two_tuple = PyTuple_New(2); |
| 5941 | if (two_tuple == NULL) |
| 5942 | return -1; |
| 5943 | /* We use this temp container with no regard to refcounts, or to |
| 5944 | * keeping containees alive. Exempt from GC, because we don't |
| 5945 | * want anything looking at two_tuple() by magic. |
| 5946 | */ |
| 5947 | PyObject_GC_UnTrack(two_tuple); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5948 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5949 | /* Ugh */ |
| 5950 | if (!( t=PyImport_ImportModule("__builtin__"))) return -1; |
| 5951 | if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0) |
| 5952 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5953 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5954 | if (!( t=PyDict_New())) return -1; |
| 5955 | if (!( r=PyRun_String( |
| 5956 | "def __str__(self):\n" |
| 5957 | " return self.args and ('%s' % self.args[0]) or '(what)'\n", |
| 5958 | Py_file_input, |
| 5959 | module_dict, t) )) return -1; |
| 5960 | Py_DECREF(r); |
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 | PickleError = PyErr_NewException("cPickle.PickleError", NULL, t); |
| 5963 | if (!PickleError) |
| 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 | Py_DECREF(t); |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5967 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5968 | PicklingError = PyErr_NewException("cPickle.PicklingError", |
| 5969 | PickleError, NULL); |
| 5970 | if (!PicklingError) |
| 5971 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5972 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5973 | if (!( t=PyDict_New())) return -1; |
| 5974 | if (!( r=PyRun_String( |
| 5975 | "def __str__(self):\n" |
| 5976 | " a=self.args\n" |
| 5977 | " a=a and type(a[0]) or '(what)'\n" |
| 5978 | " return 'Cannot pickle %s objects' % a\n" |
| 5979 | , Py_file_input, |
| 5980 | module_dict, t) )) return -1; |
| 5981 | Py_DECREF(r); |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5982 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5983 | if (!( UnpickleableError = PyErr_NewException( |
| 5984 | "cPickle.UnpickleableError", PicklingError, t))) |
| 5985 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5986 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5987 | Py_DECREF(t); |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5988 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5989 | if (!( UnpicklingError = PyErr_NewException("cPickle.UnpicklingError", |
| 5990 | PickleError, NULL))) |
| 5991 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5992 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5993 | if (!( BadPickleGet = PyErr_NewException("cPickle.BadPickleGet", |
| 5994 | UnpicklingError, NULL))) |
| 5995 | return -1; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5996 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5997 | if (PyDict_SetItemString(module_dict, "PickleError", |
| 5998 | PickleError) < 0) |
| 5999 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 6000 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 6001 | if (PyDict_SetItemString(module_dict, "PicklingError", |
| 6002 | PicklingError) < 0) |
| 6003 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 6004 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 6005 | if (PyDict_SetItemString(module_dict, "UnpicklingError", |
| 6006 | UnpicklingError) < 0) |
| 6007 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 6008 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 6009 | if (PyDict_SetItemString(module_dict, "UnpickleableError", |
| 6010 | UnpickleableError) < 0) |
| 6011 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 6012 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 6013 | if (PyDict_SetItemString(module_dict, "BadPickleGet", |
| 6014 | BadPickleGet) < 0) |
| 6015 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 6016 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 6017 | PycString_IMPORT; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 6018 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 6019 | return 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 6020 | } |
| 6021 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 6022 | #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */ |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 6023 | #define PyMODINIT_FUNC void |
Guido van Rossum | f9ffb03 | 1999-02-04 14:54:04 +0000 | [diff] [blame] | 6024 | #endif |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 6025 | PyMODINIT_FUNC |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 6026 | initcPickle(void) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 6027 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 6028 | PyObject *m, *d, *di, *v, *k; |
| 6029 | Py_ssize_t i; |
| 6030 | char *rev = "1.71"; /* XXX when does this change? */ |
| 6031 | PyObject *format_version; |
| 6032 | PyObject *compatible_formats; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 6033 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 6034 | Py_TYPE(&Picklertype) = &PyType_Type; |
| 6035 | Py_TYPE(&Unpicklertype) = &PyType_Type; |
| 6036 | Py_TYPE(&PdataType) = &PyType_Type; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 6037 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 6038 | /* Initialize some pieces. We need to do this before module creation, |
| 6039 | * so we're forced to use a temporary dictionary. :( |
| 6040 | */ |
| 6041 | di = PyDict_New(); |
| 6042 | if (!di) return; |
| 6043 | if (init_stuff(di) < 0) return; |
Guido van Rossum | ebba420 | 2000-09-07 14:35:37 +0000 | [diff] [blame] | 6044 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 6045 | /* Create the module and add the functions */ |
| 6046 | m = Py_InitModule4("cPickle", cPickle_methods, |
| 6047 | cPickle_module_documentation, |
| 6048 | (PyObject*)NULL,PYTHON_API_VERSION); |
| 6049 | if (m == NULL) |
| 6050 | return; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 6051 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 6052 | /* Add some symbolic constants to the module */ |
| 6053 | d = PyModule_GetDict(m); |
| 6054 | v = PyString_FromString(rev); |
| 6055 | PyDict_SetItemString(d, "__version__", v); |
| 6056 | Py_XDECREF(v); |
Barry Warsaw | 93d29b6 | 1997-01-14 17:45:08 +0000 | [diff] [blame] | 6057 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 6058 | /* Copy data from di. Waaa. */ |
| 6059 | for (i=0; PyDict_Next(di, &i, &k, &v); ) { |
| 6060 | if (PyObject_SetItem(d, k, v) < 0) { |
| 6061 | Py_DECREF(di); |
| 6062 | return; |
| 6063 | } |
| 6064 | } |
| 6065 | Py_DECREF(di); |
Guido van Rossum | ebba420 | 2000-09-07 14:35:37 +0000 | [diff] [blame] | 6066 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 6067 | i = PyModule_AddIntConstant(m, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL); |
| 6068 | if (i < 0) |
| 6069 | return; |
Tim Peters | 8587b3c | 2003-02-13 15:44:41 +0000 | [diff] [blame] | 6070 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 6071 | /* These are purely informational; no code uses them. */ |
| 6072 | /* File format version we write. */ |
| 6073 | format_version = PyString_FromString("2.0"); |
| 6074 | /* Format versions we can read. */ |
| 6075 | compatible_formats = Py_BuildValue("[sssss]", |
| 6076 | "1.0", /* Original protocol 0 */ |
| 6077 | "1.1", /* Protocol 0 + INST */ |
| 6078 | "1.2", /* Original protocol 1 */ |
| 6079 | "1.3", /* Protocol 1 + BINFLOAT */ |
| 6080 | "2.0"); /* Original protocol 2 */ |
| 6081 | PyDict_SetItemString(d, "format_version", format_version); |
| 6082 | PyDict_SetItemString(d, "compatible_formats", compatible_formats); |
| 6083 | Py_XDECREF(format_version); |
| 6084 | Py_XDECREF(compatible_formats); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 6085 | } |