You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1410 lines
38 KiB

35 years ago
35 years ago
35 years ago
35 years ago
35 years ago
35 years ago
35 years ago
35 years ago
35 years ago
35 years ago
35 years ago
35 years ago
35 years ago
35 years ago
35 years ago
35 years ago
35 years ago
35 years ago
35 years ago
35 years ago
35 years ago
35 years ago
35 years ago
35 years ago
35 years ago
35 years ago
  1. /* Write Python objects to files and read them back.
  2. This is intended for writing and reading compiled Python code only;
  3. a true persistent storage facility would be much harder, since
  4. it would have to take circular links and sharing into account. */
  5. #define PY_SSIZE_T_CLEAN
  6. #include "Python.h"
  7. #include "longintrepr.h"
  8. #include "code.h"
  9. #include "marshal.h"
  10. #define ABS(x) ((x) < 0 ? -(x) : (x))
  11. /* High water mark to determine when the marshalled object is dangerously deep
  12. * and risks coring the interpreter. When the object stack gets this deep,
  13. * raise an exception instead of continuing.
  14. */
  15. #define MAX_MARSHAL_STACK_DEPTH 2000
  16. #define TYPE_NULL '0'
  17. #define TYPE_NONE 'N'
  18. #define TYPE_FALSE 'F'
  19. #define TYPE_TRUE 'T'
  20. #define TYPE_STOPITER 'S'
  21. #define TYPE_ELLIPSIS '.'
  22. #define TYPE_INT 'i'
  23. #define TYPE_INT64 'I'
  24. #define TYPE_FLOAT 'f'
  25. #define TYPE_BINARY_FLOAT 'g'
  26. #define TYPE_COMPLEX 'x'
  27. #define TYPE_BINARY_COMPLEX 'y'
  28. #define TYPE_LONG 'l'
  29. #define TYPE_STRING 's'
  30. #define TYPE_INTERNED 't'
  31. #define TYPE_STRINGREF 'R'
  32. #define TYPE_TUPLE '('
  33. #define TYPE_LIST '['
  34. #define TYPE_DICT '{'
  35. #define TYPE_CODE 'c'
  36. #define TYPE_UNICODE 'u'
  37. #define TYPE_UNKNOWN '?'
  38. #define TYPE_SET '<'
  39. #define TYPE_FROZENSET '>'
  40. #define WFERR_OK 0
  41. #define WFERR_UNMARSHALLABLE 1
  42. #define WFERR_NESTEDTOODEEP 2
  43. #define WFERR_NOMEMORY 3
  44. typedef struct {
  45. FILE *fp;
  46. int error; /* see WFERR_* values */
  47. int depth;
  48. /* If fp == NULL, the following are valid: */
  49. PyObject *str;
  50. char *ptr;
  51. char *end;
  52. PyObject *strings; /* dict on marshal, list on unmarshal */
  53. int version;
  54. } WFILE;
  55. #define w_byte(c, p) if (((p)->fp)) putc((c), (p)->fp); \
  56. else if ((p)->ptr != (p)->end) *(p)->ptr++ = (c); \
  57. else w_more(c, p)
  58. static void
  59. w_more(int c, WFILE *p)
  60. {
  61. Py_ssize_t size, newsize;
  62. if (p->str == NULL)
  63. return; /* An error already occurred */
  64. size = PyString_Size(p->str);
  65. newsize = size + size + 1024;
  66. if (newsize > 32*1024*1024) {
  67. newsize = size + (size >> 3); /* 12.5% overallocation */
  68. }
  69. if (_PyString_Resize(&p->str, newsize) != 0) {
  70. p->ptr = p->end = NULL;
  71. }
  72. else {
  73. p->ptr = PyString_AS_STRING((PyStringObject *)p->str) + size;
  74. p->end =
  75. PyString_AS_STRING((PyStringObject *)p->str) + newsize;
  76. *p->ptr++ = Py_SAFE_DOWNCAST(c, int, char);
  77. }
  78. }
  79. static void
  80. w_string(char *s, int n, WFILE *p)
  81. {
  82. if (p->fp != NULL) {
  83. fwrite(s, 1, n, p->fp);
  84. }
  85. else {
  86. while (--n >= 0) {
  87. w_byte(*s, p);
  88. s++;
  89. }
  90. }
  91. }
  92. static void
  93. w_short(int x, WFILE *p)
  94. {
  95. w_byte((char)( x & 0xff), p);
  96. w_byte((char)((x>> 8) & 0xff), p);
  97. }
  98. static void
  99. w_long(long x, WFILE *p)
  100. {
  101. w_byte((char)( x & 0xff), p);
  102. w_byte((char)((x>> 8) & 0xff), p);
  103. w_byte((char)((x>>16) & 0xff), p);
  104. w_byte((char)((x>>24) & 0xff), p);
  105. }
  106. #if SIZEOF_LONG > 4
  107. static void
  108. w_long64(long x, WFILE *p)
  109. {
  110. w_long(x, p);
  111. w_long(x>>32, p);
  112. }
  113. #endif
  114. /* We assume that Python longs are stored internally in base some power of
  115. 2**15; for the sake of portability we'll always read and write them in base
  116. exactly 2**15. */
  117. #define PyLong_MARSHAL_SHIFT 15
  118. #define PyLong_MARSHAL_BASE ((short)1 << PyLong_MARSHAL_SHIFT)
  119. #define PyLong_MARSHAL_MASK (PyLong_MARSHAL_BASE - 1)
  120. #if PyLong_SHIFT % PyLong_MARSHAL_SHIFT != 0
  121. #error "PyLong_SHIFT must be a multiple of PyLong_MARSHAL_SHIFT"
  122. #endif
  123. #define PyLong_MARSHAL_RATIO (PyLong_SHIFT / PyLong_MARSHAL_SHIFT)
  124. static void
  125. w_PyLong(const PyLongObject *ob, WFILE *p)
  126. {
  127. Py_ssize_t i, j, n, l;
  128. digit d;
  129. w_byte(TYPE_LONG, p);
  130. if (Py_SIZE(ob) == 0) {
  131. w_long((long)0, p);
  132. return;
  133. }
  134. /* set l to number of base PyLong_MARSHAL_BASE digits */
  135. n = ABS(Py_SIZE(ob));
  136. l = (n-1) * PyLong_MARSHAL_RATIO;
  137. d = ob->ob_digit[n-1];
  138. assert(d != 0); /* a PyLong is always normalized */
  139. do {
  140. d >>= PyLong_MARSHAL_SHIFT;
  141. l++;
  142. } while (d != 0);
  143. w_long((long)(Py_SIZE(ob) > 0 ? l : -l), p);
  144. for (i=0; i < n-1; i++) {
  145. d = ob->ob_digit[i];
  146. for (j=0; j < PyLong_MARSHAL_RATIO; j++) {
  147. w_short(d & PyLong_MARSHAL_MASK, p);
  148. d >>= PyLong_MARSHAL_SHIFT;
  149. }
  150. assert (d == 0);
  151. }
  152. d = ob->ob_digit[n-1];
  153. do {
  154. w_short(d & PyLong_MARSHAL_MASK, p);
  155. d >>= PyLong_MARSHAL_SHIFT;
  156. } while (d != 0);
  157. }
  158. static void
  159. w_object(PyObject *v, WFILE *p)
  160. {
  161. Py_ssize_t i, n;
  162. p->depth++;
  163. if (p->depth > MAX_MARSHAL_STACK_DEPTH) {
  164. p->error = WFERR_NESTEDTOODEEP;
  165. }
  166. else if (v == NULL) {
  167. w_byte(TYPE_NULL, p);
  168. }
  169. else if (v == Py_None) {
  170. w_byte(TYPE_NONE, p);
  171. }
  172. else if (v == PyExc_StopIteration) {
  173. w_byte(TYPE_STOPITER, p);
  174. }
  175. else if (v == Py_Ellipsis) {
  176. w_byte(TYPE_ELLIPSIS, p);
  177. }
  178. else if (v == Py_False) {
  179. w_byte(TYPE_FALSE, p);
  180. }
  181. else if (v == Py_True) {
  182. w_byte(TYPE_TRUE, p);
  183. }
  184. else if (PyInt_CheckExact(v)) {
  185. long x = PyInt_AS_LONG((PyIntObject *)v);
  186. #if SIZEOF_LONG > 4
  187. long y = Py_ARITHMETIC_RIGHT_SHIFT(long, x, 31);
  188. if (y && y != -1) {
  189. w_byte(TYPE_INT64, p);
  190. w_long64(x, p);
  191. }
  192. else
  193. #endif
  194. {
  195. w_byte(TYPE_INT, p);
  196. w_long(x, p);
  197. }
  198. }
  199. else if (PyLong_CheckExact(v)) {
  200. PyLongObject *ob = (PyLongObject *)v;
  201. w_PyLong(ob, p);
  202. }
  203. else if (PyFloat_CheckExact(v)) {
  204. if (p->version > 1) {
  205. unsigned char buf[8];
  206. if (_PyFloat_Pack8(PyFloat_AsDouble(v),
  207. buf, 1) < 0) {
  208. p->error = WFERR_UNMARSHALLABLE;
  209. return;
  210. }
  211. w_byte(TYPE_BINARY_FLOAT, p);
  212. w_string((char*)buf, 8, p);
  213. }
  214. else {
  215. char *buf = PyOS_double_to_string(PyFloat_AS_DOUBLE(v),
  216. 'g', 17, 0, NULL);
  217. if (!buf) {
  218. p->error = WFERR_NOMEMORY;
  219. return;
  220. }
  221. n = strlen(buf);
  222. w_byte(TYPE_FLOAT, p);
  223. w_byte((int)n, p);
  224. w_string(buf, (int)n, p);
  225. PyMem_Free(buf);
  226. }
  227. }
  228. #ifndef WITHOUT_COMPLEX
  229. else if (PyComplex_CheckExact(v)) {
  230. if (p->version > 1) {
  231. unsigned char buf[8];
  232. if (_PyFloat_Pack8(PyComplex_RealAsDouble(v),
  233. buf, 1) < 0) {
  234. p->error = WFERR_UNMARSHALLABLE;
  235. return;
  236. }
  237. w_byte(TYPE_BINARY_COMPLEX, p);
  238. w_string((char*)buf, 8, p);
  239. if (_PyFloat_Pack8(PyComplex_ImagAsDouble(v),
  240. buf, 1) < 0) {
  241. p->error = WFERR_UNMARSHALLABLE;
  242. return;
  243. }
  244. w_string((char*)buf, 8, p);
  245. }
  246. else {
  247. char *buf;
  248. w_byte(TYPE_COMPLEX, p);
  249. buf = PyOS_double_to_string(PyComplex_RealAsDouble(v),
  250. 'g', 17, 0, NULL);
  251. if (!buf) {
  252. p->error = WFERR_NOMEMORY;
  253. return;
  254. }
  255. n = strlen(buf);
  256. w_byte((int)n, p);
  257. w_string(buf, (int)n, p);
  258. PyMem_Free(buf);
  259. buf = PyOS_double_to_string(PyComplex_ImagAsDouble(v),
  260. 'g', 17, 0, NULL);
  261. if (!buf) {
  262. p->error = WFERR_NOMEMORY;
  263. return;
  264. }
  265. n = strlen(buf);
  266. w_byte((int)n, p);
  267. w_string(buf, (int)n, p);
  268. PyMem_Free(buf);
  269. }
  270. }
  271. #endif
  272. else if (PyString_CheckExact(v)) {
  273. if (p->strings && PyString_CHECK_INTERNED(v)) {
  274. PyObject *o = PyDict_GetItem(p->strings, v);
  275. if (o) {
  276. long w = PyInt_AsLong(o);
  277. w_byte(TYPE_STRINGREF, p);
  278. w_long(w, p);
  279. goto exit;
  280. }
  281. else {
  282. int ok;
  283. o = PyInt_FromSsize_t(PyDict_Size(p->strings));
  284. ok = o &&
  285. PyDict_SetItem(p->strings, v, o) >= 0;
  286. Py_XDECREF(o);
  287. if (!ok) {
  288. p->depth--;
  289. p->error = WFERR_UNMARSHALLABLE;
  290. return;
  291. }
  292. w_byte(TYPE_INTERNED, p);
  293. }
  294. }
  295. else {
  296. w_byte(TYPE_STRING, p);
  297. }
  298. n = PyString_GET_SIZE(v);
  299. if (n > INT_MAX) {
  300. /* huge strings are not supported */
  301. p->depth--;
  302. p->error = WFERR_UNMARSHALLABLE;
  303. return;
  304. }
  305. w_long((long)n, p);
  306. w_string(PyString_AS_STRING(v), (int)n, p);
  307. }
  308. #ifdef Py_USING_UNICODE
  309. else if (PyUnicode_CheckExact(v)) {
  310. PyObject *utf8;
  311. utf8 = PyUnicode_AsUTF8String(v);
  312. if (utf8 == NULL) {
  313. p->depth--;
  314. p->error = WFERR_UNMARSHALLABLE;
  315. return;
  316. }
  317. w_byte(TYPE_UNICODE, p);
  318. n = PyString_GET_SIZE(utf8);
  319. if (n > INT_MAX) {
  320. p->depth--;
  321. p->error = WFERR_UNMARSHALLABLE;
  322. return;
  323. }
  324. w_long((long)n, p);
  325. w_string(PyString_AS_STRING(utf8), (int)n, p);
  326. Py_DECREF(utf8);
  327. }
  328. #endif
  329. else if (PyTuple_CheckExact(v)) {
  330. w_byte(TYPE_TUPLE, p);
  331. n = PyTuple_Size(v);
  332. w_long((long)n, p);
  333. for (i = 0; i < n; i++) {
  334. w_object(PyTuple_GET_ITEM(v, i), p);
  335. }
  336. }
  337. else if (PyList_CheckExact(v)) {
  338. w_byte(TYPE_LIST, p);
  339. n = PyList_GET_SIZE(v);
  340. w_long((long)n, p);
  341. for (i = 0; i < n; i++) {
  342. w_object(PyList_GET_ITEM(v, i), p);
  343. }
  344. }
  345. else if (PyDict_CheckExact(v)) {
  346. Py_ssize_t pos;
  347. PyObject *key, *value;
  348. w_byte(TYPE_DICT, p);
  349. /* This one is NULL object terminated! */
  350. pos = 0;
  351. while (PyDict_Next(v, &pos, &key, &value)) {
  352. w_object(key, p);
  353. w_object(value, p);
  354. }
  355. w_object((PyObject *)NULL, p);
  356. }
  357. else if (PyAnySet_CheckExact(v)) {
  358. PyObject *value, *it;
  359. if (PyObject_TypeCheck(v, &PySet_Type))
  360. w_byte(TYPE_SET, p);
  361. else
  362. w_byte(TYPE_FROZENSET, p);
  363. n = PyObject_Size(v);
  364. if (n == -1) {
  365. p->depth--;
  366. p->error = WFERR_UNMARSHALLABLE;
  367. return;
  368. }
  369. w_long((long)n, p);
  370. it = PyObject_GetIter(v);
  371. if (it == NULL) {
  372. p->depth--;
  373. p->error = WFERR_UNMARSHALLABLE;
  374. return;
  375. }
  376. while ((value = PyIter_Next(it)) != NULL) {
  377. w_object(value, p);
  378. Py_DECREF(value);
  379. }
  380. Py_DECREF(it);
  381. if (PyErr_Occurred()) {
  382. p->depth--;
  383. p->error = WFERR_UNMARSHALLABLE;
  384. return;
  385. }
  386. }
  387. else if (PyCode_Check(v)) {
  388. PyCodeObject *co = (PyCodeObject *)v;
  389. w_byte(TYPE_CODE, p);
  390. w_long(co->co_argcount, p);
  391. w_long(co->co_nlocals, p);
  392. w_long(co->co_stacksize, p);
  393. w_long(co->co_flags, p);
  394. w_object(co->co_code, p);
  395. w_object(co->co_consts, p);
  396. w_object(co->co_names, p);
  397. w_object(co->co_varnames, p);
  398. w_object(co->co_freevars, p);
  399. w_object(co->co_cellvars, p);
  400. w_object(co->co_filename, p);
  401. w_object(co->co_name, p);
  402. w_long(co->co_firstlineno, p);
  403. w_object(co->co_lnotab, p);
  404. }
  405. else if (PyObject_CheckReadBuffer(v)) {
  406. /* Write unknown buffer-style objects as a string */
  407. char *s;
  408. PyBufferProcs *pb = v->ob_type->tp_as_buffer;
  409. w_byte(TYPE_STRING, p);
  410. n = (*pb->bf_getreadbuffer)(v, 0, (void **)&s);
  411. if (n > INT_MAX) {
  412. p->depth--;
  413. p->error = WFERR_UNMARSHALLABLE;
  414. return;
  415. }
  416. w_long((long)n, p);
  417. w_string(s, (int)n, p);
  418. }
  419. else {
  420. w_byte(TYPE_UNKNOWN, p);
  421. p->error = WFERR_UNMARSHALLABLE;
  422. }
  423. exit:
  424. p->depth--;
  425. }
  426. /* version currently has no effect for writing longs. */
  427. void
  428. PyMarshal_WriteLongToFile(long x, FILE *fp, int version)
  429. {
  430. WFILE wf;
  431. wf.fp = fp;
  432. wf.error = WFERR_OK;
  433. wf.depth = 0;
  434. wf.strings = NULL;
  435. wf.version = version;
  436. w_long(x, &wf);
  437. }
  438. void
  439. PyMarshal_WriteObjectToFile(PyObject *x, FILE *fp, int version)
  440. {
  441. WFILE wf;
  442. wf.fp = fp;
  443. wf.error = WFERR_OK;
  444. wf.depth = 0;
  445. wf.strings = (version > 0) ? PyDict_New() : NULL;
  446. wf.version = version;
  447. w_object(x, &wf);
  448. Py_XDECREF(wf.strings);
  449. }
  450. typedef WFILE RFILE; /* Same struct with different invariants */
  451. #define rs_byte(p) (((p)->ptr < (p)->end) ? (unsigned char)*(p)->ptr++ : EOF)
  452. #define r_byte(p) ((p)->fp ? getc((p)->fp) : rs_byte(p))
  453. static int
  454. r_string(char *s, int n, RFILE *p)
  455. {
  456. if (p->fp != NULL)
  457. /* The result fits into int because it must be <=n. */
  458. return (int)fread(s, 1, n, p->fp);
  459. if (p->end - p->ptr < n)
  460. n = (int)(p->end - p->ptr);
  461. memcpy(s, p->ptr, n);
  462. p->ptr += n;
  463. return n;
  464. }
  465. static int
  466. r_short(RFILE *p)
  467. {
  468. register short x;
  469. x = r_byte(p);
  470. x |= r_byte(p) << 8;
  471. /* Sign-extension, in case short greater than 16 bits */
  472. x |= -(x & 0x8000);
  473. return x;
  474. }
  475. static long
  476. r_long(RFILE *p)
  477. {
  478. register long x;
  479. register FILE *fp = p->fp;
  480. if (fp) {
  481. x = getc(fp);
  482. x |= (long)getc(fp) << 8;
  483. x |= (long)getc(fp) << 16;
  484. x |= (long)getc(fp) << 24;
  485. }
  486. else {
  487. x = rs_byte(p);
  488. x |= (long)rs_byte(p) << 8;
  489. x |= (long)rs_byte(p) << 16;
  490. x |= (long)rs_byte(p) << 24;
  491. }
  492. #if SIZEOF_LONG > 4
  493. /* Sign extension for 64-bit machines */
  494. x |= -(x & 0x80000000L);
  495. #endif
  496. return x;
  497. }
  498. /* r_long64 deals with the TYPE_INT64 code. On a machine with
  499. sizeof(long) > 4, it returns a Python int object, else a Python long
  500. object. Note that w_long64 writes out TYPE_INT if 32 bits is enough,
  501. so there's no inefficiency here in returning a PyLong on 32-bit boxes
  502. for everything written via TYPE_INT64 (i.e., if an int is written via
  503. TYPE_INT64, it *needs* more than 32 bits).
  504. */
  505. static PyObject *
  506. r_long64(RFILE *p)
  507. {
  508. long lo4 = r_long(p);
  509. long hi4 = r_long(p);
  510. #if SIZEOF_LONG > 4
  511. long x = (hi4 << 32) | (lo4 & 0xFFFFFFFFL);
  512. return PyInt_FromLong(x);
  513. #else
  514. unsigned char buf[8];
  515. int one = 1;
  516. int is_little_endian = (int)*(char*)&one;
  517. if (is_little_endian) {
  518. memcpy(buf, &lo4, 4);
  519. memcpy(buf+4, &hi4, 4);
  520. }
  521. else {
  522. memcpy(buf, &hi4, 4);
  523. memcpy(buf+4, &lo4, 4);
  524. }
  525. return _PyLong_FromByteArray(buf, 8, is_little_endian, 1);
  526. #endif
  527. }
  528. static PyObject *
  529. r_PyLong(RFILE *p)
  530. {
  531. PyLongObject *ob;
  532. int size, i, j, md, shorts_in_top_digit;
  533. long n;
  534. digit d;
  535. n = r_long(p);
  536. if (n == 0)
  537. return (PyObject *)_PyLong_New(0);
  538. if (n < -INT_MAX || n > INT_MAX) {
  539. PyErr_SetString(PyExc_ValueError,
  540. "bad marshal data (long size out of range)");
  541. return NULL;
  542. }
  543. size = 1 + (ABS(n) - 1) / PyLong_MARSHAL_RATIO;
  544. shorts_in_top_digit = 1 + (ABS(n) - 1) % PyLong_MARSHAL_RATIO;
  545. ob = _PyLong_New(size);
  546. if (ob == NULL)
  547. return NULL;
  548. Py_SIZE(ob) = n > 0 ? size : -size;
  549. for (i = 0; i < size-1; i++) {
  550. d = 0;
  551. for (j=0; j < PyLong_MARSHAL_RATIO; j++) {
  552. md = r_short(p);
  553. if (md < 0 || md > PyLong_MARSHAL_BASE)
  554. goto bad_digit;
  555. d += (digit)md << j*PyLong_MARSHAL_SHIFT;
  556. }
  557. ob->ob_digit[i] = d;
  558. }
  559. d = 0;
  560. for (j=0; j < shorts_in_top_digit; j++) {
  561. md = r_short(p);
  562. if (md < 0 || md > PyLong_MARSHAL_BASE)
  563. goto bad_digit;
  564. /* topmost marshal digit should be nonzero */
  565. if (md == 0 && j == shorts_in_top_digit - 1) {
  566. Py_DECREF(ob);
  567. PyErr_SetString(PyExc_ValueError,
  568. "bad marshal data (unnormalized long data)");
  569. return NULL;
  570. }
  571. d += (digit)md << j*PyLong_MARSHAL_SHIFT;
  572. }
  573. /* top digit should be nonzero, else the resulting PyLong won't be
  574. normalized */
  575. ob->ob_digit[size-1] = d;
  576. return (PyObject *)ob;
  577. bad_digit:
  578. Py_DECREF(ob);
  579. PyErr_SetString(PyExc_ValueError,
  580. "bad marshal data (digit out of range in long)");
  581. return NULL;
  582. }
  583. static PyObject *
  584. r_object(RFILE *p)
  585. {
  586. /* NULL is a valid return value, it does not necessarily means that
  587. an exception is set. */
  588. PyObject *v, *v2;
  589. long i, n;
  590. int type = r_byte(p);
  591. PyObject *retval;
  592. p->depth++;
  593. if (p->depth > MAX_MARSHAL_STACK_DEPTH) {
  594. p->depth--;
  595. PyErr_SetString(PyExc_ValueError, "recursion limit exceeded");
  596. return NULL;
  597. }
  598. switch (type) {
  599. case EOF:
  600. PyErr_SetString(PyExc_EOFError,
  601. "EOF read where object expected");
  602. retval = NULL;
  603. break;
  604. case TYPE_NULL:
  605. retval = NULL;
  606. break;
  607. case TYPE_NONE:
  608. Py_INCREF(Py_None);
  609. retval = Py_None;
  610. break;
  611. case TYPE_STOPITER:
  612. Py_INCREF(PyExc_StopIteration);
  613. retval = PyExc_StopIteration;
  614. break;
  615. case TYPE_ELLIPSIS:
  616. Py_INCREF(Py_Ellipsis);
  617. retval = Py_Ellipsis;
  618. break;
  619. case TYPE_FALSE:
  620. Py_INCREF(Py_False);
  621. retval = Py_False;
  622. break;
  623. case TYPE_TRUE:
  624. Py_INCREF(Py_True);
  625. retval = Py_True;
  626. break;
  627. case TYPE_INT:
  628. retval = PyInt_FromLong(r_long(p));
  629. break;
  630. case TYPE_INT64:
  631. retval = r_long64(p);
  632. break;
  633. case TYPE_LONG:
  634. retval = r_PyLong(p);
  635. break;
  636. case TYPE_FLOAT:
  637. {
  638. char buf[256];
  639. double dx;
  640. n = r_byte(p);
  641. if (n == EOF || r_string(buf, (int)n, p) != n) {
  642. PyErr_SetString(PyExc_EOFError,
  643. "EOF read where object expected");
  644. retval = NULL;
  645. break;
  646. }
  647. buf[n] = '\0';
  648. dx = PyOS_string_to_double(buf, NULL, NULL);
  649. if (dx == -1.0 && PyErr_Occurred()) {
  650. retval = NULL;
  651. break;
  652. }
  653. retval = PyFloat_FromDouble(dx);
  654. break;
  655. }
  656. case TYPE_BINARY_FLOAT:
  657. {
  658. unsigned char buf[8];
  659. double x;
  660. if (r_string((char*)buf, 8, p) != 8) {
  661. PyErr_SetString(PyExc_EOFError,
  662. "EOF read where object expected");
  663. retval = NULL;
  664. break;
  665. }
  666. x = _PyFloat_Unpack8(buf, 1);
  667. if (x == -1.0 && PyErr_Occurred()) {
  668. retval = NULL;
  669. break;
  670. }
  671. retval = PyFloat_FromDouble(x);
  672. break;
  673. }
  674. #ifndef WITHOUT_COMPLEX
  675. case TYPE_COMPLEX:
  676. {
  677. char buf[256];
  678. Py_complex c;
  679. n = r_byte(p);
  680. if (n == EOF || r_string(buf, (int)n, p) != n) {
  681. PyErr_SetString(PyExc_EOFError,
  682. "EOF read where object expected");
  683. retval = NULL;
  684. break;
  685. }
  686. buf[n] = '\0';
  687. c.real = PyOS_string_to_double(buf, NULL, NULL);
  688. if (c.real == -1.0 && PyErr_Occurred()) {
  689. retval = NULL;
  690. break;
  691. }
  692. n = r_byte(p);
  693. if (n == EOF || r_string(buf, (int)n, p) != n) {
  694. PyErr_SetString(PyExc_EOFError,
  695. "EOF read where object expected");
  696. retval = NULL;
  697. break;
  698. }
  699. buf[n] = '\0';
  700. c.imag = PyOS_string_to_double(buf, NULL, NULL);
  701. if (c.imag == -1.0 && PyErr_Occurred()) {
  702. retval = NULL;
  703. break;
  704. }
  705. retval = PyComplex_FromCComplex(c);
  706. break;
  707. }
  708. case TYPE_BINARY_COMPLEX:
  709. {
  710. unsigned char buf[8];
  711. Py_complex c;
  712. if (r_string((char*)buf, 8, p) != 8) {
  713. PyErr_SetString(PyExc_EOFError,
  714. "EOF read where object expected");
  715. retval = NULL;
  716. break;
  717. }
  718. c.real = _PyFloat_Unpack8(buf, 1);
  719. if (c.real == -1.0 && PyErr_Occurred()) {
  720. retval = NULL;
  721. break;
  722. }
  723. if (r_string((char*)buf, 8, p) != 8) {
  724. PyErr_SetString(PyExc_EOFError,
  725. "EOF read where object expected");
  726. retval = NULL;
  727. break;
  728. }
  729. c.imag = _PyFloat_Unpack8(buf, 1);
  730. if (c.imag == -1.0 && PyErr_Occurred()) {
  731. retval = NULL;
  732. break;
  733. }
  734. retval = PyComplex_FromCComplex(c);
  735. break;
  736. }
  737. #endif
  738. case TYPE_INTERNED:
  739. case TYPE_STRING:
  740. n = r_long(p);
  741. if (n < 0 || n > INT_MAX) {
  742. PyErr_SetString(PyExc_ValueError, "bad marshal data (string size out of range)");
  743. retval = NULL;
  744. break;
  745. }
  746. v = PyString_FromStringAndSize((char *)NULL, n);
  747. if (v == NULL) {
  748. retval = NULL;
  749. break;
  750. }
  751. if (r_string(PyString_AS_STRING(v), (int)n, p) != n) {
  752. Py_DECREF(v);
  753. PyErr_SetString(PyExc_EOFError,
  754. "EOF read where object expected");
  755. retval = NULL;
  756. break;
  757. }
  758. if (type == TYPE_INTERNED) {
  759. PyString_InternInPlace(&v);
  760. if (PyList_Append(p->strings, v) < 0) {
  761. retval = NULL;
  762. break;
  763. }
  764. }
  765. retval = v;
  766. break;
  767. case TYPE_STRINGREF:
  768. n = r_long(p);
  769. if (n < 0 || n >= PyList_GET_SIZE(p->strings)) {
  770. PyErr_SetString(PyExc_ValueError, "bad marshal data (string ref out of range)");
  771. retval = NULL;
  772. break;
  773. }
  774. v = PyList_GET_ITEM(p->strings, n);
  775. Py_INCREF(v);
  776. retval = v;
  777. break;
  778. #ifdef Py_USING_UNICODE
  779. case TYPE_UNICODE:
  780. {
  781. char *buffer;
  782. n = r_long(p);
  783. if (n < 0 || n > INT_MAX) {
  784. PyErr_SetString(PyExc_ValueError, "bad marshal data (unicode size out of range)");
  785. retval = NULL;
  786. break;
  787. }
  788. buffer = PyMem_NEW(char, n);
  789. if (buffer == NULL) {
  790. retval = PyErr_NoMemory();
  791. break;
  792. }
  793. if (r_string(buffer, (int)n, p) != n) {
  794. PyMem_DEL(buffer);
  795. PyErr_SetString(PyExc_EOFError,
  796. "EOF read where object expected");
  797. retval = NULL;
  798. break;
  799. }
  800. v = PyUnicode_DecodeUTF8(buffer, n, NULL);
  801. PyMem_DEL(buffer);
  802. retval = v;
  803. break;
  804. }
  805. #endif
  806. case TYPE_TUPLE:
  807. n = r_long(p);
  808. if (n < 0 || n > INT_MAX) {
  809. PyErr_SetString(PyExc_ValueError, "bad marshal data (tuple size out of range)");
  810. retval = NULL;
  811. break;
  812. }
  813. v = PyTuple_New((int)n);
  814. if (v == NULL) {
  815. retval = NULL;
  816. break;
  817. }
  818. for (i = 0; i < n; i++) {
  819. v2 = r_object(p);
  820. if ( v2 == NULL ) {
  821. if (!PyErr_Occurred())
  822. PyErr_SetString(PyExc_TypeError,
  823. "NULL object in marshal data for tuple");
  824. Py_DECREF(v);
  825. v = NULL;
  826. break;
  827. }
  828. PyTuple_SET_ITEM(v, (int)i, v2);
  829. }
  830. retval = v;
  831. break;
  832. case TYPE_LIST:
  833. n = r_long(p);
  834. if (n < 0 || n > INT_MAX) {
  835. PyErr_SetString(PyExc_ValueError, "bad marshal data (list size out of range)");
  836. retval = NULL;
  837. break;
  838. }
  839. v = PyList_New((int)n);
  840. if (v == NULL) {
  841. retval = NULL;
  842. break;
  843. }
  844. for (i = 0; i < n; i++) {
  845. v2 = r_object(p);
  846. if ( v2 == NULL ) {
  847. if (!PyErr_Occurred())
  848. PyErr_SetString(PyExc_TypeError,
  849. "NULL object in marshal data for list");
  850. Py_DECREF(v);
  851. v = NULL;
  852. break;
  853. }
  854. PyList_SET_ITEM(v, (int)i, v2);
  855. }
  856. retval = v;
  857. break;
  858. case TYPE_DICT:
  859. v = PyDict_New();
  860. if (v == NULL) {
  861. retval = NULL;
  862. break;
  863. }
  864. for (;;) {
  865. PyObject *key, *val;
  866. key = r_object(p);
  867. if (key == NULL)
  868. break;
  869. val = r_object(p);
  870. if (val != NULL)
  871. PyDict_SetItem(v, key, val);
  872. Py_DECREF(key);
  873. Py_XDECREF(val);
  874. }
  875. if (PyErr_Occurred()) {
  876. Py_DECREF(v);
  877. v = NULL;
  878. }
  879. retval = v;
  880. break;
  881. case TYPE_SET:
  882. case TYPE_FROZENSET:
  883. n = r_long(p);
  884. if (n < 0 || n > INT_MAX) {
  885. PyErr_SetString(PyExc_ValueError, "bad marshal data (set size out of range)");
  886. retval = NULL;
  887. break;
  888. }
  889. v = (type == TYPE_SET) ? PySet_New(NULL) : PyFrozenSet_New(NULL);
  890. if (v == NULL) {
  891. retval = NULL;
  892. break;
  893. }
  894. for (i = 0; i < n; i++) {
  895. v2 = r_object(p);
  896. if ( v2 == NULL ) {
  897. if (!PyErr_Occurred())
  898. PyErr_SetString(PyExc_TypeError,
  899. "NULL object in marshal data for set");
  900. Py_DECREF(v);
  901. v = NULL;
  902. break;
  903. }
  904. if (PySet_Add(v, v2) == -1) {
  905. Py_DECREF(v);
  906. Py_DECREF(v2);
  907. v = NULL;
  908. break;
  909. }
  910. Py_DECREF(v2);
  911. }
  912. retval = v;
  913. break;
  914. case TYPE_CODE:
  915. if (PyEval_GetRestricted()) {
  916. PyErr_SetString(PyExc_RuntimeError,
  917. "cannot unmarshal code objects in "
  918. "restricted execution mode");
  919. retval = NULL;
  920. break;
  921. }
  922. else {
  923. int argcount;
  924. int nlocals;
  925. int stacksize;
  926. int flags;
  927. PyObject *code = NULL;
  928. PyObject *consts = NULL;
  929. PyObject *names = NULL;
  930. PyObject *varnames = NULL;
  931. PyObject *freevars = NULL;
  932. PyObject *cellvars = NULL;
  933. PyObject *filename = NULL;
  934. PyObject *name = NULL;
  935. int firstlineno;
  936. PyObject *lnotab = NULL;
  937. v = NULL;
  938. /* XXX ignore long->int overflows for now */
  939. argcount = (int)r_long(p);
  940. nlocals = (int)r_long(p);
  941. stacksize = (int)r_long(p);
  942. flags = (int)r_long(p);
  943. code = r_object(p);
  944. if (code == NULL)
  945. goto code_error;
  946. consts = r_object(p);
  947. if (consts == NULL)
  948. goto code_error;
  949. names = r_object(p);
  950. if (names == NULL)
  951. goto code_error;
  952. varnames = r_object(p);
  953. if (varnames == NULL)
  954. goto code_error;
  955. freevars = r_object(p);
  956. if (freevars == NULL)
  957. goto code_error;
  958. cellvars = r_object(p);
  959. if (cellvars == NULL)
  960. goto code_error;
  961. filename = r_object(p);
  962. if (filename == NULL)
  963. goto code_error;
  964. name = r_object(p);
  965. if (name == NULL)
  966. goto code_error;
  967. firstlineno = (int)r_long(p);
  968. lnotab = r_object(p);
  969. if (lnotab == NULL)
  970. goto code_error;
  971. v = (PyObject *) PyCode_New(
  972. argcount, nlocals, stacksize, flags,
  973. code, consts, names, varnames,
  974. freevars, cellvars, filename, name,
  975. firstlineno, lnotab);
  976. code_error:
  977. Py_XDECREF(code);
  978. Py_XDECREF(consts);
  979. Py_XDECREF(names);
  980. Py_XDECREF(varnames);
  981. Py_XDECREF(freevars);
  982. Py_XDECREF(cellvars);
  983. Py_XDECREF(filename);
  984. Py_XDECREF(name);
  985. Py_XDECREF(lnotab);
  986. }
  987. retval = v;
  988. break;
  989. default:
  990. /* Bogus data got written, which isn't ideal.
  991. This will let you keep working and recover. */
  992. PyErr_SetString(PyExc_ValueError, "bad marshal data (unknown type code)");
  993. retval = NULL;
  994. break;
  995. }
  996. p->depth--;
  997. return retval;
  998. }
  999. static PyObject *
  1000. read_object(RFILE *p)
  1001. {
  1002. PyObject *v;
  1003. if (PyErr_Occurred()) {
  1004. fprintf(stderr, "XXX readobject called with exception set\n");
  1005. return NULL;
  1006. }
  1007. v = r_object(p);
  1008. if (v == NULL && !PyErr_Occurred())
  1009. PyErr_SetString(PyExc_TypeError, "NULL object in marshal data for object");
  1010. return v;
  1011. }
  1012. int
  1013. PyMarshal_ReadShortFromFile(FILE *fp)
  1014. {
  1015. RFILE rf;
  1016. assert(fp);
  1017. rf.fp = fp;
  1018. rf.strings = NULL;
  1019. rf.end = rf.ptr = NULL;
  1020. return r_short(&rf);
  1021. }
  1022. long
  1023. PyMarshal_ReadLongFromFile(FILE *fp)
  1024. {
  1025. RFILE rf;
  1026. rf.fp = fp;
  1027. rf.strings = NULL;
  1028. rf.ptr = rf.end = NULL;
  1029. return r_long(&rf);
  1030. }
  1031. #ifdef HAVE_FSTAT
  1032. /* Return size of file in bytes; < 0 if unknown. */
  1033. static off_t
  1034. getfilesize(FILE *fp)
  1035. {
  1036. struct stat st;
  1037. if (fstat(fileno(fp), &st) != 0)
  1038. return -1;
  1039. else
  1040. return st.st_size;
  1041. }
  1042. #endif
  1043. /* If we can get the size of the file up-front, and it's reasonably small,
  1044. * read it in one gulp and delegate to ...FromString() instead. Much quicker
  1045. * than reading a byte at a time from file; speeds .pyc imports.
  1046. * CAUTION: since this may read the entire remainder of the file, don't
  1047. * call it unless you know you're done with the file.
  1048. */
  1049. PyObject *
  1050. PyMarshal_ReadLastObjectFromFile(FILE *fp)
  1051. {
  1052. /* REASONABLE_FILE_LIMIT is by defn something big enough for Tkinter.pyc. */
  1053. #define REASONABLE_FILE_LIMIT (1L << 18)
  1054. #ifdef HAVE_FSTAT
  1055. off_t filesize;
  1056. filesize = getfilesize(fp);
  1057. if (filesize > 0 && filesize <= REASONABLE_FILE_LIMIT) {
  1058. char* pBuf = (char *)PyMem_MALLOC(filesize);
  1059. if (pBuf != NULL) {
  1060. PyObject* v;
  1061. size_t n;
  1062. /* filesize must fit into an int, because it
  1063. is smaller than REASONABLE_FILE_LIMIT */
  1064. n = fread(pBuf, 1, (int)filesize, fp);
  1065. v = PyMarshal_ReadObjectFromString(pBuf, n);
  1066. PyMem_FREE(pBuf);
  1067. return v;
  1068. }
  1069. }
  1070. #endif
  1071. /* We don't have fstat, or we do but the file is larger than
  1072. * REASONABLE_FILE_LIMIT or malloc failed -- read a byte at a time.
  1073. */
  1074. return PyMarshal_ReadObjectFromFile(fp);
  1075. #undef REASONABLE_FILE_LIMIT
  1076. }
  1077. PyObject *
  1078. PyMarshal_ReadObjectFromFile(FILE *fp)
  1079. {
  1080. RFILE rf;
  1081. PyObject *result;
  1082. rf.fp = fp;
  1083. rf.strings = PyList_New(0);
  1084. rf.depth = 0;
  1085. rf.ptr = rf.end = NULL;
  1086. result = r_object(&rf);
  1087. Py_DECREF(rf.strings);
  1088. return result;
  1089. }
  1090. PyObject *
  1091. PyMarshal_ReadObjectFromString(char *str, Py_ssize_t len)
  1092. {
  1093. RFILE rf;
  1094. PyObject *result;
  1095. rf.fp = NULL;
  1096. rf.ptr = str;
  1097. rf.end = str + len;
  1098. rf.strings = PyList_New(0);
  1099. rf.depth = 0;
  1100. result = r_object(&rf);
  1101. Py_DECREF(rf.strings);
  1102. return result;
  1103. }
  1104. static void
  1105. set_error(int error)
  1106. {
  1107. switch (error) {
  1108. case WFERR_NOMEMORY:
  1109. PyErr_NoMemory();
  1110. break;
  1111. case WFERR_UNMARSHALLABLE:
  1112. PyErr_SetString(PyExc_ValueError, "unmarshallable object");
  1113. break;
  1114. case WFERR_NESTEDTOODEEP:
  1115. default:
  1116. PyErr_SetString(PyExc_ValueError,
  1117. "object too deeply nested to marshal");
  1118. break;
  1119. }
  1120. }
  1121. PyObject *
  1122. PyMarshal_WriteObjectToString(PyObject *x, int version)
  1123. {
  1124. WFILE wf;
  1125. wf.fp = NULL;
  1126. wf.str = PyString_FromStringAndSize((char *)NULL, 50);
  1127. if (wf.str == NULL)
  1128. return NULL;
  1129. wf.ptr = PyString_AS_STRING((PyStringObject *)wf.str);
  1130. wf.end = wf.ptr + PyString_Size(wf.str);
  1131. wf.error = WFERR_OK;
  1132. wf.depth = 0;
  1133. wf.version = version;
  1134. wf.strings = (version > 0) ? PyDict_New() : NULL;
  1135. w_object(x, &wf);
  1136. Py_XDECREF(wf.strings);
  1137. if (wf.str != NULL) {
  1138. char *base = PyString_AS_STRING((PyStringObject *)wf.str);
  1139. if (wf.ptr - base > PY_SSIZE_T_MAX) {
  1140. Py_DECREF(wf.str);
  1141. PyErr_SetString(PyExc_OverflowError,
  1142. "too much marshall data for a string");
  1143. return NULL;
  1144. }
  1145. if (_PyString_Resize(&wf.str, (Py_ssize_t)(wf.ptr - base)))
  1146. return NULL;
  1147. }
  1148. if (wf.error != WFERR_OK) {
  1149. Py_XDECREF(wf.str);
  1150. set_error(wf.error);
  1151. return NULL;
  1152. }
  1153. return wf.str;
  1154. }
  1155. /* And an interface for Python programs... */
  1156. static PyObject *
  1157. marshal_dump(PyObject *self, PyObject *args)
  1158. {
  1159. WFILE wf;
  1160. PyObject *x;
  1161. PyObject *f;
  1162. int version = Py_MARSHAL_VERSION;
  1163. if (!PyArg_ParseTuple(args, "OO|i:dump", &x, &f, &version))
  1164. return NULL;
  1165. if (!PyFile_Check(f)) {
  1166. PyErr_SetString(PyExc_TypeError,
  1167. "marshal.dump() 2nd arg must be file");
  1168. return NULL;
  1169. }
  1170. wf.fp = PyFile_AsFile(f);
  1171. wf.str = NULL;
  1172. wf.ptr = wf.end = NULL;
  1173. wf.error = WFERR_OK;
  1174. wf.depth = 0;
  1175. wf.strings = (version > 0) ? PyDict_New() : 0;
  1176. wf.version = version;
  1177. w_object(x, &wf);
  1178. Py_XDECREF(wf.strings);
  1179. if (wf.error != WFERR_OK) {
  1180. set_error(wf.error);
  1181. return NULL;
  1182. }
  1183. Py_INCREF(Py_None);
  1184. return Py_None;
  1185. }
  1186. PyDoc_STRVAR(dump_doc,
  1187. "dump(value, file[, version])\n\
  1188. \n\
  1189. Write the value on the open file. The value must be a supported type.\n\
  1190. The file must be an open file object such as sys.stdout or returned by\n\
  1191. open() or os.popen(). It must be opened in binary mode ('wb' or 'w+b').\n\
  1192. \n\
  1193. If the value has (or contains an object that has) an unsupported type, a\n\
  1194. ValueError exception is raised but garbage data will also be written\n\
  1195. to the file. The object will not be properly read back by load()\n\
  1196. \n\
  1197. New in version 2.4: The version argument indicates the data format that\n\
  1198. dump should use.");
  1199. static PyObject *
  1200. marshal_load(PyObject *self, PyObject *f)
  1201. {
  1202. RFILE rf;
  1203. PyObject *result;
  1204. if (!PyFile_Check(f)) {
  1205. PyErr_SetString(PyExc_TypeError,
  1206. "marshal.load() arg must be file");
  1207. return NULL;
  1208. }
  1209. rf.fp = PyFile_AsFile(f);
  1210. rf.strings = PyList_New(0);
  1211. rf.depth = 0;
  1212. result = read_object(&rf);
  1213. Py_DECREF(rf.strings);
  1214. return result;
  1215. }
  1216. PyDoc_STRVAR(load_doc,
  1217. "load(file)\n\
  1218. \n\
  1219. Read one value from the open file and return it. If no valid value is\n\
  1220. read (e.g. because the data has a different Python versions\n\
  1221. incompatible marshal format), raise EOFError, ValueError or TypeError.\n\
  1222. The file must be an open file object opened in binary mode ('rb' or\n\
  1223. 'r+b').\n\
  1224. \n\
  1225. Note: If an object containing an unsupported type was marshalled with\n\
  1226. dump(), load() will substitute None for the unmarshallable type.");
  1227. static PyObject *
  1228. marshal_dumps(PyObject *self, PyObject *args)
  1229. {
  1230. PyObject *x;
  1231. int version = Py_MARSHAL_VERSION;
  1232. if (!PyArg_ParseTuple(args, "O|i:dumps", &x, &version))
  1233. return NULL;
  1234. return PyMarshal_WriteObjectToString(x, version);
  1235. }
  1236. PyDoc_STRVAR(dumps_doc,
  1237. "dumps(value[, version])\n\
  1238. \n\
  1239. Return the string that would be written to a file by dump(value, file).\n\
  1240. The value must be a supported type. Raise a ValueError exception if\n\
  1241. value has (or contains an object that has) an unsupported type.\n\
  1242. \n\
  1243. New in version 2.4: The version argument indicates the data format that\n\
  1244. dumps should use.");
  1245. static PyObject *
  1246. marshal_loads(PyObject *self, PyObject *args)
  1247. {
  1248. RFILE rf;
  1249. char *s;
  1250. Py_ssize_t n;
  1251. PyObject* result;
  1252. if (!PyArg_ParseTuple(args, "s#:loads", &s, &n))
  1253. return NULL;
  1254. rf.fp = NULL;
  1255. rf.ptr = s;
  1256. rf.end = s + n;
  1257. rf.strings = PyList_New(0);
  1258. rf.depth = 0;
  1259. result = read_object(&rf);
  1260. Py_DECREF(rf.strings);
  1261. return result;
  1262. }
  1263. PyDoc_STRVAR(loads_doc,
  1264. "loads(string)\n\
  1265. \n\
  1266. Convert the string to a value. If no valid value is found, raise\n\
  1267. EOFError, ValueError or TypeError. Extra characters in the string are\n\
  1268. ignored.");
  1269. static PyMethodDef marshal_methods[] = {
  1270. {"dump", marshal_dump, METH_VARARGS, dump_doc},
  1271. {"load", marshal_load, METH_O, load_doc},
  1272. {"dumps", marshal_dumps, METH_VARARGS, dumps_doc},
  1273. {"loads", marshal_loads, METH_VARARGS, loads_doc},
  1274. {NULL, NULL} /* sentinel */
  1275. };
  1276. PyDoc_STRVAR(marshal_doc,
  1277. "This module contains functions that can read and write Python values in\n\
  1278. a binary format. The format is specific to Python, but independent of\n\
  1279. machine architecture issues.\n\
  1280. \n\
  1281. Not all Python object types are supported; in general, only objects\n\
  1282. whose value is independent from a particular invocation of Python can be\n\
  1283. written and read by this module. The following types are supported:\n\
  1284. None, integers, long integers, floating point numbers, strings, Unicode\n\
  1285. objects, tuples, lists, sets, dictionaries, and code objects, where it\n\
  1286. should be understood that tuples, lists and dictionaries are only\n\
  1287. supported as long as the values contained therein are themselves\n\
  1288. supported; and recursive lists and dictionaries should not be written\n\
  1289. (they will cause infinite loops).\n\
  1290. \n\
  1291. Variables:\n\
  1292. \n\
  1293. version -- indicates the format that the module uses. Version 0 is the\n\
  1294. historical format, version 1 (added in Python 2.4) shares interned\n\
  1295. strings and version 2 (added in Python 2.5) uses a binary format for\n\
  1296. floating point numbers. (New in version 2.4)\n\
  1297. \n\
  1298. Functions:\n\
  1299. \n\
  1300. dump() -- write value to a file\n\
  1301. load() -- read value from a file\n\
  1302. dumps() -- write value to a string\n\
  1303. loads() -- read value from a string");
  1304. PyMODINIT_FUNC
  1305. PyMarshal_Init(void)
  1306. {
  1307. PyObject *mod = Py_InitModule3("marshal", marshal_methods,
  1308. marshal_doc);
  1309. if (mod == NULL)
  1310. return;
  1311. PyModule_AddIntConstant(mod, "version", Py_MARSHAL_VERSION);
  1312. }