The use of `PyStackRef_AsPyObjectBorrow` makes it hard to track ownership of references, making analysis of escaping calls too difficult
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
The problem with PyStackRef_AsPyObjectBorrow is that it is not clear whether the reference is owned by the stack ref or the pointer.
Borrowing references across calls is fine but in less structured code, it is error prone and very hard to analyze.
The solution is change most, ideally all, uses of PyStackRef_AsPyObjectBorrow to PyStackRef_AsPyObjectSteal so that the ownership of the reference is clear.
E.g.
inst(UNARY_NEGATIVE, (value -- res)) {
PyObject *val_o = PyStackRef_AsPyObjectBorrow(value);
PyObject *res_o = PyNumber_Negative(val_o);
PyStackRef_CLOSE(value);
ERROR_IF(res_o == NULL, error);
res = PyStackRef_FromPyObjectSteal(res_o);
}
would become:
inst(UNARY_NEGATIVE, (value -- res)) {
PyObject *val_o = PyStackRef_AsPyObjectSteal(value);
PyObject *res_o = PyNumber_Negative(val_o);
Py_DECREF(val_o);
ERROR_IF(res_o == NULL, error);
res = PyStackRef_FromPyObjectSteal(res_o);
}
This ensures that during the escaping call to PyNumber_Negative, the reference to the value a PyObject *, so will not be reclaimed by the garbage collector.
Linked PRs
- gh-122037
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
Busca en el código fuente de CPython los usos de PyStackRef_AsPyObjectBorrow y revisa los patrones de ownership de PyStackRef_AsPyObjectSteal y PyStackRef_CLOSE mostrados en el issue. Determina qué usos atraviesan llamadas en las que las referencias pueden escapar y, después, verifica que las referencias relevantes sigan siendo válidas y que el ownership sea explícito; compara el trabajo con el PR enlazado gh-122037.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- c, python
- Área
- backend
- Tipo de issue
- Refactorización
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 25/100