Frame 物件

type PyFrameObject
受限 API 的一部分 (做為一個不透明結構 (opaque struct)).

用來描述 frame 物件的 C 結構。

在這個結構中沒有公開的成員。

在 3.11 版的變更: 此结构体的成员已从公有 C API 中移除。 请参阅 What's New entry 了解详情。

可以使用函数 PyEval_GetFrame()PyThreadState_GetFrame() 去获取一个帧对象。

可参考:Reflection 1

PyTypeObject PyFrame_Type

帧对象的类型。 它与 Python 层中的 types.FrameType 是同一对象。

在 3.11 版的變更: 在之前版本中,此类型仅在包括 <frameobject.h> 之后可用。

int PyFrame_Check(PyObject *obj)

如果 obj 是一个帧对象则返回非零值。

在 3.11 版的變更: 在之前版本中,只函数仅在包括 <frameobject.h> 之后可用。

PyFrameObject *PyFrame_GetBack(PyFrameObject *frame)

获取 frame 为下一个外部帧。

返回一个 strong reference,或者如果 frame 没有外部帧则返回 NULL

在 3.9 版新加入.

PyObject *PyFrame_GetBuiltins(PyFrameObject *frame)

取得 framef_builtins 屬性。

回傳 strong reference。結果不能為 NULL

在 3.11 版新加入.

PyCodeObject *PyFrame_GetCode(PyFrameObject *frame)
穩定 ABI 的一部分 自 3.10 版本開始.

获取 frame 的代码。

回傳 strong reference

结果(帧代码)不可为 NULL

在 3.9 版新加入.

PyObject *PyFrame_GetGenerator(PyFrameObject *frame)

获取拥有该帧的生成器、协程或异步生成器,或者如果该帧不被某个生成器所拥有则为 NULL。 不会引发异常,即使其返回值为 NULL

回傳 strong referenceNULL

在 3.11 版新加入.

PyObject *PyFrame_GetGlobals(PyFrameObject *frame)

取得 framef_globals 屬性。

回傳 strong reference。結果不能為 NULL

在 3.11 版新加入.

int PyFrame_GetLasti(PyFrameObject *frame)

取得 framef_lasti 屬性。

如果 frame.f_lastiNone 則回傳 -1。

在 3.11 版新加入.

PyObject *PyFrame_GetVar(PyFrameObject *frame, PyObject *name)

取得 frame 的變數 name

  • 在成功時回傳變數值的 strong reference

  • 如果變數不存在,則引發 NameError 並回傳 NULL

  • 在錯誤時引發例外並回傳 NULL

name 的型別必須是 str

在 3.12 版新加入.

PyObject *PyFrame_GetVarString(PyFrameObject *frame, const char *name)

PyFrame_GetVar() 相似,但该变量名是一个使用 UTF-8 编码的 C 字符串。

在 3.12 版新加入.

PyObject *PyFrame_GetLocals(PyFrameObject *frame)

获取 framef_locals 属性 (dict)。

回傳 strong reference

在 3.11 版新加入.

int PyFrame_GetLineNumber(PyFrameObject *frame)
穩定 ABI 的一部分 自 3.10 版本開始.

返回 frame 当前正在执行的行号。

内部帧

除非使用:pep:523,否则你不会需要它。

struct _PyInterpreterFrame

解释器的内部帧表示。

在 3.11 版新加入.

PyObject *PyUnstable_InterpreterFrame_GetCode(struct _PyInterpreterFrame *frame);
這是 不穩定 API,它可能在小版本發布中沒有任何警告地被變更。

返回一个指向帧的代码对象的 strong reference

在 3.12 版新加入.

int PyUnstable_InterpreterFrame_GetLasti(struct _PyInterpreterFrame *frame);
這是 不穩定 API,它可能在小版本發布中沒有任何警告地被變更。

将字节偏移量返回到最后执行的指令中。

在 3.12 版新加入.

int PyUnstable_InterpreterFrame_GetLine(struct _PyInterpreterFrame *frame);
這是 不穩定 API,它可能在小版本發布中沒有任何警告地被變更。

返回正在执行的指令的行数,如果没有行数,则返回-1。

在 3.12 版新加入.