@@ -381,12 +381,11 @@ next_readline(struct tok_state *tok, _PyTok_Chunk *chunk)
381381 Py_DECREF (raw );
382382 return _PYTOK_READ_ERROR ;
383383 }
384- if (_PyTok_StartDecoder (tok , "replace" ) < 0 ) {
385- Py_XDECREF (raw );
386- return _PYTOK_READ_ERROR ;
387- }
388384 if (PyBytes_GET_SIZE (raw ) == 0 ) {
389385 Py_DECREF (raw );
386+ if (_PyTok_StartDecoder (tok , "replace" ) < 0 ) {
387+ return _PYTOK_READ_ERROR ;
388+ }
390389 if (finalize_decoding (tok ) < 0 ) {
391390 return _PYTOK_READ_ERROR ;
392391 }
@@ -396,7 +395,20 @@ next_readline(struct tok_state *tok, _PyTok_Chunk *chunk)
396395 input .data = PyBytes_AS_STRING (raw );
397396 input .len = PyBytes_GET_SIZE (raw );
398397 input .ownership = _PYTOK_CHUNK_PYOBJECT ;
399- if (_PyTok_DecodeChunk (tok , & input , 0 ) < 0 ) {
398+ int decoded ;
399+ if (reader -> decoder == NULL &&
400+ strcmp (tok -> encoding , "utf-8" ) == 0 &&
401+ chunk_is_line (& input )) {
402+ decoded = _PyTok_DecodeOnce (
403+ tok , & input , "utf-8" , "replace" );
404+ }
405+ else {
406+ decoded = _PyTok_StartDecoder (tok , "replace" );
407+ if (decoded == 0 ) {
408+ decoded = _PyTok_DecodeChunk (tok , & input , 0 );
409+ }
410+ }
411+ if (decoded < 0 ) {
400412 _PyTok_ChunkClear (& input );
401413 return _PYTOK_READ_ERROR ;
402414 }
@@ -482,7 +494,8 @@ next_interactive(struct tok_state *tok, _PyTok_Chunk *chunk)
482494 .ownership = _PYTOK_CHUNK_PYMEM ,
483495 };
484496 if (tok -> encoding != NULL &&
485- _PyTok_DecodeOnce (tok , & decoded , tok -> encoding ) < 0 ) {
497+ _PyTok_DecodeOnce (
498+ tok , & decoded , tok -> encoding , NULL ) < 0 ) {
486499 _PyTok_ChunkClear (& decoded );
487500 return _PYTOK_READ_ERROR ;
488501 }
0 commit comments