: PyTypeObject Python 1 PyObject_*() PyType_*() Python
(standard type) C
Typedefs: unaryfunc, binaryfunc, ternaryfunc, inquiry, intargfunc, intintargfunc, intobjargproc, intintobjargproc, objobjargproc, destructor, freefunc, printfunc, getattrfunc, getattrofunc, setattrfunc, setattrofunc, cmpfunc, reprfunc, hashfunc
PyTypeObject Include/object.h :
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;
PyAsyncMethods *tp_as_async; /* formerly known as tp_compare (Python 2)
or tp_reserved (Python 3) */
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 */
unsigned long tp_flags;
const char *tp_doc; /* Documentation string */
/* call function for all accessible objects */
traverseproc tp_traverse;
/* delete references to contained objects */
inquiry tp_clear;
/* rich comparisons */
richcmpfunc tp_richcompare;
/* weak reference enabler */
Py_ssize_t tp_weaklistoffset;
/* 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;
destructor tp_finalize;
} PyTypeObject;
PyVarObject ob_size ( class type_new() ) PyType_Type () tp_itemsize () ob_size
-
PyObject*
PyObject._ob_next -
PyObject*
PyObject._ob_prev Py_TRACE_REFSPyObject_HEAD_INITNULL NULL ;PYTHONDUMPREFS
-
PyTypeObject*
PyObject.ob_type PyObject_HEAD_INIT&PyType_Type() WindowsPyObject_HEAD_INITNULL :Foo_Type.ob_type = &PyType_Type;
PyType_Ready()ob_typeNULL NULLob_typeob_typePyType_Ready()
-
const char*
PyTypeObject.tp_name NUL ;
PQMTtp_name"P.Q.M.T"'__module__'tp_name
__module____name__
-
Py_ssize_t
PyTypeObject.tp_basicsize -
Py_ssize_t
PyTypeObject.tp_itemsize -
:
tp_itemsizetp_itemsizetp_basicsizeob_sizeN ""tp_basicsizetp_itemsizeN Nob_size:ob_sizeNabs(ob_size)ob_size(ob_size)PyObject_HEADPyObject_VAR_HEAD()_ob_prev_ob_nexttp_basicsizesizeofGC:
tp_basicsize:doubletp_itemsizesizeof(double)tp_basicsizesizeof(double)(double)
-
destructor
PyTypeObject.tp_dealloc (
NoneEllipsis)Py_DECREF()Py_XDECREF()() ()tp_free(Py_TPFLAGS_BASETYPE)tp_free(deallocator) ;PyObject_New()PyObject_VarNew()PyObject_Del()PyObject_GC_New()PyObject_GC_NewVar()PyObject_GC_Del()
-
getattrfunc
PyTypeObject.tp_getattr get-attribute-string
tp_getattroPython C :PyObject * tp_getattr(PyObject *o, char *attr_name);
tp_getattro:tp_getattrtp_getattroNULLtp_getattrtp_getattro
-
setattrfunc
PyTypeObject.tp_setattr -
tp_setattroPython C :PyObject * tp_setattr(PyObject *o, char *attr_name, PyObject *v);
v NULL
tp_setattro:tp_setattrtp_setattroNULLtp_setattrtp_setattro
-
PyAsyncMethods*
tp_as_async C awaitable asynchronous iterator async
3.5 :
tp_comparetp_reserved
-
reprfunc
PyTypeObject.tp_repr -
PyObject_Repr()Unicodeeval()'<''>'<%s object at %p>%s%p
-
PyNumberMethods*
tp_as_number -
tp_as_number
-
PySequenceMethods*
tp_as_sequence -
tp_as_sequence
-
PyMappingMethods*
tp_as_mapping -
tp_as_mapping
-
hashfunc
PyTypeObject.tp_hash -
PyObject_Hash()Py_hash_t-1;-1PyObject_HashNotImplemented()type Python__hash__ = Noneisinstance(o, collections.Hashable)False- Python__hash__ = Nonetp_hashPyObject_HashNotImplemented()tp_richcompare:tp_richcomparetp_hashNULLtp_richcomparetp_hash
-
ternaryfunc
PyTypeObject.tp_call NULL
PyObject_Call()
-
reprfunc
PyTypeObject.tp_str str()(strstr()strPyObject_Str()PyObject_Str())PyObject_Str(); Unicode " (friendly)"print()
-
getattrofunc
PyTypeObject.tp_getattro get-attribute
PyObject_GetAttr()PyObject_GenericGetAttr()tp_getattr:tp_getattrtp_getattroNULLtp_getattrtp_getattro
-
setattrofunc
PyTypeObject.tp_setattro -
PyObject_SetAttr()v NULLPyObject_GenericSetAttr()tp_setattr:tp_setattrtp_setattroNULLtp_setattrtp_setattro
-
PyBufferProcs*
PyTypeObject.tp_as_buffer
-
unsigned long
PyTypeObject.tp_flags ; (
tp_as_numbertp_as_sequencetp_as_mappingtp_as_buffer) ; NULLPy_TPFLAGS_HAVE_GCtp_traversetp_clearPy_TPFLAGS_HAVE_GCtp_traversetp_clearNULL;
|tp_flagsPyType_HasFeature()tp ftp->tp_flags & f-
Py_TPFLAGS_HAVE_GC (GC)
PyObject_GC_New()PyObject_GC_Del()GCtp_traversetp_clear
-
-
traverseproc
PyTypeObject.tp_traverse (traversal function)
Py_TPFLAGS_HAVE_GCPythontp_traversetp_traversePythonPy_VISIT()_threadlocal_traverse():static int local_traverse(localobject *self, visitproc visit, void *arg) { Py_VISIT(self->args); Py_VISIT(self->kw); Py_VISIT(self->dict); return 0; }
Py_VISIT()self->keyNULL PythonPy_VISIT()local_traverse()visit argtp_clearPy_TPFLAGS_HAVE_GC:tp_traversetp_cleartp_traversetp_clear
-
inquiry
PyTypeObject.tp_clear (clear function)
Py_TPFLAGS_HAVE_GCtp_clearGCtp_clear(:tp_clear)tp_cleartp_cleartp_cleartp_cleartp_clearPython NULL :static int local_clear(localobject *self) { Py_CLEAR(self->key); Py_CLEAR(self->args); Py_CLEAR(self->kw); Py_CLEAR(self->dict); return 0; }
Py_CLEAR(): NULL () Python self NULL selfPy_CLEAR()tp_clearPython Python Pythontp_dealloctp_cleartp_traversePy_TPFLAGS_HAVE_GC:tp_traversetp_cleartp_traversetp_clear
-
richcmpfunc
PyTypeObject.tp_richcompare PyObject *tp_richcompare(PyObject *a, PyObject *b, int op)1PyTypeObject(
Py_TruePy_False)Py_NotImplementedNULL(
==!=<)TypeErrortp_hash:tp_richcomparetp_hashNULLtp_richcomparetp_hashtp_richcomparePyObject_RichCompare():Py_LT<Py_LE<=Py_EQ==Py_NE!=Py_GT>Py_GE>=
-
Py_ssize_t
PyTypeObject.tp_weaklistoffset (GC );
PyObject_ClearWeakRefs()PyWeakref_*()NULLPyObject*class__slots__tp_weaklistoffset__slots____weakref__tp_weaklistoffset__slots____weakref__tp_weaklistoffset
-
iternextfunc
PyTypeObject.tp_iternext NULL
StopIterationNULLtp_iter()
-
struct PyMethodDef*
PyTypeObject.tp_methods (regular)
PyMethodDefNULL(
tp_dict)()
-
struct PyMemberDef*
PyTypeObject.tp_members (regular) ()
PyMemberDefNULL(
tp_dict)()
-
struct PyGetSetDef*
PyTypeObject.tp_getset (computed attribute)
PyGetSetDefNULLgetter/setter (
tp_dict)()
-
PyTypeObject*
PyTypeObject.tp_base (single inheritance) ;
() (Python
object)&PyBaseObject_Type
-
PyObject*
PyTypeObject.tp_dict -
PyType_Ready()NULL ;PyType_Ready()(__add__())()
tp_dictPyDict_SetItem()C-API
-
descrgetfunc
PyTypeObject.tp_descr_get get
PyObject * tp_descr_get(PyObject *self, PyObject *obj, PyObject *type);
-
descrsetfunc
PyTypeObject.tp_descr_set -
int tp_descr_set(PyObject *self, PyObject *obj, PyObject *value);
value NULL
-
Py_ssize_t
PyTypeObject.tp_dictoffset -
tp_dict;strtupletp_basicsize4tp_dictoffset-4dictoffset = tp_basicsize + abs(ob_size)*tp_itemsize + tp_dictoffset if dictoffset is not aligned on sizeof(void*): round up to sizeof(void*)
tp_basicsizetp_itemsizetp_dictoffsetob_sizeob_size(;_PyObject_GetDictPtr())class__slots__tp_dictoffset(
__dict____slots____weakref__)
-
initproc
PyTypeObject.tp_init -
__init__()__init__()__init__()__init__()int tp_init(PyObject *self, PyObject *args, PyObject *kwds)
self ; args kwds
__init__()
-
allocfunc
PyTypeObject.tp_alloc -
PyObject *tp_alloc(PyTypeObject *self, Py_ssize_t nitems)
ob_refcnt1ob_type(type argument)tp_itemsizeob_sizenitemstp_basicsize + nitems*tp_itemsizesizeof(void*); nitemstp_basicsize;
tp_new
-
newfunc
PyTypeObject.tp_new -
NULL ;
PyObject *tp_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds)
subtype ; args kwds
tp_new; ()tp_newsubtype->tp_alloc(subtype, nitems)tp_inittp_newtp_inittp_baseNULL&PyBaseObject_Type
-
inquiry
PyTypeObject.tp_is_gc -
tp_flagsPy_TPFLAGS_HAVE_GC;10int tp_is_gc(PyObject *self)
(
PyType_Type)
-
destructor
PyTypeObject.tp_finalize destructor:void tp_finalize(PyObject *)
tp_finalize1 ()tp_finalize; :static void local_finalize(PyObject *self) { PyObject *error_type, *error_value, *error_traceback; /* Save the current exception, if any. */ PyErr_Fetch(&error_type, &error_value, &error_traceback); /* ... */ /* Restore the saved exception. */ PyErr_Restore(error_type, error_value, error_traceback); }
3.4 .
"" (PEP 442)
-
PyObject*
PyTypeObject.tp_cache
-
PyObject*
PyTypeObject.tp_subclasses
-
PyObject*
PyTypeObject.tp_weaklist
COUNT_ALLOCS
-
PyTypeObject*
PyTypeObject.tp_next
Python tp_dealloc Python ()Python API tp_dealloc (GIL: Global Interpreter Lock) C C++ tp_dealloc
-
PyNumberMethods -
:
typedef struct { binaryfunc nb_add; binaryfunc nb_subtract; binaryfunc nb_multiply; binaryfunc nb_remainder; binaryfunc nb_divmod; ternaryfunc nb_power; unaryfunc nb_negative; unaryfunc nb_positive; unaryfunc nb_absolute; inquiry nb_bool; unaryfunc nb_invert; binaryfunc nb_lshift; binaryfunc nb_rshift; binaryfunc nb_and; binaryfunc nb_xor; binaryfunc nb_or; unaryfunc nb_int; void *nb_reserved; unaryfunc nb_float; binaryfunc nb_inplace_add; binaryfunc nb_inplace_subtract; binaryfunc nb_inplace_multiply; 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; binaryfunc nb_matrix_multiply; binaryfunc nb_inplace_matrix_multiply; } PyNumberMethods;
().
Py_NotImplementedNULLnb_reservedNULLnb_longPython 3.0.1
-
lenfunc
PyMappingMethods.mp_length This function is used by
PyMapping_Length()andPyObject_Size(), and has the same signature. This slot may be set to NULL if the object has no defined length.
-
binaryfunc
PyMappingMethods.mp_subscript This function is used by
PyObject_GetItem()and has the same signature. This slot must be filled for thePyMapping_Check()function to return1, it can be NULL otherwise.
-
objobjargproc
PyMappingMethods.mp_ass_subscript This function is used by
PyObject_SetItem()andPyObject_DelItem(). It has the same signature asPyObject_SetItem(), but v can also be set to NULL to delete an item. If this slot is NULL, the object does not support item assignment and deletion.
-
lenfunc
PySequenceMethods.sq_length This function is used by
PySequence_Size()andPyObject_Size(), and has the same signature.
-
binaryfunc
PySequenceMethods.sq_concat This function is used by
PySequence_Concat()and has the same signature. It is also used by the+operator, after trying the numeric addition via thenb_addslot.
-
ssizeargfunc
PySequenceMethods.sq_repeat This function is used by
PySequence_Repeat()and has the same signature. It is also used by the*operator, after trying numeric multiplication via thenb_multiplyslot.
-
ssizeargfunc
PySequenceMethods.sq_item This function is used by
PySequence_GetItem()and has the same signature. This slot must be filled for thePySequence_Check()function to return1, it can be NULL otherwise.:
sq_lengthsq_itemsq_lengthNULL
-
ssizeobjargproc
PySequenceMethods.sq_ass_item This function is used by
PySequence_SetItem()and has the same signature. This slot may be left to NULL if the object does not support item assignment and deletion.
-
objobjproc
PySequenceMethods.sq_contains This function may be used by
PySequence_Contains()and has the same signature. This slot may be left to NULL, in this casePySequence_Contains()simply traverses the sequence until it finds a match.
-
binaryfunc
PySequenceMethods.sq_inplace_concat This function is used by
PySequence_InPlaceConcat()and has the same signature. It should modify its first operand, and return it.
-
ssizeargfunc
PySequenceMethods.sq_inplace_repeat This function is used by
PySequence_InPlaceRepeat()and has the same signature. It should modify its first operand, and return it.
(buffer object structure)
-
getbufferproc
PyBufferProcs.bf_getbuffer :
int (PyObject *exporter, Py_buffer *view, int flags);
flags view exporter (3) :
-
PyExc_BufferErrorview->objNULL-1 view->objexporterview->obj0
exporter :
- :
view->obj - :
view->obj
-
-
releasebufferproc
PyBufferProcs.bf_releasebuffer :
void (PyObject *exporter, Py_buffer *view);
PyBufferProcs.bf_releasebufferNULL (optional step) :-
0view
internalviewview->objPyBuffer_Release()()-
async
3.5 .
-
PyAsyncMethods awaitable asynchronous iterator
:
typedef struct { unaryfunc am_await; unaryfunc am_aiter; unaryfunc am_anext; } PyAsyncMethods;
-
unaryfunc
PyAsyncMethods.am_await :
PyObject *am_await(PyObject *self)
awaitable NULL
-
unaryfunc
PyAsyncMethods.am_anext :
PyObject *am_anext(PyObject *self)
awaitable
__anext__()NULL