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 |
| 142 | int length; /* number of initial slots in data currently used */ |
| 143 | int size; /* number of slots in data allocated */ |
| 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 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 150 | int i; |
| 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 |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 196 | Pdata_clear(Pdata *self, int clearto) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 197 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 198 | int i; |
| 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 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 217 | int bigger; |
| 218 | size_t nbytes; |
| 219 | PyObject **tmp; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 220 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 221 | bigger = self->size << 1; |
| 222 | if (bigger <= 0) /* was 0, or new value overflows */ |
| 223 | goto nomemory; |
| 224 | if ((int)(size_t)bigger != bigger) |
| 225 | goto nomemory; |
| 226 | nbytes = (size_t)bigger * sizeof(PyObject *); |
| 227 | if (nbytes / sizeof(PyObject *) != (size_t)bigger) |
| 228 | goto nomemory; |
| 229 | tmp = realloc(self->data, nbytes); |
| 230 | if (tmp == NULL) |
| 231 | goto nomemory; |
| 232 | self->data = tmp; |
| 233 | self->size = bigger; |
| 234 | return 0; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 235 | |
| 236 | nomemory: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 237 | PyErr_NoMemory(); |
| 238 | return -1; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 239 | } |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 240 | |
Tim Peters | e0a3907 | 2003-02-03 15:45:56 +0000 | [diff] [blame] | 241 | /* D is a Pdata*. Pop the topmost element and store it into V, which |
| 242 | * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 243 | * is raised and V is set to NULL. D and V may be evaluated several times. |
| 244 | */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 245 | #define PDATA_POP(D, V) { \ |
| 246 | if ((D)->length) \ |
| 247 | (V) = (D)->data[--((D)->length)]; \ |
| 248 | else { \ |
| 249 | PyErr_SetString(UnpicklingError, "bad pickle data"); \ |
| 250 | (V) = NULL; \ |
| 251 | } \ |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Tim Peters | e0a3907 | 2003-02-03 15:45:56 +0000 | [diff] [blame] | 254 | /* PDATA_PUSH and PDATA_APPEND both push rvalue PyObject* O on to Pdata* |
| 255 | * D. If the Pdata stack can't be grown to hold the new value, both |
| 256 | * raise MemoryError and execute "return ER". The difference is in ownership |
| 257 | * of O after: _PUSH transfers ownership of O from the caller to the stack |
| 258 | * (no incref of O is done, and in case of error O is decrefed), while |
| 259 | * _APPEND pushes a new reference. |
| 260 | */ |
| 261 | |
| 262 | /* Push O on stack D, giving ownership of O to the stack. */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 263 | #define PDATA_PUSH(D, O, ER) { \ |
| 264 | if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \ |
| 265 | Pdata_grow((Pdata*)(D)) < 0) { \ |
| 266 | Py_DECREF(O); \ |
| 267 | return ER; \ |
| 268 | } \ |
| 269 | ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \ |
Tim Peters | e0a3907 | 2003-02-03 15:45:56 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | /* Push O on stack D, pushing a new reference. */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 273 | #define PDATA_APPEND(D, O, ER) { \ |
| 274 | if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \ |
| 275 | Pdata_grow((Pdata*)(D)) < 0) \ |
| 276 | return ER; \ |
| 277 | Py_INCREF(O); \ |
| 278 | ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \ |
Tim Peters | e0a3907 | 2003-02-03 15:45:56 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 282 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 283 | Pdata_popTuple(Pdata *self, int start) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 284 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 285 | PyObject *r; |
| 286 | int i, j, l; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 287 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 288 | l = self->length-start; |
| 289 | r = PyTuple_New(l); |
| 290 | if (r == NULL) |
| 291 | return NULL; |
| 292 | for (i = start, j = 0 ; j < l; i++, j++) |
| 293 | PyTuple_SET_ITEM(r, j, self->data[i]); |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 294 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 295 | self->length = start; |
| 296 | return r; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 300 | Pdata_popList(Pdata *self, int start) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 301 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 302 | PyObject *r; |
| 303 | int i, j, l; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 304 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 305 | l=self->length-start; |
| 306 | if (!( r=PyList_New(l))) return NULL; |
| 307 | for (i=start, j=0 ; j < l; i++, j++) |
| 308 | PyList_SET_ITEM(r, j, self->data[i]); |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 309 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 310 | self->length=start; |
| 311 | return r; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 314 | /*************************************************************************/ |
| 315 | |
| 316 | #define ARG_TUP(self, o) { \ |
| 317 | if (self->arg || (self->arg=PyTuple_New(1))) { \ |
| 318 | Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \ |
| 319 | PyTuple_SET_ITEM(self->arg,0,o); \ |
| 320 | } \ |
| 321 | else { \ |
| 322 | Py_DECREF(o); \ |
| 323 | } \ |
| 324 | } |
| 325 | |
| 326 | #define FREE_ARG_TUP(self) { \ |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 327 | if (Py_REFCNT(self->arg) > 1) { \ |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 328 | Py_DECREF(self->arg); \ |
| 329 | self->arg=NULL; \ |
| 330 | } \ |
| 331 | } |
| 332 | |
Thomas Wouters | 3b6448f | 2000-07-22 23:56:07 +0000 | [diff] [blame] | 333 | typedef struct Picklerobject { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 334 | PyObject_HEAD |
| 335 | FILE *fp; |
| 336 | PyObject *write; |
| 337 | PyObject *file; |
| 338 | PyObject *memo; |
| 339 | PyObject *arg; |
| 340 | PyObject *pers_func; |
| 341 | PyObject *inst_pers_func; |
Tim Peters | 797ec24 | 2003-02-01 06:22:36 +0000 | [diff] [blame] | 342 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 343 | /* pickle protocol number, >= 0 */ |
| 344 | int proto; |
Tim Peters | 797ec24 | 2003-02-01 06:22:36 +0000 | [diff] [blame] | 345 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 346 | /* bool, true if proto > 0 */ |
| 347 | int bin; |
Tim Peters | 797ec24 | 2003-02-01 06:22:36 +0000 | [diff] [blame] | 348 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 349 | int fast; /* Fast mode doesn't save in memo, don't use if circ ref */ |
| 350 | int (*write_func)(struct Picklerobject *, const char *, Py_ssize_t); |
| 351 | char *write_buf; |
| 352 | int buf_size; |
| 353 | PyObject *dispatch_table; |
| 354 | int fast_container; /* count nested container dumps */ |
| 355 | PyObject *fast_memo; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 356 | } Picklerobject; |
| 357 | |
Barry Warsaw | 52acb49 | 2001-12-21 20:04:22 +0000 | [diff] [blame] | 358 | #ifndef PY_CPICKLE_FAST_LIMIT |
| 359 | #define PY_CPICKLE_FAST_LIMIT 50 |
| 360 | #endif |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 361 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 362 | static PyTypeObject Picklertype; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 363 | |
Thomas Wouters | 3b6448f | 2000-07-22 23:56:07 +0000 | [diff] [blame] | 364 | typedef struct Unpicklerobject { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 365 | PyObject_HEAD |
| 366 | FILE *fp; |
| 367 | PyObject *file; |
| 368 | PyObject *readline; |
| 369 | PyObject *read; |
| 370 | PyObject *memo; |
| 371 | PyObject *arg; |
| 372 | Pdata *stack; |
| 373 | PyObject *mark; |
| 374 | PyObject *pers_func; |
| 375 | PyObject *last_string; |
| 376 | int *marks; |
| 377 | int num_marks; |
| 378 | int marks_size; |
| 379 | Py_ssize_t (*read_func)(struct Unpicklerobject *, char **, Py_ssize_t); |
| 380 | Py_ssize_t (*readline_func)(struct Unpicklerobject *, char **); |
| 381 | int buf_size; |
| 382 | char *buf; |
| 383 | PyObject *find_class; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 384 | } Unpicklerobject; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 385 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 386 | static PyTypeObject Unpicklertype; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 387 | |
Thomas Wouters | 4789b3a | 2000-07-24 11:36:47 +0000 | [diff] [blame] | 388 | /* Forward decls that need the above structs */ |
| 389 | static int save(Picklerobject *, PyObject *, int); |
| 390 | static int put2(Picklerobject *, PyObject *); |
| 391 | |
Guido van Rossum | d385d59 | 1997-04-09 17:47:47 +0000 | [diff] [blame] | 392 | static |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 393 | PyObject * |
Thomas Wouters | 3b6448f | 2000-07-22 23:56:07 +0000 | [diff] [blame] | 394 | cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...) |
| 395 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 396 | va_list va; |
| 397 | PyObject *args=0, *retval=0; |
| 398 | va_start(va, format); |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 399 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 400 | if (format) args = Py_VaBuildValue(format, va); |
| 401 | va_end(va); |
| 402 | if (format && ! args) return NULL; |
| 403 | if (stringformat && !(retval=PyString_FromString(stringformat))) |
| 404 | return NULL; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 405 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 406 | if (retval) { |
| 407 | if (args) { |
| 408 | PyObject *v; |
| 409 | v=PyString_Format(retval, args); |
| 410 | Py_DECREF(retval); |
| 411 | Py_DECREF(args); |
| 412 | if (! v) return NULL; |
| 413 | retval=v; |
| 414 | } |
| 415 | } |
| 416 | else |
| 417 | if (args) retval=args; |
| 418 | else { |
| 419 | PyErr_SetObject(ErrType,Py_None); |
| 420 | return NULL; |
| 421 | } |
| 422 | PyErr_SetObject(ErrType,retval); |
| 423 | Py_DECREF(retval); |
| 424 | return NULL; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 427 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 428 | 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] | 429 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 430 | size_t nbyteswritten; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 431 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 432 | if (s == NULL) { |
| 433 | return 0; |
| 434 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 435 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 436 | if (n > INT_MAX) { |
| 437 | /* String too large */ |
| 438 | return -1; |
| 439 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 440 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 441 | PyFile_IncUseCount((PyFileObject *)self->file); |
| 442 | Py_BEGIN_ALLOW_THREADS |
| 443 | nbyteswritten = fwrite(s, sizeof(char), n, self->fp); |
| 444 | Py_END_ALLOW_THREADS |
| 445 | PyFile_DecUseCount((PyFileObject *)self->file); |
| 446 | if (nbyteswritten != (size_t)n) { |
| 447 | PyErr_SetFromErrno(PyExc_IOError); |
| 448 | return -1; |
| 449 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 450 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 451 | return (int)n; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 454 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 455 | 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] | 456 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 457 | if (s == NULL) { |
| 458 | return 0; |
| 459 | } |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 460 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 461 | if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) { |
| 462 | return -1; |
| 463 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 464 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 465 | return (int)n; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 468 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 469 | 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] | 470 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 471 | if (s == NULL) return 0; |
| 472 | if (n > INT_MAX) return -1; |
| 473 | return (int)n; |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 476 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 477 | 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] | 478 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 479 | PyObject *py_str = 0, *junk = 0; |
| 480 | int n; |
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 (_n > INT_MAX) |
| 483 | return -1; |
| 484 | n = (int)_n; |
| 485 | if (s == NULL) { |
| 486 | if (!( self->buf_size )) return 0; |
| 487 | py_str = PyString_FromStringAndSize(self->write_buf, |
| 488 | self->buf_size); |
| 489 | if (!py_str) |
| 490 | return -1; |
| 491 | } |
| 492 | else { |
| 493 | if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) { |
| 494 | if (write_other(self, NULL, 0) < 0) |
| 495 | return -1; |
| 496 | } |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 497 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 498 | if (n > WRITE_BUF_SIZE) { |
| 499 | if (!( py_str = |
| 500 | PyString_FromStringAndSize(s, n))) |
| 501 | return -1; |
| 502 | } |
| 503 | else { |
| 504 | memcpy(self->write_buf + self->buf_size, s, n); |
| 505 | self->buf_size += n; |
| 506 | return n; |
| 507 | } |
| 508 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 509 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 510 | if (self->write) { |
| 511 | /* object with write method */ |
| 512 | ARG_TUP(self, py_str); |
| 513 | if (self->arg) { |
| 514 | junk = PyObject_Call(self->write, self->arg, NULL); |
| 515 | FREE_ARG_TUP(self); |
| 516 | } |
| 517 | if (junk) Py_DECREF(junk); |
| 518 | else return -1; |
| 519 | } |
| 520 | else |
| 521 | PDATA_PUSH(self->file, py_str, -1); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 522 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 523 | self->buf_size = 0; |
| 524 | return n; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 528 | static Py_ssize_t |
| 529 | read_file(Unpicklerobject *self, char **s, Py_ssize_t n) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 530 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 531 | size_t nbytesread; |
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 | if (self->buf_size == 0) { |
| 534 | int size; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 535 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 536 | size = ((n < 32) ? 32 : n); |
| 537 | if (!( self->buf = (char *)malloc(size))) { |
| 538 | PyErr_NoMemory(); |
| 539 | return -1; |
| 540 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 541 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 542 | self->buf_size = size; |
| 543 | } |
| 544 | else if (n > self->buf_size) { |
| 545 | char *newbuf = (char *)realloc(self->buf, n); |
| 546 | if (!newbuf) { |
| 547 | PyErr_NoMemory(); |
| 548 | return -1; |
| 549 | } |
| 550 | self->buf = newbuf; |
| 551 | self->buf_size = n; |
| 552 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 553 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 554 | PyFile_IncUseCount((PyFileObject *)self->file); |
| 555 | Py_BEGIN_ALLOW_THREADS |
| 556 | nbytesread = fread(self->buf, sizeof(char), n, self->fp); |
| 557 | Py_END_ALLOW_THREADS |
| 558 | PyFile_DecUseCount((PyFileObject *)self->file); |
| 559 | if (nbytesread != (size_t)n) { |
| 560 | if (feof(self->fp)) { |
| 561 | PyErr_SetNone(PyExc_EOFError); |
| 562 | return -1; |
| 563 | } |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 564 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 565 | PyErr_SetFromErrno(PyExc_IOError); |
| 566 | return -1; |
| 567 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 568 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 569 | *s = self->buf; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 570 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 571 | return n; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 575 | static Py_ssize_t |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 576 | readline_file(Unpicklerobject *self, char **s) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 577 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 578 | int i; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 579 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 580 | if (self->buf_size == 0) { |
| 581 | if (!( self->buf = (char *)malloc(40))) { |
| 582 | PyErr_NoMemory(); |
| 583 | return -1; |
| 584 | } |
| 585 | self->buf_size = 40; |
| 586 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 587 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 588 | i = 0; |
| 589 | while (1) { |
| 590 | int bigger; |
| 591 | char *newbuf; |
| 592 | for (; i < (self->buf_size - 1); i++) { |
| 593 | if (feof(self->fp) || |
| 594 | (self->buf[i] = getc(self->fp)) == '\n') { |
| 595 | self->buf[i + 1] = '\0'; |
| 596 | *s = self->buf; |
| 597 | return i + 1; |
| 598 | } |
| 599 | } |
| 600 | bigger = self->buf_size << 1; |
| 601 | if (bigger <= 0) { /* overflow */ |
| 602 | PyErr_NoMemory(); |
| 603 | return -1; |
| 604 | } |
| 605 | newbuf = (char *)realloc(self->buf, bigger); |
| 606 | if (!newbuf) { |
| 607 | PyErr_NoMemory(); |
| 608 | return -1; |
| 609 | } |
| 610 | self->buf = newbuf; |
| 611 | self->buf_size = bigger; |
| 612 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 613 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 614 | |
| 615 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 616 | static Py_ssize_t |
| 617 | read_cStringIO(Unpicklerobject *self, char **s, Py_ssize_t n) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 618 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 619 | char *ptr; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 620 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 621 | if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) { |
| 622 | PyErr_SetNone(PyExc_EOFError); |
| 623 | return -1; |
| 624 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 625 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 626 | *s = ptr; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 627 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 628 | return n; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 632 | static Py_ssize_t |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 633 | readline_cStringIO(Unpicklerobject *self, char **s) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 634 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 635 | Py_ssize_t n; |
| 636 | char *ptr; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 637 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 638 | if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) { |
| 639 | return -1; |
| 640 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 641 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 642 | *s = ptr; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 643 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 644 | return n; |
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 |
| 649 | read_other(Unpicklerobject *self, char **s, Py_ssize_t n) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 650 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 651 | PyObject *bytes, *str=0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 652 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 653 | if (!( bytes = PyInt_FromSsize_t(n))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 654 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 655 | ARG_TUP(self, bytes); |
| 656 | if (self->arg) { |
| 657 | str = PyObject_Call(self->read, self->arg, NULL); |
| 658 | FREE_ARG_TUP(self); |
| 659 | } |
| 660 | if (! str) return -1; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 661 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 662 | Py_XDECREF(self->last_string); |
| 663 | self->last_string = str; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 664 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 665 | if (! (*s = PyString_AsString(str))) return -1; |
Amaury Forgeot d'Arc | 74b3016 | 2009-07-23 19:26:02 +0000 | [diff] [blame] | 666 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 667 | if (PyString_GET_SIZE(str) != n) { |
| 668 | PyErr_SetNone(PyExc_EOFError); |
| 669 | return -1; |
| 670 | } |
Amaury Forgeot d'Arc | 74b3016 | 2009-07-23 19:26:02 +0000 | [diff] [blame] | 671 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 672 | return n; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 676 | static Py_ssize_t |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 677 | readline_other(Unpicklerobject *self, char **s) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 678 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 679 | PyObject *str; |
| 680 | Py_ssize_t str_size; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 681 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 682 | if (!( str = PyObject_CallObject(self->readline, empty_tuple))) { |
| 683 | return -1; |
| 684 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 685 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 686 | if ((str_size = PyString_Size(str)) < 0) |
| 687 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 688 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 689 | Py_XDECREF(self->last_string); |
| 690 | self->last_string = str; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 691 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 692 | if (! (*s = PyString_AsString(str))) |
| 693 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 694 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 695 | return str_size; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 696 | } |
| 697 | |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 698 | /* Copy the first n bytes from s into newly malloc'ed memory, plus a |
| 699 | * trailing 0 byte. Return a pointer to that, or NULL if out of memory. |
| 700 | * The caller is responsible for free()'ing the return value. |
| 701 | */ |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 702 | static char * |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 703 | pystrndup(const char *s, int n) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 704 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 705 | char *r = (char *)malloc(n+1); |
| 706 | if (r == NULL) |
| 707 | return (char*)PyErr_NoMemory(); |
| 708 | memcpy(r, s, n); |
| 709 | r[n] = 0; |
| 710 | return r; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | |
| 714 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 715 | get(Picklerobject *self, PyObject *id) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 716 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 717 | PyObject *value, *mv; |
| 718 | long c_value; |
| 719 | char s[30]; |
| 720 | size_t len; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 721 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 722 | if (!( mv = PyDict_GetItem(self->memo, id))) { |
| 723 | PyErr_SetObject(PyExc_KeyError, id); |
| 724 | return -1; |
| 725 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 726 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 727 | if (!( value = PyTuple_GetItem(mv, 0))) |
| 728 | return -1; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 729 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 730 | if (!( PyInt_Check(value))) { |
| 731 | PyErr_SetString(PicklingError, "no int where int expected in memo"); |
| 732 | return -1; |
| 733 | } |
| 734 | c_value = PyInt_AS_LONG((PyIntObject*)value); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 735 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 736 | if (!self->bin) { |
| 737 | s[0] = GET; |
| 738 | PyOS_snprintf(s + 1, sizeof(s) - 1, "%ld\n", c_value); |
| 739 | len = strlen(s); |
| 740 | } |
| 741 | else if (Pdata_Check(self->file)) { |
| 742 | if (write_other(self, NULL, 0) < 0) return -1; |
| 743 | PDATA_APPEND(self->file, mv, -1); |
| 744 | return 0; |
| 745 | } |
| 746 | else { |
| 747 | if (c_value < 256) { |
| 748 | s[0] = BINGET; |
| 749 | s[1] = (int)(c_value & 0xff); |
| 750 | len = 2; |
| 751 | } |
| 752 | else { |
| 753 | s[0] = LONG_BINGET; |
| 754 | s[1] = (int)(c_value & 0xff); |
| 755 | s[2] = (int)((c_value >> 8) & 0xff); |
| 756 | s[3] = (int)((c_value >> 16) & 0xff); |
| 757 | s[4] = (int)((c_value >> 24) & 0xff); |
| 758 | len = 5; |
| 759 | } |
| 760 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 761 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 762 | if (self->write_func(self, s, len) < 0) |
| 763 | return -1; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 764 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 765 | return 0; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 766 | } |
Jeremy Hylton | ce616e4 | 1998-08-13 23:13:52 +0000 | [diff] [blame] | 767 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 768 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 769 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 770 | put(Picklerobject *self, PyObject *ob) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 771 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 772 | if (Py_REFCNT(ob) < 2 || self->fast) |
| 773 | return 0; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 774 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 775 | return put2(self, ob); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 776 | } |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 777 | |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 778 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 779 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 780 | put2(Picklerobject *self, PyObject *ob) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 781 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 782 | char c_str[30]; |
| 783 | int p; |
| 784 | size_t len; |
| 785 | int res = -1; |
| 786 | PyObject *py_ob_id = 0, *memo_len = 0, *t = 0; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 787 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 788 | if (self->fast) |
| 789 | return 0; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 790 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 791 | if ((p = PyDict_Size(self->memo)) < 0) |
| 792 | goto finally; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 793 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 794 | /* Make sure memo keys are positive! */ |
| 795 | /* XXX Why? |
| 796 | * XXX And does "positive" really mean non-negative? |
| 797 | * XXX pickle.py starts with PUT index 0, not 1. This makes for |
| 798 | * XXX gratuitous differences between the pickling modules. |
| 799 | */ |
| 800 | p++; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 801 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 802 | if (!( py_ob_id = PyLong_FromVoidPtr(ob))) |
| 803 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 804 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 805 | if (!( memo_len = PyInt_FromLong(p))) |
| 806 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 807 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 808 | if (!( t = PyTuple_New(2))) |
| 809 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 810 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 811 | PyTuple_SET_ITEM(t, 0, memo_len); |
| 812 | Py_INCREF(memo_len); |
| 813 | PyTuple_SET_ITEM(t, 1, ob); |
| 814 | Py_INCREF(ob); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 815 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 816 | if (PyDict_SetItem(self->memo, py_ob_id, t) < 0) |
| 817 | goto finally; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 818 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 819 | if (!self->bin) { |
| 820 | c_str[0] = PUT; |
| 821 | PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%d\n", p); |
| 822 | len = strlen(c_str); |
| 823 | } |
| 824 | else if (Pdata_Check(self->file)) { |
| 825 | if (write_other(self, NULL, 0) < 0) return -1; |
| 826 | PDATA_APPEND(self->file, memo_len, -1); |
| 827 | res=0; /* Job well done ;) */ |
| 828 | goto finally; |
| 829 | } |
| 830 | else { |
| 831 | if (p >= 256) { |
| 832 | c_str[0] = LONG_BINPUT; |
| 833 | c_str[1] = (int)(p & 0xff); |
| 834 | c_str[2] = (int)((p >> 8) & 0xff); |
| 835 | c_str[3] = (int)((p >> 16) & 0xff); |
| 836 | c_str[4] = (int)((p >> 24) & 0xff); |
| 837 | len = 5; |
| 838 | } |
| 839 | else { |
| 840 | c_str[0] = BINPUT; |
| 841 | c_str[1] = p; |
| 842 | len = 2; |
| 843 | } |
| 844 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 845 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 846 | if (self->write_func(self, c_str, len) < 0) |
| 847 | goto finally; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 848 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 849 | res = 0; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 850 | |
| 851 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 852 | Py_XDECREF(py_ob_id); |
| 853 | Py_XDECREF(memo_len); |
| 854 | Py_XDECREF(t); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 855 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 856 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 857 | } |
| 858 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 859 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 860 | whichmodule(PyObject *global, PyObject *global_name) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 861 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 862 | Py_ssize_t i, j; |
| 863 | PyObject *module = 0, *modules_dict = 0, |
| 864 | *global_name_attr = 0, *name = 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 865 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 866 | module = PyObject_GetAttrString(global, "__module__"); |
| 867 | if (module) |
| 868 | return module; |
| 869 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 870 | PyErr_Clear(); |
| 871 | else |
| 872 | return NULL; |
Guido van Rossum | 4518823 | 1997-09-28 05:38:51 +0000 | [diff] [blame] | 873 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 874 | if (!( modules_dict = PySys_GetObject("modules"))) |
| 875 | return NULL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 876 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 877 | i = 0; |
| 878 | while ((j = PyDict_Next(modules_dict, &i, &name, &module))) { |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 879 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 880 | if (PyObject_Compare(name, __main___str)==0) continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 881 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 882 | global_name_attr = PyObject_GetAttr(module, global_name); |
| 883 | if (!global_name_attr) { |
| 884 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 885 | PyErr_Clear(); |
| 886 | else |
| 887 | return NULL; |
| 888 | continue; |
| 889 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 890 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 891 | if (global_name_attr != global) { |
| 892 | Py_DECREF(global_name_attr); |
| 893 | continue; |
| 894 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 895 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 896 | Py_DECREF(global_name_attr); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 897 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 898 | break; |
| 899 | } |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 900 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 901 | /* The following implements the rule in pickle.py added in 1.5 |
| 902 | that used __main__ if no module is found. I don't actually |
| 903 | like this rule. jlf |
| 904 | */ |
| 905 | if (!j) { |
| 906 | name=__main___str; |
| 907 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 908 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 909 | Py_INCREF(name); |
| 910 | return name; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 914 | static int |
Jeremy Hylton | 499ab6a | 2001-10-15 21:37:58 +0000 | [diff] [blame] | 915 | fast_save_enter(Picklerobject *self, PyObject *obj) |
| 916 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 917 | /* if fast_container < 0, we're doing an error exit. */ |
| 918 | if (++self->fast_container >= PY_CPICKLE_FAST_LIMIT) { |
| 919 | PyObject *key = NULL; |
| 920 | if (self->fast_memo == NULL) { |
| 921 | self->fast_memo = PyDict_New(); |
| 922 | if (self->fast_memo == NULL) { |
| 923 | self->fast_container = -1; |
| 924 | return 0; |
| 925 | } |
| 926 | } |
| 927 | key = PyLong_FromVoidPtr(obj); |
| 928 | if (key == NULL) |
| 929 | return 0; |
| 930 | if (PyDict_GetItem(self->fast_memo, key)) { |
| 931 | Py_DECREF(key); |
| 932 | PyErr_Format(PyExc_ValueError, |
| 933 | "fast mode: can't pickle cyclic objects " |
| 934 | "including object type %s at %p", |
| 935 | Py_TYPE(obj)->tp_name, obj); |
| 936 | self->fast_container = -1; |
| 937 | return 0; |
| 938 | } |
| 939 | if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) { |
| 940 | Py_DECREF(key); |
| 941 | self->fast_container = -1; |
| 942 | return 0; |
| 943 | } |
| 944 | Py_DECREF(key); |
| 945 | } |
| 946 | return 1; |
Jeremy Hylton | 499ab6a | 2001-10-15 21:37:58 +0000 | [diff] [blame] | 947 | } |
| 948 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 949 | int |
Jeremy Hylton | 499ab6a | 2001-10-15 21:37:58 +0000 | [diff] [blame] | 950 | fast_save_leave(Picklerobject *self, PyObject *obj) |
| 951 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 952 | if (self->fast_container-- >= PY_CPICKLE_FAST_LIMIT) { |
| 953 | PyObject *key = PyLong_FromVoidPtr(obj); |
| 954 | if (key == NULL) |
| 955 | return 0; |
| 956 | if (PyDict_DelItem(self->fast_memo, key) < 0) { |
| 957 | Py_DECREF(key); |
| 958 | return 0; |
| 959 | } |
| 960 | Py_DECREF(key); |
| 961 | } |
| 962 | return 1; |
Jeremy Hylton | 499ab6a | 2001-10-15 21:37:58 +0000 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 966 | save_none(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 967 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 968 | static char none = NONE; |
| 969 | if (self->write_func(self, &none, 1) < 0) |
| 970 | return -1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 971 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 972 | return 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 973 | } |
| 974 | |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 975 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 976 | save_bool(Picklerobject *self, PyObject *args) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 977 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 978 | static const char *buf[2] = {FALSE, TRUE}; |
| 979 | static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1}; |
| 980 | long l = PyInt_AS_LONG((PyIntObject *)args); |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 981 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 982 | if (self->proto >= 2) { |
| 983 | char opcode = l ? NEWTRUE : NEWFALSE; |
| 984 | if (self->write_func(self, &opcode, 1) < 0) |
| 985 | return -1; |
| 986 | } |
| 987 | else if (self->write_func(self, buf[l], len[l]) < 0) |
| 988 | return -1; |
| 989 | return 0; |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 990 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 991 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 992 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 993 | save_int(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 994 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 995 | char c_str[32]; |
| 996 | long l = PyInt_AS_LONG((PyIntObject *)args); |
| 997 | int len = 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 998 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 999 | if (!self->bin |
Guido van Rossum | de8d6d7 | 1997-05-13 18:00:44 +0000 | [diff] [blame] | 1000 | #if SIZEOF_LONG > 4 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1001 | || l > 0x7fffffffL |
| 1002 | || l < -0x80000000L |
Guido van Rossum | de8d6d7 | 1997-05-13 18:00:44 +0000 | [diff] [blame] | 1003 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1004 | ) { |
| 1005 | /* Text-mode pickle, or long too big to fit in the 4-byte |
| 1006 | * signed BININT format: store as a string. |
| 1007 | */ |
| 1008 | c_str[0] = INT; |
| 1009 | PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%ld\n", l); |
| 1010 | if (self->write_func(self, c_str, strlen(c_str)) < 0) |
| 1011 | return -1; |
| 1012 | } |
| 1013 | else { |
| 1014 | /* Binary pickle and l fits in a signed 4-byte int. */ |
| 1015 | c_str[1] = (int)( l & 0xff); |
| 1016 | c_str[2] = (int)((l >> 8) & 0xff); |
| 1017 | c_str[3] = (int)((l >> 16) & 0xff); |
| 1018 | c_str[4] = (int)((l >> 24) & 0xff); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1019 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1020 | if ((c_str[4] == 0) && (c_str[3] == 0)) { |
| 1021 | if (c_str[2] == 0) { |
| 1022 | c_str[0] = BININT1; |
| 1023 | len = 2; |
| 1024 | } |
| 1025 | else { |
| 1026 | c_str[0] = BININT2; |
| 1027 | len = 3; |
| 1028 | } |
| 1029 | } |
| 1030 | else { |
| 1031 | c_str[0] = BININT; |
| 1032 | len = 5; |
| 1033 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1034 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1035 | if (self->write_func(self, c_str, len) < 0) |
| 1036 | return -1; |
| 1037 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1038 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1039 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | |
| 1043 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1044 | save_long(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1045 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1046 | Py_ssize_t size; |
| 1047 | int res = -1; |
| 1048 | PyObject *repr = NULL; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1049 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1050 | static char l = LONG; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1051 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1052 | if (self->proto >= 2) { |
| 1053 | /* Linear-time pickling. */ |
| 1054 | size_t nbits; |
| 1055 | size_t nbytes; |
| 1056 | unsigned char *pdata; |
| 1057 | char c_str[5]; |
| 1058 | int i; |
| 1059 | int sign = _PyLong_Sign(args); |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 1060 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1061 | if (sign == 0) { |
| 1062 | /* It's 0 -- an empty bytestring. */ |
| 1063 | c_str[0] = LONG1; |
| 1064 | c_str[1] = 0; |
| 1065 | i = self->write_func(self, c_str, 2); |
| 1066 | if (i < 0) goto finally; |
| 1067 | res = 0; |
| 1068 | goto finally; |
| 1069 | } |
| 1070 | nbits = _PyLong_NumBits(args); |
| 1071 | if (nbits == (size_t)-1 && PyErr_Occurred()) |
| 1072 | goto finally; |
| 1073 | /* How many bytes do we need? There are nbits >> 3 full |
| 1074 | * bytes of data, and nbits & 7 leftover bits. If there |
| 1075 | * are any leftover bits, then we clearly need another |
| 1076 | * byte. Wnat's not so obvious is that we *probably* |
| 1077 | * need another byte even if there aren't any leftovers: |
| 1078 | * the most-significant bit of the most-significant byte |
| 1079 | * acts like a sign bit, and it's usually got a sense |
| 1080 | * opposite of the one we need. The exception is longs |
| 1081 | * of the form -(2**(8*j-1)) for j > 0. Such a long is |
| 1082 | * its own 256's-complement, so has the right sign bit |
| 1083 | * even without the extra byte. That's a pain to check |
| 1084 | * for in advance, though, so we always grab an extra |
| 1085 | * byte at the start, and cut it back later if possible. |
| 1086 | */ |
| 1087 | nbytes = (nbits >> 3) + 1; |
| 1088 | if (nbytes > INT_MAX) { |
| 1089 | PyErr_SetString(PyExc_OverflowError, "long too large " |
| 1090 | "to pickle"); |
| 1091 | goto finally; |
| 1092 | } |
| 1093 | repr = PyString_FromStringAndSize(NULL, (int)nbytes); |
| 1094 | if (repr == NULL) goto finally; |
| 1095 | pdata = (unsigned char *)PyString_AS_STRING(repr); |
| 1096 | i = _PyLong_AsByteArray((PyLongObject *)args, |
| 1097 | pdata, nbytes, |
| 1098 | 1 /* little endian */, 1 /* signed */); |
| 1099 | if (i < 0) goto finally; |
| 1100 | /* If the long is negative, this may be a byte more than |
| 1101 | * needed. This is so iff the MSB is all redundant sign |
| 1102 | * bits. |
| 1103 | */ |
| 1104 | if (sign < 0 && nbytes > 1 && pdata[nbytes - 1] == 0xff && |
| 1105 | (pdata[nbytes - 2] & 0x80) != 0) |
| 1106 | --nbytes; |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 1107 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1108 | if (nbytes < 256) { |
| 1109 | c_str[0] = LONG1; |
| 1110 | c_str[1] = (char)nbytes; |
| 1111 | size = 2; |
| 1112 | } |
| 1113 | else { |
| 1114 | c_str[0] = LONG4; |
| 1115 | size = (int)nbytes; |
| 1116 | for (i = 1; i < 5; i++) { |
| 1117 | c_str[i] = (char)(size & 0xff); |
| 1118 | size >>= 8; |
| 1119 | } |
| 1120 | size = 5; |
| 1121 | } |
| 1122 | i = self->write_func(self, c_str, size); |
| 1123 | if (i < 0) goto finally; |
| 1124 | i = self->write_func(self, (char *)pdata, (int)nbytes); |
| 1125 | if (i < 0) goto finally; |
| 1126 | res = 0; |
| 1127 | goto finally; |
| 1128 | } |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 1129 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1130 | /* proto < 2: write the repr and newline. This is quadratic-time |
| 1131 | * (in the number of digits), in both directions. |
| 1132 | */ |
| 1133 | if (!( repr = PyObject_Repr(args))) |
| 1134 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1135 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1136 | if ((size = PyString_Size(repr)) < 0) |
| 1137 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1138 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1139 | if (self->write_func(self, &l, 1) < 0) |
| 1140 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1141 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1142 | if (self->write_func(self, |
| 1143 | PyString_AS_STRING((PyStringObject *)repr), |
| 1144 | size) < 0) |
| 1145 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1146 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1147 | if (self->write_func(self, "\n", 1) < 0) |
| 1148 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1149 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1150 | res = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1151 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1152 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1153 | Py_XDECREF(repr); |
| 1154 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | |
| 1158 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1159 | save_float(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1160 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1161 | double x = PyFloat_AS_DOUBLE((PyFloatObject *)args); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1162 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1163 | if (self->bin) { |
| 1164 | char str[9]; |
| 1165 | str[0] = BINFLOAT; |
| 1166 | if (_PyFloat_Pack8(x, (unsigned char *)&str[1], 0) < 0) |
| 1167 | return -1; |
| 1168 | if (self->write_func(self, str, 9) < 0) |
| 1169 | return -1; |
| 1170 | } |
| 1171 | else { |
| 1172 | int result = -1; |
| 1173 | char *buf = NULL; |
| 1174 | char op = FLOAT; |
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, &op, 1) < 0) |
| 1177 | goto done; |
Eric Smith | b05d3be | 2009-10-26 15:06:39 +0000 | [diff] [blame] | 1178 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1179 | buf = PyOS_double_to_string(x, 'g', 17, 0, NULL); |
| 1180 | if (!buf) { |
| 1181 | PyErr_NoMemory(); |
| 1182 | goto done; |
| 1183 | } |
Eric Smith | b05d3be | 2009-10-26 15:06:39 +0000 | [diff] [blame] | 1184 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1185 | if (self->write_func(self, buf, strlen(buf)) < 0) |
| 1186 | goto done; |
Eric Smith | b05d3be | 2009-10-26 15:06:39 +0000 | [diff] [blame] | 1187 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1188 | if (self->write_func(self, "\n", 1) < 0) |
| 1189 | goto done; |
Eric Smith | b05d3be | 2009-10-26 15:06:39 +0000 | [diff] [blame] | 1190 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1191 | result = 0; |
Eric Smith | b05d3be | 2009-10-26 15:06:39 +0000 | [diff] [blame] | 1192 | done: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1193 | PyMem_Free(buf); |
| 1194 | return result; |
| 1195 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1196 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1197 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | |
| 1201 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1202 | save_string(Picklerobject *self, PyObject *args, int doput) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1203 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1204 | int size, len; |
| 1205 | PyObject *repr=0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1206 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1207 | if ((size = PyString_Size(args)) < 0) |
| 1208 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1209 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1210 | if (!self->bin) { |
| 1211 | char *repr_str; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1212 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1213 | static char string = STRING; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1214 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1215 | if (!( repr = PyObject_Repr(args))) |
| 1216 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1217 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1218 | if ((len = PyString_Size(repr)) < 0) |
| 1219 | goto err; |
| 1220 | repr_str = PyString_AS_STRING((PyStringObject *)repr); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1221 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1222 | if (self->write_func(self, &string, 1) < 0) |
| 1223 | goto err; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1224 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1225 | if (self->write_func(self, repr_str, len) < 0) |
| 1226 | goto err; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1227 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1228 | if (self->write_func(self, "\n", 1) < 0) |
| 1229 | goto err; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1230 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1231 | Py_XDECREF(repr); |
| 1232 | } |
| 1233 | else { |
| 1234 | int i; |
| 1235 | char c_str[5]; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1236 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1237 | if (size < 256) { |
| 1238 | c_str[0] = SHORT_BINSTRING; |
| 1239 | c_str[1] = size; |
| 1240 | len = 2; |
| 1241 | } |
| 1242 | else if (size <= INT_MAX) { |
| 1243 | c_str[0] = BINSTRING; |
| 1244 | for (i = 1; i < 5; i++) |
| 1245 | c_str[i] = (int)(size >> ((i - 1) * 8)); |
| 1246 | len = 5; |
| 1247 | } |
| 1248 | else |
| 1249 | return -1; /* string too large */ |
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, c_str, len) < 0) |
| 1252 | return -1; |
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 (size > 128 && Pdata_Check(self->file)) { |
| 1255 | if (write_other(self, NULL, 0) < 0) return -1; |
| 1256 | PDATA_APPEND(self->file, args, -1); |
| 1257 | } |
| 1258 | else { |
| 1259 | if (self->write_func(self, |
| 1260 | PyString_AS_STRING( |
| 1261 | (PyStringObject *)args), |
| 1262 | size) < 0) |
| 1263 | return -1; |
| 1264 | } |
| 1265 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1266 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1267 | if (doput) |
| 1268 | if (put(self, args) < 0) |
| 1269 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1270 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1271 | return 0; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 1272 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1273 | err: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1274 | Py_XDECREF(repr); |
| 1275 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
| 1278 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1279 | #ifdef Py_USING_UNICODE |
Guido van Rossum | fb10c3f | 2000-12-19 02:08:38 +0000 | [diff] [blame] | 1280 | /* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates |
| 1281 | backslash and newline characters to \uXXXX escapes. */ |
| 1282 | static PyObject * |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1283 | modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, Py_ssize_t size) |
Guido van Rossum | fb10c3f | 2000-12-19 02:08:38 +0000 | [diff] [blame] | 1284 | { |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1285 | PyObject *repr; |
| 1286 | char *p; |
| 1287 | char *q; |
Guido van Rossum | fb10c3f | 2000-12-19 02:08:38 +0000 | [diff] [blame] | 1288 | |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1289 | static const char *hexdigit = "0123456789abcdef"; |
| 1290 | #ifdef Py_UNICODE_WIDE |
| 1291 | const Py_ssize_t expandsize = 10; |
| 1292 | #else |
| 1293 | const Py_ssize_t expandsize = 6; |
| 1294 | #endif |
Guido van Rossum | fb10c3f | 2000-12-19 02:08:38 +0000 | [diff] [blame] | 1295 | |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1296 | if (size > PY_SSIZE_T_MAX / expandsize) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1297 | return PyErr_NoMemory(); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1298 | |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1299 | repr = PyString_FromStringAndSize(NULL, expandsize * size); |
| 1300 | if (repr == NULL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1301 | return NULL; |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1302 | if (size == 0) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1303 | return repr; |
Guido van Rossum | fb10c3f | 2000-12-19 02:08:38 +0000 | [diff] [blame] | 1304 | |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1305 | p = q = PyString_AS_STRING(repr); |
| 1306 | while (size-- > 0) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1307 | Py_UNICODE ch = *s++; |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1308 | #ifdef Py_UNICODE_WIDE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1309 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 1310 | if (ch >= 0x10000) { |
| 1311 | *p++ = '\\'; |
| 1312 | *p++ = 'U'; |
| 1313 | *p++ = hexdigit[(ch >> 28) & 0xf]; |
| 1314 | *p++ = hexdigit[(ch >> 24) & 0xf]; |
| 1315 | *p++ = hexdigit[(ch >> 20) & 0xf]; |
| 1316 | *p++ = hexdigit[(ch >> 16) & 0xf]; |
| 1317 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 1318 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 1319 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 1320 | *p++ = hexdigit[ch & 15]; |
| 1321 | } |
| 1322 | else |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1323 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1324 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 1325 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 1326 | Py_UNICODE ch2; |
| 1327 | Py_UCS4 ucs; |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1328 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1329 | ch2 = *s++; |
| 1330 | size--; |
| 1331 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 1332 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 1333 | *p++ = '\\'; |
| 1334 | *p++ = 'U'; |
| 1335 | *p++ = hexdigit[(ucs >> 28) & 0xf]; |
| 1336 | *p++ = hexdigit[(ucs >> 24) & 0xf]; |
| 1337 | *p++ = hexdigit[(ucs >> 20) & 0xf]; |
| 1338 | *p++ = hexdigit[(ucs >> 16) & 0xf]; |
| 1339 | *p++ = hexdigit[(ucs >> 12) & 0xf]; |
| 1340 | *p++ = hexdigit[(ucs >> 8) & 0xf]; |
| 1341 | *p++ = hexdigit[(ucs >> 4) & 0xf]; |
| 1342 | *p++ = hexdigit[ucs & 0xf]; |
| 1343 | continue; |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1344 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1345 | /* Fall through: isolated surrogates are copied as-is */ |
| 1346 | s--; |
| 1347 | size++; |
| 1348 | } |
| 1349 | #endif |
| 1350 | /* Map 16-bit characters to '\uxxxx' */ |
| 1351 | if (ch >= 256 || ch == '\\' || ch == '\n') { |
| 1352 | *p++ = '\\'; |
| 1353 | *p++ = 'u'; |
| 1354 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 1355 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 1356 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 1357 | *p++ = hexdigit[ch & 15]; |
| 1358 | } |
| 1359 | /* Copy everything else as-is */ |
| 1360 | else |
| 1361 | *p++ = (char) ch; |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1362 | } |
| 1363 | *p = '\0'; |
| 1364 | _PyString_Resize(&repr, p - q); |
| 1365 | return repr; |
| 1366 | } |
Guido van Rossum | fb10c3f | 2000-12-19 02:08:38 +0000 | [diff] [blame] | 1367 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1368 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1369 | save_unicode(Picklerobject *self, PyObject *args, int doput) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1370 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1371 | Py_ssize_t size, len; |
| 1372 | PyObject *repr=0; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1373 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1374 | if (!PyUnicode_Check(args)) |
| 1375 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1376 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1377 | if (!self->bin) { |
| 1378 | char *repr_str; |
| 1379 | static char string = UNICODE; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1380 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1381 | repr = modified_EncodeRawUnicodeEscape( |
| 1382 | PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args)); |
| 1383 | if (!repr) |
| 1384 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1385 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1386 | if ((len = PyString_Size(repr)) < 0) |
| 1387 | goto err; |
| 1388 | repr_str = PyString_AS_STRING((PyStringObject *)repr); |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1389 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1390 | if (self->write_func(self, &string, 1) < 0) |
| 1391 | goto err; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1392 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1393 | if (self->write_func(self, repr_str, len) < 0) |
| 1394 | goto err; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1395 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1396 | if (self->write_func(self, "\n", 1) < 0) |
| 1397 | goto err; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1398 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1399 | Py_XDECREF(repr); |
| 1400 | } |
| 1401 | else { |
| 1402 | int i; |
| 1403 | char c_str[5]; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1404 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1405 | if (!( repr = PyUnicode_AsUTF8String(args))) |
| 1406 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1407 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1408 | if ((size = PyString_Size(repr)) < 0) |
| 1409 | goto err; |
| 1410 | if (size > INT_MAX) |
| 1411 | return -1; /* string too large */ |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1412 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1413 | c_str[0] = BINUNICODE; |
| 1414 | for (i = 1; i < 5; i++) |
| 1415 | c_str[i] = (int)(size >> ((i - 1) * 8)); |
| 1416 | len = 5; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1417 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1418 | if (self->write_func(self, c_str, len) < 0) |
| 1419 | goto err; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1420 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1421 | if (size > 128 && Pdata_Check(self->file)) { |
| 1422 | if (write_other(self, NULL, 0) < 0) |
| 1423 | goto err; |
| 1424 | PDATA_APPEND(self->file, repr, -1); |
| 1425 | } |
| 1426 | else { |
| 1427 | if (self->write_func(self, PyString_AS_STRING(repr), |
| 1428 | size) < 0) |
| 1429 | goto err; |
| 1430 | } |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1431 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1432 | Py_DECREF(repr); |
| 1433 | } |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1434 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1435 | if (doput) |
| 1436 | if (put(self, args) < 0) |
| 1437 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1438 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1439 | return 0; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1440 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1441 | err: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1442 | Py_XDECREF(repr); |
| 1443 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1444 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1445 | #endif |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1446 | |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1447 | /* A helper for save_tuple. Push the len elements in tuple t on the stack. */ |
| 1448 | static int |
Tim Peters | 6792014 | 2003-02-05 03:46:17 +0000 | [diff] [blame] | 1449 | store_tuple_elements(Picklerobject *self, PyObject *t, int len) |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1450 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1451 | int i; |
| 1452 | int res = -1; /* guilty until proved innocent */ |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1453 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1454 | assert(PyTuple_Size(t) == len); |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1455 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1456 | for (i = 0; i < len; i++) { |
| 1457 | PyObject *element = PyTuple_GET_ITEM(t, i); |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1458 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1459 | if (element == NULL) |
| 1460 | goto finally; |
| 1461 | if (save(self, element, 0) < 0) |
| 1462 | goto finally; |
| 1463 | } |
| 1464 | res = 0; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1465 | |
| 1466 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1467 | return res; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1468 | } |
| 1469 | |
| 1470 | /* Tuples are ubiquitous in the pickle protocols, so many techniques are |
| 1471 | * used across protocols to minimize the space needed to pickle them. |
Tim Peters | 6792014 | 2003-02-05 03:46:17 +0000 | [diff] [blame] | 1472 | * Tuples are also the only builtin immutable type that can be recursive |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1473 | * (a tuple can be reached from itself), and that requires some subtle |
| 1474 | * magic so that it works in all cases. IOW, this is a long routine. |
| 1475 | */ |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1476 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1477 | save_tuple(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1478 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1479 | PyObject *py_tuple_id = NULL; |
| 1480 | int len, i; |
| 1481 | int res = -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1482 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1483 | static char tuple = TUPLE; |
| 1484 | static char pop = POP; |
| 1485 | static char pop_mark = POP_MARK; |
| 1486 | static char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3}; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1487 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1488 | if ((len = PyTuple_Size(args)) < 0) |
| 1489 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1490 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1491 | if (len == 0) { |
| 1492 | char c_str[2]; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 1493 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1494 | if (self->proto) { |
| 1495 | c_str[0] = EMPTY_TUPLE; |
| 1496 | len = 1; |
| 1497 | } |
| 1498 | else { |
| 1499 | c_str[0] = MARK; |
| 1500 | c_str[1] = TUPLE; |
| 1501 | len = 2; |
| 1502 | } |
| 1503 | if (self->write_func(self, c_str, len) >= 0) |
| 1504 | res = 0; |
| 1505 | /* Don't memoize an empty tuple. */ |
| 1506 | goto finally; |
| 1507 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1508 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1509 | /* A non-empty tuple. */ |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1510 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1511 | /* id(tuple) isn't in the memo now. If it shows up there after |
| 1512 | * saving the tuple elements, the tuple must be recursive, in |
| 1513 | * which case we'll pop everything we put on the stack, and fetch |
| 1514 | * its value from the memo. |
| 1515 | */ |
| 1516 | py_tuple_id = PyLong_FromVoidPtr(args); |
| 1517 | if (py_tuple_id == NULL) |
| 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 <= 3 && self->proto >= 2) { |
| 1521 | /* Use TUPLE{1,2,3} opcodes. */ |
| 1522 | if (store_tuple_elements(self, args, len) < 0) |
| 1523 | goto finally; |
| 1524 | if (PyDict_GetItem(self->memo, py_tuple_id)) { |
| 1525 | /* pop the len elements */ |
| 1526 | for (i = 0; i < len; ++i) |
| 1527 | if (self->write_func(self, &pop, 1) < 0) |
| 1528 | goto finally; |
| 1529 | /* fetch from memo */ |
| 1530 | if (get(self, py_tuple_id) < 0) |
| 1531 | goto finally; |
| 1532 | res = 0; |
| 1533 | goto finally; |
| 1534 | } |
| 1535 | /* Not recursive. */ |
| 1536 | if (self->write_func(self, len2opcode + len, 1) < 0) |
| 1537 | goto finally; |
| 1538 | goto memoize; |
| 1539 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1540 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1541 | /* proto < 2 and len > 0, or proto >= 2 and len > 3. |
| 1542 | * Generate MARK elt1 elt2 ... TUPLE |
| 1543 | */ |
| 1544 | if (self->write_func(self, &MARKv, 1) < 0) |
| 1545 | goto finally; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1546 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1547 | if (store_tuple_elements(self, args, len) < 0) |
| 1548 | goto finally; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1549 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1550 | if (PyDict_GetItem(self->memo, py_tuple_id)) { |
| 1551 | /* pop the stack stuff we pushed */ |
| 1552 | if (self->bin) { |
| 1553 | if (self->write_func(self, &pop_mark, 1) < 0) |
| 1554 | goto finally; |
| 1555 | } |
| 1556 | else { |
| 1557 | /* Note that we pop one more than len, to remove |
| 1558 | * the MARK too. |
| 1559 | */ |
| 1560 | for (i = 0; i <= len; i++) |
| 1561 | if (self->write_func(self, &pop, 1) < 0) |
| 1562 | goto finally; |
| 1563 | } |
| 1564 | /* fetch from memo */ |
| 1565 | if (get(self, py_tuple_id) >= 0) |
| 1566 | res = 0; |
| 1567 | goto finally; |
| 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 | /* Not recursive. */ |
| 1571 | if (self->write_func(self, &tuple, 1) < 0) |
| 1572 | goto finally; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 1573 | |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1574 | memoize: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1575 | if (put(self, args) >= 0) |
| 1576 | res = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1577 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1578 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1579 | Py_XDECREF(py_tuple_id); |
| 1580 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1581 | } |
| 1582 | |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1583 | /* iter is an iterator giving items, and we batch up chunks of |
| 1584 | * MARK item item ... item APPENDS |
| 1585 | * opcode sequences. Calling code should have arranged to first create an |
| 1586 | * empty list, or list-like object, for the APPENDS to operate on. |
| 1587 | * Returns 0 on success, <0 on error. |
| 1588 | */ |
| 1589 | static int |
| 1590 | batch_list(Picklerobject *self, PyObject *iter) |
| 1591 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1592 | PyObject *obj = NULL; |
| 1593 | PyObject *firstitem = NULL; |
| 1594 | int i, n; |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1595 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1596 | static char append = APPEND; |
| 1597 | static char appends = APPENDS; |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1598 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1599 | assert(iter != NULL); |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1600 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1601 | if (self->proto == 0) { |
| 1602 | /* APPENDS isn't available; do one at a time. */ |
| 1603 | for (;;) { |
| 1604 | obj = PyIter_Next(iter); |
| 1605 | if (obj == NULL) { |
| 1606 | if (PyErr_Occurred()) |
| 1607 | return -1; |
| 1608 | break; |
| 1609 | } |
| 1610 | i = save(self, obj, 0); |
| 1611 | Py_DECREF(obj); |
| 1612 | if (i < 0) |
| 1613 | return -1; |
| 1614 | if (self->write_func(self, &append, 1) < 0) |
| 1615 | return -1; |
| 1616 | } |
| 1617 | return 0; |
| 1618 | } |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1619 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1620 | /* proto > 0: write in batches of BATCHSIZE. */ |
| 1621 | do { |
| 1622 | /* Get first item */ |
| 1623 | firstitem = PyIter_Next(iter); |
| 1624 | if (firstitem == NULL) { |
| 1625 | if (PyErr_Occurred()) |
| 1626 | goto BatchFailed; |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1627 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1628 | /* nothing more to add */ |
| 1629 | break; |
| 1630 | } |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1631 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1632 | /* Try to get a second item */ |
| 1633 | obj = PyIter_Next(iter); |
| 1634 | if (obj == NULL) { |
| 1635 | if (PyErr_Occurred()) |
| 1636 | goto BatchFailed; |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1637 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1638 | /* Only one item to write */ |
| 1639 | if (save(self, firstitem, 0) < 0) |
| 1640 | goto BatchFailed; |
| 1641 | if (self->write_func(self, &append, 1) < 0) |
| 1642 | goto BatchFailed; |
| 1643 | Py_CLEAR(firstitem); |
| 1644 | break; |
| 1645 | } |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1646 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1647 | /* More than one item to write */ |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1648 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1649 | /* Pump out MARK, items, APPENDS. */ |
| 1650 | if (self->write_func(self, &MARKv, 1) < 0) |
| 1651 | goto BatchFailed; |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1652 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1653 | if (save(self, firstitem, 0) < 0) |
| 1654 | goto BatchFailed; |
| 1655 | Py_CLEAR(firstitem); |
| 1656 | n = 1; |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1657 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1658 | /* Fetch and save up to BATCHSIZE items */ |
| 1659 | while (obj) { |
| 1660 | if (save(self, obj, 0) < 0) |
| 1661 | goto BatchFailed; |
| 1662 | Py_CLEAR(obj); |
| 1663 | n += 1; |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1664 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1665 | if (n == BATCHSIZE) |
| 1666 | break; |
| 1667 | |
| 1668 | obj = PyIter_Next(iter); |
| 1669 | if (obj == NULL) { |
| 1670 | if (PyErr_Occurred()) |
| 1671 | goto BatchFailed; |
| 1672 | break; |
| 1673 | } |
| 1674 | } |
| 1675 | |
| 1676 | if (self->write_func(self, &appends, 1) < 0) |
| 1677 | goto BatchFailed; |
| 1678 | |
| 1679 | } while (n == BATCHSIZE); |
| 1680 | return 0; |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1681 | |
| 1682 | BatchFailed: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1683 | Py_XDECREF(firstitem); |
| 1684 | Py_XDECREF(obj); |
| 1685 | return -1; |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1686 | } |
| 1687 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1688 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1689 | save_list(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1690 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1691 | int res = -1; |
| 1692 | char s[3]; |
| 1693 | int len; |
| 1694 | PyObject *iter; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1695 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1696 | if (self->fast && !fast_save_enter(self, args)) |
| 1697 | goto finally; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 1698 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1699 | /* Create an empty list. */ |
| 1700 | if (self->bin) { |
| 1701 | s[0] = EMPTY_LIST; |
| 1702 | len = 1; |
| 1703 | } |
| 1704 | else { |
| 1705 | s[0] = MARK; |
| 1706 | s[1] = LIST; |
| 1707 | len = 2; |
| 1708 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1709 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1710 | if (self->write_func(self, s, len) < 0) |
| 1711 | goto finally; |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1712 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1713 | /* Get list length, and bow out early if empty. */ |
| 1714 | if ((len = PyList_Size(args)) < 0) |
| 1715 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1716 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1717 | /* Memoize. */ |
| 1718 | if (len == 0) { |
| 1719 | if (put(self, args) >= 0) |
| 1720 | res = 0; |
| 1721 | goto finally; |
| 1722 | } |
| 1723 | if (put2(self, args) < 0) |
| 1724 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1725 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1726 | /* Materialize the list elements. */ |
| 1727 | iter = PyObject_GetIter(args); |
| 1728 | if (iter == NULL) |
| 1729 | goto finally; |
Facundo Batista | 763d309 | 2008-06-30 01:10:55 +0000 | [diff] [blame] | 1730 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1731 | if (Py_EnterRecursiveCall(" while pickling an object") == 0) |
| 1732 | { |
| 1733 | res = batch_list(self, iter); |
| 1734 | Py_LeaveRecursiveCall(); |
| 1735 | } |
| 1736 | Py_DECREF(iter); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1737 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1738 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1739 | if (self->fast && !fast_save_leave(self, args)) |
| 1740 | res = -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1741 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1742 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1743 | } |
| 1744 | |
| 1745 | |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1746 | /* iter is an iterator giving (key, value) pairs, and we batch up chunks of |
| 1747 | * MARK key value ... key value SETITEMS |
| 1748 | * opcode sequences. Calling code should have arranged to first create an |
| 1749 | * empty dict, or dict-like object, for the SETITEMS to operate on. |
| 1750 | * Returns 0 on success, <0 on error. |
| 1751 | * |
| 1752 | * This is very much like batch_list(). The difference between saving |
| 1753 | * elements directly, and picking apart two-tuples, is so long-winded at |
| 1754 | * the C level, though, that attempts to combine these routines were too |
| 1755 | * ugly to bear. |
| 1756 | */ |
| 1757 | static int |
| 1758 | batch_dict(Picklerobject *self, PyObject *iter) |
| 1759 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1760 | PyObject *p = NULL; |
| 1761 | PyObject *firstitem = NULL; |
| 1762 | int i, n; |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1763 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1764 | static char setitem = SETITEM; |
| 1765 | static char setitems = SETITEMS; |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1766 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1767 | assert(iter != NULL); |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1768 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1769 | if (self->proto == 0) { |
| 1770 | /* SETITEMS isn't available; do one at a time. */ |
| 1771 | for (;;) { |
| 1772 | p = PyIter_Next(iter); |
| 1773 | if (p == NULL) { |
| 1774 | if (PyErr_Occurred()) |
| 1775 | return -1; |
| 1776 | break; |
| 1777 | } |
| 1778 | if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) { |
| 1779 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 1780 | "iterator must return 2-tuples"); |
| 1781 | return -1; |
| 1782 | } |
| 1783 | i = save(self, PyTuple_GET_ITEM(p, 0), 0); |
| 1784 | if (i >= 0) |
| 1785 | i = save(self, PyTuple_GET_ITEM(p, 1), 0); |
| 1786 | Py_DECREF(p); |
| 1787 | if (i < 0) |
| 1788 | return -1; |
| 1789 | if (self->write_func(self, &setitem, 1) < 0) |
| 1790 | return -1; |
| 1791 | } |
| 1792 | return 0; |
| 1793 | } |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1794 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1795 | /* proto > 0: write in batches of BATCHSIZE. */ |
| 1796 | do { |
| 1797 | /* Get first item */ |
| 1798 | firstitem = PyIter_Next(iter); |
| 1799 | if (firstitem == NULL) { |
| 1800 | if (PyErr_Occurred()) |
| 1801 | goto BatchFailed; |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1802 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1803 | /* nothing more to add */ |
| 1804 | break; |
| 1805 | } |
| 1806 | if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) { |
| 1807 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 1808 | "iterator must return 2-tuples"); |
| 1809 | goto BatchFailed; |
| 1810 | } |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1811 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1812 | /* Try to get a second item */ |
| 1813 | p = PyIter_Next(iter); |
| 1814 | if (p == NULL) { |
| 1815 | if (PyErr_Occurred()) |
| 1816 | goto BatchFailed; |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1817 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1818 | /* Only one item to write */ |
| 1819 | if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0) |
| 1820 | goto BatchFailed; |
| 1821 | if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0) |
| 1822 | goto BatchFailed; |
| 1823 | if (self->write_func(self, &setitem, 1) < 0) |
| 1824 | goto BatchFailed; |
| 1825 | Py_CLEAR(firstitem); |
| 1826 | break; |
| 1827 | } |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1828 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1829 | /* More than one item to write */ |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1830 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1831 | /* Pump out MARK, items, SETITEMS. */ |
| 1832 | if (self->write_func(self, &MARKv, 1) < 0) |
| 1833 | goto BatchFailed; |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1834 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1835 | if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0) |
| 1836 | goto BatchFailed; |
| 1837 | if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0) |
| 1838 | goto BatchFailed; |
| 1839 | Py_CLEAR(firstitem); |
| 1840 | n = 1; |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1841 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1842 | /* Fetch and save up to BATCHSIZE items */ |
| 1843 | while (p) { |
| 1844 | if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) { |
| 1845 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 1846 | "iterator must return 2-tuples"); |
| 1847 | goto BatchFailed; |
| 1848 | } |
| 1849 | if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0) |
| 1850 | goto BatchFailed; |
| 1851 | if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0) |
| 1852 | goto BatchFailed; |
| 1853 | Py_CLEAR(p); |
| 1854 | n += 1; |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1855 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1856 | if (n == BATCHSIZE) |
| 1857 | break; |
Amaury Forgeot d'Arc | 24cb382 | 2008-09-11 20:56:13 +0000 | [diff] [blame] | 1858 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1859 | p = PyIter_Next(iter); |
| 1860 | if (p == NULL) { |
| 1861 | if (PyErr_Occurred()) |
| 1862 | goto BatchFailed; |
| 1863 | break; |
| 1864 | } |
| 1865 | } |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1866 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1867 | if (self->write_func(self, &setitems, 1) < 0) |
| 1868 | goto BatchFailed; |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1869 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1870 | } while (n == BATCHSIZE); |
| 1871 | return 0; |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1872 | |
| 1873 | BatchFailed: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1874 | Py_XDECREF(firstitem); |
| 1875 | Py_XDECREF(p); |
| 1876 | return -1; |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1877 | } |
| 1878 | |
Collin Winter | 179bf21 | 2009-05-25 04:34:39 +0000 | [diff] [blame] | 1879 | /* This is a variant of batch_dict() above that specializes for dicts, with no |
| 1880 | * support for dict subclasses. Like batch_dict(), we batch up chunks of |
| 1881 | * MARK key value ... key value SETITEMS |
| 1882 | * opcode sequences. Calling code should have arranged to first create an |
| 1883 | * empty dict, or dict-like object, for the SETITEMS to operate on. |
| 1884 | * Returns 0 on success, -1 on error. |
| 1885 | * |
| 1886 | * Note that this currently doesn't work for protocol 0. |
| 1887 | */ |
| 1888 | static int |
| 1889 | batch_dict_exact(Picklerobject *self, PyObject *obj) |
| 1890 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1891 | PyObject *key = NULL, *value = NULL; |
| 1892 | int i; |
| 1893 | Py_ssize_t dict_size, ppos = 0; |
Collin Winter | 179bf21 | 2009-05-25 04:34:39 +0000 | [diff] [blame] | 1894 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1895 | static char setitem = SETITEM; |
| 1896 | static char setitems = SETITEMS; |
Collin Winter | 179bf21 | 2009-05-25 04:34:39 +0000 | [diff] [blame] | 1897 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1898 | assert(obj != NULL); |
| 1899 | assert(self->proto > 0); |
Collin Winter | 179bf21 | 2009-05-25 04:34:39 +0000 | [diff] [blame] | 1900 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1901 | dict_size = PyDict_Size(obj); |
Collin Winter | 179bf21 | 2009-05-25 04:34:39 +0000 | [diff] [blame] | 1902 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1903 | /* Special-case len(d) == 1 to save space. */ |
| 1904 | if (dict_size == 1) { |
| 1905 | PyDict_Next(obj, &ppos, &key, &value); |
| 1906 | if (save(self, key, 0) < 0) |
| 1907 | return -1; |
| 1908 | if (save(self, value, 0) < 0) |
| 1909 | return -1; |
| 1910 | if (self->write_func(self, &setitem, 1) < 0) |
| 1911 | return -1; |
| 1912 | return 0; |
| 1913 | } |
Collin Winter | 179bf21 | 2009-05-25 04:34:39 +0000 | [diff] [blame] | 1914 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1915 | /* Write in batches of BATCHSIZE. */ |
| 1916 | do { |
| 1917 | i = 0; |
| 1918 | if (self->write_func(self, &MARKv, 1) < 0) |
| 1919 | return -1; |
| 1920 | while (PyDict_Next(obj, &ppos, &key, &value)) { |
| 1921 | if (save(self, key, 0) < 0) |
| 1922 | return -1; |
| 1923 | if (save(self, value, 0) < 0) |
| 1924 | return -1; |
| 1925 | if (++i == BATCHSIZE) |
| 1926 | break; |
| 1927 | } |
| 1928 | if (self->write_func(self, &setitems, 1) < 0) |
| 1929 | return -1; |
| 1930 | if (PyDict_Size(obj) != dict_size) { |
| 1931 | PyErr_Format( |
| 1932 | PyExc_RuntimeError, |
| 1933 | "dictionary changed size during iteration"); |
| 1934 | return -1; |
| 1935 | } |
Collin Winter | 179bf21 | 2009-05-25 04:34:39 +0000 | [diff] [blame] | 1936 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1937 | } while (i == BATCHSIZE); |
| 1938 | return 0; |
Collin Winter | 179bf21 | 2009-05-25 04:34:39 +0000 | [diff] [blame] | 1939 | } |
| 1940 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1941 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1942 | save_dict(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1943 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1944 | int res = -1; |
| 1945 | char s[3]; |
| 1946 | int len; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1947 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1948 | if (self->fast && !fast_save_enter(self, args)) |
| 1949 | goto finally; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 1950 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1951 | /* Create an empty dict. */ |
| 1952 | if (self->bin) { |
| 1953 | s[0] = EMPTY_DICT; |
| 1954 | len = 1; |
| 1955 | } |
| 1956 | else { |
| 1957 | s[0] = MARK; |
| 1958 | s[1] = DICT; |
| 1959 | len = 2; |
| 1960 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1961 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1962 | if (self->write_func(self, s, len) < 0) |
| 1963 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1964 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1965 | /* Get dict size, and bow out early if empty. */ |
| 1966 | if ((len = PyDict_Size(args)) < 0) |
| 1967 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1968 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1969 | if (len == 0) { |
| 1970 | if (put(self, args) >= 0) |
| 1971 | res = 0; |
| 1972 | goto finally; |
| 1973 | } |
| 1974 | if (put2(self, args) < 0) |
| 1975 | goto finally; |
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 | /* Materialize the dict items. */ |
| 1978 | if (PyDict_CheckExact(args) && self->proto > 0) { |
| 1979 | /* We can take certain shortcuts if we know this is a dict and |
| 1980 | not a dict subclass. */ |
| 1981 | if (Py_EnterRecursiveCall(" while pickling an object") == 0) { |
| 1982 | res = batch_dict_exact(self, args); |
| 1983 | Py_LeaveRecursiveCall(); |
| 1984 | } |
| 1985 | } else { |
| 1986 | PyObject *iter = PyObject_CallMethod(args, "iteritems", "()"); |
| 1987 | if (iter == NULL) |
| 1988 | goto finally; |
| 1989 | if (Py_EnterRecursiveCall(" while pickling an object") == 0) { |
| 1990 | res = batch_dict(self, iter); |
| 1991 | Py_LeaveRecursiveCall(); |
| 1992 | } |
| 1993 | Py_DECREF(iter); |
| 1994 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1995 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1996 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1997 | if (self->fast && !fast_save_leave(self, args)) |
| 1998 | res = -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1999 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2000 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2001 | } |
| 2002 | |
| 2003 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2004 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2005 | save_inst(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2006 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2007 | PyObject *class = 0, *module = 0, *name = 0, *state = 0, |
| 2008 | *getinitargs_func = 0, *getstate_func = 0, *class_args = 0; |
| 2009 | char *module_str, *name_str; |
| 2010 | int module_size, name_size, res = -1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2011 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2012 | static char inst = INST, obj = OBJ, build = BUILD; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2013 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2014 | if (self->fast && !fast_save_enter(self, args)) |
| 2015 | goto finally; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 2016 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2017 | if (self->write_func(self, &MARKv, 1) < 0) |
| 2018 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2019 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2020 | if (!( class = PyObject_GetAttr(args, __class___str))) |
| 2021 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2022 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2023 | if (self->bin) { |
| 2024 | if (save(self, class, 0) < 0) |
| 2025 | goto finally; |
| 2026 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2027 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2028 | if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) { |
| 2029 | PyObject *element = 0; |
| 2030 | int i, len; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2031 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2032 | if (!( class_args = |
| 2033 | PyObject_Call(getinitargs_func, empty_tuple, NULL))) |
| 2034 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2035 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2036 | if ((len = PyObject_Size(class_args)) < 0) |
| 2037 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2038 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2039 | for (i = 0; i < len; i++) { |
| 2040 | if (!( element = PySequence_GetItem(class_args, i))) |
| 2041 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2042 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2043 | if (save(self, element, 0) < 0) { |
| 2044 | Py_DECREF(element); |
| 2045 | goto finally; |
| 2046 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2047 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2048 | Py_DECREF(element); |
| 2049 | } |
| 2050 | } |
| 2051 | else { |
| 2052 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 2053 | PyErr_Clear(); |
| 2054 | else |
| 2055 | goto finally; |
| 2056 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2057 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2058 | if (!self->bin) { |
| 2059 | if (!( name = ((PyClassObject *)class)->cl_name )) { |
| 2060 | PyErr_SetString(PicklingError, "class has no name"); |
| 2061 | goto finally; |
| 2062 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2063 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2064 | if (!( module = whichmodule(class, name))) |
| 2065 | goto finally; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 2066 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2067 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2068 | if ((module_size = PyString_Size(module)) < 0 || |
| 2069 | (name_size = PyString_Size(name)) < 0) |
| 2070 | goto finally; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 2071 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2072 | module_str = PyString_AS_STRING((PyStringObject *)module); |
| 2073 | name_str = PyString_AS_STRING((PyStringObject *)name); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2074 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2075 | if (self->write_func(self, &inst, 1) < 0) |
| 2076 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2077 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2078 | if (self->write_func(self, module_str, module_size) < 0) |
| 2079 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2080 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2081 | if (self->write_func(self, "\n", 1) < 0) |
| 2082 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2083 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2084 | if (self->write_func(self, name_str, name_size) < 0) |
| 2085 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2086 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2087 | if (self->write_func(self, "\n", 1) < 0) |
| 2088 | goto finally; |
| 2089 | } |
| 2090 | else if (self->write_func(self, &obj, 1) < 0) { |
| 2091 | goto finally; |
| 2092 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2093 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2094 | if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) { |
| 2095 | state = PyObject_Call(getstate_func, empty_tuple, NULL); |
| 2096 | if (!state) |
| 2097 | goto finally; |
| 2098 | } |
| 2099 | else { |
| 2100 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 2101 | PyErr_Clear(); |
| 2102 | else |
| 2103 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2104 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2105 | if (!( state = PyObject_GetAttr(args, __dict___str))) { |
| 2106 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 2107 | PyErr_Clear(); |
| 2108 | else |
| 2109 | goto finally; |
| 2110 | res = 0; |
| 2111 | goto finally; |
| 2112 | } |
| 2113 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2114 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2115 | if (!PyDict_Check(state)) { |
| 2116 | if (put2(self, args) < 0) |
| 2117 | goto finally; |
| 2118 | } |
| 2119 | else { |
| 2120 | if (put(self, args) < 0) |
| 2121 | goto finally; |
| 2122 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2123 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2124 | if (save(self, state, 0) < 0) |
| 2125 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2126 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2127 | if (self->write_func(self, &build, 1) < 0) |
| 2128 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2129 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2130 | res = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2131 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2132 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2133 | if (self->fast && !fast_save_leave(self, args)) |
| 2134 | res = -1; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 2135 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2136 | Py_XDECREF(module); |
| 2137 | Py_XDECREF(class); |
| 2138 | Py_XDECREF(state); |
| 2139 | Py_XDECREF(getinitargs_func); |
| 2140 | Py_XDECREF(getstate_func); |
| 2141 | Py_XDECREF(class_args); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2142 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2143 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2144 | } |
| 2145 | |
| 2146 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2147 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2148 | save_global(Picklerobject *self, PyObject *args, PyObject *name) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2149 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2150 | PyObject *global_name = 0, *module = 0, *mod = 0, *klass = 0; |
| 2151 | char *name_str, *module_str; |
| 2152 | int module_size, name_size, res = -1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2153 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2154 | static char global = GLOBAL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2155 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2156 | if (name) { |
| 2157 | global_name = name; |
| 2158 | Py_INCREF(global_name); |
| 2159 | } |
| 2160 | else { |
| 2161 | if (!( global_name = PyObject_GetAttr(args, __name___str))) |
| 2162 | goto finally; |
| 2163 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2164 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2165 | if (!( module = whichmodule(args, global_name))) |
| 2166 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2167 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2168 | if ((module_size = PyString_Size(module)) < 0 || |
| 2169 | (name_size = PyString_Size(global_name)) < 0) |
| 2170 | goto finally; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 2171 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2172 | module_str = PyString_AS_STRING((PyStringObject *)module); |
| 2173 | name_str = PyString_AS_STRING((PyStringObject *)global_name); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2174 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2175 | /* XXX This can be doing a relative import. Clearly it shouldn't, |
| 2176 | but I don't know how to stop it. :-( */ |
| 2177 | mod = PyImport_ImportModule(module_str); |
| 2178 | if (mod == NULL) { |
| 2179 | cPickle_ErrFormat(PicklingError, |
| 2180 | "Can't pickle %s: import of module %s " |
| 2181 | "failed", |
| 2182 | "OS", args, module); |
| 2183 | goto finally; |
| 2184 | } |
| 2185 | klass = PyObject_GetAttrString(mod, name_str); |
| 2186 | if (klass == NULL) { |
| 2187 | cPickle_ErrFormat(PicklingError, |
| 2188 | "Can't pickle %s: attribute lookup %s.%s " |
| 2189 | "failed", |
| 2190 | "OSS", args, module, global_name); |
| 2191 | goto finally; |
| 2192 | } |
| 2193 | if (klass != args) { |
| 2194 | Py_DECREF(klass); |
| 2195 | cPickle_ErrFormat(PicklingError, |
| 2196 | "Can't pickle %s: it's not the same object " |
| 2197 | "as %s.%s", |
| 2198 | "OSS", args, module, global_name); |
| 2199 | goto finally; |
| 2200 | } |
| 2201 | Py_DECREF(klass); |
Guido van Rossum | a92d16a | 2001-08-18 21:22:07 +0000 | [diff] [blame] | 2202 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2203 | if (self->proto >= 2) { |
| 2204 | /* See whether this is in the extension registry, and if |
| 2205 | * so generate an EXT opcode. |
| 2206 | */ |
| 2207 | PyObject *py_code; /* extension code as Python object */ |
| 2208 | long code; /* extension code as C value */ |
| 2209 | char c_str[5]; |
| 2210 | int n; |
Tim Peters | 731098b | 2003-02-04 20:56:09 +0000 | [diff] [blame] | 2211 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2212 | PyTuple_SET_ITEM(two_tuple, 0, module); |
| 2213 | PyTuple_SET_ITEM(two_tuple, 1, global_name); |
| 2214 | py_code = PyDict_GetItem(extension_registry, two_tuple); |
| 2215 | if (py_code == NULL) |
| 2216 | goto gen_global; /* not registered */ |
Tim Peters | 731098b | 2003-02-04 20:56:09 +0000 | [diff] [blame] | 2217 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2218 | /* Verify py_code has the right type and value. */ |
| 2219 | if (!PyInt_Check(py_code)) { |
| 2220 | cPickle_ErrFormat(PicklingError, "Can't pickle %s: " |
| 2221 | "extension code %s isn't an integer", |
| 2222 | "OO", args, py_code); |
| 2223 | goto finally; |
| 2224 | } |
| 2225 | code = PyInt_AS_LONG(py_code); |
| 2226 | if (code <= 0 || code > 0x7fffffffL) { |
| 2227 | cPickle_ErrFormat(PicklingError, "Can't pickle %s: " |
| 2228 | "extension code %ld is out of range", |
| 2229 | "Ol", args, code); |
| 2230 | goto finally; |
| 2231 | } |
Tim Peters | 731098b | 2003-02-04 20:56:09 +0000 | [diff] [blame] | 2232 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2233 | /* Generate an EXT opcode. */ |
| 2234 | if (code <= 0xff) { |
| 2235 | c_str[0] = EXT1; |
| 2236 | c_str[1] = (char)code; |
| 2237 | n = 2; |
| 2238 | } |
| 2239 | else if (code <= 0xffff) { |
| 2240 | c_str[0] = EXT2; |
| 2241 | c_str[1] = (char)(code & 0xff); |
| 2242 | c_str[2] = (char)((code >> 8) & 0xff); |
| 2243 | n = 3; |
| 2244 | } |
| 2245 | else { |
| 2246 | c_str[0] = EXT4; |
| 2247 | c_str[1] = (char)(code & 0xff); |
| 2248 | c_str[2] = (char)((code >> 8) & 0xff); |
| 2249 | c_str[3] = (char)((code >> 16) & 0xff); |
| 2250 | c_str[4] = (char)((code >> 24) & 0xff); |
| 2251 | n = 5; |
| 2252 | } |
Tim Peters | 731098b | 2003-02-04 20:56:09 +0000 | [diff] [blame] | 2253 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2254 | if (self->write_func(self, c_str, n) >= 0) |
| 2255 | res = 0; |
| 2256 | goto finally; /* and don't memoize */ |
| 2257 | } |
Tim Peters | 731098b | 2003-02-04 20:56:09 +0000 | [diff] [blame] | 2258 | |
| 2259 | gen_global: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2260 | if (self->write_func(self, &global, 1) < 0) |
| 2261 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2262 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2263 | if (self->write_func(self, module_str, module_size) < 0) |
| 2264 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2265 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2266 | if (self->write_func(self, "\n", 1) < 0) |
| 2267 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2268 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2269 | if (self->write_func(self, name_str, name_size) < 0) |
| 2270 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2271 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2272 | if (self->write_func(self, "\n", 1) < 0) |
| 2273 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2274 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2275 | if (put(self, args) < 0) |
| 2276 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2277 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2278 | res = 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2279 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2280 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2281 | Py_XDECREF(module); |
| 2282 | Py_XDECREF(global_name); |
| 2283 | Py_XDECREF(mod); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2284 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2285 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2286 | } |
| 2287 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2288 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2289 | save_pers(Picklerobject *self, PyObject *args, PyObject *f) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2290 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2291 | PyObject *pid = 0; |
| 2292 | int size, res = -1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2293 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2294 | static char persid = PERSID, binpersid = BINPERSID; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2295 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2296 | Py_INCREF(args); |
| 2297 | ARG_TUP(self, args); |
| 2298 | if (self->arg) { |
| 2299 | pid = PyObject_Call(f, self->arg, NULL); |
| 2300 | FREE_ARG_TUP(self); |
| 2301 | } |
| 2302 | if (! pid) return -1; |
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 (pid != Py_None) { |
| 2305 | if (!self->bin) { |
| 2306 | if (!PyString_Check(pid)) { |
| 2307 | PyErr_SetString(PicklingError, |
| 2308 | "persistent id must be string"); |
| 2309 | goto finally; |
| 2310 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2311 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2312 | if (self->write_func(self, &persid, 1) < 0) |
| 2313 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2314 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2315 | if ((size = PyString_Size(pid)) < 0) |
| 2316 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2317 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2318 | if (self->write_func(self, |
| 2319 | PyString_AS_STRING( |
| 2320 | (PyStringObject *)pid), |
| 2321 | size) < 0) |
| 2322 | goto finally; |
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 | if (self->write_func(self, "\n", 1) < 0) |
| 2325 | goto finally; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2326 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2327 | res = 1; |
| 2328 | goto finally; |
| 2329 | } |
| 2330 | else if (save(self, pid, 1) >= 0) { |
| 2331 | if (self->write_func(self, &binpersid, 1) < 0) |
| 2332 | res = -1; |
| 2333 | else |
| 2334 | res = 1; |
| 2335 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2336 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2337 | goto finally; |
| 2338 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2339 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2340 | res = 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2341 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2342 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2343 | Py_XDECREF(pid); |
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 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2346 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2347 | |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2348 | /* We're saving ob, and args is the 2-thru-5 tuple returned by the |
| 2349 | * appropriate __reduce__ method for ob. |
| 2350 | */ |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2351 | static int |
Amaury Forgeot d'Arc | 69a9c5b | 2008-10-30 21:18:34 +0000 | [diff] [blame] | 2352 | save_reduce(Picklerobject *self, PyObject *args, PyObject *fn, PyObject *ob) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2353 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2354 | PyObject *callable; |
| 2355 | PyObject *argtup; |
| 2356 | PyObject *state = NULL; |
| 2357 | PyObject *listitems = Py_None; |
| 2358 | PyObject *dictitems = Py_None; |
| 2359 | Py_ssize_t size; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2360 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2361 | int use_newobj = self->proto >= 2; |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2362 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2363 | static char reduce = REDUCE; |
| 2364 | static char build = BUILD; |
| 2365 | static char newobj = NEWOBJ; |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2366 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2367 | size = PyTuple_Size(args); |
| 2368 | if (size < 2 || size > 5) { |
| 2369 | cPickle_ErrFormat(PicklingError, "tuple returned by " |
| 2370 | "%s must contain 2 through 5 elements", |
| 2371 | "O", fn); |
| 2372 | return -1; |
| 2373 | } |
Amaury Forgeot d'Arc | 69a9c5b | 2008-10-30 21:18:34 +0000 | [diff] [blame] | 2374 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2375 | if (! PyArg_UnpackTuple(args, "save_reduce", 2, 5, |
| 2376 | &callable, |
| 2377 | &argtup, |
| 2378 | &state, |
| 2379 | &listitems, |
| 2380 | &dictitems)) |
| 2381 | return -1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2382 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2383 | if (!PyTuple_Check(argtup)) { |
| 2384 | cPickle_ErrFormat(PicklingError, "Second element of " |
| 2385 | "tuple returned by %s must be a tuple", |
| 2386 | "O", fn); |
| 2387 | return -1; |
| 2388 | } |
Raymond Hettinger | a6b45cc | 2004-12-07 07:05:57 +0000 | [diff] [blame] | 2389 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2390 | if (state == Py_None) |
| 2391 | state = NULL; |
Amaury Forgeot d'Arc | 69a9c5b | 2008-10-30 21:18:34 +0000 | [diff] [blame] | 2392 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2393 | if (listitems == Py_None) |
| 2394 | listitems = NULL; |
| 2395 | else if (!PyIter_Check(listitems)) { |
| 2396 | cPickle_ErrFormat(PicklingError, "Fourth element of " |
| 2397 | "tuple returned by %s must be an iterator, not %s", |
| 2398 | "Os", fn, Py_TYPE(listitems)->tp_name); |
| 2399 | return -1; |
| 2400 | } |
Amaury Forgeot d'Arc | 69a9c5b | 2008-10-30 21:18:34 +0000 | [diff] [blame] | 2401 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2402 | if (dictitems == Py_None) |
| 2403 | dictitems = NULL; |
| 2404 | else if (!PyIter_Check(dictitems)) { |
| 2405 | cPickle_ErrFormat(PicklingError, "Fifth element of " |
| 2406 | "tuple returned by %s must be an iterator, not %s", |
| 2407 | "Os", fn, Py_TYPE(dictitems)->tp_name); |
| 2408 | return -1; |
| 2409 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2410 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2411 | /* Protocol 2 special case: if callable's name is __newobj__, use |
| 2412 | * NEWOBJ. This consumes a lot of code. |
| 2413 | */ |
| 2414 | if (use_newobj) { |
| 2415 | PyObject *temp = PyObject_GetAttr(callable, __name___str); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2416 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2417 | if (temp == NULL) { |
| 2418 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 2419 | PyErr_Clear(); |
| 2420 | else |
| 2421 | return -1; |
| 2422 | use_newobj = 0; |
| 2423 | } |
| 2424 | else { |
| 2425 | use_newobj = PyString_Check(temp) && |
| 2426 | strcmp(PyString_AS_STRING(temp), |
| 2427 | "__newobj__") == 0; |
| 2428 | Py_DECREF(temp); |
| 2429 | } |
| 2430 | } |
| 2431 | if (use_newobj) { |
| 2432 | PyObject *cls; |
| 2433 | PyObject *newargtup; |
| 2434 | int n, i; |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2435 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2436 | /* Sanity checks. */ |
| 2437 | n = PyTuple_Size(argtup); |
| 2438 | if (n < 1) { |
| 2439 | PyErr_SetString(PicklingError, "__newobj__ arglist " |
| 2440 | "is empty"); |
| 2441 | return -1; |
| 2442 | } |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2443 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2444 | cls = PyTuple_GET_ITEM(argtup, 0); |
| 2445 | if (! PyObject_HasAttrString(cls, "__new__")) { |
| 2446 | PyErr_SetString(PicklingError, "args[0] from " |
| 2447 | "__newobj__ args has no __new__"); |
| 2448 | return -1; |
| 2449 | } |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2450 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2451 | /* XXX How could ob be NULL? */ |
| 2452 | if (ob != NULL) { |
| 2453 | PyObject *ob_dot_class; |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2454 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2455 | ob_dot_class = PyObject_GetAttr(ob, __class___str); |
| 2456 | if (ob_dot_class == NULL) { |
| 2457 | if (PyErr_ExceptionMatches( |
| 2458 | PyExc_AttributeError)) |
| 2459 | PyErr_Clear(); |
| 2460 | else |
| 2461 | return -1; |
| 2462 | } |
| 2463 | i = ob_dot_class != cls; /* true iff a problem */ |
| 2464 | Py_XDECREF(ob_dot_class); |
| 2465 | if (i) { |
| 2466 | PyErr_SetString(PicklingError, "args[0] from " |
| 2467 | "__newobj__ args has the wrong class"); |
| 2468 | return -1; |
| 2469 | } |
| 2470 | } |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2471 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2472 | /* Save the class and its __new__ arguments. */ |
| 2473 | if (save(self, cls, 0) < 0) |
| 2474 | return -1; |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2475 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2476 | newargtup = PyTuple_New(n-1); /* argtup[1:] */ |
| 2477 | if (newargtup == NULL) |
| 2478 | return -1; |
| 2479 | for (i = 1; i < n; ++i) { |
| 2480 | PyObject *temp = PyTuple_GET_ITEM(argtup, i); |
| 2481 | Py_INCREF(temp); |
| 2482 | PyTuple_SET_ITEM(newargtup, i-1, temp); |
| 2483 | } |
| 2484 | i = save(self, newargtup, 0); |
| 2485 | Py_DECREF(newargtup); |
| 2486 | if (i < 0) |
| 2487 | return -1; |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2488 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2489 | /* Add NEWOBJ opcode. */ |
| 2490 | if (self->write_func(self, &newobj, 1) < 0) |
| 2491 | return -1; |
| 2492 | } |
| 2493 | else { |
| 2494 | /* Not using NEWOBJ. */ |
| 2495 | if (save(self, callable, 0) < 0 || |
| 2496 | save(self, argtup, 0) < 0 || |
| 2497 | self->write_func(self, &reduce, 1) < 0) |
| 2498 | return -1; |
| 2499 | } |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2500 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2501 | /* Memoize. */ |
| 2502 | /* XXX How can ob be NULL? */ |
| 2503 | if (ob != NULL) { |
| 2504 | if (state && !PyDict_Check(state)) { |
| 2505 | if (put2(self, ob) < 0) |
| 2506 | return -1; |
| 2507 | } |
| 2508 | else if (put(self, ob) < 0) |
| 2509 | return -1; |
| 2510 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2511 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2512 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2513 | if (listitems && batch_list(self, listitems) < 0) |
| 2514 | return -1; |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2515 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2516 | if (dictitems && batch_dict(self, dictitems) < 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 | if (state) { |
| 2520 | if (save(self, state, 0) < 0 || |
| 2521 | self->write_func(self, &build, 1) < 0) |
| 2522 | return -1; |
| 2523 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2524 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2525 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2526 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2527 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2528 | static int |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 2529 | save(Picklerobject *self, PyObject *args, int pers_save) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2530 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2531 | PyTypeObject *type; |
| 2532 | PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0; |
| 2533 | int res = -1; |
| 2534 | int tmp; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2535 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2536 | if (Py_EnterRecursiveCall(" while pickling an object")) |
| 2537 | return -1; |
Martin v. Löwis | 5a39530 | 2002-08-04 08:20:23 +0000 | [diff] [blame] | 2538 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2539 | if (!pers_save && self->pers_func) { |
| 2540 | if ((tmp = save_pers(self, args, self->pers_func)) != 0) { |
| 2541 | res = tmp; |
| 2542 | goto finally; |
| 2543 | } |
| 2544 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2545 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2546 | if (args == Py_None) { |
| 2547 | res = save_none(self, args); |
| 2548 | goto finally; |
| 2549 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2550 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2551 | type = Py_TYPE(args); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2552 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2553 | switch (type->tp_name[0]) { |
| 2554 | case 'b': |
| 2555 | if (args == Py_False || args == Py_True) { |
| 2556 | res = save_bool(self, args); |
| 2557 | goto finally; |
| 2558 | } |
| 2559 | break; |
| 2560 | case 'i': |
| 2561 | if (type == &PyInt_Type) { |
| 2562 | res = save_int(self, args); |
| 2563 | goto finally; |
| 2564 | } |
| 2565 | break; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2566 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2567 | case 'l': |
| 2568 | if (type == &PyLong_Type) { |
| 2569 | res = save_long(self, args); |
| 2570 | goto finally; |
| 2571 | } |
| 2572 | break; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2573 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2574 | case 'f': |
| 2575 | if (type == &PyFloat_Type) { |
| 2576 | res = save_float(self, args); |
| 2577 | goto finally; |
| 2578 | } |
| 2579 | break; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2580 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2581 | case 't': |
| 2582 | if (type == &PyTuple_Type && PyTuple_Size(args) == 0) { |
| 2583 | res = save_tuple(self, args); |
| 2584 | goto finally; |
| 2585 | } |
| 2586 | break; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2587 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2588 | case 's': |
| 2589 | if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) { |
| 2590 | res = save_string(self, args, 0); |
| 2591 | goto finally; |
| 2592 | } |
| 2593 | break; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 2594 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2595 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2596 | case 'u': |
| 2597 | if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) { |
| 2598 | res = save_unicode(self, args, 0); |
| 2599 | goto finally; |
| 2600 | } |
| 2601 | break; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2602 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2603 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2604 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2605 | if (Py_REFCNT(args) > 1) { |
| 2606 | if (!( py_ob_id = PyLong_FromVoidPtr(args))) |
| 2607 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2608 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2609 | if (PyDict_GetItem(self->memo, py_ob_id)) { |
| 2610 | if (get(self, py_ob_id) < 0) |
| 2611 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2612 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2613 | res = 0; |
| 2614 | goto finally; |
| 2615 | } |
| 2616 | } |
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 | switch (type->tp_name[0]) { |
| 2619 | case 's': |
| 2620 | if (type == &PyString_Type) { |
| 2621 | res = save_string(self, args, 1); |
| 2622 | goto finally; |
| 2623 | } |
| 2624 | break; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2625 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2626 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2627 | case 'u': |
| 2628 | if (type == &PyUnicode_Type) { |
| 2629 | res = save_unicode(self, args, 1); |
| 2630 | goto finally; |
| 2631 | } |
| 2632 | break; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2633 | #endif |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 2634 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2635 | case 't': |
| 2636 | if (type == &PyTuple_Type) { |
| 2637 | res = save_tuple(self, args); |
| 2638 | goto finally; |
| 2639 | } |
| 2640 | if (type == &PyType_Type) { |
| 2641 | res = save_global(self, args, NULL); |
| 2642 | goto finally; |
| 2643 | } |
| 2644 | break; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2645 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2646 | case 'l': |
| 2647 | if (type == &PyList_Type) { |
| 2648 | res = save_list(self, args); |
| 2649 | goto finally; |
| 2650 | } |
| 2651 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2652 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2653 | case 'd': |
| 2654 | if (type == &PyDict_Type) { |
| 2655 | res = save_dict(self, args); |
| 2656 | goto finally; |
| 2657 | } |
| 2658 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2659 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2660 | case 'i': |
| 2661 | if (type == &PyInstance_Type) { |
| 2662 | res = save_inst(self, args); |
| 2663 | goto finally; |
| 2664 | } |
| 2665 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2666 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2667 | case 'c': |
| 2668 | if (type == &PyClass_Type) { |
| 2669 | res = save_global(self, args, NULL); |
| 2670 | goto finally; |
| 2671 | } |
| 2672 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2673 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2674 | case 'f': |
| 2675 | if (type == &PyFunction_Type) { |
| 2676 | res = save_global(self, args, NULL); |
| 2677 | if (res && PyErr_ExceptionMatches(PickleError)) { |
| 2678 | /* fall back to reduce */ |
| 2679 | PyErr_Clear(); |
| 2680 | break; |
| 2681 | } |
| 2682 | goto finally; |
| 2683 | } |
| 2684 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2685 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2686 | case 'b': |
| 2687 | if (type == &PyCFunction_Type) { |
| 2688 | res = save_global(self, args, NULL); |
| 2689 | goto finally; |
| 2690 | } |
| 2691 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2692 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2693 | if (!pers_save && self->inst_pers_func) { |
| 2694 | if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) { |
| 2695 | res = tmp; |
| 2696 | goto finally; |
| 2697 | } |
| 2698 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2699 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2700 | /* Get a reduction callable, and call it. This may come from |
| 2701 | * copy_reg.dispatch_table, the object's __reduce_ex__ method, |
| 2702 | * or the object's __reduce__ method. |
| 2703 | */ |
| 2704 | __reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type); |
| 2705 | if (__reduce__ != NULL) { |
| 2706 | Py_INCREF(__reduce__); |
| 2707 | Py_INCREF(args); |
| 2708 | ARG_TUP(self, args); |
| 2709 | if (self->arg) { |
| 2710 | t = PyObject_Call(__reduce__, self->arg, NULL); |
| 2711 | FREE_ARG_TUP(self); |
| 2712 | } |
| 2713 | } |
| 2714 | else { |
Antoine Pitrou | 561a821 | 2011-10-04 09:34:48 +0200 | [diff] [blame] | 2715 | if (PyType_IsSubtype(type, &PyType_Type)) { |
| 2716 | res = save_global(self, args, NULL); |
| 2717 | goto finally; |
| 2718 | } |
| 2719 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2720 | /* Check for a __reduce_ex__ method. */ |
| 2721 | __reduce__ = PyObject_GetAttr(args, __reduce_ex___str); |
| 2722 | if (__reduce__ != NULL) { |
| 2723 | t = PyInt_FromLong(self->proto); |
| 2724 | if (t != NULL) { |
| 2725 | ARG_TUP(self, t); |
| 2726 | t = NULL; |
| 2727 | if (self->arg) { |
| 2728 | t = PyObject_Call(__reduce__, |
| 2729 | self->arg, NULL); |
| 2730 | FREE_ARG_TUP(self); |
| 2731 | } |
| 2732 | } |
| 2733 | } |
| 2734 | else { |
| 2735 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 2736 | PyErr_Clear(); |
| 2737 | else |
| 2738 | goto finally; |
| 2739 | /* Check for a __reduce__ method. */ |
| 2740 | __reduce__ = PyObject_GetAttr(args, __reduce___str); |
| 2741 | if (__reduce__ != NULL) { |
| 2742 | t = PyObject_Call(__reduce__, |
| 2743 | empty_tuple, NULL); |
| 2744 | } |
| 2745 | else { |
| 2746 | PyErr_SetObject(UnpickleableError, args); |
| 2747 | goto finally; |
| 2748 | } |
| 2749 | } |
| 2750 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2751 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2752 | if (t == NULL) |
| 2753 | goto finally; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2754 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2755 | if (PyString_Check(t)) { |
| 2756 | res = save_global(self, args, t); |
| 2757 | goto finally; |
| 2758 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2759 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2760 | if (!PyTuple_Check(t)) { |
| 2761 | cPickle_ErrFormat(PicklingError, "Value returned by " |
| 2762 | "%s must be string or tuple", |
| 2763 | "O", __reduce__); |
| 2764 | goto finally; |
| 2765 | } |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2766 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2767 | res = save_reduce(self, t, __reduce__, args); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2768 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2769 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2770 | Py_LeaveRecursiveCall(); |
| 2771 | Py_XDECREF(py_ob_id); |
| 2772 | Py_XDECREF(__reduce__); |
| 2773 | Py_XDECREF(t); |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2774 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2775 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2776 | } |
| 2777 | |
| 2778 | |
| 2779 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2780 | dump(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2781 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2782 | static char stop = STOP; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2783 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2784 | if (self->proto >= 2) { |
| 2785 | char bytes[2]; |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 2786 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2787 | bytes[0] = PROTO; |
| 2788 | assert(self->proto >= 0 && self->proto < 256); |
| 2789 | bytes[1] = (char)self->proto; |
| 2790 | if (self->write_func(self, bytes, 2) < 0) |
| 2791 | return -1; |
| 2792 | } |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 2793 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2794 | if (save(self, args, 0) < 0) |
| 2795 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2796 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2797 | if (self->write_func(self, &stop, 1) < 0) |
| 2798 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2799 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2800 | if (self->write_func(self, NULL, 0) < 0) |
| 2801 | return -1; |
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 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2804 | } |
| 2805 | |
| 2806 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2807 | Pickle_clear_memo(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2808 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2809 | if (self->memo) |
| 2810 | PyDict_Clear(self->memo); |
| 2811 | Py_INCREF(Py_None); |
| 2812 | return Py_None; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2813 | } |
| 2814 | |
| 2815 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2816 | Pickle_getvalue(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2817 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2818 | int l, i, rsize, ssize, clear=1, lm; |
| 2819 | long ik; |
| 2820 | PyObject *k, *r; |
| 2821 | char *s, *p, *have_get; |
| 2822 | Pdata *data; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2823 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2824 | /* Can be called by Python code or C code */ |
| 2825 | if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear)) |
| 2826 | return NULL; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2827 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2828 | /* Check to make sure we are based on a list */ |
| 2829 | if (! Pdata_Check(self->file)) { |
| 2830 | PyErr_SetString(PicklingError, |
| 2831 | "Attempt to getvalue() a non-list-based pickler"); |
| 2832 | return NULL; |
| 2833 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2834 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2835 | /* flush write buffer */ |
| 2836 | if (write_other(self, NULL, 0) < 0) return NULL; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2837 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2838 | data=(Pdata*)self->file; |
| 2839 | l=data->length; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2840 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2841 | /* set up an array to hold get/put status */ |
| 2842 | lm = PyDict_Size(self->memo); |
| 2843 | if (lm < 0) return NULL; |
| 2844 | lm++; |
| 2845 | have_get = malloc(lm); |
| 2846 | if (have_get == NULL) return PyErr_NoMemory(); |
| 2847 | memset(have_get, 0, lm); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2848 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2849 | /* Scan for gets. */ |
| 2850 | for (rsize = 0, i = l; --i >= 0; ) { |
| 2851 | k = data->data[i]; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2852 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2853 | if (PyString_Check(k)) |
| 2854 | rsize += PyString_GET_SIZE(k); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2855 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2856 | else if (PyInt_Check(k)) { /* put */ |
| 2857 | ik = PyInt_AS_LONG((PyIntObject*)k); |
| 2858 | if (ik >= lm || ik == 0) { |
| 2859 | PyErr_SetString(PicklingError, |
| 2860 | "Invalid get data"); |
| 2861 | goto err; |
| 2862 | } |
| 2863 | if (have_get[ik]) /* with matching get */ |
| 2864 | rsize += ik < 256 ? 2 : 5; |
| 2865 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2866 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2867 | else if (! (PyTuple_Check(k) && |
| 2868 | PyTuple_GET_SIZE(k) == 2 && |
| 2869 | PyInt_Check((k = PyTuple_GET_ITEM(k, 0)))) |
| 2870 | ) { |
| 2871 | PyErr_SetString(PicklingError, |
| 2872 | "Unexpected data in internal list"); |
| 2873 | goto err; |
| 2874 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2875 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2876 | else { /* put */ |
| 2877 | ik = PyInt_AS_LONG((PyIntObject *)k); |
| 2878 | if (ik >= lm || ik == 0) { |
| 2879 | PyErr_SetString(PicklingError, |
| 2880 | "Invalid get data"); |
| 2881 | return NULL; |
| 2882 | } |
| 2883 | have_get[ik] = 1; |
| 2884 | rsize += ik < 256 ? 2 : 5; |
| 2885 | } |
| 2886 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2887 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2888 | /* Now generate the result */ |
| 2889 | r = PyString_FromStringAndSize(NULL, rsize); |
| 2890 | if (r == NULL) goto err; |
| 2891 | s = PyString_AS_STRING((PyStringObject *)r); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2892 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2893 | for (i = 0; i < l; i++) { |
| 2894 | k = data->data[i]; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2895 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2896 | if (PyString_Check(k)) { |
| 2897 | ssize = PyString_GET_SIZE(k); |
| 2898 | if (ssize) { |
| 2899 | p=PyString_AS_STRING((PyStringObject *)k); |
| 2900 | while (--ssize >= 0) |
| 2901 | *s++ = *p++; |
| 2902 | } |
| 2903 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2904 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2905 | else if (PyTuple_Check(k)) { /* get */ |
| 2906 | ik = PyInt_AS_LONG((PyIntObject *) |
| 2907 | PyTuple_GET_ITEM(k, 0)); |
| 2908 | if (ik < 256) { |
| 2909 | *s++ = BINGET; |
| 2910 | *s++ = (int)(ik & 0xff); |
| 2911 | } |
| 2912 | else { |
| 2913 | *s++ = LONG_BINGET; |
| 2914 | *s++ = (int)(ik & 0xff); |
| 2915 | *s++ = (int)((ik >> 8) & 0xff); |
| 2916 | *s++ = (int)((ik >> 16) & 0xff); |
| 2917 | *s++ = (int)((ik >> 24) & 0xff); |
| 2918 | } |
| 2919 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2920 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2921 | else { /* put */ |
| 2922 | ik = PyInt_AS_LONG((PyIntObject*)k); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2923 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2924 | if (have_get[ik]) { /* with matching get */ |
| 2925 | if (ik < 256) { |
| 2926 | *s++ = BINPUT; |
| 2927 | *s++ = (int)(ik & 0xff); |
| 2928 | } |
| 2929 | else { |
| 2930 | *s++ = LONG_BINPUT; |
| 2931 | *s++ = (int)(ik & 0xff); |
| 2932 | *s++ = (int)((ik >> 8) & 0xff); |
| 2933 | *s++ = (int)((ik >> 16) & 0xff); |
| 2934 | *s++ = (int)((ik >> 24) & 0xff); |
| 2935 | } |
| 2936 | } |
| 2937 | } |
| 2938 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2939 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2940 | if (clear) { |
| 2941 | PyDict_Clear(self->memo); |
| 2942 | Pdata_clear(data, 0); |
| 2943 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2944 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2945 | free(have_get); |
| 2946 | return r; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2947 | err: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2948 | free(have_get); |
| 2949 | return NULL; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 2950 | } |
| 2951 | |
| 2952 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2953 | Pickler_dump(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2954 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2955 | PyObject *ob; |
| 2956 | int get=0; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 2957 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2958 | if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get))) |
| 2959 | return NULL; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 2960 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2961 | if (dump(self, ob) < 0) |
| 2962 | return NULL; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 2963 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2964 | if (get) return Pickle_getvalue(self, NULL); |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 2965 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2966 | /* XXX Why does dump() return self? */ |
| 2967 | Py_INCREF(self); |
| 2968 | return (PyObject*)self; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2969 | } |
| 2970 | |
| 2971 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2972 | static struct PyMethodDef Pickler_methods[] = |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2973 | { |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 2974 | {"dump", (PyCFunction)Pickler_dump, METH_VARARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 2975 | PyDoc_STR("dump(object) -- " |
| 2976 | "Write an object in pickle format to the object's pickle stream")}, |
Fred Drake | 0ebacc8 | 2002-05-01 20:36:39 +0000 | [diff] [blame] | 2977 | {"clear_memo", (PyCFunction)Pickle_clear_memo, METH_NOARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 2978 | PyDoc_STR("clear_memo() -- Clear the picklers memo")}, |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 2979 | {"getvalue", (PyCFunction)Pickle_getvalue, METH_VARARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 2980 | PyDoc_STR("getvalue() -- Finish picking a list-based pickle")}, |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2981 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2982 | }; |
| 2983 | |
| 2984 | |
| 2985 | static Picklerobject * |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 2986 | newPicklerobject(PyObject *file, int proto) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2987 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2988 | Picklerobject *self; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2989 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2990 | if (proto < 0) |
| 2991 | proto = HIGHEST_PROTOCOL; |
| 2992 | if (proto > HIGHEST_PROTOCOL) { |
| 2993 | PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; " |
| 2994 | "the highest available protocol is %d", |
| 2995 | proto, HIGHEST_PROTOCOL); |
| 2996 | return NULL; |
| 2997 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2998 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2999 | self = PyObject_GC_New(Picklerobject, &Picklertype); |
| 3000 | if (self == NULL) |
| 3001 | return NULL; |
| 3002 | self->proto = proto; |
| 3003 | self->bin = proto > 0; |
| 3004 | self->fp = NULL; |
| 3005 | self->write = NULL; |
| 3006 | self->memo = NULL; |
| 3007 | self->arg = NULL; |
| 3008 | self->pers_func = NULL; |
| 3009 | self->inst_pers_func = NULL; |
| 3010 | self->write_buf = NULL; |
| 3011 | self->fast = 0; |
| 3012 | self->fast_container = 0; |
| 3013 | self->fast_memo = NULL; |
| 3014 | self->buf_size = 0; |
| 3015 | self->dispatch_table = NULL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3016 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3017 | self->file = NULL; |
| 3018 | if (file) |
| 3019 | Py_INCREF(file); |
| 3020 | else { |
| 3021 | file = Pdata_New(); |
| 3022 | if (file == NULL) |
| 3023 | goto err; |
| 3024 | } |
| 3025 | self->file = file; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 3026 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3027 | if (!( self->memo = PyDict_New())) |
| 3028 | goto err; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3029 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3030 | if (PyFile_Check(file)) { |
| 3031 | self->fp = PyFile_AsFile(file); |
| 3032 | if (self->fp == NULL) { |
| 3033 | PyErr_SetString(PyExc_ValueError, |
| 3034 | "I/O operation on closed file"); |
| 3035 | goto err; |
| 3036 | } |
| 3037 | self->write_func = write_file; |
| 3038 | } |
| 3039 | else if (PycStringIO_OutputCheck(file)) { |
| 3040 | self->write_func = write_cStringIO; |
| 3041 | } |
| 3042 | else if (file == Py_None) { |
| 3043 | self->write_func = write_none; |
| 3044 | } |
| 3045 | else { |
| 3046 | self->write_func = write_other; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3047 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3048 | if (! Pdata_Check(file)) { |
| 3049 | self->write = PyObject_GetAttr(file, write_str); |
| 3050 | if (!self->write) { |
| 3051 | PyErr_Clear(); |
| 3052 | PyErr_SetString(PyExc_TypeError, |
| 3053 | "argument must have 'write' " |
| 3054 | "attribute"); |
| 3055 | goto err; |
| 3056 | } |
| 3057 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3058 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3059 | self->write_buf = (char *)PyMem_Malloc(WRITE_BUF_SIZE); |
| 3060 | if (self->write_buf == NULL) { |
| 3061 | PyErr_NoMemory(); |
| 3062 | goto err; |
| 3063 | } |
| 3064 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3065 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3066 | if (PyEval_GetRestricted()) { |
| 3067 | /* Restricted execution, get private tables */ |
| 3068 | PyObject *m = PyImport_ImportModule("copy_reg"); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3069 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3070 | if (m == NULL) |
| 3071 | goto err; |
| 3072 | self->dispatch_table = PyObject_GetAttr(m, dispatch_table_str); |
| 3073 | Py_DECREF(m); |
| 3074 | if (self->dispatch_table == NULL) |
| 3075 | goto err; |
| 3076 | } |
| 3077 | else { |
| 3078 | self->dispatch_table = dispatch_table; |
| 3079 | Py_INCREF(dispatch_table); |
| 3080 | } |
| 3081 | PyObject_GC_Track(self); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3082 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3083 | return self; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3084 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3085 | err: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3086 | Py_DECREF(self); |
| 3087 | return NULL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3088 | } |
| 3089 | |
| 3090 | |
| 3091 | static PyObject * |
Martin v. Löwis | 544f119 | 2004-07-27 05:22:33 +0000 | [diff] [blame] | 3092 | get_Pickler(PyObject *self, PyObject *args, PyObject *kwds) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3093 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3094 | static char *kwlist[] = {"file", "protocol", NULL}; |
| 3095 | PyObject *file = NULL; |
| 3096 | int proto = 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3097 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3098 | /* XXX |
| 3099 | * The documented signature is Pickler(file, protocol=0), but this |
| 3100 | * accepts Pickler() and Pickler(integer) too. The meaning then |
| 3101 | * is clear as mud, undocumented, and not supported by pickle.py. |
| 3102 | * I'm told Zope uses this, but I haven't traced into this code |
| 3103 | * far enough to figure out what it means. |
| 3104 | */ |
| 3105 | if (!PyArg_ParseTuple(args, "|i:Pickler", &proto)) { |
| 3106 | PyErr_Clear(); |
| 3107 | proto = 0; |
| 3108 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|i:Pickler", |
| 3109 | kwlist, &file, &proto)) |
| 3110 | return NULL; |
| 3111 | } |
| 3112 | return (PyObject *)newPicklerobject(file, proto); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3113 | } |
| 3114 | |
| 3115 | |
| 3116 | static void |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3117 | Pickler_dealloc(Picklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3118 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3119 | PyObject_GC_UnTrack(self); |
| 3120 | Py_XDECREF(self->write); |
| 3121 | Py_XDECREF(self->memo); |
| 3122 | Py_XDECREF(self->fast_memo); |
| 3123 | Py_XDECREF(self->arg); |
| 3124 | Py_XDECREF(self->file); |
| 3125 | Py_XDECREF(self->pers_func); |
| 3126 | Py_XDECREF(self->inst_pers_func); |
| 3127 | Py_XDECREF(self->dispatch_table); |
| 3128 | PyMem_Free(self->write_buf); |
| 3129 | Py_TYPE(self)->tp_free((PyObject *)self); |
Jeremy Hylton | 4cf6319 | 2003-04-09 21:05:12 +0000 | [diff] [blame] | 3130 | } |
| 3131 | |
| 3132 | static int |
| 3133 | Pickler_traverse(Picklerobject *self, visitproc visit, void *arg) |
| 3134 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3135 | Py_VISIT(self->write); |
| 3136 | Py_VISIT(self->memo); |
| 3137 | Py_VISIT(self->fast_memo); |
| 3138 | Py_VISIT(self->arg); |
| 3139 | Py_VISIT(self->file); |
| 3140 | Py_VISIT(self->pers_func); |
| 3141 | Py_VISIT(self->inst_pers_func); |
| 3142 | Py_VISIT(self->dispatch_table); |
| 3143 | return 0; |
Jeremy Hylton | 4cf6319 | 2003-04-09 21:05:12 +0000 | [diff] [blame] | 3144 | } |
| 3145 | |
| 3146 | static int |
| 3147 | Pickler_clear(Picklerobject *self) |
| 3148 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3149 | Py_CLEAR(self->write); |
| 3150 | Py_CLEAR(self->memo); |
| 3151 | Py_CLEAR(self->fast_memo); |
| 3152 | Py_CLEAR(self->arg); |
| 3153 | Py_CLEAR(self->file); |
| 3154 | Py_CLEAR(self->pers_func); |
| 3155 | Py_CLEAR(self->inst_pers_func); |
| 3156 | Py_CLEAR(self->dispatch_table); |
| 3157 | return 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3158 | } |
| 3159 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3160 | static PyObject * |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3161 | Pickler_get_pers_func(Picklerobject *p) |
| 3162 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3163 | if (p->pers_func == NULL) |
| 3164 | PyErr_SetString(PyExc_AttributeError, "persistent_id"); |
| 3165 | else |
| 3166 | Py_INCREF(p->pers_func); |
| 3167 | return p->pers_func; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3168 | } |
| 3169 | |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3170 | static int |
| 3171 | Pickler_set_pers_func(Picklerobject *p, PyObject *v) |
| 3172 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3173 | if (v == NULL) { |
| 3174 | PyErr_SetString(PyExc_TypeError, |
| 3175 | "attribute deletion is not supported"); |
| 3176 | return -1; |
| 3177 | } |
| 3178 | Py_XDECREF(p->pers_func); |
| 3179 | Py_INCREF(v); |
| 3180 | p->pers_func = v; |
| 3181 | return 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3182 | } |
| 3183 | |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3184 | static int |
| 3185 | Pickler_set_inst_pers_func(Picklerobject *p, PyObject *v) |
| 3186 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3187 | if (v == NULL) { |
| 3188 | PyErr_SetString(PyExc_TypeError, |
| 3189 | "attribute deletion is not supported"); |
| 3190 | return -1; |
| 3191 | } |
| 3192 | Py_XDECREF(p->inst_pers_func); |
| 3193 | Py_INCREF(v); |
| 3194 | p->inst_pers_func = v; |
| 3195 | return 0; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3196 | } |
| 3197 | |
| 3198 | static PyObject * |
| 3199 | Pickler_get_memo(Picklerobject *p) |
| 3200 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3201 | if (p->memo == NULL) |
| 3202 | PyErr_SetString(PyExc_AttributeError, "memo"); |
| 3203 | else |
| 3204 | Py_INCREF(p->memo); |
| 3205 | return p->memo; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3206 | } |
| 3207 | |
| 3208 | static int |
| 3209 | Pickler_set_memo(Picklerobject *p, PyObject *v) |
| 3210 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3211 | if (v == NULL) { |
| 3212 | PyErr_SetString(PyExc_TypeError, |
| 3213 | "attribute deletion is not supported"); |
| 3214 | return -1; |
| 3215 | } |
| 3216 | if (!PyDict_Check(v)) { |
| 3217 | PyErr_SetString(PyExc_TypeError, "memo must be a dictionary"); |
| 3218 | return -1; |
| 3219 | } |
| 3220 | Py_XDECREF(p->memo); |
| 3221 | Py_INCREF(v); |
| 3222 | p->memo = v; |
| 3223 | return 0; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3224 | } |
| 3225 | |
| 3226 | static PyObject * |
| 3227 | Pickler_get_error(Picklerobject *p) |
| 3228 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3229 | /* why is this an attribute on the Pickler? */ |
| 3230 | Py_INCREF(PicklingError); |
| 3231 | return PicklingError; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3232 | } |
| 3233 | |
| 3234 | static PyMemberDef Pickler_members[] = { |
| 3235 | {"binary", T_INT, offsetof(Picklerobject, bin)}, |
| 3236 | {"fast", T_INT, offsetof(Picklerobject, fast)}, |
Jeremy Hylton | 3eb46f3 | 2001-10-16 17:10:49 +0000 | [diff] [blame] | 3237 | {NULL} |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3238 | }; |
| 3239 | |
| 3240 | static PyGetSetDef Pickler_getsets[] = { |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3241 | {"persistent_id", (getter)Pickler_get_pers_func, |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3242 | (setter)Pickler_set_pers_func}, |
| 3243 | {"inst_persistent_id", NULL, (setter)Pickler_set_inst_pers_func}, |
| 3244 | {"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo}, |
Jeremy Hylton | 3eb46f3 | 2001-10-16 17:10:49 +0000 | [diff] [blame] | 3245 | {"PicklingError", (getter)Pickler_get_error, NULL}, |
| 3246 | {NULL} |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3247 | }; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3248 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3249 | PyDoc_STRVAR(Picklertype__doc__, |
| 3250 | "Objects that know how to pickle objects\n"); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3251 | |
Guido van Rossum | 9716aaa | 1997-12-08 15:15:16 +0000 | [diff] [blame] | 3252 | static PyTypeObject Picklertype = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 3253 | PyVarObject_HEAD_INIT(NULL, 0) |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 3254 | "cPickle.Pickler", /*tp_name*/ |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3255 | sizeof(Picklerobject), /*tp_basicsize*/ |
| 3256 | 0, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3257 | (destructor)Pickler_dealloc, /* tp_dealloc */ |
| 3258 | 0, /* tp_print */ |
| 3259 | 0, /* tp_getattr */ |
| 3260 | 0, /* tp_setattr */ |
| 3261 | 0, /* tp_compare */ |
| 3262 | 0, /* tp_repr */ |
| 3263 | 0, /* tp_as_number */ |
| 3264 | 0, /* tp_as_sequence */ |
| 3265 | 0, /* tp_as_mapping */ |
| 3266 | 0, /* tp_hash */ |
| 3267 | 0, /* tp_call */ |
| 3268 | 0, /* tp_str */ |
| 3269 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3270 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 3271 | 0, /* tp_as_buffer */ |
Jeremy Hylton | 4cf6319 | 2003-04-09 21:05:12 +0000 | [diff] [blame] | 3272 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3273 | Picklertype__doc__, /* tp_doc */ |
| 3274 | (traverseproc)Pickler_traverse, /* tp_traverse */ |
| 3275 | (inquiry)Pickler_clear, /* tp_clear */ |
| 3276 | 0, /* tp_richcompare */ |
| 3277 | 0, /* tp_weaklistoffset */ |
| 3278 | 0, /* tp_iter */ |
| 3279 | 0, /* tp_iternext */ |
| 3280 | Pickler_methods, /* tp_methods */ |
| 3281 | Pickler_members, /* tp_members */ |
| 3282 | Pickler_getsets, /* tp_getset */ |
Guido van Rossum | 9716aaa | 1997-12-08 15:15:16 +0000 | [diff] [blame] | 3283 | }; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3284 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3285 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3286 | 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] | 3287 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3288 | PyObject *global = 0, *module; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3289 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3290 | if (fc) { |
| 3291 | if (fc==Py_None) { |
| 3292 | PyErr_SetString(UnpicklingError, "Global and instance " |
| 3293 | "pickles are not supported."); |
| 3294 | return NULL; |
| 3295 | } |
| 3296 | return PyObject_CallFunctionObjArgs(fc, py_module_name, |
| 3297 | py_global_name, NULL); |
| 3298 | } |
Guido van Rossum | 1b9e0aa | 1999-04-19 17:58:18 +0000 | [diff] [blame] | 3299 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3300 | module = PySys_GetObject("modules"); |
| 3301 | if (module == NULL) |
| 3302 | return NULL; |
Jeremy Hylton | d105523 | 1998-08-11 19:52:51 +0000 | [diff] [blame] | 3303 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3304 | module = PyDict_GetItem(module, py_module_name); |
| 3305 | if (module == NULL) { |
| 3306 | module = PyImport_Import(py_module_name); |
| 3307 | if (!module) |
| 3308 | return NULL; |
| 3309 | global = PyObject_GetAttr(module, py_global_name); |
| 3310 | Py_DECREF(module); |
| 3311 | } |
| 3312 | else |
| 3313 | global = PyObject_GetAttr(module, py_global_name); |
| 3314 | return global; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3315 | } |
| 3316 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3317 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3318 | marker(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3319 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3320 | if (self->num_marks < 1) { |
| 3321 | PyErr_SetString(UnpicklingError, "could not find MARK"); |
| 3322 | return -1; |
| 3323 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3324 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3325 | return self->marks[--self->num_marks]; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3326 | } |
| 3327 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3328 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3329 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3330 | load_none(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3331 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3332 | PDATA_APPEND(self->stack, Py_None, -1); |
| 3333 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3334 | } |
| 3335 | |
Guido van Rossum | f9ffb03 | 1999-02-04 14:54:04 +0000 | [diff] [blame] | 3336 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3337 | bad_readline(void) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3338 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3339 | PyErr_SetString(UnpicklingError, "pickle data was truncated"); |
| 3340 | return -1; |
Guido van Rossum | f9ffb03 | 1999-02-04 14:54:04 +0000 | [diff] [blame] | 3341 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3342 | |
| 3343 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3344 | load_int(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3345 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3346 | PyObject *py_int = 0; |
| 3347 | char *endptr, *s; |
| 3348 | int len, res = -1; |
| 3349 | long l; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3350 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3351 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
| 3352 | if (len < 2) return bad_readline(); |
| 3353 | if (!( s=pystrndup(s,len))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3354 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3355 | errno = 0; |
| 3356 | l = strtol(s, &endptr, 0); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3357 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3358 | if (errno || (*endptr != '\n') || (endptr[1] != '\0')) { |
| 3359 | /* Hm, maybe we've got something long. Let's try reading |
| 3360 | it as a Python long object. */ |
| 3361 | errno = 0; |
| 3362 | py_int = PyLong_FromString(s, NULL, 0); |
| 3363 | if (py_int == NULL) { |
| 3364 | PyErr_SetString(PyExc_ValueError, |
| 3365 | "could not convert string to int"); |
| 3366 | goto finally; |
| 3367 | } |
| 3368 | } |
| 3369 | else { |
| 3370 | if (len == 3 && (l == 0 || l == 1)) { |
| 3371 | if (!( py_int = PyBool_FromLong(l))) goto finally; |
| 3372 | } |
| 3373 | else { |
| 3374 | if (!( py_int = PyInt_FromLong(l))) goto finally; |
| 3375 | } |
| 3376 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3377 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3378 | free(s); |
| 3379 | PDATA_PUSH(self->stack, py_int, -1); |
| 3380 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3381 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3382 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3383 | free(s); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3384 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3385 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3386 | } |
| 3387 | |
Tim Peters | 3c67d79 | 2003-02-02 17:59:11 +0000 | [diff] [blame] | 3388 | static int |
| 3389 | load_bool(Unpicklerobject *self, PyObject *boolean) |
| 3390 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3391 | assert(boolean == Py_True || boolean == Py_False); |
| 3392 | PDATA_APPEND(self->stack, boolean, -1); |
| 3393 | return 0; |
Tim Peters | 3c67d79 | 2003-02-02 17:59:11 +0000 | [diff] [blame] | 3394 | } |
| 3395 | |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 3396 | /* s contains x bytes of a little-endian integer. Return its value as a |
| 3397 | * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian |
| 3398 | * int, but when x is 4 it's a signed one. This is an historical source |
| 3399 | * of x-platform bugs. |
| 3400 | */ |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3401 | static long |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 3402 | calc_binint(char *s, int x) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3403 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3404 | unsigned char c; |
| 3405 | int i; |
| 3406 | long l; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3407 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3408 | for (i = 0, l = 0L; i < x; i++) { |
| 3409 | c = (unsigned char)s[i]; |
| 3410 | l |= (long)c << (i * 8); |
| 3411 | } |
Tim Peters | bfa18f7 | 2001-04-10 01:54:42 +0000 | [diff] [blame] | 3412 | #if SIZEOF_LONG > 4 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3413 | /* Unlike BININT1 and BININT2, BININT (more accurately BININT4) |
| 3414 | * is signed, so on a box with longs bigger than 4 bytes we need |
| 3415 | * to extend a BININT's sign bit to the full width. |
| 3416 | */ |
| 3417 | if (x == 4 && l & (1L << 31)) |
| 3418 | l |= (~0L) << 32; |
Tim Peters | bfa18f7 | 2001-04-10 01:54:42 +0000 | [diff] [blame] | 3419 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3420 | return l; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3421 | } |
| 3422 | |
| 3423 | |
| 3424 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3425 | load_binintx(Unpicklerobject *self, char *s, int x) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3426 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3427 | PyObject *py_int = 0; |
| 3428 | long l; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3429 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3430 | l = calc_binint(s, x); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3431 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3432 | if (!( py_int = PyInt_FromLong(l))) |
| 3433 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3434 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3435 | PDATA_PUSH(self->stack, py_int, -1); |
| 3436 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3437 | } |
| 3438 | |
| 3439 | |
| 3440 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3441 | load_binint(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3442 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3443 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3444 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3445 | if (self->read_func(self, &s, 4) < 0) |
| 3446 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3447 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3448 | return load_binintx(self, s, 4); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3449 | } |
| 3450 | |
| 3451 | |
| 3452 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3453 | load_binint1(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3454 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3455 | char *s; |
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 | if (self->read_func(self, &s, 1) < 0) |
| 3458 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3459 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3460 | return load_binintx(self, s, 1); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3461 | } |
| 3462 | |
| 3463 | |
| 3464 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3465 | load_binint2(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3466 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3467 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3468 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3469 | if (self->read_func(self, &s, 2) < 0) |
| 3470 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3471 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3472 | return load_binintx(self, s, 2); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3473 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3474 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3475 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3476 | load_long(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3477 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3478 | PyObject *l = 0; |
| 3479 | char *end, *s; |
| 3480 | int len, res = -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3481 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3482 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
| 3483 | if (len < 2) return bad_readline(); |
| 3484 | if (!( s=pystrndup(s,len))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3485 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3486 | if (!( l = PyLong_FromString(s, &end, 0))) |
| 3487 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3488 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3489 | free(s); |
| 3490 | PDATA_PUSH(self->stack, l, -1); |
| 3491 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3492 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3493 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3494 | free(s); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3495 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3496 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3497 | } |
| 3498 | |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 3499 | /* 'size' bytes contain the # of bytes of little-endian 256's-complement |
| 3500 | * data following. |
| 3501 | */ |
| 3502 | static int |
| 3503 | load_counted_long(Unpicklerobject *self, int size) |
| 3504 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3505 | Py_ssize_t i; |
| 3506 | char *nbytes; |
| 3507 | unsigned char *pdata; |
| 3508 | PyObject *along; |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 3509 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3510 | assert(size == 1 || size == 4); |
| 3511 | i = self->read_func(self, &nbytes, size); |
| 3512 | if (i < 0) return -1; |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 3513 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3514 | size = calc_binint(nbytes, size); |
| 3515 | if (size < 0) { |
| 3516 | /* Corrupt or hostile pickle -- we never write one like |
| 3517 | * this. |
| 3518 | */ |
| 3519 | PyErr_SetString(UnpicklingError, "LONG pickle has negative " |
| 3520 | "byte count"); |
| 3521 | return -1; |
| 3522 | } |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 3523 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3524 | if (size == 0) |
| 3525 | along = PyLong_FromLong(0L); |
| 3526 | else { |
| 3527 | /* Read the raw little-endian bytes & convert. */ |
| 3528 | i = self->read_func(self, (char **)&pdata, size); |
| 3529 | if (i < 0) return -1; |
| 3530 | along = _PyLong_FromByteArray(pdata, (size_t)size, |
| 3531 | 1 /* little endian */, 1 /* signed */); |
| 3532 | } |
| 3533 | if (along == NULL) |
| 3534 | return -1; |
| 3535 | PDATA_PUSH(self->stack, along, -1); |
| 3536 | return 0; |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 3537 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3538 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3539 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3540 | load_float(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3541 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3542 | PyObject *py_float = 0; |
| 3543 | char *endptr, *s; |
| 3544 | int len, res = -1; |
| 3545 | double d; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3546 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3547 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
| 3548 | if (len < 2) return bad_readline(); |
| 3549 | if (!( s=pystrndup(s,len))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3550 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3551 | d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3552 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3553 | if (d == -1.0 && PyErr_Occurred()) { |
| 3554 | goto finally; |
| 3555 | } else if ((endptr[0] != '\n') || (endptr[1] != '\0')) { |
| 3556 | PyErr_SetString(PyExc_ValueError, |
| 3557 | "could not convert string to float"); |
| 3558 | goto finally; |
| 3559 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3560 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3561 | if (!( py_float = PyFloat_FromDouble(d))) |
| 3562 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3563 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3564 | free(s); |
| 3565 | PDATA_PUSH(self->stack, py_float, -1); |
| 3566 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3567 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3568 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3569 | free(s); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3570 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3571 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3572 | } |
| 3573 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3574 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3575 | load_binfloat(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3576 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3577 | PyObject *py_float; |
| 3578 | double x; |
| 3579 | char *p; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3580 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3581 | if (self->read_func(self, &p, 8) < 0) |
| 3582 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3583 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3584 | x = _PyFloat_Unpack8((unsigned char *)p, 0); |
| 3585 | if (x == -1.0 && PyErr_Occurred()) |
| 3586 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3587 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3588 | py_float = PyFloat_FromDouble(x); |
| 3589 | if (py_float == NULL) |
| 3590 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3591 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3592 | PDATA_PUSH(self->stack, py_float, -1); |
| 3593 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3594 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3595 | |
| 3596 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3597 | load_string(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3598 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3599 | PyObject *str = 0; |
| 3600 | int len, res = -1; |
| 3601 | char *s, *p; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3602 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3603 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
| 3604 | if (len < 2) return bad_readline(); |
| 3605 | if (!( s=pystrndup(s,len))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3606 | |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 3607 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3608 | /* Strip outermost quotes */ |
| 3609 | while (s[len-1] <= ' ') |
| 3610 | len--; |
| 3611 | if(s[0]=='"' && s[len-1]=='"'){ |
| 3612 | s[len-1] = '\0'; |
| 3613 | p = s + 1 ; |
| 3614 | len -= 2; |
| 3615 | } else if(s[0]=='\'' && s[len-1]=='\''){ |
| 3616 | s[len-1] = '\0'; |
| 3617 | p = s + 1 ; |
| 3618 | len -= 2; |
| 3619 | } else |
| 3620 | goto insecure; |
| 3621 | /********************************************/ |
Guido van Rossum | 9716aaa | 1997-12-08 15:15:16 +0000 | [diff] [blame] | 3622 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3623 | str = PyString_DecodeEscape(p, len, NULL, 0, NULL); |
| 3624 | free(s); |
| 3625 | if (str) { |
| 3626 | PDATA_PUSH(self->stack, str, -1); |
| 3627 | res = 0; |
| 3628 | } |
| 3629 | return res; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3630 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3631 | insecure: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3632 | free(s); |
| 3633 | PyErr_SetString(PyExc_ValueError,"insecure string pickle"); |
| 3634 | return -1; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3635 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3636 | |
| 3637 | |
| 3638 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3639 | load_binstring(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3640 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3641 | PyObject *py_string = 0; |
| 3642 | long l; |
| 3643 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3644 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3645 | if (self->read_func(self, &s, 4) < 0) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3646 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3647 | l = calc_binint(s, 4); |
| 3648 | if (l < 0) { |
| 3649 | /* Corrupt or hostile pickle -- we never write one like |
| 3650 | * this. |
| 3651 | */ |
| 3652 | PyErr_SetString(UnpicklingError, |
| 3653 | "BINSTRING pickle has negative byte count"); |
| 3654 | return -1; |
| 3655 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3656 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3657 | if (self->read_func(self, &s, l) < 0) |
| 3658 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3659 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3660 | if (!( py_string = PyString_FromStringAndSize(s, l))) |
| 3661 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3662 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3663 | PDATA_PUSH(self->stack, py_string, -1); |
| 3664 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3665 | } |
| 3666 | |
| 3667 | |
| 3668 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3669 | load_short_binstring(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3670 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3671 | PyObject *py_string = 0; |
| 3672 | unsigned char l; |
| 3673 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3674 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3675 | if (self->read_func(self, &s, 1) < 0) |
| 3676 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3677 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3678 | l = (unsigned char)s[0]; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3679 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3680 | if (self->read_func(self, &s, l) < 0) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3681 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3682 | if (!( py_string = PyString_FromStringAndSize(s, l))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3683 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3684 | PDATA_PUSH(self->stack, py_string, -1); |
| 3685 | return 0; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3686 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3687 | |
| 3688 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3689 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3690 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3691 | load_unicode(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3692 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3693 | PyObject *str = 0; |
| 3694 | int len, res = -1; |
| 3695 | char *s; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3696 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3697 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
| 3698 | if (len < 1) return bad_readline(); |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3699 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3700 | if (!( str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL))) |
| 3701 | goto finally; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3702 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3703 | PDATA_PUSH(self->stack, str, -1); |
| 3704 | return 0; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3705 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3706 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3707 | return res; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3708 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3709 | #endif |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3710 | |
| 3711 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3712 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3713 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3714 | load_binunicode(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3715 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3716 | PyObject *unicode; |
| 3717 | long l; |
| 3718 | char *s; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3719 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3720 | if (self->read_func(self, &s, 4) < 0) return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3721 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3722 | l = calc_binint(s, 4); |
| 3723 | if (l < 0) { |
| 3724 | /* Corrupt or hostile pickle -- we never write one like |
| 3725 | * this. |
| 3726 | */ |
| 3727 | PyErr_SetString(UnpicklingError, |
| 3728 | "BINUNICODE pickle has negative byte count"); |
| 3729 | return -1; |
| 3730 | } |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3731 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3732 | if (self->read_func(self, &s, l) < 0) |
| 3733 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3734 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3735 | if (!( unicode = PyUnicode_DecodeUTF8(s, l, NULL))) |
| 3736 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3737 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3738 | PDATA_PUSH(self->stack, unicode, -1); |
| 3739 | return 0; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3740 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3741 | #endif |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3742 | |
| 3743 | |
| 3744 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3745 | load_tuple(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 *tup; |
| 3748 | int i; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3749 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3750 | if ((i = marker(self)) < 0) return -1; |
| 3751 | if (!( tup=Pdata_popTuple(self->stack, i))) return -1; |
| 3752 | PDATA_PUSH(self->stack, tup, -1); |
| 3753 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3754 | } |
| 3755 | |
| 3756 | static int |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 3757 | load_counted_tuple(Unpicklerobject *self, int len) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3758 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3759 | PyObject *tup = PyTuple_New(len); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3760 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3761 | if (tup == NULL) |
| 3762 | return -1; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 3763 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3764 | while (--len >= 0) { |
| 3765 | PyObject *element; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 3766 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3767 | PDATA_POP(self->stack, element); |
| 3768 | if (element == NULL) |
| 3769 | return -1; |
| 3770 | PyTuple_SET_ITEM(tup, len, element); |
| 3771 | } |
| 3772 | PDATA_PUSH(self->stack, tup, -1); |
| 3773 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3774 | } |
| 3775 | |
| 3776 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3777 | load_empty_list(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3778 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3779 | PyObject *list; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3780 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3781 | if (!( list=PyList_New(0))) return -1; |
| 3782 | PDATA_PUSH(self->stack, list, -1); |
| 3783 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3784 | } |
| 3785 | |
| 3786 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3787 | load_empty_dict(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3788 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3789 | PyObject *dict; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3790 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3791 | if (!( dict=PyDict_New())) return -1; |
| 3792 | PDATA_PUSH(self->stack, dict, -1); |
| 3793 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3794 | } |
| 3795 | |
| 3796 | |
| 3797 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3798 | load_list(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3799 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3800 | PyObject *list = 0; |
| 3801 | int i; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3802 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3803 | if ((i = marker(self)) < 0) return -1; |
| 3804 | if (!( list=Pdata_popList(self->stack, i))) return -1; |
| 3805 | PDATA_PUSH(self->stack, list, -1); |
| 3806 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3807 | } |
| 3808 | |
| 3809 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3810 | load_dict(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3811 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3812 | PyObject *dict, *key, *value; |
| 3813 | int i, j, k; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3814 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3815 | if ((i = marker(self)) < 0) return -1; |
| 3816 | j=self->stack->length; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3817 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3818 | if (!( dict = PyDict_New())) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3819 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3820 | for (k = i+1; k < j; k += 2) { |
| 3821 | key =self->stack->data[k-1]; |
| 3822 | value=self->stack->data[k ]; |
| 3823 | if (PyDict_SetItem(dict, key, value) < 0) { |
| 3824 | Py_DECREF(dict); |
| 3825 | return -1; |
| 3826 | } |
| 3827 | } |
| 3828 | Pdata_clear(self->stack, i); |
| 3829 | PDATA_PUSH(self->stack, dict, -1); |
| 3830 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3831 | } |
| 3832 | |
| 3833 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3834 | Instance_New(PyObject *cls, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3835 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3836 | PyObject *r = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3837 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3838 | if (PyClass_Check(cls)) { |
| 3839 | int l; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3840 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3841 | if ((l=PyObject_Size(args)) < 0) goto err; |
| 3842 | if (!( l )) { |
| 3843 | PyObject *__getinitargs__; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3844 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3845 | __getinitargs__ = PyObject_GetAttr(cls, |
| 3846 | __getinitargs___str); |
| 3847 | if (!__getinitargs__) { |
| 3848 | /* We have a class with no __getinitargs__, |
| 3849 | so bypass usual construction */ |
| 3850 | PyObject *inst; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3851 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3852 | PyErr_Clear(); |
| 3853 | if (!( inst=PyInstance_NewRaw(cls, NULL))) |
| 3854 | goto err; |
| 3855 | return inst; |
| 3856 | } |
| 3857 | Py_DECREF(__getinitargs__); |
| 3858 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3859 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3860 | if ((r=PyInstance_New(cls, args, NULL))) return r; |
| 3861 | else goto err; |
| 3862 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3863 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3864 | if ((r=PyObject_CallObject(cls, args))) return r; |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 3865 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3866 | err: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3867 | { |
| 3868 | PyObject *tp, *v, *tb, *tmp_value; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3869 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3870 | PyErr_Fetch(&tp, &v, &tb); |
| 3871 | tmp_value = v; |
| 3872 | /* NULL occurs when there was a KeyboardInterrupt */ |
| 3873 | if (tmp_value == NULL) |
| 3874 | tmp_value = Py_None; |
| 3875 | if ((r = PyTuple_Pack(3, tmp_value, cls, args))) { |
| 3876 | Py_XDECREF(v); |
| 3877 | v=r; |
| 3878 | } |
| 3879 | PyErr_Restore(tp,v,tb); |
| 3880 | } |
| 3881 | return NULL; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3882 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3883 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3884 | |
| 3885 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3886 | load_obj(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3887 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3888 | PyObject *class, *tup, *obj=0; |
| 3889 | int i; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3890 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3891 | if ((i = marker(self)) < 0) return -1; |
| 3892 | if (!( tup=Pdata_popTuple(self->stack, i+1))) return -1; |
| 3893 | PDATA_POP(self->stack, class); |
| 3894 | if (class) { |
| 3895 | obj = Instance_New(class, tup); |
| 3896 | Py_DECREF(class); |
| 3897 | } |
| 3898 | Py_DECREF(tup); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3899 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3900 | if (! obj) return -1; |
| 3901 | PDATA_PUSH(self->stack, obj, -1); |
| 3902 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3903 | } |
| 3904 | |
| 3905 | |
| 3906 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3907 | load_inst(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3908 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3909 | PyObject *tup, *class=0, *obj=0, *module_name, *class_name; |
| 3910 | int i, len; |
| 3911 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3912 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3913 | if ((i = marker(self)) < 0) return -1; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3914 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3915 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
| 3916 | if (len < 2) return bad_readline(); |
| 3917 | module_name = PyString_FromStringAndSize(s, len - 1); |
| 3918 | if (!module_name) return -1; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3919 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3920 | if ((len = self->readline_func(self, &s)) >= 0) { |
| 3921 | if (len < 2) return bad_readline(); |
| 3922 | if ((class_name = PyString_FromStringAndSize(s, len - 1))) { |
| 3923 | class = find_class(module_name, class_name, |
| 3924 | self->find_class); |
| 3925 | Py_DECREF(class_name); |
| 3926 | } |
| 3927 | } |
| 3928 | Py_DECREF(module_name); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3929 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3930 | if (! class) return -1; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3931 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3932 | if ((tup=Pdata_popTuple(self->stack, i))) { |
| 3933 | obj = Instance_New(class, tup); |
| 3934 | Py_DECREF(tup); |
| 3935 | } |
| 3936 | Py_DECREF(class); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3937 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3938 | if (! obj) return -1; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3939 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3940 | PDATA_PUSH(self->stack, obj, -1); |
| 3941 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3942 | } |
| 3943 | |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 3944 | static int |
| 3945 | load_newobj(Unpicklerobject *self) |
| 3946 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3947 | PyObject *args = NULL; |
| 3948 | PyObject *clsraw = NULL; |
| 3949 | PyTypeObject *cls; /* clsraw cast to its true type */ |
| 3950 | PyObject *obj; |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 3951 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3952 | /* Stack is ... cls argtuple, and we want to call |
| 3953 | * cls.__new__(cls, *argtuple). |
| 3954 | */ |
| 3955 | PDATA_POP(self->stack, args); |
| 3956 | if (args == NULL) goto Fail; |
| 3957 | if (! PyTuple_Check(args)) { |
| 3958 | PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg " |
| 3959 | "tuple."); |
| 3960 | goto Fail; |
| 3961 | } |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 3962 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3963 | PDATA_POP(self->stack, clsraw); |
| 3964 | cls = (PyTypeObject *)clsraw; |
| 3965 | if (cls == NULL) goto Fail; |
| 3966 | if (! PyType_Check(cls)) { |
| 3967 | PyErr_SetString(UnpicklingError, "NEWOBJ class argument " |
| 3968 | "isn't a type object"); |
| 3969 | goto Fail; |
| 3970 | } |
| 3971 | if (cls->tp_new == NULL) { |
| 3972 | PyErr_SetString(UnpicklingError, "NEWOBJ class argument " |
| 3973 | "has NULL tp_new"); |
| 3974 | goto Fail; |
| 3975 | } |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 3976 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3977 | /* Call __new__. */ |
| 3978 | obj = cls->tp_new(cls, args, NULL); |
| 3979 | if (obj == NULL) goto Fail; |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 3980 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3981 | Py_DECREF(args); |
| 3982 | Py_DECREF(clsraw); |
| 3983 | PDATA_PUSH(self->stack, obj, -1); |
| 3984 | return 0; |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 3985 | |
| 3986 | Fail: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3987 | Py_XDECREF(args); |
| 3988 | Py_XDECREF(clsraw); |
| 3989 | return -1; |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 3990 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3991 | |
| 3992 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3993 | load_global(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3994 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3995 | PyObject *class = 0, *module_name = 0, *class_name = 0; |
| 3996 | int len; |
| 3997 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3998 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3999 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
| 4000 | if (len < 2) return bad_readline(); |
| 4001 | module_name = PyString_FromStringAndSize(s, len - 1); |
| 4002 | if (!module_name) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4003 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4004 | if ((len = self->readline_func(self, &s)) >= 0) { |
| 4005 | if (len < 2) { |
| 4006 | Py_DECREF(module_name); |
| 4007 | return bad_readline(); |
| 4008 | } |
| 4009 | if ((class_name = PyString_FromStringAndSize(s, len - 1))) { |
| 4010 | class = find_class(module_name, class_name, |
| 4011 | self->find_class); |
| 4012 | Py_DECREF(class_name); |
| 4013 | } |
| 4014 | } |
| 4015 | Py_DECREF(module_name); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4016 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4017 | if (! class) return -1; |
| 4018 | PDATA_PUSH(self->stack, class, -1); |
| 4019 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4020 | } |
| 4021 | |
| 4022 | |
| 4023 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4024 | load_persid(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4025 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4026 | PyObject *pid = 0; |
| 4027 | int len; |
| 4028 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4029 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4030 | if (self->pers_func) { |
| 4031 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
| 4032 | if (len < 2) return bad_readline(); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4033 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4034 | pid = PyString_FromStringAndSize(s, len - 1); |
| 4035 | if (!pid) return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4036 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4037 | if (PyList_Check(self->pers_func)) { |
| 4038 | if (PyList_Append(self->pers_func, pid) < 0) { |
| 4039 | Py_DECREF(pid); |
| 4040 | return -1; |
| 4041 | } |
| 4042 | } |
| 4043 | else { |
| 4044 | ARG_TUP(self, pid); |
| 4045 | if (self->arg) { |
| 4046 | pid = PyObject_Call(self->pers_func, self->arg, |
| 4047 | NULL); |
| 4048 | FREE_ARG_TUP(self); |
| 4049 | } |
| 4050 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4051 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4052 | if (! pid) return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4053 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4054 | PDATA_PUSH(self->stack, pid, -1); |
| 4055 | return 0; |
| 4056 | } |
| 4057 | else { |
| 4058 | PyErr_SetString(UnpicklingError, |
| 4059 | "A load persistent id instruction was encountered,\n" |
| 4060 | "but no persistent_load function was specified."); |
| 4061 | return -1; |
| 4062 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4063 | } |
| 4064 | |
| 4065 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4066 | load_binpersid(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4067 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4068 | PyObject *pid = 0; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4069 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4070 | if (self->pers_func) { |
| 4071 | PDATA_POP(self->stack, pid); |
| 4072 | if (! pid) return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4073 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4074 | if (PyList_Check(self->pers_func)) { |
| 4075 | if (PyList_Append(self->pers_func, pid) < 0) { |
| 4076 | Py_DECREF(pid); |
| 4077 | return -1; |
| 4078 | } |
| 4079 | } |
| 4080 | else { |
| 4081 | ARG_TUP(self, pid); |
| 4082 | if (self->arg) { |
| 4083 | pid = PyObject_Call(self->pers_func, self->arg, |
| 4084 | NULL); |
| 4085 | FREE_ARG_TUP(self); |
| 4086 | } |
| 4087 | if (! pid) return -1; |
| 4088 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4089 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4090 | PDATA_PUSH(self->stack, pid, -1); |
| 4091 | return 0; |
| 4092 | } |
| 4093 | else { |
| 4094 | PyErr_SetString(UnpicklingError, |
| 4095 | "A load persistent id instruction was encountered,\n" |
| 4096 | "but no persistent_load function was specified."); |
| 4097 | return -1; |
| 4098 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4099 | } |
| 4100 | |
| 4101 | |
| 4102 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4103 | load_pop(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4104 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4105 | int len = self->stack->length; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4106 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4107 | /* Note that we split the (pickle.py) stack into two stacks, |
| 4108 | an object stack and a mark stack. We have to be clever and |
| 4109 | pop the right one. We do this by looking at the top of the |
| 4110 | mark stack first, and only signalling a stack underflow if |
| 4111 | the object stack is empty and the mark stack doesn't match |
| 4112 | our expectations. |
| 4113 | */ |
| 4114 | if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) { |
| 4115 | self->num_marks--; |
| 4116 | } else if (len > 0) { |
| 4117 | len--; |
| 4118 | Py_DECREF(self->stack->data[len]); |
| 4119 | self->stack->length = len; |
| 4120 | } else { |
| 4121 | return stackUnderflow(); |
| 4122 | } |
| 4123 | return 0; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4124 | } |
| 4125 | |
| 4126 | |
| 4127 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4128 | load_pop_mark(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4129 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4130 | int i; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4131 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4132 | if ((i = marker(self)) < 0) |
| 4133 | return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4134 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4135 | Pdata_clear(self->stack, i); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4136 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4137 | return 0; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4138 | } |
| 4139 | |
| 4140 | |
| 4141 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4142 | load_dup(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4143 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4144 | PyObject *last; |
| 4145 | int len; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4146 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4147 | if ((len = self->stack->length) <= 0) return stackUnderflow(); |
| 4148 | last=self->stack->data[len-1]; |
| 4149 | Py_INCREF(last); |
| 4150 | PDATA_PUSH(self->stack, last, -1); |
| 4151 | return 0; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4152 | } |
| 4153 | |
| 4154 | |
| 4155 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4156 | load_get(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4157 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4158 | PyObject *py_str = 0, *value = 0; |
| 4159 | int len; |
| 4160 | char *s; |
| 4161 | int rc; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4162 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4163 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
| 4164 | if (len < 2) return bad_readline(); |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4165 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4166 | if (!( py_str = PyString_FromStringAndSize(s, len - 1))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4167 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4168 | value = PyDict_GetItem(self->memo, py_str); |
| 4169 | if (! value) { |
| 4170 | PyErr_SetObject(BadPickleGet, py_str); |
| 4171 | rc = -1; |
| 4172 | } |
| 4173 | else { |
| 4174 | PDATA_APPEND(self->stack, value, -1); |
| 4175 | rc = 0; |
| 4176 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4177 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4178 | Py_DECREF(py_str); |
| 4179 | return rc; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4180 | } |
| 4181 | |
| 4182 | |
| 4183 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4184 | load_binget(Unpicklerobject *self) |
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 | PyObject *py_key = 0, *value = 0; |
| 4187 | unsigned char key; |
| 4188 | char *s; |
| 4189 | int rc; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4190 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4191 | if (self->read_func(self, &s, 1) < 0) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4192 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4193 | key = (unsigned char)s[0]; |
| 4194 | if (!( py_key = PyInt_FromLong((long)key))) return -1; |
Guido van Rossum | ea2b715 | 2000-05-09 18:14:50 +0000 | [diff] [blame] | 4195 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4196 | value = PyDict_GetItem(self->memo, py_key); |
| 4197 | if (! value) { |
| 4198 | PyErr_SetObject(BadPickleGet, py_key); |
| 4199 | rc = -1; |
| 4200 | } |
| 4201 | else { |
| 4202 | PDATA_APPEND(self->stack, value, -1); |
| 4203 | rc = 0; |
| 4204 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4205 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4206 | Py_DECREF(py_key); |
| 4207 | return rc; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4208 | } |
| 4209 | |
| 4210 | |
| 4211 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4212 | load_long_binget(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4213 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4214 | PyObject *py_key = 0, *value = 0; |
| 4215 | unsigned char c; |
| 4216 | char *s; |
| 4217 | long key; |
| 4218 | int rc; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4219 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4220 | if (self->read_func(self, &s, 4) < 0) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4221 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4222 | c = (unsigned char)s[0]; |
| 4223 | key = (long)c; |
| 4224 | c = (unsigned char)s[1]; |
| 4225 | key |= (long)c << 8; |
| 4226 | c = (unsigned char)s[2]; |
| 4227 | key |= (long)c << 16; |
| 4228 | c = (unsigned char)s[3]; |
| 4229 | key |= (long)c << 24; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4230 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4231 | if (!( py_key = PyInt_FromLong((long)key))) return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4232 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4233 | value = PyDict_GetItem(self->memo, py_key); |
| 4234 | if (! value) { |
| 4235 | PyErr_SetObject(BadPickleGet, py_key); |
| 4236 | rc = -1; |
| 4237 | } |
| 4238 | else { |
| 4239 | PDATA_APPEND(self->stack, value, -1); |
| 4240 | rc = 0; |
| 4241 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4242 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4243 | Py_DECREF(py_key); |
| 4244 | return rc; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4245 | } |
| 4246 | |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 4247 | /* Push an object from the extension registry (EXT[124]). nbytes is |
| 4248 | * the number of bytes following the opcode, holding the index (code) value. |
| 4249 | */ |
| 4250 | static int |
| 4251 | load_extension(Unpicklerobject *self, int nbytes) |
| 4252 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4253 | char *codebytes; /* the nbytes bytes after the opcode */ |
| 4254 | long code; /* calc_binint returns long */ |
| 4255 | PyObject *py_code; /* code as a Python int */ |
| 4256 | PyObject *obj; /* the object to push */ |
| 4257 | PyObject *pair; /* (module_name, class_name) */ |
| 4258 | PyObject *module_name, *class_name; |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 4259 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4260 | assert(nbytes == 1 || nbytes == 2 || nbytes == 4); |
| 4261 | if (self->read_func(self, &codebytes, nbytes) < 0) return -1; |
| 4262 | code = calc_binint(codebytes, nbytes); |
| 4263 | if (code <= 0) { /* note that 0 is forbidden */ |
| 4264 | /* Corrupt or hostile pickle. */ |
| 4265 | PyErr_SetString(UnpicklingError, "EXT specifies code <= 0"); |
| 4266 | return -1; |
| 4267 | } |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 4268 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4269 | /* Look for the code in the cache. */ |
| 4270 | py_code = PyInt_FromLong(code); |
| 4271 | if (py_code == NULL) return -1; |
| 4272 | obj = PyDict_GetItem(extension_cache, py_code); |
| 4273 | if (obj != NULL) { |
| 4274 | /* Bingo. */ |
| 4275 | Py_DECREF(py_code); |
| 4276 | PDATA_APPEND(self->stack, obj, -1); |
| 4277 | return 0; |
| 4278 | } |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 4279 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4280 | /* Look up the (module_name, class_name) pair. */ |
| 4281 | pair = PyDict_GetItem(inverted_registry, py_code); |
| 4282 | if (pair == NULL) { |
| 4283 | Py_DECREF(py_code); |
| 4284 | PyErr_Format(PyExc_ValueError, "unregistered extension " |
| 4285 | "code %ld", code); |
| 4286 | return -1; |
| 4287 | } |
| 4288 | /* Since the extension registry is manipulable via Python code, |
| 4289 | * confirm that pair is really a 2-tuple of strings. |
| 4290 | */ |
| 4291 | if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 || |
| 4292 | !PyString_Check(module_name = PyTuple_GET_ITEM(pair, 0)) || |
| 4293 | !PyString_Check(class_name = PyTuple_GET_ITEM(pair, 1))) { |
| 4294 | Py_DECREF(py_code); |
| 4295 | PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] " |
| 4296 | "isn't a 2-tuple of strings", code); |
| 4297 | return -1; |
| 4298 | } |
| 4299 | /* Load the object. */ |
| 4300 | obj = find_class(module_name, class_name, self->find_class); |
| 4301 | if (obj == NULL) { |
| 4302 | Py_DECREF(py_code); |
| 4303 | return -1; |
| 4304 | } |
| 4305 | /* Cache code -> obj. */ |
| 4306 | code = PyDict_SetItem(extension_cache, py_code, obj); |
| 4307 | Py_DECREF(py_code); |
| 4308 | if (code < 0) { |
| 4309 | Py_DECREF(obj); |
| 4310 | return -1; |
| 4311 | } |
| 4312 | PDATA_PUSH(self->stack, obj, -1); |
| 4313 | return 0; |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 4314 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4315 | |
| 4316 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4317 | load_put(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4318 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4319 | PyObject *py_str = 0, *value = 0; |
| 4320 | int len, l; |
| 4321 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4322 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4323 | if ((l = self->readline_func(self, &s)) < 0) return -1; |
| 4324 | if (l < 2) return bad_readline(); |
| 4325 | if (!( len=self->stack->length )) return stackUnderflow(); |
| 4326 | if (!( py_str = PyString_FromStringAndSize(s, l - 1))) return -1; |
| 4327 | value=self->stack->data[len-1]; |
| 4328 | l=PyDict_SetItem(self->memo, py_str, value); |
| 4329 | Py_DECREF(py_str); |
| 4330 | return l; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4331 | } |
| 4332 | |
| 4333 | |
| 4334 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4335 | load_binput(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4336 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4337 | PyObject *py_key = 0, *value = 0; |
| 4338 | unsigned char key; |
| 4339 | char *s; |
| 4340 | int len; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4341 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4342 | if (self->read_func(self, &s, 1) < 0) return -1; |
| 4343 | if (!( (len=self->stack->length) > 0 )) return stackUnderflow(); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4344 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4345 | key = (unsigned char)s[0]; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 4346 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4347 | if (!( py_key = PyInt_FromLong((long)key))) return -1; |
| 4348 | value=self->stack->data[len-1]; |
| 4349 | len=PyDict_SetItem(self->memo, py_key, value); |
| 4350 | Py_DECREF(py_key); |
| 4351 | return len; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4352 | } |
| 4353 | |
| 4354 | |
| 4355 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4356 | load_long_binput(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4357 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4358 | PyObject *py_key = 0, *value = 0; |
| 4359 | long key; |
| 4360 | unsigned char c; |
| 4361 | char *s; |
| 4362 | int len; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4363 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4364 | if (self->read_func(self, &s, 4) < 0) return -1; |
| 4365 | if (!( len=self->stack->length )) return stackUnderflow(); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4366 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4367 | c = (unsigned char)s[0]; |
| 4368 | key = (long)c; |
| 4369 | c = (unsigned char)s[1]; |
| 4370 | key |= (long)c << 8; |
| 4371 | c = (unsigned char)s[2]; |
| 4372 | key |= (long)c << 16; |
| 4373 | c = (unsigned char)s[3]; |
| 4374 | key |= (long)c << 24; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4375 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4376 | if (!( py_key = PyInt_FromLong(key))) return -1; |
| 4377 | value=self->stack->data[len-1]; |
| 4378 | len=PyDict_SetItem(self->memo, py_key, value); |
| 4379 | Py_DECREF(py_key); |
| 4380 | return len; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4381 | } |
| 4382 | |
| 4383 | |
| 4384 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4385 | do_append(Unpicklerobject *self, int x) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4386 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4387 | PyObject *value = 0, *list = 0, *append_method = 0; |
| 4388 | int len, i; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4389 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4390 | len=self->stack->length; |
| 4391 | if (!( len >= x && x > 0 )) return stackUnderflow(); |
| 4392 | /* nothing to do */ |
| 4393 | if (len==x) return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4394 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4395 | list=self->stack->data[x-1]; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4396 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4397 | if (PyList_Check(list)) { |
| 4398 | PyObject *slice; |
| 4399 | int list_len; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4400 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4401 | slice=Pdata_popList(self->stack, x); |
| 4402 | if (! slice) return -1; |
| 4403 | list_len = PyList_GET_SIZE(list); |
| 4404 | i=PyList_SetSlice(list, list_len, list_len, slice); |
| 4405 | Py_DECREF(slice); |
| 4406 | return i; |
| 4407 | } |
| 4408 | else { |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4409 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4410 | if (!( append_method = PyObject_GetAttr(list, append_str))) |
| 4411 | return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4412 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4413 | for (i = x; i < len; i++) { |
| 4414 | PyObject *junk; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4415 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4416 | value=self->stack->data[i]; |
| 4417 | junk=0; |
| 4418 | ARG_TUP(self, value); |
| 4419 | if (self->arg) { |
| 4420 | junk = PyObject_Call(append_method, self->arg, |
| 4421 | NULL); |
| 4422 | FREE_ARG_TUP(self); |
| 4423 | } |
| 4424 | if (! junk) { |
| 4425 | Pdata_clear(self->stack, i+1); |
| 4426 | self->stack->length=x; |
| 4427 | Py_DECREF(append_method); |
| 4428 | return -1; |
| 4429 | } |
| 4430 | Py_DECREF(junk); |
| 4431 | } |
| 4432 | self->stack->length=x; |
| 4433 | Py_DECREF(append_method); |
| 4434 | } |
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 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4437 | } |
| 4438 | |
| 4439 | |
| 4440 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4441 | load_append(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4442 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4443 | return do_append(self, self->stack->length - 1); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4444 | } |
| 4445 | |
| 4446 | |
| 4447 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4448 | load_appends(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4449 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4450 | return do_append(self, marker(self)); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4451 | } |
| 4452 | |
| 4453 | |
| 4454 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4455 | do_setitems(Unpicklerobject *self, int x) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4456 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4457 | PyObject *value = 0, *key = 0, *dict = 0; |
| 4458 | int len, i, r=0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4459 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4460 | if (!( (len=self->stack->length) >= x |
| 4461 | && x > 0 )) return stackUnderflow(); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4462 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4463 | dict=self->stack->data[x-1]; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4464 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4465 | for (i = x+1; i < len; i += 2) { |
| 4466 | key =self->stack->data[i-1]; |
| 4467 | value=self->stack->data[i ]; |
| 4468 | if (PyObject_SetItem(dict, key, value) < 0) { |
| 4469 | r=-1; |
| 4470 | break; |
| 4471 | } |
| 4472 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4473 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4474 | Pdata_clear(self->stack, x); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4475 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4476 | return r; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4477 | } |
| 4478 | |
| 4479 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4480 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4481 | load_setitem(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4482 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4483 | return do_setitems(self, self->stack->length - 2); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4484 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4485 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4486 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4487 | load_setitems(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4488 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4489 | return do_setitems(self, marker(self)); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4490 | } |
| 4491 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4492 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4493 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4494 | load_build(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4495 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4496 | PyObject *state, *inst, *slotstate; |
| 4497 | PyObject *__setstate__; |
| 4498 | PyObject *d_key, *d_value; |
| 4499 | Py_ssize_t i; |
| 4500 | int res = -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4501 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4502 | /* Stack is ... instance, state. We want to leave instance at |
| 4503 | * the stack top, possibly mutated via instance.__setstate__(state). |
| 4504 | */ |
| 4505 | if (self->stack->length < 2) |
| 4506 | return stackUnderflow(); |
| 4507 | PDATA_POP(self->stack, state); |
| 4508 | if (state == NULL) |
| 4509 | return -1; |
| 4510 | inst = self->stack->data[self->stack->length - 1]; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4511 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4512 | __setstate__ = PyObject_GetAttr(inst, __setstate___str); |
| 4513 | if (__setstate__ != NULL) { |
| 4514 | PyObject *junk = NULL; |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 4515 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4516 | /* The explicit __setstate__ is responsible for everything. */ |
| 4517 | ARG_TUP(self, state); |
| 4518 | if (self->arg) { |
| 4519 | junk = PyObject_Call(__setstate__, self->arg, NULL); |
| 4520 | FREE_ARG_TUP(self); |
| 4521 | } |
| 4522 | Py_DECREF(__setstate__); |
| 4523 | if (junk == NULL) |
| 4524 | return -1; |
| 4525 | Py_DECREF(junk); |
| 4526 | return 0; |
| 4527 | } |
| 4528 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 4529 | return -1; |
| 4530 | PyErr_Clear(); |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 4531 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4532 | /* A default __setstate__. First see whether state embeds a |
| 4533 | * slot state dict too (a proto 2 addition). |
| 4534 | */ |
| 4535 | if (PyTuple_Check(state) && PyTuple_Size(state) == 2) { |
| 4536 | PyObject *temp = state; |
| 4537 | state = PyTuple_GET_ITEM(temp, 0); |
| 4538 | slotstate = PyTuple_GET_ITEM(temp, 1); |
| 4539 | Py_INCREF(state); |
| 4540 | Py_INCREF(slotstate); |
| 4541 | Py_DECREF(temp); |
| 4542 | } |
| 4543 | else |
| 4544 | slotstate = NULL; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4545 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4546 | /* Set inst.__dict__ from the state dict (if any). */ |
| 4547 | if (state != Py_None) { |
| 4548 | PyObject *dict; |
| 4549 | if (! PyDict_Check(state)) { |
| 4550 | PyErr_SetString(UnpicklingError, "state is not a " |
| 4551 | "dictionary"); |
| 4552 | goto finally; |
| 4553 | } |
| 4554 | dict = PyObject_GetAttr(inst, __dict___str); |
| 4555 | if (dict == NULL) |
| 4556 | goto finally; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4557 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4558 | i = 0; |
| 4559 | while (PyDict_Next(state, &i, &d_key, &d_value)) { |
| 4560 | /* normally the keys for instance attributes are |
| 4561 | interned. we should try to do that here. */ |
| 4562 | Py_INCREF(d_key); |
| 4563 | if (PyString_CheckExact(d_key)) |
| 4564 | PyString_InternInPlace(&d_key); |
| 4565 | if (PyObject_SetItem(dict, d_key, d_value) < 0) { |
| 4566 | Py_DECREF(d_key); |
| 4567 | goto finally; |
| 4568 | } |
| 4569 | Py_DECREF(d_key); |
| 4570 | } |
| 4571 | Py_DECREF(dict); |
| 4572 | } |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 4573 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4574 | /* Also set instance attributes from the slotstate dict (if any). */ |
| 4575 | if (slotstate != NULL) { |
| 4576 | if (! PyDict_Check(slotstate)) { |
| 4577 | PyErr_SetString(UnpicklingError, "slot state is not " |
| 4578 | "a dictionary"); |
| 4579 | goto finally; |
| 4580 | } |
| 4581 | i = 0; |
| 4582 | while (PyDict_Next(slotstate, &i, &d_key, &d_value)) { |
| 4583 | if (PyObject_SetAttr(inst, d_key, d_value) < 0) |
| 4584 | goto finally; |
| 4585 | } |
| 4586 | } |
| 4587 | res = 0; |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 4588 | |
| 4589 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4590 | Py_DECREF(state); |
| 4591 | Py_XDECREF(slotstate); |
| 4592 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4593 | } |
| 4594 | |
| 4595 | |
| 4596 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4597 | load_mark(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4598 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4599 | int s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4600 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4601 | /* Note that we split the (pickle.py) stack into two stacks, an |
| 4602 | object stack and a mark stack. Here we push a mark onto the |
| 4603 | mark stack. |
| 4604 | */ |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4605 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4606 | if ((self->num_marks + 1) >= self->marks_size) { |
| 4607 | int *marks; |
| 4608 | s=self->marks_size+20; |
| 4609 | if (s <= self->num_marks) s=self->num_marks + 1; |
| 4610 | if (self->marks == NULL) |
| 4611 | marks=(int *)malloc(s * sizeof(int)); |
| 4612 | else |
| 4613 | marks=(int *)realloc(self->marks, |
| 4614 | s * sizeof(int)); |
| 4615 | if (!marks) { |
| 4616 | PyErr_NoMemory(); |
| 4617 | return -1; |
| 4618 | } |
| 4619 | self->marks = marks; |
| 4620 | self->marks_size = s; |
| 4621 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4622 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4623 | self->marks[self->num_marks++] = self->stack->length; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4624 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4625 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4626 | } |
| 4627 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4628 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4629 | load_reduce(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4630 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4631 | PyObject *callable = 0, *arg_tup = 0, *ob = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4632 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4633 | PDATA_POP(self->stack, arg_tup); |
| 4634 | if (! arg_tup) return -1; |
| 4635 | PDATA_POP(self->stack, callable); |
| 4636 | if (callable) { |
| 4637 | ob = Instance_New(callable, arg_tup); |
| 4638 | Py_DECREF(callable); |
| 4639 | } |
| 4640 | Py_DECREF(arg_tup); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4641 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4642 | if (! ob) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4643 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4644 | PDATA_PUSH(self->stack, ob, -1); |
| 4645 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4646 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4647 | |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 4648 | /* Just raises an error if we don't know the protocol specified. PROTO |
| 4649 | * is the first opcode for protocols >= 2. |
| 4650 | */ |
| 4651 | static int |
| 4652 | load_proto(Unpicklerobject *self) |
| 4653 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4654 | int i; |
| 4655 | char *protobyte; |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 4656 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4657 | i = self->read_func(self, &protobyte, 1); |
| 4658 | if (i < 0) |
| 4659 | return -1; |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 4660 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4661 | i = calc_binint(protobyte, 1); |
| 4662 | /* No point checking for < 0, since calc_binint returns an unsigned |
| 4663 | * int when chewing on 1 byte. |
| 4664 | */ |
| 4665 | assert(i >= 0); |
| 4666 | if (i <= HIGHEST_PROTOCOL) |
| 4667 | return 0; |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 4668 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4669 | PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i); |
| 4670 | return -1; |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 4671 | } |
| 4672 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4673 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4674 | load(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4675 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4676 | PyObject *err = 0, *val = 0; |
| 4677 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4678 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4679 | self->num_marks = 0; |
| 4680 | if (self->stack->length) Pdata_clear(self->stack, 0); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4681 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4682 | while (1) { |
| 4683 | if (self->read_func(self, &s, 1) < 0) |
| 4684 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4685 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4686 | switch (s[0]) { |
| 4687 | case NONE: |
| 4688 | if (load_none(self) < 0) |
| 4689 | break; |
| 4690 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4691 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4692 | case BININT: |
| 4693 | if (load_binint(self) < 0) |
| 4694 | break; |
| 4695 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4696 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4697 | case BININT1: |
| 4698 | if (load_binint1(self) < 0) |
| 4699 | break; |
| 4700 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4701 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4702 | case BININT2: |
| 4703 | if (load_binint2(self) < 0) |
| 4704 | break; |
| 4705 | continue; |
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 | case INT: |
| 4708 | if (load_int(self) < 0) |
| 4709 | break; |
| 4710 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4711 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4712 | case LONG: |
| 4713 | if (load_long(self) < 0) |
| 4714 | break; |
| 4715 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4716 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4717 | case LONG1: |
| 4718 | if (load_counted_long(self, 1) < 0) |
| 4719 | break; |
| 4720 | continue; |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 4721 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4722 | case LONG4: |
| 4723 | if (load_counted_long(self, 4) < 0) |
| 4724 | break; |
| 4725 | continue; |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 4726 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4727 | case FLOAT: |
| 4728 | if (load_float(self) < 0) |
| 4729 | break; |
| 4730 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4731 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4732 | case BINFLOAT: |
| 4733 | if (load_binfloat(self) < 0) |
| 4734 | break; |
| 4735 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4736 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4737 | case BINSTRING: |
| 4738 | if (load_binstring(self) < 0) |
| 4739 | break; |
| 4740 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4741 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4742 | case SHORT_BINSTRING: |
| 4743 | if (load_short_binstring(self) < 0) |
| 4744 | break; |
| 4745 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4746 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4747 | case STRING: |
| 4748 | if (load_string(self) < 0) |
| 4749 | break; |
| 4750 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4751 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4752 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4753 | case UNICODE: |
| 4754 | if (load_unicode(self) < 0) |
| 4755 | break; |
| 4756 | continue; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 4757 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4758 | case BINUNICODE: |
| 4759 | if (load_binunicode(self) < 0) |
| 4760 | break; |
| 4761 | continue; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4762 | #endif |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 4763 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4764 | case EMPTY_TUPLE: |
| 4765 | if (load_counted_tuple(self, 0) < 0) |
| 4766 | break; |
| 4767 | continue; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 4768 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4769 | case TUPLE1: |
| 4770 | if (load_counted_tuple(self, 1) < 0) |
| 4771 | break; |
| 4772 | continue; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 4773 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4774 | case TUPLE2: |
| 4775 | if (load_counted_tuple(self, 2) < 0) |
| 4776 | break; |
| 4777 | continue; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 4778 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4779 | case TUPLE3: |
| 4780 | if (load_counted_tuple(self, 3) < 0) |
| 4781 | break; |
| 4782 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4783 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4784 | case TUPLE: |
| 4785 | if (load_tuple(self) < 0) |
| 4786 | break; |
| 4787 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4788 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4789 | case EMPTY_LIST: |
| 4790 | if (load_empty_list(self) < 0) |
| 4791 | break; |
| 4792 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4793 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4794 | case LIST: |
| 4795 | if (load_list(self) < 0) |
| 4796 | break; |
| 4797 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4798 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4799 | case EMPTY_DICT: |
| 4800 | if (load_empty_dict(self) < 0) |
| 4801 | break; |
| 4802 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4803 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4804 | case DICT: |
| 4805 | if (load_dict(self) < 0) |
| 4806 | break; |
| 4807 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4808 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4809 | case OBJ: |
| 4810 | if (load_obj(self) < 0) |
| 4811 | break; |
| 4812 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4813 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4814 | case INST: |
| 4815 | if (load_inst(self) < 0) |
| 4816 | break; |
| 4817 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4818 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4819 | case NEWOBJ: |
| 4820 | if (load_newobj(self) < 0) |
| 4821 | break; |
| 4822 | continue; |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 4823 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4824 | case GLOBAL: |
| 4825 | if (load_global(self) < 0) |
| 4826 | break; |
| 4827 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4828 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4829 | case APPEND: |
| 4830 | if (load_append(self) < 0) |
| 4831 | break; |
| 4832 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4833 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4834 | case APPENDS: |
| 4835 | if (load_appends(self) < 0) |
| 4836 | break; |
| 4837 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4838 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4839 | case BUILD: |
| 4840 | if (load_build(self) < 0) |
| 4841 | break; |
| 4842 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4843 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4844 | case DUP: |
| 4845 | if (load_dup(self) < 0) |
| 4846 | break; |
| 4847 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4848 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4849 | case BINGET: |
| 4850 | if (load_binget(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 LONG_BINGET: |
| 4855 | if (load_long_binget(self) < 0) |
| 4856 | break; |
| 4857 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4858 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4859 | case GET: |
| 4860 | if (load_get(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 EXT1: |
| 4865 | if (load_extension(self, 1) < 0) |
| 4866 | break; |
| 4867 | continue; |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 4868 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4869 | case EXT2: |
| 4870 | if (load_extension(self, 2) < 0) |
| 4871 | break; |
| 4872 | continue; |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 4873 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4874 | case EXT4: |
| 4875 | if (load_extension(self, 4) < 0) |
| 4876 | break; |
| 4877 | continue; |
| 4878 | case MARK: |
| 4879 | if (load_mark(self) < 0) |
| 4880 | break; |
| 4881 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4882 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4883 | case BINPUT: |
| 4884 | if (load_binput(self) < 0) |
| 4885 | break; |
| 4886 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4887 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4888 | case LONG_BINPUT: |
| 4889 | if (load_long_binput(self) < 0) |
| 4890 | break; |
| 4891 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4892 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4893 | case PUT: |
| 4894 | if (load_put(self) < 0) |
| 4895 | break; |
| 4896 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4897 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4898 | case POP: |
| 4899 | if (load_pop(self) < 0) |
| 4900 | break; |
| 4901 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4902 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4903 | case POP_MARK: |
| 4904 | if (load_pop_mark(self) < 0) |
| 4905 | break; |
| 4906 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4907 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4908 | case SETITEM: |
| 4909 | if (load_setitem(self) < 0) |
| 4910 | break; |
| 4911 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4912 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4913 | case SETITEMS: |
| 4914 | if (load_setitems(self) < 0) |
| 4915 | break; |
| 4916 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4917 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4918 | case STOP: |
| 4919 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4920 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4921 | case PERSID: |
| 4922 | if (load_persid(self) < 0) |
| 4923 | break; |
| 4924 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4925 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4926 | case BINPERSID: |
| 4927 | if (load_binpersid(self) < 0) |
| 4928 | break; |
| 4929 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4930 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4931 | case REDUCE: |
| 4932 | if (load_reduce(self) < 0) |
| 4933 | break; |
| 4934 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4935 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4936 | case PROTO: |
| 4937 | if (load_proto(self) < 0) |
| 4938 | break; |
| 4939 | continue; |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 4940 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4941 | case NEWTRUE: |
| 4942 | if (load_bool(self, Py_True) < 0) |
| 4943 | break; |
| 4944 | continue; |
Tim Peters | 3c67d79 | 2003-02-02 17:59:11 +0000 | [diff] [blame] | 4945 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4946 | case NEWFALSE: |
| 4947 | if (load_bool(self, Py_False) < 0) |
| 4948 | break; |
| 4949 | continue; |
Tim Peters | 3c67d79 | 2003-02-02 17:59:11 +0000 | [diff] [blame] | 4950 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4951 | case '\0': |
| 4952 | /* end of file */ |
| 4953 | PyErr_SetNone(PyExc_EOFError); |
| 4954 | break; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4955 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4956 | default: |
| 4957 | cPickle_ErrFormat(UnpicklingError, |
| 4958 | "invalid load key, '%s'.", |
| 4959 | "c", s[0]); |
| 4960 | return NULL; |
| 4961 | } |
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 | break; |
| 4964 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4965 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4966 | if ((err = PyErr_Occurred())) { |
| 4967 | if (err == PyExc_EOFError) { |
| 4968 | PyErr_SetNone(PyExc_EOFError); |
| 4969 | } |
| 4970 | return NULL; |
| 4971 | } |
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 | PDATA_POP(self->stack, val); |
| 4974 | return val; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4975 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4976 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4977 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4978 | /* No-load functions to support noload, which is used to |
| 4979 | find persistent references. */ |
| 4980 | |
| 4981 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4982 | noload_obj(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4983 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4984 | int i; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4985 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4986 | if ((i = marker(self)) < 0) return -1; |
| 4987 | return Pdata_clear(self->stack, i+1); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4988 | } |
| 4989 | |
| 4990 | |
| 4991 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4992 | noload_inst(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4993 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4994 | int i; |
| 4995 | char *s; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4996 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4997 | if ((i = marker(self)) < 0) return -1; |
| 4998 | Pdata_clear(self->stack, i); |
| 4999 | if (self->readline_func(self, &s) < 0) return -1; |
| 5000 | if (self->readline_func(self, &s) < 0) return -1; |
| 5001 | PDATA_APPEND(self->stack, Py_None, -1); |
| 5002 | return 0; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5003 | } |
| 5004 | |
| 5005 | static int |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 5006 | noload_newobj(Unpicklerobject *self) |
| 5007 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5008 | PyObject *obj; |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 5009 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5010 | PDATA_POP(self->stack, obj); /* pop argtuple */ |
| 5011 | if (obj == NULL) return -1; |
| 5012 | Py_DECREF(obj); |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 5013 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5014 | PDATA_POP(self->stack, obj); /* pop cls */ |
| 5015 | if (obj == NULL) return -1; |
| 5016 | Py_DECREF(obj); |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 5017 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5018 | PDATA_APPEND(self->stack, Py_None, -1); |
| 5019 | return 0; |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 5020 | } |
| 5021 | |
| 5022 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5023 | noload_global(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5024 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5025 | char *s; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5026 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5027 | if (self->readline_func(self, &s) < 0) return -1; |
| 5028 | if (self->readline_func(self, &s) < 0) return -1; |
| 5029 | PDATA_APPEND(self->stack, Py_None,-1); |
| 5030 | return 0; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5031 | } |
| 5032 | |
| 5033 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5034 | noload_reduce(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5035 | { |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5036 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5037 | if (self->stack->length < 2) return stackUnderflow(); |
| 5038 | Pdata_clear(self->stack, self->stack->length-2); |
| 5039 | PDATA_APPEND(self->stack, Py_None,-1); |
| 5040 | return 0; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5041 | } |
| 5042 | |
| 5043 | static int |
| 5044 | noload_build(Unpicklerobject *self) { |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5045 | |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 5046 | if (self->stack->length < 1) return stackUnderflow(); |
| 5047 | Pdata_clear(self->stack, self->stack->length-1); |
Guido van Rossum | 50f385c | 1998-12-04 18:48:44 +0000 | [diff] [blame] | 5048 | return 0; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5049 | } |
| 5050 | |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 5051 | static int |
| 5052 | noload_extension(Unpicklerobject *self, int nbytes) |
| 5053 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5054 | char *codebytes; |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 5055 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5056 | assert(nbytes == 1 || nbytes == 2 || nbytes == 4); |
| 5057 | if (self->read_func(self, &codebytes, nbytes) < 0) return -1; |
| 5058 | PDATA_APPEND(self->stack, Py_None, -1); |
| 5059 | return 0; |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 5060 | } |
| 5061 | |
Neil Schemenauer | 973f8b4 | 2009-10-14 19:33:31 +0000 | [diff] [blame] | 5062 | static int |
| 5063 | noload_append(Unpicklerobject *self) |
| 5064 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5065 | return Pdata_clear(self->stack, self->stack->length - 1); |
Neil Schemenauer | 973f8b4 | 2009-10-14 19:33:31 +0000 | [diff] [blame] | 5066 | } |
| 5067 | |
| 5068 | static int |
| 5069 | noload_appends(Unpicklerobject *self) |
| 5070 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5071 | int i; |
| 5072 | if ((i = marker(self)) < 0) return -1; |
| 5073 | return Pdata_clear(self->stack, i); |
Neil Schemenauer | 973f8b4 | 2009-10-14 19:33:31 +0000 | [diff] [blame] | 5074 | } |
| 5075 | |
| 5076 | static int |
| 5077 | noload_setitem(Unpicklerobject *self) |
| 5078 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5079 | return Pdata_clear(self->stack, self->stack->length - 2); |
Neil Schemenauer | 973f8b4 | 2009-10-14 19:33:31 +0000 | [diff] [blame] | 5080 | } |
| 5081 | |
| 5082 | static int |
| 5083 | noload_setitems(Unpicklerobject *self) |
| 5084 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5085 | int i; |
| 5086 | if ((i = marker(self)) < 0) return -1; |
| 5087 | return Pdata_clear(self->stack, i); |
Neil Schemenauer | 973f8b4 | 2009-10-14 19:33:31 +0000 | [diff] [blame] | 5088 | } |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5089 | |
| 5090 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5091 | noload(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5092 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5093 | PyObject *err = 0, *val = 0; |
| 5094 | char *s; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5095 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5096 | self->num_marks = 0; |
| 5097 | Pdata_clear(self->stack, 0); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5098 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5099 | while (1) { |
| 5100 | if (self->read_func(self, &s, 1) < 0) |
| 5101 | break; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5102 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5103 | switch (s[0]) { |
| 5104 | case NONE: |
| 5105 | if (load_none(self) < 0) |
| 5106 | break; |
| 5107 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5108 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5109 | case BININT: |
| 5110 | if (load_binint(self) < 0) |
| 5111 | break; |
| 5112 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5113 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5114 | case BININT1: |
| 5115 | if (load_binint1(self) < 0) |
| 5116 | break; |
| 5117 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5118 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5119 | case BININT2: |
| 5120 | if (load_binint2(self) < 0) |
| 5121 | break; |
| 5122 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5123 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5124 | case INT: |
| 5125 | if (load_int(self) < 0) |
| 5126 | break; |
| 5127 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5128 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5129 | case LONG: |
| 5130 | if (load_long(self) < 0) |
| 5131 | break; |
| 5132 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5133 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5134 | case LONG1: |
| 5135 | if (load_counted_long(self, 1) < 0) |
| 5136 | break; |
| 5137 | continue; |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 5138 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5139 | case LONG4: |
| 5140 | if (load_counted_long(self, 4) < 0) |
| 5141 | break; |
| 5142 | continue; |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 5143 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5144 | case FLOAT: |
| 5145 | if (load_float(self) < 0) |
| 5146 | break; |
| 5147 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5148 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5149 | case BINFLOAT: |
| 5150 | if (load_binfloat(self) < 0) |
| 5151 | break; |
| 5152 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5153 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5154 | case BINSTRING: |
| 5155 | if (load_binstring(self) < 0) |
| 5156 | break; |
| 5157 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5158 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5159 | case SHORT_BINSTRING: |
| 5160 | if (load_short_binstring(self) < 0) |
| 5161 | break; |
| 5162 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5163 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5164 | case STRING: |
| 5165 | if (load_string(self) < 0) |
| 5166 | break; |
| 5167 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5168 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 5169 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5170 | case UNICODE: |
| 5171 | if (load_unicode(self) < 0) |
| 5172 | break; |
| 5173 | continue; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 5174 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5175 | case BINUNICODE: |
| 5176 | if (load_binunicode(self) < 0) |
| 5177 | break; |
| 5178 | continue; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 5179 | #endif |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 5180 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5181 | case EMPTY_TUPLE: |
| 5182 | if (load_counted_tuple(self, 0) < 0) |
| 5183 | break; |
| 5184 | continue; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 5185 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5186 | case TUPLE1: |
| 5187 | if (load_counted_tuple(self, 1) < 0) |
| 5188 | break; |
| 5189 | continue; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 5190 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5191 | case TUPLE2: |
| 5192 | if (load_counted_tuple(self, 2) < 0) |
| 5193 | break; |
| 5194 | continue; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 5195 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5196 | case TUPLE3: |
| 5197 | if (load_counted_tuple(self, 3) < 0) |
| 5198 | break; |
| 5199 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5200 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5201 | case TUPLE: |
| 5202 | if (load_tuple(self) < 0) |
| 5203 | break; |
| 5204 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5205 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5206 | case EMPTY_LIST: |
| 5207 | if (load_empty_list(self) < 0) |
| 5208 | break; |
| 5209 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5210 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5211 | case LIST: |
| 5212 | if (load_list(self) < 0) |
| 5213 | break; |
| 5214 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5215 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5216 | case EMPTY_DICT: |
| 5217 | if (load_empty_dict(self) < 0) |
| 5218 | break; |
| 5219 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5220 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5221 | case DICT: |
| 5222 | if (load_dict(self) < 0) |
| 5223 | break; |
| 5224 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5225 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5226 | case OBJ: |
| 5227 | if (noload_obj(self) < 0) |
| 5228 | break; |
| 5229 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5230 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5231 | case INST: |
| 5232 | if (noload_inst(self) < 0) |
| 5233 | break; |
| 5234 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5235 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5236 | case NEWOBJ: |
| 5237 | if (noload_newobj(self) < 0) |
| 5238 | break; |
| 5239 | continue; |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 5240 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5241 | case GLOBAL: |
| 5242 | if (noload_global(self) < 0) |
| 5243 | break; |
| 5244 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5245 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5246 | case APPEND: |
| 5247 | if (noload_append(self) < 0) |
| 5248 | break; |
| 5249 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5250 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5251 | case APPENDS: |
| 5252 | if (noload_appends(self) < 0) |
| 5253 | break; |
| 5254 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5255 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5256 | case BUILD: |
| 5257 | if (noload_build(self) < 0) |
| 5258 | break; |
| 5259 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5260 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5261 | case DUP: |
| 5262 | if (load_dup(self) < 0) |
| 5263 | break; |
| 5264 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5265 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5266 | case BINGET: |
| 5267 | if (load_binget(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 LONG_BINGET: |
| 5272 | if (load_long_binget(self) < 0) |
| 5273 | break; |
| 5274 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5275 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5276 | case GET: |
| 5277 | if (load_get(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 EXT1: |
| 5282 | if (noload_extension(self, 1) < 0) |
| 5283 | break; |
| 5284 | continue; |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 5285 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5286 | case EXT2: |
| 5287 | if (noload_extension(self, 2) < 0) |
| 5288 | break; |
| 5289 | continue; |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 5290 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5291 | case EXT4: |
| 5292 | if (noload_extension(self, 4) < 0) |
| 5293 | break; |
| 5294 | continue; |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 5295 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5296 | case MARK: |
| 5297 | if (load_mark(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 BINPUT: |
| 5302 | if (load_binput(self) < 0) |
| 5303 | break; |
| 5304 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5305 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5306 | case LONG_BINPUT: |
| 5307 | if (load_long_binput(self) < 0) |
| 5308 | break; |
| 5309 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5310 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5311 | case PUT: |
| 5312 | if (load_put(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 POP: |
| 5317 | if (load_pop(self) < 0) |
| 5318 | break; |
| 5319 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5320 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5321 | case POP_MARK: |
| 5322 | if (load_pop_mark(self) < 0) |
| 5323 | break; |
| 5324 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5325 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5326 | case SETITEM: |
| 5327 | if (noload_setitem(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 SETITEMS: |
| 5332 | if (noload_setitems(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 STOP: |
| 5337 | break; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5338 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5339 | case PERSID: |
| 5340 | if (load_persid(self) < 0) |
| 5341 | break; |
| 5342 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5343 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5344 | case BINPERSID: |
| 5345 | if (load_binpersid(self) < 0) |
| 5346 | break; |
| 5347 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5348 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5349 | case REDUCE: |
| 5350 | if (noload_reduce(self) < 0) |
| 5351 | break; |
| 5352 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5353 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5354 | case PROTO: |
| 5355 | if (load_proto(self) < 0) |
| 5356 | break; |
| 5357 | continue; |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 5358 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5359 | case NEWTRUE: |
| 5360 | if (load_bool(self, Py_True) < 0) |
| 5361 | break; |
| 5362 | continue; |
Tim Peters | 3c67d79 | 2003-02-02 17:59:11 +0000 | [diff] [blame] | 5363 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5364 | case NEWFALSE: |
| 5365 | if (load_bool(self, Py_False) < 0) |
| 5366 | break; |
| 5367 | continue; |
| 5368 | default: |
| 5369 | cPickle_ErrFormat(UnpicklingError, |
| 5370 | "invalid load key, '%s'.", |
| 5371 | "c", s[0]); |
| 5372 | return NULL; |
| 5373 | } |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5374 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5375 | break; |
| 5376 | } |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5377 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5378 | if ((err = PyErr_Occurred())) { |
| 5379 | if (err == PyExc_EOFError) { |
| 5380 | PyErr_SetNone(PyExc_EOFError); |
| 5381 | } |
| 5382 | return NULL; |
| 5383 | } |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5384 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5385 | PDATA_POP(self->stack, val); |
| 5386 | return val; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5387 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5388 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5389 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5390 | static PyObject * |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 5391 | Unpickler_load(Unpicklerobject *self, PyObject *unused) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5392 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5393 | return load(self); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5394 | } |
| 5395 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5396 | static PyObject * |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 5397 | Unpickler_noload(Unpicklerobject *self, PyObject *unused) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5398 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5399 | return noload(self); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5400 | } |
| 5401 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5402 | |
| 5403 | static struct PyMethodDef Unpickler_methods[] = { |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 5404 | {"load", (PyCFunction)Unpickler_load, METH_NOARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 5405 | PyDoc_STR("load() -- Load a pickle") |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5406 | }, |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 5407 | {"noload", (PyCFunction)Unpickler_noload, METH_NOARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 5408 | PyDoc_STR( |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5409 | "noload() -- not load a pickle, but go through most of the motions\n" |
| 5410 | "\n" |
| 5411 | "This function can be used to read past a pickle without instantiating\n" |
| 5412 | "any objects or importing any modules. It can also be used to find all\n" |
| 5413 | "persistent references without instantiating any objects or importing\n" |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 5414 | "any modules.\n") |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5415 | }, |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5416 | {NULL, NULL} /* sentinel */ |
| 5417 | }; |
| 5418 | |
| 5419 | |
| 5420 | static Unpicklerobject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5421 | newUnpicklerobject(PyObject *f) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5422 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5423 | Unpicklerobject *self; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5424 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5425 | if (!( self = PyObject_GC_New(Unpicklerobject, &Unpicklertype))) |
| 5426 | return NULL; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5427 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5428 | self->file = NULL; |
| 5429 | self->arg = NULL; |
| 5430 | self->stack = (Pdata*)Pdata_New(); |
| 5431 | self->pers_func = NULL; |
| 5432 | self->last_string = NULL; |
| 5433 | self->marks = NULL; |
| 5434 | self->num_marks = 0; |
| 5435 | self->marks_size = 0; |
| 5436 | self->buf_size = 0; |
| 5437 | self->read = NULL; |
| 5438 | self->readline = NULL; |
| 5439 | self->find_class = NULL; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5440 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5441 | if (!( self->memo = PyDict_New())) |
| 5442 | goto err; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5443 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5444 | if (!self->stack) |
| 5445 | goto err; |
Neal Norwitz | b59d08c | 2006-07-22 16:20:49 +0000 | [diff] [blame] | 5446 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5447 | Py_INCREF(f); |
| 5448 | self->file = f; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5449 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5450 | /* Set read, readline based on type of f */ |
| 5451 | if (PyFile_Check(f)) { |
| 5452 | self->fp = PyFile_AsFile(f); |
| 5453 | if (self->fp == NULL) { |
| 5454 | PyErr_SetString(PyExc_ValueError, |
| 5455 | "I/O operation on closed file"); |
| 5456 | goto err; |
| 5457 | } |
| 5458 | self->read_func = read_file; |
| 5459 | self->readline_func = readline_file; |
| 5460 | } |
| 5461 | else if (PycStringIO_InputCheck(f)) { |
| 5462 | self->fp = NULL; |
| 5463 | self->read_func = read_cStringIO; |
| 5464 | self->readline_func = readline_cStringIO; |
| 5465 | } |
| 5466 | else { |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5467 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5468 | self->fp = NULL; |
| 5469 | self->read_func = read_other; |
| 5470 | self->readline_func = readline_other; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5471 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5472 | if (!( (self->readline = PyObject_GetAttr(f, readline_str)) && |
| 5473 | (self->read = PyObject_GetAttr(f, read_str)))) { |
| 5474 | PyErr_Clear(); |
| 5475 | PyErr_SetString( PyExc_TypeError, |
| 5476 | "argument must have 'read' and " |
| 5477 | "'readline' attributes" ); |
| 5478 | goto err; |
| 5479 | } |
| 5480 | } |
| 5481 | PyObject_GC_Track(self); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5482 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5483 | return self; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5484 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5485 | err: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5486 | Py_DECREF((PyObject *)self); |
| 5487 | return NULL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5488 | } |
| 5489 | |
| 5490 | |
| 5491 | static PyObject * |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 5492 | get_Unpickler(PyObject *self, PyObject *file) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5493 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5494 | return (PyObject *)newUnpicklerobject(file); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5495 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5496 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5497 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5498 | static void |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5499 | Unpickler_dealloc(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5500 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5501 | PyObject_GC_UnTrack((PyObject *)self); |
| 5502 | Py_XDECREF(self->readline); |
| 5503 | Py_XDECREF(self->read); |
| 5504 | Py_XDECREF(self->file); |
| 5505 | Py_XDECREF(self->memo); |
| 5506 | Py_XDECREF(self->stack); |
| 5507 | Py_XDECREF(self->pers_func); |
| 5508 | Py_XDECREF(self->arg); |
| 5509 | Py_XDECREF(self->last_string); |
| 5510 | Py_XDECREF(self->find_class); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5511 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5512 | if (self->marks) { |
| 5513 | free(self->marks); |
| 5514 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5515 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5516 | if (self->buf_size) { |
| 5517 | free(self->buf); |
| 5518 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5519 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5520 | Py_TYPE(self)->tp_free((PyObject *)self); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5521 | } |
| 5522 | |
Jeremy Hylton | 7b5ce7f | 2003-04-09 21:25:30 +0000 | [diff] [blame] | 5523 | static int |
| 5524 | Unpickler_traverse(Unpicklerobject *self, visitproc visit, void *arg) |
| 5525 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5526 | Py_VISIT(self->readline); |
| 5527 | Py_VISIT(self->read); |
| 5528 | Py_VISIT(self->file); |
| 5529 | Py_VISIT(self->memo); |
| 5530 | Py_VISIT(self->stack); |
| 5531 | Py_VISIT(self->pers_func); |
| 5532 | Py_VISIT(self->arg); |
| 5533 | Py_VISIT(self->last_string); |
| 5534 | Py_VISIT(self->find_class); |
| 5535 | return 0; |
Jeremy Hylton | 7b5ce7f | 2003-04-09 21:25:30 +0000 | [diff] [blame] | 5536 | } |
| 5537 | |
| 5538 | static int |
| 5539 | Unpickler_clear(Unpicklerobject *self) |
| 5540 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5541 | Py_CLEAR(self->readline); |
| 5542 | Py_CLEAR(self->read); |
| 5543 | Py_CLEAR(self->file); |
| 5544 | Py_CLEAR(self->memo); |
| 5545 | Py_CLEAR(self->stack); |
| 5546 | Py_CLEAR(self->pers_func); |
| 5547 | Py_CLEAR(self->arg); |
| 5548 | Py_CLEAR(self->last_string); |
| 5549 | Py_CLEAR(self->find_class); |
| 5550 | return 0; |
Jeremy Hylton | 7b5ce7f | 2003-04-09 21:25:30 +0000 | [diff] [blame] | 5551 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5552 | |
| 5553 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5554 | Unpickler_getattr(Unpicklerobject *self, char *name) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5555 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5556 | if (!strcmp(name, "persistent_load")) { |
| 5557 | if (!self->pers_func) { |
| 5558 | PyErr_SetString(PyExc_AttributeError, name); |
| 5559 | return NULL; |
| 5560 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5561 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5562 | Py_INCREF(self->pers_func); |
| 5563 | return self->pers_func; |
| 5564 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5565 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5566 | if (!strcmp(name, "find_global")) { |
| 5567 | if (!self->find_class) { |
| 5568 | PyErr_SetString(PyExc_AttributeError, name); |
| 5569 | return NULL; |
| 5570 | } |
Guido van Rossum | 1b9e0aa | 1999-04-19 17:58:18 +0000 | [diff] [blame] | 5571 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5572 | Py_INCREF(self->find_class); |
| 5573 | return self->find_class; |
| 5574 | } |
Guido van Rossum | 1b9e0aa | 1999-04-19 17:58:18 +0000 | [diff] [blame] | 5575 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5576 | if (!strcmp(name, "memo")) { |
| 5577 | if (!self->memo) { |
| 5578 | PyErr_SetString(PyExc_AttributeError, name); |
| 5579 | return NULL; |
| 5580 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5581 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5582 | Py_INCREF(self->memo); |
| 5583 | return self->memo; |
| 5584 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5585 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5586 | if (!strcmp(name, "UnpicklingError")) { |
| 5587 | Py_INCREF(UnpicklingError); |
| 5588 | return UnpicklingError; |
| 5589 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5590 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5591 | return Py_FindMethod(Unpickler_methods, (PyObject *)self, name); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5592 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5593 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5594 | |
| 5595 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5596 | Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5597 | { |
Jeremy Hylton | ce616e4 | 1998-08-13 23:13:52 +0000 | [diff] [blame] | 5598 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5599 | if (!strcmp(name, "persistent_load")) { |
| 5600 | Py_XDECREF(self->pers_func); |
| 5601 | self->pers_func = value; |
| 5602 | Py_XINCREF(value); |
| 5603 | return 0; |
| 5604 | } |
Guido van Rossum | 1b9e0aa | 1999-04-19 17:58:18 +0000 | [diff] [blame] | 5605 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5606 | if (!strcmp(name, "find_global")) { |
| 5607 | Py_XDECREF(self->find_class); |
| 5608 | self->find_class = value; |
| 5609 | Py_XINCREF(value); |
| 5610 | return 0; |
| 5611 | } |
Guido van Rossum | 1b9e0aa | 1999-04-19 17:58:18 +0000 | [diff] [blame] | 5612 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5613 | if (! value) { |
| 5614 | PyErr_SetString(PyExc_TypeError, |
| 5615 | "attribute deletion is not supported"); |
| 5616 | return -1; |
| 5617 | } |
Jeremy Hylton | ce616e4 | 1998-08-13 23:13:52 +0000 | [diff] [blame] | 5618 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5619 | if (strcmp(name, "memo") == 0) { |
| 5620 | if (!PyDict_Check(value)) { |
| 5621 | PyErr_SetString(PyExc_TypeError, |
| 5622 | "memo must be a dictionary"); |
| 5623 | return -1; |
| 5624 | } |
| 5625 | Py_XDECREF(self->memo); |
| 5626 | self->memo = value; |
| 5627 | Py_INCREF(value); |
| 5628 | return 0; |
| 5629 | } |
Jeremy Hylton | ce616e4 | 1998-08-13 23:13:52 +0000 | [diff] [blame] | 5630 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5631 | PyErr_SetString(PyExc_AttributeError, name); |
| 5632 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5633 | } |
| 5634 | |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5635 | /* --------------------------------------------------------------------------- |
| 5636 | * Module-level functions. |
| 5637 | */ |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5638 | |
Martin v. Löwis | 544f119 | 2004-07-27 05:22:33 +0000 | [diff] [blame] | 5639 | /* dump(obj, file, protocol=0). */ |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5640 | static PyObject * |
Martin v. Löwis | 544f119 | 2004-07-27 05:22:33 +0000 | [diff] [blame] | 5641 | cpm_dump(PyObject *self, PyObject *args, PyObject *kwds) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5642 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5643 | static char *kwlist[] = {"obj", "file", "protocol", NULL}; |
| 5644 | PyObject *ob, *file, *res = NULL; |
| 5645 | Picklerobject *pickler = 0; |
| 5646 | int proto = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5647 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5648 | if (!( PyArg_ParseTupleAndKeywords(args, kwds, "OO|i", kwlist, |
| 5649 | &ob, &file, &proto))) |
| 5650 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5651 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5652 | if (!( pickler = newPicklerobject(file, proto))) |
| 5653 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5654 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5655 | if (dump(pickler, ob) < 0) |
| 5656 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5657 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5658 | Py_INCREF(Py_None); |
| 5659 | res = Py_None; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5660 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5661 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5662 | Py_XDECREF(pickler); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5663 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5664 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5665 | } |
| 5666 | |
| 5667 | |
Martin v. Löwis | 544f119 | 2004-07-27 05:22:33 +0000 | [diff] [blame] | 5668 | /* dumps(obj, protocol=0). */ |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5669 | static PyObject * |
Martin v. Löwis | 544f119 | 2004-07-27 05:22:33 +0000 | [diff] [blame] | 5670 | cpm_dumps(PyObject *self, PyObject *args, PyObject *kwds) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5671 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5672 | static char *kwlist[] = {"obj", "protocol", NULL}; |
| 5673 | PyObject *ob, *file = 0, *res = NULL; |
| 5674 | Picklerobject *pickler = 0; |
| 5675 | int proto = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5676 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5677 | if (!( PyArg_ParseTupleAndKeywords(args, kwds, "O|i:dumps", kwlist, |
| 5678 | &ob, &proto))) |
| 5679 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5680 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5681 | if (!( file = PycStringIO->NewOutput(128))) |
| 5682 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5683 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5684 | if (!( pickler = newPicklerobject(file, proto))) |
| 5685 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5686 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5687 | if (dump(pickler, ob) < 0) |
| 5688 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5689 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5690 | res = PycStringIO->cgetvalue(file); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5691 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5692 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5693 | Py_XDECREF(pickler); |
| 5694 | Py_XDECREF(file); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5695 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5696 | return res; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5697 | } |
| 5698 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5699 | |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5700 | /* load(fileobj). */ |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5701 | static PyObject * |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 5702 | cpm_load(PyObject *self, PyObject *ob) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5703 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5704 | Unpicklerobject *unpickler = 0; |
| 5705 | PyObject *res = NULL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5706 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5707 | if (!( unpickler = newUnpicklerobject(ob))) |
| 5708 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5709 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5710 | res = load(unpickler); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5711 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5712 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5713 | Py_XDECREF(unpickler); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5714 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5715 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5716 | } |
| 5717 | |
| 5718 | |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5719 | /* loads(string) */ |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5720 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5721 | cpm_loads(PyObject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5722 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5723 | PyObject *ob, *file = 0, *res = NULL; |
| 5724 | Unpicklerobject *unpickler = 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5725 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5726 | if (!( PyArg_ParseTuple(args, "S:loads", &ob))) |
| 5727 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5728 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5729 | if (!( file = PycStringIO->NewInput(ob))) |
| 5730 | goto finally; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5731 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5732 | if (!( unpickler = newUnpicklerobject(file))) |
| 5733 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5734 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5735 | res = load(unpickler); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5736 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5737 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5738 | Py_XDECREF(file); |
| 5739 | Py_XDECREF(unpickler); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5740 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5741 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5742 | } |
| 5743 | |
| 5744 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5745 | PyDoc_STRVAR(Unpicklertype__doc__, |
| 5746 | "Objects that know how to unpickle"); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5747 | |
Guido van Rossum | 9716aaa | 1997-12-08 15:15:16 +0000 | [diff] [blame] | 5748 | static PyTypeObject Unpicklertype = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 5749 | PyVarObject_HEAD_INIT(NULL, 0) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5750 | "cPickle.Unpickler", /*tp_name*/ |
Jeremy Hylton | 7b5ce7f | 2003-04-09 21:25:30 +0000 | [diff] [blame] | 5751 | sizeof(Unpicklerobject), /*tp_basicsize*/ |
| 5752 | 0, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5753 | (destructor)Unpickler_dealloc, /* tp_dealloc */ |
| 5754 | 0, /* tp_print */ |
| 5755 | (getattrfunc)Unpickler_getattr, /* tp_getattr */ |
| 5756 | (setattrfunc)Unpickler_setattr, /* tp_setattr */ |
| 5757 | 0, /* tp_compare */ |
| 5758 | 0, /* tp_repr */ |
| 5759 | 0, /* tp_as_number */ |
| 5760 | 0, /* tp_as_sequence */ |
| 5761 | 0, /* tp_as_mapping */ |
| 5762 | 0, /* tp_hash */ |
| 5763 | 0, /* tp_call */ |
| 5764 | 0, /* tp_str */ |
| 5765 | 0, /* tp_getattro */ |
| 5766 | 0, /* tp_setattro */ |
| 5767 | 0, /* tp_as_buffer */ |
Jeremy Hylton | 7b5ce7f | 2003-04-09 21:25:30 +0000 | [diff] [blame] | 5768 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5769 | Unpicklertype__doc__, /* tp_doc */ |
| 5770 | (traverseproc)Unpickler_traverse, /* tp_traverse */ |
| 5771 | (inquiry)Unpickler_clear, /* tp_clear */ |
Guido van Rossum | 9716aaa | 1997-12-08 15:15:16 +0000 | [diff] [blame] | 5772 | }; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5773 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5774 | static struct PyMethodDef cPickle_methods[] = { |
Martin v. Löwis | 544f119 | 2004-07-27 05:22:33 +0000 | [diff] [blame] | 5775 | {"dump", (PyCFunction)cpm_dump, METH_VARARGS | METH_KEYWORDS, |
| 5776 | PyDoc_STR("dump(obj, file, protocol=0) -- " |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5777 | "Write an object in pickle format to the given file.\n" |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5778 | "\n" |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5779 | "See the Pickler docstring for the meaning of optional argument proto.") |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5780 | }, |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5781 | |
Martin v. Löwis | 544f119 | 2004-07-27 05:22:33 +0000 | [diff] [blame] | 5782 | {"dumps", (PyCFunction)cpm_dumps, METH_VARARGS | METH_KEYWORDS, |
| 5783 | PyDoc_STR("dumps(obj, protocol=0) -- " |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5784 | "Return a string containing an object in pickle format.\n" |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5785 | "\n" |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5786 | "See the Pickler docstring for the meaning of optional argument proto.") |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5787 | }, |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5788 | |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 5789 | {"load", (PyCFunction)cpm_load, METH_O, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 5790 | PyDoc_STR("load(file) -- Load a pickle from the given file")}, |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5791 | |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 5792 | {"loads", (PyCFunction)cpm_loads, METH_VARARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 5793 | PyDoc_STR("loads(string) -- Load a pickle from the given string")}, |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5794 | |
Martin v. Löwis | 544f119 | 2004-07-27 05:22:33 +0000 | [diff] [blame] | 5795 | {"Pickler", (PyCFunction)get_Pickler, METH_VARARGS | METH_KEYWORDS, |
| 5796 | PyDoc_STR("Pickler(file, protocol=0) -- Create a pickler.\n" |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5797 | "\n" |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5798 | "This takes a file-like object for writing a pickle data stream.\n" |
| 5799 | "The optional proto argument tells the pickler to use the given\n" |
| 5800 | "protocol; supported protocols are 0, 1, 2. The default\n" |
| 5801 | "protocol is 0, to be backwards compatible. (Protocol 0 is the\n" |
| 5802 | "only protocol that can be written to a file opened in text\n" |
| 5803 | "mode and read back successfully. When using a protocol higher\n" |
| 5804 | "than 0, make sure the file is opened in binary mode, both when\n" |
| 5805 | "pickling and unpickling.)\n" |
| 5806 | "\n" |
| 5807 | "Protocol 1 is more efficient than protocol 0; protocol 2 is\n" |
| 5808 | "more efficient than protocol 1.\n" |
| 5809 | "\n" |
| 5810 | "Specifying a negative protocol version selects the highest\n" |
| 5811 | "protocol version supported. The higher the protocol used, the\n" |
| 5812 | "more recent the version of Python needed to read the pickle\n" |
| 5813 | "produced.\n" |
| 5814 | "\n" |
| 5815 | "The file parameter must have a write() method that accepts a single\n" |
| 5816 | "string argument. It can thus be an open file object, a StringIO\n" |
| 5817 | "object, or any other custom object that meets this interface.\n") |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5818 | }, |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5819 | |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 5820 | {"Unpickler", (PyCFunction)get_Unpickler, METH_O, |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5821 | PyDoc_STR("Unpickler(file) -- Create an unpickler.")}, |
| 5822 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5823 | { NULL, NULL } |
| 5824 | }; |
| 5825 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5826 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5827 | init_stuff(PyObject *module_dict) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5828 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5829 | PyObject *copyreg, *t, *r; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5830 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 5831 | #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] | 5832 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5833 | if (PyType_Ready(&Unpicklertype) < 0) |
| 5834 | return -1; |
| 5835 | if (PyType_Ready(&Picklertype) < 0) |
| 5836 | return -1; |
Tim Peters | 3cfe754 | 2003-05-21 21:29:48 +0000 | [diff] [blame] | 5837 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5838 | INIT_STR(__class__); |
| 5839 | INIT_STR(__getinitargs__); |
| 5840 | INIT_STR(__dict__); |
| 5841 | INIT_STR(__getstate__); |
| 5842 | INIT_STR(__setstate__); |
| 5843 | INIT_STR(__name__); |
| 5844 | INIT_STR(__main__); |
| 5845 | INIT_STR(__reduce__); |
| 5846 | INIT_STR(__reduce_ex__); |
| 5847 | INIT_STR(write); |
| 5848 | INIT_STR(append); |
| 5849 | INIT_STR(read); |
| 5850 | INIT_STR(readline); |
| 5851 | INIT_STR(dispatch_table); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5852 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5853 | if (!( copyreg = PyImport_ImportModule("copy_reg"))) |
| 5854 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5855 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5856 | /* This is special because we want to use a different |
| 5857 | one in restricted mode. */ |
| 5858 | dispatch_table = PyObject_GetAttr(copyreg, dispatch_table_str); |
| 5859 | if (!dispatch_table) return -1; |
Tim Peters | 5b7da39 | 2003-02-04 00:21:07 +0000 | [diff] [blame] | 5860 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5861 | extension_registry = PyObject_GetAttrString(copyreg, |
| 5862 | "_extension_registry"); |
| 5863 | if (!extension_registry) return -1; |
Tim Peters | 5b7da39 | 2003-02-04 00:21:07 +0000 | [diff] [blame] | 5864 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5865 | inverted_registry = PyObject_GetAttrString(copyreg, |
| 5866 | "_inverted_registry"); |
| 5867 | if (!inverted_registry) return -1; |
Tim Peters | 5b7da39 | 2003-02-04 00:21:07 +0000 | [diff] [blame] | 5868 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5869 | extension_cache = PyObject_GetAttrString(copyreg, |
| 5870 | "_extension_cache"); |
| 5871 | if (!extension_cache) return -1; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5872 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5873 | Py_DECREF(copyreg); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5874 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5875 | if (!(empty_tuple = PyTuple_New(0))) |
| 5876 | return -1; |
Tim Peters | 731098b | 2003-02-04 20:56:09 +0000 | [diff] [blame] | 5877 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5878 | two_tuple = PyTuple_New(2); |
| 5879 | if (two_tuple == NULL) |
| 5880 | return -1; |
| 5881 | /* We use this temp container with no regard to refcounts, or to |
| 5882 | * keeping containees alive. Exempt from GC, because we don't |
| 5883 | * want anything looking at two_tuple() by magic. |
| 5884 | */ |
| 5885 | PyObject_GC_UnTrack(two_tuple); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5886 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5887 | /* Ugh */ |
| 5888 | if (!( t=PyImport_ImportModule("__builtin__"))) return -1; |
| 5889 | if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0) |
| 5890 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5891 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5892 | if (!( t=PyDict_New())) return -1; |
| 5893 | if (!( r=PyRun_String( |
| 5894 | "def __str__(self):\n" |
| 5895 | " return self.args and ('%s' % self.args[0]) or '(what)'\n", |
| 5896 | Py_file_input, |
| 5897 | module_dict, t) )) return -1; |
| 5898 | Py_DECREF(r); |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5899 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5900 | PickleError = PyErr_NewException("cPickle.PickleError", NULL, t); |
| 5901 | if (!PickleError) |
| 5902 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5903 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5904 | Py_DECREF(t); |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5905 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5906 | PicklingError = PyErr_NewException("cPickle.PicklingError", |
| 5907 | PickleError, NULL); |
| 5908 | if (!PicklingError) |
| 5909 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5910 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5911 | if (!( t=PyDict_New())) return -1; |
| 5912 | if (!( r=PyRun_String( |
| 5913 | "def __str__(self):\n" |
| 5914 | " a=self.args\n" |
| 5915 | " a=a and type(a[0]) or '(what)'\n" |
| 5916 | " return 'Cannot pickle %s objects' % a\n" |
| 5917 | , Py_file_input, |
| 5918 | module_dict, t) )) return -1; |
| 5919 | Py_DECREF(r); |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5920 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5921 | if (!( UnpickleableError = PyErr_NewException( |
| 5922 | "cPickle.UnpickleableError", PicklingError, t))) |
| 5923 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5924 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5925 | Py_DECREF(t); |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5926 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5927 | if (!( UnpicklingError = PyErr_NewException("cPickle.UnpicklingError", |
| 5928 | PickleError, NULL))) |
| 5929 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5930 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5931 | if (!( BadPickleGet = PyErr_NewException("cPickle.BadPickleGet", |
| 5932 | UnpicklingError, NULL))) |
| 5933 | return -1; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5934 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5935 | if (PyDict_SetItemString(module_dict, "PickleError", |
| 5936 | PickleError) < 0) |
| 5937 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5938 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5939 | if (PyDict_SetItemString(module_dict, "PicklingError", |
| 5940 | PicklingError) < 0) |
| 5941 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5942 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5943 | if (PyDict_SetItemString(module_dict, "UnpicklingError", |
| 5944 | UnpicklingError) < 0) |
| 5945 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5946 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5947 | if (PyDict_SetItemString(module_dict, "UnpickleableError", |
| 5948 | UnpickleableError) < 0) |
| 5949 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5950 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5951 | if (PyDict_SetItemString(module_dict, "BadPickleGet", |
| 5952 | BadPickleGet) < 0) |
| 5953 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5954 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5955 | PycString_IMPORT; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 5956 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5957 | return 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5958 | } |
| 5959 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5960 | #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */ |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 5961 | #define PyMODINIT_FUNC void |
Guido van Rossum | f9ffb03 | 1999-02-04 14:54:04 +0000 | [diff] [blame] | 5962 | #endif |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 5963 | PyMODINIT_FUNC |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5964 | initcPickle(void) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5965 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5966 | PyObject *m, *d, *di, *v, *k; |
| 5967 | Py_ssize_t i; |
| 5968 | char *rev = "1.71"; /* XXX when does this change? */ |
| 5969 | PyObject *format_version; |
| 5970 | PyObject *compatible_formats; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5971 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5972 | Py_TYPE(&Picklertype) = &PyType_Type; |
| 5973 | Py_TYPE(&Unpicklertype) = &PyType_Type; |
| 5974 | Py_TYPE(&PdataType) = &PyType_Type; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5975 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5976 | /* Initialize some pieces. We need to do this before module creation, |
| 5977 | * so we're forced to use a temporary dictionary. :( |
| 5978 | */ |
| 5979 | di = PyDict_New(); |
| 5980 | if (!di) return; |
| 5981 | if (init_stuff(di) < 0) return; |
Guido van Rossum | ebba420 | 2000-09-07 14:35:37 +0000 | [diff] [blame] | 5982 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5983 | /* Create the module and add the functions */ |
| 5984 | m = Py_InitModule4("cPickle", cPickle_methods, |
| 5985 | cPickle_module_documentation, |
| 5986 | (PyObject*)NULL,PYTHON_API_VERSION); |
| 5987 | if (m == NULL) |
| 5988 | return; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5989 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5990 | /* Add some symbolic constants to the module */ |
| 5991 | d = PyModule_GetDict(m); |
| 5992 | v = PyString_FromString(rev); |
| 5993 | PyDict_SetItemString(d, "__version__", v); |
| 5994 | Py_XDECREF(v); |
Barry Warsaw | 93d29b6 | 1997-01-14 17:45:08 +0000 | [diff] [blame] | 5995 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5996 | /* Copy data from di. Waaa. */ |
| 5997 | for (i=0; PyDict_Next(di, &i, &k, &v); ) { |
| 5998 | if (PyObject_SetItem(d, k, v) < 0) { |
| 5999 | Py_DECREF(di); |
| 6000 | return; |
| 6001 | } |
| 6002 | } |
| 6003 | Py_DECREF(di); |
Guido van Rossum | ebba420 | 2000-09-07 14:35:37 +0000 | [diff] [blame] | 6004 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 6005 | i = PyModule_AddIntConstant(m, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL); |
| 6006 | if (i < 0) |
| 6007 | return; |
Tim Peters | 8587b3c | 2003-02-13 15:44:41 +0000 | [diff] [blame] | 6008 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 6009 | /* These are purely informational; no code uses them. */ |
| 6010 | /* File format version we write. */ |
| 6011 | format_version = PyString_FromString("2.0"); |
| 6012 | /* Format versions we can read. */ |
| 6013 | compatible_formats = Py_BuildValue("[sssss]", |
| 6014 | "1.0", /* Original protocol 0 */ |
| 6015 | "1.1", /* Protocol 0 + INST */ |
| 6016 | "1.2", /* Original protocol 1 */ |
| 6017 | "1.3", /* Protocol 1 + BINFLOAT */ |
| 6018 | "2.0"); /* Original protocol 2 */ |
| 6019 | PyDict_SetItemString(d, "format_version", format_version); |
| 6020 | PyDict_SetItemString(d, "compatible_formats", compatible_formats); |
| 6021 | Py_XDECREF(format_version); |
| 6022 | Py_XDECREF(compatible_formats); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 6023 | } |