@@ -26,20 +26,6 @@ static PyStringObject *nullstring;
2626static PyObject * interned ;
2727
2828
29- #if defined(HAVE_MBTOWC ) && defined(HAVE_WCHAR_H ) && defined(HAVE_WCTYPE_H )
30- # define PRINT_MULTIBYTE_STRING
31- # include <locale.h>
32- # include <wchar.h>
33- # include <wctype.h>
34- # if defined(HAVE_ISWPRINT )
35- # define _isprint iswprint
36- # else
37- # define _isprint isprint
38- # endif
39- #endif
40-
41- static const char * hexchars = "0123456789abcdef" ;
42-
4329/*
4430 For both PyString_FromString() and PyString_FromStringAndSize(), the
4531 parameter `size' denotes number of characters to allocate, not counting any
@@ -763,14 +749,8 @@ PyString_AsStringAndSize(register PyObject *obj,
763749static int
764750string_print (PyStringObject * op , FILE * fp , int flags )
765751{
766- #ifndef PRINT_MULTIBYTE_STRING
767752 int i ;
768753 char c ;
769- #else
770- char * scur , * send ;
771- wchar_t c ;
772- int cr ;
773- #endif
774754 int quote ;
775755
776756 /* XXX Ought to check for interrupts when writing long strings */
@@ -796,36 +776,20 @@ string_print(PyStringObject *op, FILE *fp, int flags)
796776 quote = '"' ;
797777
798778 fputc (quote , fp );
799- #ifndef PRINT_MULTIBYTE_STRING
800779 for (i = 0 ; i < op -> ob_size ; i ++ ) {
801780 c = op -> ob_sval [i ];
802- #else
803- for (scur = op -> ob_sval , send = op -> ob_sval + op -> ob_size ;
804- scur < send ; scur += cr ) {
805- if ((cr = mbtowc (& c , scur , send - scur )) <= 0 )
806- goto non_printable ;
807- #endif
808781 if (c == quote || c == '\\' )
809- fputc ( '\\' , fp ), fputc ( c , fp );
782+ fprintf ( fp , "\\%c" , c );
810783 else if (c == '\t' )
811- fputs ( "\\t" , fp );
784+ fprintf ( fp , "\\t" );
812785 else if (c == '\n' )
813- fputs ( "\\n" , fp );
786+ fprintf ( fp , "\\n" );
814787 else if (c == '\r' )
815- fputs ("\\r" , fp );
816- #ifndef PRINT_MULTIBYTE_STRING
817- else if (' ' <= c && c < 0x7f)
818- fputc (c , fp );
788+ fprintf (fp , "\\r" );
789+ else if (c < ' ' || c >= 0x7f )
790+ fprintf (fp , "\\x%02x" , c & 0xff );
819791 else
820- fprintf (fp , "\\x %02 x ", c & 0xff );
821- #else
822- else if (_isprint (c ))
823- fwrite (scur , cr , 1 , fp );
824- else {
825- non_printable : cr = 1 ; /* unit to move cursor */
826- fprintf (fp , "\\x%02x" , * scur & 0xff );
827- }
828- #endif
792+ fputc (c , fp );
829793 }
830794 fputc (quote , fp );
831795 return 0 ;
@@ -846,14 +810,8 @@ PyString_Repr(PyObject *obj, int smartquotes)
846810 return NULL ;
847811 }
848812 else {
849- #ifndef PRINT_MULTIBYTE_STRING
850813 register int i ;
851814 register char c ;
852- #else
853- register char * scur , * send ;
854- wchar_t c ;
855- int cr ;
856- #endif
857815 register char * p ;
858816 int quote ;
859817
@@ -866,18 +824,11 @@ PyString_Repr(PyObject *obj, int smartquotes)
866824
867825 p = PyString_AS_STRING (v );
868826 * p ++ = quote ;
869- #ifndef PRINT_MULTIBYTE_STRING
870827 for (i = 0 ; i < op -> ob_size ; i ++ ) {
871828 /* There's at least enough room for a hex escape
872829 and a closing quote. */
873830 assert (newsize - (p - PyString_AS_STRING (v )) >= 5 );
874831 c = op -> ob_sval [i ];
875- #else
876- for (scur = op -> ob_sval , send = op -> ob_sval + op -> ob_size ;
877- scur < send ; scur += cr ) {
878- if ((cr = mbtowc (& c , scur , send - scur )) <= 0 )
879- goto non_printable ;
880- #endif
881832 if (c == quote || c == '\\' )
882833 * p ++ = '\\' , * p ++ = c ;
883834 else if (c == '\t' )
@@ -886,20 +837,15 @@ PyString_Repr(PyObject *obj, int smartquotes)
886837 * p ++ = '\\' , * p ++ = 'n' ;
887838 else if (c == '\r' )
888839 * p ++ = '\\' , * p ++ = 'r' ;
889- #ifndef PRINT_MULTIBYTE_STRING
890- else if (' ' <= c && c < 0x7f)
891- * p ++ = c ;
892- else {
893- #else
894- else if (_isprint (c ))
895- memcpy (p , scur , cr ), p += cr ;
896- else {
897- non_printable : cr = 1 ; c = * scur ;
898- #endif
899- * p ++ = '\\' ; * p ++ = 'x' ;
900- * p ++ = hexchars [(c >> 4 ) & 0x0f ];
901- * p ++ = hexchars [c & 0x0f ];
840+ else if (c < ' ' || c >= 0x7f ) {
841+ /* For performance, we don't want to call
842+ PyOS_snprintf here (extra layers of
843+ function call). */
844+ sprintf (p , "\\x%02x" , c & 0xff );
845+ p += 4 ;
902846 }
847+ else
848+ * p ++ = c ;
903849 }
904850 assert (newsize - (p - PyString_AS_STRING (v )) >= 1 );
905851 * p ++ = quote ;
0 commit comments