@@ -190,10 +190,10 @@ chunk_is_line(const _PyTok_Chunk *chunk)
190190static _PyTok_ReadResult
191191next_prepared (struct tok_state * tok , _PyTok_Chunk * chunk )
192192{
193- int lineno = tok -> lineno + 1 ;
194- if (lineno > tok -> source .nlines ) {
193+ if (tok -> lineno >= tok -> source .nlines ) {
195194 return _PYTOK_READ_EOF ;
196195 }
196+ int lineno = tok -> lineno + 1 ;
197197 const char * start = tok -> inp ;
198198 const char * newline = memchr (
199199 start , '\n' , tok -> source .bytes + tok -> source .len - start );
@@ -259,7 +259,6 @@ initialize_file(struct tok_state *tok)
259259 if (result != _PYTOK_READ_LINE ) {
260260 return -1 ;
261261 }
262- reader -> prefetched_count = 1 ;
263262 Py_ssize_t bom_len ;
264263 _PyTok_EncodingResult detection = _PyTok_DetectEncoding (
265264 tok , & reader -> prefetched_lines [0 ], NULL , 0 , & bom_len );
@@ -277,16 +276,13 @@ initialize_file(struct tok_state *tok)
277276 reader -> prefetched_lines [0 ].data = first ;
278277 reader -> prefetched_lines [0 ].ownership = _PYTOK_CHUNK_PYMEM ;
279278 result = read_file_line (tok , & reader -> prefetched_lines [1 ]);
280- if (result == _PYTOK_READ_LINE ) {
281- reader -> prefetched_count = 2 ;
282- }
283- else if (result == _PYTOK_READ_EOF ) {
279+ if (result == _PYTOK_READ_EOF ) {
284280 reader -> file_eof = 1 ;
285281 }
286- else {
282+ else if ( result != _PYTOK_READ_LINE ) {
287283 return -1 ;
288284 }
289- _PyTok_Chunk * second = reader -> prefetched_count == 2
285+ _PyTok_Chunk * second = reader -> prefetched_lines [ 1 ]. data != NULL
290286 ? & reader -> prefetched_lines [1 ] : NULL ;
291287 detection = _PyTok_DetectEncoding (
292288 tok , & reader -> prefetched_lines [0 ], second , 1 , & bom_len );
@@ -356,10 +352,13 @@ next_file(struct tok_state *tok, _PyTok_Chunk *chunk)
356352 return _PYTOK_READ_LINE ;
357353 }
358354 _PyTok_Chunk input = {0 };
359- if (reader -> prefetched_index < reader -> prefetched_count ) {
360- input = reader -> prefetched_lines [reader -> prefetched_index ];
361- reader -> prefetched_lines [reader -> prefetched_index ++ ] =
362- (_PyTok_Chunk ){0 };
355+ if (reader -> prefetched_lines [0 ].data != NULL ) {
356+ input = reader -> prefetched_lines [0 ];
357+ reader -> prefetched_lines [0 ] = (_PyTok_Chunk ){0 };
358+ }
359+ else if (reader -> prefetched_lines [1 ].data != NULL ) {
360+ input = reader -> prefetched_lines [1 ];
361+ reader -> prefetched_lines [1 ] = (_PyTok_Chunk ){0 };
363362 }
364363 else if (!reader -> file_eof ) {
365364 _PyTok_ReadResult result = read_file_line (tok , & input );
@@ -557,7 +556,7 @@ next_interactive(struct tok_state *tok, _PyTok_Chunk *chunk)
557556 }
558557 chunk -> data = _PyTok_NormalizeNewlines (
559558 decoded .data , decoded .len , 0 , 0 ,
560- & chunk -> len , & chunk -> implicit_newline );
559+ & chunk -> len , NULL );
561560 _PyTok_ChunkClear (& decoded );
562561 if (chunk -> data == NULL ) {
563562 PyErr_NoMemory ();
@@ -646,6 +645,12 @@ _PyTok_ReaderUnderflow(struct tok_state *tok)
646645 }
647646 return 0 ;
648647 }
648+ if (tok -> lineno == INT_MAX ) {
649+ PyErr_SetString (PyExc_OverflowError , "too many tokenizer source lines" );
650+ tok -> done = E_ERROR ;
651+ _PyTok_ChunkClear (& chunk );
652+ return 0 ;
653+ }
649654
650655 Py_ssize_t scan_len = chunk .len ;
651656 if (kind == _PYTOK_READER_INTERACTIVE &&
0 commit comments