Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(15)

Side by Side Diff: Python/marshal.c

Issue 10639: reindent.py converts newlines to platform default
Patch Set: Created 8 years, 8 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Python/makeopcodetargets.py ('k') | Python/modsupport.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 /* Write Python objects to files and read them back. 2 /* Write Python objects to files and read them back.
3 This is intended for writing and reading compiled Python code only; 3 This is intended for writing and reading compiled Python code only;
4 a true persistent storage facility would be much harder, since 4 a true persistent storage facility would be much harder, since
5 it would have to take circular links and sharing into account. */ 5 it would have to take circular links and sharing into account. */
6 6
7 #define PY_SSIZE_T_CLEAN 7 #define PY_SSIZE_T_CLEAN
8 8
9 #include "Python.h" 9 #include "Python.h"
10 #include "longintrepr.h" 10 #include "longintrepr.h"
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
50 #define WFERR_OK 0 50 #define WFERR_OK 0
51 #define WFERR_UNMARSHALLABLE 1 51 #define WFERR_UNMARSHALLABLE 1
52 #define WFERR_NESTEDTOODEEP 2 52 #define WFERR_NESTEDTOODEEP 2
53 #define WFERR_NOMEMORY 3 53 #define WFERR_NOMEMORY 3
54 54
55 typedef struct { 55 typedef struct {
56 FILE *fp; 56 FILE *fp;
57 int error; /* see WFERR_* values */ 57 int error; /* see WFERR_* values */
58 int depth; 58 int depth;
59 /* If fp == NULL, the following are valid: */ 59 /* If fp == NULL, the following are valid: */
60 PyObject * readable; /* Stream-like object being read from */
61 PyObject *str; 60 PyObject *str;
62 PyObject *current_filename; 61 PyObject *current_filename;
63 char *ptr; 62 char *ptr;
64 char *end; 63 char *end;
65 int version; 64 int version;
66 } WFILE; 65 } WFILE;
67 66
68 #define w_byte(c, p) if (((p)->fp)) putc((c), (p)->fp); \ 67 #define w_byte(c, p) if (((p)->fp)) putc((c), (p)->fp); \
69 else if ((p)->ptr != (p)->end) *(p)->ptr++ = (c); \ 68 else if ((p)->ptr != (p)->end) *(p)->ptr++ = (c); \
70 else w_more(c, p) 69 else w_more(c, p)
(...skipping 385 matching lines...) Expand 10 before | Expand all | Expand 10 after
456 wf.fp = fp; 455 wf.fp = fp;
457 wf.error = WFERR_OK; 456 wf.error = WFERR_OK;
458 wf.depth = 0; 457 wf.depth = 0;
459 wf.version = version; 458 wf.version = version;
460 w_object(x, &wf); 459 w_object(x, &wf);
461 } 460 }
462 461
463 typedef WFILE RFILE; /* Same struct with different invariants */ 462 typedef WFILE RFILE; /* Same struct with different invariants */
464 463
465 #define rs_byte(p) (((p)->ptr < (p)->end) ? (unsigned char)*(p)->ptr++ : EOF) 464 #define rs_byte(p) (((p)->ptr < (p)->end) ? (unsigned char)*(p)->ptr++ : EOF)
465
466 #define r_byte(p) ((p)->fp ? getc((p)->fp) : rs_byte(p))
466 467
467 static int 468 static int
468 r_string(char *s, int n, RFILE *p) 469 r_string(char *s, int n, RFILE *p)
469 { 470 {
470 char * ptr; 471 if (p->fp != NULL)
471 int read, left; 472 /* The result fits into int because it must be <=n. */
472 473 return (int)fread(s, 1, n, p->fp);
473 if (!p->readable) { 474 if (p->end - p->ptr < n)
474 if (p->fp != NULL) 475 n = (int)(p->end - p->ptr);
475 /* The result fits into int because it must be <=n. */ 476 memcpy(s, p->ptr, n);
476 read = (int) fread(s, 1, n, p->fp); 477 p->ptr += n;
477 else { 478 return n;
478 left = (int)(p->end - p->ptr);
479 read = (left < n) ? left : n;
480 memcpy(s, p->ptr, read);
481 p->ptr += read;
482 }
483 }
484 else {
485 PyObject *data = PyObject_CallMethod(p->readable, "read", "i", n);
486 read = 0;
487 if (data != NULL) {
488 if (!PyBytes_Check(data)) {
489 PyErr_Format(PyExc_TypeError,
490 "f.read() returned not bytes but %.100s",
491 data->ob_type->tp_name);
492 }
493 else {
494 read = PyBytes_GET_SIZE(data);
495 if (read > 0) {
496 ptr = PyBytes_AS_STRING(data);
497 memcpy(s, ptr, read);
498 }
499 }
500 Py_DECREF(data);
501 }
502 }
503 if (!PyErr_Occurred() && (read < n)) {
504 PyErr_SetString(PyExc_EOFError, "EOF read where not expected");
505 }
506 return read;
507 }
508
509
510 static int
511 r_byte(RFILE *p)
512 {
513 int c = EOF;
514 unsigned char ch;
515 int n;
516
517 if (!p->readable)
518 c = p->fp ? getc(p->fp) : rs_byte(p);
519 else {
520 n = r_string((char *) &ch, 1, p);
521 if (n > 0)
522 c = ch;
523 }
524 return c;
525 } 479 }
526 480
527 static int 481 static int
528 r_short(RFILE *p) 482 r_short(RFILE *p)
529 { 483 {
530 register short x; 484 register short x;
531 unsigned char buffer[2]; 485 x = r_byte(p);
532 486 x |= r_byte(p) << 8;
533 r_string((char *) buffer, 2, p);
534 x = buffer[0];
535 x |= buffer[1] << 8;
536 /* Sign-extension, in case short greater than 16 bits */ 487 /* Sign-extension, in case short greater than 16 bits */
537 x |= -(x & 0x8000); 488 x |= -(x & 0x8000);
538 return x; 489 return x;
539 } 490 }
540 491
541 static long 492 static long
542 r_long(RFILE *p) 493 r_long(RFILE *p)
543 { 494 {
544 register long x; 495 register long x;
545 unsigned char buffer[4]; 496 register FILE *fp = p->fp;
546 497 if (fp) {
547 r_string((char *) buffer, 4, p); 498 x = getc(fp);
548 x = buffer[0]; 499 x |= (long)getc(fp) << 8;
549 x |= (long)buffer[1] << 8; 500 x |= (long)getc(fp) << 16;
550 x |= (long)buffer[2] << 16; 501 x |= (long)getc(fp) << 24;
551 x |= (long)buffer[3] << 24; 502 }
503 else {
504 x = rs_byte(p);
505 x |= (long)rs_byte(p) << 8;
506 x |= (long)rs_byte(p) << 16;
507 x |= (long)rs_byte(p) << 24;
508 }
552 #if SIZEOF_LONG > 4 509 #if SIZEOF_LONG > 4
553 /* Sign extension for 64-bit machines */ 510 /* Sign extension for 64-bit machines */
554 x |= -(x & 0x80000000L); 511 x |= -(x & 0x80000000L);
555 #endif 512 #endif
556 return x; 513 return x;
557 } 514 }
558 515
559 /* r_long64 deals with the TYPE_INT64 code. On a machine with 516 /* r_long64 deals with the TYPE_INT64 code. On a machine with
560 sizeof(long) > 4, it returns a Python int object, else a Python long 517 sizeof(long) > 4, it returns a Python int object, else a Python long
561 object. Note that w_long64 writes out TYPE_INT if 32 bits is enough, 518 object. Note that w_long64 writes out TYPE_INT if 32 bits is enough,
562 so there's no inefficiency here in returning a PyLong on 32-bit boxes 519 so there's no inefficiency here in returning a PyLong on 32-bit boxes
563 for everything written via TYPE_INT64 (i.e., if an int is written via 520 for everything written via TYPE_INT64 (i.e., if an int is written via
564 TYPE_INT64, it *needs* more than 32 bits). 521 TYPE_INT64, it *needs* more than 32 bits).
565 */ 522 */
566 static PyObject * 523 static PyObject *
567 r_long64(RFILE *p) 524 r_long64(RFILE *p)
568 { 525 {
569 PyObject * result = NULL;
570 long lo4 = r_long(p); 526 long lo4 = r_long(p);
571 long hi4 = r_long(p); 527 long hi4 = r_long(p);
572
573 if (!PyErr_Occurred()) {
574 #if SIZEOF_LONG > 4 528 #if SIZEOF_LONG > 4
575 long x = (hi4 << 32) | (lo4 & 0xFFFFFFFFL); 529 long x = (hi4 << 32) | (lo4 & 0xFFFFFFFFL);
576 result = PyLong_FromLong(x); 530 return PyLong_FromLong(x);
577 #else 531 #else
578 unsigned char buf[8]; 532 unsigned char buf[8];
579 int one = 1; 533 int one = 1;
580 int is_little_endian = (int)*(char*)&one; 534 int is_little_endian = (int)*(char*)&one;
581 if (is_little_endian) { 535 if (is_little_endian) {
582 memcpy(buf, &lo4, 4); 536 memcpy(buf, &lo4, 4);
583 memcpy(buf+4, &hi4, 4); 537 memcpy(buf+4, &hi4, 4);
584 } 538 }
585 else { 539 else {
586 memcpy(buf, &hi4, 4); 540 memcpy(buf, &hi4, 4);
587 memcpy(buf+4, &lo4, 4); 541 memcpy(buf+4, &lo4, 4);
588 } 542 }
589 result = _PyLong_FromByteArray(buf, 8, is_little_endian, 1); 543 return _PyLong_FromByteArray(buf, 8, is_little_endian, 1);
590 #endif 544 #endif
591 }
592 return result;
593 } 545 }
594 546
595 static PyObject * 547 static PyObject *
596 r_PyLong(RFILE *p) 548 r_PyLong(RFILE *p)
597 { 549 {
598 PyLongObject *ob; 550 PyLongObject *ob;
599 int size, i, j, md, shorts_in_top_digit; 551 int size, i, j, md, shorts_in_top_digit;
600 long n; 552 long n;
601 digit d; 553 digit d;
602 554
603 n = r_long(p); 555 n = r_long(p);
604 if (PyErr_Occurred())
605 return NULL;
606 if (n == 0) 556 if (n == 0)
607 return (PyObject *)_PyLong_New(0); 557 return (PyObject *)_PyLong_New(0);
608 if (n < -INT_MAX || n > INT_MAX) { 558 if (n < -INT_MAX || n > INT_MAX) {
609 PyErr_SetString(PyExc_ValueError, 559 PyErr_SetString(PyExc_ValueError,
610 "bad marshal data (long size out of range)"); 560 "bad marshal data (long size out of range)");
611 return NULL; 561 return NULL;
612 } 562 }
613 563
614 size = 1 + (ABS(n) - 1) / PyLong_MARSHAL_RATIO; 564 size = 1 + (ABS(n) - 1) / PyLong_MARSHAL_RATIO;
615 shorts_in_top_digit = 1 + (ABS(n) - 1) % PyLong_MARSHAL_RATIO; 565 shorts_in_top_digit = 1 + (ABS(n) - 1) % PyLong_MARSHAL_RATIO;
616 ob = _PyLong_New(size); 566 ob = _PyLong_New(size);
617 if (ob == NULL) 567 if (ob == NULL)
618 return NULL; 568 return NULL;
619 Py_SIZE(ob) = n > 0 ? size : -size; 569 Py_SIZE(ob) = n > 0 ? size : -size;
620 570
621 for (i = 0; i < size-1; i++) { 571 for (i = 0; i < size-1; i++) {
622 d = 0; 572 d = 0;
623 for (j=0; j < PyLong_MARSHAL_RATIO; j++) { 573 for (j=0; j < PyLong_MARSHAL_RATIO; j++) {
624 md = r_short(p); 574 md = r_short(p);
625 if (PyErr_Occurred())
626 break;
627 if (md < 0 || md > PyLong_MARSHAL_BASE) 575 if (md < 0 || md > PyLong_MARSHAL_BASE)
628 goto bad_digit; 576 goto bad_digit;
629 d += (digit)md << j*PyLong_MARSHAL_SHIFT; 577 d += (digit)md << j*PyLong_MARSHAL_SHIFT;
630 } 578 }
631 ob->ob_digit[i] = d; 579 ob->ob_digit[i] = d;
632 } 580 }
633 d = 0; 581 d = 0;
634 for (j=0; j < shorts_in_top_digit; j++) { 582 for (j=0; j < shorts_in_top_digit; j++) {
635 md = r_short(p); 583 md = r_short(p);
636 if (PyErr_Occurred())
637 break;
638 if (md < 0 || md > PyLong_MARSHAL_BASE) 584 if (md < 0 || md > PyLong_MARSHAL_BASE)
639 goto bad_digit; 585 goto bad_digit;
640 /* topmost marshal digit should be nonzero */ 586 /* topmost marshal digit should be nonzero */
641 if (md == 0 && j == shorts_in_top_digit - 1) { 587 if (md == 0 && j == shorts_in_top_digit - 1) {
642 Py_DECREF(ob); 588 Py_DECREF(ob);
643 PyErr_SetString(PyExc_ValueError, 589 PyErr_SetString(PyExc_ValueError,
644 "bad marshal data (unnormalized long data)"); 590 "bad marshal data (unnormalized long data)");
645 return NULL; 591 return NULL;
646 } 592 }
647 d += (digit)md << j*PyLong_MARSHAL_SHIFT; 593 d += (digit)md << j*PyLong_MARSHAL_SHIFT;
648 }
649 if (PyErr_Occurred()) {
650 Py_DECREF(ob);
651 return NULL;
652 } 594 }
653 /* top digit should be nonzero, else the resulting PyLong won't be 595 /* top digit should be nonzero, else the resulting PyLong won't be
654 normalized */ 596 normalized */
655 ob->ob_digit[size-1] = d; 597 ob->ob_digit[size-1] = d;
656 return (PyObject *)ob; 598 return (PyObject *)ob;
657 bad_digit: 599 bad_digit:
658 Py_DECREF(ob); 600 Py_DECREF(ob);
659 PyErr_SetString(PyExc_ValueError, 601 PyErr_SetString(PyExc_ValueError,
660 "bad marshal data (digit out of range in long)"); 602 "bad marshal data (digit out of range in long)");
661 return NULL; 603 return NULL;
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
711 Py_INCREF(Py_False); 653 Py_INCREF(Py_False);
712 retval = Py_False; 654 retval = Py_False;
713 break; 655 break;
714 656
715 case TYPE_TRUE: 657 case TYPE_TRUE:
716 Py_INCREF(Py_True); 658 Py_INCREF(Py_True);
717 retval = Py_True; 659 retval = Py_True;
718 break; 660 break;
719 661
720 case TYPE_INT: 662 case TYPE_INT:
721 n = r_long(p); 663 retval = PyLong_FromLong(r_long(p));
722 retval = PyErr_Occurred() ? NULL : PyLong_FromLong(n);
723 break; 664 break;
724 665
725 case TYPE_INT64: 666 case TYPE_INT64:
726 retval = r_long64(p); 667 retval = r_long64(p);
727 break; 668 break;
728 669
729 case TYPE_LONG: 670 case TYPE_LONG:
730 retval = r_PyLong(p); 671 retval = r_PyLong(p);
731 break; 672 break;
732 673
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
822 if (c.imag == -1.0 && PyErr_Occurred()) { 763 if (c.imag == -1.0 && PyErr_Occurred()) {
823 retval = NULL; 764 retval = NULL;
824 break; 765 break;
825 } 766 }
826 retval = PyComplex_FromCComplex(c); 767 retval = PyComplex_FromCComplex(c);
827 break; 768 break;
828 } 769 }
829 770
830 case TYPE_STRING: 771 case TYPE_STRING:
831 n = r_long(p); 772 n = r_long(p);
832 if (PyErr_Occurred()) {
833 retval = NULL;
834 break;
835 }
836 if (n < 0 || n > INT_MAX) { 773 if (n < 0 || n > INT_MAX) {
837 PyErr_SetString(PyExc_ValueError, "bad marshal data (string size out of range)"); 774 PyErr_SetString(PyExc_ValueError, "bad marshal data (string size out of range)");
838 retval = NULL; 775 retval = NULL;
839 break; 776 break;
840 } 777 }
841 v = PyBytes_FromStringAndSize((char *)NULL, n); 778 v = PyBytes_FromStringAndSize((char *)NULL, n);
842 if (v == NULL) { 779 if (v == NULL) {
843 retval = NULL; 780 retval = NULL;
844 break; 781 break;
845 } 782 }
846 if (r_string(PyBytes_AS_STRING(v), (int)n, p) != n) { 783 if (r_string(PyBytes_AS_STRING(v), (int)n, p) != n) {
847 Py_DECREF(v); 784 Py_DECREF(v);
848 PyErr_SetString(PyExc_EOFError, 785 PyErr_SetString(PyExc_EOFError,
849 "EOF read where object expected"); 786 "EOF read where object expected");
850 retval = NULL; 787 retval = NULL;
851 break; 788 break;
852 } 789 }
853 retval = v; 790 retval = v;
854 break; 791 break;
855 792
856 case TYPE_UNICODE: 793 case TYPE_UNICODE:
857 { 794 {
858 char *buffer; 795 char *buffer;
859 796
860 n = r_long(p); 797 n = r_long(p);
861 if (PyErr_Occurred()) {
862 retval = NULL;
863 break;
864 }
865 if (n < 0 || n > INT_MAX) { 798 if (n < 0 || n > INT_MAX) {
866 PyErr_SetString(PyExc_ValueError, "bad marshal data (unicode size ou t of range)"); 799 PyErr_SetString(PyExc_ValueError, "bad marshal data (unicode size ou t of range)");
867 retval = NULL; 800 retval = NULL;
868 break; 801 break;
869 } 802 }
870 buffer = PyMem_NEW(char, n); 803 buffer = PyMem_NEW(char, n);
871 if (buffer == NULL) { 804 if (buffer == NULL) {
872 retval = PyErr_NoMemory(); 805 retval = PyErr_NoMemory();
873 break; 806 break;
874 } 807 }
875 if (r_string(buffer, (int)n, p) != n) { 808 if (r_string(buffer, (int)n, p) != n) {
876 PyMem_DEL(buffer); 809 PyMem_DEL(buffer);
877 PyErr_SetString(PyExc_EOFError, 810 PyErr_SetString(PyExc_EOFError,
878 "EOF read where object expected"); 811 "EOF read where object expected");
879 retval = NULL; 812 retval = NULL;
880 break; 813 break;
881 } 814 }
882 v = PyUnicode_DecodeUTF8(buffer, n, "surrogatepass"); 815 v = PyUnicode_DecodeUTF8(buffer, n, "surrogatepass");
883 PyMem_DEL(buffer); 816 PyMem_DEL(buffer);
884 retval = v; 817 retval = v;
885 break; 818 break;
886 } 819 }
887 820
888 case TYPE_TUPLE: 821 case TYPE_TUPLE:
889 n = r_long(p); 822 n = r_long(p);
890 if (PyErr_Occurred()) {
891 retval = NULL;
892 break;
893 }
894 if (n < 0 || n > INT_MAX) { 823 if (n < 0 || n > INT_MAX) {
895 PyErr_SetString(PyExc_ValueError, "bad marshal data (tuple size out of range)"); 824 PyErr_SetString(PyExc_ValueError, "bad marshal data (tuple size out of range)");
896 retval = NULL; 825 retval = NULL;
897 break; 826 break;
898 } 827 }
899 v = PyTuple_New((int)n); 828 v = PyTuple_New((int)n);
900 if (v == NULL) { 829 if (v == NULL) {
901 retval = NULL; 830 retval = NULL;
902 break; 831 break;
903 } 832 }
904 for (i = 0; i < n; i++) { 833 for (i = 0; i < n; i++) {
905 v2 = r_object(p); 834 v2 = r_object(p);
906 if ( v2 == NULL ) { 835 if ( v2 == NULL ) {
907 if (!PyErr_Occurred()) 836 if (!PyErr_Occurred())
908 PyErr_SetString(PyExc_TypeError, 837 PyErr_SetString(PyExc_TypeError,
909 "NULL object in marshal data for tuple"); 838 "NULL object in marshal data for tuple");
910 Py_DECREF(v); 839 Py_DECREF(v);
911 v = NULL; 840 v = NULL;
912 break; 841 break;
913 } 842 }
914 PyTuple_SET_ITEM(v, (int)i, v2); 843 PyTuple_SET_ITEM(v, (int)i, v2);
915 } 844 }
916 retval = v; 845 retval = v;
917 break; 846 break;
918 847
919 case TYPE_LIST: 848 case TYPE_LIST:
920 n = r_long(p); 849 n = r_long(p);
921 if (PyErr_Occurred()) {
922 retval = NULL;
923 break;
924 }
925 if (n < 0 || n > INT_MAX) { 850 if (n < 0 || n > INT_MAX) {
926 PyErr_SetString(PyExc_ValueError, "bad marshal data (list size out o f range)"); 851 PyErr_SetString(PyExc_ValueError, "bad marshal data (list size out o f range)");
927 retval = NULL; 852 retval = NULL;
928 break; 853 break;
929 } 854 }
930 v = PyList_New((int)n); 855 v = PyList_New((int)n);
931 if (v == NULL) { 856 if (v == NULL) {
932 retval = NULL; 857 retval = NULL;
933 break; 858 break;
934 } 859 }
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
967 if (PyErr_Occurred()) { 892 if (PyErr_Occurred()) {
968 Py_DECREF(v); 893 Py_DECREF(v);
969 v = NULL; 894 v = NULL;
970 } 895 }
971 retval = v; 896 retval = v;
972 break; 897 break;
973 898
974 case TYPE_SET: 899 case TYPE_SET:
975 case TYPE_FROZENSET: 900 case TYPE_FROZENSET:
976 n = r_long(p); 901 n = r_long(p);
977 if (PyErr_Occurred()) {
978 retval = NULL;
979 break;
980 }
981 if (n < 0 || n > INT_MAX) { 902 if (n < 0 || n > INT_MAX) {
982 PyErr_SetString(PyExc_ValueError, "bad marshal data (set size out of range)"); 903 PyErr_SetString(PyExc_ValueError, "bad marshal data (set size out of range)");
983 retval = NULL; 904 retval = NULL;
984 break; 905 break;
985 } 906 }
986 v = (type == TYPE_SET) ? PySet_New(NULL) : PyFrozenSet_New(NULL); 907 v = (type == TYPE_SET) ? PySet_New(NULL) : PyFrozenSet_New(NULL);
987 if (v == NULL) { 908 if (v == NULL) {
988 retval = NULL; 909 retval = NULL;
989 break; 910 break;
990 } 911 }
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
1024 PyObject *cellvars = NULL; 945 PyObject *cellvars = NULL;
1025 PyObject *filename = NULL; 946 PyObject *filename = NULL;
1026 PyObject *name = NULL; 947 PyObject *name = NULL;
1027 int firstlineno; 948 int firstlineno;
1028 PyObject *lnotab = NULL; 949 PyObject *lnotab = NULL;
1029 950
1030 v = NULL; 951 v = NULL;
1031 952
1032 /* XXX ignore long->int overflows for now */ 953 /* XXX ignore long->int overflows for now */
1033 argcount = (int)r_long(p); 954 argcount = (int)r_long(p);
1034 if (PyErr_Occurred())
1035 goto code_error;
1036 kwonlyargcount = (int)r_long(p); 955 kwonlyargcount = (int)r_long(p);
1037 if (PyErr_Occurred())
1038 goto code_error;
1039 nlocals = (int)r_long(p); 956 nlocals = (int)r_long(p);
1040 if (PyErr_Occurred())
1041 goto code_error;
1042 stacksize = (int)r_long(p); 957 stacksize = (int)r_long(p);
1043 if (PyErr_Occurred())
1044 goto code_error;
1045 flags = (int)r_long(p); 958 flags = (int)r_long(p);
1046 if (PyErr_Occurred())
1047 goto code_error;
1048 code = r_object(p); 959 code = r_object(p);
1049 if (code == NULL) 960 if (code == NULL)
1050 goto code_error; 961 goto code_error;
1051 consts = r_object(p); 962 consts = r_object(p);
1052 if (consts == NULL) 963 if (consts == NULL)
1053 goto code_error; 964 goto code_error;
1054 names = r_object(p); 965 names = r_object(p);
1055 if (names == NULL) 966 if (names == NULL)
1056 goto code_error; 967 goto code_error;
1057 varnames = r_object(p); 968 varnames = r_object(p);
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
1131 if (v == NULL && !PyErr_Occurred()) 1042 if (v == NULL && !PyErr_Occurred())
1132 PyErr_SetString(PyExc_TypeError, "NULL object in marshal data for object "); 1043 PyErr_SetString(PyExc_TypeError, "NULL object in marshal data for object ");
1133 return v; 1044 return v;
1134 } 1045 }
1135 1046
1136 int 1047 int
1137 PyMarshal_ReadShortFromFile(FILE *fp) 1048 PyMarshal_ReadShortFromFile(FILE *fp)
1138 { 1049 {
1139 RFILE rf; 1050 RFILE rf;
1140 assert(fp); 1051 assert(fp);
1141 rf.readable = NULL;
1142 rf.fp = fp; 1052 rf.fp = fp;
1143 rf.current_filename = NULL; 1053 rf.current_filename = NULL;
1144 rf.end = rf.ptr = NULL; 1054 rf.end = rf.ptr = NULL;
1145 return r_short(&rf); 1055 return r_short(&rf);
1146 } 1056 }
1147 1057
1148 long 1058 long
1149 PyMarshal_ReadLongFromFile(FILE *fp) 1059 PyMarshal_ReadLongFromFile(FILE *fp)
1150 { 1060 {
1151 RFILE rf; 1061 RFILE rf;
1152 rf.fp = fp; 1062 rf.fp = fp;
1153 rf.readable = NULL;
1154 rf.current_filename = NULL; 1063 rf.current_filename = NULL;
1155 rf.ptr = rf.end = NULL; 1064 rf.ptr = rf.end = NULL;
1156 return r_long(&rf); 1065 return r_long(&rf);
1157 } 1066 }
1158 1067
1159 #ifdef HAVE_FSTAT 1068 #ifdef HAVE_FSTAT
1160 /* Return size of file in bytes; < 0 if unknown. */ 1069 /* Return size of file in bytes; < 0 if unknown. */
1161 static off_t 1070 static off_t
1162 getfilesize(FILE *fp) 1071 getfilesize(FILE *fp)
1163 { 1072 {
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
1205 1114
1206 #undef REASONABLE_FILE_LIMIT 1115 #undef REASONABLE_FILE_LIMIT
1207 } 1116 }
1208 1117
1209 PyObject * 1118 PyObject *
1210 PyMarshal_ReadObjectFromFile(FILE *fp) 1119 PyMarshal_ReadObjectFromFile(FILE *fp)
1211 { 1120 {
1212 RFILE rf; 1121 RFILE rf;
1213 PyObject *result; 1122 PyObject *result;
1214 rf.fp = fp; 1123 rf.fp = fp;
1215 rf.readable = NULL;
1216 rf.current_filename = NULL; 1124 rf.current_filename = NULL;
1217 rf.depth = 0; 1125 rf.depth = 0;
1218 rf.ptr = rf.end = NULL; 1126 rf.ptr = rf.end = NULL;
1219 result = r_object(&rf); 1127 result = r_object(&rf);
1220 return result; 1128 return result;
1221 } 1129 }
1222 1130
1223 PyObject * 1131 PyObject *
1224 PyMarshal_ReadObjectFromString(char *str, Py_ssize_t len) 1132 PyMarshal_ReadObjectFromString(char *str, Py_ssize_t len)
1225 { 1133 {
1226 RFILE rf; 1134 RFILE rf;
1227 PyObject *result; 1135 PyObject *result;
1228 rf.fp = NULL; 1136 rf.fp = NULL;
1229 rf.readable = NULL;
1230 rf.current_filename = NULL; 1137 rf.current_filename = NULL;
1231 rf.ptr = str; 1138 rf.ptr = str;
1232 rf.end = str + len; 1139 rf.end = str + len;
1233 rf.depth = 0; 1140 rf.depth = 0;
1234 result = r_object(&rf); 1141 result = r_object(&rf);
1235 return result; 1142 return result;
1236 } 1143 }
1237 1144
1238 PyObject * 1145 PyObject *
1239 PyMarshal_WriteObjectToString(PyObject *x, int version) 1146 PyMarshal_WriteObjectToString(PyObject *x, int version)
1240 { 1147 {
1241 WFILE wf; 1148 WFILE wf;
1242 PyObject *res = NULL; 1149 PyObject *res = NULL;
1243 1150
1244 wf.fp = NULL; 1151 wf.fp = NULL;
1245 wf.readable = NULL;
1246 wf.str = PyBytes_FromStringAndSize((char *)NULL, 50); 1152 wf.str = PyBytes_FromStringAndSize((char *)NULL, 50);
1247 if (wf.str == NULL) 1153 if (wf.str == NULL)
1248 return NULL; 1154 return NULL;
1249 wf.ptr = PyBytes_AS_STRING((PyBytesObject *)wf.str); 1155 wf.ptr = PyBytes_AS_STRING((PyBytesObject *)wf.str);
1250 wf.end = wf.ptr + PyBytes_Size(wf.str); 1156 wf.end = wf.ptr + PyBytes_Size(wf.str);
1251 wf.error = WFERR_OK; 1157 wf.error = WFERR_OK;
1252 wf.depth = 0; 1158 wf.depth = 0;
1253 wf.version = version; 1159 wf.version = version;
1254 w_object(x, &wf); 1160 w_object(x, &wf);
1255 if (wf.str != NULL) { 1161 if (wf.str != NULL) {
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
1311 \n\ 1217 \n\
1312 If the value has (or contains an object that has) an unsupported type, a\n\ 1218 If the value has (or contains an object that has) an unsupported type, a\n\
1313 ValueError exception is raised — but garbage data will also be written\n\ 1219 ValueError exception is raised — but garbage data will also be written\n\
1314 to the file. The object will not be properly read back by load()\n\ 1220 to the file. The object will not be properly read back by load()\n\
1315 \n\ 1221 \n\
1316 The version argument indicates the data format that dump should use."); 1222 The version argument indicates the data format that dump should use.");
1317 1223
1318 static PyObject * 1224 static PyObject *
1319 marshal_load(PyObject *self, PyObject *f) 1225 marshal_load(PyObject *self, PyObject *f)
1320 { 1226 {
1227 /* XXX Quick hack -- need to do this differently */
1321 PyObject *data, *result; 1228 PyObject *data, *result;
1322 RFILE rf; 1229 RFILE rf;
1323 1230 data = PyObject_CallMethod(f, "read", "");
1324 /*
1325 * Make a call to the read method, but read zero bytes.
1326 * This is to ensure that the object passed in at least
1327 * has a read method which returns bytes.
1328 */
1329 data = PyObject_CallMethod(f, "read", "i", 0);
1330 if (data == NULL) 1231 if (data == NULL)
1331 return NULL; 1232 return NULL;
1332 if (!PyBytes_Check(data)) { 1233 rf.fp = NULL;
1333 PyErr_Format(PyExc_TypeError, 1234 rf.current_filename = NULL;
1334 "f.read() returned not bytes but %.100s", 1235 if (PyBytes_Check(data)) {
1335 data->ob_type->tp_name); 1236 rf.ptr = PyBytes_AS_STRING(data);
1336 result = NULL; 1237 rf.end = rf.ptr + PyBytes_GET_SIZE(data);
1238 }
1239 else if (PyBytes_Check(data)) {
1240 rf.ptr = PyBytes_AS_STRING(data);
1241 rf.end = rf.ptr + PyBytes_GET_SIZE(data);
1337 } 1242 }
1338 else { 1243 else {
1339 rf.depth = 0; 1244 PyErr_Format(PyExc_TypeError,
1340 rf.fp = NULL; 1245 "f.read() returned neither string "
1341 rf.readable = f; 1246 "nor bytes but %.100s",
1342 rf.current_filename = NULL; 1247 data->ob_type->tp_name);
1343 result = read_object(&rf); 1248 Py_DECREF(data);
1249 return NULL;
1344 } 1250 }
1251 rf.depth = 0;
1252 result = read_object(&rf);
1345 Py_DECREF(data); 1253 Py_DECREF(data);
1346 return result; 1254 return result;
1347 } 1255 }
1348 1256
1349 PyDoc_STRVAR(load_doc, 1257 PyDoc_STRVAR(load_doc,
1350 "load(file)\n\ 1258 "load(file)\n\
1351 \n\ 1259 \n\
1352 Read one value from the open file and return it. If no valid value is\n\ 1260 Read one value from the open file and return it. If no valid value is\n\
1353 read (e.g. because the data has a different Python version’s\n\ 1261 read (e.g. because the data has a different Python version’s\n\
1354 incompatible marshal format), raise EOFError, ValueError or TypeError.\n\ 1262 incompatible marshal format), raise EOFError, ValueError or TypeError.\n\
(...skipping 30 matching lines...) Expand all
1385 RFILE rf; 1293 RFILE rf;
1386 Py_buffer p; 1294 Py_buffer p;
1387 char *s; 1295 char *s;
1388 Py_ssize_t n; 1296 Py_ssize_t n;
1389 PyObject* result; 1297 PyObject* result;
1390 if (!PyArg_ParseTuple(args, "s*:loads", &p)) 1298 if (!PyArg_ParseTuple(args, "s*:loads", &p))
1391 return NULL; 1299 return NULL;
1392 s = p.buf; 1300 s = p.buf;
1393 n = p.len; 1301 n = p.len;
1394 rf.fp = NULL; 1302 rf.fp = NULL;
1395 rf.readable = NULL;
1396 rf.current_filename = NULL; 1303 rf.current_filename = NULL;
1397 rf.ptr = s; 1304 rf.ptr = s;
1398 rf.end = s + n; 1305 rf.end = s + n;
1399 rf.depth = 0; 1306 rf.depth = 0;
1400 result = read_object(&rf); 1307 result = read_object(&rf);
1401 PyBuffer_Release(&p); 1308 PyBuffer_Release(&p);
1402 return result; 1309 return result;
1403 } 1310 }
1404 1311
1405 PyDoc_STRVAR(loads_doc, 1312 PyDoc_STRVAR(loads_doc,
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
1462 1369
1463 PyMODINIT_FUNC 1370 PyMODINIT_FUNC
1464 PyMarshal_Init(void) 1371 PyMarshal_Init(void)
1465 { 1372 {
1466 PyObject *mod = PyModule_Create(&marshalmodule); 1373 PyObject *mod = PyModule_Create(&marshalmodule);
1467 if (mod == NULL) 1374 if (mod == NULL)
1468 return NULL; 1375 return NULL;
1469 PyModule_AddIntConstant(mod, "version", Py_MARSHAL_VERSION); 1376 PyModule_AddIntConstant(mod, "version", Py_MARSHAL_VERSION);
1470 return mod; 1377 return mod;
1471 } 1378 }
OLDNEW
« no previous file with comments | « Python/makeopcodetargets.py ('k') | Python/modsupport.c » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+