(common object structure)

Python

Python PyObject PyVarObject Python

PyObject

Python "" PyObject Python PyObject* Py_REFCNT Py_TYPE

PyVarObject

PyObject ob_size (length) Python/C API Py_REFCNT Py_TYPE Py_SIZE

PyObject_HEAD

PyObject_HEAD :

PyObject ob_base;

PyObject

PyObject_VAR_HEAD

PyObject_VAR_HEAD :

PyVarObject ob_base;

PyVarObject

Py_TYPE(o)

Python ob_type :

(((PyObject*)(o))->ob_type)
Py_REFCNT(o)

Python ob_refcnt :

(((PyObject*)(o))->ob_refcnt)
Py_SIZE(o)

Python ob_size :

(((PyVarObject*)(o))->ob_size)
PyObject_HEAD_INIT(type)

PyObject

_PyObject_EXTRA_INIT
1, type,
PyVarObject_HEAD_INIT(type, size)

ob_size PyVarObject

_PyObject_EXTRA_INIT
1, type, size,
PyCFunction

Python C 2 PyObject* PyObject* NULL NULL Python

PyCFunctionWithKeywords

Python C : 3 PyObject* PyObject* PyCFunction

PyMethodDef

4 :

C
ml_name char *
ml_meth PyCFunction C
ml_flags int
ml_doc char * docstring

ml_meth C PyObject* PyFunction PyCFunction PyObject* C self

ml_flags (calling convention) (binding convention) METH_VARARGS METH_KEYWORDS

METH_VARARGS

PyCFunction PyObject* self ; ( args ) PyArg_ParseTuple() PyArg_UnpackTuple()

METH_KEYWORDS

PyCFunctionWithKeywords 3 : self args METH_VARARGS PyArg_ParseTupleAndKeywords()

METH_NOARGS

METH_NOARGS PyCFunction self NULL

METH_O

PyArg_ParseTuple() "O" METH_O PyCFunction self PyObject*

METH_CLASS

classmethod() (class method)

METH_STATIC

NULL staticmethod() (static method)

METH_COEXIST

METH_COEXIST sq_contains __contains__() PyCFunction PyCFunction PyCFunctions

PyMemberDef

type C :

C
name char *
type int C
offset Py_ssize_t type object offset
flags int
doc char * docstring

type C T_ 1 Python Python

C
T_SHORT short
T_INT int
T_LONG long
T_FLOAT
T_DOUBLE double
T_STRING char *
T_OBJECT PyObject *
T_OBJECT_EX PyObject *
T_CHAR char
T_BYTE char
T_UBYTE unsigned char
T_UINT unsigned int
T_USHORT unsigned short
T_ULONG unsigned long
T_BOOL char
T_LONGLONG long long
T_ULONGLONG unsigned long long
T_PYSSIZET Py_ssize_t

T_OBJECT T_OBJECT_EX T_OBJECT NULL None T_OBJECT_EX AttributeError T_OBJECT_EX T_OBJECT del T_OBJECT T_OBJECT_EX

flags 0 READONLY type T_STRING READONLY T_OBJECT T_OBJECT_EX (NULL )

PyGetSetDef

Structure to define property-like access for a type. See also description of the PyTypeObject.tp_getset slot.

C
char * attribute name
get getter C Function to get the attribute
setter optional C function to set or delete the attribute, if omitted the attribute is readonly
doc char * optional docstring
closure void * optional function pointer, providing additional data for getter and setter

The get function takes one PyObject* parameter (the instance) and a function pointer (the associated closure):

typedef PyObject *(*getter)(PyObject *, void *);

It should return a new reference on success or NULL with a set exception on failure.

set functions take two PyObject* parameters (the instance and the value to be set) and a function pointer (the associated closure):

typedef int (*setter)(PyObject *, PyObject *, void *);

In case the attribute should be deleted the second parameter is NULL. Should return 0 on success or -1 with a set exception on failure.