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

Side by Side Diff: Python/getargs.c

Issue 8992: convertsimple() doesn't need to call converterr() if an exception was already raised
Patch Set: Created 2 years, 11 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 | « no previous file | no next file » | 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 /* New getargs implementation */ 2 /* New getargs implementation */
3 3
4 #include "Python.h" 4 #include "Python.h"
5 5
6 #include <ctype.h> 6 #include <ctype.h>
7 7
8 8
9 #ifdef __cplusplus 9 #ifdef __cplusplus
10 extern "C" { 10 extern "C" {
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 static void 155 static void
156 cleanup_buffer(PyObject *self) 156 cleanup_buffer(PyObject *self)
157 { 157 {
158 Py_buffer *ptr = (Py_buffer *)PyCapsule_GetPointer(self, GETARGS_CAPSULE_NAM E_CLEANUP_BUFFER); 158 Py_buffer *ptr = (Py_buffer *)PyCapsule_GetPointer(self, GETARGS_CAPSULE_NAM E_CLEANUP_BUFFER);
159 if (ptr) { 159 if (ptr) {
160 PyBuffer_Release(ptr); 160 PyBuffer_Release(ptr);
161 } 161 }
162 } 162 }
163 163
164 static int 164 static int
165 addcleanup(void *ptr, PyObject **freelist, PyCapsule_Destructor destr) 165 addcleanup(void *ptr, PyObject **freelist, int is_buffer)
166 { 166 {
167 PyObject *cobj; 167 PyObject *cobj;
168 const char *name; 168 const char *name;
169 PyCapsule_Destructor destr;
170
171 if (is_buffer) {
172 destr = cleanup_buffer;
173 name = GETARGS_CAPSULE_NAME_CLEANUP_BUFFER;
174 } else {
175 destr = cleanup_ptr;
176 name = GETARGS_CAPSULE_NAME_CLEANUP_PTR;
177 }
169 178
170 if (!*freelist) { 179 if (!*freelist) {
171 *freelist = PyList_New(0); 180 *freelist = PyList_New(0);
172 if (!*freelist) { 181 if (!*freelist) {
173 destr(ptr); 182 destr(ptr);
174 return -1; 183 return -1;
175 } 184 }
176 } 185 }
177 186
178 if (destr == cleanup_ptr) {
179 name = GETARGS_CAPSULE_NAME_CLEANUP_PTR;
180 } else if (destr == cleanup_buffer) {
181 name = GETARGS_CAPSULE_NAME_CLEANUP_BUFFER;
182 } else {
183 return -1;
184 }
185 cobj = PyCapsule_New(ptr, name, destr); 187 cobj = PyCapsule_New(ptr, name, destr);
186 if (!cobj) { 188 if (!cobj) {
187 destr(ptr); 189 destr(ptr);
188 return -1; 190 return -1;
189 } 191 }
190 if (PyList_Append(*freelist, cobj)) { 192 if (PyList_Append(*freelist, cobj)) {
191 Py_DECREF(cobj); 193 Py_DECREF(cobj);
192 return -1; 194 return -1;
193 } 195 }
194 Py_DECREF(cobj); 196 Py_DECREF(cobj);
(...skipping 413 matching lines...) Expand 10 before | Expand all | Expand 10 after
608 static char * 610 static char *
609 convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags, 611 convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
610 char *msgbuf, size_t bufsize, PyObject **freelist) 612 char *msgbuf, size_t bufsize, PyObject **freelist)
611 { 613 {
612 /* For # codes */ 614 /* For # codes */
613 #define FETCH_SIZE int *q=NULL;Py_ssize_t *q2=NULL;\ 615 #define FETCH_SIZE int *q=NULL;Py_ssize_t *q2=NULL;\
614 if (flags & FLAG_SIZE_T) q2=va_arg(*p_va, Py_ssize_t*); \ 616 if (flags & FLAG_SIZE_T) q2=va_arg(*p_va, Py_ssize_t*); \
615 else q=va_arg(*p_va, int*); 617 else q=va_arg(*p_va, int*);
616 #define STORE_SIZE(s) if (flags & FLAG_SIZE_T) *q2=s; else *q=s; 618 #define STORE_SIZE(s) if (flags & FLAG_SIZE_T) *q2=s; else *q=s;
617 #define BUFFER_LEN ((flags & FLAG_SIZE_T) ? *q2:*q) 619 #define BUFFER_LEN ((flags & FLAG_SIZE_T) ? *q2:*q)
620 #define RETURN_ERR_OCCURRED return msgbuf
618 621
619 const char *format = *p_format; 622 const char *format = *p_format;
620 char c = *format++; 623 char c = *format++;
621 PyObject *uarg; 624 PyObject *uarg;
622 625
623 switch (c) { 626 switch (c) {
624 627
625 case 'b': { /* unsigned byte -- very short int */ 628 case 'b': { /* unsigned byte -- very short int */
626 char *p = va_arg(*p_va, char *); 629 char *p = va_arg(*p_va, char *);
627 long ival; 630 long ival;
628 if (float_argument_error(arg)) 631 if (float_argument_error(arg))
629 return converterr("integer<b>", arg, msgbuf, bufsize); 632 RETURN_ERR_OCCURRED;
630 ival = PyLong_AsLong(arg); 633 ival = PyLong_AsLong(arg);
631 if (ival == -1 && PyErr_Occurred()) 634 if (ival == -1 && PyErr_Occurred())
632 return converterr("integer<b>", arg, msgbuf, bufsize); 635 RETURN_ERR_OCCURRED;
633 else if (ival < 0) { 636 else if (ival < 0) {
634 PyErr_SetString(PyExc_OverflowError, 637 PyErr_SetString(PyExc_OverflowError,
635 "unsigned byte integer is less than minimum"); 638 "unsigned byte integer is less than minimum");
636 return converterr("integer<b>", arg, msgbuf, bufsize); 639 RETURN_ERR_OCCURRED;
637 } 640 }
638 else if (ival > UCHAR_MAX) { 641 else if (ival > UCHAR_MAX) {
639 PyErr_SetString(PyExc_OverflowError, 642 PyErr_SetString(PyExc_OverflowError,
640 "unsigned byte integer is greater than maximum"); 643 "unsigned byte integer is greater than maximum");
641 return converterr("integer<b>", arg, msgbuf, bufsize); 644 RETURN_ERR_OCCURRED;
642 } 645 }
643 else 646 else
644 *p = (unsigned char) ival; 647 *p = (unsigned char) ival;
645 break; 648 break;
646 } 649 }
647 650
648 case 'B': {/* byte sized bitfield - both signed and unsigned 651 case 'B': {/* byte sized bitfield - both signed and unsigned
649 values allowed */ 652 values allowed */
650 char *p = va_arg(*p_va, char *); 653 char *p = va_arg(*p_va, char *);
651 long ival; 654 long ival;
652 if (float_argument_error(arg)) 655 if (float_argument_error(arg))
653 return converterr("integer<B>", arg, msgbuf, bufsize); 656 RETURN_ERR_OCCURRED;
654 ival = PyLong_AsUnsignedLongMask(arg); 657 ival = PyLong_AsUnsignedLongMask(arg);
655 if (ival == -1 && PyErr_Occurred()) 658 if (ival == -1 && PyErr_Occurred())
656 return converterr("integer<B>", arg, msgbuf, bufsize); 659 RETURN_ERR_OCCURRED;
657 else 660 else
658 *p = (unsigned char) ival; 661 *p = (unsigned char) ival;
659 break; 662 break;
660 } 663 }
661 664
662 case 'h': {/* signed short int */ 665 case 'h': {/* signed short int */
663 short *p = va_arg(*p_va, short *); 666 short *p = va_arg(*p_va, short *);
664 long ival; 667 long ival;
665 if (float_argument_error(arg)) 668 if (float_argument_error(arg))
666 return converterr("integer<h>", arg, msgbuf, bufsize); 669 RETURN_ERR_OCCURRED;
667 ival = PyLong_AsLong(arg); 670 ival = PyLong_AsLong(arg);
668 if (ival == -1 && PyErr_Occurred()) 671 if (ival == -1 && PyErr_Occurred())
669 return converterr("integer<h>", arg, msgbuf, bufsize); 672 RETURN_ERR_OCCURRED;
670 else if (ival < SHRT_MIN) { 673 else if (ival < SHRT_MIN) {
671 PyErr_SetString(PyExc_OverflowError, 674 PyErr_SetString(PyExc_OverflowError,
672 "signed short integer is less than minimum"); 675 "signed short integer is less than minimum");
673 return converterr("integer<h>", arg, msgbuf, bufsize); 676 RETURN_ERR_OCCURRED;
674 } 677 }
675 else if (ival > SHRT_MAX) { 678 else if (ival > SHRT_MAX) {
676 PyErr_SetString(PyExc_OverflowError, 679 PyErr_SetString(PyExc_OverflowError,
677 "signed short integer is greater than maximum"); 680 "signed short integer is greater than maximum");
678 return converterr("integer<h>", arg, msgbuf, bufsize); 681 RETURN_ERR_OCCURRED;
679 } 682 }
680 else 683 else
681 *p = (short) ival; 684 *p = (short) ival;
682 break; 685 break;
683 } 686 }
684 687
685 case 'H': { /* short int sized bitfield, both signed and 688 case 'H': { /* short int sized bitfield, both signed and
686 unsigned allowed */ 689 unsigned allowed */
687 unsigned short *p = va_arg(*p_va, unsigned short *); 690 unsigned short *p = va_arg(*p_va, unsigned short *);
688 long ival; 691 long ival;
689 if (float_argument_error(arg)) 692 if (float_argument_error(arg))
690 return converterr("integer<H>", arg, msgbuf, bufsize); 693 RETURN_ERR_OCCURRED;
691 ival = PyLong_AsUnsignedLongMask(arg); 694 ival = PyLong_AsUnsignedLongMask(arg);
692 if (ival == -1 && PyErr_Occurred()) 695 if (ival == -1 && PyErr_Occurred())
693 return converterr("integer<H>", arg, msgbuf, bufsize); 696 RETURN_ERR_OCCURRED;
694 else 697 else
695 *p = (unsigned short) ival; 698 *p = (unsigned short) ival;
696 break; 699 break;
697 } 700 }
698 701
699 case 'i': {/* signed int */ 702 case 'i': {/* signed int */
700 int *p = va_arg(*p_va, int *); 703 int *p = va_arg(*p_va, int *);
701 long ival; 704 long ival;
702 if (float_argument_error(arg)) 705 if (float_argument_error(arg))
703 return converterr("integer<i>", arg, msgbuf, bufsize); 706 RETURN_ERR_OCCURRED;
704 ival = PyLong_AsLong(arg); 707 ival = PyLong_AsLong(arg);
705 if (ival == -1 && PyErr_Occurred()) 708 if (ival == -1 && PyErr_Occurred())
706 return converterr("integer<i>", arg, msgbuf, bufsize); 709 RETURN_ERR_OCCURRED;
707 else if (ival > INT_MAX) { 710 else if (ival > INT_MAX) {
708 PyErr_SetString(PyExc_OverflowError, 711 PyErr_SetString(PyExc_OverflowError,
709 "signed integer is greater than maximum"); 712 "signed integer is greater than maximum");
710 return converterr("integer<i>", arg, msgbuf, bufsize); 713 RETURN_ERR_OCCURRED;
711 } 714 }
712 else if (ival < INT_MIN) { 715 else if (ival < INT_MIN) {
713 PyErr_SetString(PyExc_OverflowError, 716 PyErr_SetString(PyExc_OverflowError,
714 "signed integer is less than minimum"); 717 "signed integer is less than minimum");
715 return converterr("integer<i>", arg, msgbuf, bufsize); 718 RETURN_ERR_OCCURRED;
716 } 719 }
717 else 720 else
718 *p = ival; 721 *p = ival;
719 break; 722 break;
720 } 723 }
721 724
722 case 'I': { /* int sized bitfield, both signed and 725 case 'I': { /* int sized bitfield, both signed and
723 unsigned allowed */ 726 unsigned allowed */
724 unsigned int *p = va_arg(*p_va, unsigned int *); 727 unsigned int *p = va_arg(*p_va, unsigned int *);
725 unsigned int ival; 728 unsigned int ival;
726 if (float_argument_error(arg)) 729 if (float_argument_error(arg))
727 return converterr("integer<I>", arg, msgbuf, bufsize); 730 RETURN_ERR_OCCURRED;
728 ival = (unsigned int)PyLong_AsUnsignedLongMask(arg); 731 ival = (unsigned int)PyLong_AsUnsignedLongMask(arg);
729 if (ival == (unsigned int)-1 && PyErr_Occurred()) 732 if (ival == (unsigned int)-1 && PyErr_Occurred())
730 return converterr("integer<I>", arg, msgbuf, bufsize); 733 RETURN_ERR_OCCURRED;
731 else 734 else
732 *p = ival; 735 *p = ival;
733 break; 736 break;
734 } 737 }
735 738
736 case 'n': /* Py_ssize_t */ 739 case 'n': /* Py_ssize_t */
737 { 740 {
738 PyObject *iobj; 741 PyObject *iobj;
739 Py_ssize_t *p = va_arg(*p_va, Py_ssize_t *); 742 Py_ssize_t *p = va_arg(*p_va, Py_ssize_t *);
740 Py_ssize_t ival = -1; 743 Py_ssize_t ival = -1;
741 if (float_argument_error(arg)) 744 if (float_argument_error(arg))
742 return converterr("integer<n>", arg, msgbuf, bufsize); 745 RETURN_ERR_OCCURRED;
743 iobj = PyNumber_Index(arg); 746 iobj = PyNumber_Index(arg);
744 if (iobj != NULL) { 747 if (iobj != NULL) {
745 ival = PyLong_AsSsize_t(iobj); 748 ival = PyLong_AsSsize_t(iobj);
746 Py_DECREF(iobj); 749 Py_DECREF(iobj);
747 } 750 }
748 if (ival == -1 && PyErr_Occurred()) 751 if (ival == -1 && PyErr_Occurred())
749 return converterr("integer<n>", arg, msgbuf, bufsize); 752 RETURN_ERR_OCCURRED;
750 *p = ival; 753 *p = ival;
751 break; 754 break;
752 } 755 }
753 case 'l': {/* long int */ 756 case 'l': {/* long int */
754 long *p = va_arg(*p_va, long *); 757 long *p = va_arg(*p_va, long *);
755 long ival; 758 long ival;
756 if (float_argument_error(arg)) 759 if (float_argument_error(arg))
757 return converterr("integer<l>", arg, msgbuf, bufsize); 760 RETURN_ERR_OCCURRED;
758 ival = PyLong_AsLong(arg); 761 ival = PyLong_AsLong(arg);
759 if (ival == -1 && PyErr_Occurred()) 762 if (ival == -1 && PyErr_Occurred())
760 return converterr("integer<l>", arg, msgbuf, bufsize); 763 RETURN_ERR_OCCURRED;
761 else 764 else
762 *p = ival; 765 *p = ival;
763 break; 766 break;
764 } 767 }
765 768
766 case 'k': { /* long sized bitfield */ 769 case 'k': { /* long sized bitfield */
767 unsigned long *p = va_arg(*p_va, unsigned long *); 770 unsigned long *p = va_arg(*p_va, unsigned long *);
768 unsigned long ival; 771 unsigned long ival;
769 if (PyLong_Check(arg)) 772 if (PyLong_Check(arg))
770 ival = PyLong_AsUnsignedLongMask(arg); 773 ival = PyLong_AsUnsignedLongMask(arg);
771 else 774 else
772 return converterr("integer<k>", arg, msgbuf, bufsize); 775 return converterr("integer<k>", arg, msgbuf, bufsize);
773 *p = ival; 776 *p = ival;
774 break; 777 break;
775 } 778 }
776 779
777 #ifdef HAVE_LONG_LONG 780 #ifdef HAVE_LONG_LONG
778 case 'L': {/* PY_LONG_LONG */ 781 case 'L': {/* PY_LONG_LONG */
779 PY_LONG_LONG *p = va_arg( *p_va, PY_LONG_LONG * ); 782 PY_LONG_LONG *p = va_arg( *p_va, PY_LONG_LONG * );
780 PY_LONG_LONG ival; 783 PY_LONG_LONG ival;
781 if (float_argument_error(arg)) 784 if (float_argument_error(arg))
782 return converterr("long<L>", arg, msgbuf, bufsize); 785 RETURN_ERR_OCCURRED;
783 ival = PyLong_AsLongLong(arg); 786 ival = PyLong_AsLongLong(arg);
784 if (ival == (PY_LONG_LONG)-1 && PyErr_Occurred()) 787 if (ival == (PY_LONG_LONG)-1 && PyErr_Occurred())
785 return converterr("long<L>", arg, msgbuf, bufsize); 788 RETURN_ERR_OCCURRED;
786 else 789 else
787 *p = ival; 790 *p = ival;
788 break; 791 break;
789 } 792 }
790 793
791 case 'K': { /* long long sized bitfield */ 794 case 'K': { /* long long sized bitfield */
792 unsigned PY_LONG_LONG *p = va_arg(*p_va, unsigned PY_LONG_LONG *); 795 unsigned PY_LONG_LONG *p = va_arg(*p_va, unsigned PY_LONG_LONG *);
793 unsigned PY_LONG_LONG ival; 796 unsigned PY_LONG_LONG ival;
794 if (PyLong_Check(arg)) 797 if (PyLong_Check(arg))
795 ival = PyLong_AsUnsignedLongLongMask(arg); 798 ival = PyLong_AsUnsignedLongLongMask(arg);
796 else 799 else
797 return converterr("integer<K>", arg, msgbuf, bufsize); 800 return converterr("integer<K>", arg, msgbuf, bufsize);
798 *p = ival; 801 *p = ival;
799 break; 802 break;
800 } 803 }
801 #endif 804 #endif
802 805
803 case 'f': {/* float */ 806 case 'f': {/* float */
804 float *p = va_arg(*p_va, float *); 807 float *p = va_arg(*p_va, float *);
805 double dval = PyFloat_AsDouble(arg); 808 double dval = PyFloat_AsDouble(arg);
806 if (PyErr_Occurred()) 809 if (PyErr_Occurred())
807 return converterr("float<f>", arg, msgbuf, bufsize); 810 RETURN_ERR_OCCURRED;
808 else 811 else
809 *p = (float) dval; 812 *p = (float) dval;
810 break; 813 break;
811 } 814 }
812 815
813 case 'd': {/* double */ 816 case 'd': {/* double */
814 double *p = va_arg(*p_va, double *); 817 double *p = va_arg(*p_va, double *);
815 double dval = PyFloat_AsDouble(arg); 818 double dval = PyFloat_AsDouble(arg);
816 if (PyErr_Occurred()) 819 if (PyErr_Occurred())
817 return converterr("float<d>", arg, msgbuf, bufsize); 820 RETURN_ERR_OCCURRED;
818 else 821 else
819 *p = dval; 822 *p = dval;
820 break; 823 break;
821 } 824 }
822 825
823 case 'D': {/* complex double */ 826 case 'D': {/* complex double */
824 Py_complex *p = va_arg(*p_va, Py_complex *); 827 Py_complex *p = va_arg(*p_va, Py_complex *);
825 Py_complex cval; 828 Py_complex cval;
826 cval = PyComplex_AsCComplex(arg); 829 cval = PyComplex_AsCComplex(arg);
827 if (PyErr_Occurred()) 830 if (PyErr_Occurred())
828 return converterr("complex<D>", arg, msgbuf, bufsize); 831 RETURN_ERR_OCCURRED;
829 else 832 else
830 *p = cval; 833 *p = cval;
831 break; 834 break;
832 } 835 }
833 836
834 case 'c': {/* char */ 837 case 'c': {/* char */
835 char *p = va_arg(*p_va, char *); 838 char *p = va_arg(*p_va, char *);
836 if (PyBytes_Check(arg) && PyBytes_Size(arg) == 1) 839 if (PyBytes_Check(arg) && PyBytes_Size(arg) == 1)
837 *p = PyBytes_AS_STRING(arg)[0]; 840 *p = PyBytes_AS_STRING(arg)[0];
838 else 841 else
(...skipping 14 matching lines...) Expand all
853 /* XXX WAAAAH! 's', 'y', 'z', 'u', 'Z', 'e', 'w' codes all 856 /* XXX WAAAAH! 's', 'y', 'z', 'u', 'Z', 'e', 'w' codes all
854 need to be cleaned up! */ 857 need to be cleaned up! */
855 858
856 case 's': {/* text string */ 859 case 's': {/* text string */
857 if (*format == '*') { 860 if (*format == '*') {
858 Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *); 861 Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *);
859 862
860 if (PyUnicode_Check(arg)) { 863 if (PyUnicode_Check(arg)) {
861 uarg = UNICODE_DEFAULT_ENCODING(arg); 864 uarg = UNICODE_DEFAULT_ENCODING(arg);
862 if (uarg == NULL) 865 if (uarg == NULL)
863 return converterr(CONV_UNICODE, 866 RETURN_ERR_OCCURRED;
864 arg, msgbuf, bufsize);
865 PyBuffer_FillInfo(p, arg, 867 PyBuffer_FillInfo(p, arg,
866 PyBytes_AS_STRING(uarg), PyBytes_GET_SIZE(uarg ), 868 PyBytes_AS_STRING(uarg), PyBytes_GET_SIZE(uarg ),
867 1, 0); 869 1, 0);
868 } 870 }
869 else { /* any buffer-like object */ 871 else { /* any buffer-like object */
870 char *buf; 872 char *buf;
871 if (getbuffer(arg, p, &buf) < 0) 873 if (getbuffer(arg, p, &buf) < 0)
872 return converterr(buf, arg, msgbuf, bufsize); 874 return converterr(buf, arg, msgbuf, bufsize);
873 } 875 }
874 if (addcleanup(p, freelist, cleanup_buffer)) { 876 if (addcleanup(p, freelist, 1))
875 return converterr( 877 RETURN_ERR_OCCURRED;
876 "(cleanup problem)",
877 arg, msgbuf, bufsize);
878 }
879 format++; 878 format++;
880 } else if (*format == '#') { 879 } else if (*format == '#') {
881 void **p = (void **)va_arg(*p_va, char **); 880 void **p = (void **)va_arg(*p_va, char **);
882 FETCH_SIZE; 881 FETCH_SIZE;
883 882
884 if (PyUnicode_Check(arg)) { 883 if (PyUnicode_Check(arg)) {
885 uarg = UNICODE_DEFAULT_ENCODING(arg); 884 uarg = UNICODE_DEFAULT_ENCODING(arg);
886 if (uarg == NULL) 885 if (uarg == NULL)
887 return converterr(CONV_UNICODE, 886 RETURN_ERR_OCCURRED;
888 arg, msgbuf, bufsize);
889 *p = PyBytes_AS_STRING(uarg); 887 *p = PyBytes_AS_STRING(uarg);
890 STORE_SIZE(PyBytes_GET_SIZE(uarg)); 888 STORE_SIZE(PyBytes_GET_SIZE(uarg));
891 } 889 }
892 else { /* any buffer-like object */ 890 else { /* any buffer-like object */
893 /* XXX Really? */ 891 /* XXX Really? */
894 char *buf; 892 char *buf;
895 Py_ssize_t count = convertbuffer(arg, p, &buf); 893 Py_ssize_t count = convertbuffer(arg, p, &buf);
896 if (count < 0) 894 if (count < 0)
897 return converterr(buf, arg, msgbuf, bufsize); 895 return converterr(buf, arg, msgbuf, bufsize);
898 STORE_SIZE(count); 896 STORE_SIZE(count);
899 } 897 }
900 format++; 898 format++;
901 } else { 899 } else {
902 char **p = va_arg(*p_va, char **); 900 char **p = va_arg(*p_va, char **);
903 901
904 if (PyUnicode_Check(arg)) { 902 if (PyUnicode_Check(arg)) {
905 uarg = UNICODE_DEFAULT_ENCODING(arg); 903 uarg = UNICODE_DEFAULT_ENCODING(arg);
906 if (uarg == NULL) 904 if (uarg == NULL)
907 return converterr(CONV_UNICODE, 905 RETURN_ERR_OCCURRED;
908 arg, msgbuf, bufsize);
909 *p = PyBytes_AS_STRING(uarg); 906 *p = PyBytes_AS_STRING(uarg);
910 } 907 }
911 else 908 else
912 return converterr("string", arg, msgbuf, bufsize); 909 return converterr("string", arg, msgbuf, bufsize);
913 if ((Py_ssize_t) strlen(*p) != PyBytes_GET_SIZE(uarg)) 910 if ((Py_ssize_t) strlen(*p) != PyBytes_GET_SIZE(uarg))
914 return converterr("string without null bytes", 911 return converterr("string without null bytes",
915 arg, msgbuf, bufsize); 912 arg, msgbuf, bufsize);
916 } 913 }
917 break; 914 break;
918 } 915 }
919 916
920 case 'y': {/* any buffer-like object, but not PyUnicode */ 917 case 'y': {/* any buffer-like object, but not PyUnicode */
921 void **p = (void **)va_arg(*p_va, char **); 918 void **p = (void **)va_arg(*p_va, char **);
922 char *buf; 919 char *buf;
923 Py_ssize_t count; 920 Py_ssize_t count;
924 if (*format == '*') { 921 if (*format == '*') {
925 if (getbuffer(arg, (Py_buffer*)p, &buf) < 0) 922 if (getbuffer(arg, (Py_buffer*)p, &buf) < 0)
926 return converterr(buf, arg, msgbuf, bufsize); 923 return converterr(buf, arg, msgbuf, bufsize);
927 format++; 924 format++;
928 if (addcleanup(p, freelist, cleanup_buffer)) { 925 if (addcleanup(p, freelist, 1))
929 return converterr( 926 RETURN_ERR_OCCURRED;
930 "(cleanup problem)",
931 arg, msgbuf, bufsize);
932 }
933 break; 927 break;
934 } 928 }
935 count = convertbuffer(arg, p, &buf); 929 count = convertbuffer(arg, p, &buf);
936 if (count < 0) 930 if (count < 0)
937 return converterr(buf, arg, msgbuf, bufsize); 931 return converterr(buf, arg, msgbuf, bufsize);
938 if (*format == '#') { 932 if (*format == '#') {
939 FETCH_SIZE; 933 FETCH_SIZE;
940 STORE_SIZE(count); 934 STORE_SIZE(count);
941 format++; 935 format++;
942 } else { 936 } else {
943 if (strlen(*p) != count) 937 if (strlen(*p) != count)
944 return converterr( 938 return converterr(
945 "bytes without null bytes", 939 "bytes without null bytes",
946 arg, msgbuf, bufsize); 940 arg, msgbuf, bufsize);
947 } 941 }
948 break; 942 break;
949 } 943 }
950 944
951 case 'z': {/* like 's' or 's#', but None is okay, stored as NULL */ 945 case 'z': {/* like 's' or 's#', but None is okay, stored as NULL */
952 if (*format == '*') { 946 if (*format == '*') {
953 Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *); 947 Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *);
954 948
955 if (arg == Py_None) 949 if (arg == Py_None)
956 PyBuffer_FillInfo(p, NULL, NULL, 0, 1, 0); 950 PyBuffer_FillInfo(p, NULL, NULL, 0, 1, 0);
957 else if (PyUnicode_Check(arg)) { 951 else if (PyUnicode_Check(arg)) {
958 uarg = UNICODE_DEFAULT_ENCODING(arg); 952 uarg = UNICODE_DEFAULT_ENCODING(arg);
959 if (uarg == NULL) 953 if (uarg == NULL)
960 return converterr(CONV_UNICODE, 954 RETURN_ERR_OCCURRED;
961 arg, msgbuf, bufsize);
962 PyBuffer_FillInfo(p, arg, 955 PyBuffer_FillInfo(p, arg,
963 PyBytes_AS_STRING(uarg), PyBytes_GET_SIZE(uarg ), 956 PyBytes_AS_STRING(uarg), PyBytes_GET_SIZE(uarg ),
964 1, 0); 957 1, 0);
965 } 958 }
966 else { /* any buffer-like object */ 959 else { /* any buffer-like object */
967 char *buf; 960 char *buf;
968 if (getbuffer(arg, p, &buf) < 0) 961 if (getbuffer(arg, p, &buf) < 0)
969 return converterr(buf, arg, msgbuf, bufsize); 962 return converterr(buf, arg, msgbuf, bufsize);
970 } 963 }
971 if (addcleanup(p, freelist, cleanup_buffer)) { 964 if (addcleanup(p, freelist, 1))
972 return converterr( 965 RETURN_ERR_OCCURRED;
973 "(cleanup problem)",
974 arg, msgbuf, bufsize);
975 }
976 format++; 966 format++;
977 } else if (*format == '#') { /* any buffer-like object */ 967 } else if (*format == '#') { /* any buffer-like object */
978 void **p = (void **)va_arg(*p_va, char **); 968 void **p = (void **)va_arg(*p_va, char **);
979 FETCH_SIZE; 969 FETCH_SIZE;
980 970
981 if (arg == Py_None) { 971 if (arg == Py_None) {
982 *p = 0; 972 *p = 0;
983 STORE_SIZE(0); 973 STORE_SIZE(0);
984 } 974 }
985 else if (PyUnicode_Check(arg)) { 975 else if (PyUnicode_Check(arg)) {
986 uarg = UNICODE_DEFAULT_ENCODING(arg); 976 uarg = UNICODE_DEFAULT_ENCODING(arg);
987 if (uarg == NULL) 977 if (uarg == NULL)
988 return converterr(CONV_UNICODE, 978 RETURN_ERR_OCCURRED;
989 arg, msgbuf, bufsize);
990 *p = PyBytes_AS_STRING(uarg); 979 *p = PyBytes_AS_STRING(uarg);
991 STORE_SIZE(PyBytes_GET_SIZE(uarg)); 980 STORE_SIZE(PyBytes_GET_SIZE(uarg));
992 } 981 }
993 else { /* any buffer-like object */ 982 else { /* any buffer-like object */
994 /* XXX Really? */ 983 /* XXX Really? */
995 char *buf; 984 char *buf;
996 Py_ssize_t count = convertbuffer(arg, p, &buf); 985 Py_ssize_t count = convertbuffer(arg, p, &buf);
997 if (count < 0) 986 if (count < 0)
998 return converterr(buf, arg, msgbuf, bufsize); 987 return converterr(buf, arg, msgbuf, bufsize);
999 STORE_SIZE(count); 988 STORE_SIZE(count);
1000 } 989 }
1001 format++; 990 format++;
1002 } else { 991 } else {
1003 char **p = va_arg(*p_va, char **); 992 char **p = va_arg(*p_va, char **);
1004 uarg = NULL; 993 uarg = NULL;
1005 994
1006 if (arg == Py_None) 995 if (arg == Py_None)
1007 *p = 0; 996 *p = 0;
1008 else if (PyBytes_Check(arg)) { 997 else if (PyBytes_Check(arg)) {
1009 /* Enable null byte check below */ 998 /* Enable null byte check below */
1010 uarg = arg; 999 uarg = arg;
1011 *p = PyBytes_AS_STRING(arg); 1000 *p = PyBytes_AS_STRING(arg);
1012 } 1001 }
1013 else if (PyUnicode_Check(arg)) { 1002 else if (PyUnicode_Check(arg)) {
1014 uarg = UNICODE_DEFAULT_ENCODING(arg); 1003 uarg = UNICODE_DEFAULT_ENCODING(arg);
1015 if (uarg == NULL) 1004 if (uarg == NULL)
1016 return converterr(CONV_UNICODE, 1005 RETURN_ERR_OCCURRED;
1017 arg, msgbuf, bufsize);
1018 *p = PyBytes_AS_STRING(uarg); 1006 *p = PyBytes_AS_STRING(uarg);
1019 } 1007 }
1020 else 1008 else
1021 return converterr("string or None", 1009 return converterr("string or None",
1022 arg, msgbuf, bufsize); 1010 arg, msgbuf, bufsize);
1023 if (*p != NULL && uarg != NULL && 1011 if (*p != NULL && uarg != NULL &&
1024 (Py_ssize_t) strlen(*p) != PyBytes_GET_SIZE(uarg)) 1012 (Py_ssize_t) strlen(*p) != PyBytes_GET_SIZE(uarg))
1025 return converterr( 1013 return converterr(
1026 "string without null bytes or None", 1014 "string without null bytes or None",
1027 arg, msgbuf, bufsize); 1015 arg, msgbuf, bufsize);
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
1092 if (buffer == NULL) 1080 if (buffer == NULL)
1093 return converterr("(buffer is NULL)", 1081 return converterr("(buffer is NULL)",
1094 arg, msgbuf, bufsize); 1082 arg, msgbuf, bufsize);
1095 1083
1096 /* Encode object */ 1084 /* Encode object */
1097 if (!recode_strings && 1085 if (!recode_strings &&
1098 (PyBytes_Check(arg) || PyByteArray_Check(arg))) { 1086 (PyBytes_Check(arg) || PyByteArray_Check(arg))) {
1099 s = arg; 1087 s = arg;
1100 Py_INCREF(s); 1088 Py_INCREF(s);
1101 if (PyObject_AsCharBuffer(s, &ptr, &size) < 0) 1089 if (PyObject_AsCharBuffer(s, &ptr, &size) < 0)
1102 return converterr("(AsCharBuffer failed)", 1090 RETURN_ERR_OCCURRED;
1103 arg, msgbuf, bufsize);
1104 } 1091 }
1105 else { 1092 else {
1106 PyObject *u; 1093 PyObject *u;
1107 1094
1108 /* Convert object to Unicode */ 1095 /* Convert object to Unicode */
1109 u = PyUnicode_FromObject(arg); 1096 u = PyUnicode_FromObject(arg);
1110 if (u == NULL) 1097 if (u == NULL)
1111 return converterr( 1098 RETURN_ERR_OCCURRED;
1112 "string or unicode or text buffer",
1113 arg, msgbuf, bufsize);
1114 1099
1115 /* Encode object; use default error handling */ 1100 /* Encode object; use default error handling */
1116 s = PyUnicode_AsEncodedString(u, 1101 s = PyUnicode_AsEncodedString(u, encoding, NULL);
1117 encoding,
1118 NULL);
1119 Py_DECREF(u); 1102 Py_DECREF(u);
1120 if (s == NULL) 1103 if (s == NULL)
1121 return converterr("(encoding failed)", 1104 RETURN_ERR_OCCURRED;
1122 arg, msgbuf, bufsize);
1123 if (!PyBytes_Check(s)) {
1124 Py_DECREF(s);
1125 return converterr(
1126 "(encoder failed to return bytes)",
1127 arg, msgbuf, bufsize);
1128 }
1129 size = PyBytes_GET_SIZE(s); 1105 size = PyBytes_GET_SIZE(s);
1130 ptr = PyBytes_AS_STRING(s); 1106 ptr = PyBytes_AS_STRING(s);
1131 if (ptr == NULL) 1107 if (ptr == NULL)
1132 ptr = ""; 1108 ptr = "";
1133 } 1109 }
1134 1110
1135 /* Write output; output is guaranteed to be 0-terminated */ 1111 /* Write output; output is guaranteed to be 0-terminated */
1136 if (*format == '#') { 1112 if (*format == '#') {
1137 /* Using buffer length parameter '#': 1113 /* Using buffer length parameter '#':
1138 1114
(...skipping 23 matching lines...) Expand all
1162 Py_DECREF(s); 1138 Py_DECREF(s);
1163 return converterr( 1139 return converterr(
1164 "(buffer_len is NULL)", 1140 "(buffer_len is NULL)",
1165 arg, msgbuf, bufsize); 1141 arg, msgbuf, bufsize);
1166 } 1142 }
1167 if (*buffer == NULL) { 1143 if (*buffer == NULL) {
1168 *buffer = PyMem_NEW(char, size + 1); 1144 *buffer = PyMem_NEW(char, size + 1);
1169 if (*buffer == NULL) { 1145 if (*buffer == NULL) {
1170 Py_DECREF(s); 1146 Py_DECREF(s);
1171 PyErr_NoMemory(); 1147 PyErr_NoMemory();
1172 return converterr( 1148 RETURN_ERR_OCCURRED;
1173 "(memory error)",
1174 arg, msgbuf, bufsize);
1175 } 1149 }
1176 if (addcleanup(*buffer, freelist, cleanup_ptr)) { 1150 if (addcleanup(*buffer, freelist, 0)) {
1177 Py_DECREF(s); 1151 Py_DECREF(s);
1178 return converterr( 1152 RETURN_ERR_OCCURRED;
1179 "(cleanup problem)",
1180 arg, msgbuf, bufsize);
1181 } 1153 }
1182 } else { 1154 } else {
1183 if (size + 1 > BUFFER_LEN) { 1155 if (size + 1 > BUFFER_LEN) {
1184 Py_DECREF(s); 1156 Py_DECREF(s);
1185 return converterr( 1157 return converterr(
1186 "(buffer overflow)", 1158 "(buffer overflow)",
1187 arg, msgbuf, bufsize); 1159 arg, msgbuf, bufsize);
1188 } 1160 }
1189 } 1161 }
1190 memcpy(*buffer, ptr, size+1); 1162 memcpy(*buffer, ptr, size+1);
(...skipping 15 matching lines...) Expand all
1206 if ((Py_ssize_t)strlen(ptr) != size) { 1178 if ((Py_ssize_t)strlen(ptr) != size) {
1207 Py_DECREF(s); 1179 Py_DECREF(s);
1208 return converterr( 1180 return converterr(
1209 "encoded string without NULL bytes", 1181 "encoded string without NULL bytes",
1210 arg, msgbuf, bufsize); 1182 arg, msgbuf, bufsize);
1211 } 1183 }
1212 *buffer = PyMem_NEW(char, size + 1); 1184 *buffer = PyMem_NEW(char, size + 1);
1213 if (*buffer == NULL) { 1185 if (*buffer == NULL) {
1214 Py_DECREF(s); 1186 Py_DECREF(s);
1215 PyErr_NoMemory(); 1187 PyErr_NoMemory();
1216 return converterr("(memory error)", 1188 RETURN_ERR_OCCURRED;
1217 arg, msgbuf, bufsize);
1218 } 1189 }
1219 if (addcleanup(*buffer, freelist, cleanup_ptr)) { 1190 if (addcleanup(*buffer, freelist, 0)) {
1220 Py_DECREF(s); 1191 Py_DECREF(s);
1221 return converterr("(cleanup problem)", 1192 RETURN_ERR_OCCURRED;
1222 arg, msgbuf, bufsize);
1223 } 1193 }
1224 memcpy(*buffer, ptr, size+1); 1194 memcpy(*buffer, ptr, size+1);
1225 } 1195 }
1226 Py_DECREF(s); 1196 Py_DECREF(s);
1227 break; 1197 break;
1228 } 1198 }
1229 1199
1230 case 'u': {/* raw unicode buffer (Py_UNICODE *) */ 1200 case 'u': {/* raw unicode buffer (Py_UNICODE *) */
1231 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **); 1201 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
1232 if (!PyUnicode_Check(arg)) 1202 if (!PyUnicode_Check(arg))
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
1285 return converterr(type->tp_name, arg, msgbuf, bufsize); 1255 return converterr(type->tp_name, arg, msgbuf, bufsize);
1286 1256
1287 } 1257 }
1288 else if (*format == '&') { 1258 else if (*format == '&') {
1289 typedef int (*converter)(PyObject *, void *); 1259 typedef int (*converter)(PyObject *, void *);
1290 converter convert = va_arg(*p_va, converter); 1260 converter convert = va_arg(*p_va, converter);
1291 void *addr = va_arg(*p_va, void *); 1261 void *addr = va_arg(*p_va, void *);
1292 int res; 1262 int res;
1293 format++; 1263 format++;
1294 if (! (res = (*convert)(arg, addr))) 1264 if (! (res = (*convert)(arg, addr)))
1295 return converterr("(unspecified)", 1265 return converterr("(converter error)", arg, msgbuf, bufsize);
1296 arg, msgbuf, bufsize);
1297 if (res == Py_CLEANUP_SUPPORTED && 1266 if (res == Py_CLEANUP_SUPPORTED &&
1298 addcleanup_convert(addr, freelist, convert) == -1) 1267 addcleanup_convert(addr, freelist, convert) == -1)
1299 return converterr("(cleanup problem)", 1268 RETURN_ERR_OCCURRED;
1300 arg, msgbuf, bufsize);
1301 } 1269 }
1302 else { 1270 else {
1303 p = va_arg(*p_va, PyObject **); 1271 p = va_arg(*p_va, PyObject **);
1304 *p = arg; 1272 *p = arg;
1305 } 1273 }
1306 break; 1274 break;
1307 } 1275 }
1308 1276
1309 1277
1310 case 'w': { /* memory buffer, read-write access */ 1278 case 'w': { /* memory buffer, read-write access */
(...skipping 10 matching lines...) Expand all
1321 1289
1322 1290
1323 if (pb && pb->bf_getbuffer && *format == '*') { 1291 if (pb && pb->bf_getbuffer && *format == '*') {
1324 /* Caller is interested in Py_buffer, and the object 1292 /* Caller is interested in Py_buffer, and the object
1325 supports it directly. */ 1293 supports it directly. */
1326 format++; 1294 format++;
1327 if (PyObject_GetBuffer(arg, (Py_buffer*)p, PyBUF_WRITABLE) < 0) { 1295 if (PyObject_GetBuffer(arg, (Py_buffer*)p, PyBUF_WRITABLE) < 0) {
1328 PyErr_Clear(); 1296 PyErr_Clear();
1329 return converterr("read-write buffer", arg, msgbuf, bufsize); 1297 return converterr("read-write buffer", arg, msgbuf, bufsize);
1330 } 1298 }
1331 if (addcleanup(p, freelist, cleanup_buffer)) { 1299 if (addcleanup(p, freelist, 1))
1332 return converterr( 1300 RETURN_ERR_OCCURRED;
1333 "(cleanup problem)",
1334 arg, msgbuf, bufsize);
1335 }
1336 if (!PyBuffer_IsContiguous((Py_buffer*)p, 'C')) 1301 if (!PyBuffer_IsContiguous((Py_buffer*)p, 'C'))
1337 return converterr("contiguous buffer", arg, msgbuf, bufsize); 1302 return converterr("contiguous buffer", arg, msgbuf, bufsize);
1338 break; 1303 break;
1339 } 1304 }
1340 1305
1341 /* Here we have processed w*, only w and w# remain. */ 1306 /* Here we have processed w*, only w and w# remain. */
1342 if (pb == NULL || 1307 if (pb == NULL ||
1343 pb->bf_getbuffer == NULL || 1308 pb->bf_getbuffer == NULL ||
1344 ((temp = PyObject_GetBuffer(arg, &view, 1309 ((temp = PyObject_GetBuffer(arg, &view,
1345 PyBUF_SIMPLE)) != 0) || 1310 PyBUF_SIMPLE)) != 0) ||
(...skipping 574 matching lines...) Expand 10 before | Expand all | Expand 10 after
1920 if (PyDict_Size(kw) == 0) 1885 if (PyDict_Size(kw) == 0)
1921 return 1; 1886 return 1;
1922 1887
1923 PyErr_Format(PyExc_TypeError, "%s does not take keyword arguments", 1888 PyErr_Format(PyExc_TypeError, "%s does not take keyword arguments",
1924 funcname); 1889 funcname);
1925 return 0; 1890 return 0;
1926 } 1891 }
1927 #ifdef __cplusplus 1892 #ifdef __cplusplus
1928 }; 1893 };
1929 #endif 1894 #endif
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld cbc36f91f3f7