missing global names inside a class body (calling `exec` with a `ChainMap`)
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 77.2k
- Fork
- 35.9k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
Bug report
Bug description:
I need to implement layered context running python code, so I choose ChainMap. But Python needs every globals passed to exec to be a dict, so I mixins it into ChainMap.
This patch runs well as far, but in a class it can't get the value from the outside.
I think this may because in the class context CPython uses some function like PyDict_Get instead of __getitem__, so the values didn't get copied into the class's globals context.
This bug caused this code fails:
from collections import ChainMap
class ChainMap(ChainMap, dict): # globals must be a real dict
pass
source = """
a = 1
class A:
print(a)
"""
exec(source, ChainMap()) # this line raises NameError
Other reproduction approaches
This is reproduceable in pyodide (a wasm port of CPython 3.12.1), so you can run the code above by just clicking one of the following link:
- Stackblitz - you can edit code and the result will sync instantly
- Python Online - this is faster but just has a console instead of an IDE
CPython versions tested on:
3.12
Operating systems tested on:
Linux, Windows
Linked PRs
- gh-121389
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia riproducendo il problema con exec(source, ChainMap()) usando l’esempio della classe A su CPython 3.12, quindi esamina il percorso di risoluzione dei nomi nel corpo della classe. Confronta il comportamento con un dict reale e analizza il PR collegato gh-121389; il lavoro è completato quando il nome esterno è disponibile all’interno del corpo della classe senza introdurre regressioni nel comportamento esistente di exec.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 25/100