python / python/cpython

missing global names inside a class body (calling `exec` with a `ChainMap`)

オープン
#121,306 コメント 19 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

interpreter-core triaged type-feature
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、CPython 3.12 でクラス A の例を使って exec(source, ChainMap()) における失敗を再現し、その後、クラス本体の名前解決パスを調査します。実際の dict と動作を比較し、リンクされている PR gh-121389 を確認します。既存の exec の動作を後退させることなく、クラス本体内で外側の名前を利用できるようになれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
compilers
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。