@@ -376,15 +376,24 @@ next_readline(struct _PyTokenizer *tok, _PyTok_Chunk *chunk)
376376 Py_DECREF (raw );
377377 return _PYTOK_READ_ERROR ;
378378 }
379- if (_PyTok_StartDecoder (tok , "replace" ) < 0 ) {
380- Py_DECREF (raw );
381- return _PYTOK_READ_ERROR ;
382- }
383379 input .owner = raw ;
384380 input .data = PyBytes_AS_STRING (raw );
385381 input .len = PyBytes_GET_SIZE (raw );
386382 input .ownership = _PYTOK_CHUNK_PYOBJECT ;
387- if (input .len > 0 && _PyTok_DecodeChunk (tok , & input , 0 ) < 0 ) {
383+ int decoded ;
384+ if (reader -> decoder == NULL &&
385+ strcmp (reader -> encoding , "utf-8" ) == 0 &&
386+ chunk_is_line (& input )) {
387+ decoded = _PyTok_DecodeOnce (
388+ tok , & input , "utf-8" , "replace" );
389+ }
390+ else {
391+ decoded = _PyTok_StartDecoder (tok , "replace" );
392+ if (decoded == 0 && input .len > 0 ) {
393+ decoded = _PyTok_DecodeChunk (tok , & input , 0 );
394+ }
395+ }
396+ if (decoded < 0 ) {
388397 _PyTok_ChunkClear (& input );
389398 return _PYTOK_READ_ERROR ;
390399 }
@@ -468,7 +477,8 @@ next_interactive(struct _PyTokenizer *tok, _PyTok_Chunk *chunk)
468477 .ownership = _PYTOK_CHUNK_PYMEM ,
469478 };
470479 if (reader -> encoding != NULL &&
471- _PyTok_DecodeOnce (tok , & decoded , reader -> encoding ) < 0 ) {
480+ _PyTok_DecodeOnce (
481+ tok , & decoded , reader -> encoding , NULL ) < 0 ) {
472482 _PyTok_ChunkClear (& decoded );
473483 return _PYTOK_READ_ERROR ;
474484 }
0 commit comments