python / python/cpython

Disallow arbitrary types in co_consts

Abierto
#122,850 2 comentarios 0 reacciones 1 asignado Ver en GitHub

@encukou ya está trabajando en esto.

Desde el 9/8/2024.

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

Descripción

co_consts can contain values of any type, including mutable ones. For example:

def f():
    print(())

code = f.__code__
code = code.replace(co_consts=(None, []))
exec(code)

This is dangerous especially in the free-threaded build, which

  • de-duplicates constants and
  • immortalizes constants and relies them to be immortal

The “feature” is untested, as seen from the above snippet failing an assertion in free-threaded build.

Mutable subclasses of immutable built-in types are especially “interesting” here.

A solution would be to only allow built-in immutable marshallable types (booleans, integers, floating-point numbers, complex numbers, strings, bytes, tuples, frozensets, code objects, None, Ellipsis, StopIteration), excluding sublasses. That's a backwards-incompatible change.

Note that CPython now already recursively analyzes co_consts; there's little cost to doing additional inspection.

My current plan is:

  • Disallow “bad” values in free-threaded builds in 3.14, as this build is still experimental and this breaks vital invariants.
  • Deprecate “bad” values in regular build, and initially plan to disallow them in 3.16. (Minimal PEP-387 deprecation period chosen because this “feature” is very likely to interfere with optimizations, and unlikely to get proper test coverage.)

Until then, please avoid assuming that code constants are “sane”. (cc @brandtbucher @markshannon @colesbury)

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.

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.