上一篇文章《Python的运行机制--pyc文件浅析》中已经对Python的运行单元PyCodeObject结构体作了初步的了解,但是要真正理解Python的运行机制,
还要通过分析Python的opcode才行。opcode的解释执行通过ceval.c中的以下函数:
PyObject *PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
来实现,所以这个函数是研究opcode的重点,对于opcode有什么不明白的地方都可以通过这个函数中相应的opcode处理过程来得到解答。
在开始分析opcode之前,还是有必要先了解一下一个PyFrameObject这个数据结构:
还要通过分析Python的opcode才行。opcode的解释执行通过ceval.c中的以下函数:
PyObject *PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
来实现,所以这个函数是研究opcode的重点,对于opcode有什么不明白的地方都可以通过这个函数中相应的opcode处理过程来得到解答。
在开始分析opcode之前,还是有必要先了解一下一个PyFrameObject这个数据结构:
typedef struct _frame {
PyObject_VAR_HEAD
struct _frame *f_back; /* previous frame, or NULL */
PyCodeObject *f_code; /* code segment */
PyObject *f_builtins; /* builtin symbol table (PyDictObject) */
PyObject *f_globals; /* global symbol table (PyDictObject) */
PyObject *f_locals; /* local symbol table (any mapping) */
PyObject **f_valuestack; /* points after the last local */
/* Next free slot in f_valuestack. Frame creation sets to f_valuestack.
Frame evaluation usually NULLs it, but a frame that yields sets it
to the current stack top. */
PyObject **f_stacktop;
PyObject *f_trace; /* Trace function */
/* If an exception is raised in this frame, the next three are used to
* record the exception info (if any) originally in the thread state. See
* comments before set_exc_info() -- it's not obvious.
* Invariant: if _type is NULL, then so are _