Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1 | /* |
| 2 | $Id$ |
| 3 | |
| 4 | Copyright |
| 5 | |
| 6 | Copyright 1996 Digital Creations, L.C., 910 Princess Anne |
| 7 | Street, Suite 300, Fredericksburg, Virginia 22401 U.S.A. All |
| 8 | rights reserved. Copyright in this software is owned by DCLC, |
| 9 | unless otherwise indicated. Permission to use, copy and |
| 10 | distribute this software is hereby granted, provided that the |
| 11 | above copyright notice appear in all copies and that both that |
| 12 | copyright notice and this permission notice appear. Note that |
| 13 | any product, process or technology described in this software |
| 14 | may be the subject of other Intellectual Property rights |
| 15 | reserved by Digital Creations, L.C. and are not licensed |
| 16 | hereunder. |
| 17 | |
| 18 | Trademarks |
| 19 | |
| 20 | Digital Creations & DCLC, are trademarks of Digital Creations, L.C.. |
| 21 | All other trademarks are owned by their respective companies. |
| 22 | |
| 23 | No Warranty |
| 24 | |
| 25 | The software is provided "as is" without warranty of any kind, |
| 26 | either express or implied, including, but not limited to, the |
| 27 | implied warranties of merchantability, fitness for a particular |
| 28 | purpose, or non-infringement. This software could include |
| 29 | technical inaccuracies or typographical errors. Changes are |
| 30 | periodically made to the software; these changes will be |
| 31 | incorporated in new editions of the software. DCLC may make |
| 32 | improvements and/or changes in this software at any time |
| 33 | without notice. |
| 34 | |
| 35 | Limitation Of Liability |
| 36 | |
| 37 | In no event will DCLC be liable for direct, indirect, special, |
| 38 | incidental, economic, cover, or consequential damages arising |
| 39 | out of the use of or inability to use this software even if |
| 40 | advised of the possibility of such damages. Some states do not |
| 41 | allow the exclusion or limitation of implied warranties or |
| 42 | limitation of liability for incidental or consequential |
| 43 | damages, so the above limitation or exclusion may not apply to |
| 44 | you. |
| 45 | |
| 46 | If you have questions regarding this software, |
| 47 | contact: |
| 48 | |
| 49 | Jim Fulton, jim@digicool.com |
| 50 | Digital Creations L.C. |
| 51 | |
| 52 | (540) 371-6909 |
| 53 | */ |
| 54 | |
| 55 | static char cPickle_module_documentation[] = |
| 56 | "" |
| 57 | ; |
| 58 | |
| 59 | #include "Python.h" |
| 60 | #include "cStringIO.h" |
| 61 | #include "graminit.h" |
| 62 | |
| 63 | #include <errno.h> |
| 64 | |
| 65 | static PyObject *ErrorObject; |
| 66 | |
| 67 | #ifdef __cplusplus |
| 68 | #define ARG(T, N) T N |
| 69 | #define ARGDECL(T, N) |
| 70 | #else |
| 71 | #define ARG(T, N) N |
| 72 | #define ARGDECL(T, N) T N; |
| 73 | #endif |
| 74 | |
| 75 | #define UNLESS(E) if (!(E)) |
| 76 | #define ASSIGN(V,E) {PyObject *__e; __e=(E); Py_XDECREF(V); (V)=__e;} |
| 77 | #define UNLESS_ASSIGN(V,E) ASSIGN(V,E) UNLESS(V) |
| 78 | |
| 79 | #define DEL_LIST_SLICE(list, from, to) \ |
| 80 | (PyList_SetSlice(list, from, to, empty_list)) |
| 81 | |
| 82 | #define APPEND 'a' |
| 83 | #define BUILD 'b' |
| 84 | #define DUP '2' |
| 85 | #define GET 'g' |
| 86 | #define BINGET 'h' |
| 87 | #define INST 'i' |
| 88 | #define OBJ 'o' |
| 89 | #define MARK '(' |
| 90 | static char MARKv = MARK; |
| 91 | #define PUT 'p' |
| 92 | #define BINPUT 'q' |
| 93 | #define POP '0' |
| 94 | #define SETITEM 's' |
| 95 | #define STOP '.' |
| 96 | #define CLASS 'c' |
| 97 | #define DICT 'd' |
| 98 | #define LIST 'l' |
| 99 | #define TUPLE 't' |
| 100 | #define NONE 'N' |
| 101 | #define INT 'I' |
| 102 | #define BININT 'J' |
| 103 | #define BININT1 'K' |
| 104 | #define BININT2 'M' |
| 105 | #define BININT3 'O' |
| 106 | #define LONG 'L' |
| 107 | #define FLOAT 'F' |
| 108 | #define STRING 'S' |
| 109 | #define BINSTRING 'T' |
| 110 | #define SHORT_BINSTRING 'U' |
| 111 | #define PERSID 'P' |
| 112 | |
| 113 | |
| 114 | PyTypeObject *BuiltinFunctionType; |
| 115 | |
| 116 | /* atol function from string module */ |
| 117 | static PyObject *atol_func; |
| 118 | |
| 119 | static PyObject *PicklingError; |
| 120 | static PyObject *UnpicklingError; |
| 121 | |
| 122 | static PyObject *class_map; |
| 123 | static PyObject *empty_list, *empty_tuple; |
| 124 | |
| 125 | static PyObject *save(); |
| 126 | |
| 127 | |
| 128 | typedef struct |
| 129 | { |
| 130 | PyObject_HEAD |
| 131 | FILE *fp; |
| 132 | PyObject *write; |
| 133 | PyObject *file; |
| 134 | PyObject *memo; |
| 135 | PyObject *arg; |
| 136 | PyObject *pers_func; |
| 137 | char *mark; |
| 138 | int bin; |
| 139 | int (*write_func)(); |
| 140 | } Picklerobject; |
| 141 | |
| 142 | staticforward PyTypeObject Picklertype; |
| 143 | |
| 144 | |
| 145 | typedef struct |
| 146 | { |
| 147 | PyObject_HEAD |
| 148 | FILE *fp; |
| 149 | PyObject *file; |
| 150 | PyObject *readline; |
| 151 | PyObject *read; |
| 152 | PyObject *memo; |
| 153 | PyObject *arg; |
| 154 | PyObject *stack; |
| 155 | PyObject *mark; |
| 156 | PyObject *pers_func; |
| 157 | int *marks; |
| 158 | int num_marks; |
| 159 | int marks_size; |
| 160 | int (*read_func)(); |
| 161 | int (*readline_func)(); |
| 162 | } Unpicklerobject; |
| 163 | |
| 164 | staticforward PyTypeObject Unpicklertype; |
| 165 | |
| 166 | |
| 167 | static int |
| 168 | write_file(ARG(Picklerobject *, self), ARG(char *, s), ARG(int, n)) |
| 169 | ARGDECL(Picklerobject *, self) |
| 170 | ARGDECL(char *, s) |
| 171 | ARGDECL(int, n) |
| 172 | { |
| 173 | if (fwrite(s, sizeof(char), n, self->fp) != n) |
| 174 | { |
| 175 | PyErr_SetFromErrno(PyExc_IOError); |
| 176 | return -1; |
| 177 | } |
| 178 | |
| 179 | return n; |
| 180 | } |
| 181 | |
| 182 | |
| 183 | static int |
| 184 | write_cStringIO(ARG(Picklerobject *, self), ARG(char *, s), ARG(int, n)) |
| 185 | ARGDECL(Picklerobject *, self) |
| 186 | ARGDECL(char *, s) |
| 187 | ARGDECL(int, n) |
| 188 | { |
| 189 | if ((*PycStringIO_cwrite)((PyObject *)self->file, s, n) != n) |
| 190 | { |
| 191 | return -1; |
| 192 | } |
| 193 | |
| 194 | return n; |
| 195 | } |
| 196 | |
| 197 | |
| 198 | static int |
| 199 | write_other(ARG(Picklerobject *, self), ARG(char *, s), ARG(int, n)) |
| 200 | ARGDECL(Picklerobject *, self) |
| 201 | ARGDECL(char *, s) |
| 202 | ARGDECL(int, n) |
| 203 | { |
| 204 | PyObject *py_str, *junk; |
| 205 | |
| 206 | UNLESS(py_str = PyString_FromStringAndSize(s, n)) |
| 207 | return -1; |
| 208 | |
| 209 | UNLESS(self->arg) |
| 210 | UNLESS(self->arg = PyTuple_New(1)) |
| 211 | { |
| 212 | Py_DECREF(py_str); |
| 213 | return -1; |
| 214 | } |
| 215 | |
| 216 | if (PyTuple_SetItem(self->arg, 0, py_str) == -1) |
| 217 | { |
| 218 | Py_DECREF(py_str); |
| 219 | return -1; |
| 220 | } |
| 221 | |
| 222 | Py_INCREF(py_str); |
| 223 | UNLESS(junk = PyObject_CallObject(self->write, self->arg)) |
| 224 | { |
| 225 | Py_DECREF(py_str); |
| 226 | return -1; |
| 227 | } |
| 228 | |
| 229 | Py_DECREF(junk); |
| 230 | |
| 231 | return n; |
| 232 | } |
| 233 | |
| 234 | |
| 235 | static int |
| 236 | read_file(ARG(Unpicklerobject *, self), ARG(char **, s), ARG(int, n)) |
| 237 | ARGDECL(Unpicklerobject *, self) |
| 238 | ARGDECL(char **, s) |
| 239 | ARGDECL(int, n) |
| 240 | { |
| 241 | if (fread(*s, sizeof(char), n, self->fp) != n) |
| 242 | { |
| 243 | if (feof(self->fp)) |
| 244 | { |
| 245 | PyErr_SetNone(PyExc_EOFError); |
| 246 | return -1; |
| 247 | } |
| 248 | |
| 249 | PyErr_SetFromErrno(PyExc_IOError); |
| 250 | return -1; |
| 251 | } |
| 252 | |
| 253 | return n; |
| 254 | } |
| 255 | |
| 256 | |
| 257 | static int |
| 258 | readline_file(ARG(Unpicklerobject *, self), ARG(char **, s)) |
| 259 | ARGDECL(Unpicklerobject *, self) |
| 260 | ARGDECL(char **, s) |
| 261 | { |
| 262 | int size, i; |
| 263 | char *str; |
| 264 | |
| 265 | UNLESS(str = (char *)malloc(100)) |
| 266 | { |
| 267 | PyErr_SetString(PyExc_MemoryError, "out of memory"); |
| 268 | return -1; |
| 269 | } |
| 270 | |
| 271 | size = 100; |
| 272 | i = 0; |
| 273 | |
| 274 | while (1) |
| 275 | { |
| 276 | for (; i < size; i++) |
| 277 | { |
| 278 | if (feof(self->fp) || (str[i] = getc(self->fp)) == '\n') |
| 279 | { |
| 280 | str[i] = 0; |
| 281 | *s = str; |
| 282 | return i; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | UNLESS(str = realloc(str, size += 100)) |
| 287 | { |
| 288 | PyErr_SetString(PyExc_MemoryError, "out of memory"); |
| 289 | return -1; |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | |
| 295 | static int |
| 296 | read_cStringIO(ARG(Unpicklerobject *, self), ARG(char **, s), ARG(int, n)) |
| 297 | ARGDECL(Unpicklerobject *, self) |
| 298 | ARGDECL(char **, s) |
| 299 | ARGDECL(int, n) |
| 300 | { |
| 301 | char *ptr; |
| 302 | |
| 303 | if ((*PycStringIO_cread)((PyObject *)self->file, &ptr, n) != n) |
| 304 | { |
| 305 | PyErr_SetNone(PyExc_EOFError); |
| 306 | return -1; |
| 307 | } |
| 308 | |
| 309 | memcpy(*s, ptr, n); |
| 310 | return n; |
| 311 | } |
| 312 | |
| 313 | |
| 314 | static int |
| 315 | readline_cStringIO(ARG(Unpicklerobject *, self), ARG(char **, s)) |
| 316 | ARGDECL(Unpicklerobject *, self) |
| 317 | ARGDECL(char **, s) |
| 318 | { |
| 319 | int n; |
| 320 | char *ptr, *str; |
| 321 | |
| 322 | if ((n = (*PycStringIO_creadline)((PyObject *)self->file, &ptr)) == -1) |
| 323 | { |
| 324 | return -1; |
| 325 | } |
| 326 | |
| 327 | UNLESS(str = (char *)malloc(n * sizeof(char) + 1)) |
| 328 | { |
| 329 | PyErr_SetString(PyExc_MemoryError, "out of memory"); |
| 330 | return -1; |
| 331 | } |
| 332 | |
| 333 | memcpy(str, ptr, n); |
| 334 | |
| 335 | str[((str[n - 1] == '\n') ? --n : n)] = 0; |
| 336 | |
| 337 | *s = str; |
| 338 | return n; |
| 339 | } |
| 340 | |
| 341 | |
| 342 | static int |
| 343 | read_other(ARG(Unpicklerobject *, self), ARG(char **, s), ARG(int, n)) |
| 344 | ARGDECL(Unpicklerobject *, self) |
| 345 | ARGDECL(char **, s) |
| 346 | ARGDECL(int, n) |
| 347 | { |
| 348 | PyObject *bytes, *str; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 349 | |
| 350 | UNLESS(bytes = PyInt_FromLong(n)) |
| 351 | { |
| 352 | if (!PyErr_Occurred()) |
| 353 | PyErr_SetNone(PyExc_EOFError); |
| 354 | |
| 355 | return -1; |
| 356 | } |
| 357 | |
| 358 | UNLESS(self->arg) |
| 359 | UNLESS(self->arg = PyTuple_New(1)) |
| 360 | { |
| 361 | Py_DECREF(bytes); |
| 362 | return -1; |
| 363 | } |
| 364 | |
| 365 | if (PyTuple_SetItem(self->arg, 0, bytes) == -1) |
| 366 | { |
| 367 | Py_DECREF(bytes); |
| 368 | return -1; |
| 369 | } |
| 370 | Py_INCREF(bytes); |
| 371 | |
| 372 | UNLESS(str = PyObject_CallObject(self->read, self->arg)) |
| 373 | { |
| 374 | Py_DECREF(bytes); |
| 375 | return -1; |
| 376 | } |
| 377 | |
| 378 | memcpy(*s, PyString_AsString(str), n); |
| 379 | |
| 380 | Py_DECREF(bytes); |
| 381 | Py_DECREF(str); |
| 382 | |
| 383 | return n; |
| 384 | } |
| 385 | |
| 386 | |
| 387 | static int |
| 388 | readline_other(ARG(Unpicklerobject *, self), ARG(char **, s)) |
| 389 | ARGDECL(Unpicklerobject *, self) |
| 390 | ARGDECL(char **, s) |
| 391 | { |
| 392 | PyObject *str; |
| 393 | char *c_str; |
| 394 | int size; |
| 395 | |
| 396 | UNLESS(str = PyObject_CallObject(self->readline, empty_tuple)) |
| 397 | { |
| 398 | return -1; |
| 399 | } |
| 400 | |
| 401 | size = PyString_Size(str); |
| 402 | |
| 403 | UNLESS(c_str = (char *)malloc((size + 1) * sizeof(char))) |
| 404 | { |
| 405 | PyErr_SetString(PyExc_MemoryError, "out of memory"); |
| 406 | return -1; |
| 407 | } |
| 408 | |
| 409 | memcpy(c_str, PyString_AsString(str), size); |
| 410 | |
| 411 | if (size > 0) |
| 412 | { |
| 413 | c_str[((c_str[size - 1] == '\n') ? --size : size)] = 0; |
| 414 | } |
| 415 | |
| 416 | *s = c_str; |
| 417 | |
| 418 | Py_DECREF(str); |
| 419 | |
| 420 | return size; |
| 421 | } |
| 422 | |
| 423 | |
| 424 | static int |
| 425 | put(ARG(Picklerobject *, self), ARG(PyObject *, ob)) |
| 426 | ARGDECL(Picklerobject *, self) |
| 427 | ARGDECL(PyObject *, ob) |
| 428 | { |
| 429 | char c_str[30]; |
| 430 | int p, len; |
| 431 | PyObject *py_ob_id = 0, *memo_len = 0; |
| 432 | |
| 433 | if ((p = PyDict_Size(self->memo)) == -1) |
| 434 | return -1; |
| 435 | |
| 436 | if (!self->bin || (p >= 256)) |
| 437 | { |
| 438 | c_str[0] = PUT; |
| 439 | sprintf(c_str + 1, "%d\n", p); |
| 440 | len = strlen(c_str); |
| 441 | } |
| 442 | else |
| 443 | { |
| 444 | c_str[0] = BINPUT; |
| 445 | c_str[1] = p; |
| 446 | len = 2; |
| 447 | } |
| 448 | |
| 449 | if ((*self->write_func)(self, c_str, len) == -1) |
| 450 | return -1; |
| 451 | |
| 452 | UNLESS(py_ob_id = PyInt_FromLong((long)ob)) |
| 453 | return -1; |
| 454 | |
| 455 | UNLESS(memo_len = PyInt_FromLong(p)) |
| 456 | goto err; |
| 457 | |
| 458 | if (PyDict_SetItem(self->memo, py_ob_id, memo_len) == -1) |
| 459 | goto err; |
| 460 | |
| 461 | Py_DECREF(py_ob_id); |
| 462 | Py_DECREF(memo_len); |
| 463 | |
| 464 | return 1; |
| 465 | |
| 466 | err: |
| 467 | Py_XDECREF(py_ob_id); |
| 468 | Py_XDECREF(memo_len); |
| 469 | |
| 470 | return -1; |
| 471 | } |
| 472 | |
| 473 | |
| 474 | static int |
| 475 | safe(ARG(PyObject *, ob)) |
| 476 | ARGDECL(PyObject *, ob) |
| 477 | { |
| 478 | PyTypeObject *type; |
| 479 | PyObject *this_item; |
| 480 | int len, res, i; |
| 481 | |
| 482 | type = ob->ob_type; |
| 483 | |
| 484 | if (type == &PyInt_Type || |
| 485 | type == &PyFloat_Type || |
| 486 | type == &PyString_Type || |
| 487 | ob == Py_None) |
| 488 | { |
| 489 | return 1; |
| 490 | } |
| 491 | |
| 492 | if (type == &PyTuple_Type) |
| 493 | { |
| 494 | len = PyTuple_Size(ob); |
| 495 | for (i = 0; i < len; i++) |
| 496 | { |
| 497 | UNLESS(this_item = PyTuple_GET_ITEM((PyTupleObject *)ob, i)) |
| 498 | return -1; |
| 499 | |
| 500 | if ((res = safe(this_item)) == 1) |
| 501 | continue; |
| 502 | |
| 503 | return res; |
| 504 | } |
| 505 | |
| 506 | return 1; |
| 507 | } |
| 508 | |
| 509 | return 0; |
| 510 | } |
| 511 | |
| 512 | |
| 513 | static PyObject * |
| 514 | whichmodule(ARG(PyObject *, class)) |
| 515 | ARGDECL(PyObject *, class) |
| 516 | { |
| 517 | int has_key, i, j; |
| 518 | PyObject *module = 0, *modules_dict = 0, *class_name = 0, *class_name_attr = 0, |
| 519 | *name = 0; |
| 520 | char *name_str, *class_name_str; |
| 521 | |
| 522 | static PyObject *__main__str; |
| 523 | |
| 524 | if ((has_key = PyMapping_HasKey(class_map, class)) == -1) |
| 525 | return NULL; |
| 526 | |
| 527 | if (has_key) |
| 528 | { |
| 529 | return ((module = PyDict_GetItem(class_map, class)) ? module : NULL); |
| 530 | } |
| 531 | |
| 532 | UNLESS(modules_dict = PySys_GetObject("modules")) |
| 533 | return NULL; |
| 534 | |
| 535 | UNLESS(class_name = ((PyClassObject *)class)->cl_name) |
| 536 | { |
| 537 | PyErr_SetString(PicklingError, "class has no name"); |
| 538 | return NULL; |
| 539 | } |
| 540 | |
| 541 | UNLESS(class_name_str = PyString_AsString(class_name)) |
| 542 | return NULL; |
| 543 | |
| 544 | i = 0; |
Barry Warsaw | 93d29b6 | 1997-01-14 17:45:08 +0000 | [diff] [blame] | 545 | while ((j = PyDict_Next(modules_dict, &i, &name, &module))) |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 546 | { |
| 547 | UNLESS(name_str = PyString_AsString(name)) |
| 548 | return NULL; |
| 549 | |
| 550 | if (!strcmp(name_str, "__main__")) |
| 551 | continue; |
| 552 | |
| 553 | UNLESS(class_name_attr = PyObject_GetAttr(module, class_name)) |
| 554 | { |
| 555 | PyErr_Clear(); |
| 556 | continue; |
| 557 | } |
| 558 | |
| 559 | if (class_name_attr != class) |
| 560 | { |
| 561 | Py_DECREF(class_name_attr); |
| 562 | continue; |
| 563 | } |
| 564 | |
| 565 | Py_DECREF(class_name_attr); |
| 566 | |
| 567 | break; |
| 568 | } |
| 569 | |
| 570 | if (!j) /* previous while exited normally */ |
| 571 | { |
| 572 | UNLESS(__main__str) |
| 573 | UNLESS(__main__str = PyString_FromString("__main__")) |
| 574 | return NULL; |
| 575 | |
| 576 | name = __main__str; |
| 577 | } |
| 578 | else /* previous while exited via break */ |
| 579 | { |
| 580 | Py_INCREF(name); |
| 581 | } |
| 582 | |
| 583 | PyDict_SetItem(class_map, class, name); |
| 584 | |
| 585 | return name; |
| 586 | } |
| 587 | |
| 588 | |
| 589 | static PyObject * |
| 590 | save_none(ARG(Picklerobject *, self), ARG(PyObject *, args)) |
| 591 | ARGDECL(Picklerobject *, self) |
| 592 | ARGDECL(PyObject *, args) |
| 593 | { |
| 594 | static char none[] = { NONE }; |
| 595 | |
| 596 | if ((*self->write_func)(self, none, 1) == -1) |
| 597 | return NULL; |
| 598 | |
| 599 | Py_INCREF(Py_None); |
| 600 | return Py_None; |
| 601 | } |
| 602 | |
| 603 | |
| 604 | static PyObject * |
| 605 | save_int(ARG(Picklerobject *, self), ARG(PyObject *, args)) |
| 606 | ARGDECL(Picklerobject *, self) |
| 607 | ARGDECL(PyObject *, args) |
| 608 | { |
| 609 | char c_str[25]; |
| 610 | long l = PyInt_AS_LONG((PyIntObject *)args); |
| 611 | int len; |
| 612 | |
| 613 | if (!self->bin) |
| 614 | { |
| 615 | c_str[0] = INT; |
| 616 | sprintf(c_str + 1, "%ld\n", l); |
| 617 | if ((*self->write_func)(self, c_str, strlen(c_str)) == -1) |
| 618 | return NULL; |
| 619 | } |
| 620 | else |
| 621 | { |
| 622 | c_str[1] = (int)(l & 0xff); |
| 623 | c_str[2] = (int)((l >> 8) & 0xff); |
| 624 | c_str[3] = (int)((l >> 16) & 0xff); |
| 625 | c_str[4] = (int)((l >> 24) & 0xff); |
| 626 | |
| 627 | if (!c_str[4]) |
| 628 | { |
| 629 | if (!c_str[3]) |
| 630 | { |
| 631 | if (!c_str[2]) |
| 632 | { |
| 633 | c_str[0] = BININT3; |
| 634 | len = 2; |
| 635 | } |
| 636 | else |
| 637 | { |
| 638 | c_str[0] = BININT2; |
| 639 | len = 3; |
| 640 | } |
| 641 | } |
| 642 | else |
| 643 | { |
| 644 | c_str[0] = BININT1; |
| 645 | len = 4; |
| 646 | } |
| 647 | } |
| 648 | else |
| 649 | { |
| 650 | c_str[0] = BININT; |
| 651 | len = 5; |
| 652 | } |
| 653 | |
| 654 | if ((*self->write_func)(self, c_str, len) == -1) |
| 655 | return NULL; |
| 656 | } |
| 657 | |
| 658 | Py_INCREF(Py_None); |
| 659 | return Py_None; |
| 660 | } |
| 661 | |
| 662 | |
| 663 | static PyObject * |
| 664 | save_long(ARG(Picklerobject *, self), ARG(PyObject *, args)) |
| 665 | ARGDECL(Picklerobject *, self) |
| 666 | ARGDECL(PyObject *, args) |
| 667 | { |
| 668 | char *c_str; |
| 669 | int size; |
| 670 | PyObject *repr = 0; |
| 671 | |
| 672 | UNLESS(repr = PyObject_Repr(args)) |
| 673 | return NULL; |
| 674 | |
| 675 | if ((size = PyString_Size(repr)) == -1) |
| 676 | { |
| 677 | Py_DECREF(repr); |
| 678 | return NULL; |
| 679 | } |
| 680 | |
| 681 | UNLESS(c_str = (char *)malloc((size + 2) * sizeof(char))) |
| 682 | { |
| 683 | Py_DECREF(repr); |
| 684 | PyErr_SetString(PyExc_MemoryError, "out of memory"); |
| 685 | return NULL; |
| 686 | } |
| 687 | |
| 688 | c_str[0] = LONG; |
| 689 | memcpy(c_str + 1, PyString_AS_STRING((PyStringObject *)repr), size); |
| 690 | c_str[size + 1] = '\n'; |
| 691 | |
| 692 | Py_DECREF(repr); |
| 693 | |
| 694 | if ((*self->write_func)(self, c_str, size + 2) == -1) |
| 695 | { |
| 696 | free(c_str); |
| 697 | return NULL; |
| 698 | } |
| 699 | |
| 700 | free(c_str); |
| 701 | |
| 702 | Py_INCREF(Py_None); |
| 703 | return Py_None; |
| 704 | } |
| 705 | |
| 706 | |
| 707 | static PyObject * |
| 708 | save_float(ARG(Picklerobject *, self), ARG(PyObject *, args)) |
| 709 | ARGDECL(Picklerobject *, self) |
| 710 | ARGDECL(PyObject *, args) |
| 711 | { |
| 712 | char c_str[250]; |
| 713 | |
| 714 | c_str[0] = FLOAT; |
| 715 | sprintf(c_str + 1, "%f\n", PyFloat_AS_DOUBLE((PyFloatObject *)args)); |
| 716 | |
| 717 | if ((*self->write_func)(self, c_str, strlen(c_str)) == -1) |
| 718 | return NULL; |
| 719 | |
| 720 | Py_INCREF(Py_None); |
| 721 | return Py_None; |
| 722 | } |
| 723 | |
| 724 | |
| 725 | static PyObject * |
| 726 | save_string(ARG(Picklerobject *, self), ARG(PyObject *, args)) |
| 727 | ARGDECL(Picklerobject *, self) |
| 728 | ARGDECL(PyObject *, args) |
| 729 | { |
| 730 | char *c_str; |
| 731 | int size, len; |
| 732 | |
| 733 | if (!self->bin) |
| 734 | { |
| 735 | PyObject *repr; |
| 736 | char *repr_str; |
| 737 | |
| 738 | UNLESS(repr = PyObject_Repr(args)) |
| 739 | return NULL; |
| 740 | |
| 741 | repr_str = PyString_AS_STRING((PyStringObject *)repr); |
| 742 | size = PyString_Size(repr); |
| 743 | |
| 744 | UNLESS(c_str = (char *)malloc((size + 2) * sizeof(char))) |
| 745 | { |
| 746 | PyErr_SetString(PyExc_MemoryError, "out of memory"); |
| 747 | return NULL; |
| 748 | } |
| 749 | |
| 750 | c_str[0] = STRING; |
| 751 | memcpy(c_str + 1, repr_str, size); |
| 752 | c_str[size + 1] = '\n'; |
| 753 | |
| 754 | len = size + 2; |
| 755 | |
| 756 | Py_XDECREF(repr); |
| 757 | } |
| 758 | else |
| 759 | { |
| 760 | size = PyString_Size(args); |
| 761 | |
| 762 | UNLESS(c_str = (char *)malloc((size + 30) * sizeof(char))) |
| 763 | { |
| 764 | PyErr_SetString(PyExc_MemoryError, "out of memory"); |
| 765 | return NULL; |
| 766 | } |
| 767 | |
| 768 | if (size < 256) |
| 769 | { |
| 770 | c_str[0] = SHORT_BINSTRING; |
| 771 | c_str[1] = size; |
| 772 | len = 2; |
| 773 | } |
| 774 | else |
| 775 | { |
| 776 | c_str[0] = BINSTRING; |
| 777 | sprintf(c_str + 1, "%d\n", size); |
| 778 | len = strlen(c_str); |
| 779 | } |
| 780 | |
| 781 | memcpy(c_str + len, PyString_AS_STRING((PyStringObject *)args), size); |
| 782 | |
| 783 | len += size; |
| 784 | } |
| 785 | |
| 786 | if ((*self->write_func)(self, c_str, len) == -1) |
| 787 | { |
| 788 | free(c_str); |
| 789 | return NULL; |
| 790 | } |
| 791 | |
| 792 | free(c_str); |
| 793 | |
| 794 | if (args->ob_refcnt > 1) |
| 795 | { |
| 796 | if (put(self, args) == -1) |
| 797 | { |
| 798 | return NULL; |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | Py_INCREF(Py_None); |
| 803 | return Py_None; |
| 804 | } |
| 805 | |
| 806 | |
| 807 | static PyObject * |
| 808 | save_tuple(ARG(Picklerobject *, self), ARG(PyObject *, args)) |
| 809 | ARGDECL(Picklerobject *, self) |
| 810 | ARGDECL(PyObject *, args) |
| 811 | { |
| 812 | PyObject *element = 0, *junk = 0, *py_tuple_id = 0; |
| 813 | int len, i, dict_size; |
| 814 | |
| 815 | static char tuple = TUPLE; |
| 816 | |
| 817 | if ((*self->write_func)(self, &MARKv, 1) == -1) |
| 818 | return NULL; |
| 819 | |
| 820 | UNLESS(py_tuple_id = PyInt_FromLong((long)args)) return NULL; |
| 821 | |
| 822 | if ((len = PyTuple_Size(args)) == -1) |
| 823 | goto err; |
| 824 | |
| 825 | for (i = 0; i < len; i++) |
| 826 | { |
| 827 | UNLESS(element = PyTuple_GET_ITEM((PyTupleObject *)args, i)) |
| 828 | goto err; |
| 829 | |
| 830 | dict_size = PyDict_Size(self->memo); |
| 831 | |
| 832 | UNLESS(junk = save(self, element)) |
| 833 | goto err; |
| 834 | Py_DECREF(junk); |
| 835 | |
| 836 | if (((PyDict_Size(self->memo) - dict_size) > 1) && |
| 837 | PyMapping_HasKey(self->memo, py_tuple_id)) |
| 838 | { |
| 839 | char c_str[30]; |
| 840 | long c_value; |
| 841 | int c_str_len; |
| 842 | PyObject *value; |
| 843 | static char pop = POP; |
| 844 | |
| 845 | while (i-- >= 0) |
| 846 | { |
| 847 | if ((*self->write_func)(self, &pop, 1) == -1) |
| 848 | goto err; |
| 849 | } |
| 850 | |
| 851 | UNLESS(value = PyDict_GetItem(self->memo, py_tuple_id)) |
| 852 | goto err; |
| 853 | |
| 854 | c_value = PyInt_AsLong(value); |
| 855 | |
| 856 | if (self->bin && (c_value < 256)) |
| 857 | { |
| 858 | c_str[0] = BINGET; |
| 859 | c_str[1] = c_value; |
| 860 | c_str_len = 2; |
| 861 | } |
| 862 | else |
| 863 | { |
| 864 | c_str[0] = GET; |
| 865 | sprintf(c_str + 1, "%ld\n", c_value); |
| 866 | c_str_len = strlen(c_str); |
| 867 | } |
| 868 | |
| 869 | if ((*self->write_func)(self, c_str, c_str_len) == -1) |
| 870 | goto err; |
| 871 | |
| 872 | break; |
| 873 | } |
| 874 | } |
| 875 | |
| 876 | if (i >= len) |
| 877 | { |
| 878 | if ((*self->write_func)(self, &tuple, 1) == -1) |
| 879 | goto err; |
| 880 | |
| 881 | if (args->ob_refcnt > 1) |
| 882 | { |
| 883 | if (put(self, args) == -1) |
| 884 | { |
| 885 | goto err; |
| 886 | } |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | Py_DECREF(py_tuple_id); |
| 891 | |
| 892 | Py_INCREF(Py_None); |
| 893 | return Py_None; |
| 894 | |
| 895 | err: |
| 896 | Py_XDECREF(py_tuple_id); |
| 897 | |
| 898 | return NULL; |
| 899 | } |
| 900 | |
| 901 | |
| 902 | static PyObject * |
| 903 | save_list(ARG(Picklerobject *, self), ARG(PyObject *, args)) |
| 904 | ARGDECL(Picklerobject *, self) |
| 905 | ARGDECL(PyObject *, args) |
| 906 | { |
| 907 | PyObject *element = 0, *junk = 0; |
| 908 | int len, i, safe_val; |
| 909 | static char append = APPEND, list = LIST; |
| 910 | |
| 911 | if ((*self->write_func)(self, &MARKv, 1) == -1) |
| 912 | return NULL; |
| 913 | |
| 914 | if ((len = PyList_Size(args)) == -1) |
| 915 | return NULL; |
| 916 | |
| 917 | for (i = 0; i < len; i++) |
| 918 | { |
| 919 | UNLESS(element = PyList_GET_ITEM((PyListObject *)args, i)) |
| 920 | return NULL; |
| 921 | |
| 922 | if ((safe_val = safe(element)) == -1) |
| 923 | return NULL; |
| 924 | UNLESS(safe_val) |
| 925 | break; |
| 926 | |
| 927 | UNLESS(junk = save(self, element)) |
| 928 | return NULL; |
| 929 | Py_DECREF(junk); |
| 930 | } |
| 931 | |
| 932 | if (args->ob_refcnt > 1) |
| 933 | { |
| 934 | if (put(self, args) == -1) |
| 935 | { |
| 936 | return NULL; |
| 937 | } |
| 938 | } |
| 939 | |
| 940 | if ((*self->write_func)(self, &list, 1) == -1) |
| 941 | return NULL; |
| 942 | |
| 943 | for (; i < len; i++) |
| 944 | { |
| 945 | UNLESS(element = PyList_GET_ITEM((PyListObject *)args, i)) |
| 946 | return NULL; |
| 947 | |
| 948 | UNLESS(junk = save(self, element)) |
| 949 | return NULL; |
| 950 | Py_DECREF(junk); |
| 951 | |
| 952 | if ((*self->write_func)(self, &append, 1) == -1) |
| 953 | return NULL; |
| 954 | } |
| 955 | |
| 956 | Py_INCREF(Py_None); |
| 957 | return Py_None; |
| 958 | } |
| 959 | |
| 960 | |
| 961 | static PyObject * |
| 962 | save_dict(ARG(Picklerobject *, self), ARG(PyObject *, args)) |
| 963 | ARGDECL(Picklerobject *, self) |
| 964 | ARGDECL(PyObject *, args) |
| 965 | { |
| 966 | PyObject *key = 0, *value = 0, *junk = 0; |
| 967 | int i, safe_key, safe_value; |
| 968 | |
| 969 | static char setitem = SETITEM, dict = DICT; |
| 970 | |
| 971 | if ((*self->write_func)(self, &MARKv, 1) == -1) |
| 972 | return NULL; |
| 973 | |
| 974 | i = 0; |
| 975 | |
| 976 | while (PyDict_Next(args, &i, &key, &value)) |
| 977 | { |
| 978 | if ((safe_key = safe(key)) == -1) |
| 979 | return NULL; |
| 980 | |
| 981 | UNLESS(safe_key) |
| 982 | break; |
| 983 | |
| 984 | if ((safe_value = safe(value)) == -1) |
| 985 | return NULL; |
| 986 | |
| 987 | UNLESS(safe_value) |
| 988 | break; |
| 989 | |
| 990 | UNLESS(junk = save(self, key)) |
| 991 | return NULL; |
| 992 | Py_DECREF(junk); |
| 993 | |
| 994 | UNLESS(junk = save(self, value)) |
| 995 | return NULL; |
| 996 | Py_DECREF(junk); |
| 997 | } |
| 998 | |
| 999 | if ((*self->write_func)(self, &dict, 1) == -1) |
| 1000 | return NULL; |
| 1001 | |
| 1002 | if (args->ob_refcnt > 1) |
| 1003 | { |
| 1004 | if (put(self, args) == -1) |
| 1005 | { |
| 1006 | return NULL; |
| 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | while (PyDict_Next(args, &i, &key, &value)) |
| 1011 | { |
| 1012 | UNLESS(junk = save(self, key)) |
| 1013 | return NULL; |
| 1014 | Py_DECREF(junk); |
| 1015 | |
| 1016 | UNLESS(junk = save(self, value)) |
| 1017 | return NULL; |
| 1018 | Py_DECREF(junk); |
| 1019 | |
| 1020 | if ((*self->write_func)(self, &setitem, 1) == -1) |
| 1021 | return NULL; |
| 1022 | } |
| 1023 | |
| 1024 | Py_INCREF(Py_None); |
| 1025 | return Py_None; |
| 1026 | } |
| 1027 | |
| 1028 | |
| 1029 | static PyObject * |
| 1030 | save_inst(ARG(Picklerobject *, self), ARG(PyObject *, args)) |
| 1031 | ARGDECL(Picklerobject *, self) |
| 1032 | ARGDECL(PyObject *, args) |
| 1033 | { |
Barry Warsaw | 93d29b6 | 1997-01-14 17:45:08 +0000 | [diff] [blame] | 1034 | PyObject *class = 0, *module = 0, *name = 0, |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1035 | *junk = 0, *state = 0, *getinitargs_func = 0, *getstate_func = 0; |
| 1036 | char *module_str, *name_str, *c_str; |
Barry Warsaw | 93d29b6 | 1997-01-14 17:45:08 +0000 | [diff] [blame] | 1037 | int module_size, name_size, size; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1038 | static char build = BUILD; |
| 1039 | |
| 1040 | if ((*self->write_func)(self, &MARKv, 1) == -1) |
| 1041 | return NULL; |
| 1042 | |
| 1043 | UNLESS(class = PyObject_GetAttrString(args, "__class__")) |
| 1044 | return NULL; |
| 1045 | |
| 1046 | if (self->bin) |
| 1047 | { |
| 1048 | UNLESS(junk = save(self, class)) |
| 1049 | goto err; |
| 1050 | Py_DECREF(junk); |
| 1051 | } |
| 1052 | |
Barry Warsaw | 93d29b6 | 1997-01-14 17:45:08 +0000 | [diff] [blame] | 1053 | if ((getinitargs_func = PyObject_GetAttrString(args, "__getinitargs__"))) |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1054 | { |
| 1055 | PyObject *class_args = 0, *element = 0; |
| 1056 | int i, len; |
| 1057 | |
| 1058 | UNLESS(class_args = PyObject_CallObject(getinitargs_func, empty_tuple)) |
| 1059 | { |
| 1060 | Py_DECREF(getinitargs_func); |
| 1061 | goto err; |
| 1062 | } |
| 1063 | |
| 1064 | if ((len = PyObject_Length(class_args)) == -1) |
| 1065 | { |
| 1066 | Py_DECREF(class_args); |
| 1067 | goto err; |
| 1068 | } |
| 1069 | |
| 1070 | for (i = 0; i < len; i++) |
| 1071 | { |
| 1072 | UNLESS(element = PySequence_GetItem(class_args, i)) |
| 1073 | { |
| 1074 | Py_DECREF(class_args); |
| 1075 | goto err; |
| 1076 | } |
| 1077 | |
| 1078 | UNLESS(junk = save(self, element)) |
| 1079 | { |
| 1080 | Py_DECREF(element); Py_DECREF(class_args); |
| 1081 | goto err; |
| 1082 | } |
| 1083 | |
| 1084 | Py_DECREF(junk); |
| 1085 | Py_DECREF(element); |
| 1086 | } |
| 1087 | |
| 1088 | Py_DECREF(class_args); |
| 1089 | } |
| 1090 | else |
| 1091 | { |
| 1092 | PyErr_Clear(); |
| 1093 | } |
| 1094 | |
| 1095 | if (!self->bin) |
| 1096 | { |
| 1097 | UNLESS(module = whichmodule(class)) |
| 1098 | goto err; |
| 1099 | |
| 1100 | UNLESS(name = ((PyClassObject *)class)->cl_name) |
| 1101 | { |
| 1102 | PyErr_SetString(PicklingError, "class has no name"); |
| 1103 | goto err; |
| 1104 | } |
| 1105 | |
| 1106 | module_str = PyString_AS_STRING((PyStringObject *)module); |
| 1107 | module_size = PyString_Size(module); |
| 1108 | name_str = PyString_AS_STRING((PyStringObject *)name); |
| 1109 | name_size = PyString_Size(name); |
| 1110 | |
| 1111 | size = name_size + module_size + 3; |
| 1112 | |
| 1113 | UNLESS(c_str = (char *)malloc(size * sizeof(char))) |
| 1114 | { |
| 1115 | PyErr_SetString(PyExc_MemoryError, "out of memory"); |
| 1116 | return NULL; |
| 1117 | } |
| 1118 | |
| 1119 | c_str[0] = INST; |
| 1120 | memcpy(c_str + 1, module_str, module_size); |
| 1121 | c_str[module_size + 1] = '\n'; |
| 1122 | memcpy(c_str + module_size + 2, name_str, name_size); |
| 1123 | c_str[module_size + name_size + 2] = '\n'; |
| 1124 | |
| 1125 | if ((*self->write_func)(self, c_str, size) == -1) |
| 1126 | { |
| 1127 | free(c_str); |
| 1128 | goto err; |
| 1129 | } |
| 1130 | |
| 1131 | free(c_str); |
| 1132 | } |
| 1133 | |
| 1134 | if (args->ob_refcnt > 1) |
| 1135 | { |
| 1136 | if (put(self, args) == -1) |
| 1137 | { |
| 1138 | goto err; |
| 1139 | } |
| 1140 | } |
| 1141 | |
Barry Warsaw | 93d29b6 | 1997-01-14 17:45:08 +0000 | [diff] [blame] | 1142 | if ((getstate_func = PyObject_GetAttrString(args, "__getstate__"))) |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1143 | { |
| 1144 | UNLESS(state = PyObject_CallObject(getstate_func, empty_tuple)) |
| 1145 | { |
| 1146 | Py_DECREF(getstate_func); |
| 1147 | goto err; |
| 1148 | } |
| 1149 | } |
| 1150 | else |
| 1151 | { |
| 1152 | PyErr_Clear(); |
| 1153 | |
| 1154 | UNLESS(state = PyObject_GetAttrString(args, "__dict__")) |
| 1155 | goto err; |
| 1156 | } |
| 1157 | |
| 1158 | UNLESS(junk = save(self, state)) |
| 1159 | goto err; |
| 1160 | Py_DECREF(junk); |
| 1161 | |
| 1162 | Py_XDECREF(getinitargs_func); |
| 1163 | Py_XDECREF(getstate_func); |
| 1164 | Py_XDECREF(module); |
| 1165 | Py_XDECREF(state); |
| 1166 | |
| 1167 | if ((*self->write_func)(self, &build, 1) == -1) |
| 1168 | return NULL; |
| 1169 | |
| 1170 | Py_INCREF(Py_None); |
| 1171 | return Py_None; |
| 1172 | |
| 1173 | err: |
| 1174 | Py_XDECREF(getinitargs_func); |
| 1175 | Py_XDECREF(getstate_func); |
| 1176 | Py_XDECREF(module); |
| 1177 | Py_XDECREF(state); |
| 1178 | |
| 1179 | return NULL; |
| 1180 | } |
| 1181 | |
| 1182 | |
| 1183 | static PyObject * |
| 1184 | save_class(ARG(Picklerobject *, self), ARG(PyObject *, args)) |
| 1185 | ARGDECL(Picklerobject *, self) |
| 1186 | ARGDECL(PyObject *, args) |
| 1187 | { |
| 1188 | PyObject *module = 0, *name = 0; |
| 1189 | char *name_str, *module_str, *c_str; |
| 1190 | int module_size, name_size, size; |
| 1191 | |
| 1192 | UNLESS(module = whichmodule(args)) |
| 1193 | return NULL; |
| 1194 | |
| 1195 | UNLESS(name = ((PyClassObject *)args)->cl_name) |
| 1196 | { |
| 1197 | PyErr_SetString(PicklingError, "class has no name"); |
| 1198 | goto err; |
| 1199 | } |
| 1200 | |
| 1201 | module_str = PyString_AS_STRING((PyStringObject *)module); |
| 1202 | module_size = PyString_Size(module); |
| 1203 | name_str = PyString_AS_STRING((PyStringObject *)name); |
| 1204 | name_size = PyString_Size(name); |
| 1205 | |
| 1206 | size = name_size + module_size + 3; |
| 1207 | |
| 1208 | UNLESS(c_str = (char *)malloc(size * sizeof(char))) |
| 1209 | { |
| 1210 | PyErr_SetString(PyExc_MemoryError, "out of memory"); |
| 1211 | return NULL; |
| 1212 | } |
| 1213 | |
| 1214 | c_str[0] = CLASS; |
| 1215 | memcpy(c_str + 1, module_str, module_size); |
| 1216 | c_str[module_size + 1] = '\n'; |
| 1217 | memcpy(c_str + module_size + 2, name_str, name_size); |
| 1218 | c_str[module_size + name_size + 2] = '\n'; |
| 1219 | |
| 1220 | if ((*self->write_func)(self, c_str, size) == -1) |
| 1221 | { |
| 1222 | free(c_str); |
| 1223 | goto err; |
| 1224 | } |
| 1225 | |
| 1226 | free(c_str); |
| 1227 | |
| 1228 | if (args->ob_refcnt > 1) |
| 1229 | { |
| 1230 | if (put(self, args) == -1) |
| 1231 | { |
| 1232 | goto err; |
| 1233 | } |
| 1234 | } |
| 1235 | |
| 1236 | Py_DECREF(module); |
| 1237 | |
| 1238 | Py_INCREF(Py_None); |
| 1239 | return Py_None; |
| 1240 | |
| 1241 | err: |
| 1242 | Py_XDECREF(module); |
| 1243 | |
| 1244 | return NULL; |
| 1245 | } |
| 1246 | |
| 1247 | |
| 1248 | static PyObject * |
| 1249 | save(ARG(Picklerobject *, self), ARG(PyObject *, args)) |
| 1250 | ARGDECL(Picklerobject *, self) |
| 1251 | ARGDECL(PyObject *, args) |
| 1252 | { |
| 1253 | PyTypeObject *type; |
| 1254 | char *error_str, *name_c; |
| 1255 | PyObject *name, *name_repr; |
| 1256 | |
| 1257 | if (self->pers_func) |
| 1258 | { |
| 1259 | PyObject *pid; |
| 1260 | int size; |
| 1261 | |
| 1262 | UNLESS(self->arg) |
| 1263 | UNLESS(self->arg = PyTuple_New(1)) |
| 1264 | return NULL; |
| 1265 | |
| 1266 | if (PyTuple_SetItem(self->arg, 0, args) == -1) |
| 1267 | return NULL; |
| 1268 | Py_INCREF(args); |
| 1269 | |
| 1270 | UNLESS(pid = PyObject_CallObject(self->pers_func, self->arg)) |
| 1271 | return NULL; |
| 1272 | |
| 1273 | if (pid != Py_None) |
| 1274 | { |
| 1275 | char *pid_str; |
| 1276 | |
| 1277 | if ((size = PyString_Size(pid)) == -1) |
| 1278 | { |
| 1279 | Py_DECREF(pid); |
| 1280 | return NULL; |
| 1281 | } |
| 1282 | |
| 1283 | UNLESS(pid_str = (char *)malloc((2 + size) * sizeof(char))) |
| 1284 | { |
| 1285 | PyErr_SetString(PyExc_MemoryError, "out of memory"); |
| 1286 | Py_DECREF(pid); |
| 1287 | return NULL; |
| 1288 | } |
| 1289 | |
| 1290 | pid_str[0] = PERSID; |
| 1291 | memcpy(pid_str + 1, PyString_AS_STRING((PyStringObject *)pid), size); |
| 1292 | pid_str[size + 1] = '\n'; |
| 1293 | |
| 1294 | Py_DECREF(pid); |
| 1295 | |
| 1296 | if ((*self->write_func)(self, pid_str, size + 2) == -1) |
| 1297 | { |
| 1298 | free(pid_str); |
| 1299 | return NULL; |
| 1300 | } |
| 1301 | |
| 1302 | free(pid_str); |
| 1303 | |
| 1304 | Py_INCREF(Py_None); |
| 1305 | return Py_None; |
| 1306 | } |
| 1307 | |
| 1308 | Py_DECREF(pid); |
| 1309 | } |
| 1310 | |
| 1311 | if (args == Py_None) |
| 1312 | { |
| 1313 | return save_none(self, args); |
| 1314 | } |
| 1315 | |
| 1316 | type = args->ob_type; |
| 1317 | |
| 1318 | if (type == &PyInt_Type) |
| 1319 | { |
| 1320 | return save_int(self, args); |
| 1321 | } |
| 1322 | |
| 1323 | if (type == &PyLong_Type) |
| 1324 | { |
| 1325 | return save_long(self, args); |
| 1326 | } |
| 1327 | |
| 1328 | if (type == &PyFloat_Type) |
| 1329 | { |
| 1330 | return save_float(self, args); |
| 1331 | } |
| 1332 | |
| 1333 | if (args->ob_refcnt > 1) |
| 1334 | { |
| 1335 | long ob_id; |
| 1336 | int has_key; |
| 1337 | PyObject *py_ob_id; |
| 1338 | |
| 1339 | ob_id = (long)args; |
| 1340 | |
| 1341 | UNLESS(py_ob_id = PyInt_FromLong(ob_id)) |
| 1342 | return NULL; |
| 1343 | |
| 1344 | if ((has_key = PyMapping_HasKey(self->memo, py_ob_id)) == -1) |
| 1345 | { |
| 1346 | Py_DECREF(py_ob_id); |
| 1347 | return NULL; |
| 1348 | } |
| 1349 | |
| 1350 | if (has_key) |
| 1351 | { |
| 1352 | PyObject *value; |
| 1353 | long c_value; |
| 1354 | char get_str[30]; |
| 1355 | int len; |
| 1356 | |
| 1357 | UNLESS(value = PyDict_GetItem(self->memo, py_ob_id)) |
| 1358 | { |
| 1359 | Py_DECREF(py_ob_id); |
| 1360 | return NULL; |
| 1361 | } |
| 1362 | |
| 1363 | Py_DECREF(py_ob_id); |
| 1364 | |
| 1365 | c_value = PyInt_AsLong(value); |
| 1366 | |
| 1367 | if (self->bin && (c_value < 256)) |
| 1368 | { |
| 1369 | get_str[0] = BINGET; |
| 1370 | get_str[1] = c_value; |
| 1371 | len = 2; |
| 1372 | } |
| 1373 | else |
| 1374 | { |
| 1375 | get_str[0] = GET; |
| 1376 | sprintf(get_str + 1, "%ld\n", c_value); |
| 1377 | len = strlen(get_str); |
| 1378 | } |
| 1379 | |
| 1380 | if ((*self->write_func)(self, get_str, len) == -1) |
| 1381 | return NULL; |
| 1382 | |
| 1383 | Py_INCREF(Py_None); |
| 1384 | return Py_None; |
| 1385 | } |
| 1386 | |
| 1387 | Py_DECREF(py_ob_id); |
| 1388 | } |
| 1389 | |
| 1390 | if (type == &PyString_Type) |
| 1391 | { |
| 1392 | return save_string(self, args); |
| 1393 | } |
| 1394 | |
| 1395 | if (type == &PyTuple_Type) |
| 1396 | { |
| 1397 | return save_tuple(self, args); |
| 1398 | } |
| 1399 | |
| 1400 | if (type == &PyList_Type) |
| 1401 | { |
| 1402 | return save_list(self, args); |
| 1403 | } |
| 1404 | |
| 1405 | if (type == &PyDict_Type) |
| 1406 | { |
| 1407 | return save_dict(self, args); |
| 1408 | } |
| 1409 | |
| 1410 | if (type == &PyInstance_Type) |
| 1411 | { |
| 1412 | return save_inst(self, args); |
| 1413 | } |
| 1414 | |
| 1415 | if (type == &PyClass_Type) |
| 1416 | { |
| 1417 | return save_class(self, args); |
| 1418 | } |
| 1419 | |
| 1420 | if (PyObject_HasAttrString(args, "__class__")) |
| 1421 | { |
| 1422 | return save_inst(self, args); |
| 1423 | } |
| 1424 | |
| 1425 | UNLESS(name = PyObject_GetAttrString((PyObject *)type, "__name__")) |
| 1426 | return NULL; |
| 1427 | |
| 1428 | UNLESS(name_repr = PyObject_Repr(name)) |
| 1429 | { |
| 1430 | Py_DECREF(name); |
| 1431 | } |
| 1432 | |
| 1433 | name_c = PyString_AsString(name_repr); |
| 1434 | |
| 1435 | UNLESS(error_str = (char *)malloc((strlen(name_c) + 25) * sizeof(char))) |
| 1436 | { |
| 1437 | PyErr_SetString(PyExc_MemoryError, "out of memory"); |
| 1438 | Py_DECREF(name); |
| 1439 | Py_DECREF(name_repr); |
| 1440 | return NULL; |
| 1441 | } |
| 1442 | |
| 1443 | sprintf(error_str, "Cannot pickle %s objects.", name_c); |
| 1444 | |
| 1445 | Py_DECREF(name); |
| 1446 | Py_DECREF(name_repr); |
| 1447 | |
| 1448 | PyErr_SetString(PicklingError, error_str); |
| 1449 | |
| 1450 | free(error_str); |
| 1451 | |
| 1452 | return NULL; |
| 1453 | } |
| 1454 | |
| 1455 | |
| 1456 | static PyObject * |
| 1457 | Pickler_dump(ARG(Picklerobject *, self), ARG(PyObject *, args)) |
| 1458 | ARGDECL(Picklerobject *, self) |
| 1459 | ARGDECL(PyObject *, args) |
| 1460 | { |
| 1461 | PyObject *junk; |
| 1462 | static char stop = STOP; |
| 1463 | |
| 1464 | UNLESS(PyArg_Parse(args, "O", &args)) return NULL; |
| 1465 | |
| 1466 | UNLESS(junk = save(self, args)) return NULL; |
| 1467 | Py_DECREF(junk); |
| 1468 | |
| 1469 | if ((*self->write_func)(self, &stop, 1) == -1) |
| 1470 | return NULL; |
| 1471 | |
| 1472 | Py_INCREF(Py_None); |
| 1473 | return Py_None; |
| 1474 | } |
| 1475 | |
| 1476 | |
| 1477 | static PyObject * |
| 1478 | write(ARG(Picklerobject *, self), ARG(PyObject *, args)) |
| 1479 | ARGDECL(Picklerobject *, self) |
| 1480 | ARGDECL(PyObject *, args) |
| 1481 | { |
| 1482 | char *ptr; |
| 1483 | int size; |
| 1484 | |
| 1485 | UNLESS(ptr = PyString_AsString(args)) |
| 1486 | return NULL; |
| 1487 | |
| 1488 | if ((size = PyString_Size(args)) == -1) |
| 1489 | return NULL; |
| 1490 | |
| 1491 | if ((*self->write_func)(self, ptr, size) == -1) |
| 1492 | return NULL; |
| 1493 | |
| 1494 | Py_INCREF(Py_None); |
| 1495 | return Py_None; |
| 1496 | } |
| 1497 | |
| 1498 | |
| 1499 | static struct PyMethodDef Pickler_methods[] = { |
| 1500 | {"save", (PyCFunction)save, 0, ""}, |
| 1501 | {"dump", (PyCFunction)Pickler_dump, 0, ""}, |
| 1502 | {"save_none", (PyCFunction)save_none, 0, ""}, |
| 1503 | {"save_int", (PyCFunction)save_int, 0, ""}, |
| 1504 | {"save_long", (PyCFunction)save_long, 0, ""}, |
| 1505 | {"save_float", (PyCFunction)save_float, 0, ""}, |
| 1506 | {"save_string", (PyCFunction)save_string, 0, ""}, |
| 1507 | {"save_tuple", (PyCFunction)save_tuple, 0, ""}, |
| 1508 | {"save_list", (PyCFunction)save_list, 0, ""}, |
| 1509 | {"save_dict", (PyCFunction)save_dict, 0, ""}, |
| 1510 | {"save_inst", (PyCFunction)save_inst, 0, ""}, |
| 1511 | {"save_class", (PyCFunction)save_class, 0, ""}, |
| 1512 | {"write", (PyCFunction)write, 0, ""}, |
| 1513 | {NULL, NULL} /* sentinel */ |
| 1514 | }; |
| 1515 | |
| 1516 | |
| 1517 | static Picklerobject * |
| 1518 | newPicklerobject(ARG(PyObject *, file), ARG(int, bin)) |
| 1519 | ARGDECL(PyObject *, file) |
| 1520 | ARGDECL(int, bin) |
| 1521 | { |
| 1522 | Picklerobject *self; |
| 1523 | PyObject *memo = 0; |
| 1524 | |
| 1525 | UNLESS(memo = PyDict_New()) goto err; |
| 1526 | |
| 1527 | UNLESS(self = PyObject_NEW(Picklerobject, &Picklertype)) |
| 1528 | goto err; |
| 1529 | |
| 1530 | if (PyFile_Check(file)) |
| 1531 | { |
| 1532 | self->fp = PyFile_AsFile(file); |
| 1533 | self->write_func = write_file; |
| 1534 | self->write = NULL; |
| 1535 | } |
| 1536 | else if (PycStringIO_OutputCheck(file)) |
| 1537 | { |
| 1538 | self->fp = NULL; |
| 1539 | self->write_func = write_cStringIO; |
| 1540 | self->write = NULL; |
| 1541 | } |
| 1542 | else |
| 1543 | { |
| 1544 | PyObject *write; |
| 1545 | |
| 1546 | self->fp = NULL; |
| 1547 | self->write_func = write_other; |
| 1548 | |
| 1549 | UNLESS(write = PyObject_GetAttrString(file, "write")) |
| 1550 | goto err; |
| 1551 | |
| 1552 | self->write = write; |
| 1553 | } |
| 1554 | |
| 1555 | Py_INCREF(file); |
| 1556 | |
| 1557 | self->file = file; |
| 1558 | self->bin = bin; |
| 1559 | self->memo = memo; |
| 1560 | self->arg = NULL; |
| 1561 | self->pers_func = NULL; |
| 1562 | |
| 1563 | return self; |
| 1564 | |
| 1565 | err: |
| 1566 | Py_XDECREF((PyObject *)self); |
| 1567 | Py_XDECREF(memo); |
| 1568 | return NULL; |
| 1569 | } |
| 1570 | |
| 1571 | |
| 1572 | static PyObject * |
| 1573 | get_Pickler(ARG(PyObject *, self), ARG(PyObject *, args)) |
| 1574 | ARGDECL(PyObject *, self) |
| 1575 | ARGDECL(PyObject *, args) |
| 1576 | { |
| 1577 | PyObject *file; |
| 1578 | int bin = 0; |
| 1579 | |
| 1580 | UNLESS(PyArg_ParseTuple(args, "O|i", &file, &bin)) return NULL; |
| 1581 | return (PyObject *)newPicklerobject(file, bin); |
| 1582 | } |
| 1583 | |
| 1584 | |
| 1585 | static void |
| 1586 | Pickler_dealloc(ARG(Picklerobject *, self)) |
| 1587 | ARGDECL(Picklerobject *, self) |
| 1588 | { |
| 1589 | Py_XDECREF(self->write); |
| 1590 | Py_XDECREF(self->memo); |
| 1591 | Py_XDECREF(self->arg); |
| 1592 | Py_XDECREF(self->file); |
| 1593 | Py_XDECREF(self->pers_func); |
| 1594 | PyMem_DEL(self); |
| 1595 | } |
| 1596 | |
| 1597 | |
| 1598 | static PyObject * |
| 1599 | Pickler_getattr(ARG(Picklerobject *, self), ARG(char *, name)) |
| 1600 | ARGDECL(Picklerobject *, self) |
| 1601 | ARGDECL(char *, name) |
| 1602 | { |
| 1603 | if (!strcmp(name, "persistent_id")) |
| 1604 | { |
| 1605 | if (!self->pers_func) |
| 1606 | { |
| 1607 | PyErr_SetString(PyExc_NameError, name); |
| 1608 | return NULL; |
| 1609 | } |
| 1610 | |
| 1611 | Py_INCREF(self->pers_func); |
| 1612 | return self->pers_func; |
| 1613 | } |
| 1614 | |
| 1615 | if (!strcmp(name, "memo")) |
| 1616 | { |
| 1617 | if (!self->memo) |
| 1618 | { |
| 1619 | PyErr_SetString(PyExc_NameError, name); |
| 1620 | return NULL; |
| 1621 | } |
| 1622 | |
| 1623 | Py_INCREF(self->memo); |
| 1624 | return self->memo; |
| 1625 | } |
| 1626 | |
| 1627 | if (!strcmp(name, "PicklingError")) |
| 1628 | { |
| 1629 | Py_INCREF(PicklingError); |
| 1630 | return PicklingError; |
| 1631 | } |
| 1632 | |
| 1633 | return Py_FindMethod(Pickler_methods, (PyObject *)self, name); |
| 1634 | } |
| 1635 | |
| 1636 | |
| 1637 | int |
| 1638 | Pickler_setattr(ARG(Picklerobject *, self), ARG(char *, name), ARG(PyObject *, value)) |
| 1639 | ARGDECL(Picklerobject *, self) |
| 1640 | ARGDECL(char *, name) |
| 1641 | ARGDECL(PyObject *, value) |
| 1642 | { |
| 1643 | if (!strcmp(name, "persistent_id")) |
| 1644 | { |
| 1645 | Py_XDECREF(self->pers_func); |
| 1646 | self->pers_func = value; |
| 1647 | Py_INCREF(value); |
| 1648 | return 0; |
| 1649 | } |
| 1650 | |
| 1651 | return -1; |
| 1652 | } |
| 1653 | |
| 1654 | |
| 1655 | static char Picklertype__doc__[] = ""; |
| 1656 | |
| 1657 | static PyTypeObject Picklertype = { |
| 1658 | PyObject_HEAD_INIT(&PyType_Type) |
| 1659 | 0, /*ob_size*/ |
| 1660 | "Pickler", /*tp_name*/ |
| 1661 | sizeof(Picklerobject), /*tp_basicsize*/ |
| 1662 | 0, /*tp_itemsize*/ |
| 1663 | /* methods */ |
| 1664 | (destructor)Pickler_dealloc, /*tp_dealloc*/ |
| 1665 | (printfunc)0, /*tp_print*/ |
| 1666 | (getattrfunc)Pickler_getattr, /*tp_getattr*/ |
| 1667 | (setattrfunc)Pickler_setattr, /*tp_setattr*/ |
| 1668 | (cmpfunc)0, /*tp_compare*/ |
| 1669 | (reprfunc)0, /*tp_repr*/ |
| 1670 | 0, /*tp_as_number*/ |
| 1671 | 0, /*tp_as_sequence*/ |
| 1672 | 0, /*tp_as_mapping*/ |
| 1673 | (hashfunc)0, /*tp_hash*/ |
| 1674 | (ternaryfunc)0, /*tp_call*/ |
| 1675 | (reprfunc)0, /*tp_str*/ |
| 1676 | |
| 1677 | /* Space for future expansion */ |
| 1678 | 0L,0L,0L,0L, |
| 1679 | Picklertype__doc__ /* Documentation string */ |
| 1680 | }; |
| 1681 | |
| 1682 | |
| 1683 | static PyObject * |
| 1684 | find_class(ARG(char *, module_name), ARG(char *, class_name)) |
| 1685 | ARGDECL(char *, module_name) |
| 1686 | ARGDECL(char *, class_name) |
| 1687 | { |
| 1688 | PyObject *import = 0, *class = 0, *py_module_name = 0, *py_class_name = 0, |
| 1689 | *t = 0; |
| 1690 | char *error_str; |
| 1691 | int has_key; |
| 1692 | |
| 1693 | UNLESS(py_module_name = PyString_FromString(module_name)) |
| 1694 | return NULL; |
| 1695 | |
| 1696 | UNLESS(py_class_name = PyString_FromString(class_name)) |
| 1697 | goto err; |
| 1698 | |
| 1699 | UNLESS(t = PyTuple_New(2)) |
| 1700 | goto err; |
| 1701 | |
| 1702 | UNLESS(PyTuple_SET_ITEM((PyTupleObject *)t, 0, py_module_name)) |
| 1703 | goto err; |
| 1704 | Py_INCREF(py_module_name); |
| 1705 | |
| 1706 | UNLESS(PyTuple_SET_ITEM((PyTupleObject *)t, 1, py_class_name)) |
| 1707 | goto err; |
| 1708 | Py_INCREF(py_module_name); |
| 1709 | |
| 1710 | if ((has_key = PyMapping_HasKey(class_map, t)) == -1) |
| 1711 | goto err; |
| 1712 | |
| 1713 | if (has_key) |
| 1714 | { |
| 1715 | UNLESS(class = PyDict_GetItem(class_map, t)) |
| 1716 | goto err; |
| 1717 | |
| 1718 | Py_INCREF(class); |
| 1719 | |
| 1720 | Py_DECREF(py_module_name); |
| 1721 | Py_DECREF(py_class_name); |
| 1722 | Py_DECREF(t); |
| 1723 | |
| 1724 | return class; |
| 1725 | } |
| 1726 | |
| 1727 | if (!(import = PyImport_ImportModule(module_name)) || |
| 1728 | !(class = PyObject_GetAttrString(import, class_name))) |
| 1729 | { |
| 1730 | UNLESS(error_str = (char *)malloc((strlen(module_name) + |
| 1731 | strlen(class_name) + 40) * sizeof(char))) |
| 1732 | { |
| 1733 | PyErr_SetString(PyExc_MemoryError, "out of memory"); |
| 1734 | goto err; |
| 1735 | } |
| 1736 | |
| 1737 | sprintf(error_str, "Failed to import class %s from module %s", |
| 1738 | class_name, module_name); |
| 1739 | |
| 1740 | PyErr_SetString(PyExc_SystemError, error_str); |
| 1741 | |
| 1742 | free(error_str); |
| 1743 | goto err; |
| 1744 | } |
| 1745 | |
| 1746 | if (class->ob_type == BuiltinFunctionType) |
| 1747 | { |
| 1748 | UNLESS(error_str = (char *)malloc((strlen(module_name) + |
| 1749 | strlen(class_name) + 45) * sizeof(char))) |
| 1750 | { |
| 1751 | PyErr_SetString(PyExc_MemoryError, "out of memory"); |
| 1752 | goto err; |
| 1753 | } |
| 1754 | |
| 1755 | sprintf(error_str, "Imported object %s from module %s is not a class", |
| 1756 | class_name, module_name); |
| 1757 | |
| 1758 | PyErr_SetString(PyExc_SystemError, error_str); |
| 1759 | |
| 1760 | free(error_str); |
| 1761 | goto err; |
| 1762 | } |
| 1763 | |
| 1764 | if (PyDict_SetItem(class_map, t, class) == -1) |
| 1765 | goto err; |
| 1766 | |
| 1767 | Py_DECREF(t); |
| 1768 | Py_DECREF(py_module_name); |
| 1769 | Py_DECREF(py_class_name); |
| 1770 | Py_DECREF(import); |
| 1771 | |
| 1772 | return class; |
| 1773 | |
| 1774 | err: |
| 1775 | Py_XDECREF(import); |
| 1776 | Py_XDECREF(class); |
| 1777 | Py_XDECREF(t); |
| 1778 | Py_XDECREF(py_module_name); |
| 1779 | Py_XDECREF(py_class_name); |
| 1780 | |
| 1781 | return NULL; |
| 1782 | } |
| 1783 | |
| 1784 | |
| 1785 | int |
| 1786 | marker(ARG(Unpicklerobject *, self)) |
| 1787 | ARGDECL(Unpicklerobject *, self) |
| 1788 | { |
| 1789 | if (!self->num_marks) |
| 1790 | return -1; |
| 1791 | |
| 1792 | return self->marks[--self->num_marks]; |
| 1793 | } |
| 1794 | |
| 1795 | |
| 1796 | static PyObject * |
| 1797 | load_none(ARG(Unpicklerobject *, self), ARG(PyObject *, args)) |
| 1798 | ARGDECL(Unpicklerobject *, self) |
| 1799 | ARGDECL(PyObject *, args) |
| 1800 | { |
| 1801 | if (PyList_Append(self->stack, Py_None) == -1) |
| 1802 | return NULL; |
| 1803 | |
| 1804 | Py_INCREF(Py_None); |
| 1805 | return Py_None; |
| 1806 | } |
| 1807 | |
| 1808 | |
| 1809 | static PyObject * |
| 1810 | load_int(ARG(Unpicklerobject *, self), ARG(PyObject *, args)) |
| 1811 | ARGDECL(Unpicklerobject *, self) |
| 1812 | ARGDECL(PyObject *, args) |
| 1813 | { |
| 1814 | PyObject *py_int = 0; |
| 1815 | char *s, *endptr; |
| 1816 | int len; |
| 1817 | long l; |
| 1818 | |
| 1819 | if ((len = (*self->readline_func)(self, &s)) == -1) |
| 1820 | return NULL; |
| 1821 | |
| 1822 | errno = 0; |
| 1823 | l = strtol(s, &endptr, 0); |
| 1824 | |
| 1825 | free(s); |
| 1826 | |
| 1827 | if (errno || strlen(endptr)) |
| 1828 | { |
| 1829 | PyErr_SetString(PyExc_ValueError, "could not convert string to int"); |
| 1830 | goto err; |
| 1831 | } |
| 1832 | |
| 1833 | UNLESS(py_int = PyInt_FromLong(l)) |
| 1834 | goto err; |
| 1835 | |
| 1836 | if (PyList_Append(self->stack, py_int) == -1) |
| 1837 | goto err; |
| 1838 | |
| 1839 | Py_DECREF(py_int); |
| 1840 | |
| 1841 | Py_INCREF(Py_None); |
| 1842 | return Py_None; |
| 1843 | |
| 1844 | err: |
| 1845 | Py_XDECREF(py_int); |
| 1846 | |
| 1847 | return NULL; |
| 1848 | } |
| 1849 | |
| 1850 | |
| 1851 | static PyObject * |
| 1852 | load_binint(ARG(Unpicklerobject *, self), ARG(PyObject *, args)) |
| 1853 | ARGDECL(Unpicklerobject *, self) |
| 1854 | ARGDECL(PyObject *, args) |
| 1855 | { |
| 1856 | PyObject *py_int = 0; |
| 1857 | char *s; |
| 1858 | unsigned char c; |
| 1859 | long l; |
| 1860 | |
| 1861 | UNLESS(s = (char *)malloc(4 * sizeof(char))) |
| 1862 | { |
| 1863 | PyErr_SetString(PyExc_MemoryError, "out of memory"); |
| 1864 | return NULL; |
| 1865 | } |
| 1866 | |
| 1867 | if ((*self->read_func)(self, &s, 4) == -1) |
| 1868 | { |
| 1869 | free(s); |
| 1870 | return NULL; |
| 1871 | } |
| 1872 | |
| 1873 | c = (unsigned char)s[0]; |
| 1874 | l = (long)c; |
| 1875 | c = (unsigned char)s[1]; |
| 1876 | l |= (long)c << 8; |
| 1877 | c = (unsigned char)s[2]; |
| 1878 | l |= (long)c << 16; |
| 1879 | c = (unsigned char)s[3]; |
| 1880 | l |= (long)c << 24; |
| 1881 | |
| 1882 | free(s); |
| 1883 | |
| 1884 | UNLESS(py_int = PyInt_FromLong(l)) |
| 1885 | return NULL; |
| 1886 | |
| 1887 | if (PyList_Append(self->stack, py_int) == -1) |
| 1888 | { |
| 1889 | Py_DECREF(py_int); |
| 1890 | return NULL; |
| 1891 | } |
| 1892 | |
| 1893 | Py_DECREF(py_int); |
| 1894 | |
| 1895 | Py_INCREF(Py_None); |
| 1896 | return Py_None; |
| 1897 | } |
| 1898 | |
| 1899 | |
| 1900 | static PyObject * |
| 1901 | load_binint1(ARG(Unpicklerobject *, self), ARG(PyObject *, args)) |
| 1902 | ARGDECL(Unpicklerobject *, self) |
| 1903 | ARGDECL(PyObject *, args) |
| 1904 | { |
| 1905 | PyObject *py_int = 0; |
| 1906 | char *s; |
| 1907 | unsigned char c; |
| 1908 | long l; |
| 1909 | |
| 1910 | UNLESS(s = (char *)malloc(3 * sizeof(char))) |
| 1911 | { |
| 1912 | PyErr_SetString(PyExc_MemoryError, "out of memory"); |
| 1913 | return NULL; |
| 1914 | } |
| 1915 | |
| 1916 | if ((*self->read_func)(self, &s, 3) == -1) |
| 1917 | { |
| 1918 | free(s); |
| 1919 | return NULL; |
| 1920 | } |
| 1921 | |
| 1922 | c = (unsigned char)s[0]; |
| 1923 | l = (long)c; |
| 1924 | c = (unsigned char)s[1]; |
| 1925 | l |= (long)c << 8; |
| 1926 | c = (unsigned char)s[2]; |
| 1927 | l |= (long)c << 16; |
| 1928 | |
| 1929 | free(s); |
| 1930 | |
| 1931 | UNLESS(py_int = PyInt_FromLong(l)) |
| 1932 | return NULL; |
| 1933 | |
| 1934 | if (PyList_Append(self->stack, py_int) == -1) |
| 1935 | { |
| 1936 | Py_DECREF(py_int); |
| 1937 | return NULL; |
| 1938 | } |
| 1939 | |
| 1940 | Py_DECREF(py_int); |
| 1941 | |
| 1942 | Py_INCREF(Py_None); |
| 1943 | return Py_None; |
| 1944 | } |
| 1945 | |
| 1946 | |
| 1947 | static PyObject * |
| 1948 | load_binint2(ARG(Unpicklerobject *, self), ARG(PyObject *, args)) |
| 1949 | ARGDECL(Unpicklerobject *, self) |
| 1950 | ARGDECL(PyObject *, args) |
| 1951 | { |
| 1952 | PyObject *py_int = 0; |
| 1953 | char *s; |
| 1954 | unsigned char c; |
| 1955 | long l; |
| 1956 | |
| 1957 | UNLESS(s = (char *)malloc(2 * sizeof(char))) |
| 1958 | { |
| 1959 | PyErr_SetString(PyExc_MemoryError, "out of memory"); |
| 1960 | return NULL; |
| 1961 | } |
| 1962 | |
| 1963 | if ((*self->read_func)(self, &s, 2) == -1) |
| 1964 | return NULL; |
| 1965 | |
| 1966 | c = (unsigned char)s[0]; |
| 1967 | l = (long)c; |
| 1968 | c = (unsigned char)s[1]; |
| 1969 | l |= (long)c << 8; |
| 1970 | |
| 1971 | free(s); |
| 1972 | |
| 1973 | UNLESS(py_int = PyInt_FromLong(l)) |
| 1974 | return NULL; |
| 1975 | |
| 1976 | if (PyList_Append(self->stack, py_int) == -1) |
| 1977 | { |
| 1978 | Py_DECREF(py_int); |
| 1979 | return NULL; |
| 1980 | } |
| 1981 | |
| 1982 | Py_DECREF(py_int); |
| 1983 | |
| 1984 | Py_INCREF(Py_None); |
| 1985 | return Py_None; |
| 1986 | } |
| 1987 | |
| 1988 | |
| 1989 | static PyObject * |
| 1990 | load_binint3(ARG(Unpicklerobject *, self), ARG(PyObject *, args)) |
| 1991 | ARGDECL(Unpicklerobject *, self) |
| 1992 | ARGDECL(PyObject *, args) |
| 1993 | { |
| 1994 | PyObject *py_int = 0; |
| 1995 | char *s; |
| 1996 | unsigned char c; |
| 1997 | long l; |
| 1998 | |
| 1999 | UNLESS(s = (char *)malloc(sizeof(char))) |
| 2000 | { |
| 2001 | PyErr_SetString(PyExc_MemoryError, "out of memory"); |
| 2002 | return NULL; |
| 2003 | } |
| 2004 | |
| 2005 | if ((*self->read_func)(self, &s, 1) == -1) |
| 2006 | { |
| 2007 | free(s); |
| 2008 | return NULL; |
| 2009 | } |
| 2010 | |
| 2011 | c = (unsigned char)s[0]; |
| 2012 | l = (long)c; |
| 2013 | |
| 2014 | free(s); |
| 2015 | |
| 2016 | UNLESS(py_int = PyInt_FromLong(l)) |
| 2017 | return NULL; |
| 2018 | |
| 2019 | if (PyList_Append(self->stack, py_int) == -1) |
| 2020 | { |
| 2021 | Py_DECREF(py_int); |
| 2022 | return NULL; |
| 2023 | } |
| 2024 | |
| 2025 | Py_DECREF(py_int); |
| 2026 | |
| 2027 | Py_INCREF(Py_None); |
| 2028 | return Py_None; |
| 2029 | } |
| 2030 | |
| 2031 | |
| 2032 | static PyObject * |
| 2033 | load_long(ARG(Unpicklerobject *, self), ARG(PyObject *, args)) |
| 2034 | ARGDECL(Unpicklerobject *, self) |
| 2035 | ARGDECL(PyObject *, args) |
| 2036 | { |
| 2037 | PyObject *py_str = 0, *l = 0; |
| 2038 | char *s; |
| 2039 | int len; |
| 2040 | |
| 2041 | static PyObject *arg; |
| 2042 | |
| 2043 | if ((len = (*self->readline_func)(self, &s)) == -1) |
| 2044 | return NULL; |
| 2045 | |
| 2046 | UNLESS(py_str = PyString_FromStringAndSize(s, len)) |
| 2047 | { |
| 2048 | free(s); |
| 2049 | return NULL; |
| 2050 | } |
| 2051 | |
| 2052 | free(s); |
| 2053 | |
| 2054 | UNLESS(arg) |
| 2055 | { |
| 2056 | UNLESS(arg = Py_BuildValue("(Oi)", py_str, 0)) |
| 2057 | return NULL; |
| 2058 | } |
| 2059 | else |
| 2060 | { |
| 2061 | if (PyTuple_SetItem(arg, 0, py_str) == -1) |
| 2062 | goto err; |
| 2063 | Py_INCREF(py_str); |
| 2064 | } |
| 2065 | |
| 2066 | UNLESS(l = PyObject_CallObject(atol_func, arg)) |
| 2067 | goto err; |
| 2068 | |
| 2069 | if (PyList_Append(self->stack, l) == -1) |
| 2070 | goto err; |
| 2071 | |
| 2072 | Py_DECREF(py_str); |
| 2073 | Py_DECREF(l); |
| 2074 | |
| 2075 | Py_INCREF(Py_None); |
| 2076 | return Py_None; |
| 2077 | |
| 2078 | err: |
| 2079 | Py_XDECREF(l); |
| 2080 | Py_XDECREF(py_str); |
| 2081 | |
| 2082 | return NULL; |
| 2083 | } |
| 2084 | |
| 2085 | |
| 2086 | static PyObject * |
| 2087 | load_float(ARG(Unpicklerobject *, self), ARG(PyObject *, args)) |
| 2088 | ARGDECL(Unpicklerobject *, self) |
| 2089 | ARGDECL(PyObject *, args) |
| 2090 | { |
| 2091 | PyObject *py_float = 0; |
| 2092 | char *s, *endptr; |
| 2093 | int len; |
| 2094 | double d; |
| 2095 | |
| 2096 | if ((len = (*self->readline_func)(self, &s)) == -1) |
| 2097 | return NULL; |
| 2098 | |
| 2099 | errno = 0; |
| 2100 | d = strtod(s, &endptr); |
| 2101 | |
| 2102 | free(s); |
| 2103 | |
| 2104 | if (errno || strlen(endptr)) |
| 2105 | { |
| 2106 | PyErr_SetString(PyExc_ValueError, "could not convert string to long"); |
| 2107 | goto err; |
| 2108 | } |
| 2109 | |
| 2110 | UNLESS(py_float = PyFloat_FromDouble(d)) |
| 2111 | goto err; |
| 2112 | |
| 2113 | if (PyList_Append(self->stack, py_float) == -1) |
| 2114 | goto err; |
| 2115 | |
| 2116 | Py_DECREF(py_float); |
| 2117 | |
| 2118 | Py_INCREF(Py_None); |
| 2119 | return Py_None; |
| 2120 | |
| 2121 | err: |
| 2122 | Py_XDECREF(py_float); |
| 2123 | |
| 2124 | return NULL; |
| 2125 | } |
| 2126 | |
| 2127 | |
| 2128 | static PyObject * |
| 2129 | load_string(ARG(Unpicklerobject *, self), ARG(PyObject *, args)) |
| 2130 | ARGDECL(Unpicklerobject *, self) |
| 2131 | ARGDECL(PyObject *, args) |
| 2132 | { |
| 2133 | PyObject *str = 0; |
| 2134 | char *s; |
| 2135 | int len; |
| 2136 | static PyObject *eval_dict = 0; |
| 2137 | |
| 2138 | UNLESS(eval_dict) |
| 2139 | UNLESS(eval_dict = Py_BuildValue("{s{}}", "__builtins__")) |
| 2140 | return NULL; |
| 2141 | |
| 2142 | if ((len = (*self->readline_func)(self, &s)) == -1) |
| 2143 | return NULL; |
| 2144 | |
| 2145 | UNLESS(str = PyRun_String(s, eval_input, eval_dict, eval_dict)) |
| 2146 | { |
| 2147 | free(s); |
| 2148 | goto err; |
| 2149 | } |
| 2150 | |
| 2151 | free(s); |
| 2152 | |
| 2153 | if (PyList_Append(self->stack, str) == -1) |
| 2154 | goto err; |
| 2155 | |
| 2156 | Py_DECREF(str); |
| 2157 | |
| 2158 | Py_INCREF(Py_None); |
| 2159 | return Py_None; |
| 2160 | |
| 2161 | err: |
| 2162 | Py_XDECREF(str); |
| 2163 | |
| 2164 | return NULL; |
| 2165 | } |
| 2166 | |
| 2167 | |
| 2168 | static PyObject * |
| 2169 | load_binstring(ARG(Unpicklerobject *, self), ARG(PyObject *, args)) |
| 2170 | ARGDECL(Unpicklerobject *, self) |
| 2171 | ARGDECL(PyObject *, args) |
| 2172 | { |
| 2173 | PyObject *py_string = 0; |
Barry Warsaw | 93d29b6 | 1997-01-14 17:45:08 +0000 | [diff] [blame] | 2174 | char *s; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2175 | int len, i; |
| 2176 | |
| 2177 | if ((len = (*self->readline_func)(self, &s)) == -1) |
| 2178 | return NULL; |
| 2179 | |
| 2180 | i = atoi(s); |
| 2181 | |
| 2182 | if (i > len) |
| 2183 | { |
| 2184 | free(s); |
| 2185 | |
| 2186 | UNLESS(s = (char *)malloc(i * sizeof(char))) |
| 2187 | { |
| 2188 | PyErr_SetString(PyExc_MemoryError, "out of memory"); |
| 2189 | return NULL; |
| 2190 | } |
| 2191 | } |
| 2192 | |
| 2193 | if ((*self->read_func)(self, &s, i) == -1) |
| 2194 | { |
| 2195 | free(s); |
| 2196 | return NULL; |
| 2197 | } |
| 2198 | |
| 2199 | UNLESS(py_string = PyString_FromStringAndSize(s, i)) |
| 2200 | { |
| 2201 | free(s); |
| 2202 | return NULL; |
| 2203 | } |
| 2204 | |
| 2205 | free(s); |
| 2206 | |
| 2207 | if (PyList_Append(self->stack, py_string) == -1) |
| 2208 | goto err; |
| 2209 | |
| 2210 | Py_DECREF(py_string); |
| 2211 | |
| 2212 | Py_INCREF(Py_None); |
| 2213 | return Py_None; |
| 2214 | |
| 2215 | err: |
| 2216 | Py_XDECREF(py_string); |
| 2217 | |
| 2218 | return NULL; |
| 2219 | } |
| 2220 | |
| 2221 | |
| 2222 | static PyObject * |
| 2223 | load_short_binstring(ARG(Unpicklerobject *, self), ARG(PyObject *, args)) |
| 2224 | ARGDECL(Unpicklerobject *, self) |
| 2225 | ARGDECL(PyObject *, args) |
| 2226 | { |
| 2227 | PyObject *py_string = 0; |
| 2228 | char *s; |
| 2229 | unsigned char l; |
| 2230 | |
| 2231 | |
| 2232 | UNLESS(s = (char *)malloc(sizeof(char))) |
| 2233 | { |
| 2234 | PyErr_SetString(PyExc_MemoryError, "out of memory"); |
| 2235 | return NULL; |
| 2236 | } |
| 2237 | |
| 2238 | if ((*self->read_func)(self, &s, 1) == -1) |
| 2239 | { |
| 2240 | free(s); |
| 2241 | return NULL; |
| 2242 | } |
| 2243 | |
| 2244 | l = (unsigned char)s[0]; |
| 2245 | |
| 2246 | if (l > 1) |
| 2247 | { |
| 2248 | free(s); |
| 2249 | |
| 2250 | UNLESS(s = (char *)malloc(l * sizeof(char))) |
| 2251 | { |
| 2252 | PyErr_SetString(PyExc_MemoryError, "out of memory"); |
| 2253 | return NULL; |
| 2254 | } |
| 2255 | } |
| 2256 | |
| 2257 | if ((*self->read_func)(self, &s, l) == -1) |
| 2258 | { |
| 2259 | free(s); |
| 2260 | return NULL; |
| 2261 | } |
| 2262 | |
| 2263 | UNLESS(py_string = PyString_FromStringAndSize(s, l)) |
| 2264 | { |
| 2265 | free(s); |
| 2266 | return NULL; |
| 2267 | } |
| 2268 | |
| 2269 | free(s); |
| 2270 | |
| 2271 | if (PyList_Append(self->stack, py_string) == -1) |
| 2272 | { |
| 2273 | Py_DECREF(py_string); |
| 2274 | return NULL; |
| 2275 | } |
| 2276 | |
| 2277 | Py_DECREF(py_string); |
| 2278 | |
| 2279 | Py_INCREF(Py_None); |
| 2280 | return Py_None; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2281 | } |
| 2282 | |
| 2283 | |
| 2284 | static PyObject * |
| 2285 | load_tuple(ARG(Unpicklerobject *, self), ARG(PyObject *, args)) |
| 2286 | ARGDECL(Unpicklerobject *, self) |
| 2287 | ARGDECL(PyObject *, args) |
| 2288 | { |
| 2289 | PyObject *tup = 0, *slice = 0, *list = 0; |
| 2290 | int i, j; |
| 2291 | |
| 2292 | if ((i = marker(self)) == -1) |
| 2293 | return NULL; |
| 2294 | |
| 2295 | if ((j = PyList_Size(self->stack)) == -1) |
| 2296 | goto err; |
| 2297 | |
| 2298 | UNLESS(slice = PyList_GetSlice(self->stack, i, j)) |
| 2299 | goto err; |
| 2300 | |
| 2301 | UNLESS(tup = PySequence_Tuple(slice)) |
| 2302 | goto err; |
| 2303 | |
| 2304 | UNLESS(list = PyList_New(1)) |
| 2305 | goto err; |
| 2306 | |
| 2307 | if (PyList_SetItem(list, 0, tup) == -1) |
| 2308 | goto err; |
| 2309 | |
| 2310 | Py_INCREF(tup); |
| 2311 | |
| 2312 | if (PyList_SetSlice(self->stack, i, j, list) == -1) |
| 2313 | goto err; |
| 2314 | |
| 2315 | Py_DECREF(tup); |
| 2316 | Py_DECREF(list); |
| 2317 | Py_DECREF(slice); |
| 2318 | |
| 2319 | Py_INCREF(Py_None); |
| 2320 | return Py_None; |
| 2321 | |
| 2322 | err: |
| 2323 | Py_XDECREF(tup); |
| 2324 | Py_XDECREF(list); |
| 2325 | Py_XDECREF(slice); |
| 2326 | |
| 2327 | return NULL; |
| 2328 | } |
| 2329 | |
| 2330 | |
| 2331 | static PyObject * |
| 2332 | load_list(ARG(Unpicklerobject *, self), ARG(PyObject *, args)) |
| 2333 | ARGDECL(Unpicklerobject *, self) |
| 2334 | ARGDECL(PyObject *, args) |
| 2335 | { |
| 2336 | PyObject *list = 0, *slice = 0; |
| 2337 | int i, j; |
| 2338 | |
| 2339 | if ((i = marker(self)) == -1) |
| 2340 | return NULL; |
| 2341 | |
| 2342 | if ((j = PyList_Size(self->stack)) == -1) |
| 2343 | goto err; |
| 2344 | |
| 2345 | UNLESS(slice = PyList_GetSlice(self->stack, i, j)) |
| 2346 | goto err; |
| 2347 | |
| 2348 | UNLESS(list = PyList_New(1)) |
| 2349 | goto err; |
| 2350 | |
| 2351 | if (PyList_SetItem(list, 0, slice) == -1) |
| 2352 | goto err; |
| 2353 | Py_INCREF(slice); |
| 2354 | |
| 2355 | if (PyList_SetSlice(self->stack, i, j, list) == -1) |
| 2356 | goto err; |
| 2357 | |
| 2358 | Py_DECREF(list); |
| 2359 | Py_DECREF(slice); |
| 2360 | |
| 2361 | Py_INCREF(Py_None); |
| 2362 | return Py_None; |
| 2363 | |
| 2364 | err: |
| 2365 | Py_XDECREF(list); |
| 2366 | Py_XDECREF(slice); |
| 2367 | |
| 2368 | return NULL; |
| 2369 | } |
| 2370 | |
| 2371 | |
| 2372 | static PyObject * |
| 2373 | load_dict(ARG(Unpicklerobject *, self), ARG(PyObject *, args)) |
| 2374 | ARGDECL(Unpicklerobject *, self) |
| 2375 | ARGDECL(PyObject *, args) |
| 2376 | { |
| 2377 | PyObject *list = 0, *dict = 0, *key = 0, *value = 0; |
| 2378 | int i, j, k; |
| 2379 | |
| 2380 | if ((i = marker(self)) == -1) |
| 2381 | return NULL; |
| 2382 | |
| 2383 | if ((j = PyList_Size(self->stack)) == -1) |
| 2384 | goto err; |
| 2385 | |
| 2386 | UNLESS(dict = PyDict_New()) |
| 2387 | goto err; |
| 2388 | |
| 2389 | for (k = i; k < j; k += 2) |
| 2390 | { |
| 2391 | UNLESS(key = PyList_GET_ITEM((PyListObject *)self->stack, k)) |
| 2392 | goto err; |
| 2393 | |
| 2394 | UNLESS(value = PyList_GET_ITEM((PyListObject *)self->stack, k + 1)) |
| 2395 | goto err; |
| 2396 | |
| 2397 | if (PyDict_SetItem(dict, key, value) == -1) |
| 2398 | goto err; |
| 2399 | } |
| 2400 | |
| 2401 | UNLESS(list = PyList_New(1)) |
| 2402 | goto err; |
| 2403 | |
| 2404 | if (PyList_SetItem(list, 0, dict) == -1) |
| 2405 | goto err; |
| 2406 | Py_INCREF(dict); |
| 2407 | |
| 2408 | if (PyList_SetSlice(self->stack, i, j, list) == -1) |
| 2409 | goto err; |
| 2410 | |
| 2411 | Py_DECREF(list); |
| 2412 | Py_DECREF(dict); |
| 2413 | |
| 2414 | Py_INCREF(Py_None); |
| 2415 | return Py_None; |
| 2416 | |
| 2417 | err: |
| 2418 | Py_XDECREF(dict); |
| 2419 | Py_XDECREF(list); |
| 2420 | |
| 2421 | return NULL; |
| 2422 | } |
| 2423 | |
| 2424 | |
| 2425 | static PyObject * |
| 2426 | load_obj(ARG(Unpicklerobject *, self), ARG(PyObject *, args)) |
| 2427 | ARGDECL(Unpicklerobject *, self) |
| 2428 | ARGDECL(PyObject *, args) |
| 2429 | { |
| 2430 | PyObject *class = 0, *slice = 0, *tup = 0, *obj = 0; |
| 2431 | long i; |
| 2432 | int len; |
| 2433 | |
| 2434 | if ((i = marker(self)) == -1) |
| 2435 | return NULL; |
| 2436 | |
| 2437 | class = PyList_GET_ITEM((PyListObject *)self->stack, i); |
| 2438 | Py_INCREF(class); |
| 2439 | |
| 2440 | if ((len = PyList_Size(self->stack)) == -1) |
| 2441 | goto err; |
| 2442 | |
| 2443 | UNLESS(slice = PyList_GetSlice(self->stack, i + 1, len)) |
| 2444 | goto err; |
| 2445 | |
| 2446 | UNLESS(tup = PySequence_Tuple(slice)) |
| 2447 | goto err; |
| 2448 | |
| 2449 | if (DEL_LIST_SLICE(self->stack, i, len) == -1) |
| 2450 | goto err; |
| 2451 | |
| 2452 | UNLESS(obj = PyInstance_New(class, tup, NULL)) |
| 2453 | goto err; |
| 2454 | |
| 2455 | if (PyList_Append(self->stack, obj) == -1) |
| 2456 | goto err; |
| 2457 | |
| 2458 | Py_DECREF(class); |
| 2459 | Py_DECREF(slice); |
| 2460 | Py_DECREF(tup); |
| 2461 | Py_DECREF(obj); |
| 2462 | |
| 2463 | Py_INCREF(Py_None); |
| 2464 | return Py_None; |
| 2465 | |
| 2466 | err: |
| 2467 | Py_XDECREF(class); |
| 2468 | Py_XDECREF(slice); |
| 2469 | Py_XDECREF(tup); |
| 2470 | Py_XDECREF(obj); |
| 2471 | |
| 2472 | return NULL; |
| 2473 | } |
| 2474 | |
| 2475 | |
| 2476 | static PyObject * |
| 2477 | load_inst(ARG(Unpicklerobject *, self), ARG(PyObject *, args)) |
| 2478 | ARGDECL(Unpicklerobject *, self) |
| 2479 | ARGDECL(PyObject *, args) |
| 2480 | { |
| 2481 | PyObject *arg_tup = 0, *arg_slice = 0, *class = 0, *obj = 0; |
Barry Warsaw | 93d29b6 | 1997-01-14 17:45:08 +0000 | [diff] [blame] | 2482 | int i, j; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2483 | char *s, *module_name, *class_name; |
| 2484 | |
| 2485 | if ((i = marker(self)) == -1) |
| 2486 | return NULL; |
| 2487 | |
| 2488 | if ((j = PyList_Size(self->stack)) == -1) |
| 2489 | goto err; |
| 2490 | |
| 2491 | UNLESS(arg_slice = PyList_GetSlice(self->stack, i, j)) |
| 2492 | goto err; |
| 2493 | |
| 2494 | UNLESS(arg_tup = PySequence_Tuple(arg_slice)) |
| 2495 | goto err; |
| 2496 | |
| 2497 | if (DEL_LIST_SLICE(self->stack, i, j) == -1) |
| 2498 | goto err; |
| 2499 | |
| 2500 | if ((*self->readline_func)(self, &s) == -1) |
| 2501 | goto err; |
| 2502 | |
| 2503 | module_name = s; |
| 2504 | |
| 2505 | if ((*self->readline_func)(self, &s) == -1) |
| 2506 | { |
| 2507 | free(module_name); |
| 2508 | goto err; |
| 2509 | } |
| 2510 | |
| 2511 | class_name = s; |
| 2512 | |
| 2513 | UNLESS(class = find_class(module_name, class_name)) |
| 2514 | { |
| 2515 | free(module_name); |
| 2516 | free(class_name); |
| 2517 | goto err; |
| 2518 | } |
| 2519 | |
| 2520 | free(module_name); |
| 2521 | free(class_name); |
| 2522 | |
| 2523 | UNLESS(obj = PyInstance_New(class, arg_tup, NULL)) |
| 2524 | goto err; |
| 2525 | |
| 2526 | if (PyList_Append(self->stack, obj) == -1) |
| 2527 | goto err; |
| 2528 | |
| 2529 | Py_DECREF(arg_slice); |
| 2530 | Py_DECREF(arg_tup); |
| 2531 | Py_DECREF(class); |
| 2532 | Py_DECREF(obj); |
| 2533 | |
| 2534 | Py_INCREF(Py_None); |
| 2535 | return Py_None; |
| 2536 | |
| 2537 | err: |
| 2538 | Py_XDECREF(arg_slice); |
| 2539 | Py_XDECREF(arg_tup); |
| 2540 | Py_XDECREF(class); |
| 2541 | Py_XDECREF(obj); |
| 2542 | |
| 2543 | return NULL; |
| 2544 | } |
| 2545 | |
| 2546 | |
| 2547 | static PyObject * |
| 2548 | load_class(ARG(Unpicklerobject *, self), ARG(PyObject *, args)) |
| 2549 | ARGDECL(Unpicklerobject *, self) |
| 2550 | ARGDECL(PyObject *, args) |
| 2551 | { |
| 2552 | PyObject *class = 0; |
| 2553 | char *s, *module_name, *class_name; |
| 2554 | |
| 2555 | if ((*self->readline_func)(self, &s) == -1) |
| 2556 | return NULL; |
| 2557 | |
| 2558 | module_name = s; |
| 2559 | |
| 2560 | if ((*self->readline_func)(self, &s) == -1) |
| 2561 | { |
| 2562 | free(module_name); |
| 2563 | return NULL; |
| 2564 | } |
| 2565 | |
| 2566 | class_name = s; |
| 2567 | |
| 2568 | UNLESS(class = find_class(module_name, class_name)) |
| 2569 | { |
| 2570 | free(module_name); |
| 2571 | free(class_name); |
| 2572 | return NULL; |
| 2573 | } |
| 2574 | |
| 2575 | free(module_name); |
| 2576 | free(class_name); |
| 2577 | |
| 2578 | if (PyList_Append(self->stack, class) == -1) |
| 2579 | goto err; |
| 2580 | |
| 2581 | Py_DECREF(class); |
| 2582 | |
| 2583 | Py_INCREF(Py_None); |
| 2584 | return Py_None; |
| 2585 | |
| 2586 | err: |
| 2587 | Py_XDECREF(class); |
| 2588 | |
| 2589 | return NULL; |
| 2590 | } |
| 2591 | |
| 2592 | |
| 2593 | static PyObject * |
| 2594 | load_persid(ARG(Unpicklerobject *, self), ARG(PyObject *, args)) |
| 2595 | ARGDECL(Unpicklerobject *, self) |
| 2596 | ARGDECL(PyObject *, args) |
| 2597 | { |
| 2598 | PyObject *pid = 0, *pers_load_val = 0; |
| 2599 | char *s; |
| 2600 | int len; |
| 2601 | |
| 2602 | if (self->pers_func) |
| 2603 | { |
| 2604 | if ((len = (*self->readline_func)(self, &s)) == -1) |
| 2605 | return NULL; |
| 2606 | |
| 2607 | UNLESS(pid = PyString_FromStringAndSize(s, len)) |
| 2608 | { |
| 2609 | free(s); |
| 2610 | return NULL; |
| 2611 | } |
| 2612 | |
| 2613 | free(s); |
| 2614 | |
| 2615 | UNLESS(self->arg) |
| 2616 | UNLESS(self->arg = PyTuple_New(1)) |
| 2617 | goto err; |
| 2618 | |
| 2619 | if (PyTuple_SetItem(self->arg, 0, pid) == -1) |
| 2620 | goto err; |
| 2621 | Py_INCREF(pid); |
| 2622 | |
| 2623 | UNLESS(pers_load_val = PyObject_CallObject(self->pers_func, self->arg)) |
| 2624 | goto err; |
| 2625 | |
| 2626 | if (PyList_Append(self->stack, pers_load_val) == -1) |
| 2627 | goto err; |
| 2628 | |
| 2629 | Py_DECREF(pid); |
| 2630 | Py_DECREF(pers_load_val); |
| 2631 | |
| 2632 | Py_INCREF(Py_None); |
| 2633 | return Py_None; |
| 2634 | } |
| 2635 | |
| 2636 | Py_INCREF(Py_None); |
| 2637 | return Py_None; |
| 2638 | |
| 2639 | err: |
| 2640 | Py_XDECREF(pid); |
| 2641 | Py_XDECREF(pers_load_val); |
| 2642 | |
| 2643 | return NULL; |
| 2644 | } |
| 2645 | |
| 2646 | |
| 2647 | static PyObject * |
| 2648 | load_pop(ARG(Unpicklerobject *, self), ARG(PyObject *, args)) |
| 2649 | ARGDECL(Unpicklerobject *, self) |
| 2650 | ARGDECL(PyObject *, args) |
| 2651 | { |
| 2652 | int len; |
| 2653 | |
| 2654 | if ((len = PyList_Size(self->stack)) == -1) |
| 2655 | return NULL; |
| 2656 | |
| 2657 | if (DEL_LIST_SLICE(self->stack, len - 1, len) == -1) |
| 2658 | return NULL; |
| 2659 | |
| 2660 | Py_INCREF(Py_None); |
| 2661 | return Py_None; |
| 2662 | } |
| 2663 | |
| 2664 | |
| 2665 | static PyObject * |
| 2666 | load_dup(ARG(Unpicklerobject *, self), ARG(PyObject *, args)) |
| 2667 | ARGDECL(Unpicklerobject *, self) |
| 2668 | ARGDECL(PyObject *, args) |
| 2669 | { |
| 2670 | PyObject *last; |
| 2671 | int len; |
| 2672 | |
| 2673 | if ((len = PyList_Size(self->stack)) == -1) |
| 2674 | return NULL; |
| 2675 | |
| 2676 | UNLESS(last = PyList_GetItem(self->stack, len - 1)) |
| 2677 | return NULL; |
| 2678 | |
| 2679 | if (PyList_Append(self->stack, last) == -1) |
| 2680 | return NULL; |
| 2681 | |
| 2682 | Py_INCREF(Py_None); |
| 2683 | return Py_None; |
| 2684 | } |
| 2685 | |
| 2686 | |
| 2687 | static PyObject * |
| 2688 | load_get(ARG(Unpicklerobject *, self), ARG(PyObject *, args)) |
| 2689 | ARGDECL(Unpicklerobject *, self) |
| 2690 | ARGDECL(PyObject *, args) |
| 2691 | { |
| 2692 | PyObject *py_str = 0, *value = 0; |
| 2693 | char *s; |
| 2694 | int len; |
| 2695 | |
| 2696 | if ((len = (*self->readline_func)(self, &s)) == -1) |
| 2697 | return NULL; |
| 2698 | |
| 2699 | UNLESS(py_str = PyString_FromStringAndSize(s, len)) |
| 2700 | { |
| 2701 | free(s); |
| 2702 | return NULL; |
| 2703 | } |
| 2704 | |
| 2705 | free(s); |
| 2706 | |
| 2707 | UNLESS(value = PyDict_GetItem(self->memo, py_str)) |
| 2708 | goto err; |
| 2709 | |
| 2710 | if (PyList_Append(self->stack, value) == -1) |
| 2711 | goto err; |
| 2712 | |
| 2713 | Py_DECREF(py_str); |
| 2714 | |
| 2715 | Py_INCREF(Py_None); |
| 2716 | return Py_None; |
| 2717 | |
| 2718 | err: |
| 2719 | Py_XDECREF(py_str); |
| 2720 | |
| 2721 | return NULL; |
| 2722 | } |
| 2723 | |
| 2724 | |
| 2725 | static PyObject * |
| 2726 | load_binget(ARG(Unpicklerobject *, self), ARG(PyObject *, args)) |
| 2727 | ARGDECL(Unpicklerobject *, self) |
| 2728 | ARGDECL(PyObject *, args) |
| 2729 | { |
| 2730 | PyObject *py_key = 0, *value = 0; |
| 2731 | char *s; |
| 2732 | unsigned char key; |
| 2733 | |
| 2734 | UNLESS(s = (char *)malloc(sizeof(char))) |
| 2735 | { |
| 2736 | PyErr_SetString(PyExc_MemoryError, "out of memory"); |
| 2737 | return NULL; |
| 2738 | } |
| 2739 | |
| 2740 | if ((*self->read_func)(self, &s, 1) == -1) |
| 2741 | { |
| 2742 | free(s); |
| 2743 | return NULL; |
| 2744 | } |
| 2745 | |
| 2746 | key = (unsigned char)s[0]; |
| 2747 | free(s); |
| 2748 | |
| 2749 | UNLESS(py_key = PyInt_FromLong((long)key)) |
| 2750 | return NULL; |
| 2751 | |
| 2752 | UNLESS(value = PyDict_GetItem(self->memo, py_key)) |
| 2753 | goto err; |
| 2754 | |
| 2755 | if (PyList_Append(self->stack, value) == -1) |
| 2756 | return NULL; |
| 2757 | |
| 2758 | Py_DECREF(py_key); |
| 2759 | |
| 2760 | Py_INCREF(Py_None); |
| 2761 | return Py_None; |
| 2762 | |
| 2763 | err: |
| 2764 | Py_XDECREF(py_key); |
| 2765 | |
| 2766 | return NULL; |
| 2767 | } |
| 2768 | |
| 2769 | |
| 2770 | static PyObject * |
| 2771 | load_put(ARG(Unpicklerobject *, self), ARG(PyObject *, args)) |
| 2772 | ARGDECL(Unpicklerobject *, self) |
| 2773 | ARGDECL(PyObject *, args) |
| 2774 | { |
| 2775 | PyObject *py_str = 0, *value = 0; |
| 2776 | int len; |
| 2777 | char *s; |
| 2778 | |
| 2779 | if ((len = (*self->readline_func)(self, &s)) == -1) |
| 2780 | return NULL; |
| 2781 | |
| 2782 | UNLESS(py_str = PyString_FromStringAndSize(s, len)) |
| 2783 | { |
| 2784 | free(s); |
| 2785 | return NULL; |
| 2786 | } |
| 2787 | |
| 2788 | free(s); |
| 2789 | |
| 2790 | if ((len = PyList_Size(self->stack)) == -1) |
| 2791 | goto err; |
| 2792 | |
| 2793 | UNLESS(value = PyList_GetItem(self->stack, len - 1)) |
| 2794 | goto err; |
| 2795 | |
| 2796 | if (PyDict_SetItem(self->memo, py_str, value) == -1) |
| 2797 | goto err; |
| 2798 | |
| 2799 | Py_DECREF(py_str); |
| 2800 | |
| 2801 | Py_INCREF(Py_None); |
| 2802 | return Py_None; |
| 2803 | |
| 2804 | err: |
| 2805 | Py_XDECREF(py_str); |
| 2806 | |
| 2807 | return NULL; |
| 2808 | } |
| 2809 | |
| 2810 | |
| 2811 | static PyObject * |
| 2812 | load_binput(ARG(Unpicklerobject *, self), ARG(PyObject *, args)) |
| 2813 | ARGDECL(Unpicklerobject *, self) |
| 2814 | ARGDECL(PyObject *, args) |
| 2815 | { |
| 2816 | PyObject *py_key = 0, *value = 0; |
| 2817 | char *s; |
| 2818 | unsigned char key; |
| 2819 | int len; |
| 2820 | |
| 2821 | UNLESS(s = (char *)malloc(sizeof(char))) |
| 2822 | { |
| 2823 | PyErr_SetString(PyExc_MemoryError, "out of memory"); |
| 2824 | return NULL; |
| 2825 | } |
| 2826 | |
| 2827 | if ((*self->read_func)(self, &s, 1) == -1) |
| 2828 | return NULL; |
| 2829 | |
| 2830 | key = (unsigned char)s[0]; |
| 2831 | |
| 2832 | free(s); |
| 2833 | |
| 2834 | UNLESS(py_key = PyInt_FromLong((long)key)) |
| 2835 | return NULL; |
| 2836 | |
| 2837 | if ((len = PyList_Size(self->stack)) == -1) |
| 2838 | goto err; |
| 2839 | |
| 2840 | UNLESS(value = PyList_GetItem(self->stack, len - 1)) |
| 2841 | goto err; |
| 2842 | |
| 2843 | if (PyDict_SetItem(self->memo, py_key, value) == -1) |
| 2844 | goto err; |
| 2845 | |
| 2846 | Py_DECREF(py_key); |
| 2847 | |
| 2848 | Py_INCREF(Py_None); |
| 2849 | return Py_None; |
| 2850 | |
| 2851 | err: |
| 2852 | Py_XDECREF(py_key); |
| 2853 | |
| 2854 | return NULL; |
| 2855 | } |
| 2856 | |
| 2857 | |
| 2858 | static PyObject * |
| 2859 | load_append(ARG(Unpicklerobject *, self), ARG(PyObject *, args)) |
| 2860 | ARGDECL(Unpicklerobject *, self) |
| 2861 | ARGDECL(PyObject *, args) |
| 2862 | { |
| 2863 | PyObject *value = 0, *list = 0; |
| 2864 | int len; |
| 2865 | static PyObject *append_str = 0; |
| 2866 | |
| 2867 | UNLESS(append_str) |
| 2868 | UNLESS(append_str = PyString_FromString("append")) |
| 2869 | return NULL; |
| 2870 | |
| 2871 | if ((len = PyList_Size(self->stack)) == -1) |
| 2872 | return NULL; |
| 2873 | |
| 2874 | UNLESS(value = PyList_GetItem(self->stack, len - 1)) |
| 2875 | return NULL; |
| 2876 | Py_INCREF(value); |
| 2877 | |
| 2878 | if (DEL_LIST_SLICE(self->stack, len - 1, len) == -1) |
| 2879 | goto err; |
| 2880 | |
| 2881 | UNLESS(list = PyList_GetItem(self->stack, len - 2)) |
| 2882 | goto err; |
| 2883 | |
| 2884 | if (PyList_Check(list)) |
| 2885 | { |
| 2886 | if (PyList_Append(list, value) == -1) |
| 2887 | goto err; |
| 2888 | } |
| 2889 | else |
| 2890 | { |
| 2891 | PyObject *append_method, *junk; |
| 2892 | |
| 2893 | UNLESS(append_method = PyObject_GetAttr(list, append_str)) |
| 2894 | return NULL; |
| 2895 | |
| 2896 | UNLESS(self->arg) |
| 2897 | UNLESS(self->arg = PyTuple_New(1)) |
| 2898 | { |
| 2899 | Py_DECREF(append_method); |
| 2900 | goto err; |
| 2901 | } |
| 2902 | |
| 2903 | if (PyTuple_SetItem(self->arg, 0, value) == -1) |
| 2904 | { |
| 2905 | Py_DECREF(append_method); |
| 2906 | goto err; |
| 2907 | } |
| 2908 | Py_INCREF(value); |
| 2909 | |
| 2910 | UNLESS(junk = PyObject_CallObject(append_method, self->arg)) |
| 2911 | { |
| 2912 | Py_DECREF(append_method); |
| 2913 | goto err; |
| 2914 | } |
| 2915 | Py_DECREF(junk); |
| 2916 | |
| 2917 | Py_DECREF(append_method); |
| 2918 | } |
| 2919 | |
| 2920 | Py_DECREF(value); |
| 2921 | |
| 2922 | Py_INCREF(Py_None); |
| 2923 | return Py_None; |
| 2924 | |
| 2925 | err: |
| 2926 | Py_XDECREF(value); |
| 2927 | |
| 2928 | return NULL; |
| 2929 | } |
| 2930 | |
| 2931 | |
| 2932 | static PyObject * |
| 2933 | load_setitem(ARG(Unpicklerobject *, self), ARG(PyObject *, args)) |
| 2934 | ARGDECL(Unpicklerobject *, self) |
| 2935 | ARGDECL(PyObject *, args) |
| 2936 | { |
| 2937 | PyObject *value = 0, *key = 0, *dict = 0; |
| 2938 | int len; |
| 2939 | |
| 2940 | if ((len = PyList_Size(self->stack)) == -1) |
| 2941 | return NULL; |
| 2942 | |
| 2943 | UNLESS(value = PyList_GetItem(self->stack, len - 1)) |
| 2944 | return NULL; |
| 2945 | Py_INCREF(value); |
| 2946 | |
| 2947 | UNLESS(key = PyList_GetItem(self->stack, len - 2)) |
| 2948 | goto err; |
| 2949 | Py_INCREF(key); |
| 2950 | |
| 2951 | if (DEL_LIST_SLICE(self->stack, len - 2, len) == -1) |
| 2952 | goto err; |
| 2953 | |
| 2954 | UNLESS(dict = PyList_GetItem(self->stack, len - 3)) |
| 2955 | goto err; |
| 2956 | |
| 2957 | if (PyObject_SetItem(dict, key, value) == -1) |
| 2958 | goto err; |
| 2959 | |
| 2960 | Py_DECREF(value); |
| 2961 | Py_DECREF(key); |
| 2962 | |
| 2963 | Py_INCREF(Py_None); |
| 2964 | return Py_None; |
| 2965 | |
| 2966 | err: |
| 2967 | Py_XDECREF(value); |
| 2968 | Py_XDECREF(key); |
| 2969 | |
| 2970 | return NULL; |
| 2971 | } |
| 2972 | |
| 2973 | |
| 2974 | static PyObject * |
| 2975 | load_build(ARG(Unpicklerobject *, self), ARG(PyObject *, args)) |
| 2976 | ARGDECL(Unpicklerobject *, self) |
| 2977 | ARGDECL(PyObject *, args) |
| 2978 | { |
| 2979 | PyObject *value = 0, *inst = 0, *instdict = 0, *d_key = 0, *d_value = 0, |
| 2980 | *junk = 0; |
| 2981 | static PyObject *py_string__dict__; |
| 2982 | int len, i; |
| 2983 | |
| 2984 | UNLESS(py_string__dict__) |
| 2985 | UNLESS(py_string__dict__ = PyString_FromString("__dict__")) |
| 2986 | return NULL; |
| 2987 | |
| 2988 | if ((len = PyList_Size(self->stack)) == -1) |
| 2989 | goto err; |
| 2990 | |
| 2991 | UNLESS(value = PyList_GetItem(self->stack, len - 1)) |
| 2992 | goto err; |
| 2993 | Py_INCREF(value); |
| 2994 | |
| 2995 | if (DEL_LIST_SLICE(self->stack, len - 1, len) == -1) |
| 2996 | goto err; |
| 2997 | |
| 2998 | UNLESS(inst = PyList_GetItem(self->stack, len - 2)) |
| 2999 | goto err; |
| 3000 | |
| 3001 | UNLESS(PyObject_HasAttrString(inst, "__setstate__")) |
| 3002 | { |
| 3003 | UNLESS(instdict = PyObject_GetAttr(inst, py_string__dict__)) |
| 3004 | goto err; |
| 3005 | |
| 3006 | i = 0; |
| 3007 | while (PyDict_Next(value, &i, &d_key, &d_value)) |
| 3008 | { |
| 3009 | if (PyObject_SetItem(instdict, d_key, d_value) == -1) |
| 3010 | goto err; |
| 3011 | } |
| 3012 | } |
| 3013 | else |
| 3014 | { |
| 3015 | UNLESS(junk = PyObject_CallMethod(inst, "__setstate__", "O", value)) |
| 3016 | goto err; |
| 3017 | Py_DECREF(junk); |
| 3018 | } |
| 3019 | |
| 3020 | Py_DECREF(value); |
| 3021 | Py_XDECREF(instdict); |
| 3022 | |
| 3023 | Py_INCREF(Py_None); |
| 3024 | return Py_None; |
| 3025 | |
| 3026 | err: |
| 3027 | Py_XDECREF(value); |
| 3028 | Py_XDECREF(instdict); |
| 3029 | |
| 3030 | return NULL; |
| 3031 | } |
| 3032 | |
| 3033 | |
| 3034 | static PyObject * |
| 3035 | load_mark(ARG(Unpicklerobject *, self), ARG(PyObject *, args)) |
| 3036 | ARGDECL(Unpicklerobject *, self) |
| 3037 | ARGDECL(PyObject *, args) |
| 3038 | { |
| 3039 | int len; |
| 3040 | |
| 3041 | if ((len = PyList_Size(self->stack)) == -1) |
| 3042 | return NULL; |
| 3043 | |
| 3044 | if (!self->num_marks) |
| 3045 | { |
| 3046 | UNLESS(self->marks = (int *)malloc(20 * sizeof(int))) |
| 3047 | { |
| 3048 | PyErr_SetString(PyExc_MemoryError, "out of memory"); |
| 3049 | return NULL; |
| 3050 | } |
| 3051 | |
| 3052 | self->marks_size = 20; |
| 3053 | } |
| 3054 | else if ((self->num_marks + 1) > self->marks_size) |
| 3055 | { |
| 3056 | UNLESS(self->marks = (int *)realloc(self->marks, (self->marks_size + 20) * sizeof(int))) |
| 3057 | { |
| 3058 | PyErr_SetString(PyExc_MemoryError, "out of memory"); |
| 3059 | return NULL; |
| 3060 | } |
| 3061 | |
| 3062 | self->marks_size += 20; |
| 3063 | } |
| 3064 | |
| 3065 | self->marks[self->num_marks++] = len; |
| 3066 | |
| 3067 | Py_INCREF(Py_None); |
| 3068 | return Py_None; |
| 3069 | } |
| 3070 | |
| 3071 | |
| 3072 | static PyObject * |
| 3073 | load_eof(ARG(Unpicklerobject *, self), ARG(PyObject *, args)) |
| 3074 | ARGDECL(Unpicklerobject *, self) |
| 3075 | ARGDECL(PyObject *, args) |
| 3076 | { |
| 3077 | PyErr_SetNone(PyExc_EOFError); |
| 3078 | return NULL; |
| 3079 | } |
| 3080 | |
| 3081 | |
| 3082 | static PyObject * |
| 3083 | Unpickler_load(ARG(Unpicklerobject *, self), ARG(PyObject *, args)) |
| 3084 | ARGDECL(Unpicklerobject *, self) |
| 3085 | ARGDECL(PyObject *, args) |
| 3086 | { |
| 3087 | PyObject *stack = 0, *key = 0, *junk = 0, *err = 0, |
Barry Warsaw | 93d29b6 | 1997-01-14 17:45:08 +0000 | [diff] [blame] | 3088 | *val = 0, *str = 0, *key_repr = 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3089 | char c; |
| 3090 | char *c_str; |
| 3091 | int len; |
| 3092 | |
| 3093 | c_str=&c; |
| 3094 | |
| 3095 | UNLESS(stack = PyList_New(0)) |
| 3096 | goto err; |
| 3097 | |
| 3098 | self->stack = stack; |
| 3099 | self->num_marks = 0; |
| 3100 | |
| 3101 | while (1) |
| 3102 | { |
| 3103 | if ((*self->read_func)(self, &c_str, 1) == -1) |
| 3104 | break; |
| 3105 | |
| 3106 | switch (c_str[0]) |
| 3107 | { |
| 3108 | case NONE: |
| 3109 | UNLESS(junk = load_none(self, NULL)) |
| 3110 | break; |
| 3111 | continue; |
| 3112 | |
| 3113 | case BININT: |
| 3114 | UNLESS(junk = load_binint(self, NULL)) |
| 3115 | break; |
| 3116 | continue; |
| 3117 | |
| 3118 | case BININT1: |
| 3119 | UNLESS(junk = load_binint1(self, NULL)) |
| 3120 | break; |
| 3121 | continue; |
| 3122 | |
| 3123 | case BININT2: |
| 3124 | UNLESS(junk = load_binint2(self, NULL)) |
| 3125 | break; |
| 3126 | continue; |
| 3127 | |
| 3128 | case BININT3: |
| 3129 | UNLESS(junk = load_binint3(self, NULL)) |
| 3130 | break; |
| 3131 | continue; |
| 3132 | |
| 3133 | case INT: |
| 3134 | UNLESS(junk = load_int(self, NULL)) |
| 3135 | break; |
| 3136 | continue; |
| 3137 | |
| 3138 | case LONG: |
| 3139 | UNLESS(junk = load_long(self, NULL)) |
| 3140 | break; |
| 3141 | continue; |
| 3142 | |
| 3143 | case FLOAT: |
| 3144 | UNLESS(junk = load_float(self, NULL)) |
| 3145 | break; |
| 3146 | continue; |
| 3147 | |
| 3148 | case BINSTRING: |
| 3149 | UNLESS(junk = load_binstring(self, NULL)) |
| 3150 | break; |
| 3151 | continue; |
| 3152 | |
| 3153 | case SHORT_BINSTRING: |
| 3154 | UNLESS(junk = load_short_binstring(self, NULL)) |
| 3155 | break; |
| 3156 | continue; |
| 3157 | |
| 3158 | case STRING: |
| 3159 | UNLESS(junk = load_string(self, NULL)) |
| 3160 | break; |
| 3161 | Py_DECREF(junk); |
| 3162 | continue; |
| 3163 | |
| 3164 | case TUPLE: |
| 3165 | UNLESS(junk = load_tuple(self, NULL)) |
| 3166 | break; |
| 3167 | Py_DECREF(junk); |
| 3168 | continue; |
| 3169 | |
| 3170 | case LIST: |
| 3171 | UNLESS(junk = load_list(self, NULL)) |
| 3172 | break; |
| 3173 | Py_DECREF(junk); |
| 3174 | continue; |
| 3175 | |
| 3176 | case DICT: |
| 3177 | UNLESS(junk = load_dict(self, NULL)) |
| 3178 | break; |
| 3179 | Py_DECREF(junk); |
| 3180 | continue; |
| 3181 | |
| 3182 | case OBJ: |
| 3183 | UNLESS(junk = load_obj(self, NULL)) |
| 3184 | break; |
| 3185 | Py_DECREF(junk); |
| 3186 | continue; |
| 3187 | |
| 3188 | case INST: |
| 3189 | UNLESS(junk = load_inst(self, NULL)) |
| 3190 | break; |
| 3191 | Py_DECREF(junk); |
| 3192 | continue; |
| 3193 | |
| 3194 | case CLASS: |
| 3195 | UNLESS(junk = load_class(self, NULL)) |
| 3196 | break; |
| 3197 | Py_DECREF(junk); |
| 3198 | continue; |
| 3199 | |
| 3200 | case APPEND: |
| 3201 | UNLESS(junk = load_append(self, NULL)) |
| 3202 | break; |
| 3203 | Py_DECREF(junk); |
| 3204 | continue; |
| 3205 | |
| 3206 | case BUILD: |
| 3207 | UNLESS(junk = load_build(self, NULL)) |
| 3208 | break; |
| 3209 | Py_DECREF(junk); |
| 3210 | continue; |
| 3211 | |
| 3212 | case DUP: |
| 3213 | UNLESS(junk = load_dup(self, NULL)) |
| 3214 | break; |
| 3215 | Py_DECREF(junk); |
| 3216 | continue; |
| 3217 | |
| 3218 | case BINGET: |
| 3219 | UNLESS(junk = load_binget(self, NULL)) |
| 3220 | break; |
| 3221 | Py_DECREF(junk); |
| 3222 | continue; |
| 3223 | |
| 3224 | case GET: |
| 3225 | UNLESS(junk = load_get(self, NULL)) |
| 3226 | break; |
| 3227 | Py_DECREF(junk); |
| 3228 | continue; |
| 3229 | |
| 3230 | case MARK: |
| 3231 | UNLESS(junk = load_mark(self, NULL)) |
| 3232 | break; |
| 3233 | Py_DECREF(junk); |
| 3234 | continue; |
| 3235 | |
| 3236 | case BINPUT: |
| 3237 | UNLESS(junk = load_binput(self, NULL)) |
| 3238 | break; |
| 3239 | Py_DECREF(junk); |
| 3240 | continue; |
| 3241 | |
| 3242 | case PUT: |
| 3243 | UNLESS(junk = load_put(self, NULL)) |
| 3244 | break; |
| 3245 | Py_DECREF(junk); |
| 3246 | continue; |
| 3247 | |
| 3248 | case POP: |
| 3249 | UNLESS(junk = load_pop(self, NULL)) |
| 3250 | break; |
| 3251 | Py_DECREF(junk); |
| 3252 | continue; |
| 3253 | |
| 3254 | case SETITEM: |
| 3255 | UNLESS(junk = load_setitem(self, NULL)) |
| 3256 | break; |
| 3257 | Py_DECREF(junk); |
| 3258 | continue; |
| 3259 | |
| 3260 | case STOP: |
| 3261 | break; |
| 3262 | |
| 3263 | case PERSID: |
| 3264 | UNLESS(junk = load_persid(self, NULL)) |
| 3265 | break; |
| 3266 | Py_DECREF(junk); |
| 3267 | continue; |
| 3268 | |
| 3269 | default: |
| 3270 | UNLESS(key = PyString_FromStringAndSize(c_str, 1)) |
| 3271 | return NULL; |
| 3272 | |
| 3273 | UNLESS(key_repr = PyObject_Repr(key)) |
| 3274 | return NULL; |
| 3275 | |
| 3276 | UNLESS(str = PyString_FromStringAndSize(NULL, 19 + PyString_Size(key_repr))) |
| 3277 | return NULL; |
| 3278 | |
| 3279 | sprintf(PyString_AS_STRING((PyStringObject *)str), |
Barry Warsaw | 93d29b6 | 1997-01-14 17:45:08 +0000 | [diff] [blame] | 3280 | "invalid load key, \%s.", PyString_AS_STRING((PyStringObject *)key_repr)); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3281 | |
| 3282 | PyErr_SetObject(UnpicklingError, str); |
| 3283 | |
| 3284 | Py_DECREF(str); |
| 3285 | Py_DECREF(key); |
| 3286 | Py_DECREF(key_repr); |
| 3287 | |
| 3288 | return NULL; |
| 3289 | } |
| 3290 | |
| 3291 | break; |
| 3292 | } |
| 3293 | |
| 3294 | if ((err = PyErr_Occurred()) == PyExc_EOFError) |
| 3295 | { |
| 3296 | return load_eof(self, NULL); |
| 3297 | } |
| 3298 | |
| 3299 | if (err) |
| 3300 | return NULL; |
| 3301 | |
| 3302 | if ((len = PyList_Size(self->stack)) == -1) |
| 3303 | return NULL; |
| 3304 | |
| 3305 | UNLESS(val = PyList_GetItem(self->stack, len - 1)) |
| 3306 | return NULL; |
| 3307 | Py_INCREF(val); |
| 3308 | |
| 3309 | if (DEL_LIST_SLICE(self->stack, len - 1, len) == -1) |
| 3310 | { |
| 3311 | Py_DECREF(val); |
| 3312 | return NULL; |
| 3313 | } |
| 3314 | |
| 3315 | return val; |
| 3316 | |
| 3317 | err: |
| 3318 | Py_XDECREF(stack); |
| 3319 | |
| 3320 | return NULL; |
| 3321 | } |
| 3322 | |
| 3323 | |
| 3324 | static struct PyMethodDef Unpickler_methods[] = |
| 3325 | { |
| 3326 | {"load", (PyCFunction)Unpickler_load, 0, ""}, |
| 3327 | {"load_none", (PyCFunction)load_none, 0, ""}, |
| 3328 | {"load_int", (PyCFunction)load_int, 0, ""}, |
| 3329 | {"load_long", (PyCFunction)load_long, 0, ""}, |
| 3330 | {"load_float", (PyCFunction)load_float, 0, ""}, |
| 3331 | {"load_string", (PyCFunction)load_string, 0, ""}, |
| 3332 | {"load_tuple", (PyCFunction)load_tuple, 0, ""}, |
| 3333 | {"load_list", (PyCFunction)load_list, 0, ""}, |
| 3334 | {"load_dict", (PyCFunction)load_dict, 0, ""}, |
| 3335 | {"load_inst", (PyCFunction)load_inst, 0, ""}, |
| 3336 | {"load_class", (PyCFunction)load_class, 0, ""}, |
| 3337 | {"load_persid", (PyCFunction)load_persid, 0, ""}, |
| 3338 | {"load_pop", (PyCFunction)load_pop, 0, ""}, |
| 3339 | {"load_dup", (PyCFunction)load_dup, 0, ""}, |
| 3340 | {"load_get", (PyCFunction)load_get, 0, ""}, |
| 3341 | {"load_put", (PyCFunction)load_put, 0, ""}, |
| 3342 | {"load_append", (PyCFunction)load_append, 0, ""}, |
| 3343 | {"load_setitem", (PyCFunction)load_setitem, 0, ""}, |
| 3344 | {"load_build", (PyCFunction)load_build, 0, ""}, |
| 3345 | {"load_mark", (PyCFunction)load_mark, 0, ""}, |
| 3346 | {"load_eof", (PyCFunction)load_eof, 0, ""}, |
| 3347 | {NULL, NULL} /* sentinel */ |
| 3348 | }; |
| 3349 | |
| 3350 | |
| 3351 | static Unpicklerobject * |
| 3352 | newUnpicklerobject(ARG(PyObject *, f)) |
| 3353 | ARGDECL(PyObject *, f) |
| 3354 | { |
| 3355 | Unpicklerobject *self; |
| 3356 | PyObject *memo = 0; |
| 3357 | |
| 3358 | UNLESS(memo = PyDict_New()) |
| 3359 | goto err; |
| 3360 | |
| 3361 | UNLESS(self = PyObject_NEW(Unpicklerobject, &Unpicklertype)) goto err; |
| 3362 | |
| 3363 | if (PyFile_Check(f)) |
| 3364 | { |
| 3365 | self->fp = PyFile_AsFile(f); |
| 3366 | self->read_func = read_file; |
| 3367 | self->readline_func = readline_file; |
| 3368 | self->read = NULL; |
| 3369 | self->readline = NULL; |
| 3370 | } |
| 3371 | else if (PycStringIO_InputCheck(f)) |
| 3372 | { |
| 3373 | self->fp = NULL; |
| 3374 | self->read_func = read_cStringIO; |
| 3375 | self->readline_func = readline_cStringIO; |
| 3376 | self->read = NULL; |
| 3377 | self->readline = NULL; |
| 3378 | } |
| 3379 | else |
| 3380 | { |
| 3381 | PyObject *readline, *read; |
| 3382 | |
| 3383 | self->fp = NULL; |
| 3384 | self->read_func = read_other; |
| 3385 | self->readline_func = readline_other; |
| 3386 | |
| 3387 | UNLESS(readline = PyObject_GetAttrString(f, "readline")) |
| 3388 | goto err; |
| 3389 | |
| 3390 | UNLESS(read = PyObject_GetAttrString(f, "read")) |
| 3391 | { |
| 3392 | Py_DECREF(readline); |
| 3393 | goto err; |
| 3394 | } |
| 3395 | |
| 3396 | self->read = read; |
| 3397 | self->readline = readline; |
| 3398 | } |
| 3399 | |
| 3400 | Py_INCREF(f); |
| 3401 | |
| 3402 | self->file = f; |
| 3403 | self->memo = memo; |
| 3404 | self->arg = NULL; |
| 3405 | self->stack = NULL; |
| 3406 | self->pers_func = NULL; |
| 3407 | self->marks = NULL; |
| 3408 | self->num_marks = 0; |
| 3409 | self->marks_size = 0; |
| 3410 | |
| 3411 | return self; |
| 3412 | |
| 3413 | err: |
| 3414 | Py_XDECREF(memo); |
| 3415 | Py_XDECREF((PyObject *)self); |
| 3416 | |
| 3417 | return NULL; |
| 3418 | } |
| 3419 | |
| 3420 | |
| 3421 | static PyObject * |
| 3422 | get_Unpickler(ARG(PyObject *, self), ARG(PyObject *, args)) |
| 3423 | ARGDECL(PyObject *, self) |
| 3424 | ARGDECL(PyObject *, args) |
| 3425 | { |
| 3426 | PyObject *file; |
| 3427 | |
| 3428 | UNLESS(PyArg_Parse(args, "O", &file)) return NULL; |
| 3429 | return (PyObject *)newUnpicklerobject(file); |
| 3430 | } |
| 3431 | |
| 3432 | |
| 3433 | static void |
| 3434 | Unpickler_dealloc(ARG(Unpicklerobject *, self)) |
| 3435 | ARGDECL(Unpicklerobject *, self) |
| 3436 | { |
| 3437 | Py_XDECREF(self->readline); |
| 3438 | Py_XDECREF(self->read); |
| 3439 | Py_XDECREF(self->file); |
| 3440 | Py_XDECREF(self->memo); |
| 3441 | Py_XDECREF(self->stack); |
| 3442 | Py_XDECREF(self->pers_func); |
| 3443 | Py_XDECREF(self->arg); |
| 3444 | free(self->marks); |
| 3445 | PyMem_DEL(self); |
| 3446 | } |
| 3447 | |
| 3448 | |
| 3449 | static PyObject * |
| 3450 | Unpickler_getattr(ARG(Unpicklerobject *, self), ARG(char *, name)) |
| 3451 | ARGDECL(Unpicklerobject *, self) |
| 3452 | ARGDECL(char *, name) |
| 3453 | { |
| 3454 | if (!strcmp(name, "persistent_load")) |
| 3455 | { |
| 3456 | if (!self->pers_func) |
| 3457 | { |
| 3458 | PyErr_SetString(PyExc_NameError, name); |
| 3459 | return NULL; |
| 3460 | } |
| 3461 | |
| 3462 | Py_INCREF(self->pers_func); |
| 3463 | return self->pers_func; |
| 3464 | } |
| 3465 | |
| 3466 | if (!strcmp(name, "memo")) |
| 3467 | { |
| 3468 | if (!self->memo) |
| 3469 | { |
| 3470 | PyErr_SetString(PyExc_NameError, name); |
| 3471 | return NULL; |
| 3472 | } |
| 3473 | |
| 3474 | Py_INCREF(self->memo); |
| 3475 | return self->memo; |
| 3476 | } |
| 3477 | |
| 3478 | if (!strcmp(name, "UnpicklingError")) |
| 3479 | { |
| 3480 | Py_INCREF(UnpicklingError); |
| 3481 | return UnpicklingError; |
| 3482 | } |
| 3483 | |
| 3484 | return Py_FindMethod(Unpickler_methods, (PyObject *)self, name); |
| 3485 | } |
| 3486 | |
| 3487 | |
| 3488 | static int |
| 3489 | Unpickler_setattr(ARG(Unpicklerobject *, self), ARG(char *, name), ARG(PyObject *, value)) |
| 3490 | ARGDECL(Unpicklerobject *, self) |
| 3491 | ARGDECL(char *, name) |
| 3492 | ARGDECL(PyObject *, value) |
| 3493 | { |
| 3494 | if (!strcmp(name, "persistent_load")) |
| 3495 | { |
| 3496 | Py_XDECREF(self->pers_func); |
| 3497 | self->pers_func = value; |
| 3498 | Py_INCREF(value); |
| 3499 | return 0; |
| 3500 | } |
| 3501 | |
| 3502 | return -1; |
| 3503 | } |
| 3504 | |
| 3505 | |
| 3506 | static PyObject * |
| 3507 | dump(ARG(PyObject *, self), ARG(PyObject *, args)) |
| 3508 | ARGDECL(PyObject *, self) |
| 3509 | ARGDECL(PyObject *, args) |
| 3510 | { |
| 3511 | PyObject *ob, *file, *ret_val; |
| 3512 | Picklerobject *pickler; |
| 3513 | int bin = 0; |
| 3514 | |
| 3515 | UNLESS(PyArg_ParseTuple(args, "OO|i", &ob, &file, &bin)) |
| 3516 | return NULL; |
| 3517 | |
| 3518 | UNLESS(pickler = newPicklerobject(file, bin)) |
| 3519 | return NULL; |
| 3520 | |
| 3521 | UNLESS(ret_val = Pickler_dump(pickler, ob)) |
| 3522 | { |
| 3523 | Pickler_dealloc(pickler); |
| 3524 | return NULL; |
| 3525 | } |
| 3526 | |
| 3527 | Pickler_dealloc(pickler); |
| 3528 | |
| 3529 | return ret_val; |
| 3530 | } |
| 3531 | |
| 3532 | |
| 3533 | static PyObject * |
| 3534 | dumps(ARG(PyObject *, self), ARG(PyObject *, args)) |
| 3535 | ARGDECL(PyObject *, self) |
| 3536 | ARGDECL(PyObject *, args) |
| 3537 | { |
| 3538 | PyObject *ob, *file, *pickle_str; |
| 3539 | Picklerobject *pickler; |
| 3540 | int bin = 0; |
| 3541 | |
| 3542 | UNLESS(PyArg_ParseTuple(args, "O|i", &ob, &bin)) |
| 3543 | return NULL; |
| 3544 | |
| 3545 | UNLESS(file = (*PycStringIO_NewOutput)(128)) |
| 3546 | return NULL; |
| 3547 | |
| 3548 | UNLESS(pickler = newPicklerobject(file, bin)) |
| 3549 | { |
| 3550 | Py_DECREF(file); |
| 3551 | return NULL; |
| 3552 | } |
| 3553 | |
| 3554 | UNLESS(Pickler_dump(pickler, ob)) |
| 3555 | { |
| 3556 | Pickler_dealloc(pickler); |
| 3557 | Py_DECREF(file); |
| 3558 | return NULL; |
| 3559 | } |
| 3560 | |
| 3561 | Pickler_dealloc(pickler); |
| 3562 | |
| 3563 | UNLESS(pickle_str = (*PycStringIO_cgetvalue)((PyObject *)file)) |
| 3564 | { |
| 3565 | Py_DECREF(file); |
| 3566 | return NULL; |
| 3567 | } |
| 3568 | |
| 3569 | Py_DECREF(file); |
| 3570 | |
| 3571 | return pickle_str; |
| 3572 | } |
| 3573 | |
| 3574 | |
| 3575 | static PyObject * |
| 3576 | cpm_load(ARG(PyObject *, self), ARG(PyObject *, args)) |
| 3577 | ARGDECL(PyObject *, self) |
| 3578 | ARGDECL(PyObject *, args) |
| 3579 | { |
| 3580 | Unpicklerobject *unpickler; |
| 3581 | PyObject *load_result; |
| 3582 | |
| 3583 | UNLESS(PyArg_Parse(args, "O", &args)) |
| 3584 | return NULL; |
| 3585 | |
| 3586 | UNLESS(unpickler = newUnpicklerobject(args)) |
| 3587 | return NULL; |
| 3588 | |
| 3589 | UNLESS(load_result = Unpickler_load(unpickler, NULL)) |
| 3590 | { |
| 3591 | Unpickler_dealloc(unpickler); |
| 3592 | return NULL; |
| 3593 | } |
| 3594 | |
| 3595 | Py_DECREF(unpickler); |
| 3596 | |
| 3597 | return load_result; |
| 3598 | } |
| 3599 | |
| 3600 | |
| 3601 | static PyObject * |
| 3602 | loads(ARG(PyObject *, self), ARG(PyObject *, args)) |
| 3603 | ARGDECL(PyObject *, self) |
| 3604 | ARGDECL(PyObject *, args) |
| 3605 | { |
| 3606 | PyObject *file, *load_result; |
| 3607 | Unpicklerobject *unpickler; |
| 3608 | |
| 3609 | UNLESS(PyArg_Parse(args, "O", &args)) |
| 3610 | return NULL; |
| 3611 | |
| 3612 | UNLESS(file = (*PycStringIO_NewInput)(args)) |
| 3613 | return NULL; |
| 3614 | |
| 3615 | UNLESS(unpickler = newUnpicklerobject(file)) |
| 3616 | { |
| 3617 | Py_DECREF(file); |
| 3618 | return NULL; |
| 3619 | } |
| 3620 | |
| 3621 | UNLESS(load_result = Unpickler_load(unpickler, NULL)) |
| 3622 | { |
| 3623 | Unpickler_dealloc(unpickler); |
| 3624 | Py_DECREF(file); |
| 3625 | return NULL; |
| 3626 | } |
| 3627 | |
| 3628 | Py_DECREF(file); |
| 3629 | Unpickler_dealloc(unpickler); |
| 3630 | |
| 3631 | return load_result; |
| 3632 | } |
| 3633 | |
| 3634 | |
| 3635 | static char Unpicklertype__doc__[] = ""; |
| 3636 | |
| 3637 | static PyTypeObject Unpicklertype = |
| 3638 | { |
| 3639 | PyObject_HEAD_INIT(&PyType_Type) |
| 3640 | 0, /*ob_size*/ |
| 3641 | "Unpickler", /*tp_name*/ |
| 3642 | sizeof(Unpicklerobject), /*tp_basicsize*/ |
| 3643 | 0, /*tp_itemsize*/ |
| 3644 | /* methods */ |
| 3645 | (destructor)Unpickler_dealloc, /*tp_dealloc*/ |
| 3646 | (printfunc)0, /*tp_print*/ |
| 3647 | (getattrfunc)Unpickler_getattr, /*tp_getattr*/ |
| 3648 | (setattrfunc)Unpickler_setattr, /*tp_setattr*/ |
| 3649 | (cmpfunc)0, /*tp_compare*/ |
| 3650 | (reprfunc)0, /*tp_repr*/ |
| 3651 | 0, /*tp_as_number*/ |
| 3652 | 0, /*tp_as_sequence*/ |
| 3653 | 0, /*tp_as_mapping*/ |
| 3654 | (hashfunc)0, /*tp_hash*/ |
| 3655 | (ternaryfunc)0, /*tp_call*/ |
| 3656 | (reprfunc)0, /*tp_str*/ |
| 3657 | |
| 3658 | /* Space for future expansion */ |
| 3659 | 0L,0L,0L,0L, |
| 3660 | Unpicklertype__doc__ /* Documentation string */ |
| 3661 | }; |
| 3662 | |
| 3663 | |
| 3664 | static struct PyMethodDef cPickle_methods[] = |
| 3665 | { |
| 3666 | {"dump", (PyCFunction)dump, 1, ""}, |
| 3667 | {"dumps", (PyCFunction)dumps, 1, ""}, |
| 3668 | {"load", (PyCFunction)cpm_load, 0, ""}, |
| 3669 | {"loads", (PyCFunction)loads, 0, ""}, |
| 3670 | {"Pickler", (PyCFunction)get_Pickler, 1, ""}, |
| 3671 | {"Unpickler", (PyCFunction)get_Unpickler, 0, ""}, |
| 3672 | { NULL, NULL } |
| 3673 | }; |
| 3674 | |
| 3675 | |
| 3676 | static int |
| 3677 | init_stuff() |
| 3678 | { |
| 3679 | PyObject *builtins, *apply_func, *string; |
| 3680 | |
| 3681 | UNLESS(builtins = PyImport_ImportModule("__builtin__")) |
Barry Warsaw | 93d29b6 | 1997-01-14 17:45:08 +0000 | [diff] [blame] | 3682 | return 1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3683 | |
| 3684 | UNLESS(apply_func = PyObject_GetAttrString(builtins, "apply")) |
Barry Warsaw | 93d29b6 | 1997-01-14 17:45:08 +0000 | [diff] [blame] | 3685 | return 1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3686 | |
| 3687 | BuiltinFunctionType = apply_func->ob_type; |
| 3688 | |
| 3689 | Py_DECREF(apply_func); |
| 3690 | |
| 3691 | Py_DECREF(builtins); |
| 3692 | |
| 3693 | UNLESS(string = PyImport_ImportModule("string")) |
Barry Warsaw | 93d29b6 | 1997-01-14 17:45:08 +0000 | [diff] [blame] | 3694 | return 1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3695 | |
| 3696 | UNLESS(atol_func = PyObject_GetAttrString(string, "atol")) |
Barry Warsaw | 93d29b6 | 1997-01-14 17:45:08 +0000 | [diff] [blame] | 3697 | return 1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3698 | |
| 3699 | Py_DECREF(string); |
| 3700 | |
| 3701 | UNLESS(empty_list = PyList_New(0)) |
Barry Warsaw | 93d29b6 | 1997-01-14 17:45:08 +0000 | [diff] [blame] | 3702 | return 1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3703 | |
| 3704 | UNLESS(empty_tuple = PyTuple_New(0)) |
Barry Warsaw | 93d29b6 | 1997-01-14 17:45:08 +0000 | [diff] [blame] | 3705 | return 1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3706 | |
| 3707 | UNLESS(class_map = PyDict_New()) |
Barry Warsaw | 93d29b6 | 1997-01-14 17:45:08 +0000 | [diff] [blame] | 3708 | return 1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3709 | |
| 3710 | UNLESS(PicklingError = PyString_FromString("cPickle.PicklingError")) |
Barry Warsaw | 93d29b6 | 1997-01-14 17:45:08 +0000 | [diff] [blame] | 3711 | return 1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3712 | |
| 3713 | UNLESS(UnpicklingError = PyString_FromString("cPickle.UnpicklingError")) |
Barry Warsaw | 93d29b6 | 1997-01-14 17:45:08 +0000 | [diff] [blame] | 3714 | return 1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3715 | |
| 3716 | PycString_IMPORT; |
Barry Warsaw | 93d29b6 | 1997-01-14 17:45:08 +0000 | [diff] [blame] | 3717 | return 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3718 | } |
| 3719 | |
| 3720 | |
| 3721 | #define CHECK_FOR_ERRORS(MESS) \ |
| 3722 | if(PyErr_Occurred()) { \ |
| 3723 | PyObject *__sys_exc_type, *__sys_exc_value, *__sys_exc_traceback; \ |
| 3724 | PyErr_Fetch( &__sys_exc_type, &__sys_exc_value, &__sys_exc_traceback); \ |
| 3725 | fprintf(stderr, # MESS ":\n\t"); \ |
| 3726 | PyObject_Print(__sys_exc_type, stderr,0); \ |
| 3727 | fprintf(stderr,", "); \ |
| 3728 | PyObject_Print(__sys_exc_value, stderr,0); \ |
| 3729 | fprintf(stderr,"\n"); \ |
| 3730 | fflush(stderr); \ |
| 3731 | Py_FatalError(# MESS); \ |
| 3732 | } |
| 3733 | |
| 3734 | |
| 3735 | /* Initialization function for the module (*must* be called initcPickle) */ |
| 3736 | void |
| 3737 | initcPickle() |
| 3738 | { |
| 3739 | PyObject *m, *d; |
| 3740 | |
| 3741 | /* Create the module and add the functions */ |
| 3742 | m = Py_InitModule4("cPickle", cPickle_methods, |
| 3743 | cPickle_module_documentation, |
| 3744 | (PyObject*)NULL,PYTHON_API_VERSION); |
| 3745 | |
| 3746 | /* Add some symbolic constants to the module */ |
| 3747 | d = PyModule_GetDict(m); |
| 3748 | ErrorObject = PyString_FromString("cPickle.error"); |
| 3749 | PyDict_SetItemString(d, "error", ErrorObject); |
| 3750 | |
| 3751 | /* XXXX Add constants here */ |
| 3752 | |
Barry Warsaw | 93d29b6 | 1997-01-14 17:45:08 +0000 | [diff] [blame] | 3753 | if (init_stuff()) |
| 3754 | Py_FatalError("can't initialize module cPickle"); |
| 3755 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3756 | CHECK_FOR_ERRORS("can't initialize module cPickle"); |
| 3757 | |
| 3758 | PyDict_SetItemString(d, "PicklingError", PicklingError); |
| 3759 | CHECK_FOR_ERRORS("can't initialize module cPickle: error creating PicklingError"); |
| 3760 | |
| 3761 | PyDict_SetItemString(d, "UnpicklingError", UnpicklingError); |
| 3762 | CHECK_FOR_ERRORS("can't initialize module cPickle: error creating UnpicklingError"); |
| 3763 | } |