python / python/cpython

Base the tokenizer API on source offsets

Abierto
#153,569 2 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

interpreter-core type-feature
Lenguaje dominante
Python
Estrellas
77.2k
Forks
35.9k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

The tokenizer currently stores many source positions as pointers into buffers that can move. Reallocating the input means rebasing all these pointers, and missing one is very easy, especially with incremental input and f-strings.

I think tokenizer positions should be offsets into the decoded source instead:

typedef Py_ssize_t TokenizerOffset;

typedef struct {
    TokenizerOffset start;
    TokenizerOffset end;
} TokenizerSpan;

The main ideas would be:

  • One source object owns the decoded text.
  • The cursor only stores its current offset and line boundaries.
  • Tokens, errors and f-string state use offset spans instead of pointers.
  • pegen and _tokenize ask the source for a view or copy of a span.
  • Sequential tokenization keeps the current line in the cursor, while uncommon line lookups can use a small sparse index.
  • Normal parsing can keep one contiguous buffer, while tokenize(readline) can eventually use reclaimable chunks.

For incremental tokenization, new decoded input would be appended only when the cursor needs more data. Existing offsets would remain valid even if the underlying storage moves. Once tokenize has returned copied token and line strings, old input could be discarded when no active token, cursor, f-string frame or error still refers to it.

This is very nice because it separates source storage from tokenizer state, removes pointer rebasing, and means consumers no longer need to access tokenizer internals.

Linked PRs
  • gh-153585
  • gh-153587
  • gh-156472
  • gh-156482
  • gh-156484
  • gh-156654
  • gh-157055

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Empieza leyendo la implementación del tokenizer y las interfaces utilizadas por pegen y _tokenize; después, revisa los PR enlazados gh-153585, gh-153587, gh-156472, gh-156482, gh-156484, gh-156654 y gh-157055. Se considerará terminado cuando el estado del tokenizer, los tokens, los errores y el estado de f-string utilicen intervalos de offsets de origen sin reasignación de punteros, preservando la tokenización normal e incremental.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
c, python
Área
compilers
Tipo de issue
Refactorización
Dificultad
5/5
Tiempo estimado
Más de una semana
Estado de actividad
Estancado
Claridad
Necesita aclaración
Aptitud para principiantes
25/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.