Add `PyUnstable_InterpreterFrame_GetLocal` to read a local var from an interp. frame
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Python
- Estrellas
- 77.2k
- Forks
- 35.9k
- Métricas de merge de PR
- Métricas de PR pendientes
Descripción
Proposal:
Add an unstable C API to read a single local variable of an internal interpreter frame by its localsplus index:
PyObject *
PyUnstable_InterpreterFrame_GetLocal(struct _PyInterpreterFrame *frame,
Py_ssize_t index);
Returns a new strong reference, with cell and free variables unboxed to their contents. Free variables are resolved from the function closure, so it also works on a frame that has not started executing (before COPY_FREE_VARS). index outside [0, co_nlocalsplus) returns NULL with an IndexError set; an unset or hidden slot returns NULL with no exception set. The frame is not modified.
Motivation
TorchDynamo (PyTorch JIT compiler) relies on the eval frame API (PEP 523), and one of the things it needs to do when it intercepts a frame is to read the frame's local variables.
CPython has 3 public APIs for reading frame locals, but none of them are suitable to be used in Dynamo:
PyFrame_GetLocals(PyFrameObject*)PyFrame_GetVar(PyFrameObject*, PyObject*)PyEval_GetFrameLocals(void)
The first two take a PyFrameObject* as first argument and there's no public way to get one from a _PyInterpreterFrame*. The last one (PyEval_GetFrameLocals) reads the current frame, and the frame might not be ready when Dynamo gets it.
So today PyTorch implements its own version of *_GetLocals, which relies on an interface that is fragile and hard to maintain.
Has this already been discussed elsewhere?
I have already discussed this feature proposal on Discourse
Links to previous discussion of this feature:
Linked PRs
- gh-156134
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza revisando el PR enlazado gh-156134 y la discusión en Discourse; después compara el comportamiento propuesto de PyUnstable_InterpreterFrame_GetLocal con la implementación de PyTorch en framelocals_mapping.cpp. Se considera terminado cuando la C API inestable gestiona los índices de localsplus, las variables cell y free, los slots no establecidos y los frames previos a la ejecución según lo especificado, con las pruebas adecuadas.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- c, python
- Área
- api, backend
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 25/100