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 / Python / marshal.c < prev    next >
C/C++ Source or Header  |  2000-12-21  |  17KB  |  846 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* Write Python objects to files and read them back.
  33.    This is intended for writing and reading compiled Python code only;
  34.    a true persistent storage facility would be much harder, since
  35.    it would have to take circular links and sharing into account. */
  36.  
  37. #include "Python.h"
  38. #include "longintrepr.h"
  39. #include "compile.h"
  40. #include "marshal.h"
  41.  
  42. #define TYPE_NULL    '0'
  43. #define TYPE_NONE    'N'
  44. #define TYPE_ELLIPSIS   '.'
  45. #define TYPE_INT    'i'
  46. #define TYPE_INT64    'I'
  47. #define TYPE_FLOAT    'f'
  48. #define TYPE_COMPLEX    'x'
  49. #define TYPE_LONG    'l'
  50. #define TYPE_STRING    's'
  51. #define TYPE_TUPLE    '('
  52. #define TYPE_LIST    '['
  53. #define TYPE_DICT    '{'
  54. #define TYPE_CODE    'c'
  55. #define TYPE_UNKNOWN    '?'
  56.  
  57. typedef struct {
  58.     FILE *fp;
  59.     int error;
  60.     /* If fp == NULL, the following are valid: */
  61.     PyObject *str;
  62.     char *ptr;
  63.     char *end;
  64. } WFILE;
  65.  
  66. typedef WFILE RFILE; /* Same struct with different invariants */
  67.  
  68. #define w_byte(c, p) if (((p)->fp)) putc((c), (p)->fp); \
  69.               else if ((p)->ptr != (p)->end) *(p)->ptr++ = (c); \
  70.                else w_more(c, p)
  71. #include "other/marshal_c.h"
  72.  
  73. static void
  74. w_more(c, p)
  75.     char c;
  76.     WFILE *p;
  77. {
  78.     int size, newsize;
  79.     if (p->str == NULL)
  80.         return; /* An error already occurred */
  81.     size = PyString_Size(p->str);
  82.     newsize = size + 1024;
  83.     if (_PyString_Resize(&p->str, newsize) != 0) {
  84.         p->ptr = p->end = NULL;
  85.     }
  86.     else {
  87.         p->ptr = PyString_AS_STRING((PyStringObject *)p->str) + size;
  88.         p->end =
  89.             PyString_AS_STRING((PyStringObject *)p->str) + newsize;
  90.         *p->ptr++ = c;
  91.     }
  92. }
  93.  
  94. static void
  95. w_string(s, n, p)
  96.     char *s;
  97.     int n;
  98.     WFILE *p;
  99. {
  100.     if (p->fp != NULL) {
  101.         fwrite(s, 1, n, p->fp);
  102.     }
  103.     else {
  104.         while (--n >= 0) {
  105.             w_byte(*s, p);
  106.             s++;
  107.         }
  108.     }
  109. }
  110.  
  111. static void
  112. w_short(x, p)
  113.     int x;
  114.     WFILE *p;
  115. {
  116.     w_byte( x      & 0xff, p);
  117.     w_byte((x>> 8) & 0xff, p);
  118. }
  119.  
  120. static void
  121. w_long(x, p)
  122.     long x;
  123.     WFILE *p;
  124. {
  125.     w_byte((int)( x      & 0xff), p);
  126.     w_byte((int)((x>> 8) & 0xff), p);
  127.     w_byte((int)((x>>16) & 0xff), p);
  128.     w_byte((int)((x>>24) & 0xff), p);
  129. }
  130.  
  131. #if SIZEOF_LONG > 4
  132. static void
  133. w_long64(x, p)
  134.     long x;
  135.     WFILE *p;
  136. {
  137.     w_long(x, p);
  138.     w_long(x>>32, p);
  139. }
  140. #endif
  141.  
  142. static void
  143. w_object(v, p)
  144.     PyObject *v;
  145.     WFILE *p;
  146. {
  147.     int i, n;
  148.     PyBufferProcs *pb;
  149.     
  150.     if (v == NULL) {
  151.         w_byte(TYPE_NULL, p);
  152.     }
  153.     else if (v == Py_None) {
  154.         w_byte(TYPE_NONE, p);
  155.     }
  156.     else if (v == Py_Ellipsis) {
  157.             w_byte(TYPE_ELLIPSIS, p);
  158.     }
  159.     else if (PyInt_Check(v)) {
  160.         long x = PyInt_AS_LONG((PyIntObject *)v);
  161. #if SIZEOF_LONG > 4
  162.         long y = x>>31;
  163.         if (y && y != -1) {
  164.             w_byte(TYPE_INT64, p);
  165.             w_long64(x, p);
  166.         }
  167.         else
  168. #endif
  169.             {
  170.             w_byte(TYPE_INT, p);
  171.             w_long(x, p);
  172.         }
  173.     }
  174.     else if (PyLong_Check(v)) {
  175.         PyLongObject *ob = (PyLongObject *)v;
  176.         w_byte(TYPE_LONG, p);
  177.         n = ob->ob_size;
  178.         w_long((long)n, p);
  179.         if (n < 0)
  180.             n = -n;
  181.         for (i = 0; i < n; i++)
  182.             w_short(ob->ob_digit[i], p);
  183.     }
  184. #ifndef WITHOUT_FLOAT
  185.     else if (PyFloat_Check(v)) {
  186.         extern void PyFloat_AsString
  187.             Py_PROTO((char *, PyFloatObject *));
  188.         char buf[256]; /* Plenty to format any double */
  189.         PyFloat_AsString(buf, (PyFloatObject *)v);
  190.         n = strlen(buf);
  191.         w_byte(TYPE_FLOAT, p);
  192.         w_byte(n, p);
  193.         w_string(buf, n, p);
  194.     }
  195. #endif /* WITHOUT_FLOAT */
  196. #ifndef WITHOUT_COMPLEX
  197.     else if (PyComplex_Check(v)) {
  198.         extern void PyFloat_AsString
  199.             Py_PROTO((char *, PyFloatObject *));
  200.         char buf[256]; /* Plenty to format any double */
  201.         PyFloatObject *temp;
  202.         w_byte(TYPE_COMPLEX, p);
  203.         temp = (PyFloatObject*)PyFloat_FromDouble(
  204.             PyComplex_RealAsDouble(v));
  205.         PyFloat_AsString(buf, temp);
  206.         Py_DECREF(temp);
  207.         n = strlen(buf);
  208.         w_byte(n, p);
  209.         w_string(buf, n, p);
  210.         temp = (PyFloatObject*)PyFloat_FromDouble(
  211.             PyComplex_ImagAsDouble(v));
  212.         PyFloat_AsString(buf, temp);
  213.         Py_DECREF(temp);
  214.         n = strlen(buf);
  215.         w_byte(n, p);
  216.         w_string(buf, n, p);
  217.     }
  218. #endif
  219.     else if (PyString_Check(v)) {
  220.         w_byte(TYPE_STRING, p);
  221.         n = PyString_Size(v);
  222.         w_long((long)n, p);
  223.         w_string(PyString_AsString(v), n, p);
  224.     }
  225.     else if (PyTuple_Check(v)) {
  226.         w_byte(TYPE_TUPLE, p);
  227.         n = PyTuple_Size(v);
  228.         w_long((long)n, p);
  229.         for (i = 0; i < n; i++) {
  230.             w_object(PyTuple_GET_ITEM(v, i), p);
  231.         }
  232.     }
  233.     else if (PyList_Check(v)) {
  234.         w_byte(TYPE_LIST, p);
  235.         n = PyList_Size(v);
  236.         w_long((long)n, p);
  237.         for (i = 0; i < n; i++) {
  238.             w_object(PyList_GetItem(v, i), p);
  239.         }
  240.     }
  241.     else if (PyDict_Check(v)) {
  242.         int pos;
  243.         PyObject *key, *value;
  244.         w_byte(TYPE_DICT, p);
  245.         /* This one is NULL object terminated! */
  246.         pos = 0;
  247.         while (PyDict_Next(v, &pos, &key, &value)) {
  248.             w_object(key, p);
  249.             w_object(value, p);
  250.         }
  251.         w_object((PyObject *)NULL, p);
  252.     }
  253.     else if (PyCode_Check(v)) {
  254.         PyCodeObject *co = (PyCodeObject *)v;
  255.         w_byte(TYPE_CODE, p);
  256.         w_short(co->co_argcount, p);
  257.         w_short(co->co_nlocals, p);
  258.         w_short(co->co_stacksize, p);
  259.         w_short(co->co_flags, p);
  260.         w_object(co->co_code, p);
  261.         w_object(co->co_consts, p);
  262.         w_object(co->co_names, p);
  263.         w_object(co->co_varnames, p);
  264.         w_object(co->co_filename, p);
  265.         w_object(co->co_name, p);
  266.         w_short(co->co_firstlineno, p);
  267.         w_object(co->co_lnotab, p);
  268.     }
  269.     else if ((pb = v->ob_type->tp_as_buffer) != NULL &&
  270.          pb->bf_getsegcount != NULL &&
  271.          pb->bf_getreadbuffer != NULL &&
  272.          (*pb->bf_getsegcount)(v, NULL) == 1)
  273.     {
  274.         /* Write unknown buffer-style objects as a string */
  275.         char *s;
  276.         w_byte(TYPE_STRING, p);
  277.         n = (*pb->bf_getreadbuffer)(v, 0, (void **)&s);
  278.         w_long((long)n, p);
  279.         w_string(s, n, p);
  280.     }
  281.     else {
  282.         w_byte(TYPE_UNKNOWN, p);
  283.         p->error = 1;
  284.     }
  285. }
  286.  
  287. void
  288. PyMarshal_WriteLongToFile(x, fp)
  289.     long x;
  290.     FILE *fp;
  291. {
  292.     WFILE wf;
  293.     wf.fp = fp;
  294.     wf.error = 0;
  295.     w_long(x, &wf);
  296. }
  297.  
  298. void
  299. PyMarshal_WriteObjectToFile(x, fp)
  300.     PyObject *x;
  301.     FILE *fp;
  302. {
  303.     WFILE wf;
  304.     wf.fp = fp;
  305.     wf.error = 0;
  306.     w_object(x, &wf);
  307. }
  308.  
  309.  
  310. #define rs_byte(p) (((p)->ptr != (p)->end) ? (unsigned char)*(p)->ptr++ : EOF)
  311.  
  312. #define r_byte(p) ((p)->fp ? getc((p)->fp) : rs_byte(p))
  313.  
  314. static int
  315. r_string(s, n, p)
  316.     char *s;
  317.     int n;
  318.     RFILE *p;
  319. {
  320.     if (p->fp != NULL)
  321.         return fread(s, 1, n, p->fp);
  322.     if (p->end - p->ptr < n)
  323.         n = p->end - p->ptr;
  324.     memcpy(s, p->ptr, n);
  325.     p->ptr += n;
  326.     return n;
  327. }
  328.  
  329. static int
  330. r_string_in_place(s, n, p)
  331.     char **s;
  332.     int n;
  333.     RFILE *p;
  334. {
  335. /*     if (p->fp != NULL) */
  336. /*         return fread(s, 1, n, p->fp); */
  337.     if (p->end - p->ptr < n)
  338.         n = p->end - p->ptr;
  339.     *s = p->ptr;
  340.     p->ptr += n;
  341.     return n;
  342. }
  343.  
  344. static int
  345. r_short(p)
  346.     RFILE *p;
  347. {
  348.     register short x;
  349.     x = r_byte(p);
  350.     x |= r_byte(p) << 8;
  351.     /* XXX If your short is > 16 bits, add sign-extension here!!! */
  352.     return x;
  353. }
  354.  
  355. static long
  356. r_long(p)
  357.     RFILE *p;
  358. {
  359.     register long x;
  360.     register FILE *fp = p->fp;
  361.     if (fp) {
  362.         x = getc(fp);
  363.         x |= (long)getc(fp) << 8;
  364.         x |= (long)getc(fp) << 16;
  365.         x |= (long)getc(fp) << 24;
  366.     }
  367.     else {
  368.         x = rs_byte(p);
  369.         x |= (long)rs_byte(p) << 8;
  370.         x |= (long)rs_byte(p) << 16;
  371.         x |= (long)rs_byte(p) << 24;
  372.     }
  373. #if SIZEOF_LONG > 4
  374.     /* Sign extension for 64-bit machines */
  375.     x <<= (8*sizeof(long) - 32);
  376.     x >>= (8*sizeof(long) - 32);
  377. #endif
  378.     return x;
  379. }
  380.  
  381. static long
  382. r_long64(p)
  383.     RFILE *p;
  384. {
  385.     register long x;
  386.     x = r_long(p);
  387. #if SIZEOF_LONG > 4
  388.     x = (x & 0xFFFFFFFF) | (r_long(p) << 32);
  389. #else
  390.     if (r_long(p) != 0) {
  391.         PyObject *f = PySys_GetObject("stderr");
  392.         if (f != NULL)
  393.             (void) PyFile_WriteString(
  394.                 "Warning: un-marshal 64-bit int in 32-bit mode\n",
  395.                 f);
  396.     }
  397. #endif
  398.     return x;
  399. }
  400.  
  401. static PyObject *
  402. r_code_as_buffer(RFILE *p)
  403. {
  404.     /* this is a hack to read bytecodes directly from datamanager memory,
  405.        and should be replaced by a more robust solution.
  406.      */
  407.  
  408.     long n;
  409.     char *s;
  410.     PyObject *v;
  411.     int type = r_byte(p);
  412.     
  413.     switch (type) {
  414.  
  415.     case TYPE_STRING:
  416.         n = r_long(p);
  417.         if (n < 0) {
  418.             PyErr_SetString(PyExc_ValueError, "bad marshal data");
  419.             return NULL;
  420.         }
  421.  
  422.         if (r_string_in_place(&s, (int)n, p) != n) {
  423.             PyErr_SetString(PyExc_EOFError,
  424.                     "EOF read where object expected");
  425.             return NULL;
  426.         }
  427.         
  428.         v = PyBuffer_FromMemory(s, n, (unsigned)0);
  429.         if (v==NULL)
  430.             return NULL;
  431. /*         printf("buffer size = %d\n", ((PyBufferObject *)v)->b_size); */
  432.         
  433.         return v;
  434.     default:
  435.         PyErr_SetString(PyExc_ValueError, 
  436.                 "Hey - we were expecting a string for co_code");
  437.         return NULL;
  438.     }
  439.  
  440. }
  441.  
  442.  
  443. static PyObject *
  444. r_object(p)
  445.     RFILE *p;
  446. {
  447.     PyObject *v, *v2;
  448.     long i, n;
  449.     int type = r_byte(p);
  450.     
  451.     switch (type) {
  452.     
  453.     case EOF:
  454.         PyErr_SetString(PyExc_EOFError,
  455.                 "EOF read where object expected");
  456.         return NULL;
  457.     
  458.     case TYPE_NULL:
  459.         return NULL;
  460.     
  461.     case TYPE_NONE:
  462.         Py_INCREF(Py_None);
  463.         return Py_None;
  464.     
  465.     case TYPE_ELLIPSIS:
  466.         Py_INCREF(Py_Ellipsis);
  467.         return Py_Ellipsis;
  468.  
  469.     case TYPE_INT:
  470.         return PyInt_FromLong(r_long(p));
  471.     
  472.     case TYPE_INT64:
  473.         return PyInt_FromLong(r_long64(p));
  474.     
  475.     case TYPE_LONG:
  476.         {
  477.             int size;
  478.             PyLongObject *ob;
  479.             n = r_long(p);
  480.             size = n<0 ? -n : n;
  481.             ob = _PyLong_New(size);
  482.             if (ob == NULL)
  483.                 return NULL;
  484.             ob->ob_size = n;
  485.             for (i = 0; i < size; i++)
  486.                 ob->ob_digit[i] = r_short(p);
  487.             return (PyObject *)ob;
  488.         }
  489.     
  490.     case TYPE_FLOAT:
  491.         {
  492. #ifndef WITHOUT_FLOAT
  493.             extern double atof Py_PROTO((const char *));
  494.             char buf[256];
  495.             double dx;
  496.             n = r_byte(p);
  497.             if (r_string(buf, (int)n, p) != n) {
  498.                 PyErr_SetString(PyExc_EOFError,
  499.                     "EOF read where object expected");
  500.                 return NULL;
  501.             }
  502.             buf[n] = '\0';
  503.             PyFPE_START_PROTECT("atof", return 0)
  504.             dx = atof(buf);
  505.             PyFPE_END_PROTECT(dx)
  506.             return PyFloat_FromDouble(dx);
  507. #else /* !WITHOUT_FLOAT */
  508.             PyErr_SetString(PyExc_MissingFeatureError,
  509.             "Float objects are not provided in this python build");
  510.             return NULL;
  511. #endif /* !WITHOUT_FLOAT */
  512.         }
  513.     
  514.     case TYPE_COMPLEX:
  515.         {
  516. #ifndef WITHOUT_COMPLEX
  517.             extern double atof Py_PROTO((const char *));
  518.             char buf[256];
  519.             Py_complex c;
  520.             n = r_byte(p);
  521.             if (r_string(buf, (int)n, p) != n) {
  522.                 PyErr_SetString(PyExc_EOFError,
  523.                     "EOF read where object expected");
  524.                 return NULL;
  525.             }
  526.             buf[n] = '\0';
  527.             PyFPE_START_PROTECT("atof", return 0)
  528.             c.real = atof(buf);
  529.             PyFPE_END_PROTECT(c)
  530.             n = r_byte(p);
  531.             if (r_string(buf, (int)n, p) != n) {
  532.                 PyErr_SetString(PyExc_EOFError,
  533.                     "EOF read where object expected");
  534.                 return NULL;
  535.             }
  536.             buf[n] = '\0';
  537.             PyFPE_START_PROTECT("atof", return 0)
  538.             c.imag = atof(buf);
  539.             PyFPE_END_PROTECT(c)
  540.             return PyComplex_FromCComplex(c);
  541. #else /* !WITHOUT_COMPLEX */
  542.             PyErr_SetString(PyExc_MissingFeatureError,
  543.                     "Complex objects are not provided in this python build");
  544.             return NULL;
  545. #endif
  546.         }
  547.     
  548.     case TYPE_STRING:
  549.         n = r_long(p);
  550.         if (n < 0) {
  551.             PyErr_SetString(PyExc_ValueError, "bad marshal data");
  552.             return NULL;
  553.         }
  554.         v = PyString_FromStringAndSize((char *)NULL, n);
  555.         if (v != NULL) {
  556.             if (r_string(PyString_AsString(v), (int)n, p) != n) {
  557.                 Py_DECREF(v);
  558.                 v = NULL;
  559.                 PyErr_SetString(PyExc_EOFError,
  560.                     "EOF read where object expected");
  561.             }
  562.         }
  563.         return v;
  564.     
  565.     case TYPE_TUPLE:
  566.         n = r_long(p);
  567.         if (n < 0) {
  568.             PyErr_SetString(PyExc_ValueError, "bad marshal data");
  569.             return NULL;
  570.         }
  571.         v = PyTuple_New((int)n);
  572.         if (v == NULL)
  573.             return v;
  574.         for (i = 0; i < n; i++) {
  575.             v2 = r_object(p);
  576.             if ( v2 == NULL ) {
  577.                 Py_DECREF(v);
  578.                 v = NULL;
  579.                 break;
  580.             }
  581.             PyTuple_SET_ITEM(v, (int)i, v2);
  582.         }
  583.         return v;
  584.     
  585.     case TYPE_LIST:
  586.         n = r_long(p);
  587.         if (n < 0) {
  588.             PyErr_SetString(PyExc_ValueError, "bad marshal data");
  589.             return NULL;
  590.         }
  591.         v = PyList_New((int)n);
  592.         if (v == NULL)
  593.             return v;
  594.         for (i = 0; i < n; i++) {
  595.             v2 = r_object(p);
  596.             if ( v2 == NULL ) {
  597.                 Py_DECREF(v);
  598.                 v = NULL;
  599.                 break;
  600.             }
  601.             PyList_SetItem(v, (int)i, v2);
  602.         }
  603.         return v;
  604.     
  605.     case TYPE_DICT:
  606.         v = PyDict_New();
  607.         if (v == NULL)
  608.             return NULL;
  609.         for (;;) {
  610.             PyObject *key, *val;
  611.             key = r_object(p);
  612.             if (key == NULL)
  613.                 break; /* XXX Assume TYPE_NULL, not an error */
  614.             val = r_object(p);
  615.             if (val != NULL)
  616.                 PyDict_SetItem(v, key, val);
  617.             Py_DECREF(key);
  618.             Py_XDECREF(val);
  619.         }
  620.         return v;
  621.     
  622.     case TYPE_CODE:
  623.         {
  624.             int argcount = r_short(p);
  625.             int nlocals = r_short(p);
  626.             int stacksize = r_short(p);
  627.             int flags = r_short(p);
  628.             PyObject *code = NULL;
  629.             PyObject *consts = NULL;
  630.             PyObject *names = NULL;
  631.             PyObject *varnames = NULL;
  632.             PyObject *filename = NULL;
  633.             PyObject *name = NULL;
  634.             int firstlineno = 0;
  635.             PyObject *lnotab = NULL;
  636.             
  637. /*             code = r_object(p); */
  638.             code = r_code_as_buffer(p);
  639.             if (code) consts = r_object(p);
  640.             if (consts) names = r_object(p);
  641.             if (names) varnames = r_object(p);
  642.             if (varnames) filename = r_object(p);
  643.             if (filename) name = r_object(p);
  644.             if (name) {
  645.                 firstlineno = r_short(p);
  646.                 lnotab = r_object(p);
  647.             }
  648.             
  649.             if (!PyErr_Occurred()) {
  650.                 v = (PyObject *) PyCode_New(
  651.                     argcount, nlocals, stacksize, flags, 
  652.                     code, consts, names, varnames,
  653.                     filename, name, firstlineno, lnotab);
  654.             }
  655.             else
  656.                 v = NULL;
  657.             Py_XDECREF(code);
  658.             Py_XDECREF(consts);
  659.             Py_XDECREF(names);
  660.             Py_XDECREF(varnames);
  661.             Py_XDECREF(filename);
  662.             Py_XDECREF(name);
  663.             Py_XDECREF(lnotab);
  664.  
  665.         }
  666.         return v;
  667.     
  668.     default:
  669.         /* Bogus data got written, which isn't ideal.
  670.            This will let you keep working and recover. */
  671.         PyErr_SetString(PyExc_ValueError, "bad marshal data");
  672.         return NULL;
  673.     
  674.     }
  675. }
  676.  
  677. long
  678. PyMarshal_ReadLongFromFile(fp)
  679.     FILE *fp;
  680. {
  681.     RFILE rf;
  682.     rf.fp = fp;
  683.     return r_long(&rf);
  684. }
  685.  
  686. PyObject *
  687. PyMarshal_ReadObjectFromFile(fp)
  688.     FILE *fp;
  689. {
  690.     RFILE rf;
  691.     if (PyErr_Occurred()) {
  692.         fprintf(stderr, "XXX rd_object called with exception set\n");
  693.         return NULL;
  694.     }
  695.     rf.fp = fp;
  696.     return r_object(&rf);
  697. }
  698.  
  699. PyObject *
  700. PyMarshal_ReadObjectFromString(str, len)
  701.     char *str;
  702.     int len;
  703. {
  704.     RFILE rf;
  705.     if (PyErr_Occurred()) {
  706.         fprintf(stderr, "XXX rds_object called with exception set\n");
  707.         return NULL;
  708.     }
  709.     rf.fp = NULL;
  710.     rf.str = NULL;
  711.     rf.ptr = str;
  712.     rf.end = str + len;
  713.     return r_object(&rf);
  714. }
  715.  
  716. PyObject *
  717. PyMarshal_WriteObjectToString(x) /* wrs_object() */
  718.     PyObject *x;
  719. {
  720.     WFILE wf;
  721.     wf.fp = NULL;
  722.     wf.str = PyString_FromStringAndSize((char *)NULL, 50);
  723.     if (wf.str == NULL)
  724.         return NULL;
  725.     wf.ptr = PyString_AS_STRING((PyStringObject *)wf.str);
  726.     wf.end = wf.ptr + PyString_Size(wf.str);
  727.     wf.error = 0;
  728.     w_object(x, &wf);
  729.     if (wf.str != NULL)
  730.         _PyString_Resize(&wf.str,
  731.             (int) (wf.ptr -
  732.                PyString_AS_STRING((PyStringObject *)wf.str)));
  733.     if (wf.error) {
  734.         Py_XDECREF(wf.str);
  735.         PyErr_SetString(PyExc_ValueError, "unmarshallable object");
  736.         return NULL;
  737.     }
  738.     return wf.str;
  739. }
  740.  
  741. /* And an interface for Python programs... */
  742.  
  743. static PyObject *
  744. marshal_dump(self, args)
  745.     PyObject *self;
  746.     PyObject *args;
  747. {
  748.     WFILE wf;
  749.     PyObject *x;
  750.     PyObject *f;
  751.     if (!PyArg_Parse(args, "(OO)", &x, &f))
  752.         return NULL;
  753.     if (!PyFile_Check(f)) {
  754.         PyErr_SetString(PyExc_TypeError,
  755.                 "marshal.dump() 2nd arg must be file");
  756.         return NULL;
  757.     }
  758.     wf.fp = PyFile_AsFile(f);
  759.     wf.str = NULL;
  760.     wf.ptr = wf.end = NULL;
  761.     wf.error = 0;
  762.     w_object(x, &wf);
  763.     if (wf.error) {
  764.         PyErr_SetString(PyExc_ValueError, "unmarshallable object");
  765.         return NULL;
  766.     }
  767.     Py_INCREF(Py_None);
  768.     return Py_None;
  769. }
  770.  
  771. static PyObject *
  772. marshal_load(self, args)
  773.     PyObject *self;
  774.     PyObject *args;
  775. {
  776.     RFILE rf;
  777.     PyObject *f;
  778.     PyObject *v;
  779.     if (!PyArg_Parse(args, "O", &f))
  780.         return NULL;
  781.     if (!PyFile_Check(f)) {
  782.         PyErr_SetString(PyExc_TypeError,
  783.                 "marshal.load() arg must be file");
  784.         return NULL;
  785.     }
  786.     rf.fp = PyFile_AsFile(f);
  787.     rf.str = NULL;
  788.     rf.ptr = rf.end = NULL;
  789.     PyErr_Clear();
  790.     v = r_object(&rf);
  791.     if (PyErr_Occurred()) {
  792.         Py_XDECREF(v);
  793.         v = NULL;
  794.     }
  795.     return v;
  796. }
  797.  
  798. static PyObject *
  799. marshal_dumps(self, args)
  800.     PyObject *self;
  801.     PyObject *args;
  802. {
  803.     PyObject *x;
  804.     if (!PyArg_Parse(args, "O", &x))
  805.         return NULL;
  806.     return PyMarshal_WriteObjectToString(x);
  807. }
  808.  
  809. static PyObject *
  810. marshal_loads(self, args)
  811.     PyObject *self;
  812.     PyObject *args;
  813. {
  814.     RFILE rf;
  815.     PyObject *v;
  816.     char *s;
  817.     int n;
  818.     if (!PyArg_Parse(args, "s#", &s, &n))
  819.         return NULL;
  820.     rf.fp = NULL;
  821.     rf.str = args;
  822.     rf.ptr = s;
  823.     rf.end = s + n;
  824.     PyErr_Clear();
  825.     v = r_object(&rf);
  826.     if (PyErr_Occurred()) {
  827.         Py_XDECREF(v);
  828.         v = NULL;
  829.     }
  830.     return v;
  831. }
  832.  
  833. static PyMethodDef marshal_methods[] = {
  834.     {"dump",    marshal_dump},
  835.     {"load",    marshal_load},
  836.     {"dumps",    marshal_dumps},
  837.     {"loads",    marshal_loads},
  838.     {NULL,        NULL}        /* sentinel */
  839. };
  840.  
  841. void
  842. PyMarshal_Init()
  843. {
  844.     (void) Py_InitModule("marshal", marshal_methods);
  845. }
  846.