python / python/cpython

Code object comparison could lead to monitoring issue

Abierto
#136,223 2 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

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

Descripción

Bug report

Bug description:

Currently we compare code objects with a lot of its features, but not the file it is defined in - that might make some sense because the two code objects are "equivalent" as long as all their features match. However, the two code objects could still be two different ones - for example, if they are two exact same functions defined in two different files (this is a real case for me and it took me a while to figure it out).

The problem is, code objects can be used as keys. When we have "equal but not the same" code objects, it could affect monitoring. We thought we were monitoring one code object while we are actually monitoring another one.

The code was like this:

@cache
def get_code_lineno(code):
    ...
    return {code: [...]}

assert code1 == code2  # two code objects in two different files

for c, linenos in get_code_lineno(code1):
    sys.monitoring.set_local_events(tool, c, E.LINE)

for c, linenos in get_code_lineno(code2):
    # This does not instrument code2! This instruments code1 because of the cache!
    sys.monitoring.set_local_events(tool, c, E.LINE)

Do we consider this an expected behavior? Should we just "live with it" as it's not that common? (Again, this is a real issue I had when I was working on a project).

@markshannon

CPython versions tested on:

3.12, CPython main branch, 3.13, 3.14

Operating systems tested on:

No response

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

El informe no menciona ningún archivo fuente ni ninguna prueba. Empieza reproduciendo la búsqueda en caché con objetos de código equivalentes de distintos archivos y, después, inspecciona la comparación de objetos de código y el punto de entrada sys.monitoring.set_local_events. El comportamiento previsto debe decidirse antes de que una prueba de regresión pueda verificar que la monitorización no apunta al objeto de código equivocado.

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

Evaluación

Stack tecnológico
python
Área
devtools
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Necesita aclaración
Aptitud para principiantes
30/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.