@@ -803,6 +803,7 @@ bytes_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
803803 return 0 ;
804804 }
805805
806+ #ifdef Py_USING_UNICODE
806807 if (PyUnicode_Check (arg )) {
807808 /* Encode via the codec registry */
808809 PyObject * encoded , * new ;
@@ -822,6 +823,7 @@ bytes_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
822823 Py_DECREF (new );
823824 return 0 ;
824825 }
826+ #endif
825827
826828 /* If it's not unicode, there can't be encoding or errors */
827829 if (encoding != NULL || errors != NULL ) {
@@ -929,14 +931,14 @@ bytes_repr(PyByteArrayObject *self)
929931 "bytearray object is too large to make repr" );
930932 return NULL ;
931933 }
932- v = PyUnicode_FromUnicode (NULL , newsize );
934+ v = PyString_FromStringAndSize (NULL , newsize );
933935 if (v == NULL ) {
934936 return NULL ;
935937 }
936938 else {
937939 register Py_ssize_t i ;
938- register Py_UNICODE c ;
939- register Py_UNICODE * p ;
940+ register char c ;
941+ register char * p ;
940942 int quote ;
941943
942944 /* Figure out which quote to use; single is preferred */
@@ -956,15 +958,15 @@ bytes_repr(PyByteArrayObject *self)
956958 ;
957959 }
958960
959- p = PyUnicode_AS_UNICODE (v );
961+ p = PyString_AS_STRING (v );
960962 while (* quote_prefix )
961963 * p ++ = * quote_prefix ++ ;
962964 * p ++ = quote ;
963965
964966 for (i = 0 ; i < length ; i ++ ) {
965967 /* There's at least enough room for a hex escape
966968 and a closing quote. */
967- assert (newsize - (p - PyUnicode_AS_UNICODE (v )) >= 5 );
969+ assert (newsize - (p - PyString_AS_STRING (v )) >= 5 );
968970 c = self -> ob_bytes [i ];
969971 if (c == '\'' || c == '\\' )
970972 * p ++ = '\\' , * p ++ = c ;
@@ -985,13 +987,13 @@ bytes_repr(PyByteArrayObject *self)
985987 else
986988 * p ++ = c ;
987989 }
988- assert (newsize - (p - PyUnicode_AS_UNICODE (v )) >= 1 );
990+ assert (newsize - (p - PyString_AS_STRING (v )) >= 1 );
989991 * p ++ = quote ;
990992 while (* quote_postfix ) {
991993 * p ++ = * quote_postfix ++ ;
992994 }
993995 * p = '\0' ;
994- if (PyUnicode_Resize (& v , (p - PyUnicode_AS_UNICODE (v )))) {
996+ if (_PyString_Resize (& v , (p - PyString_AS_STRING (v )))) {
995997 Py_DECREF (v );
996998 return NULL ;
997999 }
@@ -1025,6 +1027,7 @@ bytes_richcompare(PyObject *self, PyObject *other, int op)
10251027 /* Bytes can be compared to anything that supports the (binary)
10261028 buffer API. Except that a comparison with Unicode is always an
10271029 error, even if the comparison is for equality. */
1030+ #ifdef Py_USING_UNICODE
10281031 if (PyObject_IsInstance (self , (PyObject * )& PyUnicode_Type ) ||
10291032 PyObject_IsInstance (other , (PyObject * )& PyUnicode_Type )) {
10301033 if (Py_BytesWarningFlag && op == Py_EQ ) {
@@ -1036,6 +1039,7 @@ bytes_richcompare(PyObject *self, PyObject *other, int op)
10361039 Py_INCREF (Py_NotImplemented );
10371040 return Py_NotImplemented ;
10381041 }
1042+ #endif
10391043
10401044 self_size = _getbuffer (self , & self_bytes );
10411045 if (self_size < 0 ) {
@@ -2939,8 +2943,14 @@ bytes_decode(PyObject *self, PyObject *args)
29392943
29402944 if (!PyArg_ParseTuple (args , "|ss:decode" , & encoding , & errors ))
29412945 return NULL ;
2942- if (encoding == NULL )
2946+ if (encoding == NULL ) {
2947+ #ifdef Py_USING_UNICODE
29432948 encoding = PyUnicode_GetDefaultEncoding ();
2949+ #else
2950+ PyErr_SetString (PyExc_ValueError , "no encoding specified" );
2951+ return NULL ;
2952+ #endif
2953+ }
29442954 return PyCodec_Decode (self , encoding , errors );
29452955}
29462956
@@ -3038,10 +3048,8 @@ Spaces between two numbers are accepted.\n\
30383048Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef')." );
30393049
30403050static int
3041- hex_digit_to_int (Py_UNICODE c )
3051+ hex_digit_to_int (char c )
30423052{
3043- if (c >= 128 )
3044- return -1 ;
30453053 if (ISDIGIT (c ))
30463054 return c - '0' ;
30473055 else {
@@ -3056,17 +3064,14 @@ hex_digit_to_int(Py_UNICODE c)
30563064static PyObject *
30573065bytes_fromhex (PyObject * cls , PyObject * args )
30583066{
3059- PyObject * newbytes , * hexobj ;
3067+ PyObject * newbytes ;
30603068 char * buf ;
3061- Py_UNICODE * hex ;
3069+ char * hex ;
30623070 Py_ssize_t hexlen , byteslen , i , j ;
30633071 int top , bot ;
30643072
3065- if (!PyArg_ParseTuple (args , "U :fromhex" , & hexobj ))
3073+ if (!PyArg_ParseTuple (args , "s# :fromhex" , & hex , & hexlen ))
30663074 return NULL ;
3067- assert (PyUnicode_Check (hexobj ));
3068- hexlen = PyUnicode_GET_SIZE (hexobj );
3069- hex = PyUnicode_AS_UNICODE (hexobj );
30703075 byteslen = hexlen /2 ; /* This overestimates if there are spaces */
30713076 newbytes = PyByteArray_FromStringAndSize (NULL , byteslen );
30723077 if (!newbytes )
@@ -3104,10 +3109,18 @@ bytes_reduce(PyByteArrayObject *self)
31043109{
31053110 PyObject * latin1 , * dict ;
31063111 if (self -> ob_bytes )
3112+ #ifdef Py_USING_UNICODE
31073113 latin1 = PyUnicode_DecodeLatin1 (self -> ob_bytes ,
31083114 Py_SIZE (self ), NULL );
3115+ #else
3116+ latin1 = PyString_FromStringAndSize (self -> ob_bytes , Py_SIZE (self ))
3117+ #endif
31093118 else
3119+ #ifdef Py_USING_UNICODE
31103120 latin1 = PyUnicode_FromString ("" );
3121+ #else
3122+ latin1 = PyString_FromString ("" );
3123+ #endif
31113124
31123125 dict = PyObject_GetAttrString ((PyObject * )self , "__dict__" );
31133126 if (dict == NULL ) {
0 commit comments