阅读源码的版本: cmmit id: 9e4f2f3a6b8ee995c365e86d976937c141d867f8

在阅读Python的源码时,我们第一个想到的就是先去阅读Python的数据结构的源码。

那么,我们先去挨着看看。

数据结构

long

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11

struct _longobject {

    PyObject_VAR_HEAD

    digit ob_digit[1];

};



list

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

typedef struct {

    PyObject_VAR_HEAD

    /* Vector of pointers to list elements.  list[0] is ob_item[0], etc. */

    PyObject **ob_item;



    /* ob_item contains space for 'allocated' elements.  The number

     * currently in use is ob_size.

     * Invariants:

     *     0 <= ob_size <= allocated

     *     len(list) == ob_size

     *     ob_item == NULL implies ob_size == allocated == 0

     * list.sort() temporarily sets allocated to -1 to detect mutations.

     *

     * Items must normally not be NULL, except during construction when

     * the list is not yet visible outside the function that builds it.

     */

    Py_ssize_t allocated;

} PyListObject;

tuple

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15

typedef struct {

    PyObject_VAR_HEAD

    /* ob_item contains space for 'ob_size' elements.

       Items must normally not be NULL, except during construction when

       the tuple is not yet visible outside the function that builds it. */

    PyObject *ob_item[1];

} PyTupleObject;

纵观上面几种数据结构,我们发现,在struct的开头都嵌入了一个叫做PyObject_HEAD的宏,那么这个宏定义到底是什么呢?

PyObject_VAR_HEAD 定义

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

/* PyObject_VAR_HEAD defines the initial segment of all variable-size

 * container objects.  These end with a declaration of an array with 1

 * element, but enough space is malloc'ed so that the array actually

 * has room for ob_size elements.  Note that ob_size is an element count,

 * not necessarily a byte count.

 */

 /* PyObject_VAR_HEAD定义所有可变长度对象的首段信息。

  * 这些对象在结束部分会声明一个长度为1的数组,

  * 但是实际上已经有充足的空间申请,以至于这个数组实际上只有ob_size个元素。

  * 注意这个ob_size是一个元素数量。

  */

#define PyObject_VAR_HEAD      PyVarObject ob_base;

PyVarObject 定义

1
2
3
4
5
6
7
8
9

typedef struct {

    PyObject ob_base;

    Py_ssize_t ob_size; /* Number of items in variable part */

} PyVarObject;

PyObject 定义

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21

/* Nothing is actually declared to be a PyObject, but every pointer to

 * a Python object can be cast to a PyObject*.  This is inheritance built

 * by hand.  Similarly every pointer to a variable-size Python object can,

 * in addition, be cast to PyVarObject*.

 */

typedef struct _object {

    _PyObject_HEAD_EXTRA

    Py_ssize_t ob_refcnt;

    struct _typeobject *ob_type;

} PyObject;

_PyObject_HEAD_EXTRA

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

#ifdef Py_TRACE_REFS

/* Define pointers to support a doubly-linked list of all live heap objects. */

#define _PyObject_HEAD_EXTRA            \

    struct _object *_ob_next;           \

    struct _object *_ob_prev;



#define _PyObject_EXTRA_INIT 0, 0,



#else

#define _PyObject_HEAD_EXTRA

#define _PyObject_EXTRA_INIT

#endif

所以,我们最终可以解开这个所谓的PyObject_VAR_HEAD宏到底是什么了:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17

struc PyObject_VAR_HEAD

{

	 struct _object *_ob_next;

	 struct _object *_ob_prev;

	 Py_ssize_t ob_refcnt;

	 struct _typeobject *ob_type;

	 Py_ssize_t ob_size;

}