@@ -34,6 +34,7 @@ _PyTok_ReaderFree(struct tok_state *tok)
3434 i < (int )Py_ARRAY_LENGTH (reader -> prefetched_lines ); i ++ ) {
3535 _PyTok_ChunkClear (& reader -> prefetched_lines [i ]);
3636 }
37+ PyMem_Free (reader -> encoding );
3738 PyMem_Free (reader -> file_buffer );
3839 PyMem_Free (reader -> decoded );
3940 PyMem_Free (reader );
@@ -202,7 +203,7 @@ read_file_line(struct tok_state *tok, _PyTok_Chunk *chunk)
202203 int available = (int )Py_MIN (reader -> file_buffer_cap - len , INT_MAX );
203204 size_t read = 0 ;
204205 char * result = _Py_UniversalNewlineFgetsWithSize (
205- reader -> file_buffer + len , available , tok -> fp , NULL , & read );
206+ reader -> file_buffer + len , available , reader -> fp , NULL , & read );
206207 if (result == NULL ) {
207208 if (len == 0 ) {
208209 return _PYTOK_READ_EOF ;
@@ -227,7 +228,7 @@ initialize_file(struct tok_state *tok)
227228{
228229 _PyTok_Reader * reader = tok -> reader ;
229230 reader -> file_initialized = 1 ;
230- if (tok -> encoding != NULL ) {
231+ if (reader -> encoding != NULL ) {
231232 return _PyTok_StartDecoder (tok , "strict" );
232233 }
233234
@@ -410,7 +411,7 @@ next_readline(struct tok_state *tok, _PyTok_Chunk *chunk)
410411 }
411412
412413 _PyTok_Chunk input = {0 };
413- if (tok -> encoding != NULL ) {
414+ if (reader -> encoding != NULL ) {
414415 if (!PyBytes_Check (raw )) {
415416 PyErr_SetString (PyExc_TypeError ,
416417 "readline() returned a non-bytes object" );
@@ -433,7 +434,7 @@ next_readline(struct tok_state *tok, _PyTok_Chunk *chunk)
433434 input .ownership = _PYTOK_CHUNK_PYOBJECT ;
434435 int decoded ;
435436 if (reader -> decoder == NULL &&
436- strcmp (tok -> encoding , "utf-8" ) == 0 &&
437+ strcmp (reader -> encoding , "utf-8" ) == 0 &&
437438 chunk_is_line (& input )) {
438439 decoded = _PyTok_DecodeOnce (
439440 tok , & input , "utf-8" , "replace" );
@@ -509,7 +510,7 @@ next_interactive(struct tok_state *tok, _PyTok_Chunk *chunk)
509510 return _PYTOK_READ_STOPPED ;
510511 }
511512 char * input = PyOS_Readline (
512- tok -> fp != NULL ? tok -> fp : stdin , stdout , reader -> prompt );
513+ reader -> fp != NULL ? reader -> fp : stdin , stdout , reader -> prompt );
513514 if (reader -> nextprompt != NULL ) {
514515 reader -> prompt = reader -> nextprompt ;
515516 }
@@ -526,9 +527,9 @@ next_interactive(struct tok_state *tok, _PyTok_Chunk *chunk)
526527 .len = len ,
527528 .ownership = _PYTOK_CHUNK_PYMEM ,
528529 };
529- if (tok -> encoding != NULL &&
530+ if (reader -> encoding != NULL &&
530531 _PyTok_DecodeOnce (
531- tok , & decoded , tok -> encoding , NULL ) < 0 ) {
532+ tok , & decoded , reader -> encoding , NULL ) < 0 ) {
532533 _PyTok_ChunkClear (& decoded );
533534 return _PYTOK_READ_ERROR ;
534535 }
604605_PyTok_ReaderUnderflow (struct tok_state * tok )
605606{
606607 assert (tok -> cur >= tok -> buf_offset && tok -> cur <= tok -> inp );
607- _PyTok_ReaderKind kind = tok -> reader -> kind ;
608+ _PyTok_Reader * reader = tok -> reader ;
609+ _PyTok_ReaderKind kind = reader -> kind ;
608610 int prepared = kind == _PYTOK_READER_PREPARED ;
609611 int streaming = reader_is_streaming (kind );
610612 int reset_buffer = !prepared && tok -> start < 0 &&
@@ -675,7 +677,7 @@ _PyTok_ReaderUnderflow(struct tok_state *tok)
675677
676678 tok -> lineno ++ ;
677679 if (kind == _PYTOK_READER_FILE &&
678- (tok -> encoding == NULL || strcmp (tok -> encoding , "utf-8" ) == 0 ) &&
680+ (reader -> encoding == NULL || strcmp (reader -> encoding , "utf-8" ) == 0 ) &&
679681 !_PyTokenizer_ensure_utf8 (
680682 _PyTok_SourcePointer (& tok -> source , tok -> cur ), tok , tok -> lineno )) {
681683 _PyTok_ChunkClear (& chunk );
@@ -779,7 +781,7 @@ _PyTokenizer_FromFile(FILE *fp, const char *encoding,
779781 _PyTokenizer_Free (tok );
780782 return NULL ;
781783 }
782- tok -> fp = fp ;
784+ tok -> reader -> fp = fp ;
783785 tok -> reader -> prompt = ps1 ;
784786 tok -> reader -> nextprompt = ps2 ;
785787 return tok ;
@@ -840,8 +842,8 @@ _PyTokenizer_FindEncodingFilename(int fd, PyObject *filename)
840842 tok -> filename = Py_NewRef (filename != NULL ? filename : & _Py_STR (anon_string ));
841843 char * encoding = NULL ;
842844 if (initialize_file (tok ) == 0 ) {
843- encoding = tok -> encoding ;
844- tok -> encoding = NULL ;
845+ encoding = tok -> reader -> encoding ;
846+ tok -> reader -> encoding = NULL ;
845847 }
846848 fclose (fp );
847849 _PyTokenizer_Free (tok );
0 commit comments