Welcome 微信登录

首页 / 操作系统 / Linux / Python源码学习 之 Python解释器

源码结构

下载Python的源码,解压,即可看到源码的目录结构。
  • 奇怪:Python2.7.2根目录下的 README 文件中有 各个目录的说明,在 Python3.2.1根目录下的README文件中却没有相应的介绍了。
Include/公有 头文件
Lib/Python编写的模块
Modules/C实现的模块
Objects/内置对象类型的实现
PC/Windows下构建Python的工程文件
PCbuild/
Parser/解释器的 parser、tokenizer、input handling
Python/解释器的 byte-compiler、interpreter
configureshell 脚本
... 

编译

在Windows下: PCbuild 下是VS2008 的工程文件在linux下:./configure make sudo make install

Python解释器

Python 解释器(可执行程序) 本身的代码非常简单,就是调用了 Py_Main 这个函数!
Python2.7Python3.2
int Py_Main(int argc, char **argv)int Py_Main(int argc, wchar_t **argv)
PyMain PyRun_AnyFileExFlags PyRun_InteractiveLoopFlags PyRun_InteractiveOneFlags PyParser_ParseFileFlagsEx PyAST_Compile PyEval_EvalCode PyRun_SimpleFileExFlags PyParser_ASTFromFile PyAST_Compile PyEval_EvalCode调用主要有两个分支
  • 交互式
  • 执行脚本
二者最终都是
  • Parser
  • Compile
  • Eval
三个步骤。

源码

在python2中,使用的窄字符串,在python3中,使用宽字符串。所以python3的源码乍看起来复杂了好多。源码:Modules/python.c
  • python 2.7
#include "Python.h"int main(int argc, char **argv) { ... return Py_Main(argc, argv); }
  • python 3.2
#include "Python.h" #include <locale.h>#ifdef MS_WINDOWS int wmain(int argc, wchar_t **argv) { return Py_Main(argc, argv); } #elseint main(int argc, char **argv) { wchar_t **argv_copy = (wchar_t **)PyMem_Malloc(sizeof(wchar_t*)*argc); /* We need a second copies, as Python might modify the first one. */ wchar_t **argv_copy2 = (wchar_t **)PyMem_Malloc(sizeof(wchar_t*)*argc); ... res = Py_Main(argc, argv_copy); ... return res; } #endif在 Windows 下,由于链接子系统和入口函数问题,所以有一个单独的 pythonw.exe :源码 PC/WinMain.c#include "Python.h"#define WIN32_LEAN_AND_MEAN #include <windows.h>int WINAPI wWinMain( HINSTANCE hInstance, /* handle to current instance */ HINSTANCE hPrevInstance, /* handle to previous instance */ LPWSTR lpCmdLine, /* pointer to command line */ int nCmdShow /* show state of window */ ) { return Py_Main(__argc, __wargv); }Python2中与此几乎完全相同,用 __argv 取代 __wargv