@@ -1234,53 +1234,52 @@ PyObject *PyUnicode_FromEncodedObject(register PyObject *obj,
12341234 const char * encoding ,
12351235 const char * errors )
12361236{
1237- const char * s = NULL ;
1238- Py_ssize_t len ;
1237+ Py_buffer buffer ;
12391238 PyObject * v ;
12401239
12411240 if (obj == NULL ) {
12421241 PyErr_BadInternalCall ();
12431242 return NULL ;
12441243 }
12451244
1245+ /* Decoding bytes objects is the most common case and should be fast */
1246+ if (PyBytes_Check (obj )) {
1247+ if (PyBytes_GET_SIZE (obj ) == 0 ) {
1248+ Py_INCREF (unicode_empty );
1249+ v = (PyObject * ) unicode_empty ;
1250+ }
1251+ else {
1252+ v = PyUnicode_Decode (
1253+ PyBytes_AS_STRING (obj ), PyBytes_GET_SIZE (obj ),
1254+ encoding , errors );
1255+ }
1256+ return v ;
1257+ }
1258+
12461259 if (PyUnicode_Check (obj )) {
12471260 PyErr_SetString (PyExc_TypeError ,
12481261 "decoding str is not supported" );
12491262 return NULL ;
12501263 }
12511264
1252- /* Coerce object */
1253- if (PyBytes_Check (obj )) {
1254- s = PyBytes_AS_STRING (obj );
1255- len = PyBytes_GET_SIZE (obj );
1256- }
1257- else if (PyByteArray_Check (obj )) {
1258- s = PyByteArray_AS_STRING (obj );
1259- len = PyByteArray_GET_SIZE (obj );
1260- }
1261- else if (PyObject_AsCharBuffer (obj , & s , & len )) {
1262- /* Overwrite the error message with something more useful in
1263- case of a TypeError. */
1264- if (PyErr_ExceptionMatches (PyExc_TypeError ))
1265- PyErr_Format (PyExc_TypeError ,
1266- "coercing to str: need bytes, bytearray or char buffer, "
1267- "%.80s found" ,
1268- Py_TYPE (obj )-> tp_name );
1269- goto onError ;
1265+ /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */
1266+ if (PyObject_GetBuffer (obj , & buffer , PyBUF_SIMPLE ) < 0 ) {
1267+ PyErr_Format (PyExc_TypeError ,
1268+ "coercing to str: need bytes, bytearray "
1269+ "or buffer-like object, %.80s found" ,
1270+ Py_TYPE (obj )-> tp_name );
1271+ return NULL ;
12701272 }
12711273
1272- /* Convert to Unicode */
1273- if (len == 0 ) {
1274+ if (buffer .len == 0 ) {
12741275 Py_INCREF (unicode_empty );
1275- v = (PyObject * )unicode_empty ;
1276+ v = (PyObject * ) unicode_empty ;
12761277 }
12771278 else
1278- v = PyUnicode_Decode (s , len , encoding , errors );
1279+ v = PyUnicode_Decode (( char * ) buffer . buf , buffer . len , encoding , errors );
12791280
1281+ PyBuffer_Release (& buffer );
12801282 return v ;
1281-
1282- onError :
1283- return NULL ;
12841283}
12851284
12861285/* Convert encoding to lower case and replace '_' with '-' in order to
0 commit comments