python / python/cpython

Add `PyUnstable_InterpreterFrame_GetLocal` to read a local var from an interp. frame

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

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

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

説明

Proposal:

Add an unstable C API to read a single local variable of an internal interpreter frame by its localsplus index:

PyObject *
PyUnstable_InterpreterFrame_GetLocal(struct _PyInterpreterFrame *frame,
                                     Py_ssize_t index);

Returns a new strong reference, with cell and free variables unboxed to their contents. Free variables are resolved from the function closure, so it also works on a frame that has not started executing (before COPY_FREE_VARS). index outside [0, co_nlocalsplus) returns NULL with an IndexError set; an unset or hidden slot returns NULL with no exception set. The frame is not modified.

Motivation

TorchDynamo (PyTorch JIT compiler) relies on the eval frame API (PEP 523), and one of the things it needs to do when it intercepts a frame is to read the frame's local variables.

CPython has 3 public APIs for reading frame locals, but none of them are suitable to be used in Dynamo:

  • PyFrame_GetLocals(PyFrameObject*)
  • PyFrame_GetVar(PyFrameObject*, PyObject*)
  • PyEval_GetFrameLocals(void)

The first two take a PyFrameObject* as first argument and there's no public way to get one from a _PyInterpreterFrame*. The last one (PyEval_GetFrameLocals) reads the current frame, and the frame might not be ready when Dynamo gets it.

So today PyTorch implements its own version of *_GetLocals, which relies on an interface that is fragile and hard to maintain.

Has this already been discussed elsewhere?

I have already discussed this feature proposal on Discourse

Links to previous discussion of this feature:

https://discuss.python.org/t/add-pyunstable-interpreterframe-getlocals-read-a-frame-s-locals-from-the-interpreter-frame-c-api/108520

Linked PRs
  • gh-156134

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

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

はじめの一歩

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

調査の方向性

まず、リンクされている PR gh-156134 と Discourse の議論を確認し、その後、提案されている PyUnstable_InterpreterFrame_GetLocal の動作を、PyTorch の framelocals_mapping.cpp の実装と比較します。unstable C API が、仕様どおりに localsplus インデックス、cell 変数と free 変数、未設定のスロット、実行前のフレームを処理し、適切なテストが用意されていれば完了です。

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

評価

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

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

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