Python2 源码学习之 pyc

文中以 Python 2.7.8 版本源码为例。

1. Python 中常见的文件格式

  • py 文件
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
/* Load a source module from a given file and return its module */
/* object WITH INCREMENTED REFERENCE COUNT.  If there's a matching */
/* byte-compiled file, use that instead. */
static PyObject *
load_source_module(char *name, char *pathname, FILE *fp)
{
    if (check_compiled_module(pathname, mtime, cpathname))) {
        co = read_compiled_module(cpathname, fpc);
    }
    else {
        write_compiled_module(co, cpathname, &st, mtime);
    }
    m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);
}