home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / palmos / pippy-0.6beta-src.tar.gz / pippy-0.6beta-src.tar / pippy-0.6beta-src / src / Modules / cPickle.c < prev    next >
C/C++ Source or Header  |  2000-12-21  |  105KB  |  4,400 lines

  1. /*
  2.  * cPickle.c,v 1.71 1999/07/11 13:30:34 jim Exp
  3.  * 
  4.  * Copyright (c) 1996-1998, Digital Creations, Fredericksburg, VA, USA.  
  5.  * All rights reserved.
  6.  * 
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions are
  9.  * met:
  10.  * 
  11.  *   o Redistributions of source code must retain the above copyright
  12.  *     notice, this list of conditions, and the disclaimer that follows.
  13.  * 
  14.  *   o Redistributions in binary form must reproduce the above copyright
  15.  *     notice, this list of conditions, and the following disclaimer in
  16.  *     the documentation and/or other materials provided with the
  17.  *     distribution.
  18.  * 
  19.  *   o Neither the name of Digital Creations nor the names of its
  20.  *     contributors may be used to endorse or promote products derived
  21.  *     from this software without specific prior written permission.
  22.  * 
  23.  * 
  24.  * THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS AND CONTRIBUTORS *AS
  25.  * IS* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  26.  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  27.  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL
  28.  * CREATIONS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  29.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  30.  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  31.  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  32.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  33.  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  34.  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  35.  * DAMAGE.
  36.  * 
  37.  # 
  38.  # If you have questions regarding this software, contact:
  39.  #
  40.  #   Digital Creations, L.C.
  41.  #   910 Princess Ann Street
  42.  #   Fredericksburge, Virginia  22401
  43.  #
  44.  #   info@digicool.com
  45.  #
  46.  #   (540) 371-6909
  47.  */
  48.  
  49. static char cPickle_module_documentation[] = 
  50. "C implementation and optimization of the Python pickle module\n"
  51. "\n"
  52. "cPickle.c,v 1.71 1999/07/11 13:30:34 jim Exp\n"
  53. ;
  54.  
  55. #include "Python.h"
  56. #include "cStringIO.h"
  57. #include "mymath.h"
  58.  
  59. #ifndef Py_eval_input
  60. #include <graminit.h>
  61. #define Py_eval_input eval_input
  62. #endif /* Py_eval_input */
  63.  
  64. #include <errno.h>
  65.  
  66. #define UNLESS(E) if (!(E))
  67.  
  68. #define DEL_LIST_SLICE(list, from, to) (PyList_SetSlice(list, from, to, NULL))
  69.  
  70. #define WRITE_BUF_SIZE 256
  71.  
  72.  
  73. #define MARK        '('
  74. #define STOP        '.'
  75. #define POP         '0'
  76. #define POP_MARK    '1'
  77. #define DUP         '2'
  78. #define FLOAT       'F'
  79. #define BINFLOAT    'G'
  80. #define INT         'I'
  81. #define BININT      'J'
  82. #define BININT1     'K'
  83. #define LONG        'L'
  84. #define BININT2     'M'
  85. #define NONE        'N'
  86. #define PERSID      'P'
  87. #define BINPERSID   'Q'
  88. #define REDUCE      'R'
  89. #define STRING      'S'
  90. #define BINSTRING   'T'
  91. #define SHORT_BINSTRING 'U'
  92. #define APPEND      'a'
  93. #define BUILD       'b'
  94. #define GLOBAL      'c'
  95. #define DICT        'd'
  96. #define EMPTY_DICT  '}'
  97. #define APPENDS     'e'
  98. #define GET         'g'
  99. #define BINGET      'h'
  100. #define INST        'i'
  101. #define LONG_BINGET 'j'
  102. #define LIST        'l'
  103. #define EMPTY_LIST  ']'
  104. #define OBJ         'o'
  105. #define PUT         'p'
  106. #define BINPUT      'q'
  107. #define LONG_BINPUT 'r'
  108. #define SETITEM     's'
  109. #define TUPLE       't'
  110. #define EMPTY_TUPLE ')'
  111. #define SETITEMS    'u'
  112.  
  113. static char MARKv = MARK;
  114.  
  115. /* atol function from string module */
  116. static PyObject *atol_func;
  117.  
  118. static PyObject *PickleError;
  119. static PyObject *PicklingError;
  120. static PyObject *UnpickleableError;
  121. static PyObject *UnpicklingError;
  122. static PyObject *BadPickleGet;
  123.  
  124.  
  125. static PyObject *dispatch_table;
  126. static PyObject *safe_constructors;
  127. static PyObject *empty_tuple;
  128.  
  129. static PyObject *__class___str, *__getinitargs___str, *__dict___str,
  130.   *__getstate___str, *__setstate___str, *__name___str, *__reduce___str,
  131.   *write_str, *__safe_for_unpickling___str, *append_str,
  132.   *read_str, *readline_str, *__main___str, *__basicnew___str,
  133.   *copy_reg_str, *dispatch_table_str, *safe_constructors_str, *empty_str;
  134.  
  135. static int save();
  136. static int put2();
  137.  
  138. #ifndef PyList_SET_ITEM
  139. #define PyList_SET_ITEM(op, i, v) (((PyListObject *)(op))->ob_item[i] = (v))
  140. #endif
  141. #ifndef PyList_GET_SIZE
  142. #define PyList_GET_SIZE(op)    (((PyListObject *)(op))->ob_size)
  143. #endif
  144. #ifndef PyTuple_SET_ITEM
  145. #define PyTuple_SET_ITEM(op, i, v) (((PyTupleObject *)(op))->ob_item[i] = (v))
  146. #endif
  147. #ifndef PyTuple_GET_SIZE
  148. #define PyTuple_GET_SIZE(op)    (((PyTupleObject *)(op))->ob_size)
  149. #endif
  150. #ifndef PyString_GET_SIZE
  151. #define PyString_GET_SIZE(op)    (((PyStringObject *)(op))->ob_size)
  152. #endif
  153.  
  154. /*************************************************************************
  155.  Internal Data type for pickle data.                                     */
  156.  
  157. typedef struct {
  158.      PyObject_HEAD
  159.      int length, size;
  160.      PyObject **data;
  161. } Pdata;
  162.  
  163. static void 
  164. Pdata_dealloc(Pdata *self) {
  165.     int i;
  166.     PyObject **p;
  167.  
  168.     for (i=self->length, p=self->data; --i >= 0; p++) Py_DECREF(*p);
  169.  
  170.     if (self->data) free(self->data);
  171.  
  172.     PyMem_DEL(self);
  173. }
  174.  
  175. static PyTypeObject PdataType = {
  176.     PyObject_HEAD_INIT(NULL) 0, "Pdata", sizeof(Pdata), 0,
  177.     (destructor)Pdata_dealloc,
  178.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0L,0L,0L,0L, ""
  179. };
  180.  
  181. #define Pdata_Check(O) ((O)->ob_type == &PdataType)
  182.  
  183. static PyObject *
  184. Pdata_New() {
  185.     Pdata *self;
  186.  
  187.     UNLESS (self = PyObject_NEW(Pdata, &PdataType)) return NULL;
  188.     self->size=8;
  189.     self->length=0;
  190.     self->data=malloc(self->size * sizeof(PyObject*));
  191.     if (self->data) return (PyObject*)self;
  192.     Py_DECREF(self);
  193.     return PyErr_NoMemory();
  194. }
  195.  
  196. static int 
  197. stackUnderflow() {
  198.     PyErr_SetString(UnpicklingError, "unpickling stack underflow");
  199.     return -1;
  200. }
  201.  
  202. static int
  203. Pdata_clear(Pdata *self, int clearto) {
  204.     int i;
  205.     PyObject **p;
  206.  
  207.     if (clearto < 0) return stackUnderflow();
  208.     if (clearto >= self->length) return 0;
  209.  
  210.     for (i=self->length, p=self->data+clearto; --i >= clearto; p++)
  211.       Py_DECREF(*p);
  212.     self->length=clearto;
  213.  
  214.     return 0;
  215. }
  216.  
  217.  
  218. static int 
  219. Pdata_grow(Pdata *self) {
  220.   if (! self->size) {
  221.       PyErr_NoMemory();
  222.       return -1;
  223.   }
  224.   self->size *= 2;                          
  225.   self->data = realloc(self->data, self->size*sizeof(PyObject*));
  226.   if (! self->data) {
  227.     self->size = 0;                       
  228.     PyErr_NoMemory();                              
  229.     return -1;                                     
  230.   }
  231.   return 0;
  232. }                                                      
  233.  
  234. #define PDATA_POP(D,V) {                                      \
  235.     if ((D)->length) V=D->data[--((D)->length)];              \
  236.     else {                                                    \
  237.         PyErr_SetString(UnpicklingError, "bad pickle data");  \
  238.         V=NULL;                                               \
  239.     }                                                         \
  240. }
  241.  
  242.  
  243. static PyObject *
  244. Pdata_popTuple(Pdata *self, int start) {
  245.     PyObject *r;
  246.     int i, j, l;
  247.  
  248.     l=self->length-start;
  249.     UNLESS (r=PyTuple_New(l)) return NULL;
  250.     for (i=start, j=0 ; j < l; )
  251.         PyTuple_SET_ITEM(r,j++,self->data[i++]);
  252.  
  253.     self->length=start;
  254.     return r;
  255. }
  256.  
  257. static PyObject *
  258. Pdata_popList(Pdata *self, int start) {
  259.     PyObject *r;
  260.     int i, j, l;
  261.  
  262.     l=self->length-start;
  263.     UNLESS (r=PyList_New(l)) return NULL;
  264.     for (i=start, j=0 ; j < l; )
  265.         PyList_SET_ITEM(r,j++,self->data[i++]);
  266.  
  267.     self->length=start;
  268.     return r;
  269. }
  270.  
  271. #define PDATA_APPEND_(D,O,ER) { \
  272.   if (Pdata_Append(((Pdata*)(D)), O) < 0) return ER; \
  273. }
  274.  
  275. #define PDATA_APPEND(D,O,ER) {                                 \
  276.     if (((Pdata*)(D))->length == ((Pdata*)(D))->size &&        \
  277.         Pdata_grow((Pdata*)(D)) < 0)                           \
  278.         return ER;                                             \
  279.     Py_INCREF(O);                                              \
  280.     ((Pdata*)(D))->data[((Pdata*)(D))->length++]=O;            \
  281. }
  282.  
  283. #define PDATA_PUSH(D,O,ER) {                                   \
  284.     if (((Pdata*)(D))->length == ((Pdata*)(D))->size &&        \
  285.         Pdata_grow((Pdata*)(D)) < 0) {                         \
  286.         Py_DECREF(O);                                          \
  287.         return ER;                                             \
  288.     }                                                          \
  289.     ((Pdata*)(D))->data[((Pdata*)(D))->length++]=O;            \
  290. }
  291.  
  292. /*************************************************************************/
  293.  
  294. #define ARG_TUP(self, o) {                          \
  295.   if (self->arg || (self->arg=PyTuple_New(1))) {    \
  296.       Py_XDECREF(PyTuple_GET_ITEM(self->arg,0));    \
  297.       PyTuple_SET_ITEM(self->arg,0,o);              \
  298.   }                                                 \
  299.   else {                                            \
  300.       Py_DECREF(o);                                 \
  301.   }                                                 \
  302. }
  303.  
  304. #define FREE_ARG_TUP(self) {                        \
  305.     if (self->arg->ob_refcnt > 1) {                 \
  306.       Py_DECREF(self->arg);                         \
  307.       self->arg=NULL;                               \
  308.     }                                               \
  309.   }
  310.  
  311. typedef struct {
  312.      PyObject_HEAD
  313.      FILE *fp;
  314.      PyObject *write;
  315.      PyObject *file;
  316.      PyObject *memo;
  317.      PyObject *arg;
  318.      PyObject *pers_func;
  319.      PyObject *inst_pers_func;
  320.      int bin;
  321.      int fast; /* Fast mode doesn't save in memo, don't use if circ ref */
  322.      int (*write_func)();
  323.      char *write_buf;
  324.      int buf_size;
  325.      PyObject *dispatch_table;
  326. } Picklerobject;
  327.  
  328. staticforward PyTypeObject Picklertype;
  329.  
  330. typedef struct {
  331.      PyObject_HEAD
  332.      FILE *fp;
  333.      PyObject *file;
  334.      PyObject *readline;
  335.      PyObject *read;
  336.      PyObject *memo;
  337.      PyObject *arg;
  338.      Pdata *stack;
  339.      PyObject *mark;
  340.      PyObject *pers_func;
  341.      PyObject *last_string;
  342.      int *marks;
  343.      int num_marks;
  344.      int marks_size;
  345.      int (*read_func)();
  346.      int (*readline_func)();
  347.      int buf_size;
  348.      char *buf;
  349.      PyObject *safe_constructors;
  350.      PyObject *find_class;
  351. } Unpicklerobject;
  352.  
  353. staticforward PyTypeObject Unpicklertype;
  354.  
  355. int 
  356. cPickle_PyMapping_HasKey(PyObject *o, PyObject *key) {
  357.     PyObject *v;
  358.  
  359.     if ((v = PyObject_GetItem(o,key))) {
  360.         Py_DECREF(v);
  361.         return 1;
  362.     }
  363.  
  364.     PyErr_Clear();
  365.     return 0;
  366. }
  367.  
  368. static
  369. PyObject *
  370. #ifdef HAVE_STDARG_PROTOTYPES
  371. /* VARARGS 2 */
  372. cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...) {
  373. #else
  374. /* VARARGS */
  375. cPickle_ErrFormat(va_alist) va_dcl {
  376. #endif
  377.   va_list va;
  378.   PyObject *args=0, *retval=0;
  379. #ifdef HAVE_STDARG_PROTOTYPES
  380.   va_start(va, format);
  381. #else
  382.   PyObject *ErrType;
  383.   char *stringformat, *format;
  384.   va_start(va);
  385.   ErrType = va_arg(va, PyObject *);
  386.   stringformat   = va_arg(va, char *);
  387.   format   = va_arg(va, char *);
  388. #endif
  389.   
  390.   if (format) args = Py_VaBuildValue(format, va);
  391.   va_end(va);
  392.   if (format && ! args) return NULL;
  393.   if (stringformat && !(retval=PyString_FromString(stringformat))) return NULL;
  394.  
  395.   if (retval) {
  396.       if (args) {
  397.           PyObject *v;
  398.           v=PyString_Format(retval, args);
  399.           Py_DECREF(retval);
  400.           Py_DECREF(args);
  401.           if (! v) return NULL;
  402.           retval=v;
  403.         }
  404.     }
  405.   else
  406.     if (args) retval=args;
  407.     else {
  408.         PyErr_SetObject(ErrType,Py_None);
  409.         return NULL;
  410.       }
  411.   PyErr_SetObject(ErrType,retval);
  412.   Py_DECREF(retval);
  413.   return NULL;
  414. }
  415.  
  416. static int 
  417. write_file(Picklerobject *self, char *s, int  n) {
  418.     if (s == NULL) {
  419.         return 0;
  420.     }
  421.  
  422.     if ((int)fwrite(s, sizeof(char), n, self->fp) != n) {
  423.         PyErr_SetFromErrno(PyExc_IOError);
  424.         return -1;
  425.     }
  426.  
  427.     return n;
  428. }
  429.  
  430. static int 
  431. write_cStringIO(Picklerobject *self, char *s, int  n) {
  432.     if (s == NULL) {
  433.         return 0;
  434.     }
  435.  
  436.     if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
  437.         return -1;
  438.     }
  439.  
  440.     return n;
  441. }
  442.  
  443. static int 
  444. write_none(Picklerobject *self, char *s, int  n) {
  445.     if (s == NULL) return 0;
  446.     return n;
  447. }
  448.  
  449. static int 
  450. write_other(Picklerobject *self, char *s, int  n) {
  451.     PyObject *py_str = 0, *junk = 0;
  452.  
  453.     if (s == NULL) {
  454.         UNLESS (self->buf_size) return 0;
  455.         UNLESS (py_str = 
  456.             PyString_FromStringAndSize(self->write_buf, self->buf_size))
  457.             return -1;
  458.     }
  459.     else {
  460.         if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
  461.             if (write_other(self, NULL, 0) < 0)
  462.                 return -1;
  463.         }
  464.  
  465.         if (n > WRITE_BUF_SIZE) {    
  466.             UNLESS (py_str = 
  467.                 PyString_FromStringAndSize(s, n))
  468.                 return -1;
  469.         }
  470.         else {
  471.             memcpy(self->write_buf + self->buf_size, s, n);
  472.             self->buf_size += n;
  473.             return n;
  474.         }
  475.     }
  476.  
  477.     if (self->write) {
  478.         /* object with write method */
  479.         ARG_TUP(self, py_str);
  480.         if (self->arg) {
  481.             junk = PyObject_CallObject(self->write, self->arg);
  482.             FREE_ARG_TUP(self);
  483.         }
  484.         if (junk) Py_DECREF(junk);
  485.         else return -1;
  486.       }
  487.     else 
  488.       PDATA_PUSH(self->file, py_str, -1);
  489.     
  490.     self->buf_size = 0; 
  491.     return n;
  492. }
  493.  
  494.  
  495. static int 
  496. read_file(Unpicklerobject *self, char **s, int  n) {
  497.  
  498.     if (self->buf_size == 0) {
  499.         int size;
  500.  
  501.         size = ((n < 32) ? 32 : n); 
  502.         UNLESS (self->buf = (char *)malloc(size * sizeof(char))) {
  503.             PyErr_NoMemory();
  504.             return -1;
  505.         }
  506.  
  507.         self->buf_size = size;
  508.     }
  509.     else if (n > self->buf_size) {
  510.         UNLESS (self->buf = (char *)realloc(self->buf, n * sizeof(char))) {
  511.             PyErr_NoMemory();
  512.             return -1;
  513.         }
  514.  
  515.         self->buf_size = n;
  516.     }
  517.             
  518.     if ((int)fread(self->buf, sizeof(char), n, self->fp) != n) {  
  519.         if (feof(self->fp)) {
  520.             PyErr_SetNone(PyExc_EOFError);
  521.             return -1;
  522.         }
  523.  
  524.         PyErr_SetFromErrno(PyExc_IOError);
  525.         return -1;
  526.     }
  527.  
  528.     *s = self->buf;
  529.  
  530.     return n;
  531. }
  532.  
  533.  
  534. static int 
  535. readline_file(Unpicklerobject *self, char **s) {
  536.     int i;
  537.  
  538.     if (self->buf_size == 0) {
  539.         UNLESS (self->buf = (char *)malloc(40 * sizeof(char))) {
  540.             PyErr_NoMemory();
  541.             return -1;
  542.         }
  543.    
  544.         self->buf_size = 40;
  545.     }
  546.  
  547.     i = 0;
  548.     while (1) {
  549.         for (; i < (self->buf_size - 1); i++) {
  550.             if (feof(self->fp) || (self->buf[i] = getc(self->fp)) == '\n') {
  551.                 self->buf[i + 1] = '\0';
  552.                 *s = self->buf;
  553.                 return i + 1;
  554.             }
  555.         }
  556.  
  557.         UNLESS (self->buf = (char *)realloc(self->buf, 
  558.             (self->buf_size * 2) * sizeof(char))) {
  559.             PyErr_NoMemory();
  560.             return -1;
  561.         }
  562.  
  563.         self->buf_size *= 2;
  564.     }
  565.  
  566. }    
  567.  
  568.  
  569. static int 
  570. read_cStringIO(Unpicklerobject *self, char **s, int  n) {
  571.     char *ptr;
  572.  
  573.     if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
  574.         PyErr_SetNone(PyExc_EOFError);
  575.         return -1;
  576.     }
  577.  
  578.     *s = ptr;
  579.  
  580.     return n;
  581. }
  582.  
  583.  
  584. static int 
  585. readline_cStringIO(Unpicklerobject *self, char **s) {
  586.     int n;
  587.     char *ptr;
  588.  
  589.     if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
  590.         return -1;
  591.     }
  592.  
  593.     *s = ptr;
  594.  
  595.     return n;
  596. }
  597.  
  598.  
  599. static int 
  600. read_other(Unpicklerobject *self, char **s, int  n) {
  601.     PyObject *bytes, *str=0;
  602.  
  603.     UNLESS (bytes = PyInt_FromLong(n)) return -1;
  604.  
  605.     ARG_TUP(self, bytes);
  606.     if (self->arg) {
  607.         str = PyObject_CallObject(self->read, self->arg);
  608.         FREE_ARG_TUP(self);
  609.     }
  610.     if (! str) return -1;
  611.  
  612.     Py_XDECREF(self->last_string);
  613.     self->last_string = str;
  614.  
  615.     if (! (*s = PyString_AsString(str))) return -1;
  616.     return n;
  617. }
  618.  
  619.  
  620. static int 
  621. readline_other(Unpicklerobject *self, char **s) {
  622.     PyObject *str;
  623.     int str_size;
  624.  
  625.     UNLESS (str = PyObject_CallObject(self->readline, empty_tuple)) {
  626.         return -1;
  627.     }
  628.  
  629.     if ((str_size = PyString_Size(str)) < 0)
  630.       return -1;
  631.  
  632.     Py_XDECREF(self->last_string);
  633.     self->last_string = str;
  634.  
  635.     if (! (*s = PyString_AsString(str)))
  636.       return -1;
  637.  
  638.     return str_size;
  639. }
  640.  
  641.  
  642. static char *
  643. pystrndup(char *s, int l) {
  644.   char *r;
  645.   UNLESS (r=malloc((l+1)*sizeof(char))) return (char*)PyErr_NoMemory();
  646.   memcpy(r,s,l);
  647.   r[l]=0;
  648.   return r;
  649. }
  650.  
  651.  
  652. static int
  653. get(Picklerobject *self, PyObject *id) {
  654.     PyObject *value, *mv;
  655.     long c_value;
  656.     char s[30];
  657.     int len;
  658.  
  659.     UNLESS (mv = PyDict_GetItem(self->memo, id)) {
  660.         PyErr_SetObject(PyExc_KeyError, id);
  661.         return -1;
  662.       }
  663.  
  664.     UNLESS (value = PyTuple_GetItem(mv, 0))
  665.         return -1;
  666.         
  667.     UNLESS (PyInt_Check(value)) {
  668.       PyErr_SetString(PicklingError, "no int where int expected in memo");
  669.       return -1;
  670.     }
  671.     c_value = PyInt_AS_LONG((PyIntObject*)value);
  672.  
  673.     if (!self->bin) {
  674.         s[0] = GET;
  675.         sprintf(s + 1, "%ld\n", c_value);
  676.         len = strlen(s);
  677.     }
  678.     else if (Pdata_Check(self->file)) {
  679.         if (write_other(self, NULL, 0) < 0) return -1;
  680.         PDATA_APPEND(self->file, mv, -1);
  681.         return 0;
  682.       }
  683.     else {
  684.         if (c_value < 256) {
  685.             s[0] = BINGET;
  686.             s[1] = (int)(c_value & 0xff);
  687.             len = 2;
  688.         }
  689.         else {
  690.             s[0] = LONG_BINGET;
  691.             s[1] = (int)(c_value & 0xff);
  692.             s[2] = (int)((c_value >> 8)  & 0xff);
  693.             s[3] = (int)((c_value >> 16) & 0xff);
  694.             s[4] = (int)((c_value >> 24) & 0xff);
  695.             len = 5;
  696.         }
  697.     }
  698.  
  699.     if ((*self->write_func)(self, s, len) < 0)
  700.         return -1;
  701.  
  702.     return 0;
  703. }
  704.     
  705.  
  706. static int
  707. put(Picklerobject *self, PyObject *ob) {
  708.     if (ob->ob_refcnt < 2 || self->fast)
  709.         return 0;
  710.  
  711.     return put2(self, ob);
  712. }
  713.  
  714.   
  715. static int
  716. put2(Picklerobject *self, PyObject *ob) {
  717.     char c_str[30];
  718.     int p, len, res = -1;
  719.     PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
  720.  
  721.     if (self->fast) return 0;
  722.  
  723.     if ((p = PyDict_Size(self->memo)) < 0)
  724.         goto finally;
  725.  
  726.     p++;  /* Make sure memo keys are positive! */
  727.  
  728.     UNLESS (py_ob_id = PyInt_FromLong((long)ob))
  729.         goto finally;
  730.  
  731.     UNLESS (memo_len = PyInt_FromLong(p))
  732.         goto finally;
  733.  
  734.     UNLESS (t = PyTuple_New(2))
  735.         goto finally;
  736.  
  737.     PyTuple_SET_ITEM(t, 0, memo_len);
  738.     Py_INCREF(memo_len);
  739.     PyTuple_SET_ITEM(t, 1, ob);
  740.     Py_INCREF(ob);
  741.  
  742.     if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
  743.         goto finally;
  744.  
  745.     if (!self->bin) {
  746.         c_str[0] = PUT;
  747.         sprintf(c_str + 1, "%d\n", p);
  748.         len = strlen(c_str);
  749.     }
  750.     else if (Pdata_Check(self->file)) {
  751.         if (write_other(self, NULL, 0) < 0) return -1;
  752.         PDATA_APPEND(self->file, memo_len, -1);
  753.         res=0;          /* Job well done ;) */
  754.         goto finally;
  755.     }
  756.     else {
  757.         if (p >= 256) {
  758.             c_str[0] = LONG_BINPUT;
  759.             c_str[1] = (int)(p & 0xff);
  760.             c_str[2] = (int)((p >> 8)  & 0xff);
  761.             c_str[3] = (int)((p >> 16) & 0xff);
  762.             c_str[4] = (int)((p >> 24) & 0xff);
  763.             len = 5;
  764.         }
  765.         else {
  766.             c_str[0] = BINPUT;
  767.             c_str[1] = p;
  768.             len = 2; 
  769.         }
  770.     }
  771.  
  772.     if ((*self->write_func)(self, c_str, len) < 0)
  773.         goto finally;
  774.  
  775.     res = 0;
  776.  
  777. finally:
  778.     Py_XDECREF(py_ob_id);
  779.     Py_XDECREF(memo_len);
  780.     Py_XDECREF(t);
  781.  
  782.     return res;
  783. }
  784.  
  785. #define PyImport_Import cPickle_Import
  786.  
  787. static PyObject *
  788. PyImport_Import(PyObject *module_name) {
  789.   static PyObject *silly_list=0, *__builtins___str=0, *__import___str;
  790.   static PyObject *standard_builtins=0;
  791.   PyObject *globals=0, *__import__=0, *__builtins__=0, *r=0;
  792.  
  793.   UNLESS (silly_list) {
  794.       UNLESS (__import___str=PyString_FromString("__import__"))
  795.         return NULL;
  796.       UNLESS (__builtins___str=PyString_FromString("__builtins__"))
  797.         return NULL;
  798.       UNLESS (silly_list=Py_BuildValue("[s]","__doc__"))
  799.         return NULL;
  800.     }
  801.  
  802.   if ((globals=PyEval_GetGlobals())) {
  803.       Py_INCREF(globals);
  804.       UNLESS (__builtins__=PyObject_GetItem(globals,__builtins___str))
  805.         goto err;
  806.     }
  807.   else {
  808.       PyErr_Clear();
  809.  
  810.       UNLESS (standard_builtins ||
  811.              (standard_builtins=PyImport_ImportModule("__builtin__")))
  812.         return NULL;
  813.       
  814.       __builtins__=standard_builtins;
  815.       Py_INCREF(__builtins__);
  816.       UNLESS (globals = Py_BuildValue("{sO}", "__builtins__", __builtins__))
  817.         goto err;
  818.     }
  819.  
  820.   if (PyDict_Check(__builtins__)) {
  821.     UNLESS (__import__=PyObject_GetItem(__builtins__,__import___str)) goto err;
  822.   }
  823.   else {
  824.     UNLESS (__import__=PyObject_GetAttr(__builtins__,__import___str)) goto err;
  825.   }
  826.  
  827.   UNLESS (r=PyObject_CallFunction(__import__,"OOOO",
  828.                                  module_name, globals, globals, silly_list))
  829.     goto err;
  830.  
  831.   Py_DECREF(globals);
  832.   Py_DECREF(__builtins__);
  833.   Py_DECREF(__import__);
  834.   
  835.   return r;
  836. err:
  837.   Py_XDECREF(globals);
  838.   Py_XDECREF(__builtins__);
  839.   Py_XDECREF(__import__);
  840.   return NULL;
  841. }
  842.  
  843. static PyObject *
  844. whichmodule(PyObject *global, PyObject *global_name) {
  845.     int i, j;
  846.     PyObject *module = 0, *modules_dict = 0,
  847.         *global_name_attr = 0, *name = 0;
  848.  
  849.     module = PyObject_GetAttrString(global, "__module__");
  850.     if (module) return module;
  851.     PyErr_Clear();
  852.  
  853.     UNLESS (modules_dict = PySys_GetObject("modules"))
  854.         return NULL;
  855.  
  856.     i = 0;
  857.     while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
  858.  
  859.         if (PyObject_Compare(name, __main___str)==0) continue;
  860.       
  861.         UNLESS (global_name_attr = PyObject_GetAttr(module, global_name)) {
  862.             PyErr_Clear();
  863.             continue;
  864.         }
  865.  
  866.         if (global_name_attr != global) {
  867.             Py_DECREF(global_name_attr);
  868.             continue;
  869.         }
  870.  
  871.         Py_DECREF(global_name_attr);
  872.  
  873.         break;
  874.     }
  875.  
  876.     /* The following implements the rule in pickle.py added in 1.5
  877.        that used __main__ if no module is found.  I don't actually
  878.        like this rule. jlf
  879.      */
  880.     if (!j) {
  881.         j=1;
  882.         name=__main___str;
  883.     }
  884.  
  885.     Py_INCREF(name);
  886.     return name;
  887. }
  888.  
  889.  
  890. static int
  891. save_none(Picklerobject *self, PyObject *args) {
  892.     static char none = NONE;
  893.     if ((*self->write_func)(self, &none, 1) < 0)  
  894.         return -1;
  895.  
  896.     return 0;
  897. }
  898.  
  899.       
  900. static int
  901. save_int(Picklerobject *self, PyObject *args) {
  902.     char c_str[32];
  903.     long l = PyInt_AS_LONG((PyIntObject *)args);
  904.     int len = 0;
  905.  
  906.     if (!self->bin
  907. #if SIZEOF_LONG > 4
  908.         || (l >> 32)
  909. #endif
  910.             ) {
  911.                 /* Save extra-long ints in non-binary mode, so that
  912.                    we can use python long parsing code to restore,
  913.                    if necessary. */
  914.         c_str[0] = INT;
  915.         sprintf(c_str + 1, "%ld\n", l);
  916.         if ((*self->write_func)(self, c_str, strlen(c_str)) < 0)
  917.             return -1;
  918.     }
  919.     else {
  920.         c_str[1] = (int)( l        & 0xff);
  921.         c_str[2] = (int)((l >> 8)  & 0xff);
  922.         c_str[3] = (int)((l >> 16) & 0xff);
  923.         c_str[4] = (int)((l >> 24) & 0xff);
  924.  
  925.         if ((c_str[4] == 0) && (c_str[3] == 0)) {
  926.             if (c_str[2] == 0) {
  927.                 c_str[0] = BININT1;
  928.                 len = 2;
  929.             }
  930.             else {
  931.                 c_str[0] = BININT2;
  932.                 len = 3;
  933.             }
  934.         }
  935.         else {
  936.             c_str[0] = BININT;
  937.             len = 5;
  938.         }
  939.  
  940.         if ((*self->write_func)(self, c_str, len) < 0)
  941.             return -1;
  942.     }
  943.  
  944.     return 0;
  945. }
  946.  
  947.  
  948. static int
  949. save_long(Picklerobject *self, PyObject *args) {
  950.     int size, res = -1;
  951.     PyObject *repr = 0;
  952.  
  953.     static char l = LONG;
  954.  
  955.     UNLESS (repr = PyObject_Repr(args))
  956.         goto finally;
  957.  
  958.     if ((size = PyString_Size(repr)) < 0)
  959.         goto finally;
  960.  
  961.     if ((*self->write_func)(self, &l, 1) < 0)
  962.         goto finally;
  963.  
  964.     if ((*self->write_func)(self, 
  965.         PyString_AS_STRING((PyStringObject *)repr), size) < 0)
  966.         goto finally;
  967.  
  968.     if ((*self->write_func)(self, "\n", 1) < 0)
  969.         goto finally;
  970.  
  971.     res = 0;
  972.  
  973. finally:
  974.     Py_XDECREF(repr);
  975.  
  976.     return res;
  977. }
  978.  
  979.  
  980. static int
  981. save_float(Picklerobject *self, PyObject *args) {
  982.     double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
  983.  
  984.     if (self->bin) {
  985.         int s, e;
  986.         double f;
  987.         long fhi, flo;
  988.         char str[9], *p = str;
  989.  
  990.         *p = BINFLOAT;
  991.         p++;
  992.  
  993.         if (x < 0) {
  994.             s = 1;
  995.             x = -x;
  996.         }
  997.         else
  998.             s = 0;
  999.  
  1000.         f = frexp(x, &e);
  1001.  
  1002.         /* Normalize f to be in the range [1.0, 2.0) */
  1003.         if (0.5 <= f && f < 1.0) {
  1004.             f *= 2.0;
  1005.             e--;
  1006.         }
  1007.         else if (f == 0.0) {
  1008.             e = 0;
  1009.         }
  1010.         else {
  1011.             PyErr_SetString(PyExc_SystemError,
  1012.                             "frexp() result out of range");
  1013.             return -1;
  1014.         }
  1015.  
  1016.         if (e >= 1024) {
  1017.             /* XXX 1024 itself is reserved for Inf/NaN */
  1018.             PyErr_SetString(PyExc_OverflowError,
  1019.                             "float too large to pack with d format");
  1020.             return -1;
  1021.         }
  1022.         else if (e < -1022) {
  1023.             /* Gradual underflow */
  1024.             f = ldexp(f, 1022 + e);
  1025.             e = 0;
  1026.         }
  1027.         else if (!(e == 0 && f == 0.0)) {
  1028.             e += 1023;
  1029.             f -= 1.0; /* Get rid of leading 1 */
  1030.         }
  1031.  
  1032.         /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */
  1033.         f *= 268435456.0; /* 2**28 */
  1034.         fhi = (long) floor(f); /* Truncate */
  1035.         f -= (double)fhi;
  1036.         f *= 16777216.0; /* 2**24 */
  1037.         flo = (long) floor(f + 0.5); /* Round */
  1038.  
  1039.         /* First byte */
  1040.         *p = (s<<7) | (e>>4);
  1041.         p++;
  1042.  
  1043.         /* Second byte */
  1044.         *p = (char) (((e&0xF)<<4) | (fhi>>24));
  1045.         p++;
  1046.  
  1047.         /* Third byte */
  1048.         *p = (fhi>>16) & 0xFF;
  1049.         p++;
  1050.  
  1051.         /* Fourth byte */
  1052.         *p = (fhi>>8) & 0xFF;
  1053.         p++;
  1054.  
  1055.         /* Fifth byte */
  1056.         *p = fhi & 0xFF;
  1057.         p++;
  1058.  
  1059.         /* Sixth byte */
  1060.         *p = (flo>>16) & 0xFF;
  1061.         p++;
  1062.  
  1063.         /* Seventh byte */
  1064.         *p = (flo>>8) & 0xFF;
  1065.         p++;
  1066.  
  1067.         /* Eighth byte */
  1068.         *p = flo & 0xFF;
  1069.  
  1070.         if ((*self->write_func)(self, str, 9) < 0)
  1071.             return -1;
  1072.     }
  1073.     else {
  1074.         char c_str[250];
  1075.         c_str[0] = FLOAT;
  1076.         sprintf(c_str + 1, "%.17g\n", x);
  1077.  
  1078.         if ((*self->write_func)(self, c_str, strlen(c_str)) < 0)
  1079.             return -1;
  1080.     }
  1081.  
  1082.     return 0;
  1083. }
  1084.  
  1085.  
  1086. static int
  1087. save_string(Picklerobject *self, PyObject *args, int doput) {
  1088.     int size, len;
  1089.     PyObject *repr=0;
  1090.  
  1091.     if ((size = PyString_Size(args)) < 0)
  1092.       return -1;
  1093.  
  1094.     if (!self->bin) {
  1095.         char *repr_str;
  1096.  
  1097.         static char string = STRING;
  1098.  
  1099.         UNLESS (repr = PyObject_Repr(args))
  1100.             return -1;
  1101.  
  1102.         if ((len = PyString_Size(repr)) < 0)
  1103.           goto err;
  1104.         repr_str = PyString_AS_STRING((PyStringObject *)repr);
  1105.  
  1106.         if ((*self->write_func)(self, &string, 1) < 0)
  1107.             goto err;
  1108.  
  1109.         if ((*self->write_func)(self, repr_str, len) < 0)
  1110.             goto err;
  1111.  
  1112.         if ((*self->write_func)(self, "\n", 1) < 0)
  1113.             goto err;
  1114.  
  1115.         Py_XDECREF(repr);
  1116.     }
  1117.     else {
  1118.         int i;
  1119.         char c_str[5];
  1120.  
  1121.         if ((size = PyString_Size(args)) < 0)
  1122.           return -1;
  1123.  
  1124.         if (size < 256) {
  1125.             c_str[0] = SHORT_BINSTRING;
  1126.             c_str[1] = size;
  1127.             len = 2;
  1128.         }
  1129.         else {
  1130.             c_str[0] = BINSTRING;
  1131.             for (i = 1; i < 5; i++)
  1132.                 c_str[i] = (int)(size >> ((i - 1) * 8));
  1133.             len = 5;
  1134.         }
  1135.  
  1136.         if ((*self->write_func)(self, c_str, len) < 0)
  1137.             return -1;
  1138.  
  1139.         if (size > 128 && Pdata_Check(self->file)) {
  1140.             if (write_other(self, NULL, 0) < 0) return -1;
  1141.             PDATA_APPEND(self->file, args, -1);
  1142.           }
  1143.         else {
  1144.           if ((*self->write_func)(self, 
  1145.               PyString_AS_STRING((PyStringObject *)args), size) < 0)
  1146.             return -1;
  1147.         }
  1148.     }
  1149.  
  1150.     if (doput)
  1151.       if (put(self, args) < 0)
  1152.         return -1;
  1153.  
  1154.     return 0;
  1155.  
  1156. err:
  1157.     Py_XDECREF(repr);
  1158.     return -1;
  1159. }
  1160.  
  1161.  
  1162. static int
  1163. save_tuple(Picklerobject *self, PyObject *args) {
  1164.     PyObject *element = 0, *py_tuple_id = 0;
  1165.     int len, i, has_key, res = -1;
  1166.  
  1167.     static char tuple = TUPLE;
  1168.  
  1169.     if ((*self->write_func)(self, &MARKv, 1) < 0)
  1170.         goto finally;
  1171.  
  1172.     if ((len = PyTuple_Size(args)) < 0)  
  1173.         goto finally;
  1174.  
  1175.     for (i = 0; i < len; i++) {
  1176.         UNLESS (element = PyTuple_GET_ITEM((PyTupleObject *)args, i))  
  1177.             goto finally;
  1178.     
  1179.         if (save(self, element, 0) < 0)
  1180.             goto finally;
  1181.     }
  1182.  
  1183.     UNLESS (py_tuple_id = PyInt_FromLong((long)args))
  1184.         goto finally;
  1185.  
  1186.     if (len) {
  1187.         if ((has_key = cPickle_PyMapping_HasKey(self->memo, py_tuple_id)) < 0)
  1188.             goto finally;
  1189.  
  1190.         if (has_key) {
  1191.             if (self->bin) {
  1192.                 static char pop_mark = POP_MARK;
  1193.   
  1194.                 if ((*self->write_func)(self, &pop_mark, 1) < 0)
  1195.                     goto finally;
  1196.             }
  1197.             else {
  1198.                 static char pop = POP;
  1199.        
  1200.                 for (i = 0; i <= len; i++) {
  1201.                     if ((*self->write_func)(self, &pop, 1) < 0)
  1202.                         goto finally;
  1203.                 } 
  1204.             }
  1205.         
  1206.             if (get(self, py_tuple_id) < 0)
  1207.                 goto finally;
  1208.  
  1209.             res = 0;
  1210.             goto finally;
  1211.         }
  1212.     }
  1213.  
  1214.     if ((*self->write_func)(self, &tuple, 1) < 0) {
  1215.         goto finally;
  1216.     }
  1217.  
  1218.     if (put(self, args) < 0)
  1219.         goto finally;
  1220.  
  1221.     res = 0;
  1222.  
  1223. finally:
  1224.     Py_XDECREF(py_tuple_id);
  1225.  
  1226.     return res;
  1227. }
  1228.  
  1229. static int
  1230. save_empty_tuple(Picklerobject *self, PyObject *args) {
  1231.     static char tuple = EMPTY_TUPLE;
  1232.  
  1233.     return (*self->write_func)(self, &tuple, 1);
  1234. }
  1235.  
  1236.  
  1237. static int
  1238. save_list(Picklerobject *self, PyObject *args) {
  1239.     PyObject *element = 0;
  1240.     int s_len, len, i, using_appends, res = -1;
  1241.     char s[3];
  1242.  
  1243.     static char append = APPEND, appends = APPENDS;
  1244.  
  1245.     if (self->bin) {
  1246.         s[0] = EMPTY_LIST;
  1247.         s_len = 1;
  1248.     } 
  1249.     else {
  1250.         s[0] = MARK;
  1251.         s[1] = LIST;
  1252.         s_len = 2;
  1253.     }
  1254.  
  1255.     if ((len = PyList_Size(args)) < 0)
  1256.         goto finally;
  1257.  
  1258.     if ((*self->write_func)(self, s, s_len) < 0)
  1259.         goto finally;
  1260.  
  1261.     if (len == 0) {
  1262.         if (put(self, args) < 0)
  1263.             goto finally;
  1264.     }
  1265.     else {
  1266.         if (put2(self, args) < 0)
  1267.             goto finally;
  1268.     }
  1269.  
  1270.     if ((using_appends = (self->bin && (len > 1))))
  1271.         if ((*self->write_func)(self, &MARKv, 1) < 0)
  1272.             goto finally;
  1273.  
  1274.     for (i = 0; i < len; i++) {
  1275.         UNLESS (element = PyList_GET_ITEM((PyListObject *)args, i))  
  1276.             goto finally;
  1277.  
  1278.         if (save(self, element, 0) < 0)  
  1279.             goto finally;       
  1280.  
  1281.         if (!using_appends) {
  1282.             if ((*self->write_func)(self, &append, 1) < 0)
  1283.                 goto finally;
  1284.         }
  1285.     }
  1286.  
  1287.     if (using_appends) {
  1288.         if ((*self->write_func)(self, &appends, 1) < 0)
  1289.             goto finally;
  1290.     }
  1291.  
  1292.     res = 0;
  1293.  
  1294. finally:
  1295.  
  1296.     return res;
  1297. }
  1298.  
  1299.  
  1300. static int
  1301. save_dict(Picklerobject *self, PyObject *args) {
  1302.     PyObject *key = 0, *value = 0;
  1303.     int i, len, res = -1, using_setitems;
  1304.     char s[3];
  1305.  
  1306.     static char setitem = SETITEM, setitems = SETITEMS;
  1307.  
  1308.     if (self->bin) {
  1309.         s[0] = EMPTY_DICT;
  1310.         len = 1;
  1311.     }
  1312.     else {
  1313.         s[0] = MARK;
  1314.         s[1] = DICT;
  1315.         len = 2;
  1316.     }
  1317.  
  1318.     if ((*self->write_func)(self, s, len) < 0)
  1319.         goto finally;
  1320.  
  1321.     if ((len = PyDict_Size(args)) < 0)
  1322.         goto finally;
  1323.  
  1324.     if (len == 0) {
  1325.         if (put(self, args) < 0)
  1326.             goto finally;
  1327.     }
  1328.     else {
  1329.         if (put2(self, args) < 0)
  1330.             goto finally;
  1331.     }
  1332.  
  1333.     if ((using_setitems = (self->bin && (PyDict_Size(args) > 1))))
  1334.         if ((*self->write_func)(self, &MARKv, 1) < 0)
  1335.             goto finally;
  1336.  
  1337.     i = 0;
  1338.     while (PyDict_Next(args, &i, &key, &value)) {
  1339.         if (save(self, key, 0) < 0)
  1340.             goto finally;
  1341.  
  1342.         if (save(self, value, 0) < 0)
  1343.             goto finally;
  1344.  
  1345.         if (!using_setitems) {
  1346.             if ((*self->write_func)(self, &setitem, 1) < 0)
  1347.                 goto finally;
  1348.         }
  1349.     }
  1350.  
  1351.     if (using_setitems) {
  1352.         if ((*self->write_func)(self, &setitems, 1) < 0)
  1353.             goto finally;
  1354.     }
  1355.  
  1356.     res = 0;
  1357.  
  1358. finally:
  1359.  
  1360.     return res;
  1361. }
  1362.  
  1363.  
  1364. static int  
  1365. save_inst(Picklerobject *self, PyObject *args) {
  1366.     PyObject *class = 0, *module = 0, *name = 0, *state = 0, 
  1367.              *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
  1368.     char *module_str, *name_str;
  1369.     int module_size, name_size, res = -1;
  1370.  
  1371.     static char inst = INST, obj = OBJ, build = BUILD;
  1372.  
  1373.     if ((*self->write_func)(self, &MARKv, 1) < 0)
  1374.         goto finally;
  1375.  
  1376.     UNLESS (class = PyObject_GetAttr(args, __class___str))
  1377.         goto finally;
  1378.  
  1379.     if (self->bin) {
  1380.         if (save(self, class, 0) < 0)
  1381.             goto finally;
  1382.     }
  1383.  
  1384.     if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
  1385.         PyObject *element = 0;
  1386.         int i, len;
  1387.  
  1388.         UNLESS (class_args = 
  1389.             PyObject_CallObject(getinitargs_func, empty_tuple))
  1390.             goto finally;
  1391.  
  1392.         if ((len = PyObject_Length(class_args)) < 0)  
  1393.             goto finally;
  1394.  
  1395.         for (i = 0; i < len; i++) {
  1396.             UNLESS (element = PySequence_GetItem(class_args, i)) 
  1397.                 goto finally;
  1398.  
  1399.             if (save(self, element, 0) < 0) {
  1400.                 Py_DECREF(element);
  1401.                 goto finally;
  1402.             }
  1403.  
  1404.             Py_DECREF(element);
  1405.         }
  1406.     }
  1407.     else {
  1408.         PyErr_Clear();
  1409.     }
  1410.  
  1411.     if (!self->bin) {
  1412.         UNLESS (name = ((PyClassObject *)class)->cl_name) {
  1413.             PyErr_SetString(PicklingError, "class has no name");
  1414.             goto finally;
  1415.         }
  1416.  
  1417.         UNLESS (module = whichmodule(class, name))
  1418.             goto finally;
  1419.  
  1420.         
  1421.         if ((module_size = PyString_Size(module)) < 0 ||
  1422.            (name_size = PyString_Size(name)) < 0)
  1423.           goto finally;
  1424.  
  1425.         module_str = PyString_AS_STRING((PyStringObject *)module);
  1426.         name_str   = PyString_AS_STRING((PyStringObject *)name);
  1427.  
  1428.         if ((*self->write_func)(self, &inst, 1) < 0)
  1429.             goto finally;
  1430.  
  1431.         if ((*self->write_func)(self, module_str, module_size) < 0)
  1432.             goto finally;
  1433.  
  1434.         if ((*self->write_func)(self, "\n", 1) < 0)
  1435.             goto finally;
  1436.  
  1437.         if ((*self->write_func)(self, name_str, name_size) < 0)
  1438.             goto finally;
  1439.  
  1440.         if ((*self->write_func)(self, "\n", 1) < 0)
  1441.             goto finally;
  1442.     }
  1443.     else if ((*self->write_func)(self, &obj, 1) < 0) {
  1444.         goto finally;
  1445.     }
  1446.  
  1447.     if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
  1448.         UNLESS (state = PyObject_CallObject(getstate_func, empty_tuple))
  1449.             goto finally;
  1450.     }
  1451.     else {
  1452.         PyErr_Clear();
  1453.  
  1454.         UNLESS (state = PyObject_GetAttr(args, __dict___str)) {
  1455.             PyErr_Clear();
  1456.             res = 0;
  1457.             goto finally;
  1458.         }
  1459.     }
  1460.  
  1461.     if (!PyDict_Check(state)) {
  1462.         if (put2(self, args) < 0)
  1463.             goto finally;
  1464.     }
  1465.     else {
  1466.         if (put(self, args) < 0)
  1467.             goto finally;
  1468.     }
  1469.   
  1470.     if (save(self, state, 0) < 0)
  1471.         goto finally;
  1472.  
  1473.     if ((*self->write_func)(self, &build, 1) < 0)
  1474.         goto finally;
  1475.  
  1476.     res = 0;
  1477.  
  1478. finally:
  1479.     Py_XDECREF(module);
  1480.     Py_XDECREF(class);
  1481.     Py_XDECREF(state);
  1482.     Py_XDECREF(getinitargs_func);
  1483.     Py_XDECREF(getstate_func);
  1484.     Py_XDECREF(class_args);
  1485.  
  1486.     return res;
  1487. }
  1488.  
  1489.  
  1490. static int
  1491. save_global(Picklerobject *self, PyObject *args, PyObject *name) {
  1492.     PyObject *global_name = 0, *module = 0;
  1493.     char *name_str, *module_str; 
  1494.     int module_size, name_size, res = -1;
  1495.  
  1496.     static char global = GLOBAL;
  1497.  
  1498.     if (name) {
  1499.         global_name = name;
  1500.         Py_INCREF(global_name);
  1501.     }
  1502.     else {
  1503.         UNLESS (global_name = PyObject_GetAttr(args, __name___str))
  1504.             goto finally;
  1505.     }
  1506.  
  1507.     UNLESS (module = whichmodule(args, global_name))
  1508.         goto finally;
  1509.  
  1510.     if ((module_size = PyString_Size(module)) < 0 ||
  1511.         (name_size = PyString_Size(global_name)) < 0)
  1512.       goto finally;
  1513.  
  1514.     module_str = PyString_AS_STRING((PyStringObject *)module);
  1515.     name_str   = PyString_AS_STRING((PyStringObject *)global_name);
  1516.  
  1517.     if ((*self->write_func)(self, &global, 1) < 0)
  1518.         goto finally;
  1519.  
  1520.     if ((*self->write_func)(self, module_str, module_size) < 0)
  1521.         goto finally;
  1522.  
  1523.     if ((*self->write_func)(self, "\n", 1) < 0)
  1524.         goto finally;
  1525.  
  1526.     if ((*self->write_func)(self, name_str, name_size) < 0)
  1527.         goto finally;
  1528.  
  1529.     if ((*self->write_func)(self, "\n", 1) < 0)
  1530.         goto finally;
  1531.  
  1532.     if (put(self, args) < 0)
  1533.         goto finally;
  1534.  
  1535.     res = 0;
  1536.  
  1537. finally:
  1538.     Py_XDECREF(module);
  1539.     Py_XDECREF(global_name);
  1540.  
  1541.     return res;
  1542. }
  1543.  
  1544. static int
  1545. save_pers(Picklerobject *self, PyObject *args, PyObject *f) {
  1546.     PyObject *pid = 0;
  1547.     int size, res = -1;
  1548.  
  1549.     static char persid = PERSID, binpersid = BINPERSID;
  1550.  
  1551.     Py_INCREF(args);
  1552.     ARG_TUP(self, args);
  1553.     if (self->arg) {
  1554.         pid = PyObject_CallObject(f, self->arg);
  1555.         FREE_ARG_TUP(self);
  1556.     }
  1557.     if (! pid) return -1;
  1558.  
  1559.     if (pid != Py_None) {
  1560.         if (!self->bin) {
  1561.             if (!PyString_Check(pid)) {
  1562.                 PyErr_SetString(PicklingError, 
  1563.                     "persistent id must be string");
  1564.                 goto finally;
  1565.             }
  1566.  
  1567.             if ((*self->write_func)(self, &persid, 1) < 0)
  1568.                 goto finally;
  1569.  
  1570.             if ((size = PyString_Size(pid)) < 0)
  1571.                 goto finally;
  1572.  
  1573.             if ((*self->write_func)(self, 
  1574.                 PyString_AS_STRING((PyStringObject *)pid), size) < 0)
  1575.                 goto finally;
  1576.  
  1577.             if ((*self->write_func)(self, "\n", 1) < 0)
  1578.                 goto finally;
  1579.      
  1580.             res = 1;
  1581.             goto finally;
  1582.         }
  1583.         else if (save(self, pid, 1) >= 0) {
  1584.             if ((*self->write_func)(self, &binpersid, 1) < 0)
  1585.                 res = -1;
  1586.             else
  1587.                 res = 1;
  1588.         }
  1589.  
  1590.         goto finally;              
  1591.     }
  1592.  
  1593.     res = 0;
  1594.  
  1595. finally:
  1596.     Py_XDECREF(pid);
  1597.  
  1598.     return res;
  1599. }
  1600.  
  1601.  
  1602. static int 
  1603. save_reduce(Picklerobject *self, PyObject *callable,
  1604.             PyObject *tup, PyObject *state, PyObject *ob) {
  1605.     static char reduce = REDUCE, build = BUILD;
  1606.  
  1607.     if (save(self, callable, 0) < 0)
  1608.         return -1;
  1609.  
  1610.     if (save(self, tup, 0) < 0)
  1611.         return -1;
  1612.  
  1613.     if ((*self->write_func)(self, &reduce, 1) < 0)
  1614.         return -1;
  1615.  
  1616.     if (ob != NULL) {
  1617.         if (state && !PyDict_Check(state)) {
  1618.             if (put2(self, ob) < 0)
  1619.                 return -1;
  1620.         }
  1621.         else {
  1622.             if (put(self, ob) < 0)
  1623.                 return -1;
  1624.         }
  1625.     }
  1626.     
  1627.     if (state) {
  1628.         if (save(self, state, 0) < 0)
  1629.             return -1;
  1630.  
  1631.         if ((*self->write_func)(self, &build, 1) < 0)
  1632.             return -1;
  1633.     }
  1634.  
  1635.     return 0;
  1636. }
  1637.  
  1638. static int
  1639. save(Picklerobject *self, PyObject *args, int  pers_save) {
  1640.     PyTypeObject *type;
  1641.     PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0, *arg_tup = 0,
  1642.              *callable = 0, *state = 0;
  1643.     int res = -1, tmp, size;
  1644.  
  1645.     if (!pers_save && self->pers_func) {
  1646.         if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
  1647.             res = tmp;
  1648.             goto finally;
  1649.         }
  1650.     }
  1651.  
  1652.     if (args == Py_None) {
  1653.         res = save_none(self, args);
  1654.         goto finally;
  1655.     }
  1656.  
  1657.     type = args->ob_type;
  1658.  
  1659.     switch (type->tp_name[0]) {
  1660.         case 'i':
  1661.             if (type == &PyInt_Type) {
  1662.                 res = save_int(self, args);
  1663.                 goto finally;
  1664.             }
  1665.             break;
  1666.  
  1667.         case 'l':
  1668.             if (type == &PyLong_Type) {
  1669.                 res = save_long(self, args);
  1670.                 goto finally;
  1671.             }
  1672.             break;
  1673.  
  1674.         case 'f':
  1675.             if (type == &PyFloat_Type) {
  1676.                 res = save_float(self, args);
  1677.                 goto finally;
  1678.             }
  1679.             break;
  1680.  
  1681.         case 't':
  1682.             if (type == &PyTuple_Type && PyTuple_Size(args)==0) {
  1683.                 if (self->bin) res = save_empty_tuple(self, args);
  1684.                 else          res = save_tuple(self, args);
  1685.                 goto finally;
  1686.             }
  1687.             break;
  1688.  
  1689.         case 's':
  1690.             if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
  1691.                 res = save_string(self, args, 0);
  1692.                 goto finally;
  1693.             }
  1694.     }
  1695.  
  1696.     if (args->ob_refcnt > 1) {
  1697.         long ob_id;
  1698.         int  has_key;
  1699.  
  1700.         ob_id = (long)args;
  1701.  
  1702.         UNLESS (py_ob_id = PyInt_FromLong(ob_id))
  1703.             goto finally;
  1704.  
  1705.         if ((has_key = cPickle_PyMapping_HasKey(self->memo, py_ob_id)) < 0)
  1706.             goto finally;
  1707.  
  1708.         if (has_key) {
  1709.             if (get(self, py_ob_id) < 0)
  1710.                 goto finally;
  1711.  
  1712.             res = 0;
  1713.             goto finally;
  1714.         }
  1715.     }
  1716.  
  1717.     switch (type->tp_name[0]) {
  1718.         case 's':
  1719.             if (type == &PyString_Type) {
  1720.                 res = save_string(self, args, 1);
  1721.                 goto finally;
  1722.             }
  1723.             break;
  1724.  
  1725.         case 't':
  1726.             if (type == &PyTuple_Type) {
  1727.                 res = save_tuple(self, args);
  1728.                 goto finally;
  1729.             }
  1730.             break;
  1731.  
  1732.         case 'l':
  1733.             if (type == &PyList_Type) {
  1734.                 res = save_list(self, args);
  1735.                 goto finally;
  1736.             }
  1737.             break;
  1738.  
  1739.         case 'd':
  1740.             if (type == &PyDict_Type) {
  1741.                 res = save_dict(self, args);
  1742.                 goto finally; 
  1743.             }
  1744.             break;
  1745.  
  1746.         case 'i':
  1747.             if (type == &PyInstance_Type) {
  1748.                 res = save_inst(self, args);
  1749.                 goto finally;
  1750.             }
  1751.             break;
  1752.  
  1753.         case 'c':
  1754.             if (type == &PyClass_Type) {
  1755.                 res = save_global(self, args, NULL);
  1756.                 goto finally;
  1757.             }
  1758.             break;
  1759.  
  1760.         case 'f':
  1761.             if (type == &PyFunction_Type) {
  1762.                 res = save_global(self, args, NULL);
  1763.                 goto finally;
  1764.             }
  1765.             break;
  1766.  
  1767.         case 'b':
  1768.             if (type == &PyCFunction_Type) {
  1769.                 res = save_global(self, args, NULL);
  1770.                 goto finally;
  1771.             }
  1772.     }
  1773.  
  1774.     if (!pers_save && self->inst_pers_func) {
  1775.         if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
  1776.             res = tmp;
  1777.             goto finally;
  1778.         }
  1779.     }
  1780.  
  1781.     if ((__reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type))) {
  1782.         Py_INCREF(__reduce__);
  1783.  
  1784.         Py_INCREF(args);
  1785.         ARG_TUP(self, args);
  1786.         if (self->arg) {
  1787.             t = PyObject_CallObject(__reduce__, self->arg);
  1788.             FREE_ARG_TUP(self);
  1789.         }
  1790.         if (! t) goto finally;
  1791.     }        
  1792.     else {
  1793.         PyErr_Clear();
  1794.  
  1795.         if ((__reduce__ = PyObject_GetAttr(args, __reduce___str))) {
  1796.             UNLESS (t = PyObject_CallObject(__reduce__, empty_tuple))
  1797.                 goto finally;
  1798.         }
  1799.         else {
  1800.             PyErr_Clear();
  1801.         }
  1802.     }
  1803.  
  1804.     if (t) {
  1805.         if (PyString_Check(t)) {
  1806.             res = save_global(self, args, t);
  1807.             goto finally;
  1808.         }
  1809.  
  1810.         if (!PyTuple_Check(t)) {
  1811.             cPickle_ErrFormat(PicklingError, "Value returned by %s must "
  1812.                 "be a tuple", "O", __reduce__);
  1813.             goto finally;
  1814.         }
  1815.  
  1816.         size = PyTuple_Size(t);
  1817.         
  1818.         if ((size != 3) && (size != 2)) {
  1819.             cPickle_ErrFormat(PicklingError, "tuple returned by %s must "     
  1820.                 "contain only two or three elements", "O", __reduce__);
  1821.                 goto finally;
  1822.         }
  1823.         
  1824.         callable = PyTuple_GET_ITEM(t, 0);
  1825.  
  1826.         arg_tup = PyTuple_GET_ITEM(t, 1);
  1827.  
  1828.         if (size > 2) {
  1829.             state = PyTuple_GET_ITEM(t, 2);
  1830.         }
  1831.  
  1832.         UNLESS (PyTuple_Check(arg_tup) || arg_tup==Py_None) {
  1833.             cPickle_ErrFormat(PicklingError, "Second element of tuple "
  1834.                 "returned by %s must be a tuple", "O", __reduce__);
  1835.             goto finally;
  1836.         }
  1837.  
  1838.         res = save_reduce(self, callable, arg_tup, state, args);
  1839.         goto finally;
  1840.     }
  1841.  
  1842.     PyErr_SetObject(UnpickleableError, args);
  1843.  
  1844. finally:
  1845.     Py_XDECREF(py_ob_id);
  1846.     Py_XDECREF(__reduce__);
  1847.     Py_XDECREF(t);
  1848.    
  1849.     return res;
  1850. }
  1851.  
  1852.  
  1853. static int
  1854. dump(Picklerobject *self, PyObject *args) {
  1855.     static char stop = STOP;
  1856.  
  1857.     if (save(self, args, 0) < 0)
  1858.         return -1;
  1859.  
  1860.     if ((*self->write_func)(self, &stop, 1) < 0)
  1861.         return -1;
  1862.  
  1863.     if ((*self->write_func)(self, NULL, 0) < 0)
  1864.         return -1;
  1865.  
  1866.     return 0;
  1867. }
  1868.  
  1869. static PyObject *
  1870. Pickle_clear_memo(Picklerobject *self, PyObject *args) {
  1871.   if (args && ! PyArg_ParseTuple(args,":clear_memo")) return NULL;
  1872.   if (self->memo) PyDict_Clear(self->memo);
  1873.   Py_INCREF(Py_None);
  1874.   return Py_None;
  1875. }
  1876.  
  1877. static PyObject *
  1878. Pickle_getvalue(Picklerobject *self, PyObject *args) {
  1879.   int l, i, rsize, ssize, clear=1, lm;
  1880.   long ik;
  1881.   PyObject *k, *r;
  1882.   char *s, *p, *have_get;
  1883.   Pdata *data;
  1884.  
  1885.   if (args && ! PyArg_ParseTuple(args,"|i:getvalue",&clear)) return NULL;
  1886.  
  1887.   /* Check to make sure we are based on a list */
  1888.   if (! Pdata_Check(self->file)) {
  1889.       PyErr_SetString(PicklingError,
  1890.                       "Attempt to getvalue a non-list-based pickler");
  1891.       return NULL;
  1892.     }
  1893.  
  1894.   /* flush write buffer */
  1895.   if (write_other(self, NULL, 0) < 0) return NULL;
  1896.  
  1897.   data=(Pdata*)self->file;
  1898.   l=data->length;
  1899.  
  1900.   /* set up an array to hold get/put status */
  1901.   if ((lm=PyDict_Size(self->memo)) < 0) return NULL;
  1902.   lm++;
  1903.   if (! (have_get=malloc((lm)*sizeof(char)))) return PyErr_NoMemory();  
  1904.   memset(have_get,0,lm);
  1905.  
  1906.   /* Scan for gets. */
  1907.   for (rsize=0, i=l; --i >= 0; ) {
  1908.       k=data->data[i];
  1909.       
  1910.       if (PyString_Check(k)) {
  1911.         rsize += PyString_GET_SIZE(k);
  1912.       }
  1913.  
  1914.       else if (PyInt_Check(k)) { /* put */
  1915.         ik=PyInt_AS_LONG((PyIntObject*)k);
  1916.         if (ik >= lm || ik==0) {
  1917.           PyErr_SetString(PicklingError,
  1918.                           "Invalid get data");
  1919.           return NULL;
  1920.         }      
  1921.         if (have_get[ik]) { /* with matching get */
  1922.           if (ik < 256) rsize += 2;
  1923.           else rsize+=5;
  1924.         }
  1925.       }
  1926.  
  1927.       else if (! (PyTuple_Check(k) &&
  1928.                   PyTuple_GET_SIZE(k) == 2 &&
  1929.                   PyInt_Check((k=PyTuple_GET_ITEM(k,0))))
  1930.                ) {
  1931.         PyErr_SetString(PicklingError,
  1932.                         "Unexpected data in internal list");
  1933.         return NULL;
  1934.       }
  1935.  
  1936.       else { /* put */
  1937.         ik=PyInt_AS_LONG((PyIntObject*)k);
  1938.         if (ik >= lm || ik==0) {
  1939.           PyErr_SetString(PicklingError,
  1940.                           "Invalid get data");
  1941.           return NULL;
  1942.         }      
  1943.         have_get[ik]=1;
  1944.         if (ik < 256) rsize += 2;
  1945.         else rsize+=5;
  1946.       }
  1947.  
  1948.     }
  1949.  
  1950.   /* Now generate the result */
  1951.   UNLESS (r=PyString_FromStringAndSize(NULL,rsize)) goto err;
  1952.   s=PyString_AS_STRING((PyStringObject*)r);
  1953.  
  1954.   for (i=0; i<l; i++) {
  1955.       k=data->data[i];
  1956.  
  1957.       if (PyString_Check(k)) {
  1958.         ssize=PyString_GET_SIZE(k);
  1959.         if (ssize) {
  1960.             p=PyString_AS_STRING((PyStringObject*)k);
  1961.             while (--ssize >= 0) *s++=*p++;
  1962.           }
  1963.       }
  1964.  
  1965.       else if (PyTuple_Check(k)) { /* get */
  1966.         ik=PyInt_AS_LONG((PyIntObject*)PyTuple_GET_ITEM(k,0));
  1967.         if (ik < 256) {
  1968.           *s++ = BINGET;
  1969.           *s++ = (int)(ik & 0xff);
  1970.         }
  1971.         else {
  1972.           *s++ = LONG_BINGET;
  1973.           *s++ = (int)(ik & 0xff);
  1974.           *s++ = (int)((ik >> 8)  & 0xff);
  1975.           *s++ = (int)((ik >> 16) & 0xff);
  1976.           *s++ = (int)((ik >> 24) & 0xff);
  1977.         }
  1978.       }
  1979.  
  1980.       else { /* put */
  1981.         ik=PyInt_AS_LONG((PyIntObject*)k);
  1982.  
  1983.         if (have_get[ik]) { /* with matching get */
  1984.           if (ik < 256) {
  1985.             *s++ = BINPUT;
  1986.             *s++ = (int)(ik & 0xff);
  1987.           }
  1988.           else {
  1989.             *s++ = LONG_BINPUT;
  1990.             *s++ = (int)(ik & 0xff);
  1991.             *s++ = (int)((ik >> 8)  & 0xff);
  1992.             *s++ = (int)((ik >> 16) & 0xff);
  1993.             *s++ = (int)((ik >> 24) & 0xff);
  1994.           }
  1995.         }
  1996.       }
  1997.  
  1998.     }
  1999.  
  2000.   if (clear) {
  2001.     PyDict_Clear(self->memo);
  2002.     Pdata_clear(data,0);
  2003.   }
  2004.     
  2005.   free(have_get);
  2006.   return r;
  2007. err:
  2008.   free(have_get);
  2009.   return NULL;
  2010. }
  2011.  
  2012. static PyObject *
  2013. Pickler_dump(Picklerobject *self, PyObject *args) {
  2014.     PyObject *ob;
  2015.     int get=0;
  2016.  
  2017.     UNLESS (PyArg_ParseTuple(args, "O|i:dump", &ob, &get))
  2018.         return NULL;
  2019.  
  2020.     if (dump(self, ob) < 0)
  2021.         return NULL;
  2022.  
  2023.     if (get) return Pickle_getvalue(self, NULL);
  2024.  
  2025.     Py_INCREF(self);
  2026.     return (PyObject*)self;
  2027. }
  2028.  
  2029.  
  2030. static struct PyMethodDef Pickler_methods[] = {
  2031.   {"dump",          (PyCFunction)Pickler_dump,  1,
  2032.    "dump(object) --"
  2033.    "Write an object in pickle format to the object's pickle stream\n"
  2034.   },
  2035.   {"clear_memo",  (PyCFunction)Pickle_clear_memo,  1,
  2036.    "clear_memo() -- Clear the picklers memo"},
  2037.   {"getvalue",  (PyCFunction)Pickle_getvalue,  1,
  2038.    "getvalue() -- Finish picking a list-based pickle"},
  2039.   {NULL,                NULL}           /* sentinel */
  2040. };
  2041.  
  2042.  
  2043. static Picklerobject *
  2044. newPicklerobject(PyObject *file, int bin) {
  2045.     Picklerobject *self;
  2046.  
  2047.     UNLESS (self = PyObject_NEW(Picklerobject, &Picklertype))
  2048.         return NULL;
  2049.  
  2050.     self->fp = NULL;
  2051.     self->write = NULL;
  2052.     self->memo = NULL;
  2053.     self->arg = NULL;
  2054.     self->pers_func = NULL;
  2055.     self->inst_pers_func = NULL;
  2056.     self->write_buf = NULL;
  2057.     self->bin = bin;
  2058.     self->fast = 0;
  2059.     self->buf_size = 0;
  2060.     self->dispatch_table = NULL;
  2061.  
  2062.     if (file)
  2063.       Py_INCREF(file);
  2064.     else
  2065.       file=Pdata_New();
  2066.  
  2067.     self->file = file;
  2068.  
  2069.     UNLESS (self->memo = PyDict_New()) {
  2070.        Py_XDECREF((PyObject *)self);
  2071.        return NULL;
  2072.     }
  2073.  
  2074.     if (PyFile_Check(file)) {
  2075.         self->fp = PyFile_AsFile(file);
  2076.     if (self->fp == NULL) {
  2077.         PyErr_SetString(PyExc_IOError, "output file closed");
  2078.         return NULL;
  2079.     }
  2080.         self->write_func = write_file;
  2081.     }
  2082.     else if (PycStringIO_OutputCheck(file)) {
  2083.         self->write_func = write_cStringIO;
  2084.     }
  2085.     else if (file == Py_None) {
  2086.         self->write_func = write_none;
  2087.     }
  2088.     else {
  2089.         self->write_func = write_other;
  2090.  
  2091.         if (! Pdata_Check(file)) {
  2092.           UNLESS (self->write = PyObject_GetAttr(file, write_str)) {
  2093.             PyErr_Clear();
  2094.             PyErr_SetString(PyExc_TypeError, "argument must have 'write' "
  2095.                             "attribute");
  2096.             goto err;
  2097.           }
  2098.         }
  2099.  
  2100.         UNLESS (self->write_buf = 
  2101.             (char *)malloc(WRITE_BUF_SIZE * sizeof(char))) { 
  2102.             PyErr_NoMemory();
  2103.             goto err;
  2104.         }
  2105.     }
  2106.  
  2107.     if (PyEval_GetRestricted()) {
  2108.         /* Restricted execution, get private tables */
  2109.         PyObject *m;
  2110.  
  2111.         UNLESS (m=PyImport_Import(copy_reg_str)) goto err;
  2112.         self->dispatch_table=PyObject_GetAttr(m, dispatch_table_str);
  2113.         Py_DECREF(m);
  2114.         UNLESS (self->dispatch_table) goto err;
  2115.     }
  2116.     else {
  2117.         self->dispatch_table=dispatch_table;
  2118.         Py_INCREF(dispatch_table);
  2119.     }
  2120.  
  2121.     return self;
  2122.  
  2123. err:
  2124.     Py_DECREF((PyObject *)self);
  2125.     return NULL;
  2126. }
  2127.  
  2128.  
  2129. static PyObject *
  2130. get_Pickler(PyObject *self, PyObject *args) {
  2131.     PyObject *file=NULL;
  2132.     int bin;
  2133.  
  2134.     bin=1;
  2135.     if (! PyArg_ParseTuple(args, "|i:Pickler", &bin)) {
  2136.         PyErr_Clear();
  2137.         bin=0;
  2138.         if (! PyArg_ParseTuple(args, "O|i:Pickler", &file, &bin))
  2139.           return NULL;
  2140.       }
  2141.     return (PyObject *)newPicklerobject(file, bin);
  2142. }
  2143.  
  2144.  
  2145. static void
  2146. Pickler_dealloc(Picklerobject *self) {
  2147.     Py_XDECREF(self->write);
  2148.     Py_XDECREF(self->memo);
  2149.     Py_XDECREF(self->arg);
  2150.     Py_XDECREF(self->file);
  2151.     Py_XDECREF(self->pers_func);
  2152.     Py_XDECREF(self->inst_pers_func);
  2153.     Py_XDECREF(self->dispatch_table);
  2154.  
  2155.     if (self->write_buf) {    
  2156.         free(self->write_buf);
  2157.     }
  2158.  
  2159.     PyMem_DEL(self);
  2160. }
  2161.  
  2162.  
  2163. static PyObject *
  2164. Pickler_getattr(Picklerobject *self, char *name) {
  2165.  
  2166.   switch (*name) {
  2167.   case 'p':
  2168.     if (strcmp(name, "persistent_id") == 0) {
  2169.         if (!self->pers_func) {
  2170.             PyErr_SetString(PyExc_AttributeError, name);
  2171.             return NULL;
  2172.         }
  2173.  
  2174.         Py_INCREF(self->pers_func);
  2175.         return self->pers_func;
  2176.     }
  2177.     break;
  2178.   case 'm':
  2179.     if (strcmp(name, "memo") == 0) {
  2180.         if (!self->memo) {
  2181.             PyErr_SetString(PyExc_AttributeError, name);
  2182.             return NULL;
  2183.         }
  2184.  
  2185.         Py_INCREF(self->memo);
  2186.         return self->memo;
  2187.     }
  2188.     break;
  2189.   case 'P':
  2190.     if (strcmp(name, "PicklingError") == 0) {
  2191.         Py_INCREF(PicklingError);
  2192.         return PicklingError;
  2193.     }
  2194.     break;
  2195.   case 'b':
  2196.     if (strcmp(name, "binary")==0)
  2197.       return PyInt_FromLong(self->bin);
  2198.     break;
  2199.   case 'f':
  2200.     if (strcmp(name, "fast")==0)
  2201.       return PyInt_FromLong(self->fast);
  2202.     break;
  2203.   case 'g':
  2204.     if (strcmp(name, "getvalue")==0 && ! Pdata_Check(self->file)) {
  2205.       PyErr_SetString(PyExc_AttributeError, name);
  2206.       return NULL;
  2207.     }
  2208.     break;
  2209.   }
  2210.   return Py_FindMethod(Pickler_methods, (PyObject *)self, name);
  2211. }
  2212.  
  2213.  
  2214. int 
  2215. Pickler_setattr(Picklerobject *self, char *name, PyObject *value) {
  2216.  
  2217.     if (! value) {
  2218.         PyErr_SetString(PyExc_TypeError,
  2219.                         "attribute deletion is not supported");
  2220.         return -1;
  2221.     }
  2222.   
  2223.     if (strcmp(name, "persistent_id") == 0) {
  2224.         Py_XDECREF(self->pers_func);
  2225.         self->pers_func = value;
  2226.         Py_INCREF(value);
  2227.         return 0;
  2228.     }
  2229.  
  2230.     if (strcmp(name, "inst_persistent_id") == 0) {
  2231.         Py_XDECREF(self->inst_pers_func);
  2232.         self->inst_pers_func = value;
  2233.         Py_INCREF(value);
  2234.         return 0;
  2235.     }
  2236.  
  2237.     if (strcmp(name, "memo") == 0) {
  2238.         if (! PyDict_Check(value)) {
  2239.           PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
  2240.           return -1;
  2241.         }
  2242.         Py_XDECREF(self->memo);
  2243.         self->memo = value;
  2244.         Py_INCREF(value);
  2245.         return 0;
  2246.     }
  2247.  
  2248.     if (strcmp(name, "binary")==0) {
  2249.         self->bin=PyObject_IsTrue(value);
  2250.         return 0;
  2251.     }
  2252.  
  2253.     if (strcmp(name, "fast")==0) {
  2254.         self->fast=PyObject_IsTrue(value);
  2255.         return 0;
  2256.     }
  2257.  
  2258.     PyErr_SetString(PyExc_AttributeError, name);
  2259.     return -1;
  2260. }
  2261.  
  2262.  
  2263. static char Picklertype__doc__[] =
  2264. "Objects that know how to pickle objects\n"
  2265. ;
  2266.  
  2267. static PyTypeObject Picklertype = {
  2268.     PyObject_HEAD_INIT(NULL)
  2269.     0,                            /*ob_size*/
  2270.     "Pickler",                    /*tp_name*/
  2271.     sizeof(Picklerobject),                /*tp_basicsize*/
  2272.     0,                            /*tp_itemsize*/
  2273.     /* methods */
  2274.     (destructor)Pickler_dealloc,  /*tp_dealloc*/
  2275.     (printfunc)0,         /*tp_print*/
  2276.     (getattrfunc)Pickler_getattr, /*tp_getattr*/
  2277.     (setattrfunc)Pickler_setattr, /*tp_setattr*/
  2278.     (cmpfunc)0,           /*tp_compare*/
  2279.     (reprfunc)0,          /*tp_repr*/
  2280.     0,                    /*tp_as_number*/
  2281.     0,            /*tp_as_sequence*/
  2282.     0,            /*tp_as_mapping*/
  2283.     (hashfunc)0,          /*tp_hash*/
  2284.     (ternaryfunc)0,               /*tp_call*/
  2285.     (reprfunc)0,          /*tp_str*/
  2286.  
  2287.     /* Space for future expansion */
  2288.     0L,0L,0L,0L,
  2289.     Picklertype__doc__ /* Documentation string */
  2290. };
  2291.  
  2292. static PyObject *
  2293. find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc) {
  2294.     PyObject *global = 0, *module;
  2295.  
  2296.     if (fc) {
  2297.       if (fc==Py_None) {
  2298.     PyErr_SetString(UnpicklingError, 
  2299.             "Global and instance pickles are not supported.");
  2300.     return NULL;
  2301.       }
  2302.       return PyObject_CallFunction(fc, "OO", py_module_name, py_global_name);
  2303.     }
  2304.  
  2305.     module = PySys_GetObject("modules");
  2306.     if (module == NULL)
  2307.       return NULL;
  2308.  
  2309.     module = PyDict_GetItem(module, py_module_name);
  2310.     if (module == NULL) {
  2311.       module = PyImport_Import(py_module_name);
  2312.       if (!module)
  2313.           return NULL;
  2314.       global = PyObject_GetAttr(module, py_global_name);
  2315.       Py_DECREF(module);
  2316.     }
  2317.     else
  2318.       global = PyObject_GetAttr(module, py_global_name);
  2319.     if (global == NULL) {
  2320.       char buf[256 + 37];
  2321.       sprintf(buf, "Failed to import class %.128s from module %.128s",
  2322.               PyString_AS_STRING((PyStringObject*)py_global_name),
  2323.               PyString_AS_STRING((PyStringObject*)py_module_name));  
  2324.       PyErr_SetString(PyExc_SystemError, buf);
  2325.       return NULL;
  2326.     }
  2327.     return global;
  2328. }
  2329.  
  2330. static int
  2331. marker(Unpicklerobject *self) {
  2332.     if (self->num_marks < 1) {
  2333.         PyErr_SetString(UnpicklingError, "could not find MARK");
  2334.         return -1;
  2335.     }
  2336.  
  2337.     return self->marks[--self->num_marks];
  2338. }
  2339.  
  2340.     
  2341. static int
  2342. load_none(Unpicklerobject *self) {
  2343.     PDATA_APPEND(self->stack, Py_None, -1);
  2344.     return 0;
  2345. }
  2346.  
  2347. static int
  2348. bad_readline() {
  2349.     PyErr_SetString(UnpicklingError, "pickle data was truncated");
  2350.     return -1;
  2351. }
  2352.  
  2353. static int
  2354. load_int(Unpicklerobject *self) {
  2355.     PyObject *py_int = 0;
  2356.     char *endptr, *s;
  2357.     int len, res = -1;
  2358.     long l;
  2359.  
  2360.     if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
  2361.     if (len < 2) return bad_readline();
  2362.     UNLESS (s=pystrndup(s,len)) return -1;
  2363.  
  2364.     errno = 0;
  2365.     l = strtol(s, &endptr, 0);
  2366.  
  2367.     if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
  2368.         /* Hm, maybe we've got something long.  Let's try reading
  2369.            it as a Python long object. */
  2370.         errno=0;
  2371.         UNLESS (py_int=PyLong_FromString(s,&endptr,0)) goto finally;
  2372.  
  2373.         if ((*endptr != '\n') || (endptr[1] != '\0')) {
  2374.             PyErr_SetString(PyExc_ValueError,
  2375.                             "could not convert string to int");
  2376.             goto finally;
  2377.         }
  2378.     }
  2379.     else {
  2380.         UNLESS (py_int = PyInt_FromLong(l)) goto finally;
  2381.     }
  2382.  
  2383.     free(s);
  2384.     PDATA_PUSH(self->stack, py_int, -1);
  2385.     return 0;
  2386.  
  2387. finally:
  2388.     free(s);
  2389.  
  2390.     return res;
  2391. }
  2392.  
  2393.  
  2394. static long 
  2395. calc_binint(char *s, int  x) {
  2396.     unsigned char c;
  2397.     int i;
  2398.     long l;
  2399.  
  2400.     for (i = 0, l = 0L; i < x; i++) {
  2401.         c = (unsigned char)s[i];
  2402.         l |= (long)c << (i * 8);
  2403.     }
  2404.  
  2405.     return l;
  2406. }
  2407.  
  2408.  
  2409. static int
  2410. load_binintx(Unpicklerobject *self, char *s, int  x) {
  2411.     PyObject *py_int = 0;
  2412.     long l;
  2413.  
  2414.     l = calc_binint(s, x);
  2415.  
  2416.     UNLESS (py_int = PyInt_FromLong(l))
  2417.         return -1;
  2418.  
  2419.     PDATA_PUSH(self->stack, py_int, -1);
  2420.     return 0;
  2421. }
  2422.  
  2423.  
  2424. static int
  2425. load_binint(Unpicklerobject *self) {
  2426.     char *s;
  2427.  
  2428.     if ((*self->read_func)(self, &s, 4) < 0)
  2429.         return -1;
  2430.  
  2431.     return load_binintx(self, s, 4);
  2432. }
  2433.  
  2434.  
  2435. static int
  2436. load_binint1(Unpicklerobject *self) {
  2437.     char *s;
  2438.  
  2439.     if ((*self->read_func)(self, &s, 1) < 0)
  2440.         return -1;
  2441.  
  2442.     return load_binintx(self, s, 1);
  2443. }
  2444.  
  2445.  
  2446. static int
  2447. load_binint2(Unpicklerobject *self) {
  2448.     char *s;
  2449.  
  2450.     if ((*self->read_func)(self, &s, 2) < 0)
  2451.         return -1;
  2452.  
  2453.     return load_binintx(self, s, 2);
  2454. }
  2455.     
  2456. static int
  2457. load_long(Unpicklerobject *self) {
  2458.     PyObject *l = 0;
  2459.     char *end, *s;
  2460.     int len, res = -1;
  2461.  
  2462.     if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
  2463.     if (len < 2) return bad_readline();
  2464.     UNLESS (s=pystrndup(s,len)) return -1;
  2465.  
  2466.     UNLESS (l = PyLong_FromString(s, &end, 0))
  2467.         goto finally;
  2468.  
  2469.     free(s);
  2470.     PDATA_PUSH(self->stack, l, -1);
  2471.     return 0;
  2472.  
  2473. finally:
  2474.     free(s);
  2475.  
  2476.     return res;
  2477. }
  2478.  
  2479.  
  2480. static int
  2481. load_float(Unpicklerobject *self) {
  2482.     PyObject *py_float = 0;
  2483.     char *endptr, *s;
  2484.     int len, res = -1;
  2485.     double d;
  2486.  
  2487.     if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
  2488.     if (len < 2) return bad_readline();
  2489.     UNLESS (s=pystrndup(s,len)) return -1;
  2490.  
  2491.     errno = 0;
  2492.     d = strtod(s, &endptr);
  2493.  
  2494.     if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
  2495.         PyErr_SetString(PyExc_ValueError, 
  2496.         "could not convert string to float");
  2497.         goto finally;
  2498.     }
  2499.  
  2500.     UNLESS (py_float = PyFloat_FromDouble(d))
  2501.         goto finally;
  2502.  
  2503.     free(s);
  2504.     PDATA_PUSH(self->stack, py_float, -1);
  2505.     return 0;
  2506.  
  2507. finally:
  2508.     free(s);
  2509.  
  2510.     return res;
  2511. }
  2512.  
  2513. static int
  2514. load_binfloat(Unpicklerobject *self) {
  2515.     PyObject *py_float = 0;
  2516.     int s, e;
  2517.     long fhi, flo;
  2518.     double x;
  2519.     char *p;
  2520.  
  2521.     if ((*self->read_func)(self, &p, 8) < 0)
  2522.         return -1;
  2523.  
  2524.     /* First byte */
  2525.     s = (*p>>7) & 1;
  2526.     e = (*p & 0x7F) << 4;
  2527.     p++;
  2528.  
  2529.     /* Second byte */
  2530.     e |= (*p>>4) & 0xF;
  2531.     fhi = (*p & 0xF) << 24;
  2532.     p++;
  2533.  
  2534.     /* Third byte */
  2535.     fhi |= (*p & 0xFF) << 16;
  2536.     p++;
  2537.  
  2538.     /* Fourth byte */
  2539.     fhi |= (*p & 0xFF) << 8;
  2540.     p++;
  2541.  
  2542.     /* Fifth byte */
  2543.     fhi |= *p & 0xFF;
  2544.     p++;
  2545.  
  2546.     /* Sixth byte */
  2547.     flo = (*p & 0xFF) << 16;
  2548.     p++;
  2549.  
  2550.     /* Seventh byte */
  2551.     flo |= (*p & 0xFF) << 8;
  2552.     p++;
  2553.  
  2554.     /* Eighth byte */
  2555.     flo |= *p & 0xFF;
  2556.  
  2557.     x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
  2558.     x /= 268435456.0; /* 2**28 */
  2559.  
  2560.     /* XXX This sadly ignores Inf/NaN */
  2561.     if (e == 0)
  2562.         e = -1022;
  2563.     else {
  2564.         x += 1.0;
  2565.         e -= 1023;
  2566.     }
  2567.     x = ldexp(x, e);
  2568.  
  2569.     if (s)
  2570.         x = -x;
  2571.  
  2572.     UNLESS (py_float = PyFloat_FromDouble(x)) return -1;
  2573.  
  2574.     PDATA_PUSH(self->stack, py_float, -1);
  2575.     return 0;
  2576. }
  2577.  
  2578. static int
  2579. load_string(Unpicklerobject *self) {
  2580.     PyObject *str = 0;
  2581.     int len, res = -1, nslash;
  2582.     char *s, q, *p;
  2583.  
  2584.     static PyObject *eval_dict = 0;
  2585.  
  2586.     if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
  2587.     if (len < 2) return bad_readline();
  2588.     UNLESS (s=pystrndup(s,len)) return -1;
  2589.  
  2590.     /* Check for unquoted quotes (evil strings) */
  2591.     q=*s;
  2592.     if (q != '"' && q != '\'') goto insecure;
  2593.     for (p=s+1, nslash=0; *p; p++) {
  2594.         if (*p==q && nslash%2==0) break;
  2595.         if (*p=='\\') nslash++;
  2596.         else nslash=0;
  2597.       }
  2598.     if (*p==q)
  2599.       {
  2600.         for (p++; *p; p++) if (*p > ' ') goto insecure;
  2601.       }
  2602.     else goto insecure;
  2603.     /********************************************/
  2604.  
  2605.     UNLESS (eval_dict)
  2606.         UNLESS (eval_dict = Py_BuildValue("{s{}}", "__builtins__"))
  2607.             goto finally;
  2608.  
  2609.     UNLESS (str = PyRun_String(s, Py_eval_input, eval_dict, eval_dict))
  2610.         goto finally;
  2611.  
  2612.     free(s);
  2613.     PDATA_PUSH(self->stack, str, -1);
  2614.     return 0;
  2615.  
  2616. finally:
  2617.     free(s);
  2618.  
  2619.     return res;
  2620.     
  2621. insecure:
  2622.     free(s);
  2623.     PyErr_SetString(PyExc_ValueError,"insecure string pickle");
  2624.     return -1;
  2625.  
  2626.  
  2627. static int
  2628. load_binstring(Unpicklerobject *self) {
  2629.     PyObject *py_string = 0;
  2630.     long l;
  2631.     char *s;
  2632.  
  2633.     if ((*self->read_func)(self, &s, 4) < 0) return -1;
  2634.  
  2635.     l = calc_binint(s, 4);
  2636.  
  2637.     if ((*self->read_func)(self, &s, l) < 0)
  2638.         return -1;
  2639.  
  2640.     UNLESS (py_string = PyString_FromStringAndSize(s, l))
  2641.         return -1;
  2642.  
  2643.     PDATA_PUSH(self->stack, py_string, -1);
  2644.     return 0;
  2645. }
  2646.  
  2647.  
  2648. static int
  2649. load_short_binstring(Unpicklerobject *self) {
  2650.     PyObject *py_string = 0;
  2651.     unsigned char l;  
  2652.     char *s;
  2653.  
  2654.     if ((*self->read_func)(self, &s, 1) < 0)
  2655.         return -1;
  2656.  
  2657.     l = (unsigned char)s[0];
  2658.  
  2659.     if ((*self->read_func)(self, &s, l) < 0) return -1;
  2660.  
  2661.     UNLESS (py_string = PyString_FromStringAndSize(s, l)) return -1;
  2662.  
  2663.     PDATA_PUSH(self->stack, py_string, -1);
  2664.     return 0;
  2665.  
  2666.  
  2667. static int
  2668. load_tuple(Unpicklerobject *self) {
  2669.     PyObject *tup;
  2670.     int i;
  2671.  
  2672.     if ((i = marker(self)) < 0) return -1;
  2673.     UNLESS (tup=Pdata_popTuple(self->stack, i)) return -1;
  2674.     PDATA_PUSH(self->stack, tup, -1);
  2675.     return 0;
  2676. }
  2677.  
  2678. static int
  2679. load_empty_tuple(Unpicklerobject *self) {
  2680.     PyObject *tup;
  2681.  
  2682.     UNLESS (tup=PyTuple_New(0)) return -1;
  2683.     PDATA_PUSH(self->stack, tup, -1);
  2684.     return 0;
  2685. }
  2686.  
  2687. static int
  2688. load_empty_list(Unpicklerobject *self) {
  2689.     PyObject *list;
  2690.  
  2691.     UNLESS (list=PyList_New(0)) return -1;
  2692.     PDATA_PUSH(self->stack, list, -1);
  2693.     return 0;
  2694. }
  2695.  
  2696. static int
  2697. load_empty_dict(Unpicklerobject *self) {
  2698.     PyObject *dict;
  2699.  
  2700.     UNLESS (dict=PyDict_New()) return -1;
  2701.     PDATA_PUSH(self->stack, dict, -1);
  2702.     return 0;
  2703. }
  2704.  
  2705.  
  2706. static int
  2707. load_list(Unpicklerobject *self) {
  2708.     PyObject *list = 0;
  2709.     int i;
  2710.  
  2711.     if ((i = marker(self)) < 0) return -1;
  2712.     UNLESS (list=Pdata_popList(self->stack, i)) return -1;
  2713.     PDATA_PUSH(self->stack, list, -1);
  2714.     return 0;
  2715. }
  2716.  
  2717. static int
  2718. load_dict(Unpicklerobject *self) {
  2719.     PyObject *dict, *key, *value;
  2720.     int i, j, k;
  2721.  
  2722.     if ((i = marker(self)) < 0) return -1;
  2723.     j=self->stack->length;
  2724.  
  2725.     UNLESS (dict = PyDict_New()) return -1;
  2726.  
  2727.     for (k = i+1; k < j; k += 2) {
  2728.         key  =self->stack->data[k-1];
  2729.         value=self->stack->data[k  ];
  2730.         if (PyDict_SetItem(dict, key, value) < 0) {
  2731.             Py_DECREF(dict);
  2732.             return -1;
  2733.         }
  2734.     }
  2735.     Pdata_clear(self->stack, i);
  2736.     PDATA_PUSH(self->stack, dict, -1);
  2737.     return 0;
  2738. }
  2739.  
  2740. static PyObject *
  2741. Instance_New(PyObject *cls, PyObject *args) {
  2742.   int has_key;
  2743.   PyObject *safe=0, *r=0;
  2744.  
  2745.   if (PyClass_Check(cls)) {
  2746.       int l;
  2747.       
  2748.       if ((l=PyObject_Length(args)) < 0) goto err;
  2749.       UNLESS (l) {
  2750.           PyObject *__getinitargs__;
  2751.  
  2752.           UNLESS (__getinitargs__=PyObject_GetAttr(cls, __getinitargs___str)) {
  2753.               /* We have a class with no __getinitargs__, so bypass usual
  2754.                  construction  */
  2755.               PyInstanceObject *inst;
  2756.  
  2757.               PyErr_Clear();
  2758.               UNLESS (inst=PyObject_NEW(PyInstanceObject, &PyInstance_Type))
  2759.                 goto err;
  2760.               inst->in_class=(PyClassObject*)cls;
  2761.               Py_INCREF(cls);
  2762.               UNLESS (inst->in_dict=PyDict_New()) {
  2763.                 Py_DECREF(inst);
  2764.                 goto err;
  2765.               }
  2766.  
  2767.               return (PyObject *)inst;
  2768.             }
  2769.           Py_DECREF(__getinitargs__);
  2770.         }
  2771.       
  2772.       if ((r=PyInstance_New(cls, args, NULL))) return r;
  2773.       else goto err;
  2774.     }
  2775.        
  2776.   
  2777.   if ((has_key = cPickle_PyMapping_HasKey(safe_constructors, cls)) < 0)
  2778.     goto err;
  2779.     
  2780.   if (!has_key)
  2781.     if (!(safe = PyObject_GetAttr(cls, __safe_for_unpickling___str)) ||
  2782.        !PyObject_IsTrue(safe)) {
  2783.       cPickle_ErrFormat(UnpicklingError,
  2784.                         "%s is not safe for unpickling", "O", cls);
  2785.       Py_XDECREF(safe);
  2786.       return NULL;
  2787.   }
  2788.  
  2789.   if (args==Py_None) {
  2790.       /* Special case, call cls.__basicnew__() */
  2791.       PyObject *basicnew;
  2792.       
  2793.       UNLESS (basicnew=PyObject_GetAttr(cls, __basicnew___str)) return NULL;
  2794.       r=PyObject_CallObject(basicnew, NULL);
  2795.       Py_DECREF(basicnew);
  2796.       if (r) return r;
  2797.     }
  2798.  
  2799.   if ((r=PyObject_CallObject(cls, args))) return r;
  2800.  
  2801. err:
  2802.   {
  2803.     PyObject *tp, *v, *tb;
  2804.  
  2805.     PyErr_Fetch(&tp, &v, &tb);
  2806.     if ((r=Py_BuildValue("OOO",v,cls,args))) {
  2807.         Py_XDECREF(v);
  2808.         v=r;
  2809.       }
  2810.     PyErr_Restore(tp,v,tb);
  2811.   }
  2812.   return NULL;
  2813. }
  2814.   
  2815.  
  2816. static int
  2817. load_obj(Unpicklerobject *self) {
  2818.     PyObject *class, *tup, *obj=0;
  2819.     int i;
  2820.  
  2821.     if ((i = marker(self)) < 0) return -1;
  2822.     UNLESS (tup=Pdata_popTuple(self->stack, i+1)) return -1;
  2823.     PDATA_POP(self->stack, class);
  2824.     if (class) {
  2825.         obj = Instance_New(class, tup);
  2826.         Py_DECREF(class);
  2827.     }
  2828.     Py_DECREF(tup);
  2829.  
  2830.     if (! obj) return -1;
  2831.     PDATA_PUSH(self->stack, obj, -1);
  2832.     return 0;
  2833. }
  2834.  
  2835.  
  2836. static int
  2837. load_inst(Unpicklerobject *self) {
  2838.     PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
  2839.     int i, len;
  2840.     char *s;
  2841.  
  2842.     if ((i = marker(self)) < 0) return -1;
  2843.  
  2844.     if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
  2845.     if (len < 2) return bad_readline();
  2846.     UNLESS (module_name = PyString_FromStringAndSize(s, len - 1)) return -1;
  2847.  
  2848.     if ((len = (*self->readline_func)(self, &s)) >= 0) {
  2849.         if (len < 2) return bad_readline();
  2850.         if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
  2851.             class = find_class(module_name, class_name, self->find_class);
  2852.             Py_DECREF(class_name);
  2853.         }
  2854.     }
  2855.     Py_DECREF(module_name);
  2856.  
  2857.     if (! class) return -1;
  2858.       
  2859.     if ((tup=Pdata_popTuple(self->stack, i))) {
  2860.         obj = Instance_New(class, tup);
  2861.         Py_DECREF(tup);
  2862.     }
  2863.     Py_DECREF(class);
  2864.  
  2865.     if (! obj) return -1;
  2866.  
  2867.     PDATA_PUSH(self->stack, obj, -1);
  2868.     return 0;
  2869. }
  2870.  
  2871.  
  2872. static int
  2873. load_global(Unpicklerobject *self) {
  2874.     PyObject *class = 0, *module_name = 0, *class_name = 0;
  2875.     int len;
  2876.     char *s;
  2877.  
  2878.     if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
  2879.     if (len < 2) return bad_readline();
  2880.     UNLESS (module_name = PyString_FromStringAndSize(s, len - 1)) return -1;
  2881.  
  2882.     if ((len = (*self->readline_func)(self, &s)) >= 0) {
  2883.         if (len < 2) return bad_readline();
  2884.         if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
  2885.             class = find_class(module_name, class_name, self->find_class);
  2886.             Py_DECREF(class_name);
  2887.         }
  2888.     }
  2889.     Py_DECREF(module_name);
  2890.  
  2891.     if (! class) return -1;
  2892.     PDATA_PUSH(self->stack, class, -1);
  2893.     return 0;
  2894. }
  2895.  
  2896.  
  2897. static int
  2898. load_persid(Unpicklerobject *self) {
  2899.     PyObject *pid = 0;
  2900.     int len;
  2901.     char *s;
  2902.  
  2903.     if (self->pers_func) {
  2904.         if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
  2905.     if (len < 2) return bad_readline();
  2906.   
  2907.         UNLESS (pid = PyString_FromStringAndSize(s, len - 1)) return -1;
  2908.  
  2909.         if (PyList_Check(self->pers_func)) {
  2910.             if (PyList_Append(self->pers_func, pid) < 0) {
  2911.                 Py_DECREF(pid);
  2912.                 return -1;
  2913.             }
  2914.         }
  2915.         else {
  2916.             ARG_TUP(self, pid);
  2917.             if (self->arg) {
  2918.                 pid = PyObject_CallObject(self->pers_func, self->arg);
  2919.                 FREE_ARG_TUP(self);
  2920.             }
  2921.         }
  2922.  
  2923.         if (! pid) return -1;
  2924.  
  2925.         PDATA_PUSH(self->stack, pid, -1);
  2926.         return 0;
  2927.     }
  2928.     else {
  2929.       PyErr_SetString(UnpicklingError,
  2930.                       "A load persistent id instruction was encountered,\n"
  2931.                       "but no persistent_load function was specified.");
  2932.       return -1;
  2933.     }
  2934. }
  2935.  
  2936. static int
  2937. load_binpersid(Unpicklerobject *self) {
  2938.     PyObject *pid = 0;
  2939.  
  2940.     if (self->pers_func) {
  2941.         PDATA_POP(self->stack, pid);
  2942.         if (! pid) return -1;
  2943.  
  2944.         if (PyList_Check(self->pers_func)) {
  2945.             if (PyList_Append(self->pers_func, pid) < 0) {
  2946.                 Py_DECREF(pid);
  2947.                 return -1;
  2948.             }
  2949.           }
  2950.         else {
  2951.             ARG_TUP(self, pid);
  2952.             if (self->arg) {
  2953.                 pid = PyObject_CallObject(self->pers_func, self->arg);
  2954.                 FREE_ARG_TUP(self);
  2955.             }
  2956.             if (! pid) return -1;
  2957.         }
  2958.  
  2959.         PDATA_PUSH(self->stack, pid, -1);
  2960.         return 0;
  2961.     }
  2962.     else {
  2963.       PyErr_SetString(UnpicklingError,
  2964.                       "A load persistent id instruction was encountered,\n"
  2965.                       "but no persistent_load function was specified.");
  2966.       return -1;
  2967.     }
  2968. }
  2969.  
  2970.  
  2971. static int
  2972. load_pop(Unpicklerobject *self) {
  2973.     int len;
  2974.  
  2975.     UNLESS ((len=self->stack->length) > 0) return stackUnderflow();
  2976.  
  2977.     if ((self->num_marks > 0) && 
  2978.         (self->marks[self->num_marks - 1] == len))
  2979.         self->num_marks--;
  2980.     else 
  2981.         Py_DECREF(self->stack->data[--(self->stack->length)]);
  2982.  
  2983.     return 0;
  2984. }
  2985.  
  2986.  
  2987. static int
  2988. load_pop_mark(Unpicklerobject *self) {
  2989.     int i;
  2990.  
  2991.     if ((i = marker(self)) < 0)
  2992.         return -1;
  2993.  
  2994.     Pdata_clear(self->stack, i);
  2995.  
  2996.     return 0;
  2997. }
  2998.  
  2999.  
  3000. static int
  3001. load_dup(Unpicklerobject *self) {
  3002.     PyObject *last;
  3003.     int len;
  3004.  
  3005.     if ((len = self->stack->length) <= 0) return stackUnderflow();
  3006.     last=self->stack->data[len-1];
  3007.     Py_INCREF(last);
  3008.     PDATA_PUSH(self->stack, last, -1);
  3009.     return 0;
  3010. }
  3011.  
  3012.  
  3013. static int
  3014. load_get(Unpicklerobject *self) {
  3015.     PyObject *py_str = 0, *value = 0;
  3016.     int len;
  3017.     char *s;
  3018.     int rc;
  3019.  
  3020.     if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
  3021.     if (len < 2) return bad_readline();
  3022.  
  3023.     UNLESS (py_str = PyString_FromStringAndSize(s, len - 1)) return -1;
  3024.  
  3025.     value = PyDict_GetItem(self->memo, py_str);
  3026.     if (! value) {
  3027.         PyErr_SetObject(BadPickleGet, py_str);
  3028.         rc = -1;
  3029.     } else {
  3030.       PDATA_APPEND(self->stack, value, -1);
  3031.       rc = 0;
  3032.     }
  3033.  
  3034.     Py_DECREF(py_str);
  3035.     return rc;
  3036. }
  3037.  
  3038.  
  3039. static int
  3040. load_binget(Unpicklerobject *self) {
  3041.     PyObject *py_key = 0, *value = 0;
  3042.     unsigned char key;
  3043.     char *s;
  3044.     int rc;
  3045.  
  3046.     if ((*self->read_func)(self, &s, 1) < 0) return -1;
  3047.  
  3048.     key = (unsigned char)s[0];
  3049.     UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
  3050.     
  3051.     value = PyDict_GetItem(self->memo, py_key);
  3052.     if (! value) {
  3053.         PyErr_SetObject(BadPickleGet, py_key);
  3054.         rc = -1;
  3055.     } else {
  3056.       PDATA_APPEND(self->stack, value, -1);
  3057.       rc = 0;
  3058.     }
  3059.  
  3060.     Py_DECREF(py_key);
  3061.     return rc;
  3062. }
  3063.  
  3064.  
  3065. static int
  3066. load_long_binget(Unpicklerobject *self) {
  3067.     PyObject *py_key = 0, *value = 0;
  3068.     unsigned char c, *s;
  3069.     long key;
  3070.     int rc;
  3071.  
  3072.     if ((*self->read_func)(self, &s, 4) < 0) return -1;
  3073.  
  3074.     c = (unsigned char)s[0];
  3075.     key = (long)c;
  3076.     c = (unsigned char)s[1];
  3077.     key |= (long)c << 8;
  3078.     c = (unsigned char)s[2];
  3079.     key |= (long)c << 16;
  3080.     c = (unsigned char)s[3];
  3081.     key |= (long)c << 24;
  3082.  
  3083.     UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
  3084.     
  3085.     value = PyDict_GetItem(self->memo, py_key);
  3086.     if (! value) {
  3087.         PyErr_SetObject(BadPickleGet, py_key);
  3088.         rc = -1;
  3089.     } else {
  3090.       PDATA_APPEND(self->stack, value, -1);
  3091.       rc = 0;
  3092.     }
  3093.  
  3094.     Py_DECREF(py_key);
  3095.     return rc;
  3096. }
  3097.  
  3098.  
  3099. static int
  3100. load_put(Unpicklerobject *self) {
  3101.     PyObject *py_str = 0, *value = 0;
  3102.     int len, l;
  3103.     char *s;
  3104.  
  3105.     if ((l = (*self->readline_func)(self, &s)) < 0) return -1;
  3106.     if (l < 2) return bad_readline();
  3107.     UNLESS (len=self->stack->length) return stackUnderflow();
  3108.     UNLESS (py_str = PyString_FromStringAndSize(s, l - 1)) return -1;
  3109.     value=self->stack->data[len-1];
  3110.     l=PyDict_SetItem(self->memo, py_str, value);
  3111.     Py_DECREF(py_str);
  3112.     return l;
  3113. }
  3114.  
  3115.  
  3116. static int
  3117. load_binput(Unpicklerobject *self) {
  3118.     PyObject *py_key = 0, *value = 0;
  3119.     unsigned char key, *s;
  3120.     int len;
  3121.  
  3122.     if ((*self->read_func)(self, &s, 1) < 0) return -1;
  3123.     UNLESS ((len=self->stack->length) > 0) return stackUnderflow();
  3124.  
  3125.     key = (unsigned char)s[0];
  3126.  
  3127.     UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
  3128.     value=self->stack->data[len-1];
  3129.     len=PyDict_SetItem(self->memo, py_key, value);
  3130.     Py_DECREF(py_key);
  3131.     return len;
  3132. }
  3133.  
  3134.  
  3135. static int
  3136. load_long_binput(Unpicklerobject *self) {
  3137.     PyObject *py_key = 0, *value = 0;
  3138.     long key;
  3139.     unsigned char c, *s;
  3140.     int len;
  3141.  
  3142.     if ((*self->read_func)(self, &s, 4) < 0) return -1;
  3143.     UNLESS (len=self->stack->length) return stackUnderflow();
  3144.  
  3145.     c = (unsigned char)s[0];
  3146.     key = (long)c;
  3147.     c = (unsigned char)s[1];
  3148.     key |= (long)c << 8;
  3149.     c = (unsigned char)s[2];
  3150.     key |= (long)c << 16;
  3151.     c = (unsigned char)s[3];
  3152.     key |= (long)c << 24;
  3153.  
  3154.     UNLESS (py_key = PyInt_FromLong(key)) return -1;
  3155.     value=self->stack->data[len-1];
  3156.     len=PyDict_SetItem(self->memo, py_key, value);
  3157.     Py_DECREF(py_key);
  3158.     return len;
  3159. }
  3160.  
  3161.  
  3162. static int 
  3163. do_append(Unpicklerobject *self, int  x) {
  3164.     PyObject *value = 0, *list = 0, *append_method = 0;
  3165.     int len, i;
  3166.  
  3167.     UNLESS ((len=self->stack->length) >= x && x > 0) return stackUnderflow();
  3168.     if (len==x) return 0;       /* nothing to do */
  3169.  
  3170.     list=self->stack->data[x-1];
  3171.  
  3172.     if (PyList_Check(list)) {
  3173.         PyObject *slice;
  3174.         int list_len;
  3175.        
  3176.         slice=Pdata_popList(self->stack, x);
  3177.         list_len = PyList_GET_SIZE(list);
  3178.         i=PyList_SetSlice(list, list_len, list_len, slice);
  3179.         Py_DECREF(slice);
  3180.         return i;
  3181.     }
  3182.     else {
  3183.  
  3184.         UNLESS (append_method = PyObject_GetAttr(list, append_str))
  3185.             return -1;
  3186.          
  3187.         for (i = x; i < len; i++) {
  3188.             PyObject *junk;
  3189.  
  3190.             value=self->stack->data[i];
  3191.             junk=0;
  3192.             ARG_TUP(self, value);
  3193.             if (self->arg) {
  3194.               junk = PyObject_CallObject(append_method, self->arg);
  3195.               FREE_ARG_TUP(self);
  3196.             }
  3197.             if (! junk) {
  3198.                 Pdata_clear(self->stack, i+1);
  3199.                 self->stack->length=x;
  3200.                 Py_DECREF(append_method);
  3201.                 return -1;
  3202.             }
  3203.             Py_DECREF(junk);
  3204.         }
  3205.         self->stack->length=x;
  3206.         Py_DECREF(append_method);
  3207.     }
  3208.  
  3209.     return 0;
  3210. }
  3211.  
  3212.     
  3213. static int
  3214. load_append(Unpicklerobject *self) {
  3215.     return do_append(self, self->stack->length - 1);
  3216. }
  3217.  
  3218.  
  3219. static int
  3220. load_appends(Unpicklerobject *self) {
  3221.     return do_append(self, marker(self));
  3222. }
  3223.  
  3224.  
  3225. static int
  3226. do_setitems(Unpicklerobject *self, int  x) {
  3227.     PyObject *value = 0, *key = 0, *dict = 0;
  3228.     int len, i, r=0;
  3229.  
  3230.     UNLESS ((len=self->stack->length) >= x
  3231.             && x > 0) return stackUnderflow();
  3232.  
  3233.     dict=self->stack->data[x-1];
  3234.  
  3235.     for (i = x+1; i < len; i += 2) {
  3236.         key  =self->stack->data[i-1];
  3237.         value=self->stack->data[i  ];
  3238.         if (PyObject_SetItem(dict, key, value) < 0) {
  3239.             r=-1;
  3240.             break;
  3241.         }
  3242.     }
  3243.  
  3244.     Pdata_clear(self->stack, x);
  3245.  
  3246.     return r;
  3247. }
  3248.  
  3249.  
  3250. static int
  3251. load_setitem(Unpicklerobject *self) {
  3252.     return do_setitems(self, self->stack->length - 2);
  3253. }
  3254.  
  3255. static int
  3256. load_setitems(Unpicklerobject *self) {
  3257.     return do_setitems(self, marker(self));
  3258. }
  3259.  
  3260.  
  3261. static int
  3262. load_build(Unpicklerobject *self) {
  3263.     PyObject *value = 0, *inst = 0, *instdict = 0, *d_key = 0, *d_value = 0, 
  3264.              *junk = 0, *__setstate__ = 0;
  3265.     int i, r = 0;
  3266.  
  3267.     if (self->stack->length < 2) return stackUnderflow();
  3268.     PDATA_POP(self->stack, value);
  3269.     if (! value) return -1;
  3270.     inst=self->stack->data[self->stack->length-1];
  3271.  
  3272.     if ((__setstate__ = PyObject_GetAttr(inst, __setstate___str))) {
  3273.         ARG_TUP(self, value);
  3274.         if (self->arg) {
  3275.             junk = PyObject_CallObject(__setstate__, self->arg);
  3276.             FREE_ARG_TUP(self);
  3277.         }
  3278.         Py_DECREF(__setstate__);
  3279.         if (! junk) return -1;
  3280.         Py_DECREF(junk);
  3281.         return 0;
  3282.     }
  3283.  
  3284.     PyErr_Clear();
  3285.     if ((instdict = PyObject_GetAttr(inst, __dict___str))) {
  3286.         i = 0;
  3287.         while (PyDict_Next(value, &i, &d_key, &d_value)) {
  3288.             if (PyObject_SetItem(instdict, d_key, d_value) < 0) {
  3289.                 r=-1;
  3290.                 break;
  3291.             }
  3292.         }
  3293.         Py_DECREF(instdict);
  3294.     }
  3295.     else r=-1;
  3296.  
  3297.     Py_XDECREF(value);
  3298.   
  3299.     return r;
  3300. }
  3301.  
  3302.  
  3303. static int
  3304. load_mark(Unpicklerobject *self) {
  3305.     int s;
  3306.  
  3307.     if ((self->num_marks + 1) >= self->marks_size) {
  3308.         s=self->marks_size+20;
  3309.         if (s <= self->num_marks) s=self->num_marks + 1;
  3310.         if (self->marks == NULL)
  3311.             self->marks=(int *)malloc(s * sizeof(int));
  3312.         else
  3313.             self->marks=(int *)realloc(self->marks, s * sizeof(int));
  3314.         if (! self->marks) {
  3315.             PyErr_NoMemory();
  3316.             return -1;
  3317.         }
  3318.         self->marks_size = s;
  3319.     }
  3320.  
  3321.     self->marks[self->num_marks++] = self->stack->length;
  3322.  
  3323.     return 0;
  3324. }
  3325.  
  3326. static int
  3327. load_reduce(Unpicklerobject *self) {
  3328.     PyObject *callable = 0, *arg_tup = 0, *ob = 0;
  3329.  
  3330.     PDATA_POP(self->stack, arg_tup);
  3331.     if (! arg_tup) return -1;
  3332.     PDATA_POP(self->stack, callable);
  3333.     if (callable) {
  3334.         ob = Instance_New(callable, arg_tup);
  3335.         Py_DECREF(callable);
  3336.     }
  3337.     Py_DECREF(arg_tup);
  3338.  
  3339.     if (! ob) return -1;
  3340.    
  3341.     PDATA_PUSH(self->stack, ob, -1);
  3342.     return 0;
  3343. }
  3344.     
  3345. static PyObject *
  3346. load(Unpicklerobject *self) {
  3347.     PyObject *err = 0, *val = 0;
  3348.     char *s;
  3349.  
  3350.     self->num_marks = 0;
  3351.     if (self->stack->length) Pdata_clear(self->stack, 0);
  3352.  
  3353.     while (1) {
  3354.         if ((*self->read_func)(self, &s, 1) < 0)
  3355.             break;
  3356.  
  3357.         switch (s[0]) {
  3358.             case NONE:
  3359.                 if (load_none(self) < 0)
  3360.                     break;
  3361.                 continue;
  3362.  
  3363.             case BININT:
  3364.                  if (load_binint(self) < 0)
  3365.                      break;
  3366.                  continue;
  3367.  
  3368.             case BININT1:
  3369.                 if (load_binint1(self) < 0)
  3370.                     break;
  3371.                 continue;
  3372.  
  3373.             case BININT2:
  3374.                 if (load_binint2(self) < 0)
  3375.                     break;
  3376.                 continue;
  3377.  
  3378.             case INT:
  3379.                 if (load_int(self) < 0)
  3380.                     break;
  3381.                 continue;
  3382.  
  3383.             case LONG:
  3384.                 if (load_long(self) < 0)
  3385.                     break;
  3386.                 continue;
  3387.  
  3388.             case FLOAT:
  3389.                 if (load_float(self) < 0)
  3390.                     break;
  3391.                 continue;
  3392.  
  3393.             case BINFLOAT:
  3394.                 if (load_binfloat(self) < 0)
  3395.                     break;
  3396.                 continue;
  3397.  
  3398.             case BINSTRING:
  3399.                 if (load_binstring(self) < 0)
  3400.                     break;
  3401.                 continue;
  3402.  
  3403.             case SHORT_BINSTRING:
  3404.                 if (load_short_binstring(self) < 0)
  3405.                     break;
  3406.                 continue;
  3407.  
  3408.             case STRING:
  3409.                 if (load_string(self) < 0)
  3410.                     break;
  3411.                 continue;
  3412.  
  3413.             case EMPTY_TUPLE:
  3414.                 if (load_empty_tuple(self) < 0)
  3415.                     break;
  3416.                 continue;
  3417.  
  3418.             case TUPLE:
  3419.                 if (load_tuple(self) < 0)
  3420.                     break;
  3421.                 continue;
  3422.  
  3423.             case EMPTY_LIST:
  3424.                 if (load_empty_list(self) < 0)
  3425.                     break;
  3426.                 continue;
  3427.  
  3428.             case LIST:
  3429.                 if (load_list(self) < 0)
  3430.                     break;
  3431.                 continue;
  3432.  
  3433.             case EMPTY_DICT:
  3434.                 if (load_empty_dict(self) < 0)
  3435.                     break;
  3436.                 continue;
  3437.  
  3438.             case DICT:
  3439.                 if (load_dict(self) < 0)
  3440.                     break;
  3441.                 continue;
  3442.  
  3443.             case OBJ:
  3444.                 if (load_obj(self) < 0)
  3445.                     break;
  3446.                 continue;
  3447.  
  3448.             case INST:
  3449.                 if (load_inst(self) < 0)
  3450.                     break;
  3451.                 continue;
  3452.  
  3453.             case GLOBAL:
  3454.                 if (load_global(self) < 0)
  3455.                     break;
  3456.                 continue;
  3457.  
  3458.             case APPEND:
  3459.                 if (load_append(self) < 0)
  3460.                     break;
  3461.                 continue;
  3462.  
  3463.             case APPENDS:
  3464.                 if (load_appends(self) < 0)
  3465.                     break;
  3466.                 continue;
  3467.    
  3468.             case BUILD:
  3469.                 if (load_build(self) < 0)
  3470.                     break;
  3471.                 continue;
  3472.   
  3473.             case DUP:
  3474.                 if (load_dup(self) < 0)
  3475.                     break;
  3476.                 continue;
  3477.  
  3478.             case BINGET:
  3479.                 if (load_binget(self) < 0)
  3480.                     break;
  3481.                 continue;
  3482.  
  3483.             case LONG_BINGET:
  3484.                 if (load_long_binget(self) < 0)
  3485.                     break;
  3486.                 continue;
  3487.          
  3488.             case GET:
  3489.                 if (load_get(self) < 0)
  3490.                     break;
  3491.                 continue;
  3492.  
  3493.             case MARK:
  3494.                 if (load_mark(self) < 0)
  3495.                     break;
  3496.                 continue;
  3497.  
  3498.             case BINPUT:
  3499.                 if (load_binput(self) < 0)
  3500.                     break;
  3501.                 continue;
  3502.  
  3503.             case LONG_BINPUT:
  3504.                 if (load_long_binput(self) < 0)
  3505.                     break;
  3506.                 continue;
  3507.          
  3508.             case PUT:
  3509.                 if (load_put(self) < 0)
  3510.                     break;
  3511.                 continue;
  3512.  
  3513.             case POP:
  3514.                 if (load_pop(self) < 0)
  3515.                     break;
  3516.                 continue;
  3517.  
  3518.             case POP_MARK:
  3519.                 if (load_pop_mark(self) < 0)
  3520.                     break;
  3521.                 continue;
  3522.  
  3523.             case SETITEM:
  3524.                 if (load_setitem(self) < 0)
  3525.                     break;
  3526.                 continue;
  3527.  
  3528.             case SETITEMS:
  3529.                 if (load_setitems(self) < 0)
  3530.                     break;
  3531.                 continue;
  3532.  
  3533.             case STOP:
  3534.                 break;
  3535.  
  3536.             case PERSID:
  3537.                 if (load_persid(self) < 0)
  3538.                     break;
  3539.                 continue;
  3540.  
  3541.             case BINPERSID:
  3542.                 if (load_binpersid(self) < 0)
  3543.                     break;
  3544.                 continue;
  3545.  
  3546.             case REDUCE:
  3547.                 if (load_reduce(self) < 0)
  3548.                     break;
  3549.                 continue;
  3550.  
  3551.             default: 
  3552.                 cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.", 
  3553.                     "c", s[0]);
  3554.                 return NULL;
  3555.         }
  3556.  
  3557.         break;
  3558.     }
  3559.  
  3560.     if ((err = PyErr_Occurred())) {
  3561.         if (err == PyExc_EOFError) {
  3562.             PyErr_SetNone(PyExc_EOFError);
  3563.         }    
  3564.         return NULL;
  3565.     }
  3566.  
  3567.     PDATA_POP(self->stack, val);    
  3568.     return val;
  3569. }
  3570.     
  3571.  
  3572. /* No-load functions to support noload, which is used to
  3573.    find persistent references. */
  3574.  
  3575. static int
  3576. noload_obj(Unpicklerobject *self) {
  3577.     int i;
  3578.  
  3579.     if ((i = marker(self)) < 0) return -1;
  3580.     return Pdata_clear(self->stack, i+1);
  3581. }
  3582.  
  3583.  
  3584. static int
  3585. noload_inst(Unpicklerobject *self) {
  3586.     int i;
  3587.     char *s;
  3588.  
  3589.     if ((i = marker(self)) < 0) return -1;
  3590.     Pdata_clear(self->stack, i);
  3591.     if ((*self->readline_func)(self, &s) < 0) return -1;
  3592.     if ((*self->readline_func)(self, &s) < 0) return -1;
  3593.     PDATA_APPEND(self->stack, Py_None,-1);
  3594.     return 0;
  3595. }
  3596.  
  3597. static int
  3598. noload_global(Unpicklerobject *self) {
  3599.     char *s;
  3600.  
  3601.     if ((*self->readline_func)(self, &s) < 0) return -1;
  3602.     if ((*self->readline_func)(self, &s) < 0) return -1;
  3603.     PDATA_APPEND(self->stack, Py_None,-1);
  3604.     return 0;
  3605. }
  3606.  
  3607. static int
  3608. noload_reduce(Unpicklerobject *self) {
  3609.  
  3610.     if (self->stack->length < 2) return stackUnderflow();
  3611.     Pdata_clear(self->stack, self->stack->length-2);
  3612.     PDATA_APPEND(self->stack, Py_None,-1);
  3613.     return 0;
  3614. }
  3615.  
  3616. static int
  3617. noload_build(Unpicklerobject *self) {
  3618.  
  3619.   if (self->stack->length < 1) return stackUnderflow();
  3620.   Pdata_clear(self->stack, self->stack->length-1);
  3621.   return 0;
  3622. }
  3623.  
  3624.  
  3625. static PyObject *
  3626. noload(Unpicklerobject *self) {
  3627.     PyObject *err = 0, *val = 0;
  3628.     char *s;
  3629.  
  3630.     self->num_marks = 0;
  3631.     Pdata_clear(self->stack, 0);
  3632.  
  3633.     while (1) {
  3634.         if ((*self->read_func)(self, &s, 1) < 0)
  3635.             break;
  3636.  
  3637.         switch (s[0]) {
  3638.             case NONE:
  3639.                 if (load_none(self) < 0)
  3640.                     break;
  3641.                 continue;
  3642.  
  3643.             case BININT:
  3644.                  if (load_binint(self) < 0)
  3645.                      break;
  3646.                  continue;
  3647.  
  3648.             case BININT1:
  3649.                 if (load_binint1(self) < 0)
  3650.                     break;
  3651.                 continue;
  3652.  
  3653.             case BININT2:
  3654.                 if (load_binint2(self) < 0)
  3655.                     break;
  3656.                 continue;
  3657.  
  3658.             case INT:
  3659.                 if (load_int(self) < 0)
  3660.                     break;
  3661.                 continue;
  3662.  
  3663.             case LONG:
  3664.                 if (load_long(self) < 0)
  3665.                     break;
  3666.                 continue;
  3667.  
  3668.             case FLOAT:
  3669.                 if (load_float(self) < 0)
  3670.                     break;
  3671.                 continue;
  3672.  
  3673.             case BINFLOAT:
  3674.                 if (load_binfloat(self) < 0)
  3675.                     break;
  3676.                 continue;
  3677.  
  3678.             case BINSTRING:
  3679.                 if (load_binstring(self) < 0)
  3680.                     break;
  3681.                 continue;
  3682.  
  3683.             case SHORT_BINSTRING:
  3684.                 if (load_short_binstring(self) < 0)
  3685.                     break;
  3686.                 continue;
  3687.  
  3688.             case STRING:
  3689.                 if (load_string(self) < 0)
  3690.                     break;
  3691.                 continue;
  3692.  
  3693.             case EMPTY_TUPLE:
  3694.                 if (load_empty_tuple(self) < 0)
  3695.                     break;
  3696.                 continue;
  3697.  
  3698.             case TUPLE:
  3699.                 if (load_tuple(self) < 0)
  3700.                     break;
  3701.                 continue;
  3702.  
  3703.             case EMPTY_LIST:
  3704.                 if (load_empty_list(self) < 0)
  3705.                     break;
  3706.                 continue;
  3707.  
  3708.             case LIST:
  3709.                 if (load_list(self) < 0)
  3710.                     break;
  3711.                 continue;
  3712.  
  3713.             case EMPTY_DICT:
  3714.                 if (load_empty_dict(self) < 0)
  3715.                     break;
  3716.                 continue;
  3717.  
  3718.             case DICT:
  3719.                 if (load_dict(self) < 0)
  3720.                     break;
  3721.                 continue;
  3722.  
  3723.             case OBJ:
  3724.                 if (noload_obj(self) < 0)
  3725.                     break;
  3726.                 continue;
  3727.  
  3728.             case INST:
  3729.                 if (noload_inst(self) < 0)
  3730.                     break;
  3731.                 continue;
  3732.  
  3733.             case GLOBAL:
  3734.                 if (noload_global(self) < 0)
  3735.                     break;
  3736.                 continue;
  3737.  
  3738.             case APPEND:
  3739.                 if (load_append(self) < 0)
  3740.                     break;
  3741.                 continue;
  3742.  
  3743.             case APPENDS:
  3744.                 if (load_appends(self) < 0)
  3745.                     break;
  3746.                 continue;
  3747.    
  3748.             case BUILD:
  3749.                 if (noload_build(self) < 0)
  3750.                     break;
  3751.                 continue;
  3752.   
  3753.             case DUP:
  3754.                 if (load_dup(self) < 0)
  3755.                     break;
  3756.                 continue;
  3757.  
  3758.             case BINGET:
  3759.                 if (load_binget(self) < 0)
  3760.                     break;
  3761.                 continue;
  3762.  
  3763.             case LONG_BINGET:
  3764.                 if (load_long_binget(self) < 0)
  3765.                     break;
  3766.                 continue;
  3767.          
  3768.             case GET:
  3769.                 if (load_get(self) < 0)
  3770.                     break;
  3771.                 continue;
  3772.  
  3773.             case MARK:
  3774.                 if (load_mark(self) < 0)
  3775.                     break;
  3776.                 continue;
  3777.  
  3778.             case BINPUT:
  3779.                 if (load_binput(self) < 0)
  3780.                     break;
  3781.                 continue;
  3782.  
  3783.             case LONG_BINPUT:
  3784.                 if (load_long_binput(self) < 0)
  3785.                     break;
  3786.                 continue;
  3787.          
  3788.             case PUT:
  3789.                 if (load_put(self) < 0)
  3790.                     break;
  3791.                 continue;
  3792.  
  3793.             case POP:
  3794.                 if (load_pop(self) < 0)
  3795.                     break;
  3796.                 continue;
  3797.  
  3798.             case POP_MARK:
  3799.                 if (load_pop_mark(self) < 0)
  3800.                     break;
  3801.                 continue;
  3802.  
  3803.             case SETITEM:
  3804.                 if (load_setitem(self) < 0)
  3805.                     break;
  3806.                 continue;
  3807.  
  3808.             case SETITEMS:
  3809.                 if (load_setitems(self) < 0)
  3810.                     break;
  3811.                 continue;
  3812.  
  3813.             case STOP:
  3814.                 break;
  3815.  
  3816.             case PERSID:
  3817.                 if (load_persid(self) < 0)
  3818.                     break;
  3819.                 continue;
  3820.  
  3821.             case BINPERSID:
  3822.                 if (load_binpersid(self) < 0)
  3823.                     break;
  3824.                 continue;
  3825.  
  3826.             case REDUCE:
  3827.                 if (noload_reduce(self) < 0)
  3828.                     break;
  3829.                 continue;
  3830.  
  3831.             default: 
  3832.                 cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.", 
  3833.                     "c", s[0]);
  3834.                 return NULL;
  3835.         }
  3836.  
  3837.         break;
  3838.     }
  3839.  
  3840.     if ((err = PyErr_Occurred())) {
  3841.         if (err == PyExc_EOFError) {
  3842.             PyErr_SetNone(PyExc_EOFError);
  3843.         }    
  3844.         return NULL;
  3845.     }
  3846.  
  3847.     PDATA_POP(self->stack, val);    
  3848.     return val;
  3849. }
  3850.     
  3851.  
  3852. static PyObject *
  3853. Unpickler_load(Unpicklerobject *self, PyObject *args) {
  3854.     UNLESS (PyArg_ParseTuple(args, ":load")) 
  3855.         return NULL;
  3856.  
  3857.     return load(self);
  3858. }
  3859.  
  3860. static PyObject *
  3861. Unpickler_noload(Unpicklerobject *self, PyObject *args) {
  3862.     UNLESS (PyArg_ParseTuple(args, ":noload")) 
  3863.         return NULL;
  3864.  
  3865.     return noload(self);
  3866. }
  3867.  
  3868.  
  3869. static struct PyMethodDef Unpickler_methods[] = {
  3870.   {"load",         (PyCFunction)Unpickler_load,   1,
  3871.    "load() -- Load a pickle"
  3872.   },
  3873.   {"noload",         (PyCFunction)Unpickler_noload,   1,
  3874.    "noload() -- not load a pickle, but go through most of the motions\n"
  3875.    "\n"
  3876.    "This function can be used to read past a pickle without instantiating\n"
  3877.    "any objects or importing any modules.  It can also be used to find all\n"
  3878.    "persistent references without instantiating any objects or importing\n"
  3879.    "any modules.\n"
  3880.   },
  3881.   {NULL,              NULL}           /* sentinel */
  3882. };
  3883.  
  3884.  
  3885. static Unpicklerobject *
  3886. newUnpicklerobject(PyObject *f) {
  3887.     Unpicklerobject *self;
  3888.  
  3889.     UNLESS (self = PyObject_NEW(Unpicklerobject, &Unpicklertype))
  3890.         return NULL;
  3891.  
  3892.     self->file = NULL;
  3893.     self->arg = NULL;
  3894.     self->stack = (Pdata*)Pdata_New();
  3895.     self->pers_func = NULL;
  3896.     self->last_string = NULL;
  3897.     self->marks = NULL;
  3898.     self->num_marks = 0;
  3899.     self->marks_size = 0;
  3900.     self->buf_size = 0;
  3901.     self->read = NULL;
  3902.     self->readline = NULL;
  3903.     self->safe_constructors = NULL;
  3904.     self->find_class = NULL;
  3905.  
  3906.     UNLESS (self->memo = PyDict_New()) {
  3907.        Py_XDECREF((PyObject *)self);
  3908.        return NULL;
  3909.     }
  3910.  
  3911.     Py_INCREF(f);
  3912.     self->file = f;
  3913.  
  3914.     /* Set read, readline based on type of f */
  3915.     if (PyFile_Check(f)) {
  3916.         self->fp = PyFile_AsFile(f);
  3917.     if (self->fp == NULL) {
  3918.         PyErr_SetString(PyExc_IOError, "input file closed");
  3919.         return NULL;
  3920.     }
  3921.         self->read_func = read_file;
  3922.         self->readline_func = readline_file;
  3923.     }
  3924.     else if (PycStringIO_InputCheck(f)) {
  3925.         self->fp = NULL;
  3926.         self->read_func = read_cStringIO;
  3927.         self->readline_func = readline_cStringIO;
  3928.     }
  3929.     else {
  3930.  
  3931.         self->fp = NULL;
  3932.         self->read_func = read_other;
  3933.         self->readline_func = readline_other;
  3934.  
  3935.         UNLESS ((self->readline = PyObject_GetAttr(f, readline_str)) &&
  3936.             (self->read = PyObject_GetAttr(f, read_str))) {
  3937.             PyErr_Clear();
  3938.             PyErr_SetString( PyExc_TypeError, "argument must have 'read' and "
  3939.                 "'readline' attributes" );
  3940.             goto err;
  3941.         }
  3942.     }
  3943.  
  3944.     if (PyEval_GetRestricted()) {
  3945.         /* Restricted execution, get private tables */
  3946.         PyObject *m;
  3947.  
  3948.         UNLESS (m=PyImport_Import(copy_reg_str)) goto err;
  3949.         self->safe_constructors=PyObject_GetAttr(m, safe_constructors_str);
  3950.         Py_DECREF(m);
  3951.         UNLESS (self->safe_constructors) goto err;
  3952.     }
  3953.     else {
  3954.         self->safe_constructors=safe_constructors;
  3955.         Py_INCREF(safe_constructors);
  3956.     }
  3957.  
  3958.     return self;
  3959.  
  3960. err:
  3961.     Py_DECREF((PyObject *)self);
  3962.     return NULL;
  3963. }
  3964.  
  3965.  
  3966. static PyObject *
  3967. get_Unpickler(PyObject *self, PyObject *args) {
  3968.     PyObject *file;
  3969.   
  3970.     UNLESS (PyArg_ParseTuple(args, "O:Unpickler", &file))
  3971.         return NULL;
  3972.     return (PyObject *)newUnpicklerobject(file);
  3973. }
  3974.  
  3975.  
  3976. static void
  3977. Unpickler_dealloc(Unpicklerobject *self) {
  3978.     Py_XDECREF(self->readline);
  3979.     Py_XDECREF(self->read);
  3980.     Py_XDECREF(self->file);
  3981.     Py_XDECREF(self->memo);
  3982.     Py_XDECREF(self->stack);
  3983.     Py_XDECREF(self->pers_func);
  3984.     Py_XDECREF(self->arg);
  3985.     Py_XDECREF(self->last_string);
  3986.     Py_XDECREF(self->safe_constructors);
  3987.  
  3988.     if (self->marks) {
  3989.         free(self->marks);
  3990.     }
  3991.  
  3992.     if (self->buf_size) {
  3993.         free(self->buf);
  3994.     }
  3995.     
  3996.     PyMem_DEL(self);
  3997. }
  3998.  
  3999.  
  4000. static PyObject *
  4001. Unpickler_getattr(Unpicklerobject *self, char *name) {
  4002.     if (!strcmp(name, "persistent_load")) {
  4003.         if (!self->pers_func) {
  4004.             PyErr_SetString(PyExc_AttributeError, name);
  4005.             return NULL;
  4006.         }
  4007.  
  4008.         Py_INCREF(self->pers_func);
  4009.         return self->pers_func;
  4010.     }
  4011.  
  4012.     if (!strcmp(name, "find_global")) {
  4013.         if (!self->find_class) {
  4014.             PyErr_SetString(PyExc_AttributeError, name);
  4015.             return NULL;
  4016.         }
  4017.  
  4018.         Py_INCREF(self->find_class);
  4019.         return self->find_class;
  4020.     }
  4021.  
  4022.     if (!strcmp(name, "memo")) {
  4023.         if (!self->memo) {
  4024.             PyErr_SetString(PyExc_AttributeError, name);
  4025.             return NULL;
  4026.         }
  4027.  
  4028.         Py_INCREF(self->memo);
  4029.         return self->memo;
  4030.     }
  4031.  
  4032.     if (!strcmp(name, "UnpicklingError")) {
  4033.         Py_INCREF(UnpicklingError);
  4034.         return UnpicklingError;
  4035.     }
  4036.  
  4037.     return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
  4038. }
  4039.  
  4040.  
  4041. static int
  4042. Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value) {
  4043.  
  4044.     if (!strcmp(name, "persistent_load")) {
  4045.         Py_XDECREF(self->pers_func);
  4046.         self->pers_func = value;
  4047.         Py_XINCREF(value);
  4048.         return 0;
  4049.     }
  4050.  
  4051.     if (!strcmp(name, "find_global")) {
  4052.         Py_XDECREF(self->find_class);
  4053.         self->find_class = value;
  4054.         Py_XINCREF(value);
  4055.         return 0;
  4056.     }
  4057.  
  4058.     if (! value) {
  4059.         PyErr_SetString(PyExc_TypeError,
  4060.                         "attribute deletion is not supported");
  4061.         return -1;
  4062.     }
  4063.  
  4064.     if (strcmp(name, "memo") == 0) {
  4065.         if (! PyDict_Check(value)) {
  4066.           PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
  4067.           return -1;
  4068.         }
  4069.         Py_XDECREF(self->memo);
  4070.         self->memo = value;
  4071.         Py_INCREF(value);
  4072.         return 0;
  4073.     }
  4074.  
  4075.     PyErr_SetString(PyExc_AttributeError, name);
  4076.     return -1;
  4077. }
  4078.  
  4079.  
  4080. static PyObject *
  4081. cpm_dump(PyObject *self, PyObject *args) {
  4082.     PyObject *ob, *file, *res = NULL;
  4083.     Picklerobject *pickler = 0;
  4084.     int bin = 0;
  4085.  
  4086.     UNLESS (PyArg_ParseTuple(args, "OO|i", &ob, &file, &bin))
  4087.         goto finally;
  4088.  
  4089.     UNLESS (pickler = newPicklerobject(file, bin))
  4090.         goto finally;
  4091.  
  4092.     if (dump(pickler, ob) < 0)
  4093.         goto finally;
  4094.  
  4095.     Py_INCREF(Py_None);
  4096.     res = Py_None;
  4097.  
  4098. finally:
  4099.     Py_XDECREF(pickler);
  4100.  
  4101.     return res;
  4102. }
  4103.  
  4104.  
  4105. static PyObject *
  4106. cpm_dumps(PyObject *self, PyObject *args) {
  4107.     PyObject *ob, *file = 0, *res = NULL;
  4108.     Picklerobject *pickler = 0;
  4109.     int bin = 0;
  4110.  
  4111.     UNLESS (PyArg_ParseTuple(args, "O|i:dumps", &ob, &bin))
  4112.         goto finally;
  4113.  
  4114.     UNLESS (file = PycStringIO->NewOutput(128))
  4115.         goto finally;
  4116.  
  4117.     UNLESS (pickler = newPicklerobject(file, bin))
  4118.         goto finally;
  4119.  
  4120.     if (dump(pickler, ob) < 0)
  4121.         goto finally;
  4122.  
  4123.     res = PycStringIO->cgetvalue(file);
  4124.  
  4125. finally:
  4126.     Py_XDECREF(pickler);
  4127.     Py_XDECREF(file);
  4128.  
  4129.     return res;
  4130. }  
  4131.   
  4132.  
  4133. static PyObject *
  4134. cpm_load(PyObject *self, PyObject *args) {
  4135.     Unpicklerobject *unpickler = 0;
  4136.     PyObject *ob, *res = NULL;
  4137.  
  4138.     UNLESS (PyArg_ParseTuple(args, "O:load", &ob))
  4139.         goto finally;
  4140.  
  4141.     UNLESS (unpickler = newUnpicklerobject(ob))
  4142.         goto finally;
  4143.  
  4144.     res = load(unpickler);
  4145.  
  4146. finally:
  4147.     Py_XDECREF(unpickler);
  4148.  
  4149.     return res;
  4150. }
  4151.  
  4152.  
  4153. static PyObject *
  4154. cpm_loads(PyObject *self, PyObject *args) {
  4155.     PyObject *ob, *file = 0, *res = NULL;
  4156.     Unpicklerobject *unpickler = 0;
  4157.  
  4158.     UNLESS (PyArg_ParseTuple(args, "S:loads", &ob))
  4159.         goto finally;
  4160.  
  4161.     UNLESS (file = PycStringIO->NewInput(ob))
  4162.         goto finally;
  4163.   
  4164.     UNLESS (unpickler = newUnpicklerobject(file))
  4165.         goto finally;
  4166.  
  4167.     res = load(unpickler);
  4168.  
  4169. finally:
  4170.     Py_XDECREF(file);
  4171.     Py_XDECREF(unpickler);
  4172.  
  4173.     return res;
  4174. }
  4175.  
  4176.  
  4177. static char Unpicklertype__doc__[] = 
  4178. "Objects that know how to unpickle";
  4179.  
  4180. static PyTypeObject Unpicklertype = {
  4181.     PyObject_HEAD_INIT(NULL)
  4182.     0,                            /*ob_size*/
  4183.     "Unpickler",                  /*tp_name*/
  4184.     sizeof(Unpicklerobject),              /*tp_basicsize*/
  4185.     0,                            /*tp_itemsize*/
  4186.     /* methods */
  4187.     (destructor)Unpickler_dealloc,        /*tp_dealloc*/
  4188.     (printfunc)0,         /*tp_print*/
  4189.     (getattrfunc)Unpickler_getattr,       /*tp_getattr*/
  4190.     (setattrfunc)Unpickler_setattr,       /*tp_setattr*/
  4191.     (cmpfunc)0,           /*tp_compare*/
  4192.     (reprfunc)0,          /*tp_repr*/
  4193.     0,                    /*tp_as_number*/
  4194.     0,            /*tp_as_sequence*/
  4195.     0,            /*tp_as_mapping*/
  4196.     (hashfunc)0,          /*tp_hash*/
  4197.     (ternaryfunc)0,               /*tp_call*/
  4198.     (reprfunc)0,          /*tp_str*/
  4199.  
  4200.     /* Space for future expansion */
  4201.     0L,0L,0L,0L,
  4202.     Unpicklertype__doc__ /* Documentation string */
  4203. };
  4204.  
  4205. static struct PyMethodDef cPickle_methods[] = {
  4206.   {"dump",         (PyCFunction)cpm_dump,         1,
  4207.    "dump(object, file, [binary]) --"
  4208.    "Write an object in pickle format to the given file\n"
  4209.    "\n"
  4210.    "If the optional argument, binary, is provided and is true, then the\n"
  4211.    "pickle will be written in binary format, which is more space and\n"
  4212.    "computationally efficient. \n"
  4213.   },
  4214.   {"dumps",        (PyCFunction)cpm_dumps,        1,
  4215.    "dumps(object, [binary]) --"
  4216.    "Return a string containing an object in pickle format\n"
  4217.    "\n"
  4218.    "If the optional argument, binary, is provided and is true, then the\n"
  4219.    "pickle will be written in binary format, which is more space and\n"
  4220.    "computationally efficient. \n"
  4221.   },
  4222.   {"load",         (PyCFunction)cpm_load,         1,
  4223.    "load(file) -- Load a pickle from the given file"},
  4224.   {"loads",        (PyCFunction)cpm_loads,        1,
  4225.    "loads(string) -- Load a pickle from the given string"},
  4226.   {"Pickler",      (PyCFunction)get_Pickler,      1,
  4227.    "Pickler(file, [binary]) -- Create a pickler\n"
  4228.    "\n"
  4229.    "If the optional argument, binary, is provided and is true, then\n"
  4230.    "pickles will be written in binary format, which is more space and\n"
  4231.    "computationally efficient. \n"
  4232.   },
  4233.   {"Unpickler",    (PyCFunction)get_Unpickler,    1,
  4234.    "Unpickler(file) -- Create an unpickler"},
  4235.   { NULL, NULL }
  4236. };
  4237.  
  4238. static int
  4239. init_stuff(PyObject *module, PyObject *module_dict) {
  4240.     PyObject *string, *copy_reg, *t, *r;
  4241.  
  4242. #define INIT_STR(S) UNLESS(S ## _str=PyString_FromString(#S)) return -1;
  4243.  
  4244.     INIT_STR(__class__);
  4245.     INIT_STR(__getinitargs__);
  4246.     INIT_STR(__dict__);
  4247.     INIT_STR(__getstate__);
  4248.     INIT_STR(__setstate__);
  4249.     INIT_STR(__name__);
  4250.     INIT_STR(__main__);
  4251.     INIT_STR(__reduce__);
  4252.     INIT_STR(write);
  4253.     INIT_STR(__safe_for_unpickling__);
  4254.     INIT_STR(append);
  4255.     INIT_STR(read);
  4256.     INIT_STR(readline);
  4257.     INIT_STR(copy_reg);
  4258.     INIT_STR(dispatch_table);
  4259.     INIT_STR(safe_constructors);
  4260.     INIT_STR(__basicnew__);
  4261.     UNLESS (empty_str=PyString_FromString("")) return -1;
  4262.  
  4263.     UNLESS (copy_reg = PyImport_ImportModule("copy_reg"))
  4264.         return -1;
  4265.  
  4266.     /* These next few are special because we want to use different
  4267.        ones in restricted mode. */
  4268.  
  4269.     UNLESS (dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str))
  4270.         return -1;
  4271.  
  4272.     UNLESS (safe_constructors = PyObject_GetAttr(copy_reg,
  4273.                                                 safe_constructors_str))
  4274.         return -1;
  4275.  
  4276.     Py_DECREF(copy_reg);
  4277.  
  4278.     /* Down to here ********************************** */
  4279.  
  4280.     UNLESS (string = PyImport_ImportModule("string"))
  4281.         return -1;
  4282.  
  4283.     UNLESS (atol_func = PyObject_GetAttrString(string, "atol"))
  4284.         return -1;
  4285.  
  4286.     Py_DECREF(string);
  4287.  
  4288.     UNLESS (empty_tuple = PyTuple_New(0))
  4289.         return -1;
  4290.  
  4291.     /* Ugh */
  4292.     UNLESS (t=PyImport_ImportModule("__builtin__")) return -1;
  4293.     if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
  4294.       return -1;
  4295.  
  4296.     UNLESS (t=PyDict_New()) return -1;
  4297.     UNLESS (r=PyRun_String(
  4298.        "def __init__(self, *args): self.args=args\n\n"
  4299.        "def __str__(self):\n"
  4300.        "  return self.args and ('%s' % self.args[0]) or '(what)'\n",
  4301.        Py_file_input,
  4302.        module_dict, t) ) return -1;
  4303.     Py_DECREF(r);
  4304.  
  4305.     UNLESS (PickleError = PyErr_NewException("cPickle.PickleError", NULL, t))
  4306.       return -1;
  4307.  
  4308.     Py_DECREF(t);
  4309.     
  4310.  
  4311.     UNLESS (PicklingError = PyErr_NewException("cPickle.PicklingError", 
  4312.                            PickleError, NULL))
  4313.       return -1;
  4314.  
  4315.     UNLESS (t=PyDict_New()) return -1;
  4316.     UNLESS (r=PyRun_String(
  4317.        "def __init__(self, *args): self.args=args\n\n"
  4318.        "def __str__(self):\n"
  4319.        "  a=self.args\n"
  4320.        "  a=a and type(a[0]) or '(what)'\n"
  4321.        "  return 'Cannot pickle %s objects' % a\n"
  4322.        , Py_file_input,
  4323.        module_dict, t) ) return -1;
  4324.     Py_DECREF(r);
  4325.  
  4326.     UNLESS (UnpickleableError = PyErr_NewException(
  4327.                 "cPickle.UnpickleableError", PicklingError, t))
  4328.       return -1;
  4329.  
  4330.     Py_DECREF(t);
  4331.  
  4332.     UNLESS (UnpicklingError = PyErr_NewException("cPickle.UnpicklingError", 
  4333.                                 PickleError, NULL))
  4334.       return -1;
  4335.  
  4336.     if (PyDict_SetItemString(module_dict, "PickleError", 
  4337.         PickleError) < 0)
  4338.         return -1;
  4339.  
  4340.     if (PyDict_SetItemString(module_dict, "PicklingError", 
  4341.         PicklingError) < 0)
  4342.         return -1;
  4343.  
  4344.     if (PyDict_SetItemString(module_dict, "UnpicklingError",
  4345.         UnpicklingError) < 0)
  4346.         return -1;
  4347.  
  4348.     if (PyDict_SetItemString(module_dict, "UnpickleableError",
  4349.         UnpickleableError) < 0)
  4350.         return -1;
  4351.  
  4352.     UNLESS (BadPickleGet = PyString_FromString("cPickle.BadPickleGet"))
  4353.         return -1;
  4354.  
  4355.     if (PyDict_SetItemString(module_dict, "BadPickleGet",
  4356.         BadPickleGet) < 0)
  4357.         return -1;
  4358.  
  4359.     PycString_IMPORT;
  4360.  
  4361.     return 0;
  4362. }
  4363.  
  4364. #ifndef DL_EXPORT    /* declarations for DLL import/export */
  4365. #define DL_EXPORT(RTYPE) RTYPE
  4366. #endif
  4367. DL_EXPORT(void)
  4368. initcPickle() {
  4369.     PyObject *m, *d, *v;
  4370.     char *rev="1.71";
  4371.     PyObject *format_version;
  4372.     PyObject *compatible_formats;
  4373.  
  4374.     Picklertype.ob_type = &PyType_Type;
  4375.     Unpicklertype.ob_type = &PyType_Type;
  4376.     PdataType.ob_type = &PyType_Type;
  4377.  
  4378.     /* Create the module and add the functions */
  4379.     m = Py_InitModule4("cPickle", cPickle_methods,
  4380.                      cPickle_module_documentation,
  4381.                      (PyObject*)NULL,PYTHON_API_VERSION);
  4382.  
  4383.     /* Add some symbolic constants to the module */
  4384.     d = PyModule_GetDict(m);
  4385.     PyDict_SetItemString(d,"__version__", v = PyString_FromString(rev));
  4386.     Py_XDECREF(v);
  4387.  
  4388.     format_version = PyString_FromString("1.3");
  4389.     compatible_formats = Py_BuildValue("[sss]", "1.0", "1.1", "1.2");
  4390.  
  4391.     PyDict_SetItemString(d, "format_version", format_version);
  4392.     PyDict_SetItemString(d, "compatible_formats", compatible_formats);
  4393.     Py_XDECREF(format_version);
  4394.     Py_XDECREF(compatible_formats);
  4395.  
  4396.     init_stuff(m, d);
  4397. }
  4398.