@@ -1099,14 +1099,18 @@ PyObject *PyUnicode_FromEncodedObject(register PyObject *obj,
10991099
11001100 /* Coerce object */
11011101 if (PyBytes_Check (obj )) {
1102- s = PyBytes_AS_STRING (obj );
1103- len = PyBytes_GET_SIZE (obj );
1104- }
1102+ s = PyBytes_AS_STRING (obj );
1103+ len = PyBytes_GET_SIZE (obj );
1104+ }
1105+ else if (PyByteArray_Check (obj )) {
1106+ s = PyByteArray_AS_STRING (obj );
1107+ len = PyByteArray_GET_SIZE (obj );
1108+ }
11051109 else if (PyObject_AsCharBuffer (obj , & s , & len )) {
11061110 /* Overwrite the error message with something more useful in
11071111 case of a TypeError. */
11081112 if (PyErr_ExceptionMatches (PyExc_TypeError ))
1109- PyErr_Format (PyExc_TypeError ,
1113+ PyErr_Format (PyExc_TypeError ,
11101114 "coercing to Unicode: need string or buffer, "
11111115 "%.80s found" ,
11121116 Py_TYPE (obj )-> tp_name );
@@ -1188,7 +1192,7 @@ PyObject *PyUnicode_Decode(const char *s,
11881192 goto onError ;
11891193 if (!PyUnicode_Check (unicode )) {
11901194 PyErr_Format (PyExc_TypeError ,
1191- "decoder did not return an unicode object (type=%.400s)" ,
1195+ "decoder did not return a unicode object (type=%.400s)" ,
11921196 Py_TYPE (unicode )-> tp_name );
11931197 Py_DECREF (unicode );
11941198 goto onError ;
@@ -1225,6 +1229,37 @@ PyObject *PyUnicode_AsDecodedObject(PyObject *unicode,
12251229 return NULL ;
12261230}
12271231
1232+ PyObject * PyUnicode_AsDecodedUnicode (PyObject * unicode ,
1233+ const char * encoding ,
1234+ const char * errors )
1235+ {
1236+ PyObject * v ;
1237+
1238+ if (!PyUnicode_Check (unicode )) {
1239+ PyErr_BadArgument ();
1240+ goto onError ;
1241+ }
1242+
1243+ if (encoding == NULL )
1244+ encoding = PyUnicode_GetDefaultEncoding ();
1245+
1246+ /* Decode via the codec registry */
1247+ v = PyCodec_Decode (unicode , encoding , errors );
1248+ if (v == NULL )
1249+ goto onError ;
1250+ if (!PyUnicode_Check (v )) {
1251+ PyErr_Format (PyExc_TypeError ,
1252+ "decoder did not return a unicode object (type=%.400s)" ,
1253+ Py_TYPE (v )-> tp_name );
1254+ Py_DECREF (v );
1255+ goto onError ;
1256+ }
1257+ return v ;
1258+
1259+ onError :
1260+ return NULL ;
1261+ }
1262+
12281263PyObject * PyUnicode_Encode (const Py_UNICODE * s ,
12291264 Py_ssize_t size ,
12301265 const char * encoding ,
@@ -1296,7 +1331,54 @@ PyObject *PyUnicode_AsEncodedString(PyObject *unicode,
12961331 v = PyCodec_Encode (unicode , encoding , errors );
12971332 if (v == NULL )
12981333 goto onError ;
1299- assert (PyBytes_Check (v ));
1334+ if (PyByteArray_Check (v )) {
1335+ char msg [100 ];
1336+ PyOS_snprintf (msg , sizeof (msg ),
1337+ "encoder %s returned buffer instead of bytes" ,
1338+ encoding );
1339+ if (PyErr_WarnEx (PyExc_RuntimeWarning , msg , 1 ) < 0 ) {
1340+ v = NULL ;
1341+ goto onError ;
1342+ }
1343+ v = PyBytes_FromStringAndSize (PyByteArray_AS_STRING (v ), Py_SIZE (v ));
1344+ }
1345+ else if (!PyBytes_Check (v )) {
1346+ PyErr_Format (PyExc_TypeError ,
1347+ "encoder did not return a bytes object (type=%.400s)" ,
1348+ Py_TYPE (v )-> tp_name );
1349+ v = NULL ;
1350+ }
1351+ return v ;
1352+
1353+ onError :
1354+ return NULL ;
1355+ }
1356+
1357+ PyObject * PyUnicode_AsEncodedUnicode (PyObject * unicode ,
1358+ const char * encoding ,
1359+ const char * errors )
1360+ {
1361+ PyObject * v ;
1362+
1363+ if (!PyUnicode_Check (unicode )) {
1364+ PyErr_BadArgument ();
1365+ goto onError ;
1366+ }
1367+
1368+ if (encoding == NULL )
1369+ encoding = PyUnicode_GetDefaultEncoding ();
1370+
1371+ /* Encode via the codec registry */
1372+ v = PyCodec_Encode (unicode , encoding , errors );
1373+ if (v == NULL )
1374+ goto onError ;
1375+ if (!PyUnicode_Check (v )) {
1376+ PyErr_Format (PyExc_TypeError ,
1377+ "encoder did not return an unicode object (type=%.400s)" ,
1378+ Py_TYPE (v )-> tp_name );
1379+ Py_DECREF (v );
1380+ goto onError ;
1381+ }
13001382 return v ;
13011383
13021384 onError :
@@ -6617,7 +6699,7 @@ unicode_encode(PyUnicodeObject *self, PyObject *args)
66176699
66186700 if (!PyArg_ParseTuple (args , "|ss:encode" , & encoding , & errors ))
66196701 return NULL ;
6620- v = PyUnicode_AsEncodedObject ((PyObject * )self , encoding , errors );
6702+ v = PyUnicode_AsEncodedString ((PyObject * )self , encoding , errors );
66216703 if (v == NULL )
66226704 goto onError ;
66236705 if (!PyBytes_Check (v )) {
0 commit comments