python framework
//object base class
typedef struct _object {
PyObject_HEAD
} PyObject;
//variable length object base class
typedef struct {
PyObject_VAR_HEAD
} PyVarObject;
PyObject_VAR_HEAD中int ob_refcnt
[object.h]
#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_HEAD defines the initial segment of every PyObject. */
#define PyObject_HEAD \
_PyObject_HEAD_EXTRA \
int ob_refcnt; \ //引用计数
struct _typeobject *ob_type; //类型对象
#define PyObject_VAR_HEAD \
PyObject_HEAD \
int ob_size; /* Number of items in variable part */
python 中对象的分类:
- math: 数值对象,int, float, long
- container:容纳其他对象的集合对象, list, set, dict
- composition:表示程序结构的对象, class,function
- internal:运行时内部使用的对象
引用计数(reference count)
类型对象不受引用计数限制,永远不会被析构(de-constructor)
PyIntObject 对象
immutable 对象
PyObject *PyInt_FromLong(long ival)
PyObject* PyInt_FromString(char *s, char **pend, int base)
#ifdef Py_USING_UNICODE
PyObject*PyInt_FromUnicode(Py_UNICODE *s, int length, int base) #endif
PyIntObject 对象是 Immutable 对象
小数对象
小数范围确定:
NSMALLPOSINTS: upper bound
NSMALLNEGINTS : lower bound
所有小数对象常驻内存
大数对象
动态使用固定内存,这块内存通过链表的形式进行组织
>>> id(a)
140392026626384
>>> id(b)
140392026626384
>>> b=300
>>> a
100
>>> id(b)
140392025569344
PyStringObject
[stringobject.h]
typedef struct
{
PyObject_VAR_HEAD long ob_shash; //cache the string hash value
int ob_sstate; // intern flag
char ob_sval[1]; // the pointer, point to the string memory
} PyStringObject;
[stringobject.c]
PyTypeObject PyString_Type =
{
PyObject_HEAD_INIT(&PyType_Type) 0,
"str",
sizeof(PyStringObject), sizeof(char),
......
(reprfunc)string_repr,
/* tp_repr */
s=“Python” 字符串的内存分布
字符缓冲池
PyListObject
typedef struct {
PyObject_VAR_HEAD
/* Vector of pointers to list elements. list[0] is ob_item[0],
etc. */
PyObject **ob_item;
int allocated; } PyListObject;
PyDictObject
Hash 方法:开放定址 法。
typedef struct
{
long me_hash; /* cached hash code of me_key */
PyObject *me_key;
PyObject *me_value;
} PyDictEntry
#define PyDict_MINSIZE 8
typedef struct _dictobject PyDictObject;
struct _dictobject
{
PyObject_HEAD
int ma_fill; /* # Active + # Dummy */
int ma_used; /* # Active */
int ma_mask;
PyDictEntry *ma_table;
PyDictEntry *(*ma_lookup)(PyDictObject *mp, PyObject *key, long hash);
PyDictEntry ma_smalltable[PyDict_MINSIZE];
};
Python 编译和运行
编译