@@ -203,14 +203,15 @@ _get_current_line(tokenizeriterobject *it, const char *line_start, Py_ssize_t si
203203}
204204
205205static void
206- _get_col_offsets (tokenizeriterobject * it , struct token token , const char * line_start ,
207- PyObject * line , int line_changed , Py_ssize_t lineno , Py_ssize_t end_lineno ,
206+ _get_col_offsets (tokenizeriterobject * it , const char * token_start ,
207+ const char * token_end , const char * line_start , PyObject * line ,
208+ int line_changed , Py_ssize_t lineno , Py_ssize_t end_lineno ,
208209 Py_ssize_t * col_offset , Py_ssize_t * end_col_offset )
209210{
210211 _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED (it );
211212 Py_ssize_t byte_offset = -1 ;
212- if (token . start != NULL && token . start >= line_start ) {
213- byte_offset = token . start - line_start ;
213+ if (token_start != NULL && token_start >= line_start ) {
214+ byte_offset = token_start - line_start ;
214215 if (line_changed ) {
215216 * col_offset = _PyPegen_byte_offset_to_character_offset_line (line , 0 , byte_offset );
216217 it -> byte_col_offset_diff = byte_offset - * col_offset ;
@@ -220,15 +221,13 @@ _get_col_offsets(tokenizeriterobject *it, struct token token, const char *line_s
220221 }
221222 }
222223
223- if (token . end != NULL && token . end >= it -> tok -> line_start ) {
224- Py_ssize_t end_byte_offset = token . end - it -> tok -> line_start ;
224+ if (token_end != NULL && token_end >= it -> tok -> line_start ) {
225+ Py_ssize_t end_byte_offset = token_end - it -> tok -> line_start ;
225226 if (lineno == end_lineno ) {
226- // If the whole token is at the same line, we can just use the token.start
227- // buffer for figuring out the new column offset, since using line is not
228- // performant for very long lines.
227+ // Avoid rescanning the prefix of a very long line.
229228 Py_ssize_t token_col_offset = _PyPegen_byte_offset_to_character_offset_line (line , byte_offset , end_byte_offset );
230229 * end_col_offset = * col_offset + token_col_offset ;
231- it -> byte_col_offset_diff += token . end - token . start - token_col_offset ;
230+ it -> byte_col_offset_diff += token_end - token_start - token_col_offset ;
232231 }
233232 else {
234233 * end_col_offset = _PyPegen_byte_offset_to_character_offset_raw (it -> tok -> line_start , end_byte_offset );
@@ -263,12 +262,18 @@ tokenizeriter_next(PyObject *op)
263262 it -> done = 1 ;
264263 goto exit ;
265264 }
266- PyObject * str = NULL ;
267- if (token .start == NULL || token .end == NULL ) {
265+ const char * token_start = NULL ;
266+ const char * token_end = NULL ;
267+ PyObject * str ;
268+ if (!_PyTok_SpanIsValid (token .span )) {
268269 str = Py_GetConstant (Py_CONSTANT_EMPTY_STR );
269270 }
270271 else {
271- str = PyUnicode_FromStringAndSize (token .start , token .end - token .start );
272+ Py_ssize_t token_length ;
273+ token_start = _PyToken_TextView (
274+ it -> tok , & token , & token_length );
275+ token_end = token_start + token_length ;
276+ str = PyUnicode_FromStringAndSize (token_start , token_length );
272277 }
273278 if (str == NULL ) {
274279 goto exit ;
@@ -297,11 +302,11 @@ tokenizeriter_next(PyObject *op)
297302 goto exit ;
298303 }
299304
300- Py_ssize_t lineno = ISSTRINGLIT ( type ) ? it -> tok -> first_lineno : it -> tok -> lineno ;
301- Py_ssize_t end_lineno = it -> tok -> lineno ;
305+ Py_ssize_t lineno = token . start_loc . lineno ;
306+ Py_ssize_t end_lineno = token . end_loc . lineno ;
302307 Py_ssize_t col_offset = -1 ;
303308 Py_ssize_t end_col_offset = -1 ;
304- _get_col_offsets (it , token , line_start , line , line_changed ,
309+ _get_col_offsets (it , token_start , token_end , line_start , line , line_changed ,
305310 lineno , end_lineno , & col_offset , & end_col_offset );
306311
307312 if (it -> tok -> tok_extra_tokens ) {
@@ -317,7 +322,8 @@ tokenizeriter_next(PyObject *op)
317322 else if (type == NEWLINE ) {
318323 Py_DECREF (str );
319324 if (!it -> tok -> implicit_newline ) {
320- if (it -> tok -> start [0 ] == '\r' ) {
325+ assert (token_start != NULL );
326+ if (token_start [0 ] == '\r' ) {
321327 str = PyUnicode_FromString ("\r\n" );
322328 } else {
323329 str = PyUnicode_FromString ("\n" );
0 commit comments