@@ -191,20 +191,18 @@ chunk_is_line(const _PyTok_Chunk *chunk)
191191static _PyTok_ReadResult
192192next_prepared (struct tok_state * tok , _PyTok_Chunk * chunk )
193193{
194- int lineno = tok -> lineno + 1 ;
195- if (lineno > tok -> source . nlines ) {
194+ const char * source_end = _PyTok_SourceData ( & tok -> source ) + tok -> source . len ;
195+ if (tok -> inp == source_end ) {
196196 return _PYTOK_READ_EOF ;
197197 }
198198 const char * start = tok -> inp ;
199- const char * newline = memchr (
200- start , '\n' , tok -> source .bytes + tok -> source .len - start );
201- _PyTok_Off end = newline != NULL
202- ? newline - tok -> source .bytes + 1 : tok -> source .len ;
199+ const char * newline = memchr (start , '\n' , source_end - start );
200+ const char * end = newline != NULL ? newline + 1 : source_end ;
203201 chunk -> data = (char * )start ;
204- chunk -> len = tok -> source . bytes + end - start ;
202+ chunk -> len = end - start ;
205203 chunk -> ownership = _PYTOK_CHUNK_BORROWED ;
206- chunk -> implicit_newline = _PyTok_SourceLineIsImplicit (
207- & tok -> source , lineno ) ;
204+ chunk -> implicit_newline = end == source_end &&
205+ tok -> reader -> prepared_implicit_newline ;
208206 return _PYTOK_READ_LINE ;
209207}
210208
@@ -260,7 +258,6 @@ initialize_file(struct tok_state *tok)
260258 if (result != _PYTOK_READ_LINE ) {
261259 return -1 ;
262260 }
263- reader -> prefetched_count = 1 ;
264261 Py_ssize_t bom_len ;
265262 _PyTok_EncodingResult detection = _PyTok_DetectEncoding (
266263 tok , & reader -> prefetched_lines [0 ], NULL , 0 , & bom_len );
@@ -278,16 +275,13 @@ initialize_file(struct tok_state *tok)
278275 reader -> prefetched_lines [0 ].data = first ;
279276 reader -> prefetched_lines [0 ].ownership = _PYTOK_CHUNK_PYMEM ;
280277 result = read_file_line (tok , & reader -> prefetched_lines [1 ]);
281- if (result == _PYTOK_READ_LINE ) {
282- reader -> prefetched_count = 2 ;
283- }
284- else if (result == _PYTOK_READ_EOF ) {
278+ if (result == _PYTOK_READ_EOF ) {
285279 reader -> file_eof = 1 ;
286280 }
287- else {
281+ else if ( result != _PYTOK_READ_LINE ) {
288282 return -1 ;
289283 }
290- _PyTok_Chunk * second = reader -> prefetched_count == 2
284+ _PyTok_Chunk * second = reader -> prefetched_lines [ 1 ]. data != NULL
291285 ? & reader -> prefetched_lines [1 ] : NULL ;
292286 detection = _PyTok_DetectEncoding (
293287 tok , & reader -> prefetched_lines [0 ], second , 1 , & bom_len );
@@ -357,10 +351,13 @@ next_file(struct tok_state *tok, _PyTok_Chunk *chunk)
357351 return _PYTOK_READ_LINE ;
358352 }
359353 _PyTok_Chunk input = {0 };
360- if (reader -> prefetched_index < reader -> prefetched_count ) {
361- input = reader -> prefetched_lines [reader -> prefetched_index ];
362- reader -> prefetched_lines [reader -> prefetched_index ++ ] =
363- (_PyTok_Chunk ){0 };
354+ if (reader -> prefetched_lines [0 ].data != NULL ) {
355+ input = reader -> prefetched_lines [0 ];
356+ reader -> prefetched_lines [0 ] = (_PyTok_Chunk ){0 };
357+ }
358+ else if (reader -> prefetched_lines [1 ].data != NULL ) {
359+ input = reader -> prefetched_lines [1 ];
360+ reader -> prefetched_lines [1 ] = (_PyTok_Chunk ){0 };
364361 }
365362 else if (!reader -> file_eof ) {
366363 _PyTok_ReadResult result = read_file_line (tok , & input );
@@ -556,16 +553,18 @@ next_interactive(struct tok_state *tok, _PyTok_Chunk *chunk)
556553 _PyTok_ChunkClear (& decoded );
557554 return _PYTOK_READ_ERROR ;
558555 }
556+ int implicit_newline ;
559557 chunk -> data = _PyTok_NormalizeNewlines (
560558 decoded .data , decoded .len , 0 , 0 ,
561- & chunk -> len , & chunk -> implicit_newline );
559+ & chunk -> len , & implicit_newline );
562560 _PyTok_ChunkClear (& decoded );
563561 if (chunk -> data == NULL ) {
564562 PyErr_NoMemory ();
565563 tok -> done = E_NOMEM ;
566564 return _PYTOK_READ_ERROR ;
567565 }
568566 chunk -> ownership = _PYTOK_CHUNK_PYMEM ;
567+ chunk -> implicit_newline = implicit_newline ;
569568 return _PYTOK_READ_LINE ;
570569}
571570
@@ -648,6 +647,13 @@ _PyTok_ReaderUnderflow(struct tok_state *tok)
648647 }
649648 return 0 ;
650649 }
650+ if (tok -> lineno == INT_MAX ) {
651+ _PyTok_ChunkClear (& chunk );
652+ PyErr_SetString (PyExc_OverflowError ,
653+ "too many tokenizer source lines" );
654+ tok -> done = E_ERROR ;
655+ return 0 ;
656+ }
651657
652658 Py_ssize_t scan_len = chunk .len ;
653659 if (kind == _PYTOK_READER_INTERACTIVE &&
@@ -678,9 +684,8 @@ _PyTok_ReaderUnderflow(struct tok_state *tok)
678684 if (!reset_buffer ) {
679685 offsets = save_buffer_offsets (tok , tok -> source .bytes );
680686 }
681- _PyTok_Off source_start = _PyTok_SourceAppendLine (
682- & tok -> source , chunk .data , chunk .len ,
683- chunk .implicit_newline );
687+ _PyTok_Off source_start = _PyTok_SourceAppend (
688+ & tok -> source , chunk .data , chunk .len );
684689 if (source_start < 0 ) {
685690 _PyTok_ChunkClear (& chunk );
686691 tok -> done = PyErr_ExceptionMatches (PyExc_MemoryError )
0 commit comments