| 1 |
#include "Python.h" |
| 2 |
#include "cStringIO.h" |
| 3 |
#include "structmember.h" |
| 4 |
|
| 5 |
PyDoc_STRVAR(cPickle_module_documentation, |
| 6 |
"C implementation and optimization of the Python pickle module."); |
| 7 |
|
| 8 |
#ifndef Py_eval_input |
| 9 |
#include <graminit.h> |
| 10 |
#define Py_eval_input eval_input |
| 11 |
#endif |
| 12 |
|
| 13 |
#define DEL_LIST_SLICE(list, from, to) (PyList_SetSlice(list, from, to, NULL)) |
| 14 |
|
| 15 |
#define WRITE_BUF_SIZE 256 |
| 16 |
|
| 17 |
|
| 18 |
#define HIGHEST_PROTOCOL 2 |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
#ifdef UNICODE |
| 25 |
# undef UNICODE |
| 26 |
#endif |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#define MARK '(' |
| 33 |
#define STOP '.' |
| 34 |
#define POP '0' |
| 35 |
#define POP_MARK '1' |
| 36 |
#define DUP '2' |
| 37 |
#define FLOAT 'F' |
| 38 |
#define BINFLOAT 'G' |
| 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' |
| 50 |
#define SHORT_BINSTRING 'U' |
| 51 |
#define UNICODE 'V' |
| 52 |
#define BINUNICODE 'X' |
| 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' |
| 73 |
|
| 74 |
|
| 75 |
#define PROTO '\x80' |
| 76 |
#define NEWOBJ '\x81' |
| 77 |
#define EXT1 '\x82' |
| 78 |
#define EXT2 '\x83' |
| 79 |
#define EXT4 '\x84' |
| 80 |
#define TUPLE1 '\x85' |
| 81 |
#define TUPLE2 '\x86' |
| 82 |
#define TUPLE3 '\x87' |
| 83 |
#define NEWTRUE '\x88' |
| 84 |
#define NEWFALSE '\x89' |
| 85 |
#define LONG1 '\x8a' |
| 86 |
#define LONG4 '\x8b' |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
#undef TRUE |
| 94 |
#define TRUE "I01\n" |
| 95 |
#undef FALSE |
| 96 |
#define FALSE "I00\n" |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
#define BATCHSIZE 1000 |
| 104 |
|
| 105 |
static char MARKv = MARK; |
| 106 |
|
| 107 |
static PyObject *PickleError; |
| 108 |
static PyObject *PicklingError; |
| 109 |
static PyObject *UnpickleableError; |
| 110 |
static PyObject *UnpicklingError; |
| 111 |
static PyObject *BadPickleGet; |
| 112 |
|
| 113 |
|
| 114 |
static PyObject *empty_tuple; |
| 115 |
|
| 116 |
|
| 117 |
static PyObject *dispatch_table; |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
static PyObject *extension_registry; |
| 122 |
|
| 123 |
static PyObject *inverted_registry; |
| 124 |
|
| 125 |
static PyObject *extension_cache; |
| 126 |
|
| 127 |
|
| 128 |
static PyObject *two_tuple; |
| 129 |
|
| 130 |
static PyObject *__class___str, *__getinitargs___str, *__dict___str, |
| 131 |
*__getstate___str, *__setstate___str, *__name___str, *__reduce___str, |
| 132 |
*__reduce_ex___str, |
| 133 |
*write_str, *append_str, |
| 134 |
*read_str, *readline_str, *__main___str, |
| 135 |
*dispatch_table_str; |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
typedef struct { |
| 141 |
PyObject_HEAD |
| 142 |
Py_ssize_t length; |
| 143 |
Py_ssize_t size; |
| 144 |
PyObject **data; |
| 145 |
} Pdata; |
| 146 |
|
| 147 |
static void |
| 148 |
Pdata_dealloc(Pdata *self) |
| 149 |
{ |
| 150 |
Py_ssize_t i; |
| 151 |
PyObject **p; |
| 152 |
|
| 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); |
| 159 |
} |
| 160 |
|
| 161 |
static PyTypeObject PdataType = { |
| 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, "" |
| 165 |
}; |
| 166 |
|
| 167 |
#define Pdata_Check(O) (Py_TYPE(O) == &PdataType) |
| 168 |
|
| 169 |
static PyObject * |
| 170 |
Pdata_New(void) |
| 171 |
{ |
| 172 |
Pdata *self; |
| 173 |
|
| 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(); |
| 183 |
} |
| 184 |
|
| 185 |
static int |
| 186 |
stackUnderflow(void) |
| 187 |
{ |
| 188 |
PyErr_SetString(UnpicklingError, "unpickling stack underflow"); |
| 189 |
return -1; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
static int |
| 196 |
Pdata_clear(Pdata *self, Py_ssize_t clearto) |
| 197 |
{ |
| 198 |
Py_ssize_t i; |
| 199 |
PyObject **p; |
| 200 |
|
| 201 |
if (clearto < 0) return stackUnderflow(); |
| 202 |
if (clearto >= self->length) return 0; |
| 203 |
|
| 204 |
for (i = self->length, p = self->data + clearto; |
| 205 |
--i >= clearto; |
| 206 |
p++) { |
| 207 |
Py_CLEAR(*p); |
| 208 |
} |
| 209 |
self->length = clearto; |
| 210 |
|
| 211 |
return 0; |
| 212 |
} |
| 213 |
|
| 214 |
static int |
| 215 |
Pdata_grow(Pdata *self) |
| 216 |
{ |
| 217 |
Py_ssize_t bigger; |
| 218 |
Py_ssize_t nbytes; |
| 219 |
|
| 220 |
PyObject **tmp; |
| 221 |
|
| 222 |
if (self->size > (PY_SSIZE_T_MAX >> 1)) |
| 223 |
goto nomemory; |
| 224 |
bigger = self->size << 1; |
| 225 |
if (bigger > (PY_SSIZE_T_MAX / sizeof(PyObject *))) |
| 226 |
goto nomemory; |
| 227 |
nbytes = bigger * sizeof(PyObject *); |
| 228 |
tmp = realloc(self->data, nbytes); |
| 229 |
if (tmp == NULL) |
| 230 |
goto nomemory; |
| 231 |
self->data = tmp; |
| 232 |
self->size = bigger; |
| 233 |
return 0; |
| 234 |
|
| 235 |
nomemory: |
| 236 |
PyErr_NoMemory(); |
| 237 |
return -1; |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
#define PDATA_POP(D, V) { \ |
| 245 |
if ((D)->length) \ |
| 246 |
(V) = (D)->data[--((D)->length)]; \ |
| 247 |
else { \ |
| 248 |
PyErr_SetString(UnpicklingError, "bad pickle data"); \ |
| 249 |
(V) = NULL; \ |
| 250 |
} \ |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
#define PDATA_PUSH(D, O, ER) { \ |
| 263 |
if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \ |
| 264 |
Pdata_grow((Pdata*)(D)) < 0) { \ |
| 265 |
Py_DECREF(O); \ |
| 266 |
return ER; \ |
| 267 |
} \ |
| 268 |
((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \ |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
#define PDATA_APPEND(D, O, ER) { \ |
| 273 |
if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \ |
| 274 |
Pdata_grow((Pdata*)(D)) < 0) \ |
| 275 |
return ER; \ |
| 276 |
Py_INCREF(O); \ |
| 277 |
((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \ |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
static PyObject * |
| 282 |
Pdata_popTuple(Pdata *self, Py_ssize_t start) |
| 283 |
{ |
| 284 |
PyObject *r; |
| 285 |
Py_ssize_t i, j, l; |
| 286 |
|
| 287 |
l = self->length-start; |
| 288 |
r = PyTuple_New(l); |
| 289 |
if (r == NULL) |
| 290 |
return NULL; |
| 291 |
for (i = start, j = 0 ; j < l; i++, j++) |
| 292 |
PyTuple_SET_ITEM(r, j, self->data[i]); |
| 293 |
|
| 294 |
self->length = start; |
| 295 |
return r; |
| 296 |
} |
| 297 |
|
| 298 |
static PyObject * |
| 299 |
Pdata_popList(Pdata *self, Py_ssize_t start) |
| 300 |
{ |
| 301 |
PyObject *r; |
| 302 |
Py_ssize_t i, j, l; |
| 303 |
|
| 304 |
l=self->length-start; |
| 305 |
if (!( r=PyList_New(l))) return NULL; |
| 306 |
for (i=start, j=0 ; j < l; i++, j++) |
| 307 |
PyList_SET_ITEM(r, j, self->data[i]); |
| 308 |
|
| 309 |
self->length=start; |
| 310 |
return r; |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
#define ARG_TUP(self, o) { \ |
| 316 |
if (self->arg || (self->arg=PyTuple_New(1))) { \ |
| 317 |
Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \ |
| 318 |
PyTuple_SET_ITEM(self->arg,0,o); \ |
| 319 |
} \ |
| 320 |
else { \ |
| 321 |
Py_DECREF(o); \ |
| 322 |
} \ |
| 323 |
} |
| 324 |
|
| 325 |
#define FREE_ARG_TUP(self) { \ |
| 326 |
if (Py_REFCNT(self->arg) > 1) { \ |
| 327 |
Py_CLEAR(self->arg); \ |
| 328 |
} \ |
| 329 |
} |
| 330 |
|
| 331 |
typedef struct Picklerobject { |
| 332 |
PyObject_HEAD |
| 333 |
FILE *fp; |
| 334 |
PyObject *write; |
| 335 |
PyObject *file; |
| 336 |
PyObject *memo; |
| 337 |
PyObject *arg; |
| 338 |
PyObject *pers_func; |
| 339 |
PyObject *inst_pers_func; |
| 340 |
|
| 341 |
|
| 342 |
int proto; |
| 343 |
|
| 344 |
|
| 345 |
int bin; |
| 346 |
|
| 347 |
int fast; |
| 348 |
Py_ssize_t (*write_func)(struct Picklerobject *, const char *, Py_ssize_t); |
| 349 |
char *write_buf; |
| 350 |
Py_ssize_t buf_size; |
| 351 |
PyObject *dispatch_table; |
| 352 |
int fast_container; |
| 353 |
PyObject *fast_memo; |
| 354 |
} Picklerobject; |
| 355 |
|
| 356 |
#ifndef PY_CPICKLE_FAST_LIMIT |
| 357 |
#define PY_CPICKLE_FAST_LIMIT 50 |
| 358 |
#endif |
| 359 |
|
| 360 |
static PyTypeObject Picklertype; |
| 361 |
|
| 362 |
typedef struct Unpicklerobject { |
| 363 |
PyObject_HEAD |
| 364 |
FILE *fp; |
| 365 |
PyObject *file; |
| 366 |
PyObject *readline; |
| 367 |
PyObject *read; |
| 368 |
PyObject *memo; |
| 369 |
PyObject *arg; |
| 370 |
Pdata *stack; |
| 371 |
PyObject *mark; |
| 372 |
PyObject *pers_func; |
| 373 |
PyObject *last_string; |
| 374 |
Py_ssize_t *marks; |
| 375 |
Py_ssize_t num_marks; |
| 376 |
Py_ssize_t marks_size; |
| 377 |
Py_ssize_t (*read_func)(struct Unpicklerobject *, char **, Py_ssize_t); |
| 378 |
Py_ssize_t (*readline_func)(struct Unpicklerobject *, char **); |
| 379 |
Py_ssize_t buf_size; |
| 380 |
char *buf; |
| 381 |
PyObject *find_class; |
| 382 |
} Unpicklerobject; |
| 383 |
|
| 384 |
static PyTypeObject Unpicklertype; |
| 385 |
|
| 386 |
|
| 387 |
static int save(Picklerobject *, PyObject *, int); |
| 388 |
static int put2(Picklerobject *, PyObject *); |
| 389 |
|
| 390 |
static |
| 391 |
PyObject * |
| 392 |
cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...) |
| 393 |
{ |
| 394 |
va_list va; |
| 395 |
PyObject *args=0, *retval=0; |
| 396 |
va_start(va, format); |
| 397 |
|
| 398 |
if (format) args = Py_VaBuildValue(format, va); |
| 399 |
va_end(va); |
| 400 |
if (format && ! args) return NULL; |
| 401 |
if (stringformat && !(retval=PyString_FromString(stringformat))) |
| 402 |
return NULL; |
| 403 |
|
| 404 |
if (retval) { |
| 405 |
if (args) { |
| 406 |
PyObject *v; |
| 407 |
v=PyString_Format(retval, args); |
| 408 |
Py_DECREF(retval); |
| 409 |
Py_DECREF(args); |
| 410 |
if (! v) return NULL; |
| 411 |
retval=v; |
| 412 |
} |
| 413 |
} |
| 414 |
else |
| 415 |
if (args) retval=args; |
| 416 |
else { |
| 417 |
PyErr_SetObject(ErrType,Py_None); |
| 418 |
return NULL; |
| 419 |
} |
| 420 |
PyErr_SetObject(ErrType,retval); |
| 421 |
Py_DECREF(retval); |
| 422 |
return NULL; |
| 423 |
} |
| 424 |
|
| 425 |
static Py_ssize_t |
| 426 |
write_file(Picklerobject *self, const char *s, Py_ssize_t n) |
| 427 |
{ |
| 428 |
size_t nbyteswritten; |
| 429 |
|
| 430 |
if (s == NULL) { |
| 431 |
return 0; |
| 432 |
} |
| 433 |
|
| 434 |
PyFile_IncUseCount((PyFileObject *)self->file); |
| 435 |
Py_BEGIN_ALLOW_THREADS |
| 436 |
nbyteswritten = fwrite(s, sizeof(char), n, self->fp); |
| 437 |
Py_END_ALLOW_THREADS |
| 438 |
PyFile_DecUseCount((PyFileObject *)self->file); |
| 439 |
if (nbyteswritten != (size_t)n) { |
| 440 |
PyErr_SetFromErrno(PyExc_IOError); |
| 441 |
return -1; |
| 442 |
} |
| 443 |
|
| 444 |
return n; |
| 445 |
} |
| 446 |
|
| 447 |
static Py_ssize_t |
| 448 |
write_cStringIO(Picklerobject *self, const char *s, Py_ssize_t n) |
| 449 |
{ |
| 450 |
Py_ssize_t len = n; |
| 451 |
|
| 452 |
if (s == NULL) { |
| 453 |
return 0; |
| 454 |
} |
| 455 |
|
| 456 |
while (n > INT_MAX) { |
| 457 |
if (PycStringIO->cwrite((PyObject *)self->file, s, INT_MAX) != INT_MAX) { |
| 458 |
return -1; |
| 459 |
} |
| 460 |
n -= INT_MAX; |
| 461 |
} |
| 462 |
|
| 463 |
if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) { |
| 464 |
return -1; |
| 465 |
} |
| 466 |
|
| 467 |
return len; |
| 468 |
} |
| 469 |
|
| 470 |
static Py_ssize_t |
| 471 |
write_none(Picklerobject *self, const char *s, Py_ssize_t n) |
| 472 |
{ |
| 473 |
if (s == NULL) return 0; |
| 474 |
return n; |
| 475 |
} |
| 476 |
|
| 477 |
static Py_ssize_t |
| 478 |
write_other(Picklerobject *self, const char *s, Py_ssize_t n) |
| 479 |
{ |
| 480 |
PyObject *py_str = 0, *junk = 0; |
| 481 |
|
| 482 |
if (s == NULL) { |
| 483 |
if (!( self->buf_size )) return 0; |
| 484 |
py_str = PyString_FromStringAndSize(self->write_buf, |
| 485 |
self->buf_size); |
| 486 |
if (!py_str) |
| 487 |
return -1; |
| 488 |
} |
| 489 |
else { |
| 490 |
if (self->buf_size && n > WRITE_BUF_SIZE - self->buf_size) { |
| 491 |
if (write_other(self, NULL, 0) < 0) |
| 492 |
return -1; |
| 493 |
} |
| 494 |
|
| 495 |
if (n > WRITE_BUF_SIZE) { |
| 496 |
if (!( py_str = |
| 497 |
PyString_FromStringAndSize(s, n))) |
| 498 |
return -1; |
| 499 |
} |
| 500 |
else { |
| 501 |
memcpy(self->write_buf + self->buf_size, s, n); |
| 502 |
self->buf_size += n; |
| 503 |
return n; |
| 504 |
} |
| 505 |
} |
| 506 |
|
| 507 |
if (self->write) { |
| 508 |
|
| 509 |
ARG_TUP(self, py_str); |
| 510 |
if (self->arg) { |
| 511 |
junk = PyObject_Call(self->write, self->arg, NULL); |
| 512 |
FREE_ARG_TUP(self); |
| 513 |
} |
| 514 |
if (junk) Py_DECREF(junk); |
| 515 |
else return -1; |
| 516 |
} |
| 517 |
else |
| 518 |
PDATA_PUSH(self->file, py_str, -1); |
| 519 |
|
| 520 |
self->buf_size = 0; |
| 521 |
return n; |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
static Py_ssize_t |
| 526 |
read_file(Unpicklerobject *self, char **s, Py_ssize_t n) |
| 527 |
{ |
| 528 |
size_t nbytesread; |
| 529 |
|
| 530 |
if (self->buf_size == 0) { |
| 531 |
Py_ssize_t size; |
| 532 |
|
| 533 |
size = ((n < 32) ? 32 : n); |
| 534 |
if (!( self->buf = (char *)malloc(size))) { |
| 535 |
PyErr_NoMemory(); |
| 536 |
return -1; |
| 537 |
} |
| 538 |
|
| 539 |
self->buf_size = size; |
| 540 |
} |
| 541 |
else if (n > self->buf_size) { |
| 542 |
char *newbuf = (char *)realloc(self->buf, n); |
| 543 |
if (!newbuf) { |
| 544 |
PyErr_NoMemory(); |
| 545 |
return -1; |
| 546 |
} |
| 547 |
self->buf = newbuf; |
| 548 |
self->buf_size = n; |
| 549 |
} |
| 550 |
|
| 551 |
PyFile_IncUseCount((PyFileObject *)self->file); |
| 552 |
Py_BEGIN_ALLOW_THREADS |
| 553 |
nbytesread = fread(self->buf, sizeof(char), n, self->fp); |
| 554 |
Py_END_ALLOW_THREADS |
| 555 |
PyFile_DecUseCount((PyFileObject *)self->file); |
| 556 |
if (nbytesread != (size_t)n) { |
| 557 |
if (feof(self->fp)) { |
| 558 |
PyErr_SetNone(PyExc_EOFError); |
| 559 |
return -1; |
| 560 |
} |
| 561 |
|
| 562 |
PyErr_SetFromErrno(PyExc_IOError); |
| 563 |
return -1; |
| 564 |
} |
| 565 |
|
| 566 |
*s = self->buf; |
| 567 |
|
| 568 |
return n; |
| 569 |
} |
| 570 |
|
| 571 |
|
| 572 |
static Py_ssize_t |
| 573 |
readline_file(Unpicklerobject *self, char **s) |
| 574 |
{ |
| 575 |
Py_ssize_t i; |
| 576 |
|
| 577 |
if (self->buf_size == 0) { |
| 578 |
if (!( self->buf = (char *)malloc(40))) { |
| 579 |
PyErr_NoMemory(); |
| 580 |
return -1; |
| 581 |
} |
| 582 |
self->buf_size = 40; |
| 583 |
} |
| 584 |
|
| 585 |
i = 0; |
| 586 |
while (1) { |
| 587 |
Py_ssize_t bigger; |
| 588 |
char *newbuf; |
| 589 |
while (i < (self->buf_size - 1)) { |
| 590 |
int newchar = getc(self->fp); |
| 591 |
if (newchar != EOF) { |
| 592 |
self->buf[i++] = newchar; |
| 593 |
} |
| 594 |
if (newchar == EOF || newchar == '\n') { |
| 595 |
self->buf[i] = '\0'; |
| 596 |
*s = self->buf; |
| 597 |
return i; |
| 598 |
} |
| 599 |
} |
| 600 |
if (self->buf_size > (PY_SSIZE_T_MAX >> 1)) { |
| 601 |
PyErr_NoMemory(); |
| 602 |
return -1; |
| 603 |
} |
| 604 |
bigger = self->buf_size << 1; |
| 605 |
newbuf = (char *)realloc(self->buf, bigger); |
| 606 |
if (newbuf == NULL) { |
| 607 |
PyErr_NoMemory(); |
| 608 |
return -1; |
| 609 |
} |
| 610 |
self->buf = newbuf; |
| 611 |
self->buf_size = bigger; |
| 612 |
} |
| 613 |
} |
| 614 |
|
| 615 |
|
| 616 |
static Py_ssize_t |
| 617 |
read_cStringIO(Unpicklerobject *self, char **s, Py_ssize_t n) |
| 618 |
{ |
| 619 |
Py_ssize_t len = n; |
| 620 |
char *start, *end = NULL; |
| 621 |
|
| 622 |
while (1) { |
| 623 |
int k; |
| 624 |
char *ptr; |
| 625 |
if (n > INT_MAX) |
| 626 |
k = INT_MAX; |
| 627 |
else |
| 628 |
k = (int)n; |
| 629 |
if (PycStringIO->cread((PyObject *)self->file, &ptr, k) != k) { |
| 630 |
PyErr_SetNone(PyExc_EOFError); |
| 631 |
return -1; |
| 632 |
} |
| 633 |
if (end == NULL) |
| 634 |
start = ptr; |
| 635 |
else if (ptr != end) { |
| 636 |
|
| 637 |
return -1; |
| 638 |
} |
| 639 |
if (n <= INT_MAX) |
| 640 |
break; |
| 641 |
end = ptr + INT_MAX; |
| 642 |
n -= INT_MAX; |
| 643 |
} |
| 644 |
|
| 645 |
*s = start; |
| 646 |
|
| 647 |
return len; |
| 648 |
} |
| 649 |
|
| 650 |
|
| 651 |
static Py_ssize_t |
| 652 |
readline_cStringIO(Unpicklerobject *self, char **s) |
| 653 |
{ |
| 654 |
Py_ssize_t n = 0; |
| 655 |
char *start = NULL, *end = NULL; |
| 656 |
|
| 657 |
while (1) { |
| 658 |
int k; |
| 659 |
char *ptr; |
| 660 |
if ((k = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) { |
| 661 |
return -1; |
| 662 |
} |
| 663 |
n += k; |
| 664 |
if (end == NULL) |
| 665 |
start = ptr; |
| 666 |
else if (ptr != end) { |
| 667 |
|
| 668 |
return -1; |
| 669 |
} |
| 670 |
if (k == 0 || ptr[k - 1] == '\n') |
| 671 |
break; |
| 672 |
end = ptr + k; |
| 673 |
} |
| 674 |
|
| 675 |
*s = start; |
| 676 |
|
| 677 |
return n; |
| 678 |
} |
| 679 |
|
| 680 |
|
| 681 |
static Py_ssize_t |
| 682 |
read_other(Unpicklerobject *self, char **s, Py_ssize_t n) |
| 683 |
{ |
| 684 |
PyObject *bytes, *str=0; |
| 685 |
|
| 686 |
if (!( bytes = PyInt_FromSsize_t(n))) return -1; |
| 687 |
|
| 688 |
ARG_TUP(self, bytes); |
| 689 |
if (self->arg) { |
| 690 |
str = PyObject_Call(self->read, self->arg, NULL); |
| 691 |
FREE_ARG_TUP(self); |
| 692 |
} |
| 693 |
if (! str) return -1; |
| 694 |
|
| 695 |
Py_XSETREF(self->last_string, str); |
| 696 |
|
| 697 |
if (! (*s = PyString_AsString(str))) return -1; |
| 698 |
|
| 699 |
if (PyString_GET_SIZE(str) != n) { |
| 700 |
PyErr_SetNone(PyExc_EOFError); |
| 701 |
return -1; |
| 702 |
} |
| 703 |
|
| 704 |
return n; |
| 705 |
} |
| 706 |
|
| 707 |
|
| 708 |
static Py_ssize_t |
| 709 |
readline_other(Unpicklerobject *self, char **s) |
| 710 |
{ |
| 711 |
PyObject *str; |
| 712 |
Py_ssize_t str_size; |
| 713 |
|
| 714 |
if (!( str = PyObject_CallObject(self->readline, empty_tuple))) { |
| 715 |
return -1; |
| 716 |
} |
| 717 |
|
| 718 |
if ((str_size = PyString_Size(str)) < 0) |
| 719 |
return -1; |
| 720 |
|
| 721 |
Py_XSETREF(self->last_string, str); |
| 722 |
|
| 723 |
if (! (*s = PyString_AsString(str))) |
| 724 |
return -1; |
| 725 |
|
| 726 |
return str_size; |
| 727 |
} |
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
static char * |
| 734 |
pystrndup(const char *s, Py_ssize_t n) |
| 735 |
{ |
| 736 |
char *r = (char *)malloc(n+1); |
| 737 |
if (r == NULL) |
| 738 |
return (char*)PyErr_NoMemory(); |
| 739 |
memcpy(r, s, n); |
| 740 |
r[n] = 0; |
| 741 |
return r; |
| 742 |
} |
| 743 |
|
| 744 |
|
| 745 |
static int |
| 746 |
get(Picklerobject *self, PyObject *id) |
| 747 |
{ |
| 748 |
PyObject *value, *mv; |
| 749 |
Py_ssize_t c_value; |
| 750 |
char s[30]; |
| 751 |
size_t len; |
| 752 |
|
| 753 |
if (!( mv = PyDict_GetItem(self->memo, id))) { |
| 754 |
PyErr_SetObject(PyExc_KeyError, id); |
| 755 |
return -1; |
| 756 |
} |
| 757 |
|
| 758 |
if (!( value = PyTuple_GetItem(mv, 0))) |
| 759 |
return -1; |
| 760 |
|
| 761 |
if (!( PyInt_Check(value))) { |
| 762 |
PyErr_SetString(PicklingError, "no int where int expected in memo"); |
| 763 |
return -1; |
| 764 |
} |
| 765 |
c_value = PyInt_AS_LONG((PyIntObject*)value); |
| 766 |
|
| 767 |
if (!self->bin) { |
| 768 |
s[0] = GET; |
| 769 |
PyOS_snprintf(s + 1, sizeof(s) - 1, |
| 770 |
"%" PY_FORMAT_SIZE_T "d\n", c_value); |
| 771 |
len = strlen(s); |
| 772 |
} |
| 773 |
else if (Pdata_Check(self->file)) { |
| 774 |
if (write_other(self, NULL, 0) < 0) return -1; |
| 775 |
PDATA_APPEND(self->file, mv, -1); |
| 776 |
return 0; |
| 777 |
} |
| 778 |
else { |
| 779 |
if (c_value < 256) { |
| 780 |
s[0] = BINGET; |
| 781 |
s[1] = (int)(c_value & 0xff); |
| 782 |
len = 2; |
| 783 |
} |
| 784 |
else if (c_value < 0x7fffffffL) { |
| 785 |
s[0] = LONG_BINGET; |
| 786 |
s[1] = (int)(c_value & 0xff); |
| 787 |
s[2] = (int)((c_value >> 8) & 0xff); |
| 788 |
s[3] = (int)((c_value >> 16) & 0xff); |
| 789 |
s[4] = (int)((c_value >> 24) & 0xff); |
| 790 |
len = 5; |
| 791 |
} |
| 792 |
else { |
| 793 |
PyErr_SetString(PicklingError, |
| 794 |
"memo id too large for LONG_BINGET"); |
| 795 |
return -1; |
| 796 |
} |
| 797 |
} |
| 798 |
|
| 799 |
if (self->write_func(self, s, len) < 0) |
| 800 |
return -1; |
| 801 |
|
| 802 |
return 0; |
| 803 |
} |
| 804 |
|
| 805 |
|
| 806 |
static int |
| 807 |
put(Picklerobject *self, PyObject *ob) |
| 808 |
{ |
| 809 |
if (Py_REFCNT(ob) < 2 || self->fast) |
| 810 |
return 0; |
| 811 |
|
| 812 |
return put2(self, ob); |
| 813 |
} |
| 814 |
|
| 815 |
|
| 816 |
static int |
| 817 |
put2(Picklerobject *self, PyObject *ob) |
| 818 |
{ |
| 819 |
char c_str[30]; |
| 820 |
Py_ssize_t len, p; |
| 821 |
int res = -1; |
| 822 |
PyObject *py_ob_id = 0, *memo_len = 0, *t = 0; |
| 823 |
|
| 824 |
if (self->fast) |
| 825 |
return 0; |
| 826 |
|
| 827 |
if ((p = PyDict_Size(self->memo)) < 0) |
| 828 |
goto finally; |
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
p++; |
| 837 |
|
| 838 |
if (!( py_ob_id = PyLong_FromVoidPtr(ob))) |
| 839 |
goto finally; |
| 840 |
|
| 841 |
if (!( memo_len = PyInt_FromLong(p))) |
| 842 |
goto finally; |
| 843 |
|
| 844 |
if (!( t = PyTuple_New(2))) |
| 845 |
goto finally; |
| 846 |
|
| 847 |
PyTuple_SET_ITEM(t, 0, memo_len); |
| 848 |
Py_INCREF(memo_len); |
| 849 |
PyTuple_SET_ITEM(t, 1, ob); |
| 850 |
Py_INCREF(ob); |
| 851 |
|
| 852 |
if (PyDict_SetItem(self->memo, py_ob_id, t) < 0) |
| 853 |
goto finally; |
| 854 |
|
| 855 |
if (!self->bin) { |
| 856 |
c_str[0] = PUT; |
| 857 |
PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, |
| 858 |
"%" PY_FORMAT_SIZE_T "d\n", p); |
| 859 |
len = strlen(c_str); |
| 860 |
} |
| 861 |
else if (Pdata_Check(self->file)) { |
| 862 |
if (write_other(self, NULL, 0) < 0) return -1; |
| 863 |
PDATA_APPEND(self->file, memo_len, -1); |
| 864 |
res=0; |
| 865 |
goto finally; |
| 866 |
} |
| 867 |
else { |
| 868 |
if (p < 256) { |
| 869 |
c_str[0] = BINPUT; |
| 870 |
c_str[1] = p; |
| 871 |
len = 2; |
| 872 |
} |
| 873 |
else if (p < 0x7fffffffL) { |
| 874 |
c_str[0] = LONG_BINPUT; |
| 875 |
c_str[1] = (int)(p & 0xff); |
| 876 |
c_str[2] = (int)((p >> 8) & 0xff); |
| 877 |
c_str[3] = (int)((p >> 16) & 0xff); |
| 878 |
c_str[4] = (int)((p >> 24) & 0xff); |
| 879 |
len = 5; |
| 880 |
} |
| 881 |
else { |
| 882 |
PyErr_SetString(PicklingError, |
| 883 |
"memo id too large for LONG_BINPUT"); |
| 884 |
goto finally; |
| 885 |
} |
| 886 |
} |
| 887 |
|
| 888 |
if (self->write_func(self, c_str, len) < 0) |
| 889 |
goto finally; |
| 890 |
|
| 891 |
res = 0; |
| 892 |
|
| 893 |
finally: |
| 894 |
Py_XDECREF(py_ob_id); |
| 895 |
Py_XDECREF(memo_len); |
| 896 |
Py_XDECREF(t); |
| 897 |
|
| 898 |
return res; |
| 899 |
} |
| 900 |
|
| 901 |
static PyObject * |
| 902 |
whichmodule(PyObject *global, PyObject *global_name) |
| 903 |
{ |
| 904 |
Py_ssize_t i, j; |
| 905 |
PyObject *module = 0, *modules_dict = 0, |
| 906 |
*global_name_attr = 0, *name = 0; |
| 907 |
|
| 908 |
module = PyObject_GetAttrString(global, "__module__"); |
| 909 |
if (module) |
| 910 |
return module; |
| 911 |
if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 912 |
PyErr_Clear(); |
| 913 |
else |
| 914 |
return NULL; |
| 915 |
|
| 916 |
if (!( modules_dict = PySys_GetObject("modules"))) |
| 917 |
return NULL; |
| 918 |
|
| 919 |
i = 0; |
| 920 |
while ((j = PyDict_Next(modules_dict, &i, &name, &module))) { |
| 921 |
|
| 922 |
if (PyObject_Compare(name, __main___str)==0) continue; |
| 923 |
|
| 924 |
global_name_attr = PyObject_GetAttr(module, global_name); |
| 925 |
if (!global_name_attr) { |
| 926 |
if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 927 |
PyErr_Clear(); |
| 928 |
else |
| 929 |
return NULL; |
| 930 |
continue; |
| 931 |
} |
| 932 |
|
| 933 |
if (global_name_attr != global) { |
| 934 |
Py_DECREF(global_name_attr); |
| 935 |
continue; |
| 936 |
} |
| 937 |
|
| 938 |
Py_DECREF(global_name_attr); |
| 939 |
|
| 940 |
break; |
| 941 |
} |
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
if (!j) { |
| 948 |
name=__main___str; |
| 949 |
} |
| 950 |
|
| 951 |
Py_INCREF(name); |
| 952 |
return name; |
| 953 |
} |
| 954 |
|
| 955 |
|
| 956 |
static int |
| 957 |
fast_save_enter(Picklerobject *self, PyObject *obj) |
| 958 |
{ |
| 959 |
|
| 960 |
if (++self->fast_container >= PY_CPICKLE_FAST_LIMIT) { |
| 961 |
PyObject *key = NULL; |
| 962 |
if (self->fast_memo == NULL) { |
| 963 |
self->fast_memo = PyDict_New(); |
| 964 |
if (self->fast_memo == NULL) { |
| 965 |
self->fast_container = -1; |
| 966 |
return 0; |
| 967 |
} |
| 968 |
} |
| 969 |
key = PyLong_FromVoidPtr(obj); |
| 970 |
if (key == NULL) |
| 971 |
return 0; |
| 972 |
if (PyDict_GetItem(self->fast_memo, key)) { |
| 973 |
Py_DECREF(key); |
| 974 |
PyErr_Format(PyExc_ValueError, |
| 975 |
"fast mode: can't pickle cyclic objects " |
| 976 |
"including object type %s at %p", |
| 977 |
Py_TYPE(obj)->tp_name, obj); |
| 978 |
self->fast_container = -1; |
| 979 |
return 0; |
| 980 |
} |
| 981 |
if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) { |
| 982 |
Py_DECREF(key); |
| 983 |
self->fast_container = -1; |
| 984 |
return 0; |
| 985 |
} |
| 986 |
Py_DECREF(key); |
| 987 |
} |
| 988 |
return 1; |
| 989 |
} |
| 990 |
|
| 991 |
int |
| 992 |
fast_save_leave(Picklerobject *self, PyObject *obj) |
| 993 |
{ |
| 994 |
if (self->fast_container-- >= PY_CPICKLE_FAST_LIMIT) { |
| 995 |
PyObject *key = PyLong_FromVoidPtr(obj); |
| 996 |
if (key == NULL) |
| 997 |
return 0; |
| 998 |
if (PyDict_DelItem(self->fast_memo, key) < 0) { |
| 999 |
Py_DECREF(key); |
| 1000 |
return 0; |
| 1001 |
} |
| 1002 |
Py_DECREF(key); |
| 1003 |
} |
| 1004 |
return 1; |
| 1005 |
} |
| 1006 |
|
| 1007 |
static int |
| 1008 |
save_none(Picklerobject *self, PyObject *args) |
| 1009 |
{ |
| 1010 |
static char none = NONE; |
| 1011 |
if (self->write_func(self, &none, 1) < 0) |
| 1012 |
return -1; |
| 1013 |
|
| 1014 |
return 0; |
| 1015 |
} |
| 1016 |
|
| 1017 |
static int |
| 1018 |
save_bool(Picklerobject *self, PyObject *args) |
| 1019 |
{ |
| 1020 |
static const char *buf[2] = {FALSE, TRUE}; |
| 1021 |
static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1}; |
| 1022 |
long l = PyInt_AS_LONG((PyIntObject *)args); |
| 1023 |
|
| 1024 |
if (self->proto >= 2) { |
| 1025 |
char opcode = l ? NEWTRUE : NEWFALSE; |
| 1026 |
if (self->write_func(self, &opcode, 1) < 0) |
| 1027 |
return -1; |
| 1028 |
} |
| 1029 |
else if (self->write_func(self, buf[l], len[l]) < 0) |
| 1030 |
return -1; |
| 1031 |
return 0; |
| 1032 |
} |
| 1033 |
|
| 1034 |
static int |
| 1035 |
save_int(Picklerobject *self, PyObject *args) |
| 1036 |
{ |
| 1037 |
char c_str[32]; |
| 1038 |
long l = PyInt_AS_LONG((PyIntObject *)args); |
| 1039 |
Py_ssize_t len = 0; |
| 1040 |
|
| 1041 |
if (!self->bin |
| 1042 |
#if SIZEOF_LONG > 4 |
| 1043 |
|| l > 0x7fffffffL |
| 1044 |
|| l < -0x80000000L |
| 1045 |
#endif |
| 1046 |
) { |
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
c_str[0] = INT; |
| 1051 |
PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%ld\n", l); |
| 1052 |
if (self->write_func(self, c_str, strlen(c_str)) < 0) |
| 1053 |
return -1; |
| 1054 |
} |
| 1055 |
else { |
| 1056 |
|
| 1057 |
c_str[1] = (int)( l & 0xff); |
| 1058 |
c_str[2] = (int)((l >> 8) & 0xff); |
| 1059 |
c_str[3] = (int)((l >> 16) & 0xff); |
| 1060 |
c_str[4] = (int)((l >> 24) & 0xff); |
| 1061 |
|
| 1062 |
if ((c_str[4] == 0) && (c_str[3] == 0)) { |
| 1063 |
if (c_str[2] == 0) { |
| 1064 |
c_str[0] = BININT1; |
| 1065 |
len = 2; |
| 1066 |
} |
| 1067 |
else { |
| 1068 |
c_str[0] = BININT2; |
| 1069 |
len = 3; |
| 1070 |
} |
| 1071 |
} |
| 1072 |
else { |
| 1073 |
c_str[0] = BININT; |
| 1074 |
len = 5; |
| 1075 |
} |
| 1076 |
|
| 1077 |
if (self->write_func(self, c_str, len) < 0) |
| 1078 |
return -1; |
| 1079 |
} |
| 1080 |
|
| 1081 |
return 0; |
| 1082 |
} |
| 1083 |
|
| 1084 |
|
| 1085 |
static int |
| 1086 |
save_long(Picklerobject *self, PyObject *args) |
| 1087 |
{ |
| 1088 |
Py_ssize_t size; |
| 1089 |
int res = -1; |
| 1090 |
PyObject *repr = NULL; |
| 1091 |
|
| 1092 |
static char l = LONG; |
| 1093 |
|
| 1094 |
if (self->proto >= 2) { |
| 1095 |
|
| 1096 |
size_t nbits; |
| 1097 |
size_t nbytes; |
| 1098 |
unsigned char *pdata; |
| 1099 |
char c_str[5]; |
| 1100 |
int i; |
| 1101 |
int sign = _PyLong_Sign(args); |
| 1102 |
|
| 1103 |
if (sign == 0) { |
| 1104 |
|
| 1105 |
c_str[0] = LONG1; |
| 1106 |
c_str[1] = 0; |
| 1107 |
i = self->write_func(self, c_str, 2); |
| 1108 |
if (i < 0) goto finally; |
| 1109 |
res = 0; |
| 1110 |
goto finally; |
| 1111 |
} |
| 1112 |
nbits = _PyLong_NumBits(args); |
| 1113 |
if (nbits == (size_t)-1 && PyErr_Occurred()) |
| 1114 |
goto finally; |
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
nbytes = (nbits >> 3) + 1; |
| 1130 |
if (nbytes > INT_MAX) { |
| 1131 |
PyErr_SetString(PyExc_OverflowError, "long too large " |
| 1132 |
"to pickle"); |
| 1133 |
goto finally; |
| 1134 |
} |
| 1135 |
repr = PyString_FromStringAndSize(NULL, (int)nbytes); |
| 1136 |
if (repr == NULL) goto finally; |
| 1137 |
pdata = (unsigned char *)PyString_AS_STRING(repr); |
| 1138 |
i = _PyLong_AsByteArray((PyLongObject *)args, |
| 1139 |
pdata, nbytes, |
| 1140 |
1 , 1 ); |
| 1141 |
if (i < 0) goto finally; |
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
if (sign < 0 && nbytes > 1 && pdata[nbytes - 1] == 0xff && |
| 1147 |
(pdata[nbytes - 2] & 0x80) != 0) |
| 1148 |
--nbytes; |
| 1149 |
|
| 1150 |
if (nbytes < 256) { |
| 1151 |
c_str[0] = LONG1; |
| 1152 |
c_str[1] = (char)nbytes; |
| 1153 |
size = 2; |
| 1154 |
} |
| 1155 |
else { |
| 1156 |
c_str[0] = LONG4; |
| 1157 |
size = (int)nbytes; |
| 1158 |
for (i = 1; i < 5; i++) { |
| 1159 |
c_str[i] = (char)(size & 0xff); |
| 1160 |
size >>= 8; |
| 1161 |
} |
| 1162 |
size = 5; |
| 1163 |
} |
| 1164 |
i = self->write_func(self, c_str, size); |
| 1165 |
if (i < 0) goto finally; |
| 1166 |
i = self->write_func(self, (char *)pdata, (int)nbytes); |
| 1167 |
if (i < 0) goto finally; |
| 1168 |
res = 0; |
| 1169 |
goto finally; |
| 1170 |
} |
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
if (!( repr = PyObject_Repr(args))) |
| 1176 |
goto finally; |
| 1177 |
|
| 1178 |
if ((size = PyString_Size(repr)) < 0) |
| 1179 |
goto finally; |
| 1180 |
|
| 1181 |
if (self->write_func(self, &l, 1) < 0) |
| 1182 |
goto finally; |
| 1183 |
|
| 1184 |
if (self->write_func(self, |
| 1185 |
PyString_AS_STRING((PyStringObject *)repr), |
| 1186 |
size) < 0) |
| 1187 |
goto finally; |
| 1188 |
|
| 1189 |
if (self->write_func(self, "\n", 1) < 0) |
| 1190 |
goto finally; |
| 1191 |
|
| 1192 |
res = 0; |
| 1193 |
|
| 1194 |
finally: |
| 1195 |
Py_XDECREF(repr); |
| 1196 |
return res; |
| 1197 |
} |
| 1198 |
|
| 1199 |
|
| 1200 |
static int |
| 1201 |
save_float(Picklerobject *self, PyObject *args) |
| 1202 |
{ |
| 1203 |
double x = PyFloat_AS_DOUBLE((PyFloatObject *)args); |
| 1204 |
|
| 1205 |
if (self->bin) { |
| 1206 |
char str[9]; |
| 1207 |
str[0] = BINFLOAT; |
| 1208 |
if (_PyFloat_Pack8(x, (unsigned char *)&str[1], 0) < 0) |
| 1209 |
return -1; |
| 1210 |
if (self->write_func(self, str, 9) < 0) |
| 1211 |
return -1; |
| 1212 |
} |
| 1213 |
else { |
| 1214 |
int result = -1; |
| 1215 |
char *buf = NULL; |
| 1216 |
char op = FLOAT; |
| 1217 |
|
| 1218 |
if (self->write_func(self, &op, 1) < 0) |
| 1219 |
goto done; |
| 1220 |
|
| 1221 |
buf = PyOS_double_to_string(x, 'g', 17, 0, NULL); |
| 1222 |
if (!buf) { |
| 1223 |
PyErr_NoMemory(); |
| 1224 |
goto done; |
| 1225 |
} |
| 1226 |
|
| 1227 |
if (self->write_func(self, buf, strlen(buf)) < 0) |
| 1228 |
goto done; |
| 1229 |
|
| 1230 |
if (self->write_func(self, "\n", 1) < 0) |
| 1231 |
goto done; |
| 1232 |
|
| 1233 |
result = 0; |
| 1234 |
done: |
| 1235 |
PyMem_Free(buf); |
| 1236 |
return result; |
| 1237 |
} |
| 1238 |
|
| 1239 |
return 0; |
| 1240 |
} |
| 1241 |
|
| 1242 |
|
| 1243 |
static int |
| 1244 |
save_string(Picklerobject *self, PyObject *args, int doput) |
| 1245 |
{ |
| 1246 |
Py_ssize_t size, len; |
| 1247 |
PyObject *repr=0; |
| 1248 |
|
| 1249 |
if ((size = PyString_Size(args)) < 0) |
| 1250 |
return -1; |
| 1251 |
|
| 1252 |
if (!self->bin) { |
| 1253 |
char *repr_str; |
| 1254 |
|
| 1255 |
static char string = STRING; |
| 1256 |
|
| 1257 |
if (!( repr = PyObject_Repr(args))) |
| 1258 |
return -1; |
| 1259 |
|
| 1260 |
if ((len = PyString_Size(repr)) < 0) |
| 1261 |
goto err; |
| 1262 |
repr_str = PyString_AS_STRING((PyStringObject *)repr); |
| 1263 |
|
| 1264 |
if (self->write_func(self, &string, 1) < 0) |
| 1265 |
goto err; |
| 1266 |
|
| 1267 |
if (self->write_func(self, repr_str, len) < 0) |
| 1268 |
goto err; |
| 1269 |
|
| 1270 |
if (self->write_func(self, "\n", 1) < 0) |
| 1271 |
goto err; |
| 1272 |
|
| 1273 |
Py_XDECREF(repr); |
| 1274 |
} |
| 1275 |
else { |
| 1276 |
int i; |
| 1277 |
char c_str[5]; |
| 1278 |
|
| 1279 |
if (size < 256) { |
| 1280 |
c_str[0] = SHORT_BINSTRING; |
| 1281 |
c_str[1] = size; |
| 1282 |
len = 2; |
| 1283 |
} |
| 1284 |
else if (size <= 0x7fffffffL) { |
| 1285 |
c_str[0] = BINSTRING; |
| 1286 |
for (i = 1; i < 5; i++) |
| 1287 |
c_str[i] = (int)(size >> ((i - 1) * 8)); |
| 1288 |
len = 5; |
| 1289 |
} |
| 1290 |
else { |
| 1291 |
PyErr_SetString(PyExc_OverflowError, |
| 1292 |
"cannot serialize a string larger than 2 GiB"); |
| 1293 |
return -1; |
| 1294 |
} |
| 1295 |
|
| 1296 |
if (self->write_func(self, c_str, len) < 0) |
| 1297 |
return -1; |
| 1298 |
|
| 1299 |
if (size > 128 && Pdata_Check(self->file)) { |
| 1300 |
if (write_other(self, NULL, 0) < 0) return -1; |
| 1301 |
PDATA_APPEND(self->file, args, -1); |
| 1302 |
} |
| 1303 |
else { |
| 1304 |
if (self->write_func(self, |
| 1305 |
PyString_AS_STRING( |
| 1306 |
(PyStringObject *)args), |
| 1307 |
size) < 0) |
| 1308 |
return -1; |
| 1309 |
} |
| 1310 |
} |
| 1311 |
|
| 1312 |
if (doput) |
| 1313 |
if (put(self, args) < 0) |
| 1314 |
return -1; |
| 1315 |
|
| 1316 |
return 0; |
| 1317 |
|
| 1318 |
err: |
| 1319 |
Py_XDECREF(repr); |
| 1320 |
return -1; |
| 1321 |
} |
| 1322 |
|
| 1323 |
|
| 1324 |
#ifdef Py_USING_UNICODE |
| 1325 |
|
| 1326 |
|
| 1327 |
static PyObject * |
| 1328 |
modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, Py_ssize_t size) |
| 1329 |
{ |
| 1330 |
PyObject *repr; |
| 1331 |
char *p; |
| 1332 |
char *q; |
| 1333 |
|
| 1334 |
static const char *hexdigit = "0123456789abcdef"; |
| 1335 |
#ifdef Py_UNICODE_WIDE |
| 1336 |
const Py_ssize_t expandsize = 10; |
| 1337 |
#else |
| 1338 |
const Py_ssize_t expandsize = 6; |
| 1339 |
#endif |
| 1340 |
|
| 1341 |
if (size > PY_SSIZE_T_MAX / expandsize) |
| 1342 |
return PyErr_NoMemory(); |
| 1343 |
|
| 1344 |
repr = PyString_FromStringAndSize(NULL, expandsize * size); |
| 1345 |
if (repr == NULL) |
| 1346 |
return NULL; |
| 1347 |
if (size == 0) |
| 1348 |
return repr; |
| 1349 |
|
| 1350 |
p = q = PyString_AS_STRING(repr); |
| 1351 |
while (size-- > 0) { |
| 1352 |
Py_UNICODE ch = *s++; |
| 1353 |
#ifdef Py_UNICODE_WIDE |
| 1354 |
|
| 1355 |
if (ch >= 0x10000) { |
| 1356 |
*p++ = '\\'; |
| 1357 |
*p++ = 'U'; |
| 1358 |
*p++ = hexdigit[(ch >> 28) & 0xf]; |
| 1359 |
*p++ = hexdigit[(ch >> 24) & 0xf]; |
| 1360 |
*p++ = hexdigit[(ch >> 20) & 0xf]; |
| 1361 |
*p++ = hexdigit[(ch >> 16) & 0xf]; |
| 1362 |
*p++ = hexdigit[(ch >> 12) & 0xf]; |
| 1363 |
*p++ = hexdigit[(ch >> 8) & 0xf]; |
| 1364 |
*p++ = hexdigit[(ch >> 4) & 0xf]; |
| 1365 |
*p++ = hexdigit[ch & 15]; |
| 1366 |
} |
| 1367 |
else |
| 1368 |
#else |
| 1369 |
|
| 1370 |
if (ch >= 0xD800 && ch < 0xDC00) { |
| 1371 |
Py_UNICODE ch2; |
| 1372 |
Py_UCS4 ucs; |
| 1373 |
|
| 1374 |
ch2 = *s++; |
| 1375 |
size--; |
| 1376 |
if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 1377 |
ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 1378 |
*p++ = '\\'; |
| 1379 |
*p++ = 'U'; |
| 1380 |
*p++ = hexdigit[(ucs >> 28) & 0xf]; |
| 1381 |
*p++ = hexdigit[(ucs >> 24) & 0xf]; |
| 1382 |
*p++ = hexdigit[(ucs >> 20) & 0xf]; |
| 1383 |
*p++ = hexdigit[(ucs >> 16) & 0xf]; |
| 1384 |
*p++ = hexdigit[(ucs >> 12) & 0xf]; |
| 1385 |
*p++ = hexdigit[(ucs >> 8) & 0xf]; |
| 1386 |
*p++ = hexdigit[(ucs >> 4) & 0xf]; |
| 1387 |
*p++ = hexdigit[ucs & 0xf]; |
| 1388 |
continue; |
| 1389 |
} |
| 1390 |
|
| 1391 |
s--; |
| 1392 |
size++; |
| 1393 |
} |
| 1394 |
#endif |
| 1395 |
|
| 1396 |
if (ch >= 256 || ch == '\\' || ch == '\n') { |
| 1397 |
*p++ = '\\'; |
| 1398 |
*p++ = 'u'; |
| 1399 |
*p++ = hexdigit[(ch >> 12) & 0xf]; |
| 1400 |
*p++ = hexdigit[(ch >> 8) & 0xf]; |
| 1401 |
*p++ = hexdigit[(ch >> 4) & 0xf]; |
| 1402 |
*p++ = hexdigit[ch & 15]; |
| 1403 |
} |
| 1404 |
|
| 1405 |
else |
| 1406 |
*p++ = (char) ch; |
| 1407 |
} |
| 1408 |
*p = '\0'; |
| 1409 |
_PyString_Resize(&repr, p - q); |
| 1410 |
return repr; |
| 1411 |
} |
| 1412 |
|
| 1413 |
static int |
| 1414 |
save_unicode(Picklerobject *self, PyObject *args, int doput) |
| 1415 |
{ |
| 1416 |
Py_ssize_t size, len; |
| 1417 |
PyObject *repr=0; |
| 1418 |
|
| 1419 |
if (!PyUnicode_Check(args)) |
| 1420 |
return -1; |
| 1421 |
|
| 1422 |
if (!self->bin) { |
| 1423 |
char *repr_str; |
| 1424 |
static char string = UNICODE; |
| 1425 |
|
| 1426 |
repr = modified_EncodeRawUnicodeEscape( |
| 1427 |
PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args)); |
| 1428 |
if (!repr) |
| 1429 |
return -1; |
| 1430 |
|
| 1431 |
if ((len = PyString_Size(repr)) < 0) |
| 1432 |
goto err; |
| 1433 |
repr_str = PyString_AS_STRING((PyStringObject *)repr); |
| 1434 |
|
| 1435 |
if (self->write_func(self, &string, 1) < 0) |
| 1436 |
goto err; |
| 1437 |
|
| 1438 |
if (self->write_func(self, repr_str, len) < 0) |
| 1439 |
goto err; |
| 1440 |
|
| 1441 |
if (self->write_func(self, "\n", 1) < 0) |
| 1442 |
goto err; |
| 1443 |
|
| 1444 |
Py_XDECREF(repr); |
| 1445 |
} |
| 1446 |
else { |
| 1447 |
int i; |
| 1448 |
char c_str[5]; |
| 1449 |
|
| 1450 |
if (!( repr = PyUnicode_AsUTF8String(args))) |
| 1451 |
return -1; |
| 1452 |
|
| 1453 |
if ((size = PyString_Size(repr)) < 0) |
| 1454 |
goto err; |
| 1455 |
if (size > 0x7fffffffL) { |
| 1456 |
PyErr_SetString(PyExc_OverflowError, |
| 1457 |
"cannot serialize a Unicode string larger than 2 GiB"); |
| 1458 |
goto err; |
| 1459 |
} |
| 1460 |
|
| 1461 |
c_str[0] = BINUNICODE; |
| 1462 |
for (i = 1; i < 5; i++) |
| 1463 |
c_str[i] = (int)(size >> ((i - 1) * 8)); |
| 1464 |
len = 5; |
| 1465 |
|
| 1466 |
if (self->write_func(self, c_str, len) < 0) |
| 1467 |
goto err; |
| 1468 |
|
| 1469 |
if (size > 128 && Pdata_Check(self->file)) { |
| 1470 |
if (write_other(self, NULL, 0) < 0) |
| 1471 |
goto err; |
| 1472 |
PDATA_APPEND(self->file, repr, -1); |
| 1473 |
} |
| 1474 |
else { |
| 1475 |
if (self->write_func(self, PyString_AS_STRING(repr), |
| 1476 |
size) < 0) |
| 1477 |
goto err; |
| 1478 |
} |
| 1479 |
|
| 1480 |
Py_DECREF(repr); |
| 1481 |
} |
| 1482 |
|
| 1483 |
if (doput) |
| 1484 |
if (put(self, args) < 0) |
| 1485 |
return -1; |
| 1486 |
|
| 1487 |
return 0; |
| 1488 |
|
| 1489 |
err: |
| 1490 |
Py_XDECREF(repr); |
| 1491 |
return -1; |
| 1492 |
} |
| 1493 |
#endif |
| 1494 |
|
| 1495 |
|
| 1496 |
static int |
| 1497 |
store_tuple_elements(Picklerobject *self, PyObject *t, int len) |
| 1498 |
{ |
| 1499 |
Py_ssize_t i; |
| 1500 |
int res = -1; |
| 1501 |
|
| 1502 |
assert(PyTuple_Size(t) == len); |
| 1503 |
|
| 1504 |
for (i = 0; i < len; i++) { |
| 1505 |
PyObject *element = PyTuple_GET_ITEM(t, i); |
| 1506 |
|
| 1507 |
if (element == NULL) |
| 1508 |
goto finally; |
| 1509 |
if (save(self, element, 0) < 0) |
| 1510 |
goto finally; |
| 1511 |
} |
| 1512 |
res = 0; |
| 1513 |
|
| 1514 |
finally: |
| 1515 |
return res; |
| 1516 |
} |
| 1517 |
|
| 1518 |
|
| 1519 |
|
| 1520 |
|
| 1521 |
|
| 1522 |
|
| 1523 |
|
| 1524 |
static int |
| 1525 |
save_tuple(Picklerobject *self, PyObject *args) |
| 1526 |
{ |
| 1527 |
PyObject *py_tuple_id = NULL; |
| 1528 |
Py_ssize_t len, i; |
| 1529 |
int res = -1; |
| 1530 |
|
| 1531 |
static char tuple = TUPLE; |
| 1532 |
static char pop = POP; |
| 1533 |
static char pop_mark = POP_MARK; |
| 1534 |
static char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3}; |
| 1535 |
|
| 1536 |
if ((len = PyTuple_Size(args)) < 0) |
| 1537 |
goto finally; |
| 1538 |
|
| 1539 |
if (len == 0) { |
| 1540 |
char c_str[2]; |
| 1541 |
|
| 1542 |
if (self->proto) { |
| 1543 |
c_str[0] = EMPTY_TUPLE; |
| 1544 |
len = 1; |
| 1545 |
} |
| 1546 |
else { |
| 1547 |
c_str[0] = MARK; |
| 1548 |
c_str[1] = TUPLE; |
| 1549 |
len = 2; |
| 1550 |
} |
| 1551 |
if (self->write_func(self, c_str, len) >= 0) |
| 1552 |
res = 0; |
| 1553 |
|
| 1554 |
goto finally; |
| 1555 |
} |
| 1556 |
|
| 1557 |
|
| 1558 |
|
| 1559 |
|
| 1560 |
|
| 1561 |
|
| 1562 |
|
| 1563 |
|
| 1564 |
py_tuple_id = PyLong_FromVoidPtr(args); |
| 1565 |
if (py_tuple_id == NULL) |
| 1566 |
goto finally; |
| 1567 |
|
| 1568 |
if (len <= 3 && self->proto >= 2) { |
| 1569 |
|
| 1570 |
if (store_tuple_elements(self, args, len) < 0) |
| 1571 |
goto finally; |
| 1572 |
if (PyDict_GetItem(self->memo, py_tuple_id)) { |
| 1573 |
|
| 1574 |
for (i = 0; i < len; ++i) |
| 1575 |
if (self->write_func(self, &pop, 1) < 0) |
| 1576 |
goto finally; |
| 1577 |
|
| 1578 |
if (get(self, py_tuple_id) < 0) |
| 1579 |
goto finally; |
| 1580 |
res = 0; |
| 1581 |
goto finally; |
| 1582 |
} |
| 1583 |
|
| 1584 |
if (self->write_func(self, len2opcode + len, 1) < 0) |
| 1585 |
goto finally; |
| 1586 |
goto memoize; |
| 1587 |
} |
| 1588 |
|
| 1589 |
|
| 1590 |
|
| 1591 |
|
| 1592 |
if (self->write_func(self, &MARKv, 1) < 0) |
| 1593 |
goto finally; |
| 1594 |
|
| 1595 |
if (store_tuple_elements(self, args, len) < 0) |
| 1596 |
goto finally; |
| 1597 |
|
| 1598 |
if (PyDict_GetItem(self->memo, py_tuple_id)) { |
| 1599 |
|
| 1600 |
if (self->bin) { |
| 1601 |
if (self->write_func(self, &pop_mark, 1) < 0) |
| 1602 |
goto finally; |
| 1603 |
} |
| 1604 |
else { |
| 1605 |
|
| 1606 |
|
| 1607 |
|
| 1608 |
for (i = 0; i <= len; i++) |
| 1609 |
if (self->write_func(self, &pop, 1) < 0) |
| 1610 |
goto finally; |
| 1611 |
} |
| 1612 |
|
| 1613 |
if (get(self, py_tuple_id) >= 0) |
| 1614 |
res = 0; |
| 1615 |
goto finally; |
| 1616 |
} |
| 1617 |
|
| 1618 |
|
| 1619 |
if (self->write_func(self, &tuple, 1) < 0) |
| 1620 |
goto finally; |
| 1621 |
|
| 1622 |
memoize: |
| 1623 |
if (put(self, args) >= 0) |
| 1624 |
res = 0; |
| 1625 |
|
| 1626 |
finally: |
| 1627 |
Py_XDECREF(py_tuple_id); |
| 1628 |
return res; |
| 1629 |
} |
| 1630 |
|
| 1631 |
|
| 1632 |
|
| 1633 |
|
| 1634 |
|
| 1635 |
|
| 1636 |
|
| 1637 |
static int |
| 1638 |
batch_list(Picklerobject *self, PyObject *iter) |
| 1639 |
{ |
| 1640 |
PyObject *obj = NULL; |
| 1641 |
PyObject *firstitem = NULL; |
| 1642 |
int i, n; |
| 1643 |
|
| 1644 |
static char append = APPEND; |
| 1645 |
static char appends = APPENDS; |
| 1646 |
|
| 1647 |
assert(iter != NULL); |
| 1648 |
|
| 1649 |
if (self->proto == 0) { |
| 1650 |
|
| 1651 |
for (;;) { |
| 1652 |
obj = PyIter_Next(iter); |
| 1653 |
if (obj == NULL) { |
| 1654 |
if (PyErr_Occurred()) |
| 1655 |
return -1; |
| 1656 |
break; |
| 1657 |
} |
| 1658 |
i = save(self, obj, 0); |
| 1659 |
Py_DECREF(obj); |
| 1660 |
if (i < 0) |
| 1661 |
return -1; |
| 1662 |
if (self->write_func(self, &append, 1) < 0) |
| 1663 |
return -1; |
| 1664 |
} |
| 1665 |
return 0; |
| 1666 |
} |
| 1667 |
|
| 1668 |
|
| 1669 |
do { |
| 1670 |
|
| 1671 |
firstitem = PyIter_Next(iter); |
| 1672 |
if (firstitem == NULL) { |
| 1673 |
if (PyErr_Occurred()) |
| 1674 |
goto BatchFailed; |
| 1675 |
|
| 1676 |
|
| 1677 |
break; |
| 1678 |
} |
| 1679 |
|
| 1680 |
|
| 1681 |
obj = PyIter_Next(iter); |
| 1682 |
if (obj == NULL) { |
| 1683 |
if (PyErr_Occurred()) |
| 1684 |
goto BatchFailed; |
| 1685 |
|
| 1686 |
|
| 1687 |
if (save(self, firstitem, 0) < 0) |
| 1688 |
goto BatchFailed; |
| 1689 |
if (self->write_func(self, &append, 1) < 0) |
| 1690 |
goto BatchFailed; |
| 1691 |
Py_CLEAR(firstitem); |
| 1692 |
break; |
| 1693 |
} |
| 1694 |
|
| 1695 |
|
| 1696 |
|
| 1697 |
|
| 1698 |
if (self->write_func(self, &MARKv, 1) < 0) |
| 1699 |
goto BatchFailed; |
| 1700 |
|
| 1701 |
if (save(self, firstitem, 0) < 0) |
| 1702 |
goto BatchFailed; |
| 1703 |
Py_CLEAR(firstitem); |
| 1704 |
n = 1; |
| 1705 |
|
| 1706 |
|
| 1707 |
while (obj) { |
| 1708 |
if (save(self, obj, 0) < 0) |
| 1709 |
goto BatchFailed; |
| 1710 |
Py_CLEAR(obj); |
| 1711 |
n += 1; |
| 1712 |
|
| 1713 |
if (n == BATCHSIZE) |
| 1714 |
break; |
| 1715 |
|
| 1716 |
obj = PyIter_Next(iter); |
| 1717 |
if (obj == NULL) { |
| 1718 |
if (PyErr_Occurred()) |
| 1719 |
goto BatchFailed; |
| 1720 |
break; |
| 1721 |
} |
| 1722 |
} |
| 1723 |
|
| 1724 |
if (self->write_func(self, &appends, 1) < 0) |
| 1725 |
goto BatchFailed; |
| 1726 |
|
| 1727 |
} while (n == BATCHSIZE); |
| 1728 |
return 0; |
| 1729 |
|
| 1730 |
BatchFailed: |
| 1731 |
Py_XDECREF(firstitem); |
| 1732 |
Py_XDECREF(obj); |
| 1733 |
return -1; |
| 1734 |
} |
| 1735 |
|
| 1736 |
static int |
| 1737 |
save_list(Picklerobject *self, PyObject *args) |
| 1738 |
{ |
| 1739 |
int res = -1; |
| 1740 |
char s[3]; |
| 1741 |
Py_ssize_t len; |
| 1742 |
PyObject *iter; |
| 1743 |
|
| 1744 |
if (self->fast && !fast_save_enter(self, args)) |
| 1745 |
goto finally; |
| 1746 |
|
| 1747 |
|
| 1748 |
if (self->bin) { |
| 1749 |
s[0] = EMPTY_LIST; |
| 1750 |
len = 1; |
| 1751 |
} |
| 1752 |
else { |
| 1753 |
s[0] = MARK; |
| 1754 |
s[1] = LIST; |
| 1755 |
len = 2; |
| 1756 |
} |
| 1757 |
|
| 1758 |
if (self->write_func(self, s, len) < 0) |
| 1759 |
goto finally; |
| 1760 |
|
| 1761 |
|
| 1762 |
if ((len = PyList_Size(args)) < 0) |
| 1763 |
goto finally; |
| 1764 |
|
| 1765 |
|
| 1766 |
if (len == 0) { |
| 1767 |
if (put(self, args) >= 0) |
| 1768 |
res = 0; |
| 1769 |
goto finally; |
| 1770 |
} |
| 1771 |
if (put2(self, args) < 0) |
| 1772 |
goto finally; |
| 1773 |
|
| 1774 |
|
| 1775 |
iter = PyObject_GetIter(args); |
| 1776 |
if (iter == NULL) |
| 1777 |
goto finally; |
| 1778 |
|
| 1779 |
if (Py_EnterRecursiveCall(" while pickling an object") == 0) |
| 1780 |
{ |
| 1781 |
res = batch_list(self, iter); |
| 1782 |
Py_LeaveRecursiveCall(); |
| 1783 |
} |
| 1784 |
Py_DECREF(iter); |
| 1785 |
|
| 1786 |
finally: |
| 1787 |
if (self->fast && !fast_save_leave(self, args)) |
| 1788 |
res = -1; |
| 1789 |
|
| 1790 |
return res; |
| 1791 |
} |
| 1792 |
|
| 1793 |
|
| 1794 |
|
| 1795 |
|
| 1796 |
|
| 1797 |
|
| 1798 |
|
| 1799 |
|
| 1800 |
|
| 1801 |
|
| 1802 |
|
| 1803 |
|
| 1804 |
|
| 1805 |
static int |
| 1806 |
batch_dict(Picklerobject *self, PyObject *iter) |
| 1807 |
{ |
| 1808 |
PyObject *p = NULL; |
| 1809 |
PyObject *firstitem = NULL; |
| 1810 |
int i, n; |
| 1811 |
|
| 1812 |
static char setitem = SETITEM; |
| 1813 |
static char setitems = SETITEMS; |
| 1814 |
|
| 1815 |
assert(iter != NULL); |
| 1816 |
|
| 1817 |
if (self->proto == 0) { |
| 1818 |
|
| 1819 |
for (;;) { |
| 1820 |
p = PyIter_Next(iter); |
| 1821 |
if (p == NULL) { |
| 1822 |
if (PyErr_Occurred()) |
| 1823 |
return -1; |
| 1824 |
break; |
| 1825 |
} |
| 1826 |
if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) { |
| 1827 |
PyErr_SetString(PyExc_TypeError, "dict items " |
| 1828 |
"iterator must return 2-tuples"); |
| 1829 |
return -1; |
| 1830 |
} |
| 1831 |
i = save(self, PyTuple_GET_ITEM(p, 0), 0); |
| 1832 |
if (i >= 0) |
| 1833 |
i = save(self, PyTuple_GET_ITEM(p, 1), 0); |
| 1834 |
Py_DECREF(p); |
| 1835 |
if (i < 0) |
| 1836 |
return -1; |
| 1837 |
if (self->write_func(self, &setitem, 1) < 0) |
| 1838 |
return -1; |
| 1839 |
} |
| 1840 |
return 0; |
| 1841 |
} |
| 1842 |
|
| 1843 |
|
| 1844 |
do { |
| 1845 |
|
| 1846 |
firstitem = PyIter_Next(iter); |
| 1847 |
if (firstitem == NULL) { |
| 1848 |
if (PyErr_Occurred()) |
| 1849 |
goto BatchFailed; |
| 1850 |
|
| 1851 |
|
| 1852 |
break; |
| 1853 |
} |
| 1854 |
if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) { |
| 1855 |
PyErr_SetString(PyExc_TypeError, "dict items " |
| 1856 |
"iterator must return 2-tuples"); |
| 1857 |
goto BatchFailed; |
| 1858 |
} |
| 1859 |
|
| 1860 |
|
| 1861 |
p = PyIter_Next(iter); |
| 1862 |
if (p == NULL) { |
| 1863 |
if (PyErr_Occurred()) |
| 1864 |
goto BatchFailed; |
| 1865 |
|
| 1866 |
|
| 1867 |
if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0) |
| 1868 |
goto BatchFailed; |
| 1869 |
if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0) |
| 1870 |
goto BatchFailed; |
| 1871 |
if (self->write_func(self, &setitem, 1) < 0) |
| 1872 |
goto BatchFailed; |
| 1873 |
Py_CLEAR(firstitem); |
| 1874 |
break; |
| 1875 |
} |
| 1876 |
|
| 1877 |
|
| 1878 |
|
| 1879 |
|
| 1880 |
if (self->write_func(self, &MARKv, 1) < 0) |
| 1881 |
goto BatchFailed; |
| 1882 |
|
| 1883 |
if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0) |
| 1884 |
goto BatchFailed; |
| 1885 |
if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0) |
| 1886 |
goto BatchFailed; |
| 1887 |
Py_CLEAR(firstitem); |
| 1888 |
n = 1; |
| 1889 |
|
| 1890 |
|
| 1891 |
while (p) { |
| 1892 |
if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) { |
| 1893 |
PyErr_SetString(PyExc_TypeError, "dict items " |
| 1894 |
"iterator must return 2-tuples"); |
| 1895 |
goto BatchFailed; |
| 1896 |
} |
| 1897 |
if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0) |
| 1898 |
goto BatchFailed; |
| 1899 |
if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0) |
| 1900 |
goto BatchFailed; |
| 1901 |
Py_CLEAR(p); |
| 1902 |
n += 1; |
| 1903 |
|
| 1904 |
if (n == BATCHSIZE) |
| 1905 |
break; |
| 1906 |
|
| 1907 |
p = PyIter_Next(iter); |
| 1908 |
if (p == NULL) { |
| 1909 |
if (PyErr_Occurred()) |
| 1910 |
goto BatchFailed; |
| 1911 |
break; |
| 1912 |
} |
| 1913 |
} |
| 1914 |
|
| 1915 |
if (self->write_func(self, &setitems, 1) < 0) |
| 1916 |
goto BatchFailed; |
| 1917 |
|
| 1918 |
} while (n == BATCHSIZE); |
| 1919 |
return 0; |
| 1920 |
|
| 1921 |
BatchFailed: |
| 1922 |
Py_XDECREF(firstitem); |
| 1923 |
Py_XDECREF(p); |
| 1924 |
return -1; |
| 1925 |
} |
| 1926 |
|
| 1927 |
|
| 1928 |
|
| 1929 |
|
| 1930 |
|
| 1931 |
|
| 1932 |
|
| 1933 |
|
| 1934 |
|
| 1935 |
|
| 1936 |
static int |
| 1937 |
batch_dict_exact(Picklerobject *self, PyObject *obj) |
| 1938 |
{ |
| 1939 |
PyObject *key = NULL, *value = NULL; |
| 1940 |
int i; |
| 1941 |
Py_ssize_t dict_size, ppos = 0; |
| 1942 |
|
| 1943 |
static char setitem = SETITEM; |
| 1944 |
static char setitems = SETITEMS; |
| 1945 |
|
| 1946 |
assert(obj != NULL); |
| 1947 |
assert(self->proto > 0); |
| 1948 |
|
| 1949 |
dict_size = PyDict_Size(obj); |
| 1950 |
|
| 1951 |
|
| 1952 |
if (dict_size == 1) { |
| 1953 |
PyDict_Next(obj, &ppos, &key, &value); |
| 1954 |
if (save(self, key, 0) < 0) |
| 1955 |
return -1; |
| 1956 |
if (save(self, value, 0) < 0) |
| 1957 |
return -1; |
| 1958 |
if (self->write_func(self, &setitem, 1) < 0) |
| 1959 |
return -1; |
| 1960 |
return 0; |
| 1961 |
} |
| 1962 |
|
| 1963 |
|
| 1964 |
do { |
| 1965 |
i = 0; |
| 1966 |
if (self->write_func(self, &MARKv, 1) < 0) |
| 1967 |
return -1; |
| 1968 |
while (PyDict_Next(obj, &ppos, &key, &value)) { |
| 1969 |
if (save(self, key, 0) < 0) |
| 1970 |
return -1; |
| 1971 |
if (save(self, value, 0) < 0) |
| 1972 |
return -1; |
| 1973 |
if (++i == BATCHSIZE) |
| 1974 |
break; |
| 1975 |
} |
| 1976 |
if (self->write_func(self, &setitems, 1) < 0) |
| 1977 |
return -1; |
| 1978 |
if (PyDict_Size(obj) != dict_size) { |
| 1979 |
PyErr_Format( |
| 1980 |
PyExc_RuntimeError, |
| 1981 |
"dictionary changed size during iteration"); |
| 1982 |
return -1; |
| 1983 |
} |
| 1984 |
|
| 1985 |
} while (i == BATCHSIZE); |
| 1986 |
return 0; |
| 1987 |
} |
| 1988 |
|
| 1989 |
static int |
| 1990 |
save_dict(Picklerobject *self, PyObject *args) |
| 1991 |
{ |
| 1992 |
int res = -1; |
| 1993 |
char s[3]; |
| 1994 |
Py_ssize_t len; |
| 1995 |
|
| 1996 |
if (self->fast && !fast_save_enter(self, args)) |
| 1997 |
goto finally; |
| 1998 |
|
| 1999 |
|
| 2000 |
if (self->bin) { |
| 2001 |
s[0] = EMPTY_DICT; |
| 2002 |
len = 1; |
| 2003 |
} |
| 2004 |
else { |
| 2005 |
s[0] = MARK; |
| 2006 |
s[1] = DICT; |
| 2007 |
len = 2; |
| 2008 |
} |
| 2009 |
|
| 2010 |
if (self->write_func(self, s, len) < 0) |
| 2011 |
goto finally; |
| 2012 |
|
| 2013 |
|
| 2014 |
if ((len = PyDict_Size(args)) < 0) |
| 2015 |
goto finally; |
| 2016 |
|
| 2017 |
if (len == 0) { |
| 2018 |
if (put(self, args) >= 0) |
| 2019 |
res = 0; |
| 2020 |
goto finally; |
| 2021 |
} |
| 2022 |
if (put2(self, args) < 0) |
| 2023 |
goto finally; |
| 2024 |
|
| 2025 |
|
| 2026 |
if (PyDict_CheckExact(args) && self->proto > 0) { |
| 2027 |
|
| 2028 |
|
| 2029 |
if (Py_EnterRecursiveCall(" while pickling an object") == 0) { |
| 2030 |
res = batch_dict_exact(self, args); |
| 2031 |
Py_LeaveRecursiveCall(); |
| 2032 |
} |
| 2033 |
} else { |
| 2034 |
PyObject *iter = PyObject_CallMethod(args, "iteritems", "()"); |
| 2035 |
if (iter == NULL) |
| 2036 |
goto finally; |
| 2037 |
if (Py_EnterRecursiveCall(" while pickling an object") == 0) { |
| 2038 |
res = batch_dict(self, iter); |
| 2039 |
Py_LeaveRecursiveCall(); |
| 2040 |
} |
| 2041 |
Py_DECREF(iter); |
| 2042 |
} |
| 2043 |
|
| 2044 |
finally: |
| 2045 |
if (self->fast && !fast_save_leave(self, args)) |
| 2046 |
res = -1; |
| 2047 |
|
| 2048 |
return res; |
| 2049 |
} |
| 2050 |
|
| 2051 |
|
| 2052 |
static int |
| 2053 |
save_inst(Picklerobject *self, PyObject *args) |
| 2054 |
{ |
| 2055 |
PyObject *class = 0, *module = 0, *name = 0, *state = 0, |
| 2056 |
*getinitargs_func = 0, *getstate_func = 0, *class_args = 0; |
| 2057 |
char *module_str, *name_str; |
| 2058 |
int module_size, name_size, res = -1; |
| 2059 |
|
| 2060 |
static char inst = INST, obj = OBJ, build = BUILD; |
| 2061 |
|
| 2062 |
if (self->fast && !fast_save_enter(self, args)) |
| 2063 |
goto finally; |
| 2064 |
|
| 2065 |
if (self->write_func(self, &MARKv, 1) < 0) |
| 2066 |
goto finally; |
| 2067 |
|
| 2068 |
if (!( class = PyObject_GetAttr(args, __class___str))) |
| 2069 |
goto finally; |
| 2070 |
|
| 2071 |
if (self->bin) { |
| 2072 |
if (save(self, class, 0) < 0) |
| 2073 |
goto finally; |
| 2074 |
} |
| 2075 |
|
| 2076 |
if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) { |
| 2077 |
PyObject *element = 0; |
| 2078 |
Py_ssize_t i, len; |
| 2079 |
|
| 2080 |
if (!( class_args = |
| 2081 |
PyObject_Call(getinitargs_func, empty_tuple, NULL))) |
| 2082 |
goto finally; |
| 2083 |
|
| 2084 |
if ((len = PyObject_Size(class_args)) < 0) |
| 2085 |
goto finally; |
| 2086 |
|
| 2087 |
for (i = 0; i < len; i++) { |
| 2088 |
if (!( element = PySequence_GetItem(class_args, i))) |
| 2089 |
goto finally; |
| 2090 |
|
| 2091 |
if (save(self, element, 0) < 0) { |
| 2092 |
Py_DECREF(element); |
| 2093 |
goto finally; |
| 2094 |
} |
| 2095 |
|
| 2096 |
Py_DECREF(element); |
| 2097 |
} |
| 2098 |
} |
| 2099 |
else { |
| 2100 |
if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 2101 |
PyErr_Clear(); |
| 2102 |
else |
| 2103 |
goto finally; |
| 2104 |
} |
| 2105 |
|
| 2106 |
if (!self->bin) { |
| 2107 |
if (!( name = ((PyClassObject *)class)->cl_name )) { |
| 2108 |
PyErr_SetString(PicklingError, "class has no name"); |
| 2109 |
goto finally; |
| 2110 |
} |
| 2111 |
|
| 2112 |
if (!( module = whichmodule(class, name))) |
| 2113 |
goto finally; |
| 2114 |
|
| 2115 |
|
| 2116 |
if ((module_size = PyString_Size(module)) < 0 || |
| 2117 |
(name_size = PyString_Size(name)) < 0) |
| 2118 |
goto finally; |
| 2119 |
|
| 2120 |
module_str = PyString_AS_STRING((PyStringObject *)module); |
| 2121 |
name_str = PyString_AS_STRING((PyStringObject *)name); |
| 2122 |
|
| 2123 |
if (self->write_func(self, &inst, 1) < 0) |
| 2124 |
goto finally; |
| 2125 |
|
| 2126 |
if (self->write_func(self, module_str, module_size) < 0) |
| 2127 |
goto finally; |
| 2128 |
|
| 2129 |
if (self->write_func(self, "\n", 1) < 0) |
| 2130 |
goto finally; |
| 2131 |
|
| 2132 |
if (self->write_func(self, name_str, name_size) < 0) |
| 2133 |
goto finally; |
| 2134 |
|
| 2135 |
if (self->write_func(self, "\n", 1) < 0) |
| 2136 |
goto finally; |
| 2137 |
} |
| 2138 |
else if (self->write_func(self, &obj, 1) < 0) { |
| 2139 |
goto finally; |
| 2140 |
} |
| 2141 |
|
| 2142 |
if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) { |
| 2143 |
state = PyObject_Call(getstate_func, empty_tuple, NULL); |
| 2144 |
if (!state) |
| 2145 |
goto finally; |
| 2146 |
} |
| 2147 |
else { |
| 2148 |
if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 2149 |
PyErr_Clear(); |
| 2150 |
else |
| 2151 |
goto finally; |
| 2152 |
|
| 2153 |
if (!( state = PyObject_GetAttr(args, __dict___str))) { |
| 2154 |
if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 2155 |
PyErr_Clear(); |
| 2156 |
else |
| 2157 |
goto finally; |
| 2158 |
res = 0; |
| 2159 |
goto finally; |
| 2160 |
} |
| 2161 |
} |
| 2162 |
|
| 2163 |
if (!PyDict_Check(state)) { |
| 2164 |
if (put2(self, args) < 0) |
| 2165 |
goto finally; |
| 2166 |
} |
| 2167 |
else { |
| 2168 |
if (put(self, args) < 0) |
| 2169 |
goto finally; |
| 2170 |
} |
| 2171 |
|
| 2172 |
if (save(self, state, 0) < 0) |
| 2173 |
goto finally; |
| 2174 |
|
| 2175 |
if (self->write_func(self, &build, 1) < 0) |
| 2176 |
goto finally; |
| 2177 |
|
| 2178 |
res = 0; |
| 2179 |
|
| 2180 |
finally: |
| 2181 |
if (self->fast && !fast_save_leave(self, args)) |
| 2182 |
res = -1; |
| 2183 |
|
| 2184 |
Py_XDECREF(module); |
| 2185 |
Py_XDECREF(class); |
| 2186 |
Py_XDECREF(state); |
| 2187 |
Py_XDECREF(getinitargs_func); |
| 2188 |
Py_XDECREF(getstate_func); |
| 2189 |
Py_XDECREF(class_args); |
| 2190 |
|
| 2191 |
return res; |
| 2192 |
} |
| 2193 |
|
| 2194 |
|
| 2195 |
static int |
| 2196 |
save_global(Picklerobject *self, PyObject *args, PyObject *name) |
| 2197 |
{ |
| 2198 |
PyObject *global_name = 0, *module = 0, *mod = 0, *klass = 0; |
| 2199 |
char *name_str, *module_str; |
| 2200 |
int module_size, name_size, res = -1; |
| 2201 |
|
| 2202 |
static char global = GLOBAL; |
| 2203 |
|
| 2204 |
if (name) { |
| 2205 |
global_name = name; |
| 2206 |
Py_INCREF(global_name); |
| 2207 |
} |
| 2208 |
else { |
| 2209 |
if (!( global_name = PyObject_GetAttr(args, __name___str))) |
| 2210 |
goto finally; |
| 2211 |
} |
| 2212 |
|
| 2213 |
if (!( module = whichmodule(args, global_name))) |
| 2214 |
goto finally; |
| 2215 |
|
| 2216 |
if ((module_size = PyString_Size(module)) < 0 || |
| 2217 |
(name_size = PyString_Size(global_name)) < 0) |
| 2218 |
goto finally; |
| 2219 |
|
| 2220 |
module_str = PyString_AS_STRING((PyStringObject *)module); |
| 2221 |
name_str = PyString_AS_STRING((PyStringObject *)global_name); |
| 2222 |
|
| 2223 |
|
| 2224 |
|
| 2225 |
mod = PyImport_ImportModule(module_str); |
| 2226 |
if (mod == NULL) { |
| 2227 |
cPickle_ErrFormat(PicklingError, |
| 2228 |
"Can't pickle %s: import of module %s " |
| 2229 |
"failed", |
| 2230 |
"OS", args, module); |
| 2231 |
goto finally; |
| 2232 |
} |
| 2233 |
klass = PyObject_GetAttrString(mod, name_str); |
| 2234 |
if (klass == NULL) { |
| 2235 |
cPickle_ErrFormat(PicklingError, |
| 2236 |
"Can't pickle %s: attribute lookup %s.%s " |
| 2237 |
"failed", |
| 2238 |
"OSS", args, module, global_name); |
| 2239 |
goto finally; |
| 2240 |
} |
| 2241 |
if (klass != args) { |
| 2242 |
Py_DECREF(klass); |
| 2243 |
cPickle_ErrFormat(PicklingError, |
| 2244 |
"Can't pickle %s: it's not the same object " |
| 2245 |
"as %s.%s", |
| 2246 |
"OSS", args, module, global_name); |
| 2247 |
goto finally; |
| 2248 |
} |
| 2249 |
Py_DECREF(klass); |
| 2250 |
|
| 2251 |
if (self->proto >= 2) { |
| 2252 |
|
| 2253 |
|
| 2254 |
|
| 2255 |
PyObject *py_code; |
| 2256 |
long code; |
| 2257 |
char c_str[5]; |
| 2258 |
int n; |
| 2259 |
|
| 2260 |
PyTuple_SET_ITEM(two_tuple, 0, module); |
| 2261 |
PyTuple_SET_ITEM(two_tuple, 1, global_name); |
| 2262 |
py_code = PyDict_GetItem(extension_registry, two_tuple); |
| 2263 |
if (py_code == NULL) |
| 2264 |
goto gen_global; |
| 2265 |
|
| 2266 |
|
| 2267 |
if (!PyInt_Check(py_code)) { |
| 2268 |
cPickle_ErrFormat(PicklingError, "Can't pickle %s: " |
| 2269 |
"extension code %s isn't an integer", |
| 2270 |
"OO", args, py_code); |
| 2271 |
goto finally; |
| 2272 |
} |
| 2273 |
code = PyInt_AS_LONG(py_code); |
| 2274 |
if (code <= 0 || code > 0x7fffffffL) { |
| 2275 |
cPickle_ErrFormat(PicklingError, "Can't pickle %s: " |
| 2276 |
"extension code %ld is out of range", |
| 2277 |
"Ol", args, code); |
| 2278 |
goto finally; |
| 2279 |
} |
| 2280 |
|
| 2281 |
|
| 2282 |
if (code <= 0xff) { |
| 2283 |
c_str[0] = EXT1; |
| 2284 |
c_str[1] = (char)code; |
| 2285 |
n = 2; |
| 2286 |
} |
| 2287 |
else if (code <= 0xffff) { |
| 2288 |
c_str[0] = EXT2; |
| 2289 |
c_str[1] = (char)(code & 0xff); |
| 2290 |
c_str[2] = (char)((code >> 8) & 0xff); |
| 2291 |
n = 3; |
| 2292 |
} |
| 2293 |
else { |
| 2294 |
c_str[0] = EXT4; |
| 2295 |
c_str[1] = (char)(code & 0xff); |
| 2296 |
c_str[2] = (char)((code >> 8) & 0xff); |
| 2297 |
c_str[3] = (char)((code >> 16) & 0xff); |
| 2298 |
c_str[4] = (char)((code >> 24) & 0xff); |
| 2299 |
n = 5; |
| 2300 |
} |
| 2301 |
|
| 2302 |
if (self->write_func(self, c_str, n) >= 0) |
| 2303 |
res = 0; |
| 2304 |
goto finally; |
| 2305 |
} |
| 2306 |
|
| 2307 |
gen_global: |
| 2308 |
if (self->write_func(self, &global, 1) < 0) |
| 2309 |
goto finally; |
| 2310 |
|
| 2311 |
if (self->write_func(self, module_str, module_size) < 0) |
| 2312 |
goto finally; |
| 2313 |
|
| 2314 |
if (self->write_func(self, "\n", 1) < 0) |
| 2315 |
goto finally; |
| 2316 |
|
| 2317 |
if (self->write_func(self, name_str, name_size) < 0) |
| 2318 |
goto finally; |
| 2319 |
|
| 2320 |
if (self->write_func(self, "\n", 1) < 0) |
| 2321 |
goto finally; |
| 2322 |
|
| 2323 |
if (put(self, args) < 0) |
| 2324 |
goto finally; |
| 2325 |
|
| 2326 |
res = 0; |
| 2327 |
|
| 2328 |
finally: |
| 2329 |
Py_XDECREF(module); |
| 2330 |
Py_XDECREF(global_name); |
| 2331 |
Py_XDECREF(mod); |
| 2332 |
|
| 2333 |
return res; |
| 2334 |
} |
| 2335 |
|
| 2336 |
static int |
| 2337 |
save_pers(Picklerobject *self, PyObject *args, PyObject *f) |
| 2338 |
{ |
| 2339 |
PyObject *pid = 0; |
| 2340 |
Py_ssize_t size; |
| 2341 |
int res = -1; |
| 2342 |
|
| 2343 |
static char persid = PERSID, binpersid = BINPERSID; |
| 2344 |
|
| 2345 |
Py_INCREF(args); |
| 2346 |
ARG_TUP(self, args); |
| 2347 |
if (self->arg) { |
| 2348 |
pid = PyObject_Call(f, self->arg, NULL); |
| 2349 |
FREE_ARG_TUP(self); |
| 2350 |
} |
| 2351 |
if (! pid) return -1; |
| 2352 |
|
| 2353 |
if (pid != Py_None) { |
| 2354 |
if (!self->bin) { |
| 2355 |
if (!PyString_Check(pid)) { |
| 2356 |
PyErr_SetString(PicklingError, |
| 2357 |
"persistent id must be string"); |
| 2358 |
goto finally; |
| 2359 |
} |
| 2360 |
|
| 2361 |
if (self->write_func(self, &persid, 1) < 0) |
| 2362 |
goto finally; |
| 2363 |
|
| 2364 |
if ((size = PyString_Size(pid)) < 0) |
| 2365 |
goto finally; |
| 2366 |
|
| 2367 |
if (self->write_func(self, |
| 2368 |
PyString_AS_STRING( |
| 2369 |
(PyStringObject *)pid), |
| 2370 |
size) < 0) |
| 2371 |
goto finally; |
| 2372 |
|
| 2373 |
if (self->write_func(self, "\n", 1) < 0) |
| 2374 |
goto finally; |
| 2375 |
|
| 2376 |
res = 1; |
| 2377 |
goto finally; |
| 2378 |
} |
| 2379 |
else if (save(self, pid, 1) >= 0) { |
| 2380 |
if (self->write_func(self, &binpersid, 1) < 0) |
| 2381 |
res = -1; |
| 2382 |
else |
| 2383 |
res = 1; |
| 2384 |
} |
| 2385 |
|
| 2386 |
goto finally; |
| 2387 |
} |
| 2388 |
|
| 2389 |
res = 0; |
| 2390 |
|
| 2391 |
finally: |
| 2392 |
Py_XDECREF(pid); |
| 2393 |
|
| 2394 |
return res; |
| 2395 |
} |
| 2396 |
|
| 2397 |
|
| 2398 |
|
| 2399 |
|
| 2400 |
static int |
| 2401 |
save_reduce(Picklerobject *self, PyObject *args, PyObject *fn, PyObject *ob) |
| 2402 |
{ |
| 2403 |
PyObject *callable; |
| 2404 |
PyObject *argtup; |
| 2405 |
PyObject *state = NULL; |
| 2406 |
PyObject *listitems = Py_None; |
| 2407 |
PyObject *dictitems = Py_None; |
| 2408 |
Py_ssize_t size; |
| 2409 |
|
| 2410 |
int use_newobj = self->proto >= 2; |
| 2411 |
|
| 2412 |
static char reduce = REDUCE; |
| 2413 |
static char build = BUILD; |
| 2414 |
static char newobj = NEWOBJ; |
| 2415 |
|
| 2416 |
size = PyTuple_Size(args); |
| 2417 |
if (size < 2 || size > 5) { |
| 2418 |
cPickle_ErrFormat(PicklingError, "tuple returned by " |
| 2419 |
"%s must contain 2 through 5 elements", |
| 2420 |
"O", fn); |
| 2421 |
return -1; |
| 2422 |
} |
| 2423 |
|
| 2424 |
if (! PyArg_UnpackTuple(args, "save_reduce", 2, 5, |
| 2425 |
&callable, |
| 2426 |
&argtup, |
| 2427 |
&state, |
| 2428 |
&listitems, |
| 2429 |
&dictitems)) |
| 2430 |
return -1; |
| 2431 |
|
| 2432 |
if (!PyTuple_Check(argtup)) { |
| 2433 |
cPickle_ErrFormat(PicklingError, "Second element of " |
| 2434 |
"tuple returned by %s must be a tuple", |
| 2435 |
"O", fn); |
| 2436 |
return -1; |
| 2437 |
} |
| 2438 |
|
| 2439 |
if (state == Py_None) |
| 2440 |
state = NULL; |
| 2441 |
|
| 2442 |
if (listitems == Py_None) |
| 2443 |
listitems = NULL; |
| 2444 |
else if (!PyIter_Check(listitems)) { |
| 2445 |
cPickle_ErrFormat(PicklingError, "Fourth element of " |
| 2446 |
"tuple returned by %s must be an iterator, not %s", |
| 2447 |
"Os", fn, Py_TYPE(listitems)->tp_name); |
| 2448 |
return -1; |
| 2449 |
} |
| 2450 |
|
| 2451 |
if (dictitems == Py_None) |
| 2452 |
dictitems = NULL; |
| 2453 |
else if (!PyIter_Check(dictitems)) { |
| 2454 |
cPickle_ErrFormat(PicklingError, "Fifth element of " |
| 2455 |
"tuple returned by %s must be an iterator, not %s", |
| 2456 |
"Os", fn, Py_TYPE(dictitems)->tp_name); |
| 2457 |
return -1; |
| 2458 |
} |
| 2459 |
|
| 2460 |
|
| 2461 |
|
| 2462 |
|
| 2463 |
if (use_newobj) { |
| 2464 |
PyObject *temp = PyObject_GetAttr(callable, __name___str); |
| 2465 |
|
| 2466 |
if (temp == NULL) { |
| 2467 |
if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 2468 |
PyErr_Clear(); |
| 2469 |
else |
| 2470 |
return -1; |
| 2471 |
use_newobj = 0; |
| 2472 |
} |
| 2473 |
else { |
| 2474 |
use_newobj = PyString_Check(temp) && |
| 2475 |
strcmp(PyString_AS_STRING(temp), |
| 2476 |
"__newobj__") == 0; |
| 2477 |
Py_DECREF(temp); |
| 2478 |
} |
| 2479 |
} |
| 2480 |
if (use_newobj) { |
| 2481 |
PyObject *cls; |
| 2482 |
PyObject *newargtup; |
| 2483 |
Py_ssize_t n, i; |
| 2484 |
|
| 2485 |
|
| 2486 |
n = PyTuple_Size(argtup); |
| 2487 |
if (n < 1) { |
| 2488 |
PyErr_SetString(PicklingError, "__newobj__ arglist " |
| 2489 |
"is empty"); |
| 2490 |
return -1; |
| 2491 |
} |
| 2492 |
|
| 2493 |
cls = PyTuple_GET_ITEM(argtup, 0); |
| 2494 |
if (! PyObject_HasAttrString(cls, "__new__")) { |
| 2495 |
PyErr_SetString(PicklingError, "args[0] from " |
| 2496 |
"__newobj__ args has no __new__"); |
| 2497 |
return -1; |
| 2498 |
} |
| 2499 |
|
| 2500 |
|
| 2501 |
if (ob != NULL) { |
| 2502 |
PyObject *ob_dot_class; |
| 2503 |
|
| 2504 |
ob_dot_class = PyObject_GetAttr(ob, __class___str); |
| 2505 |
if (ob_dot_class == NULL) { |
| 2506 |
if (PyErr_ExceptionMatches( |
| 2507 |
PyExc_AttributeError)) |
| 2508 |
PyErr_Clear(); |
| 2509 |
else |
| 2510 |
return -1; |
| 2511 |
} |
| 2512 |
i = ob_dot_class != cls; |
| 2513 |
Py_XDECREF(ob_dot_class); |
| 2514 |
if (i) { |
| 2515 |
PyErr_SetString(PicklingError, "args[0] from " |
| 2516 |
"__newobj__ args has the wrong class"); |
| 2517 |
return -1; |
| 2518 |
} |
| 2519 |
} |
| 2520 |
|
| 2521 |
|
| 2522 |
if (save(self, cls, 0) < 0) |
| 2523 |
return -1; |
| 2524 |
|
| 2525 |
newargtup = PyTuple_New(n-1); |
| 2526 |
if (newargtup == NULL) |
| 2527 |
return -1; |
| 2528 |
for (i = 1; i < n; ++i) { |
| 2529 |
PyObject *temp = PyTuple_GET_ITEM(argtup, i); |
| 2530 |
Py_INCREF(temp); |
| 2531 |
PyTuple_SET_ITEM(newargtup, i-1, temp); |
| 2532 |
} |
| 2533 |
i = save(self, newargtup, 0); |
| 2534 |
Py_DECREF(newargtup); |
| 2535 |
if (i < 0) |
| 2536 |
return -1; |
| 2537 |
|
| 2538 |
|
| 2539 |
if (self->write_func(self, &newobj, 1) < 0) |
| 2540 |
return -1; |
| 2541 |
} |
| 2542 |
else { |
| 2543 |
|
| 2544 |
if (save(self, callable, 0) < 0 || |
| 2545 |
save(self, argtup, 0) < 0 || |
| 2546 |
self->write_func(self, &reduce, 1) < 0) |
| 2547 |
return -1; |
| 2548 |
} |
| 2549 |
|
| 2550 |
|
| 2551 |
|
| 2552 |
if (ob != NULL) { |
| 2553 |
|
| 2554 |
|
| 2555 |
|
| 2556 |
if (Py_REFCNT(ob) > 1 && !self->fast) { |
| 2557 |
PyObject *py_ob_id = PyLong_FromVoidPtr(ob); |
| 2558 |
if (!py_ob_id) |
| 2559 |
return -1; |
| 2560 |
if (PyDict_GetItem(self->memo, py_ob_id)) { |
| 2561 |
const char pop_op = POP; |
| 2562 |
if (self->write_func(self, &pop_op, 1) < 0 || |
| 2563 |
get(self, py_ob_id) < 0) { |
| 2564 |
Py_DECREF(py_ob_id); |
| 2565 |
return -1; |
| 2566 |
} |
| 2567 |
Py_DECREF(py_ob_id); |
| 2568 |
return 0; |
| 2569 |
} |
| 2570 |
Py_DECREF(py_ob_id); |
| 2571 |
if (PyErr_Occurred()) |
| 2572 |
return -1; |
| 2573 |
} |
| 2574 |
if (state && !PyDict_Check(state)) { |
| 2575 |
if (put2(self, ob) < 0) |
| 2576 |
return -1; |
| 2577 |
} |
| 2578 |
else if (put(self, ob) < 0) |
| 2579 |
return -1; |
| 2580 |
} |
| 2581 |
|
| 2582 |
|
| 2583 |
if (listitems && batch_list(self, listitems) < 0) |
| 2584 |
return -1; |
| 2585 |
|
| 2586 |
if (dictitems && batch_dict(self, dictitems) < 0) |
| 2587 |
return -1; |
| 2588 |
|
| 2589 |
if (state) { |
| 2590 |
if (save(self, state, 0) < 0 || |
| 2591 |
self->write_func(self, &build, 1) < 0) |
| 2592 |
return -1; |
| 2593 |
} |
| 2594 |
|
| 2595 |
return 0; |
| 2596 |
} |
| 2597 |
|
| 2598 |
static int |
| 2599 |
save(Picklerobject *self, PyObject *args, int pers_save) |
| 2600 |
{ |
| 2601 |
PyTypeObject *type; |
| 2602 |
PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0; |
| 2603 |
int res = -1; |
| 2604 |
int tmp; |
| 2605 |
|
| 2606 |
if (Py_EnterRecursiveCall(" while pickling an object")) |
| 2607 |
return -1; |
| 2608 |
|
| 2609 |
if (!pers_save && self->pers_func) { |
| 2610 |
if ((tmp = save_pers(self, args, self->pers_func)) != 0) { |
| 2611 |
res = tmp; |
| 2612 |
goto finally; |
| 2613 |
} |
| 2614 |
} |
| 2615 |
|
| 2616 |
if (args == Py_None) { |
| 2617 |
res = save_none(self, args); |
| 2618 |
goto finally; |
| 2619 |
} |
| 2620 |
|
| 2621 |
type = Py_TYPE(args); |
| 2622 |
|
| 2623 |
switch (type->tp_name[0]) { |
| 2624 |
case 'b': |
| 2625 |
if (args == Py_False || args == Py_True) { |
| 2626 |
res = save_bool(self, args); |
| 2627 |
goto finally; |
| 2628 |
} |
| 2629 |
break; |
| 2630 |
case 'i': |
| 2631 |
if (type == &PyInt_Type) { |
| 2632 |
res = save_int(self, args); |
| 2633 |
goto finally; |
| 2634 |
} |
| 2635 |
break; |
| 2636 |
|
| 2637 |
case 'l': |
| 2638 |
if (type == &PyLong_Type) { |
| 2639 |
res = save_long(self, args); |
| 2640 |
goto finally; |
| 2641 |
} |
| 2642 |
break; |
| 2643 |
|
| 2644 |
case 'f': |
| 2645 |
if (type == &PyFloat_Type) { |
| 2646 |
res = save_float(self, args); |
| 2647 |
goto finally; |
| 2648 |
} |
| 2649 |
break; |
| 2650 |
|
| 2651 |
case 't': |
| 2652 |
if (type == &PyTuple_Type && PyTuple_Size(args) == 0) { |
| 2653 |
res = save_tuple(self, args); |
| 2654 |
goto finally; |
| 2655 |
} |
| 2656 |
break; |
| 2657 |
|
| 2658 |
case 's': |
| 2659 |
if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) { |
| 2660 |
res = save_string(self, args, 0); |
| 2661 |
goto finally; |
| 2662 |
} |
| 2663 |
break; |
| 2664 |
|
| 2665 |
#ifdef Py_USING_UNICODE |
| 2666 |
case 'u': |
| 2667 |
if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) { |
| 2668 |
res = save_unicode(self, args, 0); |
| 2669 |
goto finally; |
| 2670 |
} |
| 2671 |
break; |
| 2672 |
#endif |
| 2673 |
} |
| 2674 |
|
| 2675 |
if (Py_REFCNT(args) > 1) { |
| 2676 |
if (!( py_ob_id = PyLong_FromVoidPtr(args))) |
| 2677 |
goto finally; |
| 2678 |
|
| 2679 |
if (PyDict_GetItem(self->memo, py_ob_id)) { |
| 2680 |
if (get(self, py_ob_id) < 0) |
| 2681 |
goto finally; |
| 2682 |
|
| 2683 |
res = 0; |
| 2684 |
goto finally; |
| 2685 |
} |
| 2686 |
} |
| 2687 |
|
| 2688 |
switch (type->tp_name[0]) { |
| 2689 |
case 's': |
| 2690 |
if (type == &PyString_Type) { |
| 2691 |
res = save_string(self, args, 1); |
| 2692 |
goto finally; |
| 2693 |
} |
| 2694 |
break; |
| 2695 |
|
| 2696 |
#ifdef Py_USING_UNICODE |
| 2697 |
case 'u': |
| 2698 |
if (type == &PyUnicode_Type) { |
| 2699 |
res = save_unicode(self, args, 1); |
| 2700 |
goto finally; |
| 2701 |
} |
| 2702 |
break; |
| 2703 |
#endif |
| 2704 |
|
| 2705 |
case 't': |
| 2706 |
if (type == &PyTuple_Type) { |
| 2707 |
res = save_tuple(self, args); |
| 2708 |
goto finally; |
| 2709 |
} |
| 2710 |
if (type == &PyType_Type) { |
| 2711 |
res = save_global(self, args, NULL); |
| 2712 |
goto finally; |
| 2713 |
} |
| 2714 |
break; |
| 2715 |
|
| 2716 |
case 'l': |
| 2717 |
if (type == &PyList_Type) { |
| 2718 |
res = save_list(self, args); |
| 2719 |
goto finally; |
| 2720 |
} |
| 2721 |
break; |
| 2722 |
|
| 2723 |
case 'd': |
| 2724 |
if (type == &PyDict_Type) { |
| 2725 |
res = save_dict(self, args); |
| 2726 |
goto finally; |
| 2727 |
} |
| 2728 |
break; |
| 2729 |
|
| 2730 |
case 'i': |
| 2731 |
if (type == &PyInstance_Type) { |
| 2732 |
res = save_inst(self, args); |
| 2733 |
goto finally; |
| 2734 |
} |
| 2735 |
break; |
| 2736 |
|
| 2737 |
case 'c': |
| 2738 |
if (type == &PyClass_Type) { |
| 2739 |
res = save_global(self, args, NULL); |
| 2740 |
goto finally; |
| 2741 |
} |
| 2742 |
break; |
| 2743 |
|
| 2744 |
case 'f': |
| 2745 |
if (type == &PyFunction_Type) { |
| 2746 |
res = save_global(self, args, NULL); |
| 2747 |
if (res && PyErr_ExceptionMatches(PickleError)) { |
| 2748 |
|
| 2749 |
PyErr_Clear(); |
| 2750 |
break; |
| 2751 |
} |
| 2752 |
goto finally; |
| 2753 |
} |
| 2754 |
break; |
| 2755 |
|
| 2756 |
case 'b': |
| 2757 |
if (type == &PyCFunction_Type) { |
| 2758 |
res = save_global(self, args, NULL); |
| 2759 |
goto finally; |
| 2760 |
} |
| 2761 |
} |
| 2762 |
|
| 2763 |
if (!pers_save && self->inst_pers_func) { |
| 2764 |
if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) { |
| 2765 |
res = tmp; |
| 2766 |
goto finally; |
| 2767 |
} |
| 2768 |
} |
| 2769 |
|
| 2770 |
|
| 2771 |
|
| 2772 |
|
| 2773 |
|
| 2774 |
__reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type); |
| 2775 |
if (__reduce__ != NULL) { |
| 2776 |
Py_INCREF(__reduce__); |
| 2777 |
Py_INCREF(args); |
| 2778 |
ARG_TUP(self, args); |
| 2779 |
if (self->arg) { |
| 2780 |
t = PyObject_Call(__reduce__, self->arg, NULL); |
| 2781 |
FREE_ARG_TUP(self); |
| 2782 |
} |
| 2783 |
} |
| 2784 |
else { |
| 2785 |
if (PyType_IsSubtype(type, &PyType_Type)) { |
| 2786 |
res = save_global(self, args, NULL); |
| 2787 |
goto finally; |
| 2788 |
} |
| 2789 |
|
| 2790 |
|
| 2791 |
__reduce__ = PyObject_GetAttr(args, __reduce_ex___str); |
| 2792 |
if (__reduce__ != NULL) { |
| 2793 |
t = PyInt_FromLong(self->proto); |
| 2794 |
if (t != NULL) { |
| 2795 |
ARG_TUP(self, t); |
| 2796 |
t = NULL; |
| 2797 |
if (self->arg) { |
| 2798 |
t = PyObject_Call(__reduce__, |
| 2799 |
self->arg, NULL); |
| 2800 |
FREE_ARG_TUP(self); |
| 2801 |
} |
| 2802 |
} |
| 2803 |
} |
| 2804 |
else { |
| 2805 |
if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 2806 |
PyErr_Clear(); |
| 2807 |
else |
| 2808 |
goto finally; |
| 2809 |
|
| 2810 |
__reduce__ = PyObject_GetAttr(args, __reduce___str); |
| 2811 |
if (__reduce__ != NULL) { |
| 2812 |
t = PyObject_Call(__reduce__, |
| 2813 |
empty_tuple, NULL); |
| 2814 |
} |
| 2815 |
else { |
| 2816 |
PyErr_SetObject(UnpickleableError, args); |
| 2817 |
goto finally; |
| 2818 |
} |
| 2819 |
} |
| 2820 |
} |
| 2821 |
|
| 2822 |
if (t == NULL) |
| 2823 |
goto finally; |
| 2824 |
|
| 2825 |
if (PyString_Check(t)) { |
| 2826 |
res = save_global(self, args, t); |
| 2827 |
goto finally; |
| 2828 |
} |
| 2829 |
|
| 2830 |
if (!PyTuple_Check(t)) { |
| 2831 |
cPickle_ErrFormat(PicklingError, "Value returned by " |
| 2832 |
"%s must be string or tuple", |
| 2833 |
"O", __reduce__); |
| 2834 |
goto finally; |
| 2835 |
} |
| 2836 |
|
| 2837 |
res = save_reduce(self, t, __reduce__, args); |
| 2838 |
|
| 2839 |
finally: |
| 2840 |
Py_LeaveRecursiveCall(); |
| 2841 |
Py_XDECREF(py_ob_id); |
| 2842 |
Py_XDECREF(__reduce__); |
| 2843 |
Py_XDECREF(t); |
| 2844 |
|
| 2845 |
return res; |
| 2846 |
} |
| 2847 |
|
| 2848 |
|
| 2849 |
static int |
| 2850 |
dump(Picklerobject *self, PyObject *args) |
| 2851 |
{ |
| 2852 |
static char stop = STOP; |
| 2853 |
|
| 2854 |
if (self->proto >= 2) { |
| 2855 |
char bytes[2]; |
| 2856 |
|
| 2857 |
bytes[0] = PROTO; |
| 2858 |
assert(self->proto >= 0 && self->proto < 256); |
| 2859 |
bytes[1] = (char)self->proto; |
| 2860 |
if (self->write_func(self, bytes, 2) < 0) |
| 2861 |
return -1; |
| 2862 |
} |
| 2863 |
|
| 2864 |
if (save(self, args, 0) < 0) |
| 2865 |
return -1; |
| 2866 |
|
| 2867 |
if (self->write_func(self, &stop, 1) < 0) |
| 2868 |
return -1; |
| 2869 |
|
| 2870 |
if (self->write_func(self, NULL, 0) < 0) |
| 2871 |
return -1; |
| 2872 |
|
| 2873 |
return 0; |
| 2874 |
} |
| 2875 |
|
| 2876 |
static PyObject * |
| 2877 |
Pickle_clear_memo(Picklerobject *self, PyObject *args) |
| 2878 |
{ |
| 2879 |
if (self->memo) |
| 2880 |
PyDict_Clear(self->memo); |
| 2881 |
Py_INCREF(Py_None); |
| 2882 |
return Py_None; |
| 2883 |
} |
| 2884 |
|
| 2885 |
static PyObject * |
| 2886 |
Pickle_getvalue(Picklerobject *self, PyObject *args) |
| 2887 |
{ |
| 2888 |
Py_ssize_t l, i, rsize, ssize, clear=1, lm; |
| 2889 |
long ik; |
| 2890 |
PyObject *k, *r; |
| 2891 |
char *s, *p, *have_get; |
| 2892 |
Pdata *data; |
| 2893 |
|
| 2894 |
|
| 2895 |
if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear)) |
| 2896 |
return NULL; |
| 2897 |
|
| 2898 |
|
| 2899 |
if (! Pdata_Check(self->file)) { |
| 2900 |
PyErr_SetString(PicklingError, |
| 2901 |
"Attempt to getvalue() a non-list-based pickler"); |
| 2902 |
return NULL; |
| 2903 |
} |
| 2904 |
|
| 2905 |
|
| 2906 |
if (write_other(self, NULL, 0) < 0) return NULL; |
| 2907 |
|
| 2908 |
data=(Pdata*)self->file; |
| 2909 |
l=data->length; |
| 2910 |
|
| 2911 |
|
| 2912 |
lm = PyDict_Size(self->memo); |
| 2913 |
if (lm < 0) return NULL; |
| 2914 |
lm++; |
| 2915 |
have_get = malloc(lm); |
| 2916 |
if (have_get == NULL) return PyErr_NoMemory(); |
| 2917 |
memset(have_get, 0, lm); |
| 2918 |
|
| 2919 |
|
| 2920 |
for (rsize = 0, i = l; --i >= 0; ) { |
| 2921 |
k = data->data[i]; |
| 2922 |
|
| 2923 |
if (PyString_Check(k)) |
| 2924 |
rsize += PyString_GET_SIZE(k); |
| 2925 |
|
| 2926 |
else if (PyInt_Check(k)) { |
| 2927 |
ik = PyInt_AS_LONG((PyIntObject*)k); |
| 2928 |
if (ik >= lm || ik == 0) { |
| 2929 |
PyErr_SetString(PicklingError, |
| 2930 |
"Invalid get data"); |
| 2931 |
goto err; |
| 2932 |
} |
| 2933 |
if (have_get[ik]) |
| 2934 |
rsize += ik < 256 ? 2 : 5; |
| 2935 |
} |
| 2936 |
|
| 2937 |
else if (! (PyTuple_Check(k) && |
| 2938 |
PyTuple_GET_SIZE(k) == 2 && |
| 2939 |
PyInt_Check((k = PyTuple_GET_ITEM(k, 0)))) |
| 2940 |
) { |
| 2941 |
PyErr_SetString(PicklingError, |
| 2942 |
"Unexpected data in internal list"); |
| 2943 |
goto err; |
| 2944 |
} |
| 2945 |
|
| 2946 |
else { |
| 2947 |
ik = PyInt_AS_LONG((PyIntObject *)k); |
| 2948 |
if (ik >= lm || ik == 0) { |
| 2949 |
PyErr_SetString(PicklingError, |
| 2950 |
"Invalid get data"); |
| 2951 |
goto err; |
| 2952 |
} |
| 2953 |
have_get[ik] = 1; |
| 2954 |
rsize += ik < 256 ? 2 : 5; |
| 2955 |
} |
| 2956 |
} |
| 2957 |
|
| 2958 |
|
| 2959 |
r = PyString_FromStringAndSize(NULL, rsize); |
| 2960 |
if (r == NULL) goto err; |
| 2961 |
s = PyString_AS_STRING((PyStringObject *)r); |
| 2962 |
|
| 2963 |
for (i = 0; i < l; i++) { |
| 2964 |
k = data->data[i]; |
| 2965 |
|
| 2966 |
if (PyString_Check(k)) { |
| 2967 |
ssize = PyString_GET_SIZE(k); |
| 2968 |
if (ssize) { |
| 2969 |
p=PyString_AS_STRING((PyStringObject *)k); |
| 2970 |
while (--ssize >= 0) |
| 2971 |
*s++ = *p++; |
| 2972 |
} |
| 2973 |
} |
| 2974 |
|
| 2975 |
else if (PyTuple_Check(k)) { |
| 2976 |
ik = PyInt_AS_LONG((PyIntObject *) |
| 2977 |
PyTuple_GET_ITEM(k, 0)); |
| 2978 |
if (ik < 256) { |
| 2979 |
*s++ = BINGET; |
| 2980 |
*s++ = (int)(ik & 0xff); |
| 2981 |
} |
| 2982 |
else { |
| 2983 |
*s++ = LONG_BINGET; |
| 2984 |
*s++ = (int)(ik & 0xff); |
| 2985 |
*s++ = (int)((ik >> 8) & 0xff); |
| 2986 |
*s++ = (int)((ik >> 16) & 0xff); |
| 2987 |
*s++ = (int)((ik >> 24) & 0xff); |
| 2988 |
} |
| 2989 |
} |
| 2990 |
|
| 2991 |
else { |
| 2992 |
ik = PyInt_AS_LONG((PyIntObject*)k); |
| 2993 |
|
| 2994 |
if (have_get[ik]) { |
| 2995 |
if (ik < 256) { |
| 2996 |
*s++ = BINPUT; |
| 2997 |
*s++ = (int)(ik & 0xff); |
| 2998 |
} |
| 2999 |
else { |
| 3000 |
*s++ = LONG_BINPUT; |
| 3001 |
*s++ = (int)(ik & 0xff); |
| 3002 |
*s++ = (int)((ik >> 8) & 0xff); |
| 3003 |
*s++ = (int)((ik >> 16) & 0xff); |
| 3004 |
*s++ = (int)((ik >> 24) & 0xff); |
| 3005 |
} |
| 3006 |
} |
| 3007 |
} |
| 3008 |
} |
| 3009 |
|
| 3010 |
if (clear) { |
| 3011 |
PyDict_Clear(self->memo); |
| 3012 |
Pdata_clear(data, 0); |
| 3013 |
} |
| 3014 |
|
| 3015 |
free(have_get); |
| 3016 |
return r; |
| 3017 |
err: |
| 3018 |
free(have_get); |
| 3019 |
return NULL; |
| 3020 |
} |
| 3021 |
|
| 3022 |
static PyObject * |
| 3023 |
Pickler_dump(Picklerobject *self, PyObject *args) |
| 3024 |
{ |
| 3025 |
PyObject *ob; |
| 3026 |
int get=0; |
| 3027 |
|
| 3028 |
if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get))) |
| 3029 |
return NULL; |
| 3030 |
|
| 3031 |
if (dump(self, ob) < 0) |
| 3032 |
return NULL; |
| 3033 |
|
| 3034 |
if (get) return Pickle_getvalue(self, NULL); |
| 3035 |
|
| 3036 |
|
| 3037 |
Py_INCREF(self); |
| 3038 |
return (PyObject*)self; |
| 3039 |
} |
| 3040 |
|
| 3041 |
|
| 3042 |
static struct PyMethodDef Pickler_methods[] = |
| 3043 |
{ |
| 3044 |
{"dump", (PyCFunction)Pickler_dump, METH_VARARGS, |
| 3045 |
PyDoc_STR("dump(object) -- " |
| 3046 |
"Write an object in pickle format to the object's pickle stream")}, |
| 3047 |
{"clear_memo", (PyCFunction)Pickle_clear_memo, METH_NOARGS, |
| 3048 |
PyDoc_STR("clear_memo() -- Clear the picklers memo")}, |
| 3049 |
{"getvalue", (PyCFunction)Pickle_getvalue, METH_VARARGS, |
| 3050 |
PyDoc_STR("getvalue() -- Finish picking a list-based pickle")}, |
| 3051 |
{NULL, NULL} |
| 3052 |
}; |
| 3053 |
|
| 3054 |
|
| 3055 |
static Picklerobject * |
| 3056 |
newPicklerobject(PyObject *file, int proto) |
| 3057 |
{ |
| 3058 |
Picklerobject *self; |
| 3059 |
|
| 3060 |
if (proto < 0) |
| 3061 |
proto = HIGHEST_PROTOCOL; |
| 3062 |
if (proto > HIGHEST_PROTOCOL) { |
| 3063 |
PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; " |
| 3064 |
"the highest available protocol is %d", |
| 3065 |
proto, HIGHEST_PROTOCOL); |
| 3066 |
return NULL; |
| 3067 |
} |
| 3068 |
|
| 3069 |
self = PyObject_GC_New(Picklerobject, &Picklertype); |
| 3070 |
if (self == NULL) |
| 3071 |
return NULL; |
| 3072 |
self->proto = proto; |
| 3073 |
self->bin = proto > 0; |
| 3074 |
self->fp = NULL; |
| 3075 |
self->write = NULL; |
| 3076 |
self->memo = NULL; |
| 3077 |
self->arg = NULL; |
| 3078 |
self->pers_func = NULL; |
| 3079 |
self->inst_pers_func = NULL; |
| 3080 |
self->write_buf = NULL; |
| 3081 |
self->fast = 0; |
| 3082 |
self->fast_container = 0; |
| 3083 |
self->fast_memo = NULL; |
| 3084 |
self->buf_size = 0; |
| 3085 |
self->dispatch_table = NULL; |
| 3086 |
|
| 3087 |
self->file = NULL; |
| 3088 |
if (file) |
| 3089 |
Py_INCREF(file); |
| 3090 |
else { |
| 3091 |
file = Pdata_New(); |
| 3092 |
if (file == NULL) |
| 3093 |
goto err; |
| 3094 |
} |
| 3095 |
self->file = file; |
| 3096 |
|
| 3097 |
if (!( self->memo = PyDict_New())) |
| 3098 |
goto err; |
| 3099 |
|
| 3100 |
if (PyFile_Check(file)) { |
| 3101 |
self->fp = PyFile_AsFile(file); |
| 3102 |
if (self->fp == NULL) { |
| 3103 |
PyErr_SetString(PyExc_ValueError, |
| 3104 |
"I/O operation on closed file"); |
| 3105 |
goto err; |
| 3106 |
} |
| 3107 |
self->write_func = write_file; |
| 3108 |
} |
| 3109 |
else if (PycStringIO_OutputCheck(file)) { |
| 3110 |
self->write_func = write_cStringIO; |
| 3111 |
} |
| 3112 |
else if (file == Py_None) { |
| 3113 |
self->write_func = write_none; |
| 3114 |
} |
| 3115 |
else { |
| 3116 |
self->write_func = write_other; |
| 3117 |
|
| 3118 |
if (! Pdata_Check(file)) { |
| 3119 |
self->write = PyObject_GetAttr(file, write_str); |
| 3120 |
if (!self->write) { |
| 3121 |
PyErr_Clear(); |
| 3122 |
PyErr_SetString(PyExc_TypeError, |
| 3123 |
"argument must have 'write' " |
| 3124 |
"attribute"); |
| 3125 |
goto err; |
| 3126 |
} |
| 3127 |
} |
| 3128 |
|
| 3129 |
self->write_buf = (char *)PyMem_Malloc(WRITE_BUF_SIZE); |
| 3130 |
if (self->write_buf == NULL) { |
| 3131 |
PyErr_NoMemory(); |
| 3132 |
goto err; |
| 3133 |
} |
| 3134 |
} |
| 3135 |
|
| 3136 |
if (PyEval_GetRestricted()) { |
| 3137 |
|
| 3138 |
PyObject *m = PyImport_ImportModule("copy_reg"); |
| 3139 |
|
| 3140 |
if (m == NULL) |
| 3141 |
goto err; |
| 3142 |
self->dispatch_table = PyObject_GetAttr(m, dispatch_table_str); |
| 3143 |
Py_DECREF(m); |
| 3144 |
if (self->dispatch_table == NULL) |
| 3145 |
goto err; |
| 3146 |
} |
| 3147 |
else { |
| 3148 |
self->dispatch_table = dispatch_table; |
| 3149 |
Py_INCREF(dispatch_table); |
| 3150 |
} |
| 3151 |
PyObject_GC_Track(self); |
| 3152 |
|
| 3153 |
return self; |
| 3154 |
|
| 3155 |
err: |
| 3156 |
Py_DECREF(self); |
| 3157 |
return NULL; |
| 3158 |
} |
| 3159 |
|
| 3160 |
|
| 3161 |
static PyObject * |
| 3162 |
get_Pickler(PyObject *self, PyObject *args, PyObject *kwds) |
| 3163 |
{ |
| 3164 |
static char *kwlist[] = {"file", "protocol", NULL}; |
| 3165 |
PyObject *file = NULL; |
| 3166 |
int proto = 0; |
| 3167 |
|
| 3168 |
|
| 3169 |
|
| 3170 |
|
| 3171 |
|
| 3172 |
|
| 3173 |
|
| 3174 |
|
| 3175 |
if (!PyArg_ParseTuple(args, "|i:Pickler", &proto)) { |
| 3176 |
PyErr_Clear(); |
| 3177 |
proto = 0; |
| 3178 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|i:Pickler", |
| 3179 |
kwlist, &file, &proto)) |
| 3180 |
return NULL; |
| 3181 |
} |
| 3182 |
return (PyObject *)newPicklerobject(file, proto); |
| 3183 |
} |
| 3184 |
|
| 3185 |
|
| 3186 |
static void |
| 3187 |
Pickler_dealloc(Picklerobject *self) |
| 3188 |
{ |
| 3189 |
PyObject_GC_UnTrack(self); |
| 3190 |
Py_XDECREF(self->write); |
| 3191 |
Py_XDECREF(self->memo); |
| 3192 |
Py_XDECREF(self->fast_memo); |
| 3193 |
Py_XDECREF(self->arg); |
| 3194 |
Py_XDECREF(self->file); |
| 3195 |
Py_XDECREF(self->pers_func); |
| 3196 |
Py_XDECREF(self->inst_pers_func); |
| 3197 |
Py_XDECREF(self->dispatch_table); |
| 3198 |
PyMem_Free(self->write_buf); |
| 3199 |
Py_TYPE(self)->tp_free((PyObject *)self); |
| 3200 |
} |
| 3201 |
|
| 3202 |
static int |
| 3203 |
Pickler_traverse(Picklerobject *self, visitproc visit, void *arg) |
| 3204 |
{ |
| 3205 |
Py_VISIT(self->write); |
| 3206 |
Py_VISIT(self->memo); |
| 3207 |
Py_VISIT(self->fast_memo); |
| 3208 |
Py_VISIT(self->arg); |
| 3209 |
Py_VISIT(self->file); |
| 3210 |
Py_VISIT(self->pers_func); |
| 3211 |
Py_VISIT(self->inst_pers_func); |
| 3212 |
Py_VISIT(self->dispatch_table); |
| 3213 |
return 0; |
| 3214 |
} |
| 3215 |
|
| 3216 |
static int |
| 3217 |
Pickler_clear(Picklerobject *self) |
| 3218 |
{ |
| 3219 |
Py_CLEAR(self->write); |
| 3220 |
Py_CLEAR(self->memo); |
| 3221 |
Py_CLEAR(self->fast_memo); |
| 3222 |
Py_CLEAR(self->arg); |
| 3223 |
Py_CLEAR(self->file); |
| 3224 |
Py_CLEAR(self->pers_func); |
| 3225 |
Py_CLEAR(self->inst_pers_func); |
| 3226 |
Py_CLEAR(self->dispatch_table); |
| 3227 |
return 0; |
| 3228 |
} |
| 3229 |
|
| 3230 |
static PyObject * |
| 3231 |
Pickler_get_pers_func(Picklerobject *p) |
| 3232 |
{ |
| 3233 |
if (p->pers_func == NULL) |
| 3234 |
PyErr_SetString(PyExc_AttributeError, "persistent_id"); |
| 3235 |
else |
| 3236 |
Py_INCREF(p->pers_func); |
| 3237 |
return p->pers_func; |
| 3238 |
} |
| 3239 |
|
| 3240 |
static int |
| 3241 |
Pickler_set_pers_func(Picklerobject *p, PyObject *v) |
| 3242 |
{ |
| 3243 |
if (v == NULL) { |
| 3244 |
PyErr_SetString(PyExc_TypeError, |
| 3245 |
"attribute deletion is not supported"); |
| 3246 |
return -1; |
| 3247 |
} |
| 3248 |
Py_INCREF(v); |
| 3249 |
Py_XSETREF(p->pers_func, v); |
| 3250 |
return 0; |
| 3251 |
} |
| 3252 |
|
| 3253 |
static int |
| 3254 |
Pickler_set_inst_pers_func(Picklerobject *p, PyObject *v) |
| 3255 |
{ |
| 3256 |
if (v == NULL) { |
| 3257 |
PyErr_SetString(PyExc_TypeError, |
| 3258 |
"attribute deletion is not supported"); |
| 3259 |
return -1; |
| 3260 |
} |
| 3261 |
Py_INCREF(v); |
| 3262 |
Py_XSETREF(p->inst_pers_func, v); |
| 3263 |
return 0; |
| 3264 |
} |
| 3265 |
|
| 3266 |
static PyObject * |
| 3267 |
Pickler_get_memo(Picklerobject *p) |
| 3268 |
{ |
| 3269 |
if (p->memo == NULL) |
| 3270 |
PyErr_SetString(PyExc_AttributeError, "memo"); |
| 3271 |
else |
| 3272 |
Py_INCREF(p->memo); |
| 3273 |
return p->memo; |
| 3274 |
} |
| 3275 |
|
| 3276 |
static int |
| 3277 |
Pickler_set_memo(Picklerobject *p, PyObject *v) |
| 3278 |
{ |
| 3279 |
if (v == NULL) { |
| 3280 |
PyErr_SetString(PyExc_TypeError, |
| 3281 |
"attribute deletion is not supported"); |
| 3282 |
return -1; |
| 3283 |
} |
| 3284 |
if (!PyDict_Check(v)) { |
| 3285 |
PyErr_SetString(PyExc_TypeError, "memo must be a dictionary"); |
| 3286 |
return -1; |
| 3287 |
} |
| 3288 |
Py_INCREF(v); |
| 3289 |
Py_XSETREF(p->memo, v); |
| 3290 |
return 0; |
| 3291 |
} |
| 3292 |
|
| 3293 |
static PyObject * |
| 3294 |
Pickler_get_error(Picklerobject *p) |
| 3295 |
{ |
| 3296 |
|
| 3297 |
Py_INCREF(PicklingError); |
| 3298 |
return PicklingError; |
| 3299 |
} |
| 3300 |
|
| 3301 |
static PyMemberDef Pickler_members[] = { |
| 3302 |
{"binary", T_INT, offsetof(Picklerobject, bin)}, |
| 3303 |
{"fast", T_INT, offsetof(Picklerobject, fast)}, |
| 3304 |
{NULL} |
| 3305 |
}; |
| 3306 |
|
| 3307 |
static PyGetSetDef Pickler_getsets[] = { |
| 3308 |
{"persistent_id", (getter)Pickler_get_pers_func, |
| 3309 |
(setter)Pickler_set_pers_func}, |
| 3310 |
{"inst_persistent_id", NULL, (setter)Pickler_set_inst_pers_func}, |
| 3311 |
{"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo}, |
| 3312 |
{"PicklingError", (getter)Pickler_get_error, NULL}, |
| 3313 |
{NULL} |
| 3314 |
}; |
| 3315 |
|
| 3316 |
PyDoc_STRVAR(Picklertype__doc__, |
| 3317 |
"Objects that know how to pickle objects\n"); |
| 3318 |
|
| 3319 |
static PyTypeObject Picklertype = { |
| 3320 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 3321 |
"cPickle.Pickler", |
| 3322 |
sizeof(Picklerobject), |
| 3323 |
0, |
| 3324 |
(destructor)Pickler_dealloc, |
| 3325 |
0, |
| 3326 |
0, |
| 3327 |
0, |
| 3328 |
0, |
| 3329 |
0, |
| 3330 |
0, |
| 3331 |
0, |
| 3332 |
0, |
| 3333 |
0, |
| 3334 |
0, |
| 3335 |
0, |
| 3336 |
PyObject_GenericGetAttr, |
| 3337 |
PyObject_GenericSetAttr, |
| 3338 |
0, |
| 3339 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 3340 |
Picklertype__doc__, |
| 3341 |
(traverseproc)Pickler_traverse, |
| 3342 |
(inquiry)Pickler_clear, |
| 3343 |
0, |
| 3344 |
0, |
| 3345 |
0, |
| 3346 |
0, |
| 3347 |
Pickler_methods, |
| 3348 |
Pickler_members, |
| 3349 |
Pickler_getsets, |
| 3350 |
}; |
| 3351 |
|
| 3352 |
static PyObject * |
| 3353 |
find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc) |
| 3354 |
{ |
| 3355 |
PyObject *global = 0, *module; |
| 3356 |
|
| 3357 |
if (fc) { |
| 3358 |
if (fc==Py_None) { |
| 3359 |
PyErr_SetString(UnpicklingError, "Global and instance " |
| 3360 |
"pickles are not supported."); |
| 3361 |
return NULL; |
| 3362 |
} |
| 3363 |
return PyObject_CallFunctionObjArgs(fc, py_module_name, |
| 3364 |
py_global_name, NULL); |
| 3365 |
} |
| 3366 |
|
| 3367 |
module = PySys_GetObject("modules"); |
| 3368 |
if (module == NULL) |
| 3369 |
return NULL; |
| 3370 |
|
| 3371 |
module = PyDict_GetItem(module, py_module_name); |
| 3372 |
if (module == NULL) { |
| 3373 |
module = PyImport_Import(py_module_name); |
| 3374 |
if (!module) |
| 3375 |
return NULL; |
| 3376 |
global = PyObject_GetAttr(module, py_global_name); |
| 3377 |
Py_DECREF(module); |
| 3378 |
} |
| 3379 |
else |
| 3380 |
global = PyObject_GetAttr(module, py_global_name); |
| 3381 |
return global; |
| 3382 |
} |
| 3383 |
|
| 3384 |
static Py_ssize_t |
| 3385 |
marker(Unpicklerobject *self) |
| 3386 |
{ |
| 3387 |
if (self->num_marks < 1) { |
| 3388 |
PyErr_SetString(UnpicklingError, "could not find MARK"); |
| 3389 |
return -1; |
| 3390 |
} |
| 3391 |
|
| 3392 |
return self->marks[--self->num_marks]; |
| 3393 |
} |
| 3394 |
|
| 3395 |
|
| 3396 |
static int |
| 3397 |
load_none(Unpicklerobject *self) |
| 3398 |
{ |
| 3399 |
PDATA_APPEND(self->stack, Py_None, -1); |
| 3400 |
return 0; |
| 3401 |
} |
| 3402 |
|
| 3403 |
static int |
| 3404 |
bad_readline(void) |
| 3405 |
{ |
| 3406 |
PyErr_SetString(UnpicklingError, "pickle data was truncated"); |
| 3407 |
return -1; |
| 3408 |
} |
| 3409 |
|
| 3410 |
static int |
| 3411 |
load_int(Unpicklerobject *self) |
| 3412 |
{ |
| 3413 |
PyObject *py_int = 0; |
| 3414 |
char *endptr, *s; |
| 3415 |
Py_ssize_t len; |
| 3416 |
int res = -1; |
| 3417 |
long l; |
| 3418 |
|
| 3419 |
if ((len = self->readline_func(self, &s)) < 0) return -1; |
| 3420 |
if (len < 2) return bad_readline(); |
| 3421 |
if (!( s=pystrndup(s,len))) return -1; |
| 3422 |
|
| 3423 |
errno = 0; |
| 3424 |
l = strtol(s, &endptr, 0); |
| 3425 |
|
| 3426 |
if (errno || (*endptr != '\n') || (endptr[1] != '\0')) { |
| 3427 |
|
| 3428 |
|
| 3429 |
errno = 0; |
| 3430 |
py_int = PyLong_FromString(s, NULL, 0); |
| 3431 |
if (py_int == NULL) { |
| 3432 |
PyErr_SetString(PyExc_ValueError, |
| 3433 |
"could not convert string to int"); |
| 3434 |
goto finally; |
| 3435 |
} |
| 3436 |
} |
| 3437 |
else { |
| 3438 |
if (len == 3 && (l == 0 || l == 1)) { |
| 3439 |
if (!( py_int = PyBool_FromLong(l))) goto finally; |
| 3440 |
} |
| 3441 |
else { |
| 3442 |
if (!( py_int = PyInt_FromLong(l))) goto finally; |
| 3443 |
} |
| 3444 |
} |
| 3445 |
|
| 3446 |
free(s); |
| 3447 |
PDATA_PUSH(self->stack, py_int, -1); |
| 3448 |
return 0; |
| 3449 |
|
| 3450 |
finally: |
| 3451 |
free(s); |
| 3452 |
|
| 3453 |
return res; |
| 3454 |
} |
| 3455 |
|
| 3456 |
static int |
| 3457 |
load_bool(Unpicklerobject *self, PyObject *boolean) |
| 3458 |
{ |
| 3459 |
assert(boolean == Py_True || boolean == Py_False); |
| 3460 |
PDATA_APPEND(self->stack, boolean, -1); |
| 3461 |
return 0; |
| 3462 |
} |
| 3463 |
|
| 3464 |
|
| 3465 |
|
| 3466 |
|
| 3467 |
|
| 3468 |
|
| 3469 |
static long |
| 3470 |
calc_binint(char *s, int x) |
| 3471 |
{ |
| 3472 |
unsigned char c; |
| 3473 |
int i; |
| 3474 |
long l; |
| 3475 |
|
| 3476 |
for (i = 0, l = 0L; i < x; i++) { |
| 3477 |
c = (unsigned char)s[i]; |
| 3478 |
l |= (long)c << (i * 8); |
| 3479 |
} |
| 3480 |
#if SIZEOF_LONG > 4 |
| 3481 |
|
| 3482 |
|
| 3483 |
|
| 3484 |
|
| 3485 |
if (x == 4 && l & (1L << 31)) |
| 3486 |
l |= (~0UL) << 32; |
| 3487 |
#endif |
| 3488 |
return l; |
| 3489 |
} |
| 3490 |
|
| 3491 |
|
| 3492 |
static int |
| 3493 |
load_binintx(Unpicklerobject *self, char *s, int x) |
| 3494 |
{ |
| 3495 |
PyObject *py_int = 0; |
| 3496 |
long l; |
| 3497 |
|
| 3498 |
l = calc_binint(s, x); |
| 3499 |
|
| 3500 |
if (!( py_int = PyInt_FromLong(l))) |
| 3501 |
return -1; |
| 3502 |
|
| 3503 |
PDATA_PUSH(self->stack, py_int, -1); |
| 3504 |
return 0; |
| 3505 |
} |
| 3506 |
|
| 3507 |
|
| 3508 |
static int |
| 3509 |
load_binint(Unpicklerobject *self) |
| 3510 |
{ |
| 3511 |
char *s; |
| 3512 |
|
| 3513 |
if (self->read_func(self, &s, 4) < 0) |
| 3514 |
return -1; |
| 3515 |
|
| 3516 |
return load_binintx(self, s, 4); |
| 3517 |
} |
| 3518 |
|
| 3519 |
|
| 3520 |
static int |
| 3521 |
load_binint1(Unpicklerobject *self) |
| 3522 |
{ |
| 3523 |
char *s; |
| 3524 |
|
| 3525 |
if (self->read_func(self, &s, 1) < 0) |
| 3526 |
return -1; |
| 3527 |
|
| 3528 |
return load_binintx(self, s, 1); |
| 3529 |
} |
| 3530 |
|
| 3531 |
|
| 3532 |
static int |
| 3533 |
load_binint2(Unpicklerobject *self) |
| 3534 |
{ |
| 3535 |
char *s; |
| 3536 |
|
| 3537 |
if (self->read_func(self, &s, 2) < 0) |
| 3538 |
return -1; |
| 3539 |
|
| 3540 |
return load_binintx(self, s, 2); |
| 3541 |
} |
| 3542 |
|
| 3543 |
static int |
| 3544 |
load_long(Unpicklerobject *self) |
| 3545 |
{ |
| 3546 |
PyObject *l = 0; |
| 3547 |
char *end, *s; |
| 3548 |
Py_ssize_t len; |
| 3549 |
int res = -1; |
| 3550 |
|
| 3551 |
if ((len = self->readline_func(self, &s)) < 0) return -1; |
| 3552 |
if (len < 2) return bad_readline(); |
| 3553 |
if (!( s=pystrndup(s,len))) return -1; |
| 3554 |
|
| 3555 |
if (!( l = PyLong_FromString(s, &end, 0))) |
| 3556 |
goto finally; |
| 3557 |
|
| 3558 |
free(s); |
| 3559 |
PDATA_PUSH(self->stack, l, -1); |
| 3560 |
return 0; |
| 3561 |
|
| 3562 |
finally: |
| 3563 |
free(s); |
| 3564 |
|
| 3565 |
return res; |
| 3566 |
} |
| 3567 |
|
| 3568 |
|
| 3569 |
|
| 3570 |
|
| 3571 |
static int |
| 3572 |
load_counted_long(Unpicklerobject *self, int size) |
| 3573 |
{ |
| 3574 |
Py_ssize_t i; |
| 3575 |
char *nbytes; |
| 3576 |
unsigned char *pdata; |
| 3577 |
PyObject *along; |
| 3578 |
|
| 3579 |
assert(size == 1 || size == 4); |
| 3580 |
i = self->read_func(self, &nbytes, size); |
| 3581 |
if (i < 0) return -1; |
| 3582 |
|
| 3583 |
size = calc_binint(nbytes, size); |
| 3584 |
if (size < 0) { |
| 3585 |
|
| 3586 |
|
| 3587 |
|
| 3588 |
PyErr_SetString(UnpicklingError, "LONG pickle has negative " |
| 3589 |
"byte count"); |
| 3590 |
return -1; |
| 3591 |
} |
| 3592 |
|
| 3593 |
if (size == 0) |
| 3594 |
along = PyLong_FromLong(0L); |
| 3595 |
else { |
| 3596 |
|
| 3597 |
i = self->read_func(self, (char **)&pdata, size); |
| 3598 |
if (i < 0) return -1; |
| 3599 |
along = _PyLong_FromByteArray(pdata, (size_t)size, |
| 3600 |
1 , 1 ); |
| 3601 |
} |
| 3602 |
if (along == NULL) |
| 3603 |
return -1; |
| 3604 |
PDATA_PUSH(self->stack, along, -1); |
| 3605 |
return 0; |
| 3606 |
} |
| 3607 |
|
| 3608 |
static int |
| 3609 |
load_float(Unpicklerobject *self) |
| 3610 |
{ |
| 3611 |
PyObject *py_float = 0; |
| 3612 |
char *endptr, *s; |
| 3613 |
Py_ssize_t len; |
| 3614 |
int res = -1; |
| 3615 |
double d; |
| 3616 |
|
| 3617 |
if ((len = self->readline_func(self, &s)) < 0) return -1; |
| 3618 |
if (len < 2) return bad_readline(); |
| 3619 |
if (!( s=pystrndup(s,len))) return -1; |
| 3620 |
|
| 3621 |
d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError); |
| 3622 |
|
| 3623 |
if (d == -1.0 && PyErr_Occurred()) { |
| 3624 |
goto finally; |
| 3625 |
} else if ((endptr[0] != '\n') || (endptr[1] != '\0')) { |
| 3626 |
PyErr_SetString(PyExc_ValueError, |
| 3627 |
"could not convert string to float"); |
| 3628 |
goto finally; |
| 3629 |
} |
| 3630 |
|
| 3631 |
if (!( py_float = PyFloat_FromDouble(d))) |
| 3632 |
goto finally; |
| 3633 |
|
| 3634 |
free(s); |
| 3635 |
PDATA_PUSH(self->stack, py_float, -1); |
| 3636 |
return 0; |
| 3637 |
|
| 3638 |
finally: |
| 3639 |
free(s); |
| 3640 |
|
| 3641 |
return res; |
| 3642 |
} |
| 3643 |
|
| 3644 |
static int |
| 3645 |
load_binfloat(Unpicklerobject *self) |
| 3646 |
{ |
| 3647 |
PyObject *py_float; |
| 3648 |
double x; |
| 3649 |
char *p; |
| 3650 |
|
| 3651 |
if (self->read_func(self, &p, 8) < 0) |
| 3652 |
return -1; |
| 3653 |
|
| 3654 |
x = _PyFloat_Unpack8((unsigned char *)p, 0); |
| 3655 |
if (x == -1.0 && PyErr_Occurred()) |
| 3656 |
return -1; |
| 3657 |
|
| 3658 |
py_float = PyFloat_FromDouble(x); |
| 3659 |
if (py_float == NULL) |
| 3660 |
return -1; |
| 3661 |
|
| 3662 |
PDATA_PUSH(self->stack, py_float, -1); |
| 3663 |
return 0; |
| 3664 |
} |
| 3665 |
|
| 3666 |
static int |
| 3667 |
load_string(Unpicklerobject *self) |
| 3668 |
{ |
| 3669 |
PyObject *str = 0; |
| 3670 |
Py_ssize_t len; |
| 3671 |
int res = -1; |
| 3672 |
char *s, *p; |
| 3673 |
|
| 3674 |
if ((len = self->readline_func(self, &s)) < 0) return -1; |
| 3675 |
if (len < 2) return bad_readline(); |
| 3676 |
if (!( s=pystrndup(s,len))) return -1; |
| 3677 |
|
| 3678 |
|
| 3679 |
|
| 3680 |
while (len > 0 && s[len-1] <= ' ') |
| 3681 |
len--; |
| 3682 |
if (len > 1 && s[0]=='"' && s[len-1]=='"') { |
| 3683 |
s[len-1] = '\0'; |
| 3684 |
p = s + 1 ; |
| 3685 |
len -= 2; |
| 3686 |
} |
| 3687 |
else if (len > 1 && s[0]=='\'' && s[len-1]=='\'') { |
| 3688 |
s[len-1] = '\0'; |
| 3689 |
p = s + 1 ; |
| 3690 |
len -= 2; |
| 3691 |
} |
| 3692 |
else |
| 3693 |
goto insecure; |
| 3694 |
|
| 3695 |
|
| 3696 |
str = PyString_DecodeEscape(p, len, NULL, 0, NULL); |
| 3697 |
free(s); |
| 3698 |
if (str) { |
| 3699 |
PDATA_PUSH(self->stack, str, -1); |
| 3700 |
res = 0; |
| 3701 |
} |
| 3702 |
return res; |
| 3703 |
|
| 3704 |
insecure: |
| 3705 |
free(s); |
| 3706 |
PyErr_SetString(PyExc_ValueError,"insecure string pickle"); |
| 3707 |
return -1; |
| 3708 |
} |
| 3709 |
|
| 3710 |
|
| 3711 |
static int |
| 3712 |
load_binstring(Unpicklerobject *self) |
| 3713 |
{ |
| 3714 |
PyObject *py_string = 0; |
| 3715 |
Py_ssize_t l; |
| 3716 |
char *s; |
| 3717 |
|
| 3718 |
if (self->read_func(self, &s, 4) < 0) return -1; |
| 3719 |
|
| 3720 |
l = calc_binint(s, 4); |
| 3721 |
if (l < 0) { |
| 3722 |
|
| 3723 |
|
| 3724 |
|
| 3725 |
PyErr_SetString(UnpicklingError, |
| 3726 |
"BINSTRING pickle has negative byte count"); |
| 3727 |
return -1; |
| 3728 |
} |
| 3729 |
|
| 3730 |
if (self->read_func(self, &s, l) < 0) |
| 3731 |
return -1; |
| 3732 |
|
| 3733 |
if (!( py_string = PyString_FromStringAndSize(s, l))) |
| 3734 |
return -1; |
| 3735 |
|
| 3736 |
PDATA_PUSH(self->stack, py_string, -1); |
| 3737 |
return 0; |
| 3738 |
} |
| 3739 |
|
| 3740 |
|
| 3741 |
static int |
| 3742 |
load_short_binstring(Unpicklerobject *self) |
| 3743 |
{ |
| 3744 |
PyObject *py_string = 0; |
| 3745 |
unsigned char l; |
| 3746 |
char *s; |
| 3747 |
|
| 3748 |
if (self->read_func(self, &s, 1) < 0) |
| 3749 |
return -1; |
| 3750 |
|
| 3751 |
l = (unsigned char)s[0]; |
| 3752 |
|
| 3753 |
if (self->read_func(self, &s, l) < 0) return -1; |
| 3754 |
|
| 3755 |
if (!( py_string = PyString_FromStringAndSize(s, l))) return -1; |
| 3756 |
|
| 3757 |
PDATA_PUSH(self->stack, py_string, -1); |
| 3758 |
return 0; |
| 3759 |
} |
| 3760 |
|
| 3761 |
|
| 3762 |
#ifdef Py_USING_UNICODE |
| 3763 |
static int |
| 3764 |
load_unicode(Unpicklerobject *self) |
| 3765 |
{ |
| 3766 |
PyObject *str = 0; |
| 3767 |
Py_ssize_t len; |
| 3768 |
char *s; |
| 3769 |
|
| 3770 |
if ((len = self->readline_func(self, &s)) < 0) return -1; |
| 3771 |
if (len < 1) return bad_readline(); |
| 3772 |
|
| 3773 |
if (!( str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL))) |
| 3774 |
return -1; |
| 3775 |
|
| 3776 |
PDATA_PUSH(self->stack, str, -1); |
| 3777 |
return 0; |
| 3778 |
} |
| 3779 |
#endif |
| 3780 |
|
| 3781 |
|
| 3782 |
#ifdef Py_USING_UNICODE |
| 3783 |
static int |
| 3784 |
load_binunicode(Unpicklerobject *self) |
| 3785 |
{ |
| 3786 |
PyObject *unicode; |
| 3787 |
Py_ssize_t l; |
| 3788 |
char *s; |
| 3789 |
|
| 3790 |
if (self->read_func(self, &s, 4) < 0) return -1; |
| 3791 |
|
| 3792 |
l = calc_binint(s, 4); |
| 3793 |
if (l < 0) { |
| 3794 |
|
| 3795 |
|
| 3796 |
|
| 3797 |
PyErr_SetString(UnpicklingError, |
| 3798 |
"BINUNICODE pickle has negative byte count"); |
| 3799 |
return -1; |
| 3800 |
} |
| 3801 |
|
| 3802 |
if (self->read_func(self, &s, l) < 0) |
| 3803 |
return -1; |
| 3804 |
|
| 3805 |
if (!( unicode = PyUnicode_DecodeUTF8(s, l, NULL))) |
| 3806 |
return -1; |
| 3807 |
|
| 3808 |
PDATA_PUSH(self->stack, unicode, -1); |
| 3809 |
return 0; |
| 3810 |
} |
| 3811 |
#endif |
| 3812 |
|
| 3813 |
|
| 3814 |
static int |
| 3815 |
load_counted_tuple(Unpicklerobject *self, int len) |
| 3816 |
{ |
| 3817 |
PyObject *tup; |
| 3818 |
|
| 3819 |
if (self->stack->length < len) |
| 3820 |
return stackUnderflow(); |
| 3821 |
|
| 3822 |
if (!(tup = Pdata_popTuple(self->stack, self->stack->length - len))) |
| 3823 |
return -1; |
| 3824 |
PDATA_PUSH(self->stack, tup, -1); |
| 3825 |
return 0; |
| 3826 |
} |
| 3827 |
|
| 3828 |
static int |
| 3829 |
load_tuple(Unpicklerobject *self) |
| 3830 |
{ |
| 3831 |
Py_ssize_t i; |
| 3832 |
|
| 3833 |
if ((i = marker(self)) < 0) return -1; |
| 3834 |
return load_counted_tuple(self, self->stack->length - i); |
| 3835 |
} |
| 3836 |
|
| 3837 |
static int |
| 3838 |
load_empty_list(Unpicklerobject *self) |
| 3839 |
{ |
| 3840 |
PyObject *list; |
| 3841 |
|
| 3842 |
if (!( list=PyList_New(0))) return -1; |
| 3843 |
PDATA_PUSH(self->stack, list, -1); |
| 3844 |
return 0; |
| 3845 |
} |
| 3846 |
|
| 3847 |
static int |
| 3848 |
load_empty_dict(Unpicklerobject *self) |
| 3849 |
{ |
| 3850 |
PyObject *dict; |
| 3851 |
|
| 3852 |
if (!( dict=PyDict_New())) return -1; |
| 3853 |
PDATA_PUSH(self->stack, dict, -1); |
| 3854 |
return 0; |
| 3855 |
} |
| 3856 |
|
| 3857 |
|
| 3858 |
static int |
| 3859 |
load_list(Unpicklerobject *self) |
| 3860 |
{ |
| 3861 |
PyObject *list = 0; |
| 3862 |
Py_ssize_t i; |
| 3863 |
|
| 3864 |
if ((i = marker(self)) < 0) return -1; |
| 3865 |
if (!( list=Pdata_popList(self->stack, i))) return -1; |
| 3866 |
PDATA_PUSH(self->stack, list, -1); |
| 3867 |
return 0; |
| 3868 |
} |
| 3869 |
|
| 3870 |
static int |
| 3871 |
load_dict(Unpicklerobject *self) |
| 3872 |
{ |
| 3873 |
PyObject *dict, *key, *value; |
| 3874 |
Py_ssize_t i, j, k; |
| 3875 |
|
| 3876 |
if ((i = marker(self)) < 0) return -1; |
| 3877 |
j=self->stack->length; |
| 3878 |
|
| 3879 |
if (!( dict = PyDict_New())) return -1; |
| 3880 |
|
| 3881 |
for (k = i+1; k < j; k += 2) { |
| 3882 |
key =self->stack->data[k-1]; |
| 3883 |
value=self->stack->data[k ]; |
| 3884 |
if (PyDict_SetItem(dict, key, value) < 0) { |
| 3885 |
Py_DECREF(dict); |
| 3886 |
return -1; |
| 3887 |
} |
| 3888 |
} |
| 3889 |
Pdata_clear(self->stack, i); |
| 3890 |
PDATA_PUSH(self->stack, dict, -1); |
| 3891 |
return 0; |
| 3892 |
} |
| 3893 |
|
| 3894 |
static PyObject * |
| 3895 |
Instance_New(PyObject *cls, PyObject *args) |
| 3896 |
{ |
| 3897 |
if (PyClass_Check(cls)) { |
| 3898 |
int l; |
| 3899 |
|
| 3900 |
if ((l=PyObject_Size(args)) < 0) return NULL; |
| 3901 |
if (!( l )) { |
| 3902 |
if (!PyObject_HasAttr(cls, __getinitargs___str)) { |
| 3903 |
|
| 3904 |
|
| 3905 |
return PyInstance_NewRaw(cls, NULL); |
| 3906 |
} |
| 3907 |
} |
| 3908 |
|
| 3909 |
return PyInstance_New(cls, args, NULL); |
| 3910 |
} |
| 3911 |
|
| 3912 |
return PyObject_CallObject(cls, args); |
| 3913 |
} |
| 3914 |
|
| 3915 |
|
| 3916 |
static int |
| 3917 |
load_obj(Unpicklerobject *self) |
| 3918 |
{ |
| 3919 |
PyObject *class, *tup, *obj=0; |
| 3920 |
Py_ssize_t i; |
| 3921 |
|
| 3922 |
if ((i = marker(self)) < 0) return -1; |
| 3923 |
|
| 3924 |
if (self->stack->length - i < 1) |
| 3925 |
return stackUnderflow(); |
| 3926 |
|
| 3927 |
if (!( tup=Pdata_popTuple(self->stack, i+1))) return -1; |
| 3928 |
PDATA_POP(self->stack, class); |
| 3929 |
if (class) { |
| 3930 |
obj = Instance_New(class, tup); |
| 3931 |
Py_DECREF(class); |
| 3932 |
} |
| 3933 |
Py_DECREF(tup); |
| 3934 |
|
| 3935 |
if (! obj) return -1; |
| 3936 |
PDATA_PUSH(self->stack, obj, -1); |
| 3937 |
return 0; |
| 3938 |
} |
| 3939 |
|
| 3940 |
|
| 3941 |
static int |
| 3942 |
load_inst(Unpicklerobject *self) |
| 3943 |
{ |
| 3944 |
PyObject *tup, *class=0, *obj=0, *module_name, *class_name; |
| 3945 |
Py_ssize_t i, len; |
| 3946 |
char *s; |
| 3947 |
|
| 3948 |
if ((i = marker(self)) < 0) return -1; |
| 3949 |
|
| 3950 |
if ((len = self->readline_func(self, &s)) < 0) return -1; |
| 3951 |
if (len < 2) return bad_readline(); |
| 3952 |
module_name = PyString_FromStringAndSize(s, len - 1); |
| 3953 |
if (!module_name) return -1; |
| 3954 |
|
| 3955 |
if ((len = self->readline_func(self, &s)) >= 0) { |
| 3956 |
if (len < 2) { |
| 3957 |
Py_DECREF(module_name); |
| 3958 |
return bad_readline(); |
| 3959 |
} |
| 3960 |
if ((class_name = PyString_FromStringAndSize(s, len - 1))) { |
| 3961 |
class = find_class(module_name, class_name, |
| 3962 |
self->find_class); |
| 3963 |
Py_DECREF(class_name); |
| 3964 |
} |
| 3965 |
} |
| 3966 |
Py_DECREF(module_name); |
| 3967 |
|
| 3968 |
if (! class) return -1; |
| 3969 |
|
| 3970 |
if ((tup=Pdata_popTuple(self->stack, i))) { |
| 3971 |
obj = Instance_New(class, tup); |
| 3972 |
Py_DECREF(tup); |
| 3973 |
} |
| 3974 |
Py_DECREF(class); |
| 3975 |
|
| 3976 |
if (! obj) return -1; |
| 3977 |
|
| 3978 |
PDATA_PUSH(self->stack, obj, -1); |
| 3979 |
return 0; |
| 3980 |
} |
| 3981 |
|
| 3982 |
static int |
| 3983 |
load_newobj(Unpicklerobject *self) |
| 3984 |
{ |
| 3985 |
PyObject *args = NULL; |
| 3986 |
PyObject *clsraw = NULL; |
| 3987 |
PyTypeObject *cls; |
| 3988 |
PyObject *obj; |
| 3989 |
|
| 3990 |
|
| 3991 |
|
| 3992 |
|
| 3993 |
PDATA_POP(self->stack, args); |
| 3994 |
if (args == NULL) goto Fail; |
| 3995 |
if (! PyTuple_Check(args)) { |
| 3996 |
PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg " |
| 3997 |
"tuple."); |
| 3998 |
goto Fail; |
| 3999 |
} |
| 4000 |
|
| 4001 |
PDATA_POP(self->stack, clsraw); |
| 4002 |
cls = (PyTypeObject *)clsraw; |
| 4003 |
if (cls == NULL) goto Fail; |
| 4004 |
if (! PyType_Check(cls)) { |
| 4005 |
PyErr_SetString(UnpicklingError, "NEWOBJ class argument " |
| 4006 |
"isn't a type object"); |
| 4007 |
goto Fail; |
| 4008 |
} |
| 4009 |
if (cls->tp_new == NULL) { |
| 4010 |
PyErr_SetString(UnpicklingError, "NEWOBJ class argument " |
| 4011 |
"has NULL tp_new"); |
| 4012 |
goto Fail; |
| 4013 |
} |
| 4014 |
|
| 4015 |
|
| 4016 |
obj = cls->tp_new(cls, args, NULL); |
| 4017 |
if (obj == NULL) goto Fail; |
| 4018 |
|
| 4019 |
Py_DECREF(args); |
| 4020 |
Py_DECREF(clsraw); |
| 4021 |
PDATA_PUSH(self->stack, obj, -1); |
| 4022 |
return 0; |
| 4023 |
|
| 4024 |
Fail: |
| 4025 |
Py_XDECREF(args); |
| 4026 |
Py_XDECREF(clsraw); |
| 4027 |
return -1; |
| 4028 |
} |
| 4029 |
|
| 4030 |
static int |
| 4031 |
load_global(Unpicklerobject *self) |
| 4032 |
{ |
| 4033 |
PyObject *class = 0, *module_name = 0, *class_name = 0; |
| 4034 |
Py_ssize_t len; |
| 4035 |
char *s; |
| 4036 |
|
| 4037 |
if ((len = self->readline_func(self, &s)) < 0) return -1; |
| 4038 |
if (len < 2) return bad_readline(); |
| 4039 |
module_name = PyString_FromStringAndSize(s, len - 1); |
| 4040 |
if (!module_name) return -1; |
| 4041 |
|
| 4042 |
if ((len = self->readline_func(self, &s)) >= 0) { |
| 4043 |
if (len < 2) { |
| 4044 |
Py_DECREF(module_name); |
| 4045 |
return bad_readline(); |
| 4046 |
} |
| 4047 |
if ((class_name = PyString_FromStringAndSize(s, len - 1))) { |
| 4048 |
class = find_class(module_name, class_name, |
| 4049 |
self->find_class); |
| 4050 |
Py_DECREF(class_name); |
| 4051 |
} |
| 4052 |
} |
| 4053 |
Py_DECREF(module_name); |
| 4054 |
|
| 4055 |
if (! class) return -1; |
| 4056 |
PDATA_PUSH(self->stack, class, -1); |
| 4057 |
return 0; |
| 4058 |
} |
| 4059 |
|
| 4060 |
|
| 4061 |
static int |
| 4062 |
load_persid(Unpicklerobject *self) |
| 4063 |
{ |
| 4064 |
PyObject *pid = 0; |
| 4065 |
Py_ssize_t len; |
| 4066 |
char *s; |
| 4067 |
|
| 4068 |
if (self->pers_func) { |
| 4069 |
if ((len = self->readline_func(self, &s)) < 0) return -1; |
| 4070 |
if (len < 2) return bad_readline(); |
| 4071 |
|
| 4072 |
pid = PyString_FromStringAndSize(s, len - 1); |
| 4073 |
if (!pid) return -1; |
| 4074 |
|
| 4075 |
if (PyList_Check(self->pers_func)) { |
| 4076 |
if (PyList_Append(self->pers_func, pid) < 0) { |
| 4077 |
Py_DECREF(pid); |
| 4078 |
return -1; |
| 4079 |
} |
| 4080 |
} |
| 4081 |
else { |
| 4082 |
ARG_TUP(self, pid); |
| 4083 |
if (self->arg) { |
| 4084 |
pid = PyObject_Call(self->pers_func, self->arg, |
| 4085 |
NULL); |
| 4086 |
FREE_ARG_TUP(self); |
| 4087 |
} |
| 4088 |
} |
| 4089 |
|
| 4090 |
if (! pid) return -1; |
| 4091 |
|
| 4092 |
PDATA_PUSH(self->stack, pid, -1); |
| 4093 |
return 0; |
| 4094 |
} |
| 4095 |
else { |
| 4096 |
PyErr_SetString(UnpicklingError, |
| 4097 |
"A load persistent id instruction was encountered,\n" |
| 4098 |
"but no persistent_load function was specified."); |
| 4099 |
return -1; |
| 4100 |
} |
| 4101 |
} |
| 4102 |
|
| 4103 |
static int |
| 4104 |
load_binpersid(Unpicklerobject *self) |
| 4105 |
{ |
| 4106 |
PyObject *pid = 0; |
| 4107 |
|
| 4108 |
if (self->pers_func) { |
| 4109 |
PDATA_POP(self->stack, pid); |
| 4110 |
if (! pid) return -1; |
| 4111 |
|
| 4112 |
if (PyList_Check(self->pers_func)) { |
| 4113 |
if (PyList_Append(self->pers_func, pid) < 0) { |
| 4114 |
Py_DECREF(pid); |
| 4115 |
return -1; |
| 4116 |
} |
| 4117 |
} |
| 4118 |
else { |
| 4119 |
ARG_TUP(self, pid); |
| 4120 |
if (self->arg) { |
| 4121 |
pid = PyObject_Call(self->pers_func, self->arg, |
| 4122 |
NULL); |
| 4123 |
FREE_ARG_TUP(self); |
| 4124 |
} |
| 4125 |
if (! pid) return -1; |
| 4126 |
} |
| 4127 |
|
| 4128 |
PDATA_PUSH(self->stack, pid, -1); |
| 4129 |
return 0; |
| 4130 |
} |
| 4131 |
else { |
| 4132 |
PyErr_SetString(UnpicklingError, |
| 4133 |
"A load persistent id instruction was encountered,\n" |
| 4134 |
"but no persistent_load function was specified."); |
| 4135 |
return -1; |
| 4136 |
} |
| 4137 |
} |
| 4138 |
|
| 4139 |
|
| 4140 |
static int |
| 4141 |
load_pop(Unpicklerobject *self) |
| 4142 |
{ |
| 4143 |
Py_ssize_t len = self->stack->length; |
| 4144 |
|
| 4145 |
|
| 4146 |
|
| 4147 |
|
| 4148 |
|
| 4149 |
|
| 4150 |
|
| 4151 |
|
| 4152 |
if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) { |
| 4153 |
self->num_marks--; |
| 4154 |
} else if (len > 0) { |
| 4155 |
len--; |
| 4156 |
Py_DECREF(self->stack->data[len]); |
| 4157 |
self->stack->length = len; |
| 4158 |
} else { |
| 4159 |
return stackUnderflow(); |
| 4160 |
} |
| 4161 |
return 0; |
| 4162 |
} |
| 4163 |
|
| 4164 |
|
| 4165 |
static int |
| 4166 |
load_pop_mark(Unpicklerobject *self) |
| 4167 |
{ |
| 4168 |
Py_ssize_t i; |
| 4169 |
|
| 4170 |
if ((i = marker(self)) < 0) |
| 4171 |
return -1; |
| 4172 |
|
| 4173 |
Pdata_clear(self->stack, i); |
| 4174 |
|
| 4175 |
return 0; |
| 4176 |
} |
| 4177 |
|
| 4178 |
|
| 4179 |
static int |
| 4180 |
load_dup(Unpicklerobject *self) |
| 4181 |
{ |
| 4182 |
PyObject *last; |
| 4183 |
Py_ssize_t len; |
| 4184 |
|
| 4185 |
if ((len = self->stack->length) <= 0) return stackUnderflow(); |
| 4186 |
last=self->stack->data[len-1]; |
| 4187 |
Py_INCREF(last); |
| 4188 |
PDATA_PUSH(self->stack, last, -1); |
| 4189 |
return 0; |
| 4190 |
} |
| 4191 |
|
| 4192 |
|
| 4193 |
static int |
| 4194 |
load_get(Unpicklerobject *self) |
| 4195 |
{ |
| 4196 |
PyObject *py_str = 0, *value = 0; |
| 4197 |
Py_ssize_t len; |
| 4198 |
char *s; |
| 4199 |
int rc; |
| 4200 |
|
| 4201 |
if ((len = self->readline_func(self, &s)) < 0) return -1; |
| 4202 |
if (len < 2) return bad_readline(); |
| 4203 |
|
| 4204 |
if (!( py_str = PyString_FromStringAndSize(s, len - 1))) return -1; |
| 4205 |
|
| 4206 |
value = PyDict_GetItem(self->memo, py_str); |
| 4207 |
if (! value) { |
| 4208 |
PyErr_SetObject(BadPickleGet, py_str); |
| 4209 |
rc = -1; |
| 4210 |
} |
| 4211 |
else { |
| 4212 |
PDATA_APPEND(self->stack, value, -1); |
| 4213 |
rc = 0; |
| 4214 |
} |
| 4215 |
|
| 4216 |
Py_DECREF(py_str); |
| 4217 |
return rc; |
| 4218 |
} |
| 4219 |
|
| 4220 |
|
| 4221 |
static int |
| 4222 |
load_binget(Unpicklerobject *self) |
| 4223 |
{ |
| 4224 |
PyObject *py_key = 0, *value = 0; |
| 4225 |
unsigned char key; |
| 4226 |
char *s; |
| 4227 |
int rc; |
| 4228 |
|
| 4229 |
if (self->read_func(self, &s, 1) < 0) return -1; |
| 4230 |
|
| 4231 |
key = (unsigned char)s[0]; |
| 4232 |
if (!( py_key = PyInt_FromLong((long)key))) return -1; |
| 4233 |
|
| 4234 |
value = PyDict_GetItem(self->memo, py_key); |
| 4235 |
if (! value) { |
| 4236 |
PyErr_SetObject(BadPickleGet, py_key); |
| 4237 |
rc = -1; |
| 4238 |
} |
| 4239 |
else { |
| 4240 |
PDATA_APPEND(self->stack, value, -1); |
| 4241 |
rc = 0; |
| 4242 |
} |
| 4243 |
|
| 4244 |
Py_DECREF(py_key); |
| 4245 |
return rc; |
| 4246 |
} |
| 4247 |
|
| 4248 |
|
| 4249 |
static int |
| 4250 |
load_long_binget(Unpicklerobject *self) |
| 4251 |
{ |
| 4252 |
PyObject *py_key = 0, *value = 0; |
| 4253 |
unsigned char c; |
| 4254 |
char *s; |
| 4255 |
Py_ssize_t key; |
| 4256 |
int rc; |
| 4257 |
|
| 4258 |
if (self->read_func(self, &s, 4) < 0) return -1; |
| 4259 |
|
| 4260 |
c = (unsigned char)s[0]; |
| 4261 |
key = (long)c; |
| 4262 |
c = (unsigned char)s[1]; |
| 4263 |
key |= (long)c << 8; |
| 4264 |
c = (unsigned char)s[2]; |
| 4265 |
key |= (long)c << 16; |
| 4266 |
c = (unsigned char)s[3]; |
| 4267 |
key |= (long)c << 24; |
| 4268 |
|
| 4269 |
if (!( py_key = PyInt_FromLong((long)key))) return -1; |
| 4270 |
|
| 4271 |
value = PyDict_GetItem(self->memo, py_key); |
| 4272 |
if (! value) { |
| 4273 |
PyErr_SetObject(BadPickleGet, py_key); |
| 4274 |
rc = -1; |
| 4275 |
} |
| 4276 |
else { |
| 4277 |
PDATA_APPEND(self->stack, value, -1); |
| 4278 |
rc = 0; |
| 4279 |
} |
| 4280 |
|
| 4281 |
Py_DECREF(py_key); |
| 4282 |
return rc; |
| 4283 |
} |
| 4284 |
|
| 4285 |
|
| 4286 |
|
| 4287 |
|
| 4288 |
static int |
| 4289 |
load_extension(Unpicklerobject *self, int nbytes) |
| 4290 |
{ |
| 4291 |
char *codebytes; |
| 4292 |
long code; |
| 4293 |
PyObject *py_code; |
| 4294 |
PyObject *obj; |
| 4295 |
PyObject *pair; |
| 4296 |
PyObject *module_name, *class_name; |
| 4297 |
|
| 4298 |
assert(nbytes == 1 || nbytes == 2 || nbytes == 4); |
| 4299 |
if (self->read_func(self, &codebytes, nbytes) < 0) return -1; |
| 4300 |
code = calc_binint(codebytes, nbytes); |
| 4301 |
if (code <= 0) { |
| 4302 |
|
| 4303 |
PyErr_SetString(UnpicklingError, "EXT specifies code <= 0"); |
| 4304 |
return -1; |
| 4305 |
} |
| 4306 |
|
| 4307 |
|
| 4308 |
py_code = PyInt_FromLong(code); |
| 4309 |
if (py_code == NULL) return -1; |
| 4310 |
obj = PyDict_GetItem(extension_cache, py_code); |
| 4311 |
if (obj != NULL) { |
| 4312 |
|
| 4313 |
Py_DECREF(py_code); |
| 4314 |
PDATA_APPEND(self->stack, obj, -1); |
| 4315 |
return 0; |
| 4316 |
} |
| 4317 |
|
| 4318 |
|
| 4319 |
pair = PyDict_GetItem(inverted_registry, py_code); |
| 4320 |
if (pair == NULL) { |
| 4321 |
Py_DECREF(py_code); |
| 4322 |
PyErr_Format(PyExc_ValueError, "unregistered extension " |
| 4323 |
"code %ld", code); |
| 4324 |
return -1; |
| 4325 |
} |
| 4326 |
|
| 4327 |
|
| 4328 |
|
| 4329 |
if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 || |
| 4330 |
!PyString_Check(module_name = PyTuple_GET_ITEM(pair, 0)) || |
| 4331 |
!PyString_Check(class_name = PyTuple_GET_ITEM(pair, 1))) { |
| 4332 |
Py_DECREF(py_code); |
| 4333 |
PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] " |
| 4334 |
"isn't a 2-tuple of strings", code); |
| 4335 |
return -1; |
| 4336 |
} |
| 4337 |
|
| 4338 |
obj = find_class(module_name, class_name, self->find_class); |
| 4339 |
if (obj == NULL) { |
| 4340 |
Py_DECREF(py_code); |
| 4341 |
return -1; |
| 4342 |
} |
| 4343 |
|
| 4344 |
code = PyDict_SetItem(extension_cache, py_code, obj); |
| 4345 |
Py_DECREF(py_code); |
| 4346 |
if (code < 0) { |
| 4347 |
Py_DECREF(obj); |
| 4348 |
return -1; |
| 4349 |
} |
| 4350 |
PDATA_PUSH(self->stack, obj, -1); |
| 4351 |
return 0; |
| 4352 |
} |
| 4353 |
|
| 4354 |
static int |
| 4355 |
load_put(Unpicklerobject *self) |
| 4356 |
{ |
| 4357 |
PyObject *py_str = 0, *value = 0; |
| 4358 |
Py_ssize_t len, l; |
| 4359 |
char *s; |
| 4360 |
|
| 4361 |
if ((l = self->readline_func(self, &s)) < 0) return -1; |
| 4362 |
if (l < 2) return bad_readline(); |
| 4363 |
if (!( len=self->stack->length )) return stackUnderflow(); |
| 4364 |
if (!( py_str = PyString_FromStringAndSize(s, l - 1))) return -1; |
| 4365 |
value=self->stack->data[len-1]; |
| 4366 |
l=PyDict_SetItem(self->memo, py_str, value); |
| 4367 |
Py_DECREF(py_str); |
| 4368 |
return l; |
| 4369 |
} |
| 4370 |
|
| 4371 |
|
| 4372 |
static int |
| 4373 |
load_binput(Unpicklerobject *self) |
| 4374 |
{ |
| 4375 |
PyObject *py_key = 0, *value = 0; |
| 4376 |
unsigned char key; |
| 4377 |
char *s; |
| 4378 |
Py_ssize_t len; |
| 4379 |
|
| 4380 |
if (self->read_func(self, &s, 1) < 0) return -1; |
| 4381 |
if (!( (len=self->stack->length) > 0 )) return stackUnderflow(); |
| 4382 |
|
| 4383 |
key = (unsigned char)s[0]; |
| 4384 |
|
| 4385 |
if (!( py_key = PyInt_FromLong((long)key))) return -1; |
| 4386 |
value=self->stack->data[len-1]; |
| 4387 |
len=PyDict_SetItem(self->memo, py_key, value); |
| 4388 |
Py_DECREF(py_key); |
| 4389 |
return len; |
| 4390 |
} |
| 4391 |
|
| 4392 |
|
| 4393 |
static int |
| 4394 |
load_long_binput(Unpicklerobject *self) |
| 4395 |
{ |
| 4396 |
PyObject *py_key = 0, *value = 0; |
| 4397 |
Py_ssize_t key; |
| 4398 |
unsigned char c; |
| 4399 |
char *s; |
| 4400 |
Py_ssize_t len; |
| 4401 |
|
| 4402 |
if (self->read_func(self, &s, 4) < 0) return -1; |
| 4403 |
if (!( len=self->stack->length )) return stackUnderflow(); |
| 4404 |
|
| 4405 |
c = (unsigned char)s[0]; |
| 4406 |
key = (long)c; |
| 4407 |
c = (unsigned char)s[1]; |
| 4408 |
key |= (long)c << 8; |
| 4409 |
c = (unsigned char)s[2]; |
| 4410 |
key |= (long)c << 16; |
| 4411 |
c = (unsigned char)s[3]; |
| 4412 |
key |= (long)c << 24; |
| 4413 |
|
| 4414 |
if (!( py_key = PyInt_FromLong(key))) return -1; |
| 4415 |
value=self->stack->data[len-1]; |
| 4416 |
len=PyDict_SetItem(self->memo, py_key, value); |
| 4417 |
Py_DECREF(py_key); |
| 4418 |
return len; |
| 4419 |
} |
| 4420 |
|
| 4421 |
|
| 4422 |
static int |
| 4423 |
do_append(Unpicklerobject *self, Py_ssize_t x) |
| 4424 |
{ |
| 4425 |
PyObject *value = 0, *list = 0, *append_method = 0; |
| 4426 |
Py_ssize_t len, i; |
| 4427 |
|
| 4428 |
len=self->stack->length; |
| 4429 |
if (!( len >= x && x > 0 )) return stackUnderflow(); |
| 4430 |
|
| 4431 |
if (len==x) return 0; |
| 4432 |
|
| 4433 |
list=self->stack->data[x-1]; |
| 4434 |
|
| 4435 |
if (PyList_Check(list)) { |
| 4436 |
PyObject *slice; |
| 4437 |
int list_len; |
| 4438 |
|
| 4439 |
slice=Pdata_popList(self->stack, x); |
| 4440 |
if (! slice) return -1; |
| 4441 |
list_len = PyList_GET_SIZE(list); |
| 4442 |
i=PyList_SetSlice(list, list_len, list_len, slice); |
| 4443 |
Py_DECREF(slice); |
| 4444 |
return i; |
| 4445 |
} |
| 4446 |
else { |
| 4447 |
|
| 4448 |
if (!( append_method = PyObject_GetAttr(list, append_str))) |
| 4449 |
return -1; |
| 4450 |
|
| 4451 |
for (i = x; i < len; i++) { |
| 4452 |
PyObject *junk; |
| 4453 |
|
| 4454 |
value=self->stack->data[i]; |
| 4455 |
junk=0; |
| 4456 |
ARG_TUP(self, value); |
| 4457 |
if (self->arg) { |
| 4458 |
junk = PyObject_Call(append_method, self->arg, |
| 4459 |
NULL); |
| 4460 |
FREE_ARG_TUP(self); |
| 4461 |
} |
| 4462 |
if (! junk) { |
| 4463 |
Pdata_clear(self->stack, i+1); |
| 4464 |
self->stack->length=x; |
| 4465 |
Py_DECREF(append_method); |
| 4466 |
return -1; |
| 4467 |
} |
| 4468 |
Py_DECREF(junk); |
| 4469 |
} |
| 4470 |
self->stack->length=x; |
| 4471 |
Py_DECREF(append_method); |
| 4472 |
} |
| 4473 |
|
| 4474 |
return 0; |
| 4475 |
} |
| 4476 |
|
| 4477 |
|
| 4478 |
static int |
| 4479 |
load_append(Unpicklerobject *self) |
| 4480 |
{ |
| 4481 |
if (self->stack->length - 1 <= 0) |
| 4482 |
return stackUnderflow(); |
| 4483 |
return do_append(self, self->stack->length - 1); |
| 4484 |
} |
| 4485 |
|
| 4486 |
|
| 4487 |
static int |
| 4488 |
load_appends(Unpicklerobject *self) |
| 4489 |
{ |
| 4490 |
Py_ssize_t i = marker(self); |
| 4491 |
if (i < 0) |
| 4492 |
return -1; |
| 4493 |
return do_append(self, i); |
| 4494 |
} |
| 4495 |
|
| 4496 |
|
| 4497 |
static Py_ssize_t |
| 4498 |
do_setitems(Unpicklerobject *self, Py_ssize_t x) |
| 4499 |
{ |
| 4500 |
PyObject *value = 0, *key = 0, *dict = 0; |
| 4501 |
Py_ssize_t len, i, r=0; |
| 4502 |
|
| 4503 |
if (!( (len=self->stack->length) >= x |
| 4504 |
&& x > 0 )) return stackUnderflow(); |
| 4505 |
if (len == x) |
| 4506 |
return 0; |
| 4507 |
if ((len - x) % 2 != 0) { |
| 4508 |
|
| 4509 |
PyErr_SetString(UnpicklingError, |
| 4510 |
"odd number of items for SETITEMS"); |
| 4511 |
return -1; |
| 4512 |
} |
| 4513 |
|
| 4514 |
dict=self->stack->data[x-1]; |
| 4515 |
|
| 4516 |
for (i = x+1; i < len; i += 2) { |
| 4517 |
key =self->stack->data[i-1]; |
| 4518 |
value=self->stack->data[i ]; |
| 4519 |
if (PyObject_SetItem(dict, key, value) < 0) { |
| 4520 |
r=-1; |
| 4521 |
break; |
| 4522 |
} |
| 4523 |
} |
| 4524 |
|
| 4525 |
Pdata_clear(self->stack, x); |
| 4526 |
|
| 4527 |
return r; |
| 4528 |
} |
| 4529 |
|
| 4530 |
|
| 4531 |
static int |
| 4532 |
load_setitem(Unpicklerobject *self) |
| 4533 |
{ |
| 4534 |
return do_setitems(self, self->stack->length - 2); |
| 4535 |
} |
| 4536 |
|
| 4537 |
static int |
| 4538 |
load_setitems(Unpicklerobject *self) |
| 4539 |
{ |
| 4540 |
Py_ssize_t i = marker(self); |
| 4541 |
if (i < 0) |
| 4542 |
return -1; |
| 4543 |
return do_setitems(self, i); |
| 4544 |
} |
| 4545 |
|
| 4546 |
|
| 4547 |
static int |
| 4548 |
load_build(Unpicklerobject *self) |
| 4549 |
{ |
| 4550 |
PyObject *state, *inst, *slotstate; |
| 4551 |
PyObject *__setstate__; |
| 4552 |
PyObject *d_key, *d_value; |
| 4553 |
int res = -1; |
| 4554 |
Py_ssize_t i; |
| 4555 |
|
| 4556 |
|
| 4557 |
|
| 4558 |
|
| 4559 |
if (self->stack->length < 2) |
| 4560 |
return stackUnderflow(); |
| 4561 |
PDATA_POP(self->stack, state); |
| 4562 |
if (state == NULL) |
| 4563 |
return -1; |
| 4564 |
inst = self->stack->data[self->stack->length - 1]; |
| 4565 |
|
| 4566 |
__setstate__ = PyObject_GetAttr(inst, __setstate___str); |
| 4567 |
if (__setstate__ != NULL) { |
| 4568 |
PyObject *junk = NULL; |
| 4569 |
|
| 4570 |
|
| 4571 |
ARG_TUP(self, state); |
| 4572 |
if (self->arg) { |
| 4573 |
junk = PyObject_Call(__setstate__, self->arg, NULL); |
| 4574 |
FREE_ARG_TUP(self); |
| 4575 |
} |
| 4576 |
Py_DECREF(__setstate__); |
| 4577 |
if (junk == NULL) |
| 4578 |
return -1; |
| 4579 |
Py_DECREF(junk); |
| 4580 |
return 0; |
| 4581 |
} |
| 4582 |
if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 4583 |
return -1; |
| 4584 |
PyErr_Clear(); |
| 4585 |
|
| 4586 |
|
| 4587 |
|
| 4588 |
|
| 4589 |
if (PyTuple_Check(state) && PyTuple_Size(state) == 2) { |
| 4590 |
PyObject *temp = state; |
| 4591 |
state = PyTuple_GET_ITEM(temp, 0); |
| 4592 |
slotstate = PyTuple_GET_ITEM(temp, 1); |
| 4593 |
Py_INCREF(state); |
| 4594 |
Py_INCREF(slotstate); |
| 4595 |
Py_DECREF(temp); |
| 4596 |
} |
| 4597 |
else |
| 4598 |
slotstate = NULL; |
| 4599 |
|
| 4600 |
|
| 4601 |
if (state != Py_None) { |
| 4602 |
PyObject *dict; |
| 4603 |
if (! PyDict_Check(state)) { |
| 4604 |
PyErr_SetString(UnpicklingError, "state is not a " |
| 4605 |
"dictionary"); |
| 4606 |
goto finally; |
| 4607 |
} |
| 4608 |
dict = PyObject_GetAttr(inst, __dict___str); |
| 4609 |
if (dict == NULL) |
| 4610 |
goto finally; |
| 4611 |
|
| 4612 |
i = 0; |
| 4613 |
while (PyDict_Next(state, &i, &d_key, &d_value)) { |
| 4614 |
|
| 4615 |
|
| 4616 |
Py_INCREF(d_key); |
| 4617 |
if (PyString_CheckExact(d_key)) |
| 4618 |
PyString_InternInPlace(&d_key); |
| 4619 |
if (PyObject_SetItem(dict, d_key, d_value) < 0) { |
| 4620 |
Py_DECREF(d_key); |
| 4621 |
goto finally; |
| 4622 |
} |
| 4623 |
Py_DECREF(d_key); |
| 4624 |
} |
| 4625 |
Py_DECREF(dict); |
| 4626 |
} |
| 4627 |
|
| 4628 |
|
| 4629 |
if (slotstate != NULL) { |
| 4630 |
if (! PyDict_Check(slotstate)) { |
| 4631 |
PyErr_SetString(UnpicklingError, "slot state is not " |
| 4632 |
"a dictionary"); |
| 4633 |
goto finally; |
| 4634 |
} |
| 4635 |
i = 0; |
| 4636 |
while (PyDict_Next(slotstate, &i, &d_key, &d_value)) { |
| 4637 |
if (PyObject_SetAttr(inst, d_key, d_value) < 0) |
| 4638 |
goto finally; |
| 4639 |
} |
| 4640 |
} |
| 4641 |
res = 0; |
| 4642 |
|
| 4643 |
finally: |
| 4644 |
Py_DECREF(state); |
| 4645 |
Py_XDECREF(slotstate); |
| 4646 |
return res; |
| 4647 |
} |
| 4648 |
|
| 4649 |
|
| 4650 |
static int |
| 4651 |
load_mark(Unpicklerobject *self) |
| 4652 |
{ |
| 4653 |
Py_ssize_t s; |
| 4654 |
|
| 4655 |
|
| 4656 |
|
| 4657 |
|
| 4658 |
|
| 4659 |
|
| 4660 |
if ((self->num_marks + 1) >= self->marks_size) { |
| 4661 |
Py_ssize_t *marks; |
| 4662 |
s=self->marks_size+20; |
| 4663 |
if (s <= self->num_marks) s=self->num_marks + 1; |
| 4664 |
if (self->marks == NULL) |
| 4665 |
marks=(Py_ssize_t *)malloc(s * sizeof(Py_ssize_t)); |
| 4666 |
else |
| 4667 |
marks=(Py_ssize_t *)realloc(self->marks, |
| 4668 |
s * sizeof(Py_ssize_t)); |
| 4669 |
if (!marks) { |
| 4670 |
PyErr_NoMemory(); |
| 4671 |
return -1; |
| 4672 |
} |
| 4673 |
self->marks = marks; |
| 4674 |
self->marks_size = s; |
| 4675 |
} |
| 4676 |
|
| 4677 |
self->marks[self->num_marks++] = self->stack->length; |
| 4678 |
|
| 4679 |
return 0; |
| 4680 |
} |
| 4681 |
|
| 4682 |
static int |
| 4683 |
load_reduce(Unpicklerobject *self) |
| 4684 |
{ |
| 4685 |
PyObject *callable = 0, *arg_tup = 0, *ob = 0; |
| 4686 |
|
| 4687 |
PDATA_POP(self->stack, arg_tup); |
| 4688 |
if (! arg_tup) return -1; |
| 4689 |
PDATA_POP(self->stack, callable); |
| 4690 |
if (callable) { |
| 4691 |
ob = Instance_New(callable, arg_tup); |
| 4692 |
Py_DECREF(callable); |
| 4693 |
} |
| 4694 |
Py_DECREF(arg_tup); |
| 4695 |
|
| 4696 |
if (! ob) return -1; |
| 4697 |
|
| 4698 |
PDATA_PUSH(self->stack, ob, -1); |
| 4699 |
return 0; |
| 4700 |
} |
| 4701 |
|
| 4702 |
|
| 4703 |
|
| 4704 |
|
| 4705 |
static int |
| 4706 |
load_proto(Unpicklerobject *self) |
| 4707 |
{ |
| 4708 |
int i; |
| 4709 |
char *protobyte; |
| 4710 |
|
| 4711 |
i = self->read_func(self, &protobyte, 1); |
| 4712 |
if (i < 0) |
| 4713 |
return -1; |
| 4714 |
|
| 4715 |
i = calc_binint(protobyte, 1); |
| 4716 |
|
| 4717 |
|
| 4718 |
|
| 4719 |
assert(i >= 0); |
| 4720 |
if (i <= HIGHEST_PROTOCOL) |
| 4721 |
return 0; |
| 4722 |
|
| 4723 |
PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i); |
| 4724 |
return -1; |
| 4725 |
} |
| 4726 |
|
| 4727 |
static PyObject * |
| 4728 |
load(Unpicklerobject *self) |
| 4729 |
{ |
| 4730 |
PyObject *err = 0, *val = 0; |
| 4731 |
char *s; |
| 4732 |
|
| 4733 |
self->num_marks = 0; |
| 4734 |
if (self->stack->length) Pdata_clear(self->stack, 0); |
| 4735 |
|
| 4736 |
while (1) { |
| 4737 |
if (self->read_func(self, &s, 1) < 0) |
| 4738 |
break; |
| 4739 |
|
| 4740 |
switch (s[0]) { |
| 4741 |
case NONE: |
| 4742 |
if (load_none(self) < 0) |
| 4743 |
break; |
| 4744 |
continue; |
| 4745 |
|
| 4746 |
case BININT: |
| 4747 |
if (load_binint(self) < 0) |
| 4748 |
break; |
| 4749 |
continue; |
| 4750 |
|
| 4751 |
case BININT1: |
| 4752 |
if (load_binint1(self) < 0) |
| 4753 |
break; |
| 4754 |
continue; |
| 4755 |
|
| 4756 |
case BININT2: |
| 4757 |
if (load_binint2(self) < 0) |
| 4758 |
break; |
| 4759 |
continue; |
| 4760 |
|
| 4761 |
case INT: |
| 4762 |
if (load_int(self) < 0) |
| 4763 |
break; |
| 4764 |
continue; |
| 4765 |
|
| 4766 |
case LONG: |
| 4767 |
if (load_long(self) < 0) |
| 4768 |
break; |
| 4769 |
continue; |
| 4770 |
|
| 4771 |
case LONG1: |
| 4772 |
if (load_counted_long(self, 1) < 0) |
| 4773 |
break; |
| 4774 |
continue; |
| 4775 |
|
| 4776 |
case LONG4: |
| 4777 |
if (load_counted_long(self, 4) < 0) |
| 4778 |
break; |
| 4779 |
continue; |
| 4780 |
|
| 4781 |
case FLOAT: |
| 4782 |
if (load_float(self) < 0) |
| 4783 |
break; |
| 4784 |
continue; |
| 4785 |
|
| 4786 |
case BINFLOAT: |
| 4787 |
if (load_binfloat(self) < 0) |
| 4788 |
break; |
| 4789 |
continue; |
| 4790 |
|
| 4791 |
case BINSTRING: |
| 4792 |
if (load_binstring(self) < 0) |
| 4793 |
break; |
| 4794 |
continue; |
| 4795 |
|
| 4796 |
case SHORT_BINSTRING: |
| 4797 |
if (load_short_binstring(self) < 0) |
| 4798 |
break; |
| 4799 |
continue; |
| 4800 |
|
| 4801 |
case STRING: |
| 4802 |
if (load_string(self) < 0) |
| 4803 |
break; |
| 4804 |
continue; |
| 4805 |
|
| 4806 |
#ifdef Py_USING_UNICODE |
| 4807 |
case UNICODE: |
| 4808 |
if (load_unicode(self) < 0) |
| 4809 |
break; |
| 4810 |
continue; |
| 4811 |
|
| 4812 |
case BINUNICODE: |
| 4813 |
if (load_binunicode(self) < 0) |
| 4814 |
break; |
| 4815 |
continue; |
| 4816 |
#endif |
| 4817 |
|
| 4818 |
case EMPTY_TUPLE: |
| 4819 |
if (load_counted_tuple(self, 0) < 0) |
| 4820 |
break; |
| 4821 |
continue; |
| 4822 |
|
| 4823 |
case TUPLE1: |
| 4824 |
if (load_counted_tuple(self, 1) < 0) |
| 4825 |
break; |
| 4826 |
continue; |
| 4827 |
|
| 4828 |
case TUPLE2: |
| 4829 |
if (load_counted_tuple(self, 2) < 0) |
| 4830 |
break; |
| 4831 |
continue; |
| 4832 |
|
| 4833 |
case TUPLE3: |
| 4834 |
if (load_counted_tuple(self, 3) < 0) |
| 4835 |
break; |
| 4836 |
continue; |
| 4837 |
|
| 4838 |
case TUPLE: |
| 4839 |
if (load_tuple(self) < 0) |
| 4840 |
break; |
| 4841 |
continue; |
| 4842 |
|
| 4843 |
case EMPTY_LIST: |
| 4844 |
if (load_empty_list(self) < 0) |
| 4845 |
break; |
| 4846 |
continue; |
| 4847 |
|
| 4848 |
case LIST: |
| 4849 |
if (load_list(self) < 0) |
| 4850 |
break; |
| 4851 |
continue; |
| 4852 |
|
| 4853 |
case EMPTY_DICT: |
| 4854 |
if (load_empty_dict(self) < 0) |
| 4855 |
break; |
| 4856 |
continue; |
| 4857 |
|
| 4858 |
case DICT: |
| 4859 |
if (load_dict(self) < 0) |
| 4860 |
break; |
| 4861 |
continue; |
| 4862 |
|
| 4863 |
case OBJ: |
| 4864 |
if (load_obj(self) < 0) |
| 4865 |
break; |
| 4866 |
continue; |
| 4867 |
|
| 4868 |
case INST: |
| 4869 |
if (load_inst(self) < 0) |
| 4870 |
break; |
| 4871 |
continue; |
| 4872 |
|
| 4873 |
case NEWOBJ: |
| 4874 |
if (load_newobj(self) < 0) |
| 4875 |
break; |
| 4876 |
continue; |
| 4877 |
|
| 4878 |
case GLOBAL: |
| 4879 |
if (load_global(self) < 0) |
| 4880 |
break; |
| 4881 |
continue; |
| 4882 |
|
| 4883 |
case APPEND: |
| 4884 |
if (load_append(self) < 0) |
| 4885 |
break; |
| 4886 |
continue; |
| 4887 |
|
| 4888 |
case APPENDS: |
| 4889 |
if (load_appends(self) < 0) |
| 4890 |
break; |
| 4891 |
continue; |
| 4892 |
|
| 4893 |
case BUILD: |
| 4894 |
if (load_build(self) < 0) |
| 4895 |
break; |
| 4896 |
continue; |
| 4897 |
|
| 4898 |
case DUP: |
| 4899 |
if (load_dup(self) < 0) |
| 4900 |
break; |
| 4901 |
continue; |
| 4902 |
|
| 4903 |
case BINGET: |
| 4904 |
if (load_binget(self) < 0) |
| 4905 |
break; |
| 4906 |
continue; |
| 4907 |
|
| 4908 |
case LONG_BINGET: |
| 4909 |
if (load_long_binget(self) < 0) |
| 4910 |
break; |
| 4911 |
continue; |
| 4912 |
|
| 4913 |
case GET: |
| 4914 |
if (load_get(self) < 0) |
| 4915 |
break; |
| 4916 |
continue; |
| 4917 |
|
| 4918 |
case EXT1: |
| 4919 |
if (load_extension(self, 1) < 0) |
| 4920 |
break; |
| 4921 |
continue; |
| 4922 |
|
| 4923 |
case EXT2: |
| 4924 |
if (load_extension(self, 2) < 0) |
| 4925 |
break; |
| 4926 |
continue; |
| 4927 |
|
| 4928 |
case EXT4: |
| 4929 |
if (load_extension(self, 4) < 0) |
| 4930 |
break; |
| 4931 |
continue; |
| 4932 |
case MARK: |
| 4933 |
if (load_mark(self) < 0) |
| 4934 |
break; |
| 4935 |
continue; |
| 4936 |
|
| 4937 |
case BINPUT: |
| 4938 |
if (load_binput(self) < 0) |
| 4939 |
break; |
| 4940 |
continue; |
| 4941 |
|
| 4942 |
case LONG_BINPUT: |
| 4943 |
if (load_long_binput(self) < 0) |
| 4944 |
break; |
| 4945 |
continue; |
| 4946 |
|
| 4947 |
case PUT: |
| 4948 |
if (load_put(self) < 0) |
| 4949 |
break; |
| 4950 |
continue; |
| 4951 |
|
| 4952 |
case POP: |
| 4953 |
if (load_pop(self) < 0) |
| 4954 |
break; |
| 4955 |
continue; |
| 4956 |
|
| 4957 |
case POP_MARK: |
| 4958 |
if (load_pop_mark(self) < 0) |
| 4959 |
break; |
| 4960 |
continue; |
| 4961 |
|
| 4962 |
case SETITEM: |
| 4963 |
if (load_setitem(self) < 0) |
| 4964 |
break; |
| 4965 |
continue; |
| 4966 |
|
| 4967 |
case SETITEMS: |
| 4968 |
if (load_setitems(self) < 0) |
| 4969 |
break; |
| 4970 |
continue; |
| 4971 |
|
| 4972 |
case STOP: |
| 4973 |
break; |
| 4974 |
|
| 4975 |
case PERSID: |
| 4976 |
if (load_persid(self) < 0) |
| 4977 |
break; |
| 4978 |
continue; |
| 4979 |
|
| 4980 |
case BINPERSID: |
| 4981 |
if (load_binpersid(self) < 0) |
| 4982 |
break; |
| 4983 |
continue; |
| 4984 |
|
| 4985 |
case REDUCE: |
| 4986 |
if (load_reduce(self) < 0) |
| 4987 |
break; |
| 4988 |
continue; |
| 4989 |
|
| 4990 |
case PROTO: |
| 4991 |
if (load_proto(self) < 0) |
| 4992 |
break; |
| 4993 |
continue; |
| 4994 |
|
| 4995 |
case NEWTRUE: |
| 4996 |
if (load_bool(self, Py_True) < 0) |
| 4997 |
break; |
| 4998 |
continue; |
| 4999 |
|
| 5000 |
case NEWFALSE: |
| 5001 |
if (load_bool(self, Py_False) < 0) |
| 5002 |
break; |
| 5003 |
continue; |
| 5004 |
|
| 5005 |
case '\0': |
| 5006 |
|
| 5007 |
PyErr_SetNone(PyExc_EOFError); |
| 5008 |
break; |
| 5009 |
|
| 5010 |
default: |
| 5011 |
cPickle_ErrFormat(UnpicklingError, |
| 5012 |
"invalid load key, '%s'.", |
| 5013 |
"c", s[0]); |
| 5014 |
return NULL; |
| 5015 |
} |
| 5016 |
|
| 5017 |
break; |
| 5018 |
} |
| 5019 |
|
| 5020 |
if ((err = PyErr_Occurred())) { |
| 5021 |
if (err == PyExc_EOFError) { |
| 5022 |
PyErr_SetNone(PyExc_EOFError); |
| 5023 |
} |
| 5024 |
return NULL; |
| 5025 |
} |
| 5026 |
|
| 5027 |
PDATA_POP(self->stack, val); |
| 5028 |
return val; |
| 5029 |
} |
| 5030 |
|
| 5031 |
|
| 5032 |
|
| 5033 |
|
| 5034 |
|
| 5035 |
static int |
| 5036 |
noload_obj(Unpicklerobject *self) |
| 5037 |
{ |
| 5038 |
Py_ssize_t i; |
| 5039 |
|
| 5040 |
if ((i = marker(self)) < 0) return -1; |
| 5041 |
return Pdata_clear(self->stack, i+1); |
| 5042 |
} |
| 5043 |
|
| 5044 |
|
| 5045 |
static int |
| 5046 |
noload_inst(Unpicklerobject *self) |
| 5047 |
{ |
| 5048 |
Py_ssize_t i; |
| 5049 |
char *s; |
| 5050 |
|
| 5051 |
if ((i = marker(self)) < 0) return -1; |
| 5052 |
Pdata_clear(self->stack, i); |
| 5053 |
if (self->readline_func(self, &s) < 0) return -1; |
| 5054 |
if (self->readline_func(self, &s) < 0) return -1; |
| 5055 |
PDATA_APPEND(self->stack, Py_None, -1); |
| 5056 |
return 0; |
| 5057 |
} |
| 5058 |
|
| 5059 |
static int |
| 5060 |
noload_newobj(Unpicklerobject *self) |
| 5061 |
{ |
| 5062 |
PyObject *obj; |
| 5063 |
|
| 5064 |
PDATA_POP(self->stack, obj); |
| 5065 |
if (obj == NULL) return -1; |
| 5066 |
Py_DECREF(obj); |
| 5067 |
|
| 5068 |
PDATA_POP(self->stack, obj); |
| 5069 |
if (obj == NULL) return -1; |
| 5070 |
Py_DECREF(obj); |
| 5071 |
|
| 5072 |
PDATA_APPEND(self->stack, Py_None, -1); |
| 5073 |
return 0; |
| 5074 |
} |
| 5075 |
|
| 5076 |
static int |
| 5077 |
noload_global(Unpicklerobject *self) |
| 5078 |
{ |
| 5079 |
char *s; |
| 5080 |
|
| 5081 |
if (self->readline_func(self, &s) < 0) return -1; |
| 5082 |
if (self->readline_func(self, &s) < 0) return -1; |
| 5083 |
PDATA_APPEND(self->stack, Py_None,-1); |
| 5084 |
return 0; |
| 5085 |
} |
| 5086 |
|
| 5087 |
static int |
| 5088 |
noload_reduce(Unpicklerobject *self) |
| 5089 |
{ |
| 5090 |
|
| 5091 |
if (self->stack->length < 2) return stackUnderflow(); |
| 5092 |
Pdata_clear(self->stack, self->stack->length-2); |
| 5093 |
PDATA_APPEND(self->stack, Py_None,-1); |
| 5094 |
return 0; |
| 5095 |
} |
| 5096 |
|
| 5097 |
static int |
| 5098 |
noload_build(Unpicklerobject *self) { |
| 5099 |
|
| 5100 |
if (self->stack->length < 1) return stackUnderflow(); |
| 5101 |
Pdata_clear(self->stack, self->stack->length-1); |
| 5102 |
return 0; |
| 5103 |
} |
| 5104 |
|
| 5105 |
static int |
| 5106 |
noload_extension(Unpicklerobject *self, int nbytes) |
| 5107 |
{ |
| 5108 |
char *codebytes; |
| 5109 |
|
| 5110 |
assert(nbytes == 1 || nbytes == 2 || nbytes == 4); |
| 5111 |
if (self->read_func(self, &codebytes, nbytes) < 0) return -1; |
| 5112 |
PDATA_APPEND(self->stack, Py_None, -1); |
| 5113 |
return 0; |
| 5114 |
} |
| 5115 |
|
| 5116 |
static int |
| 5117 |
noload_append(Unpicklerobject *self) |
| 5118 |
{ |
| 5119 |
return Pdata_clear(self->stack, self->stack->length - 1); |
| 5120 |
} |
| 5121 |
|
| 5122 |
static int |
| 5123 |
noload_appends(Unpicklerobject *self) |
| 5124 |
{ |
| 5125 |
Py_ssize_t i; |
| 5126 |
if ((i = marker(self)) < 0) return -1; |
| 5127 |
return Pdata_clear(self->stack, i); |
| 5128 |
} |
| 5129 |
|
| 5130 |
static int |
| 5131 |
noload_setitem(Unpicklerobject *self) |
| 5132 |
{ |
| 5133 |
return Pdata_clear(self->stack, self->stack->length - 2); |
| 5134 |
} |
| 5135 |
|
| 5136 |
static int |
| 5137 |
noload_setitems(Unpicklerobject *self) |
| 5138 |
{ |
| 5139 |
Py_ssize_t i; |
| 5140 |
if ((i = marker(self)) < 0) return -1; |
| 5141 |
return Pdata_clear(self->stack, i); |
| 5142 |
} |
| 5143 |
|
| 5144 |
static PyObject * |
| 5145 |
noload(Unpicklerobject *self) |
| 5146 |
{ |
| 5147 |
PyObject *err = 0, *val = 0; |
| 5148 |
char *s; |
| 5149 |
|
| 5150 |
self->num_marks = 0; |
| 5151 |
Pdata_clear(self->stack, 0); |
| 5152 |
|
| 5153 |
while (1) { |
| 5154 |
if (self->read_func(self, &s, 1) < 0) |
| 5155 |
break; |
| 5156 |
|
| 5157 |
switch (s[0]) { |
| 5158 |
case NONE: |
| 5159 |
if (load_none(self) < 0) |
| 5160 |
break; |
| 5161 |
continue; |
| 5162 |
|
| 5163 |
case BININT: |
| 5164 |
if (load_binint(self) < 0) |
| 5165 |
break; |
| 5166 |
continue; |
| 5167 |
|
| 5168 |
case BININT1: |
| 5169 |
if (load_binint1(self) < 0) |
| 5170 |
break; |
| 5171 |
continue; |
| 5172 |
|
| 5173 |
case BININT2: |
| 5174 |
if (load_binint2(self) < 0) |
| 5175 |
break; |
| 5176 |
continue; |
| 5177 |
|
| 5178 |
case INT: |
| 5179 |
if (load_int(self) < 0) |
| 5180 |
break; |
| 5181 |
continue; |
| 5182 |
|
| 5183 |
case LONG: |
| 5184 |
if (load_long(self) < 0) |
| 5185 |
break; |
| 5186 |
continue; |
| 5187 |
|
| 5188 |
case LONG1: |
| 5189 |
if (load_counted_long(self, 1) < 0) |
| 5190 |
break; |
| 5191 |
continue; |
| 5192 |
|
| 5193 |
case LONG4: |
| 5194 |
if (load_counted_long(self, 4) < 0) |
| 5195 |
break; |
| 5196 |
continue; |
| 5197 |
|
| 5198 |
case FLOAT: |
| 5199 |
if (load_float(self) < 0) |
| 5200 |
break; |
| 5201 |
continue; |
| 5202 |
|
| 5203 |
case BINFLOAT: |
| 5204 |
if (load_binfloat(self) < 0) |
| 5205 |
break; |
| 5206 |
continue; |
| 5207 |
|
| 5208 |
case BINSTRING: |
| 5209 |
if (load_binstring(self) < 0) |
| 5210 |
break; |
| 5211 |
continue; |
| 5212 |
|
| 5213 |
case SHORT_BINSTRING: |
| 5214 |
if (load_short_binstring(self) < 0) |
| 5215 |
break; |
| 5216 |
continue; |
| 5217 |
|
| 5218 |
case STRING: |
| 5219 |
if (load_string(self) < 0) |
| 5220 |
break; |
| 5221 |
continue; |
| 5222 |
|
| 5223 |
#ifdef Py_USING_UNICODE |
| 5224 |
case UNICODE: |
| 5225 |
if (load_unicode(self) < 0) |
| 5226 |
break; |
| 5227 |
continue; |
| 5228 |
|
| 5229 |
case BINUNICODE: |
| 5230 |
if (load_binunicode(self) < 0) |
| 5231 |
break; |
| 5232 |
continue; |
| 5233 |
#endif |
| 5234 |
|
| 5235 |
case EMPTY_TUPLE: |
| 5236 |
if (load_counted_tuple(self, 0) < 0) |
| 5237 |
break; |
| 5238 |
continue; |
| 5239 |
|
| 5240 |
case TUPLE1: |
| 5241 |
if (load_counted_tuple(self, 1) < 0) |
| 5242 |
break; |
| 5243 |
continue; |
| 5244 |
|
| 5245 |
case TUPLE2: |
| 5246 |
if (load_counted_tuple(self, 2) < 0) |
| 5247 |
break; |
| 5248 |
continue; |
| 5249 |
|
| 5250 |
case TUPLE3: |
| 5251 |
if (load_counted_tuple(self, 3) < 0) |
| 5252 |
break; |
| 5253 |
continue; |
| 5254 |
|
| 5255 |
case TUPLE: |
| 5256 |
if (load_tuple(self) < 0) |
| 5257 |
break; |
| 5258 |
continue; |
| 5259 |
|
| 5260 |
case EMPTY_LIST: |
| 5261 |
if (load_empty_list(self) < 0) |
| 5262 |
break; |
| 5263 |
continue; |
| 5264 |
|
| 5265 |
case LIST: |
| 5266 |
if (load_list(self) < 0) |
| 5267 |
break; |
| 5268 |
continue; |
| 5269 |
|
| 5270 |
case EMPTY_DICT: |
| 5271 |
if (load_empty_dict(self) < 0) |
| 5272 |
break; |
| 5273 |
continue; |
| 5274 |
|
| 5275 |
case DICT: |
| 5276 |
if (load_dict(self) < 0) |
| 5277 |
break; |
| 5278 |
continue; |
| 5279 |
|
| 5280 |
case OBJ: |
| 5281 |
if (noload_obj(self) < 0) |
| 5282 |
break; |
| 5283 |
continue; |
| 5284 |
|
| 5285 |
case INST: |
| 5286 |
if (noload_inst(self) < 0) |
| 5287 |
break; |
| 5288 |
continue; |
| 5289 |
|
| 5290 |
case NEWOBJ: |
| 5291 |
if (noload_newobj(self) < 0) |
| 5292 |
break; |
| 5293 |
continue; |
| 5294 |
|
| 5295 |
case GLOBAL: |
| 5296 |
if (noload_global(self) < 0) |
| 5297 |
break; |
| 5298 |
continue; |
| 5299 |
|
| 5300 |
case APPEND: |
| 5301 |
if (noload_append(self) < 0) |
| 5302 |
break; |
| 5303 |
continue; |
| 5304 |
|
| 5305 |
case APPENDS: |
| 5306 |
if (noload_appends(self) < 0) |
| 5307 |
break; |
| 5308 |
continue; |
| 5309 |
|
| 5310 |
case BUILD: |
| 5311 |
if (noload_build(self) < 0) |
| 5312 |
break; |
| 5313 |
continue; |
| 5314 |
|
| 5315 |
case DUP: |
| 5316 |
if (load_dup(self) < 0) |
| 5317 |
break; |
| 5318 |
continue; |
| 5319 |
|
| 5320 |
case BINGET: |
| 5321 |
if (load_binget(self) < 0) |
| 5322 |
break; |
| 5323 |
continue; |
| 5324 |
|
| 5325 |
case LONG_BINGET: |
| 5326 |
if (load_long_binget(self) < 0) |
| 5327 |
break; |
| 5328 |
continue; |
| 5329 |
|
| 5330 |
case GET: |
| 5331 |
if (load_get(self) < 0) |
| 5332 |
break; |
| 5333 |
continue; |
| 5334 |
|
| 5335 |
case EXT1: |
| 5336 |
if (noload_extension(self, 1) < 0) |
| 5337 |
break; |
| 5338 |
continue; |
| 5339 |
|
| 5340 |
case EXT2: |
| 5341 |
if (noload_extension(self, 2) < 0) |
| 5342 |
break; |
| 5343 |
continue; |
| 5344 |
|
| 5345 |
case EXT4: |
| 5346 |
if (noload_extension(self, 4) < 0) |
| 5347 |
break; |
| 5348 |
continue; |
| 5349 |
|
| 5350 |
case MARK: |
| 5351 |
if (load_mark(self) < 0) |
| 5352 |
break; |
| 5353 |
continue; |
| 5354 |
|
| 5355 |
case BINPUT: |
| 5356 |
if (load_binput(self) < 0) |
| 5357 |
break; |
| 5358 |
continue; |
| 5359 |
|
| 5360 |
case LONG_BINPUT: |
| 5361 |
if (load_long_binput(self) < 0) |
| 5362 |
break; |
| 5363 |
continue; |
| 5364 |
|
| 5365 |
case PUT: |
| 5366 |
if (load_put(self) < 0) |
| 5367 |
break; |
| 5368 |
continue; |
| 5369 |
|
| 5370 |
case POP: |
| 5371 |
if (load_pop(self) < 0) |
| 5372 |
break; |
| 5373 |
continue; |
| 5374 |
|
| 5375 |
case POP_MARK: |
| 5376 |
if (load_pop_mark(self) < 0) |
| 5377 |
break; |
| 5378 |
continue; |
| 5379 |
|
| 5380 |
case SETITEM: |
| 5381 |
if (noload_setitem(self) < 0) |
| 5382 |
break; |
| 5383 |
continue; |
| 5384 |
|
| 5385 |
case SETITEMS: |
| 5386 |
if (noload_setitems(self) < 0) |
| 5387 |
break; |
| 5388 |
continue; |
| 5389 |
|
| 5390 |
case STOP: |
| 5391 |
break; |
| 5392 |
|
| 5393 |
case PERSID: |
| 5394 |
if (load_persid(self) < 0) |
| 5395 |
break; |
| 5396 |
continue; |
| 5397 |
|
| 5398 |
case BINPERSID: |
| 5399 |
if (load_binpersid(self) < 0) |
| 5400 |
break; |
| 5401 |
continue; |
| 5402 |
|
| 5403 |
case REDUCE: |
| 5404 |
if (noload_reduce(self) < 0) |
| 5405 |
break; |
| 5406 |
continue; |
| 5407 |
|
| 5408 |
case PROTO: |
| 5409 |
if (load_proto(self) < 0) |
| 5410 |
break; |
| 5411 |
continue; |
| 5412 |
|
| 5413 |
case NEWTRUE: |
| 5414 |
if (load_bool(self, Py_True) < 0) |
| 5415 |
break; |
| 5416 |
continue; |
| 5417 |
|
| 5418 |
case NEWFALSE: |
| 5419 |
if (load_bool(self, Py_False) < 0) |
| 5420 |
break; |
| 5421 |
continue; |
| 5422 |
default: |
| 5423 |
cPickle_ErrFormat(UnpicklingError, |
| 5424 |
"invalid load key, '%s'.", |
| 5425 |
"c", s[0]); |
| 5426 |
return NULL; |
| 5427 |
} |
| 5428 |
|
| 5429 |
break; |
| 5430 |
} |
| 5431 |
|
| 5432 |
if ((err = PyErr_Occurred())) { |
| 5433 |
if (err == PyExc_EOFError) { |
| 5434 |
PyErr_SetNone(PyExc_EOFError); |
| 5435 |
} |
| 5436 |
return NULL; |
| 5437 |
} |
| 5438 |
|
| 5439 |
PDATA_POP(self->stack, val); |
| 5440 |
return val; |
| 5441 |
} |
| 5442 |
|
| 5443 |
|
| 5444 |
static PyObject * |
| 5445 |
Unpickler_load(Unpicklerobject *self, PyObject *unused) |
| 5446 |
{ |
| 5447 |
return load(self); |
| 5448 |
} |
| 5449 |
|
| 5450 |
static PyObject * |
| 5451 |
Unpickler_noload(Unpicklerobject *self, PyObject *unused) |
| 5452 |
{ |
| 5453 |
return noload(self); |
| 5454 |
} |
| 5455 |
|
| 5456 |
|
| 5457 |
static struct PyMethodDef Unpickler_methods[] = { |
| 5458 |
{"load", (PyCFunction)Unpickler_load, METH_NOARGS, |
| 5459 |
PyDoc_STR("load() -- Load a pickle") |
| 5460 |
}, |
| 5461 |
{"noload", (PyCFunction)Unpickler_noload, METH_NOARGS, |
| 5462 |
PyDoc_STR( |
| 5463 |
"noload() -- not load a pickle, but go through most of the motions\n" |
| 5464 |
"\n" |
| 5465 |
"This function can be used to read past a pickle without instantiating\n" |
| 5466 |
"any objects or importing any modules. It can also be used to find all\n" |
| 5467 |
"persistent references without instantiating any objects or importing\n" |
| 5468 |
"any modules.\n") |
| 5469 |
}, |
| 5470 |
{NULL, NULL} |
| 5471 |
}; |
| 5472 |
|
| 5473 |
|
| 5474 |
static Unpicklerobject * |
| 5475 |
newUnpicklerobject(PyObject *f) |
| 5476 |
{ |
| 5477 |
Unpicklerobject *self; |
| 5478 |
|
| 5479 |
if (!( self = PyObject_GC_New(Unpicklerobject, &Unpicklertype))) |
| 5480 |
return NULL; |
| 5481 |
|
| 5482 |
self->file = NULL; |
| 5483 |
self->arg = NULL; |
| 5484 |
self->stack = (Pdata*)Pdata_New(); |
| 5485 |
self->pers_func = NULL; |
| 5486 |
self->last_string = NULL; |
| 5487 |
self->marks = NULL; |
| 5488 |
self->num_marks = 0; |
| 5489 |
self->marks_size = 0; |
| 5490 |
self->buf_size = 0; |
| 5491 |
self->read = NULL; |
| 5492 |
self->readline = NULL; |
| 5493 |
self->find_class = NULL; |
| 5494 |
|
| 5495 |
if (!( self->memo = PyDict_New())) |
| 5496 |
goto err; |
| 5497 |
|
| 5498 |
if (!self->stack) |
| 5499 |
goto err; |
| 5500 |
|
| 5501 |
Py_INCREF(f); |
| 5502 |
self->file = f; |
| 5503 |
|
| 5504 |
|
| 5505 |
if (PyFile_Check(f)) { |
| 5506 |
self->fp = PyFile_AsFile(f); |
| 5507 |
if (self->fp == NULL) { |
| 5508 |
PyErr_SetString(PyExc_ValueError, |
| 5509 |
"I/O operation on closed file"); |
| 5510 |
goto err; |
| 5511 |
} |
| 5512 |
self->read_func = read_file; |
| 5513 |
self->readline_func = readline_file; |
| 5514 |
} |
| 5515 |
else if (PycStringIO_InputCheck(f)) { |
| 5516 |
self->fp = NULL; |
| 5517 |
self->read_func = read_cStringIO; |
| 5518 |
self->readline_func = readline_cStringIO; |
| 5519 |
} |
| 5520 |
else { |
| 5521 |
|
| 5522 |
self->fp = NULL; |
| 5523 |
self->read_func = read_other; |
| 5524 |
self->readline_func = readline_other; |
| 5525 |
|
| 5526 |
if (!( (self->readline = PyObject_GetAttr(f, readline_str)) && |
| 5527 |
(self->read = PyObject_GetAttr(f, read_str)))) { |
| 5528 |
PyErr_Clear(); |
| 5529 |
PyErr_SetString( PyExc_TypeError, |
| 5530 |
"argument must have 'read' and " |
| 5531 |
"'readline' attributes" ); |
| 5532 |
goto err; |
| 5533 |
} |
| 5534 |
} |
| 5535 |
PyObject_GC_Track(self); |
| 5536 |
|
| 5537 |
return self; |
| 5538 |
|
| 5539 |
err: |
| 5540 |
Py_DECREF((PyObject *)self); |
| 5541 |
return NULL; |
| 5542 |
} |
| 5543 |
|
| 5544 |
|
| 5545 |
static PyObject * |
| 5546 |
get_Unpickler(PyObject *self, PyObject *file) |
| 5547 |
{ |
| 5548 |
return (PyObject *)newUnpicklerobject(file); |
| 5549 |
} |
| 5550 |
|
| 5551 |
|
| 5552 |
static void |
| 5553 |
Unpickler_dealloc(Unpicklerobject *self) |
| 5554 |
{ |
| 5555 |
PyObject_GC_UnTrack((PyObject *)self); |
| 5556 |
Py_XDECREF(self->readline); |
| 5557 |
Py_XDECREF(self->read); |
| 5558 |
Py_XDECREF(self->file); |
| 5559 |
Py_XDECREF(self->memo); |
| 5560 |
Py_XDECREF(self->stack); |
| 5561 |
Py_XDECREF(self->pers_func); |
| 5562 |
Py_XDECREF(self->arg); |
| 5563 |
Py_XDECREF(self->last_string); |
| 5564 |
Py_XDECREF(self->find_class); |
| 5565 |
|
| 5566 |
if (self->marks) { |
| 5567 |
free(self->marks); |
| 5568 |
} |
| 5569 |
|
| 5570 |
if (self->buf_size) { |
| 5571 |
free(self->buf); |
| 5572 |
} |
| 5573 |
|
| 5574 |
Py_TYPE(self)->tp_free((PyObject *)self); |
| 5575 |
} |
| 5576 |
|
| 5577 |
static int |
| 5578 |
Unpickler_traverse(Unpicklerobject *self, visitproc visit, void *arg) |
| 5579 |
{ |
| 5580 |
Py_VISIT(self->readline); |
| 5581 |
Py_VISIT(self->read); |
| 5582 |
Py_VISIT(self->file); |
| 5583 |
Py_VISIT(self->memo); |
| 5584 |
Py_VISIT(self->stack); |
| 5585 |
Py_VISIT(self->pers_func); |
| 5586 |
Py_VISIT(self->arg); |
| 5587 |
Py_VISIT(self->last_string); |
| 5588 |
Py_VISIT(self->find_class); |
| 5589 |
return 0; |
| 5590 |
} |
| 5591 |
|
| 5592 |
static int |
| 5593 |
Unpickler_clear(Unpicklerobject *self) |
| 5594 |
{ |
| 5595 |
Py_CLEAR(self->readline); |
| 5596 |
Py_CLEAR(self->read); |
| 5597 |
Py_CLEAR(self->file); |
| 5598 |
Py_CLEAR(self->memo); |
| 5599 |
Py_CLEAR(self->stack); |
| 5600 |
Py_CLEAR(self->pers_func); |
| 5601 |
Py_CLEAR(self->arg); |
| 5602 |
Py_CLEAR(self->last_string); |
| 5603 |
Py_CLEAR(self->find_class); |
| 5604 |
return 0; |
| 5605 |
} |
| 5606 |
|
| 5607 |
static PyObject * |
| 5608 |
Unpickler_getattr(Unpicklerobject *self, char *name) |
| 5609 |
{ |
| 5610 |
if (!strcmp(name, "persistent_load")) { |
| 5611 |
if (!self->pers_func) { |
| 5612 |
PyErr_SetString(PyExc_AttributeError, name); |
| 5613 |
return NULL; |
| 5614 |
} |
| 5615 |
|
| 5616 |
Py_INCREF(self->pers_func); |
| 5617 |
return self->pers_func; |
| 5618 |
} |
| 5619 |
|
| 5620 |
if (!strcmp(name, "find_global")) { |
| 5621 |
if (!self->find_class) { |
| 5622 |
PyErr_SetString(PyExc_AttributeError, name); |
| 5623 |
return NULL; |
| 5624 |
} |
| 5625 |
|
| 5626 |
Py_INCREF(self->find_class); |
| 5627 |
return self->find_class; |
| 5628 |
} |
| 5629 |
|
| 5630 |
if (!strcmp(name, "memo")) { |
| 5631 |
if (!self->memo) { |
| 5632 |
PyErr_SetString(PyExc_AttributeError, name); |
| 5633 |
return NULL; |
| 5634 |
} |
| 5635 |
|
| 5636 |
Py_INCREF(self->memo); |
| 5637 |
return self->memo; |
| 5638 |
} |
| 5639 |
|
| 5640 |
if (!strcmp(name, "UnpicklingError")) { |
| 5641 |
Py_INCREF(UnpicklingError); |
| 5642 |
return UnpicklingError; |
| 5643 |
} |
| 5644 |
|
| 5645 |
return Py_FindMethod(Unpickler_methods, (PyObject *)self, name); |
| 5646 |
} |
| 5647 |
|
| 5648 |
|
| 5649 |
static int |
| 5650 |
Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value) |
| 5651 |
{ |
| 5652 |
|
| 5653 |
if (!strcmp(name, "persistent_load")) { |
| 5654 |
Py_XINCREF(value); |
| 5655 |
Py_XSETREF(self->pers_func, value); |
| 5656 |
return 0; |
| 5657 |
} |
| 5658 |
|
| 5659 |
if (!strcmp(name, "find_global")) { |
| 5660 |
Py_XINCREF(value); |
| 5661 |
Py_XSETREF(self->find_class, value); |
| 5662 |
return 0; |
| 5663 |
} |
| 5664 |
|
| 5665 |
if (! value) { |
| 5666 |
PyErr_SetString(PyExc_TypeError, |
| 5667 |
"attribute deletion is not supported"); |
| 5668 |
return -1; |
| 5669 |
} |
| 5670 |
|
| 5671 |
if (strcmp(name, "memo") == 0) { |
| 5672 |
if (!PyDict_Check(value)) { |
| 5673 |
PyErr_SetString(PyExc_TypeError, |
| 5674 |
"memo must be a dictionary"); |
| 5675 |
return -1; |
| 5676 |
} |
| 5677 |
Py_INCREF(value); |
| 5678 |
Py_XSETREF(self->memo, value); |
| 5679 |
return 0; |
| 5680 |
} |
| 5681 |
|
| 5682 |
PyErr_SetString(PyExc_AttributeError, name); |
| 5683 |
return -1; |
| 5684 |
} |
| 5685 |
|
| 5686 |
|
| 5687 |
|
| 5688 |
|
| 5689 |
|
| 5690 |
|
| 5691 |
static PyObject * |
| 5692 |
cpm_dump(PyObject *self, PyObject *args, PyObject *kwds) |
| 5693 |
{ |
| 5694 |
static char *kwlist[] = {"obj", "file", "protocol", NULL}; |
| 5695 |
PyObject *ob, *file, *res = NULL; |
| 5696 |
Picklerobject *pickler = 0; |
| 5697 |
int proto = 0; |
| 5698 |
|
| 5699 |
if (!( PyArg_ParseTupleAndKeywords(args, kwds, "OO|i", kwlist, |
| 5700 |
&ob, &file, &proto))) |
| 5701 |
goto finally; |
| 5702 |
|
| 5703 |
if (!( pickler = newPicklerobject(file, proto))) |
| 5704 |
goto finally; |
| 5705 |
|
| 5706 |
if (dump(pickler, ob) < 0) |
| 5707 |
goto finally; |
| 5708 |
|
| 5709 |
Py_INCREF(Py_None); |
| 5710 |
res = Py_None; |
| 5711 |
|
| 5712 |
finally: |
| 5713 |
Py_XDECREF(pickler); |
| 5714 |
|
| 5715 |
return res; |
| 5716 |
} |
| 5717 |
|
| 5718 |
|
| 5719 |
|
| 5720 |
static PyObject * |
| 5721 |
cpm_dumps(PyObject *self, PyObject *args, PyObject *kwds) |
| 5722 |
{ |
| 5723 |
static char *kwlist[] = {"obj", "protocol", NULL}; |
| 5724 |
PyObject *ob, *file = 0, *res = NULL; |
| 5725 |
Picklerobject *pickler = 0; |
| 5726 |
int proto = 0; |
| 5727 |
|
| 5728 |
if (!( PyArg_ParseTupleAndKeywords(args, kwds, "O|i:dumps", kwlist, |
| 5729 |
&ob, &proto))) |
| 5730 |
goto finally; |
| 5731 |
|
| 5732 |
if (!( file = PycStringIO->NewOutput(128))) |
| 5733 |
goto finally; |
| 5734 |
|
| 5735 |
if (!( pickler = newPicklerobject(file, proto))) |
| 5736 |
goto finally; |
| 5737 |
|
| 5738 |
if (dump(pickler, ob) < 0) |
| 5739 |
goto finally; |
| 5740 |
|
| 5741 |
res = PycStringIO->cgetvalue(file); |
| 5742 |
|
| 5743 |
finally: |
| 5744 |
Py_XDECREF(pickler); |
| 5745 |
Py_XDECREF(file); |
| 5746 |
|
| 5747 |
return res; |
| 5748 |
} |
| 5749 |
|
| 5750 |
|
| 5751 |
|
| 5752 |
static PyObject * |
| 5753 |
cpm_load(PyObject *self, PyObject *ob) |
| 5754 |
{ |
| 5755 |
Unpicklerobject *unpickler = 0; |
| 5756 |
PyObject *res = NULL; |
| 5757 |
|
| 5758 |
if (!( unpickler = newUnpicklerobject(ob))) |
| 5759 |
goto finally; |
| 5760 |
|
| 5761 |
res = load(unpickler); |
| 5762 |
|
| 5763 |
finally: |
| 5764 |
Py_XDECREF(unpickler); |
| 5765 |
|
| 5766 |
return res; |
| 5767 |
} |
| 5768 |
|
| 5769 |
|
| 5770 |
|
| 5771 |
static PyObject * |
| 5772 |
cpm_loads(PyObject *self, PyObject *args) |
| 5773 |
{ |
| 5774 |
PyObject *ob, *file = 0, *res = NULL; |
| 5775 |
Unpicklerobject *unpickler = 0; |
| 5776 |
|
| 5777 |
if (!( PyArg_ParseTuple(args, "S:loads", &ob))) |
| 5778 |
goto finally; |
| 5779 |
|
| 5780 |
if (!( file = PycStringIO->NewInput(ob))) |
| 5781 |
goto finally; |
| 5782 |
|
| 5783 |
if (!( unpickler = newUnpicklerobject(file))) |
| 5784 |
goto finally; |
| 5785 |
|
| 5786 |
res = load(unpickler); |
| 5787 |
|
| 5788 |
finally: |
| 5789 |
Py_XDECREF(file); |
| 5790 |
Py_XDECREF(unpickler); |
| 5791 |
|
| 5792 |
return res; |
| 5793 |
} |
| 5794 |
|
| 5795 |
|
| 5796 |
PyDoc_STRVAR(Unpicklertype__doc__, |
| 5797 |
"Objects that know how to unpickle"); |
| 5798 |
|
| 5799 |
static PyTypeObject Unpicklertype = { |
| 5800 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 5801 |
"cPickle.Unpickler", |
| 5802 |
sizeof(Unpicklerobject), |
| 5803 |
0, |
| 5804 |
(destructor)Unpickler_dealloc, |
| 5805 |
0, |
| 5806 |
(getattrfunc)Unpickler_getattr, |
| 5807 |
(setattrfunc)Unpickler_setattr, |
| 5808 |
0, |
| 5809 |
0, |
| 5810 |
0, |
| 5811 |
0, |
| 5812 |
0, |
| 5813 |
0, |
| 5814 |
0, |
| 5815 |
0, |
| 5816 |
0, |
| 5817 |
0, |
| 5818 |
0, |
| 5819 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 5820 |
Unpicklertype__doc__, |
| 5821 |
(traverseproc)Unpickler_traverse, |
| 5822 |
(inquiry)Unpickler_clear, |
| 5823 |
}; |
| 5824 |
|
| 5825 |
static struct PyMethodDef cPickle_methods[] = { |
| 5826 |
{"dump", (PyCFunction)cpm_dump, METH_VARARGS | METH_KEYWORDS, |
| 5827 |
PyDoc_STR("dump(obj, file, protocol=0) -- " |
| 5828 |
"Write an object in pickle format to the given file.\n" |
| 5829 |
"\n" |
| 5830 |
"See the Pickler docstring for the meaning of optional argument proto.") |
| 5831 |
}, |
| 5832 |
|
| 5833 |
{"dumps", (PyCFunction)cpm_dumps, METH_VARARGS | METH_KEYWORDS, |
| 5834 |
PyDoc_STR("dumps(obj, protocol=0) -- " |
| 5835 |
"Return a string containing an object in pickle format.\n" |
| 5836 |
"\n" |
| 5837 |
"See the Pickler docstring for the meaning of optional argument proto.") |
| 5838 |
}, |
| 5839 |
|
| 5840 |
{"load", (PyCFunction)cpm_load, METH_O, |
| 5841 |
PyDoc_STR("load(file) -- Load a pickle from the given file")}, |
| 5842 |
|
| 5843 |
{"loads", (PyCFunction)cpm_loads, METH_VARARGS, |
| 5844 |
PyDoc_STR("loads(string) -- Load a pickle from the given string")}, |
| 5845 |
|
| 5846 |
{"Pickler", (PyCFunction)get_Pickler, METH_VARARGS | METH_KEYWORDS, |
| 5847 |
PyDoc_STR("Pickler(file, protocol=0) -- Create a pickler.\n" |
| 5848 |
"\n" |
| 5849 |
"This takes a file-like object for writing a pickle data stream.\n" |
| 5850 |
"The optional proto argument tells the pickler to use the given\n" |
| 5851 |
"protocol; supported protocols are 0, 1, 2. The default\n" |
| 5852 |
"protocol is 0, to be backwards compatible. (Protocol 0 is the\n" |
| 5853 |
"only protocol that can be written to a file opened in text\n" |
| 5854 |
"mode and read back successfully. When using a protocol higher\n" |
| 5855 |
"than 0, make sure the file is opened in binary mode, both when\n" |
| 5856 |
"pickling and unpickling.)\n" |
| 5857 |
"\n" |
| 5858 |
"Protocol 1 is more efficient than protocol 0; protocol 2 is\n" |
| 5859 |
"more efficient than protocol 1.\n" |
| 5860 |
"\n" |
| 5861 |
"Specifying a negative protocol version selects the highest\n" |
| 5862 |
"protocol version supported. The higher the protocol used, the\n" |
| 5863 |
"more recent the version of Python needed to read the pickle\n" |
| 5864 |
"produced.\n" |
| 5865 |
"\n" |
| 5866 |
"The file parameter must have a write() method that accepts a single\n" |
| 5867 |
"string argument. It can thus be an open file object, a StringIO\n" |
| 5868 |
"object, or any other custom object that meets this interface.\n") |
| 5869 |
}, |
| 5870 |
|
| 5871 |
{"Unpickler", (PyCFunction)get_Unpickler, METH_O, |
| 5872 |
PyDoc_STR("Unpickler(file) -- Create an unpickler.")}, |
| 5873 |
|
| 5874 |
{ NULL, NULL } |
| 5875 |
}; |
| 5876 |
|
| 5877 |
static int |
| 5878 |
init_stuff(PyObject *module_dict) |
| 5879 |
{ |
| 5880 |
PyObject *copyreg, *t, *r; |
| 5881 |
|
| 5882 |
#define INIT_STR(S) if (!( S ## _str=PyString_InternFromString(#S))) return -1; |
| 5883 |
|
| 5884 |
if (PyType_Ready(&Unpicklertype) < 0) |
| 5885 |
return -1; |
| 5886 |
if (PyType_Ready(&Picklertype) < 0) |
| 5887 |
return -1; |
| 5888 |
|
| 5889 |
INIT_STR(__class__); |
| 5890 |
INIT_STR(__getinitargs__); |
| 5891 |
INIT_STR(__dict__); |
| 5892 |
INIT_STR(__getstate__); |
| 5893 |
INIT_STR(__setstate__); |
| 5894 |
INIT_STR(__name__); |
| 5895 |
INIT_STR(__main__); |
| 5896 |
INIT_STR(__reduce__); |
| 5897 |
INIT_STR(__reduce_ex__); |
| 5898 |
INIT_STR(write); |
| 5899 |
INIT_STR(append); |
| 5900 |
INIT_STR(read); |
| 5901 |
INIT_STR(readline); |
| 5902 |
INIT_STR(dispatch_table); |
| 5903 |
|
| 5904 |
if (!( copyreg = PyImport_ImportModule("copy_reg"))) |
| 5905 |
return -1; |
| 5906 |
|
| 5907 |
|
| 5908 |
|
| 5909 |
dispatch_table = PyObject_GetAttr(copyreg, dispatch_table_str); |
| 5910 |
if (!dispatch_table) return -1; |
| 5911 |
|
| 5912 |
extension_registry = PyObject_GetAttrString(copyreg, |
| 5913 |
"_extension_registry"); |
| 5914 |
if (!extension_registry) return -1; |
| 5915 |
|
| 5916 |
inverted_registry = PyObject_GetAttrString(copyreg, |
| 5917 |
"_inverted_registry"); |
| 5918 |
if (!inverted_registry) return -1; |
| 5919 |
|
| 5920 |
extension_cache = PyObject_GetAttrString(copyreg, |
| 5921 |
"_extension_cache"); |
| 5922 |
if (!extension_cache) return -1; |
| 5923 |
|
| 5924 |
Py_DECREF(copyreg); |
| 5925 |
|
| 5926 |
if (!(empty_tuple = PyTuple_New(0))) |
| 5927 |
return -1; |
| 5928 |
|
| 5929 |
two_tuple = PyTuple_New(2); |
| 5930 |
if (two_tuple == NULL) |
| 5931 |
return -1; |
| 5932 |
|
| 5933 |
|
| 5934 |
|
| 5935 |
|
| 5936 |
PyObject_GC_UnTrack(two_tuple); |
| 5937 |
|
| 5938 |
|
| 5939 |
if (!( t=PyImport_ImportModule("__builtin__"))) return -1; |
| 5940 |
if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0) |
| 5941 |
return -1; |
| 5942 |
|
| 5943 |
if (!( t=PyDict_New())) return -1; |
| 5944 |
if (!( r=PyRun_String( |
| 5945 |
"def __str__(self):\n" |
| 5946 |
" return self.args and ('%s' % self.args[0]) or '(what)'\n", |
| 5947 |
Py_file_input, |
| 5948 |
module_dict, t) )) return -1; |
| 5949 |
Py_DECREF(r); |
| 5950 |
|
| 5951 |
PickleError = PyErr_NewException("cPickle.PickleError", NULL, t); |
| 5952 |
if (!PickleError) |
| 5953 |
return -1; |
| 5954 |
|
| 5955 |
Py_DECREF(t); |
| 5956 |
|
| 5957 |
PicklingError = PyErr_NewException("cPickle.PicklingError", |
| 5958 |
PickleError, NULL); |
| 5959 |
if (!PicklingError) |
| 5960 |
return -1; |
| 5961 |
|
| 5962 |
if (!( t=PyDict_New())) return -1; |
| 5963 |
if (!( r=PyRun_String( |
| 5964 |
"def __str__(self):\n" |
| 5965 |
" a=self.args\n" |
| 5966 |
" a=a and type(a[0]) or '(what)'\n" |
| 5967 |
" return 'Cannot pickle %s objects' % a\n" |
| 5968 |
, Py_file_input, |
| 5969 |
module_dict, t) )) return -1; |
| 5970 |
Py_DECREF(r); |
| 5971 |
|
| 5972 |
if (!( UnpickleableError = PyErr_NewException( |
| 5973 |
"cPickle.UnpickleableError", PicklingError, t))) |
| 5974 |
return -1; |
| 5975 |
|
| 5976 |
Py_DECREF(t); |
| 5977 |
|
| 5978 |
if (!( UnpicklingError = PyErr_NewException("cPickle.UnpicklingError", |
| 5979 |
PickleError, NULL))) |
| 5980 |
return -1; |
| 5981 |
|
| 5982 |
if (!( BadPickleGet = PyErr_NewException("cPickle.BadPickleGet", |
| 5983 |
UnpicklingError, NULL))) |
| 5984 |
return -1; |
| 5985 |
|
| 5986 |
if (PyDict_SetItemString(module_dict, "PickleError", |
| 5987 |
PickleError) < 0) |
| 5988 |
return -1; |
| 5989 |
|
| 5990 |
if (PyDict_SetItemString(module_dict, "PicklingError", |
| 5991 |
PicklingError) < 0) |
| 5992 |
return -1; |
| 5993 |
|
| 5994 |
if (PyDict_SetItemString(module_dict, "UnpicklingError", |
| 5995 |
UnpicklingError) < 0) |
| 5996 |
return -1; |
| 5997 |
|
| 5998 |
if (PyDict_SetItemString(module_dict, "UnpickleableError", |
| 5999 |
UnpickleableError) < 0) |
| 6000 |
return -1; |
| 6001 |
|
| 6002 |
if (PyDict_SetItemString(module_dict, "BadPickleGet", |
| 6003 |
BadPickleGet) < 0) |
| 6004 |
return -1; |
| 6005 |
|
| 6006 |
PycString_IMPORT; |
| 6007 |
|
| 6008 |
return 0; |
| 6009 |
} |
| 6010 |
|
| 6011 |
#ifndef PyMODINIT_FUNC |
| 6012 |
#define PyMODINIT_FUNC void |
| 6013 |
#endif |
| 6014 |
PyMODINIT_FUNC |
| 6015 |
initcPickle(void) |
| 6016 |
{ |
| 6017 |
PyObject *m, *d, *di, *v, *k; |
| 6018 |
Py_ssize_t i; |
| 6019 |
char *rev = "1.71"; |
| 6020 |
PyObject *format_version; |
| 6021 |
PyObject *compatible_formats; |
| 6022 |
|
| 6023 |
Py_TYPE(&Picklertype) = &PyType_Type; |
| 6024 |
Py_TYPE(&Unpicklertype) = &PyType_Type; |
| 6025 |
Py_TYPE(&PdataType) = &PyType_Type; |
| 6026 |
|
| 6027 |
|
| 6028 |
|
| 6029 |
|
| 6030 |
di = PyDict_New(); |
| 6031 |
if (!di) return; |
| 6032 |
if (init_stuff(di) < 0) return; |
| 6033 |
|
| 6034 |
|
| 6035 |
m = Py_InitModule4("cPickle", cPickle_methods, |
| 6036 |
cPickle_module_documentation, |
| 6037 |
(PyObject*)NULL,PYTHON_API_VERSION); |
| 6038 |
if (m == NULL) |
| 6039 |
return; |
| 6040 |
|
| 6041 |
|
| 6042 |
d = PyModule_GetDict(m); |
| 6043 |
v = PyString_FromString(rev); |
| 6044 |
PyDict_SetItemString(d, "__version__", v); |
| 6045 |
Py_XDECREF(v); |
| 6046 |
|
| 6047 |
|
| 6048 |
for (i=0; PyDict_Next(di, &i, &k, &v); ) { |
| 6049 |
if (PyObject_SetItem(d, k, v) < 0) { |
| 6050 |
Py_DECREF(di); |
| 6051 |
return; |
| 6052 |
} |
| 6053 |
} |
| 6054 |
Py_DECREF(di); |
| 6055 |
|
| 6056 |
i = PyModule_AddIntConstant(m, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL); |
| 6057 |
if (i < 0) |
| 6058 |
return; |
| 6059 |
|
| 6060 |
|
| 6061 |
|
| 6062 |
format_version = PyString_FromString("2.0"); |
| 6063 |
|
| 6064 |
compatible_formats = Py_BuildValue("[sssss]", |
| 6065 |
"1.0", |
| 6066 |
"1.1", |
| 6067 |
"1.2", |
| 6068 |
"1.3", |
| 6069 |
"2.0"); |
| 6070 |
PyDict_SetItemString(d, "format_version", format_version); |
| 6071 |
PyDict_SetItemString(d, "compatible_formats", compatible_formats); |
| 6072 |
Py_XDECREF(format_version); |
| 6073 |
Py_XDECREF(compatible_formats); |
| 6074 |
} |