Python 源码整理 - 1
说明
使用的源码是 Python 2.7.9 - 2014-12-10
下载源码
https://www.python.org/downloads/source/
代码结构及主要目录说明
total 1784
drwxr-xr-x@ 23 nate staff 782 Dec 10 23:59 Demo
drwxr-xr-x@ 29 nate staff 986 Jan 28 01:17 Doc
drwxr-xr-x@ 3 nate staff 102 Dec 10 23:59 Grammar
drwxr-xr-x@ 93 nate staff 3162 Dec 10 23:59 Include
-rw-r--r--@ 1 nate staff 12755 Dec 10 23:59 LICENSE
drwxr-xr-x@ 254 nate staff 8636 Dec 10 23:59 Lib
drwxr-xr-x@ 15 nate staff 510 Dec 10 23:59 Mac
-rw-r--r--@ 1 nate staff 45329 Dec 10 23:59 Makefile.pre.in
drwxr-xr-x@ 34 nate staff 1156 Dec 10 23:59 Misc
drwxr-xr-x@ 137 nate staff 4658 Jan 28 01:16 Modules
drwxr-xr-x@ 46 nate staff 1564 Dec 10 23:59 Objects
drwxr-xr-x@ 39 nate staff 1326 Dec 10 23:59 PC
drwxr-xr-x@ 54 nate staff 1836 Dec 10 23:59 PCbuild
drwxr-xr-x@ 25 nate staff 850 Dec 10 23:59 Parser
drwxr-xr-x@ 75 nate staff 2550 Dec 10 23:59 Python
-rw-r--r--@ 1 nate staff 53978 Dec 10 23:59 README
drwxr-xr-x@ 11 nate staff 374 Dec 10 23:59 RISCOS
drwxr-xr-x@ 23 nate staff 782 Dec 11 00:00 Tools
-rwxr-xr-x@ 1 nate staff 42856 Dec 11 00:00 config.guess
-rwxr-xr-x@ 1 nate staff 35740 Dec 11 00:00 config.sub
-rwxr-xr-x@ 1 nate staff 428607 Dec 11 00:00 configure
-rw-r--r--@ 1 nate staff 134982 Dec 11 00:00 configure.ac
-rwxr-xr-x@ 1 nate staff 7122 Dec 11 00:00 install-sh
-rw-r--r--@ 1 nate staff 34898 Dec 11 00:00 pyconfig.h.in
-rw-r--r--@ 1 nate staff 97660 Dec 11 00:00 setup.py
Demo # 示例的相关Python代码目录
Doc # 文档目录
Grammar # 语法分析目录
Include # 相关的C头文件目录
Lib # 相关的Python写的模块
Mac # Mac OS相关
Modules # 对性能有要求的C语言写的模块
Objects # 所有Python的内建对象的C语言实现
Parser # 词法分析和语法分析,Python代码解析的C语言实现
Python # Python引擎的C语言实现
Python对象
Python中所有的东西都是对象
内建对象的相关头文件都在Include下面
object.h
-
PyObject
PyObject 用于定长对象的定义,比如int 在 Include/object.h里可以查到PyObject结构体的定义: typedef struct _object { PyObject_HEAD } PyObject; PyObject_HEAD是一个宏,宏的定义在同一个文件,如下: #define PyObject_HEAD \ _PyObject_HEAD_EXTRA \ Py_ssize_t ob_refcnt; \ struct _typeobject *ob_type; _PyObject_HEAD_EXTRA 是另一个宏,用于定义双向链表,需要开启Py_TRACE_REFS: #ifdef Py_TRACE_REFS #define _PyObject_HEAD_EXTRA \ struct _object *_ob_next; \ struct _object *_ob_prev; Py_ssize_t 的定义在 pyport.h #ifdef HAVE_SSIZE_T typedef ssize_t Py_ssize_t; #elif SIZEOF_VOID_P == SIZEOF_SIZE_T typedef Py_intptr_t Py_ssize_t; #else # error "Python needs a typedef for Py_ssize_t in pyport.h." #endif py_intptr_t 定义: #ifdef HAVE_UINTPTR_T typedef uintptr_t Py_uintptr_t; typedef intptr_t Py_intptr_t; #elif SIZEOF_VOID_P <= SIZEOF_INT typedef unsigned int Py_uintptr_t; typedef int Py_intptr_t; #elif SIZEOF_VOID_P <= SIZEOF_LONG typedef unsigned long Py_uintptr_t; typedef long Py_intptr_t; #elif defined(HAVE_LONG_LONG) && (SIZEOF_VOID_P <= SIZEOF_LONG_LONG) typedef unsigned PY_LONG_LONG Py_uintptr_t; typedef PY_LONG_LONG Py_intptr_t; #else # error "Python needs a typedef for Py_uintptr_t in pyport.h." #endif /* HAVE_UINTPTR_T */ 看出ob_refcnt 是个无符号整型或者无符号整型指针,用于定义引用计数,当有一个对象引用了这个对象,这个值就会自增1,相反就减1,当减到0就可以释放内存 struct _typeobject *ob_type; _typeobject的定义如下: typedef struct _typeobject { PyObject_VAR_HEAD const char *tp_name; /* For printing, in format "<module>.<name>" */ Py_ssize_t tp_basicsize, tp_itemsize; /* For allocation */ /* Methods to implement standard operations */ destructor tp_dealloc; printfunc tp_print; getattrfunc tp_getattr; setattrfunc tp_setattr; cmpfunc tp_compare; reprfunc tp_repr; /* Method suites for standard classes */ PyNumberMethods *tp_as_number; PySequenceMethods *tp_as_sequence; PyMappingMethods *tp_as_mapping; /* More standard operations (here for binary compatibility) */ hashfunc tp_hash; ternaryfunc tp_call; reprfunc tp_str; getattrofunc tp_getattro; setattrofunc tp_setattro; /* Functions to access object as input/output buffer */ PyBufferProcs *tp_as_buffer; /* Flags to define presence of optional/expanded features */ long tp_flags; const char *tp_doc; /* Documentation string */ /* Assigned meaning in release 2.0 */ /* call function for all accessible objects */ traverseproc tp_traverse; /* delete references to contained objects */ inquiry tp_clear; /* Assigned meaning in release 2.1 */ /* rich comparisons */ richcmpfunc tp_richcompare; /* weak reference enabler */ Py_ssize_t tp_weaklistoffset; /* Added in release 2.2 */ /* Iterators */ getiterfunc tp_iter; iternextfunc tp_iternext; /* Attribute descriptor and subclassing stuff */ struct PyMethodDef *tp_methods; struct PyMemberDef *tp_members; struct PyGetSetDef *tp_getset; struct _typeobject *tp_base; PyObject *tp_dict; descrgetfunc tp_descr_get; descrsetfunc tp_descr_set; Py_ssize_t tp_dictoffset; initproc tp_init; allocfunc tp_alloc; newfunc tp_new; freefunc tp_free; /* Low-level free-memory routine */ inquiry tp_is_gc; /* For PyObject_IS_GC */ PyObject *tp_bases; PyObject *tp_mro; /* method resolution order */ PyObject *tp_cache; PyObject *tp_subclasses; PyObject *tp_weaklist; destructor tp_del; /* Type attribute cache version tag. Added in version 2.6 */ unsigned int tp_version_tag; #ifdef COUNT_ALLOCS /* these must be last and never explicitly initialized */ Py_ssize_t tp_allocs; Py_ssize_t tp_frees; Py_ssize_t tp_maxalloc; struct _typeobject *tp_prev; struct _typeobject *tp_next; #endif } PyTypeObject;
-
PyVarObject PyVarObject 用于变长对象,比如string 在 Include/object.h里可以查到PyVarObject结构体的定义: typedef struct { PyObject_VAR_HEAD } PyVarObjec PyObject_VAR_HEAD是一个宏,定义如下: #define PyObject_VAR_HEAD \ PyObject_HEAD \ Py_ssize_t ob_size; /* Number of items in variable part */ 可以看出他比PyObject 就多了个 Py_ssize_t ob_size, 用于表示元素的个数。
-
PyTypeObject
typedef struct _typeobject { PyObject_VAR_HEAD //对象头 const char *tp_name; //名称,格式: <module>.<name> Py_ssize_t tp_basicsize, tp_itemsize; //分配内存的信息 destructor tp_dealloc; //析构函数 printfunc tp_print; getattrfunc tp_getattr; setattrfunc tp_setattr; cmpfunc tp_compare; reprfunc tp_repr; PyNumberMethods *tp_as_number; //数值对象的操作 PySequenceMethods *tp_as_sequence; //序列对象的操作 PyMappingMethods *tp_as_mapping; //关联对象的操作 hashfunc tp_hash; //生成hash值的函数指针 ternaryfunc tp_call; reprfunc tp_str; getattrofunc tp_getattro; setattrofunc tp_setattro; PyBufferProcs *tp_as_buffer; long tp_flags; const char *tp_doc; //文档信息 traverseproc tp_traverse; inquiry tp_clear; richcmpfunc tp_richcompare; Py_ssize_t tp_weaklistoffset; getiterfunc tp_iter; iternextfunc tp_iternext; struct PyMethodDef *tp_methods; struct PyMemberDef *tp_members; struct PyGetSetDef *tp_getset; struct _typeobject *tp_base; //指向基类的指针 PyObject *tp_dict; descrgetfunc tp_descr_get; descrsetfunc tp_descr_set; Py_ssize_t tp_dictoffset; initproc tp_init; //初始化对象 allocfunc tp_alloc; newfunc tp_new; //创建对象 freefunc tp_free; inquiry tp_is_gc; PyObject *tp_bases; PyObject *tp_mro; PyObject *tp_cache; PyObject *tp_subclasses; PyObject *tp_weaklist; destructor tp_del; unsigned int tp_version_tag; #ifdef COUNT_ALLOCS Py_ssize_t tp_allocs; Py_ssize_t tp_frees; Py_ssize_t tp_maxalloc; struct _typeobject *tp_prev; struct _typeobject *tp_next; #endif } PyTypeObject;
-
PyNumberMethods
typedef struct { binaryfunc nb_add; binaryfunc nb_subtract; binaryfunc nb_multiply; binaryfunc nb_divide; binaryfunc nb_remainder; binaryfunc nb_divmod; ternaryfunc nb_power; unaryfunc nb_negative; unaryfunc nb_positive; unaryfunc nb_absolute; inquiry nb_nonzero; unaryfunc nb_invert; binaryfunc nb_lshift; binaryfunc nb_rshift; binaryfunc nb_and; binaryfunc nb_xor; binaryfunc nb_or; coercion nb_coerce; unaryfunc nb_int; unaryfunc nb_long; unaryfunc nb_float; unaryfunc nb_oct; unaryfunc nb_hex; binaryfunc nb_inplace_add; binaryfunc nb_inplace_subtract; binaryfunc nb_inplace_multiply; binaryfunc nb_inplace_divide; binaryfunc nb_inplace_remainder; ternaryfunc nb_inplace_power; binaryfunc nb_inplace_lshift; binaryfunc nb_inplace_rshift; binaryfunc nb_inplace_and; binaryfunc nb_inplace_xor; binaryfunc nb_inplace_or; binaryfunc nb_floor_divide; binaryfunc nb_true_divide; binaryfunc nb_inplace_floor_divide; binaryfunc nb_inplace_true_divide; unaryfunc nb_index; } PyNumberMethods;
-
PySequenceMethods
typedef struct { lenfunc sq_length; binaryfunc sq_concat; ssizeargfunc sq_repeat; ssizeargfunc sq_item; ssizessizeargfunc sq_slice; ssizeobjargproc sq_ass_item; ssizessizeobjargproc sq_ass_slice; objobjproc sq_contains; /* Added in release 2.0 */ binaryfunc sq_inplace_concat; ssizeargfunc sq_inplace_repeat; } PySequenceMethods;
-
PyMappingMethods
typedef struct { lenfunc mp_length; binaryfunc mp_subscript; objobjargproc mp_ass_subscript; } PyMappingMethods;
intobject.h
-
PyIntObject
typedef struct { PyObject_HEAD long ob_ival; } PyIntObject;
longobject.h
-
PyLongObject
typedef struct _longobject PyLongObject; 而_longobject的定义在longintrepr.h, 定义如下: typedef unsigned short digit; struct _longobject { PyObject_VAR_HEAD digit ob_digit[1]; };
floatobject.h
-
PyFloatObject
typedef struct { PyObject_HEAD double ob_fval; } PyFloatObject;
boolobject.h
-
PyBoolObject
typedef PyIntObject PyBoolObject;
stringobject.h
-
PyStringObject
typedef struct { PyObject_VAR_HEAD long ob_shash; int ob_sstate; char ob_sval[1]; } PyStringObject;
tupleobject.h
-
PyTupleObject
typedef struct { PyObject_VAR_HEAD PyObject *ob_item[1]; } PyTupleObject;
listobject.h
-
PyListObject
typedef struct { PyObject_VAR_HEAD PyObject **ob_item; Py_ssize_t allocated; } PyListObject;
setobject.h
-
PySetObject
typedef struct _setobject PySetObject; struct _setobject { PyObject_HEAD Py_ssize_t fill; /* # Active + # Dummy */ Py_ssize_t used; /* # Active */ /* The table contains mask + 1 slots, and that's a power of 2. * We store the mask instead of the size because the mask is more * frequently needed. */ Py_ssize_t mask; /* table points to smalltable for small tables, else to * additional malloc'ed memory. table is never NULL! This rule * saves repeated runtime null-tests. */ setentry *table; setentry *(*lookup)(PySetObject *so, PyObject *key, long hash); setentry smalltable[PySet_MINSIZE]; long hash; /* only used by frozenset objects */ PyObject *weakreflist; /* List of weak references */ };
sliceobject.h
-
PySliceObject
typedef struct { PyObject_HEAD PyObject *start, *stop, *step; /* not NULL */ } PySliceObject;
bytearrayobject.h
-
PyByteArrayObject
typedef struct { PyObject_VAR_HEAD /* XXX(nnorwitz): should ob_exports be Py_ssize_t? */ int ob_exports; /* how many buffer exports */ Py_ssize_t ob_alloc; /* How many bytes allocated */ char *ob_bytes; } PyByteArrayObject;
cellobject.h
-
PyCellObject
typedef struct { PyObject_HEAD PyObject *ob_ref; /* Content of the cell or NULL when empty */ } PyCellObject;
classobject.h
-
PyClassObject
typedef struct { PyObject_HEAD PyObject *cl_bases; /* A tuple of class objects */ PyObject *cl_dict; /* A dictionary */ PyObject *cl_name; /* A string */ /* The following three are functions or NULL */ PyObject *cl_getattr; PyObject *cl_setattr; PyObject *cl_delattr; PyObject *cl_weakreflist; /* List of weak references */ } PyClassObject;
-
PyInstanceObject
typedef struct { PyObject_HEAD PyClassObject *in_class; /* The class object */ PyObject *in_dict; /* A dictionary */ PyObject *in_weakreflist; /* List of weak references */ } PyInstanceObject;
-
PyMethodObject
typedef struct { PyObject_HEAD PyObject *im_func; /* The callable object implementing the method */ PyObject *im_self; /* The instance it is bound to, or NULL */ PyObject *im_class; /* The class that asked for the method */ PyObject *im_weakreflist; /* List of weak references */ } PyMethodObject;
cobject.h
-
PyCObject
typedef struct { PyObject_HEAD void *cobject; void *desc; void (*destructor)(void *); } PyCObject;
code.h
-
PyCodeObject
typedef struct { PyObject_HEAD int co_argcount; /* #arguments, except *args */ int co_nlocals; /* #local variables */ int co_stacksize; /* #entries needed for evaluation stack */ int co_flags; /* CO_..., see below */ PyObject *co_code; /* instruction opcodes */ PyObject *co_consts; /* list (constants used) */ PyObject *co_names; /* list of strings (names used) */ PyObject *co_varnames; /* tuple of strings (local variable names) */ PyObject *co_freevars; /* tuple of strings (free variable names) */ PyObject *co_cellvars; /* tuple of strings (cell variable names) */ /* The rest doesn't count for hash/cmp */ PyObject *co_filename; /* string (where it was loaded from) */ PyObject *co_name; /* string (name, for reference) */ int co_firstlineno; /* first source line number */ PyObject *co_lnotab; /* string (encoding addr<->lineno mapping) See Objects/lnotab_notes.txt for details. */ void *co_zombieframe; /* for optimization only (see frameobject.c) */ PyObject *co_weakreflist; /* to support weakrefs to code objects */ } PyCodeObject;
fileobject.h
-
PyFileObject
typedef struct { PyObject_HEAD FILE *f_fp; PyObject *f_name; PyObject *f_mode; int (*f_close)(FILE *); int f_softspace; /* Flag used by 'print' command */ int f_binary; /* Flag which indicates whether the file is open in binary (1) or text (0) mode */ char* f_buf; /* Allocated readahead buffer */ char* f_bufend; /* Points after last occupied position */ char* f_bufptr; /* Current buffer position */ char *f_setbuf; /* Buffer for setbuf(3) and setvbuf(3) */ int f_univ_newline; /* Handle any newline convention */ int f_newlinetypes; /* Types of newlines seen */ int f_skipnextlf; /* Skip next \n */ PyObject *f_encoding; PyObject *f_errors; PyObject *weakreflist; /* List of weak references */ int unlocked_count; /* Num. currently running sections of code using f_fp with the GIL released. */ int readable; int writable; } PyFileObject;
funcobject.h
-
PyFuncObject
typedef struct { PyObject_HEAD PyObject *func_code; /* A code object */ PyObject *func_globals; /* A dictionary (other mappings won't do) */ PyObject *func_defaults; /* NULL or a tuple */ PyObject *func_closure; /* NULL or a tuple of cell objects */ PyObject *func_doc; /* The __doc__ attribute, can be anything */ PyObject *func_name; /* The __name__ attribute, a string object */ PyObject *func_dict; /* The __dict__ attribute, a dict or NULL */ PyObject *func_weakreflist; /* List of weak references */ PyObject *func_module; /* The __module__ attribute, can be anything */ } PyFunctionObject;
frameobject.h
-
PyFrameObject
typedef struct { int b_type; /* what kind of block this is */ int b_handler; /* where to jump to find handler */ int b_level; /* value stack level to pop to */ } PyTryBlock; 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 _value and _traceback. * Desired invariant: all three are NULL, or all three are non-NULL. That * one isn't currently true, but "should be". */ PyObject *f_exc_type, *f_exc_value, *f_exc_traceback; PyThreadState *f_tstate; int f_lasti; /* Last instruction if called */ /* Call PyFrame_GetLineNumber() instead of reading this field directly. As of 2.3 f_lineno is only valid when tracing is active (i.e. when f_trace is set). At other times we use PyCode_Addr2Line to calculate the line from the current bytecode index. */ int f_lineno; /* Current line number */ int f_iblock; /* index in f_blockstack */ PyTryBlock f_blockstack[CO_MAXBLOCKS]; /* for try and loop blocks */ PyObject *f_localsplus[1]; /* locals+stack, dynamically sized */ } PyFrameObject;
genobject.h
-
PyGenObject
struct _frame; /* Avoid including frameobject.h */ typedef struct { PyObject_HEAD /* The gi_ prefix is intended to remind of generator-iterator. */ /* Note: gi_frame can be NULL if the generator is "finished" */ struct _frame *gi_frame; /* True if generator is being executed. */ int gi_running; /* The code object backing the generator */ PyObject *gi_code; /* List of weak reference. */ PyObject *gi_weakreflist; } PyGenObject;
memoryobject.h
-
PyMemoryViewObject
typedef struct { PyObject_HEAD PyObject *base; Py_buffer view; } PyMemoryViewObject;
unicodeobject.h
-
PyUnicodeObject
typedef struct { PyObject_HEAD Py_ssize_t length; /* Length of raw Unicode data in buffer */ Py_UNICODE *str; /* Raw Unicode buffer */ long hash; /* Hash value; -1 if not set */ PyObject *defenc; /* (Default) Encoded version as Python string, or NULL; this is used for implementing the buffer protocol */ } PyUnicodeObject;
methodobject.h
-
PyCFunctionObject
typedef struct { PyObject_HEAD PyMethodDef *m_ml; /* Description of the C function to call */ PyObject *m_self; /* Passed as 'self' arg to the C func, can be NULL */ PyObject *m_module; /* The __module__ attribute, can be anything */ } PyCFunctionObject;
datetime.h
-
PyDateTime_Delta
typedef struct { PyObject_HEAD long hashcode; /* -1 when unknown */ int days; /* -MAX_DELTA_DAYS <= days <= MAX_DELTA_DAYS */ int seconds; /* 0 <= seconds < 24*3600 is invariant */ int microseconds; /* 0 <= microseconds < 1000000 is invariant */ } PyDateTime_Delta;
-
PyDateTime_TZInfo
typedef struct { PyObject_HEAD /* a pure abstract base class */ } PyDateTime_TZInfo;