@@ -102,12 +102,13 @@ typedef struct {
102102 if failure, sets the exception
103103*/
104104static int
105- parse_internal_render_format_spec (PyObject * format_spec ,
105+ parse_internal_render_format_spec (STRINGLIB_CHAR * format_spec ,
106+ Py_ssize_t format_spec_len ,
106107 InternalFormatSpec * format ,
107108 char default_type )
108109{
109- STRINGLIB_CHAR * ptr = STRINGLIB_STR ( format_spec ) ;
110- STRINGLIB_CHAR * end = ptr + STRINGLIB_LEN ( format_spec ) ;
110+ STRINGLIB_CHAR * ptr = format_spec ;
111+ STRINGLIB_CHAR * end = format_spec + format_spec_len ;
111112
112113 /* end-ptr is used throughout this code to specify the length of
113114 the input string */
@@ -756,56 +757,31 @@ format_float_internal(PyObject *value,
756757/************************************************************************/
757758/*********** built in formatters ****************************************/
758759/************************************************************************/
759- #ifdef FORMAT_STRING
760760PyObject *
761- FORMAT_STRING (PyObject * value , PyObject * args )
761+ FORMAT_STRING (PyObject * obj ,
762+ STRINGLIB_CHAR * format_spec ,
763+ Py_ssize_t format_spec_len )
762764{
763- PyObject * format_spec ;
764- PyObject * result = NULL ;
765- #if PY_VERSION_HEX < 0x03000000
766- PyObject * tmp = NULL ;
767- #endif
768765 InternalFormatSpec format ;
769-
770- /* If 2.x, we accept either str or unicode, and try to convert it
771- to the right type. In 3.x, we insist on only unicode */
772- #if PY_VERSION_HEX >= 0x03000000
773- if (!PyArg_ParseTuple (args , STRINGLIB_PARSE_CODE ":__format__" ,
774- & format_spec ))
775- goto done ;
776- #else
777- /* If 2.x, convert format_spec to the same type as value */
778- /* This is to allow things like u''.format('') */
779- if (!PyArg_ParseTuple (args , "O:__format__" , & format_spec ))
780- goto done ;
781- if (!(PyBytes_Check (format_spec ) || PyUnicode_Check (format_spec ))) {
782- PyErr_Format (PyExc_TypeError , "__format__ arg must be str "
783- "or unicode, not %s" , Py_TYPE (format_spec )-> tp_name );
784- goto done ;
785- }
786- tmp = STRINGLIB_TOSTR (format_spec );
787- if (tmp == NULL )
788- goto done ;
789- format_spec = tmp ;
790- #endif
766+ PyObject * result = NULL ;
791767
792768 /* check for the special case of zero length format spec, make
793- it equivalent to str(value ) */
794- if (STRINGLIB_LEN ( format_spec ) == 0 ) {
795- result = STRINGLIB_TOSTR (value );
769+ it equivalent to str(obj ) */
770+ if (format_spec_len == 0 ) {
771+ result = STRINGLIB_TOSTR (obj );
796772 goto done ;
797773 }
798774
799-
800775 /* parse the format_spec */
801- if (!parse_internal_render_format_spec (format_spec , & format , 's' ))
776+ if (!parse_internal_render_format_spec (format_spec , format_spec_len ,
777+ & format , 's' ))
802778 goto done ;
803779
804780 /* type conversion? */
805781 switch (format .type ) {
806782 case 's' :
807783 /* no type conversion needed, already a string. do the formatting */
808- result = format_string_internal (value , & format );
784+ result = format_string_internal (obj , & format );
809785 break ;
810786 default :
811787 /* unknown */
@@ -826,35 +802,31 @@ FORMAT_STRING(PyObject* value, PyObject* args)
826802 }
827803
828804done :
829- #if PY_VERSION_HEX < 0x03000000
830- Py_XDECREF (tmp );
831- #endif
832805 return result ;
833806}
834- #endif /* FORMAT_STRING */
835807
836808#if defined FORMAT_LONG || defined FORMAT_INT
837809static PyObject *
838- format_int_or_long (PyObject * value , PyObject * args , IntOrLongToString tostring )
810+ format_int_or_long (PyObject * obj ,
811+ STRINGLIB_CHAR * format_spec ,
812+ Py_ssize_t format_spec_len ,
813+ IntOrLongToString tostring )
839814{
840- PyObject * format_spec ;
841815 PyObject * result = NULL ;
842816 PyObject * tmp = NULL ;
843817 InternalFormatSpec format ;
844818
845- if (!PyArg_ParseTuple (args , STRINGLIB_PARSE_CODE ":__format__" ,
846- & format_spec ))
847- goto done ;
848-
849819 /* check for the special case of zero length format spec, make
850- it equivalent to str(value ) */
851- if (STRINGLIB_LEN ( format_spec ) == 0 ) {
852- result = STRINGLIB_TOSTR (value );
820+ it equivalent to str(obj ) */
821+ if (format_spec_len == 0 ) {
822+ result = STRINGLIB_TOSTR (obj );
853823 goto done ;
854824 }
855825
856826 /* parse the format_spec */
857- if (!parse_internal_render_format_spec (format_spec , & format , 'd' ))
827+ if (!parse_internal_render_format_spec (format_spec ,
828+ format_spec_len ,
829+ & format , 'd' ))
858830 goto done ;
859831
860832 /* type conversion? */
@@ -868,7 +840,7 @@ format_int_or_long(PyObject* value, PyObject* args, IntOrLongToString tostring)
868840 case 'n' :
869841 /* no type conversion needed, already an int (or long). do
870842 the formatting */
871- result = format_int_or_long_internal (value , & format , tostring );
843+ result = format_int_or_long_internal (obj , & format , tostring );
872844 break ;
873845
874846 case 'e' :
@@ -879,10 +851,10 @@ format_int_or_long(PyObject* value, PyObject* args, IntOrLongToString tostring)
879851 case 'G' :
880852 case '%' :
881853 /* convert to float */
882- tmp = PyNumber_Float (value );
854+ tmp = PyNumber_Float (obj );
883855 if (tmp == NULL )
884856 goto done ;
885- result = format_float_internal (value , & format );
857+ result = format_float_internal (obj , & format );
886858 break ;
887859
888860 default :
@@ -917,9 +889,12 @@ long_format(PyObject* value, int base)
917889#endif
918890
919891PyObject *
920- FORMAT_LONG (PyObject * value , PyObject * args )
892+ FORMAT_LONG (PyObject * obj ,
893+ STRINGLIB_CHAR * format_spec ,
894+ Py_ssize_t format_spec_len )
921895{
922- return format_int_or_long (value , args , long_format );
896+ return format_int_or_long (obj , format_spec , format_spec_len ,
897+ long_format );
923898}
924899#endif /* FORMAT_LONG */
925900
@@ -935,32 +910,35 @@ int_format(PyObject* value, int base)
935910}
936911
937912PyObject *
938- FORMAT_INT (PyObject * value , PyObject * args )
913+ FORMAT_INT (PyObject * obj ,
914+ STRINGLIB_CHAR * format_spec ,
915+ Py_ssize_t format_spec_len )
939916{
940- return format_int_or_long (value , args , int_format );
917+ return format_int_or_long (obj , format_spec , format_spec_len ,
918+ int_format );
941919}
942920#endif /* FORMAT_INT */
943921
944922#ifdef FORMAT_FLOAT
945923PyObject *
946- FORMAT_FLOAT (PyObject * value , PyObject * args )
924+ FORMAT_FLOAT (PyObject * obj ,
925+ STRINGLIB_CHAR * format_spec ,
926+ Py_ssize_t format_spec_len )
947927{
948- PyObject * format_spec ;
949928 PyObject * result = NULL ;
950929 InternalFormatSpec format ;
951930
952- if (!PyArg_ParseTuple (args , STRINGLIB_PARSE_CODE ":__format__" , & format_spec ))
953- goto done ;
954-
955931 /* check for the special case of zero length format spec, make
956- it equivalent to str(value ) */
957- if (STRINGLIB_LEN ( format_spec ) == 0 ) {
958- result = STRINGLIB_TOSTR (value );
932+ it equivalent to str(obj ) */
933+ if (format_spec_len == 0 ) {
934+ result = STRINGLIB_TOSTR (obj );
959935 goto done ;
960936 }
961937
962938 /* parse the format_spec */
963- if (!parse_internal_render_format_spec (format_spec , & format , '\0' ))
939+ if (!parse_internal_render_format_spec (format_spec ,
940+ format_spec_len ,
941+ & format , '\0' ))
964942 goto done ;
965943
966944 /* type conversion? */
@@ -979,7 +957,7 @@ FORMAT_FLOAT(PyObject *value, PyObject *args)
979957 case 'n' :
980958 case '%' :
981959 /* no conversion, already a float. do the formatting */
982- result = format_float_internal (value , & format );
960+ result = format_float_internal (obj , & format );
983961 break ;
984962
985963 default :
0 commit comments