As mentioned in the last chapter, Python allows the writer of an extensionmodule to define new types that can be manipulated from Python code, much likestrings and lists in core Python.
This is not hard; the code for all extension types follows a pattern, but thereare some details that you need to understand before you can get started.
The Python runtime sees all Python objects as variables of typePyObject*, which serves as a “base type” for all Python objects.PyObject itself only contains the refcount and a pointer to theobject’s “type object”. This is where the action is; the type object determineswhich (C) functions get called when, for instance, an attribute gets lookedup on an object or it is multiplied by another object. These C functionsare called “type methods”.
So, if you want to define a new object type, you need to create a new typeobject.
This sort of thing can only be explained by example, so here’s a minimal, butcomplete, module that defines a new type:
#include <Python.h>typedefstruct{PyObject_HEAD/* Type-specific fields go here. */}noddy_NoddyObject;staticPyTypeObjectnoddy_NoddyType={PyVarObject_HEAD_INIT(NULL,0)"noddy.Noddy",/* tp_name */sizeof(noddy_NoddyObject),/* tp_basicsize */0,/* tp_itemsize */0,/* tp_dealloc */0,/* tp_print */0,/* tp_getattr */0,/* tp_setattr */0,/* tp_reserved */0,/* tp_repr */0,/* tp_as_number */0,/* tp_as_sequence */0,/* tp_as_mapping */0,/* tp_hash */0,/* tp_call */0,/* tp_str */0,/* tp_getattro */0,/* tp_setattro */0,/* tp_as_buffer */Py_TPFLAGS_DEFAULT,/* tp_flags */"Noddy objects",/* tp_doc */};staticPyModuleDefnoddymodule={PyModuleDef_HEAD_INIT,"noddy","Example module that creates an extension type.",-1,NULL,NULL,NULL,NULL,NULL};PyMODINIT_FUNCPyInit_noddy(void){PyObject*m;noddy_NoddyType.tp_new=PyType_GenericNew;if(PyType_Ready(&noddy_NoddyType)<0)returnNULL;m=PyModule_Create(&noddymodule);if(m==NULL)returnNULL;Py_INCREF(&noddy_NoddyType);PyModule_AddObject(m,"Noddy",(PyObject*)&noddy_NoddyType);returnm;}
Now that’s quite a bit to take in at once, but hopefully bits will seem familiarfrom the last chapter.
The first bit that will be new is:
typedefstruct{PyObject_HEAD}noddy_NoddyObject;
This is what a Noddy object will contain—in this case, nothing more than whatevery Python object contains—a refcount and a pointer to a type object.These are the fields thePyObject_HEAD macro brings in. The reason for themacro is to standardize the layout and to enable special debugging fields indebug builds. Note that there is no semicolon after thePyObject_HEADmacro; one is included in the macro definition. Be wary of adding one byaccident; it’s easy to do from habit, and your compiler might not complain,but someone else’s probably will! (On Windows, MSVC is known to call this anerror and refuse to compile the code.)
For contrast, let’s take a look at the corresponding definition for standardPython floats:
typedefstruct{PyObject_HEADdoubleob_fval;}PyFloatObject;
Moving on, we come to the crunch — the type object.
staticPyTypeObjectnoddy_NoddyType={PyVarObject_HEAD_INIT(NULL,0)"noddy.Noddy",/* tp_name */sizeof(noddy_NoddyObject),/* tp_basicsize */0,/* tp_itemsize */0,/* tp_dealloc */0,/* tp_print */0,/* tp_getattr */0,/* tp_setattr */0,/* tp_reserved */0,/* tp_repr */0,/* tp_as_number */0,/* tp_as_sequence */0,/* tp_as_mapping */0,/* tp_hash */0,/* tp_call */0,/* tp_str */0,/* tp_getattro */0,/* tp_setattro */0,/* tp_as_buffer */Py_TPFLAGS_DEFAULT,/* tp_flags */"Noddy objects",/* tp_doc */};
Now if you go and look up the definition ofPyTypeObject inobject.h you’ll see that it has many more fields that the definitionabove. The remaining fields will be filled with zeros by the C compiler, andit’s common practice to not specify them explicitly unless you need them.
This is so important that we’re going to pick the top of it apart stillfurther:
PyVarObject_HEAD_INIT(NULL,0)
This line is a bit of a wart; what we’d like to write is:
PyVarObject_HEAD_INIT(&PyType_Type,0)
as the type of a type object is “type”, but this isn’t strictly conforming C andsome compilers complain. Fortunately, this member will be filled in for us byPyType_Ready().
"noddy.Noddy",/* tp_name */
The name of our type. This will appear in the default textual representation ofour objects and in some error messages, for example:
>>>""+noddy.new_noddy()Traceback(mostrecentcalllast):File"<stdin>",line1,in?TypeError:cannotaddtype"noddy.Noddy"tostring
Note that the name is a dotted name that includes both the module name and thename of the type within the module. The module in this case isnoddy andthe type isNoddy, so we set the type name tonoddy.Noddy.
sizeof(noddy_NoddyObject),/* tp_basicsize */
This is so that Python knows how much memory to allocate when you callPyObject_New().
Note
If you want your type to be subclassable from Python, and your type has the sametp_basicsize as its base type, you may have problems with multipleinheritance. A Python subclass of your type will have to list your type firstin its__bases__, or else it will not be able to call your type’s__new__() method without getting an error. You can avoid this problem byensuring that your type has a larger value fortp_basicsize than itsbase type does. Most of the time, this will be true anyway, because either yourbase type will beobject, or else you will be adding data members toyour base type, and therefore increasing its size.
0,/* tp_itemsize */
This has to do with variable length objects like lists and strings. Ignore thisfor now.
Skipping a number of type methods that we don’t provide, we set the class flagstoPy_TPFLAGS_DEFAULT.
Py_TPFLAGS_DEFAULT,/* tp_flags */
All types should include this constant in their flags. It enables all of themembers defined by the current version of Python.
We provide a doc string for the type intp_doc.
"Noddy objects",/* tp_doc */
Now we get into the type methods, the things that make your objects differentfrom the others. We aren’t going to implement any of these in this version ofthe module. We’ll expand this example later to have more interesting behavior.
For now, all we want to be able to do is to create newNoddy objects.To enable object creation, we have to provide atp_new implementation.In this case, we can just use the default implementation provided by the APIfunctionPyType_GenericNew(). We’d like to just assign this to thetp_new slot, but we can’t, for portability sake, On some platforms orcompilers, we can’t statically initialize a structure member with a functiondefined in another C module, so, instead, we’ll assign thetp_new slotin the module initialization function just before callingPyType_Ready():
noddy_NoddyType.tp_new=PyType_GenericNew;if(PyType_Ready(&noddy_NoddyType)<0)return;
All the other type methods areNULL, so we’ll go over them later — that’sfor a later section!
Everything else in the file should be familiar, except for some code inPyInit_noddy():
if(PyType_Ready(&noddy_NoddyType)<0)return;
This initializes theNoddy type, filing in a number of members,includingob_type that we initially set toNULL.
PyModule_AddObject(m,"Noddy",(PyObject*)&noddy_NoddyType);
This adds the type to the module dictionary. This allows us to createNoddy instances by calling theNoddy class:
>>>importnoddy>>>mynoddy=noddy.Noddy()
That’s it! All that remains is to build it; put the above code in a file callednoddy.c and
fromdistutils.coreimportsetup,Extensionsetup(name="noddy",version="1.0",ext_modules=[Extension("noddy",["noddy.c"])])
in a file calledsetup.py; then typing
$ python setup.py build
at a shell should produce a filenoddy.so in a subdirectory; move tothat directory and fire up Python — you should be able toimportnoddy andplay around with Noddy objects.
That wasn’t so hard, was it?
Of course, the current Noddy type is pretty uninteresting. It has no data anddoesn’t do anything. It can’t even be subclassed.
Let’s extend the basic example to add some data and methods. Let’s also makethe type usable as a base class. We’ll create a new module,noddy2 thatadds these capabilities:
#include <Python.h>#include "structmember.h"typedefstruct{PyObject_HEADPyObject*first;/* first name */PyObject*last;/* last name */intnumber;}Noddy;staticvoidNoddy_dealloc(Noddy*self){Py_XDECREF(self->first);Py_XDECREF(self->last);Py_TYPE(self)->tp_free((PyObject*)self);}staticPyObject*Noddy_new(PyTypeObject*type,PyObject*args,PyObject*kwds){Noddy*self;self=(Noddy*)type->tp_alloc(type,0);if(self!=NULL){self->first=PyUnicode_FromString("");if(self->first==NULL){Py_DECREF(self);returnNULL;}self->last=PyUnicode_FromString("");if(self->last==NULL){Py_DECREF(self);returnNULL;}self->number=0;}return(PyObject*)self;}staticintNoddy_init(Noddy*self,PyObject*args,PyObject*kwds){PyObject*first=NULL,*last=NULL,*tmp;staticchar*kwlist[]={"first","last","number",NULL};if(!PyArg_ParseTupleAndKeywords(args,kwds,"|OOi",kwlist,&first,&last,&self->number))return-1;if(first){tmp=self->first;Py_INCREF(first);self->first=first;Py_XDECREF(tmp);}if(last){tmp=self->last;Py_INCREF(last);self->last=last;Py_XDECREF(tmp);}return0;}staticPyMemberDefNoddy_members[]={{"first",T_OBJECT_EX,offsetof(Noddy,first),0,"first name"},{"last",T_OBJECT_EX,offsetof(Noddy,last),0,"last name"},{"number",T_INT,offsetof(Noddy,number),0,"noddy number"},{NULL}/* Sentinel */};staticPyObject*Noddy_name(Noddy*self){if(self->first==NULL){PyErr_SetString(PyExc_AttributeError,"first");returnNULL;}if(self->last==NULL){PyErr_SetString(PyExc_AttributeError,"last");returnNULL;}returnPyUnicode_FromFormat("%S %S",self->first,self->last);}staticPyMethodDefNoddy_methods[]={{"name",(PyCFunction)Noddy_name,METH_NOARGS,"Return the name, combining the first and last name"},{NULL}/* Sentinel */};staticPyTypeObjectNoddyType={PyVarObject_HEAD_INIT(NULL,0)"noddy.Noddy",/* tp_name */sizeof(Noddy),/* tp_basicsize */0,/* tp_itemsize */(destructor)Noddy_dealloc,/* tp_dealloc */0,/* tp_print */0,/* tp_getattr */0,/* tp_setattr */0,/* tp_reserved */0,/* tp_repr */0,/* tp_as_number */0,/* tp_as_sequence */0,/* tp_as_mapping */0,/* tp_hash */0,/* tp_call */0,/* tp_str */0,/* tp_getattro */0,/* tp_setattro */0,/* tp_as_buffer */Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,/* tp_flags */"Noddy objects",/* tp_doc */0,/* tp_traverse */0,/* tp_clear */0,/* tp_richcompare */0,/* tp_weaklistoffset */0,/* tp_iter */0,/* tp_iternext */Noddy_methods,/* tp_methods */Noddy_members,/* tp_members */0,/* tp_getset */0,/* tp_base */0,/* tp_dict */0,/* tp_descr_get */0,/* tp_descr_set */0,/* tp_dictoffset */(initproc)Noddy_init,/* tp_init */0,/* tp_alloc */Noddy_new,/* tp_new */};staticPyModuleDefnoddy2module={PyModuleDef_HEAD_INIT,"noddy2","Example module that creates an extension type.",-1,NULL,NULL,NULL,NULL,NULL};PyMODINIT_FUNCPyInit_noddy2(void){PyObject*m;if(PyType_Ready(&NoddyType)<0)returnNULL;m=PyModule_Create(&noddy2module);if(m==NULL)returnNULL;Py_INCREF(&NoddyType);PyModule_AddObject(m,"Noddy",(PyObject*)&NoddyType);returnm;}
This version of the module has a number of changes.
We’ve added an extra include:
#include <structmember.h>This include provides declarations that we use to handle attributes, asdescribed a bit later.
The name of theNoddy object structure has been shortened toNoddy. The type object name has been shortened toNoddyType.
TheNoddy type now has three data attributes,first,last, andnumber. Thefirst andlast variables are Python strings containing firstand last names. Thenumber attribute is an integer.
The object structure is updated accordingly:
typedefstruct{PyObject_HEADPyObject*first;PyObject*last;intnumber;}Noddy;
Because we now have data to manage, we have to be more careful about objectallocation and deallocation. At a minimum, we need a deallocation method:
staticvoidNoddy_dealloc(Noddy*self){Py_XDECREF(self->first);Py_XDECREF(self->last);Py_TYPE(self)->tp_free((PyObject*)self);}
which is assigned to thetp_dealloc member:
(destructor)Noddy_dealloc,/*tp_dealloc*/
This method decrements the reference counts of the two Python attributes. We usePy_XDECREF() here because thefirst andlast memberscould beNULL. It then calls thetp_free member of the object’s typeto free the object’s memory. Note that the object’s type might not beNoddyType, because the object may be an instance of a subclass.
We want to make sure that the first and last names are initialized to emptystrings, so we provide a new method:
staticPyObject*Noddy_new(PyTypeObject*type,PyObject*args,PyObject*kwds){Noddy*self;self=(Noddy*)type->tp_alloc(type,0);if(self!=NULL){self->first=PyUnicode_FromString("");if(self->first==NULL){Py_DECREF(self);returnNULL;}self->last=PyUnicode_FromString("");if(self->last==NULL){Py_DECREF(self);returnNULL;}self->number=0;}return(PyObject*)self;}
and install it in thetp_new member:
Noddy_new,/* tp_new */
The new member is responsible for creating (as opposed to initializing) objectsof the type. It is exposed in Python as the__new__() method. See thepaper titled “Unifying types and classes in Python” for a detailed discussion ofthe__new__() method. One reason to implement a new method is to assurethe initial values of instance variables. In this case, we use the new methodto make sure that the initial values of the membersfirst andlast are notNULL. If we didn’t care whether the initial values wereNULL, we could have usedPyType_GenericNew() as our new method, as wedid before.PyType_GenericNew() initializes all of the instance variablemembers toNULL.
The new method is a static method that is passed the type being instantiated andany arguments passed when the type was called, and that returns the new objectcreated. New methods always accept positional and keyword arguments, but theyoften ignore the arguments, leaving the argument handling to initializermethods. Note that if the type supports subclassing, the type passed may not bethe type being defined. The new method calls thetp_alloc slot toallocate memory. We don’t fill thetp_alloc slot ourselves. RatherPyType_Ready() fills it for us by inheriting it from our base class,which isobject by default. Most types use the default allocation.
Note
If you are creating a co-operativetp_new (one that calls a base type’stp_new or__new__()), you mustnot try to determine what methodto call using method resolution order at runtime. Always statically determinewhat type you are going to call, and call itstp_new directly, or viatype->tp_base->tp_new. If you do not do this, Python subclasses of yourtype that also inherit from other Python-defined classes may not work correctly.(Specifically, you may not be able to create instances of such subclasseswithout getting aTypeError.)
We provide an initialization function:
staticintNoddy_init(Noddy*self,PyObject*args,PyObject*kwds){PyObject*first=NULL,*last=NULL,*tmp;staticchar*kwlist[]={"first","last","number",NULL};if(!PyArg_ParseTupleAndKeywords(args,kwds,"|OOi",kwlist,&first,&last,&self->number))return-1;if(first){tmp=self->first;Py_INCREF(first);self->first=first;Py_XDECREF(tmp);}if(last){tmp=self->last;Py_INCREF(last);self->last=last;Py_XDECREF(tmp);}return0;}
by filling thetp_init slot.
(initproc)Noddy_init,/* tp_init */
Thetp_init slot is exposed in Python as the__init__() method. Itis used to initialize an object after it’s created. Unlike the new method, wecan’t guarantee that the initializer is called. The initializer isn’t calledwhen unpickling objects and it can be overridden. Our initializer acceptsarguments to provide initial values for our instance. Initializers always acceptpositional and keyword arguments.
Initializers can be called multiple times. Anyone can call the__init__()method on our objects. For this reason, we have to be extra careful whenassigning the new values. We might be tempted, for example to assign thefirst member like this:
if(first){Py_XDECREF(self->first);Py_INCREF(first);self->first=first;}
But this would be risky. Our type doesn’t restrict the type of thefirst member, so it could be any kind of object. It could have adestructor that causes code to be executed that tries to access thefirst member. To be paranoid and protect ourselves against thispossibility, we almost always reassign members before decrementing theirreference counts. When don’t we have to do this?
We want to expose our instance variables as attributes. There are anumber of ways to do that. The simplest way is to define member definitions:
staticPyMemberDefNoddy_members[]={{"first",T_OBJECT_EX,offsetof(Noddy,first),0,"first name"},{"last",T_OBJECT_EX,offsetof(Noddy,last),0,"last name"},{"number",T_INT,offsetof(Noddy,number),0,"noddy number"},{NULL}/* Sentinel */};
and put the definitions in thetp_members slot:
Noddy_members,/* tp_members */
Each member definition has a member name, type, offset, access flags anddocumentation string. See theGeneric Attribute Management section below fordetails.
A disadvantage of this approach is that it doesn’t provide a way to restrict thetypes of objects that can be assigned to the Python attributes. We expect thefirst and last names to be strings, but any Python objects can be assigned.Further, the attributes can be deleted, setting the C pointers toNULL. Eventhough we can make sure the members are initialized to non-NULL values, themembers can be set toNULL if the attributes are deleted.
We define a single method,name(), that outputs the objects name as theconcatenation of the first and last names.
staticPyObject*Noddy_name(Noddy*self){if(self->first==NULL){PyErr_SetString(PyExc_AttributeError,"first");returnNULL;}if(self->last==NULL){PyErr_SetString(PyExc_AttributeError,"last");returnNULL;}returnPyUnicode_FromFormat("%S %S",self->first,self->last);}
The method is implemented as a C function that takes aNoddy (orNoddy subclass) instance as the first argument. Methods always take aninstance as the first argument. Methods often take positional and keywordarguments as well, but in this case we don’t take any and don’t need to accepta positional argument tuple or keyword argument dictionary. This method isequivalent to the Python method:
defname(self):return"%s %s"%(self.first,self.last)
Note that we have to check for the possibility that ourfirst andlast members areNULL. This is because they can be deleted, in whichcase they are set toNULL. It would be better to prevent deletion of theseattributes and to restrict the attribute values to be strings. We’ll see how todo that in the next section.
Now that we’ve defined the method, we need to create an array of methoddefinitions:
staticPyMethodDefNoddy_methods[]={{"name",(PyCFunction)Noddy_name,METH_NOARGS,"Return the name, combining the first and last name"},{NULL}/* Sentinel */};
and assign them to thetp_methods slot:
Noddy_methods,/* tp_methods */
Note that we used theMETH_NOARGS flag to indicate that the method ispassed no arguments.
Finally, we’ll make our type usable as a base class. We’ve written our methodscarefully so far so that they don’t make any assumptions about the type of theobject being created or used, so all we need to do is to add thePy_TPFLAGS_BASETYPE to our class flag definition:
Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,/*tp_flags*/
We renamePyInit_noddy() toPyInit_noddy2() and update the modulename in thePyModuleDef struct.
Finally, we update oursetup.py file to build the new module:
fromdistutils.coreimportsetup,Extensionsetup(name="noddy",version="1.0",ext_modules=[Extension("noddy",["noddy.c"]),Extension("noddy2",["noddy2.c"]),])
In this section, we’ll provide finer control over how thefirst andlast attributes are set in theNoddy example. In the previousversion of our module, the instance variablesfirst andlastcould be set to non-string values or even deleted. We want to make sure thatthese attributes always contain strings.
#include <Python.h>#include "structmember.h"typedefstruct{PyObject_HEADPyObject*first;PyObject*last;intnumber;}Noddy;staticvoidNoddy_dealloc(Noddy*self){Py_XDECREF(self->first);Py_XDECREF(self->last);Py_TYPE(self)->tp_free((PyObject*)self);}staticPyObject*Noddy_new(PyTypeObject*type,PyObject*args,PyObject*kwds){Noddy*self;self=(Noddy*)type->tp_alloc(type,0);if(self!=NULL){self->first=PyUnicode_FromString("");if(self->first==NULL){Py_DECREF(self);returnNULL;}self->last=PyUnicode_FromString("");if(self->last==NULL){Py_DECREF(self);returnNULL;}self->number=0;}return(PyObject*)self;}staticintNoddy_init(Noddy*self,PyObject*args,PyObject*kwds){PyObject*first=NULL,*last=NULL,*tmp;staticchar*kwlist[]={"first","last","number",NULL};if(!PyArg_ParseTupleAndKeywords(args,kwds,"|SSi",kwlist,&first,&last,&self->number))return-1;if(first){tmp=self->first;Py_INCREF(first);self->first=first;Py_DECREF(tmp);}if(last){tmp=self->last;Py_INCREF(last);self->last=last;Py_DECREF(tmp);}return0;}staticPyMemberDefNoddy_members[]={{"number",T_INT,offsetof(Noddy,number),0,"noddy number"},{NULL}/* Sentinel */};staticPyObject*Noddy_getfirst(Noddy*self,void*closure){Py_INCREF(self->first);returnself->first;}staticintNoddy_setfirst(Noddy*self,PyObject*value,void*closure){if(value==NULL){PyErr_SetString(PyExc_TypeError,"Cannot delete the first attribute");return-1;}if(!PyUnicode_Check(value)){PyErr_SetString(PyExc_TypeError,"The first attribute value must be a string");return-1;}Py_DECREF(self->first);Py_INCREF(value);self->first=value;return0;}staticPyObject*Noddy_getlast(Noddy*self,void*closure){Py_INCREF(self->last);returnself->last;}staticintNoddy_setlast(Noddy*self,PyObject*value,void*closure){if(value==NULL){PyErr_SetString(PyExc_TypeError,"Cannot delete the last attribute");return-1;}if(!PyUnicode_Check(value)){PyErr_SetString(PyExc_TypeError,"The last attribute value must be a string");return-1;}Py_DECREF(self->last);Py_INCREF(value);self->last=value;return0;}staticPyGetSetDefNoddy_getseters[]={{"first",(getter)Noddy_getfirst,(setter)Noddy_setfirst,"first name",NULL},{"last",(getter)Noddy_getlast,(setter)Noddy_setlast,"last name",NULL},{NULL}/* Sentinel */};staticPyObject*Noddy_name(Noddy*self){returnPyUnicode_FromFormat("%S %S",self->first,self->last);}staticPyMethodDefNoddy_methods[]={{"name",(PyCFunction)Noddy_name,METH_NOARGS,"Return the name, combining the first and last name"},{NULL}/* Sentinel */};staticPyTypeObjectNoddyType={PyVarObject_HEAD_INIT(NULL,0)"noddy.Noddy",/* tp_name */sizeof(Noddy),/* tp_basicsize */0,/* tp_itemsize */(destructor)Noddy_dealloc,/* tp_dealloc */0,/* tp_print */0,/* tp_getattr */0,/* tp_setattr */0,/* tp_reserved */0,/* tp_repr */0,/* tp_as_number */0,/* tp_as_sequence */0,/* tp_as_mapping */0,/* tp_hash */0,/* tp_call */0,/* tp_str */0,/* tp_getattro */0,/* tp_setattro */0,/* tp_as_buffer */Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,/* tp_flags */"Noddy objects",/* tp_doc */0,/* tp_traverse */0,/* tp_clear */0,/* tp_richcompare */0,/* tp_weaklistoffset */0,/* tp_iter */0,/* tp_iternext */Noddy_methods,/* tp_methods */Noddy_members,/* tp_members */Noddy_getseters,/* tp_getset */0,/* tp_base */0,/* tp_dict */0,/* tp_descr_get */0,/* tp_descr_set */0,/* tp_dictoffset */(initproc)Noddy_init,/* tp_init */0,/* tp_alloc */Noddy_new,/* tp_new */};staticPyModuleDefnoddy3module={PyModuleDef_HEAD_INIT,"noddy3","Example module that creates an extension type.",-1,NULL,NULL,NULL,NULL,NULL};PyMODINIT_FUNCPyInit_noddy3(void){PyObject*m;if(PyType_Ready(&NoddyType)<0)returnNULL;m=PyModule_Create(&noddy3module);if(m==NULL)returnNULL;Py_INCREF(&NoddyType);PyModule_AddObject(m,"Noddy",(PyObject*)&NoddyType);returnm;}
To provide greater control, over thefirst andlast attributes,we’ll use custom getter and setter functions. Here are the functions forgetting and setting thefirst attribute:
Noddy_getfirst(Noddy*self,void*closure){Py_INCREF(self->first);returnself->first;}staticintNoddy_setfirst(Noddy*self,PyObject*value,void*closure){if(value==NULL){PyErr_SetString(PyExc_TypeError,"Cannot delete the first attribute");return-1;}if(!PyUnicode_Check(value)){PyErr_SetString(PyExc_TypeError,"The first attribute value must be a str");return-1;}Py_DECREF(self->first);Py_INCREF(value);self->first=value;return0;}
The getter function is passed aNoddy object and a “closure”, which isvoid pointer. In this case, the closure is ignored. (The closure supports anadvanced usage in which definition data is passed to the getter and setter. Thiscould, for example, be used to allow a single set of getter and setter functionsthat decide the attribute to get or set based on data in the closure.)
The setter function is passed theNoddy object, the new value, and theclosure. The new value may beNULL, in which case the attribute is beingdeleted. In our setter, we raise an error if the attribute is deleted or if theattribute value is not a string.
We create an array ofPyGetSetDef structures:
staticPyGetSetDefNoddy_getseters[]={{"first",(getter)Noddy_getfirst,(setter)Noddy_setfirst,"first name",NULL},{"last",(getter)Noddy_getlast,(setter)Noddy_setlast,"last name",NULL},{NULL}/* Sentinel */};
and register it in thetp_getset slot:
Noddy_getseters,/* tp_getset */
to register our attribute getters and setters.
The last item in aPyGetSetDef structure is the closure mentionedabove. In this case, we aren’t using the closure, so we just passNULL.
We also remove the member definitions for these attributes:
staticPyMemberDefNoddy_members[]={{"number",T_INT,offsetof(Noddy,number),0,"noddy number"},{NULL}/* Sentinel */};
We also need to update thetp_init handler to only allow strings[3] tobe passed:
staticintNoddy_init(Noddy*self,PyObject*args,PyObject*kwds){PyObject*first=NULL,*last=NULL,*tmp;staticchar*kwlist[]={"first","last","number",NULL};if(!PyArg_ParseTupleAndKeywords(args,kwds,"|SSi",kwlist,&first,&last,&self->number))return-1;if(first){tmp=self->first;Py_INCREF(first);self->first=first;Py_DECREF(tmp);}if(last){tmp=self->last;Py_INCREF(last);self->last=last;Py_DECREF(tmp);}return0;}
With these changes, we can assure that thefirst andlastmembers are neverNULL so we can remove checks forNULL values in almost allcases. This means that most of thePy_XDECREF() calls can be converted toPy_DECREF() calls. The only place we can’t change these calls is in thedeallocator, where there is the possibility that the initialization of thesemembers failed in the constructor.
We also rename the module initialization function and module name in theinitialization function, as we did before, and we add an extra definition to thesetup.py file.
Python has a cyclic-garbage collector that can identify unneeded objects evenwhen their reference counts are not zero. This can happen when objects areinvolved in cycles. For example, consider:
>>>l=[]>>>l.append(l)>>>dell
In this example, we create a list that contains itself. When we delete it, itstill has a reference from itself. Its reference count doesn’t drop to zero.Fortunately, Python’s cyclic-garbage collector will eventually figure out thatthe list is garbage and free it.
In the second version of theNoddy example, we allowed any kind ofobject to be stored in thefirst orlast attributes.[4] Thismeans thatNoddy objects can participate in cycles:
>>>importnoddy2>>>n=noddy2.Noddy()>>>l=[n]>>>n.first=l
This is pretty silly, but it gives us an excuse to add support for thecyclic-garbage collector to theNoddy example. To support cyclicgarbage collection, types need to fill two slots and set a class flag thatenables these slots:
#include <Python.h>#include "structmember.h"typedefstruct{PyObject_HEADPyObject*first;PyObject*last;intnumber;}Noddy;staticintNoddy_traverse(Noddy*self,visitprocvisit,void*arg){intvret;if(self->first){vret=visit(self->first,arg);if(vret!=0)returnvret;}if(self->last){vret=visit(self->last,arg);if(vret!=0)returnvret;}return0;}staticintNoddy_clear(Noddy*self){PyObject*tmp;tmp=self->first;self->first=NULL;Py_XDECREF(tmp);tmp=self->last;self->last=NULL;Py_XDECREF(tmp);return0;}staticvoidNoddy_dealloc(Noddy*self){Noddy_clear(self);Py_TYPE(self)->tp_free((PyObject*)self);}staticPyObject*Noddy_new(PyTypeObject*type,PyObject*args,PyObject*kwds){Noddy*self;self=(Noddy*)type->tp_alloc(type,0);if(self!=NULL){self->first=PyUnicode_FromString("");if(self->first==NULL){Py_DECREF(self);returnNULL;}self->last=PyUnicode_FromString("");if(self->last==NULL){Py_DECREF(self);returnNULL;}self->number=0;}return(PyObject*)self;}staticintNoddy_init(Noddy*self,PyObject*args,PyObject*kwds){PyObject*first=NULL,*last=NULL,*tmp;staticchar*kwlist[]={"first","last","number",NULL};if(!PyArg_ParseTupleAndKeywords(args,kwds,"|OOi",kwlist,&first,&last,&self->number))return-1;if(first){tmp=self->first;Py_INCREF(first);self->first=first;Py_XDECREF(tmp);}if(last){tmp=self->last;Py_INCREF(last);self->last=last;Py_XDECREF(tmp);}return0;}staticPyMemberDefNoddy_members[]={{"first",T_OBJECT_EX,offsetof(Noddy,first),0,"first name"},{"last",T_OBJECT_EX,offsetof(Noddy,last),0,"last name"},{"number",T_INT,offsetof(Noddy,number),0,"noddy number"},{NULL}/* Sentinel */};staticPyObject*Noddy_name(Noddy*self){if(self->first==NULL){PyErr_SetString(PyExc_AttributeError,"first");returnNULL;}if(self->last==NULL){PyErr_SetString(PyExc_AttributeError,"last");returnNULL;}returnPyUnicode_FromFormat("%S %S",self->first,self->last);}staticPyMethodDefNoddy_methods[]={{"name",(PyCFunction)Noddy_name,METH_NOARGS,"Return the name, combining the first and last name"},{NULL}/* Sentinel */};staticPyTypeObjectNoddyType={PyVarObject_HEAD_INIT(NULL,0)"noddy.Noddy",/* tp_name */sizeof(Noddy),/* tp_basicsize */0,/* tp_itemsize */(destructor)Noddy_dealloc,/* tp_dealloc */0,/* tp_print */0,/* tp_getattr */0,/* tp_setattr */0,/* tp_reserved */0,/* tp_repr */0,/* tp_as_number */0,/* tp_as_sequence */0,/* tp_as_mapping */0,/* tp_hash */0,/* tp_call */0,/* tp_str */0,/* tp_getattro */0,/* tp_setattro */0,/* tp_as_buffer */Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC,/* tp_flags */"Noddy objects",/* tp_doc */(traverseproc)Noddy_traverse,/* tp_traverse */(inquiry)Noddy_clear,/* tp_clear */0,/* tp_richcompare */0,/* tp_weaklistoffset */0,/* tp_iter */0,/* tp_iternext */Noddy_methods,/* tp_methods */Noddy_members,/* tp_members */0,/* tp_getset */0,/* tp_base */0,/* tp_dict */0,/* tp_descr_get */0,/* tp_descr_set */0,/* tp_dictoffset */(initproc)Noddy_init,/* tp_init */0,/* tp_alloc */Noddy_new,/* tp_new */};staticPyModuleDefnoddy4module={PyModuleDef_HEAD_INIT,"noddy4","Example module that creates an extension type.",-1,NULL,NULL,NULL,NULL,NULL};PyMODINIT_FUNCPyInit_noddy4(void){PyObject*m;if(PyType_Ready(&NoddyType)<0)returnNULL;m=PyModule_Create(&noddy4module);if(m==NULL)returnNULL;Py_INCREF(&NoddyType);PyModule_AddObject(m,"Noddy",(PyObject*)&NoddyType);returnm;}
The traversal method provides access to subobjects that could participate incycles:
staticintNoddy_traverse(Noddy*self,visitprocvisit,void*arg){intvret;if(self->first){vret=visit(self->first,arg);if(vret!=0)returnvret;}if(self->last){vret=visit(self->last,arg);if(vret!=0)returnvret;}return0;}
For each subobject that can participate in cycles, we need to call thevisit() function, which is passed to the traversal method. Thevisit() function takes as arguments the subobject and the extra argumentarg passed to the traversal method. It returns an integer value that must bereturned if it is non-zero.
Python provides aPy_VISIT() macro that automates calling visitfunctions. WithPy_VISIT(),Noddy_traverse() can be simplified:
staticintNoddy_traverse(Noddy*self,visitprocvisit,void*arg){Py_VISIT(self->first);Py_VISIT(self->last);return0;}
Note
Note that thetp_traverse implementation must name its arguments exactlyvisit andarg in order to usePy_VISIT(). This is to encourageuniformity across these boring implementations.
We also need to provide a method for clearing any subobjects that canparticipate in cycles. We implement the method and reimplement the deallocatorto use it:
staticintNoddy_clear(Noddy*self){PyObject*tmp;tmp=self->first;self->first=NULL;Py_XDECREF(tmp);tmp=self->last;self->last=NULL;Py_XDECREF(tmp);return0;}staticvoidNoddy_dealloc(Noddy*self){Noddy_clear(self);Py_TYPE(self)->tp_free((PyObject*)self);}
Notice the use of a temporary variable inNoddy_clear(). We use thetemporary variable so that we can set each member toNULL before decrementingits reference count. We do this because, as was discussed earlier, if thereference count drops to zero, we might cause code to run that calls back intothe object. In addition, because we now support garbage collection, we alsohave to worry about code being run that triggers garbage collection. If garbagecollection is run, ourtp_traverse handler could get called. We can’ttake a chance of havingNoddy_traverse() called when a member’s referencecount has dropped to zero and its value hasn’t been set toNULL.
Python provides aPy_CLEAR() that automates the careful decrementing ofreference counts. WithPy_CLEAR(), theNoddy_clear() function canbe simplified:
staticintNoddy_clear(Noddy*self){Py_CLEAR(self->first);Py_CLEAR(self->last);return0;}
Finally, we add thePy_TPFLAGS_HAVE_GC flag to the class flags:
Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC,/* tp_flags */
That’s pretty much it. If we had written customtp_alloc ortp_free slots, we’d need to modify them for cyclic-garbage collection.Most extensions will use the versions automatically provided.
It is possible to create new extension types that are derived from existingtypes. It is easiest to inherit from the built in types, since an extension caneasily use thePyTypeObject it needs. It can be difficult to sharethesePyTypeObject structures between extension modules.
In this example we will create aShoddy type that inherits from thebuilt-inlist type. The new type will be completely compatible withregular lists, but will have an additionalincrement() method thatincreases an internal counter.
>>>importshoddy>>>s=shoddy.Shoddy(range(3))>>>s.extend(s)>>>print(len(s))6>>>print(s.increment())1>>>print(s.increment())2
#include <Python.h>typedefstruct{PyListObjectlist;intstate;}Shoddy;staticPyObject*Shoddy_increment(Shoddy*self,PyObject*unused){self->state++;returnPyLong_FromLong(self->state);}staticPyMethodDefShoddy_methods[]={{"increment",(PyCFunction)Shoddy_increment,METH_NOARGS,PyDoc_STR("increment state counter")},{NULL,NULL},};staticintShoddy_init(Shoddy*self,PyObject*args,PyObject*kwds){if(PyList_Type.tp_init((PyObject*)self,args,kwds)<0)return-1;self->state=0;return0;}staticPyTypeObjectShoddyType={PyObject_HEAD_INIT(NULL)"shoddy.Shoddy",/* tp_name */sizeof(Shoddy),/* tp_basicsize */0,/* tp_itemsize */0,/* tp_dealloc */0,/* tp_print */0,/* tp_getattr */0,/* tp_setattr */0,/* tp_reserved */0,/* tp_repr */0,/* tp_as_number */0,/* tp_as_sequence */0,/* tp_as_mapping */0,/* tp_hash */0,/* tp_call */0,/* tp_str */0,/* tp_getattro */0,/* tp_setattro */0,/* tp_as_buffer */Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,/* tp_flags */0,/* tp_doc */0,/* tp_traverse */0,/* tp_clear */0,/* tp_richcompare */0,/* tp_weaklistoffset */0,/* tp_iter */0,/* tp_iternext */Shoddy_methods,/* tp_methods */0,/* tp_members */0,/* tp_getset */0,/* tp_base */0,/* tp_dict */0,/* tp_descr_get */0,/* tp_descr_set */0,/* tp_dictoffset */(initproc)Shoddy_init,/* tp_init */0,/* tp_alloc */0,/* tp_new */};staticPyModuleDefshoddymodule={PyModuleDef_HEAD_INIT,"shoddy","Shoddy module",-1,NULL,NULL,NULL,NULL,NULL};PyMODINIT_FUNCPyInit_shoddy(void){PyObject*m;ShoddyType.tp_base=&PyList_Type;if(PyType_Ready(&ShoddyType)<0)returnNULL;m=PyModule_Create(&shoddymodule);if(m==NULL)returnNULL;Py_INCREF(&ShoddyType);PyModule_AddObject(m,"Shoddy",(PyObject*)&ShoddyType);returnm;}
As you can see, the source code closely resembles theNoddy examples inprevious sections. We will break down the main differences between them.
typedefstruct{PyListObjectlist;intstate;}Shoddy;
The primary difference for derived type objects is that the base type’s objectstructure must be the first value. The base type will already include thePyObject_HEAD() at the beginning of its structure.
When a Python object is aShoddy instance, itsPyObject* pointer canbe safely cast to bothPyListObject* andShoddy*.
staticintShoddy_init(Shoddy*self,PyObject*args,PyObject*kwds){if(PyList_Type.tp_init((PyObject*)self,args,kwds)<0)return-1;self->state=0;return0;}
In the__init__ method for our type, we can see how to call through tothe__init__ method of the base type.
This pattern is important when writing a type with customnew anddealloc methods. Thenew method should not actually create thememory for the object withtp_alloc, that will be handled by the baseclass when calling itstp_new.
When filling out thePyTypeObject() for theShoddy type, you seea slot fortp_base(). Due to cross platform compiler issues, you can’tfill that field directly with thePyList_Type(); it can be done later inthe module’sinit() function.
PyMODINIT_FUNCPyInit_shoddy(void){PyObject*m;ShoddyType.tp_base=&PyList_Type;if(PyType_Ready(&ShoddyType)<0)returnNULL;m=PyModule_Create(&shoddymodule);if(m==NULL)returnNULL;Py_INCREF(&ShoddyType);PyModule_AddObject(m,"Shoddy",(PyObject*)&ShoddyType);returnm;}
Before callingPyType_Ready(), the type structure must have thetp_base slot filled in. When we are deriving a new type, it is notnecessary to fill out thetp_alloc slot withPyType_GenericNew()– the allocate function from the base type will be inherited.
After that, callingPyType_Ready() and adding the type object to themodule is the same as with the basicNoddy examples.
This section aims to give a quick fly-by on the various type methods you canimplement and what they do.
Here is the definition ofPyTypeObject, with some fields only used indebug builds omitted:
typedefstruct_typeobject{PyObject_VAR_HEADchar*tp_name;/* For printing, in format "<module>.<name>" */inttp_basicsize,tp_itemsize;/* For allocation *//* Methods to implement standard operations */destructortp_dealloc;printfunctp_print;getattrfunctp_getattr;setattrfunctp_setattr;void*tp_reserved;reprfunctp_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) */hashfunctp_hash;ternaryfunctp_call;reprfunctp_str;getattrofunctp_getattro;setattrofunctp_setattro;/* Functions to access object as input/output buffer */PyBufferProcs*tp_as_buffer;/* Flags to define presence of optional/expanded features */longtp_flags;char*tp_doc;/* Documentation string *//* call function for all accessible objects */traverseproctp_traverse;/* delete references to contained objects */inquirytp_clear;/* rich comparisons */richcmpfunctp_richcompare;/* weak reference enabler */longtp_weaklistoffset;/* Iterators */getiterfunctp_iter;iternextfunctp_iternext;/* Attribute descriptor and subclassing stuff */structPyMethodDef*tp_methods;structPyMemberDef*tp_members;structPyGetSetDef*tp_getset;struct_typeobject*tp_base;PyObject*tp_dict;descrgetfunctp_descr_get;descrsetfunctp_descr_set;longtp_dictoffset;initproctp_init;allocfunctp_alloc;newfunctp_new;freefunctp_free;/* Low-level free-memory routine */inquirytp_is_gc;/* For PyObject_IS_GC */PyObject*tp_bases;PyObject*tp_mro;/* method resolution order */PyObject*tp_cache;PyObject*tp_subclasses;PyObject*tp_weaklist;}PyTypeObject;
Now that’s alot of methods. Don’t worry too much though - if you have a typeyou want to define, the chances are very good that you will only implement ahandful of these.
As you probably expect by now, we’re going to go over this and give moreinformation about the various handlers. We won’t go in the order they aredefined in the structure, because there is a lot of historical baggage thatimpacts the ordering of the fields; be sure your type initialization keeps thefields in the right order! It’s often easiest to find an example that includesall the fields you need (even if they’re initialized to0) and then changethe values to suit your new type.
char*tp_name;/* For printing */
The name of the type - as mentioned in the last section, this will appear invarious places, almost entirely for diagnostic purposes. Try to choose somethingthat will be helpful in such a situation!
inttp_basicsize,tp_itemsize;/* For allocation */
These fields tell the runtime how much memory to allocate when new objects ofthis type are created. Python has some built-in support for variable lengthstructures (think: strings, lists) which is where thetp_itemsize fieldcomes in. This will be dealt with later.
char*tp_doc;
Here you can put a string (or its address) that you want returned when thePython script referencesobj.__doc__ to retrieve the doc string.
Now we come to the basic type methods—the ones most extension types willimplement.
destructortp_dealloc;
This function is called when the reference count of the instance of your type isreduced to zero and the Python interpreter wants to reclaim it. If your typehas memory to free or other clean-up to perform, put it here. The object itselfneeds to be freed here as well. Here is an example of this function:
staticvoidnewdatatype_dealloc(newdatatypeobject*obj){free(obj->obj_UnderlyingDatatypePtr);Py_TYPE(obj)->tp_free(obj);}
One important requirement of the deallocator function is that it leaves anypending exceptions alone. This is important since deallocators are frequentlycalled as the interpreter unwinds the Python stack; when the stack is unwounddue to an exception (rather than normal returns), nothing is done to protect thedeallocators from seeing that an exception has already been set. Any actionswhich a deallocator performs which may cause additional Python code to beexecuted may detect that an exception has been set. This can lead to misleadingerrors from the interpreter. The proper way to protect against this is to savea pending exception before performing the unsafe action, and restoring it whendone. This can be done using thePyErr_Fetch() andPyErr_Restore() functions:
staticvoidmy_dealloc(PyObject*obj){MyObject*self=(MyObject*)obj;PyObject*cbresult;if(self->my_callback!=NULL){PyObject*err_type,*err_value,*err_traceback;/* This saves the current exception state */PyErr_Fetch(&err_type,&err_value,&err_traceback);cbresult=PyObject_CallObject(self->my_callback,NULL);if(cbresult==NULL)PyErr_WriteUnraisable(self->my_callback);elsePy_DECREF(cbresult);/* This restores the saved exception state */PyErr_Restore(err_type,err_value,err_traceback);Py_DECREF(self->my_callback);}Py_TYPE(obj)->tp_free((PyObject*)self);}
In Python, there are two ways to generate a textual representation of an object:therepr() function, and thestr() function. (Theprint()function just callsstr().) These handlers are both optional.
reprfunctp_repr;reprfunctp_str;
Thetp_repr handler should return a string object containing arepresentation of the instance for which it is called. Here is a simpleexample:
staticPyObject*newdatatype_repr(newdatatypeobject*obj){returnPyUnicode_FromFormat("Repr-ified_newdatatype{{size:\%d}}",obj->obj_UnderlyingDatatypePtr->size);}
If notp_repr handler is specified, the interpreter will supply arepresentation that uses the type’stp_name and a uniquely-identifyingvalue for the object.
Thetp_str handler is tostr() what thetp_repr handlerdescribed above is torepr(); that is, it is called when Python code callsstr() on an instance of your object. Its implementation is very similarto thetp_repr function, but the resulting string is intended for humanconsumption. Iftp_str is not specified, thetp_repr handler isused instead.
Here is a simple example:
staticPyObject*newdatatype_str(newdatatypeobject*obj){returnPyUnicode_FromFormat("Stringified_newdatatype{{size:\%d}}",obj->obj_UnderlyingDatatypePtr->size);}
For every object which can support attributes, the corresponding type mustprovide the functions that control how the attributes are resolved. There needsto be a function which can retrieve attributes (if any are defined), and anotherto set attributes (if setting attributes is allowed). Removing an attribute isa special case, for which the new value passed to the handler isNULL.
Python supports two pairs of attribute handlers; a type that supports attributesonly needs to implement the functions for one pair. The difference is that onepair takes the name of the attribute as achar*, while the otheraccepts aPyObject*. Each type can use whichever pair makes moresense for the implementation’s convenience.
getattrfunctp_getattr;/* char * version */setattrfunctp_setattr;/* ... */getattrofunctp_getattro;/* PyObject * version */setattrofunctp_setattro;
If accessing attributes of an object is always a simple operation (this will beexplained shortly), there are generic implementations which can be used toprovide thePyObject* version of the attribute management functions.The actual need for type-specific attribute handlers almost completelydisappeared starting with Python 2.2, though there are many examples which havenot been updated to use some of the new generic mechanism that is available.
Most extension types only usesimple attributes. So, what makes theattributes simple? There are only a couple of conditions that must be met:
Note that this list does not place any restrictions on the values of theattributes, when the values are computed, or how relevant data is stored.
WhenPyType_Ready() is called, it uses three tables referenced by thetype object to createdescriptors which are placed in the dictionary of thetype object. Each descriptor controls access to one attribute of the instanceobject. Each of the tables is optional; if all three areNULL, instances ofthe type will only have attributes that are inherited from their base type, andshould leave thetp_getattro andtp_setattro fieldsNULL aswell, allowing the base type to handle attributes.
The tables are declared as three fields of the type object:
structPyMethodDef*tp_methods;structPyMemberDef*tp_members;structPyGetSetDef*tp_getset;
Iftp_methods is notNULL, it must refer to an array ofPyMethodDef structures. Each entry in the table is an instance of thisstructure:
typedefstructPyMethodDef{char*ml_name;/* method name */PyCFunctionml_meth;/* implementation function */intml_flags;/* flags */char*ml_doc;/* docstring */}PyMethodDef;
One entry should be defined for each method provided by the type; no entries areneeded for methods inherited from a base type. One additional entry is neededat the end; it is a sentinel that marks the end of the array. Theml_name field of the sentinel must beNULL.
The second table is used to define attributes which map directly to data storedin the instance. A variety of primitive C types are supported, and access maybe read-only or read-write. The structures in the table are defined as:
typedefstructPyMemberDef{char*name;inttype;intoffset;intflags;char*doc;}PyMemberDef;
For each entry in the table, adescriptor will be constructed and added to thetype which will be able to extract a value from the instance structure. Thetype field should contain one of the type codes defined in thestructmember.h header; the value will be used to determine how toconvert Python values to and from C values. Theflags field is used tostore flags which control how the attribute can be accessed.
The following flag constants are defined instructmember.h; they may becombined using bitwise-OR.
| Constant | Meaning |
|---|---|
| READONLY | Never writable. |
| READ_RESTRICTED | Not readable in restricted mode. |
| WRITE_RESTRICTED | Not writable in restricted mode. |
| RESTRICTED | Not readable or writable in restricted mode. |
An interesting advantage of using thetp_members table to builddescriptors that are used at runtime is that any attribute defined this way canhave an associated doc string simply by providing the text in the table. Anapplication can use the introspection API to retrieve the descriptor from theclass object, and get the doc string using its__doc__ attribute.
As with thetp_methods table, a sentinel entry with aname valueofNULL is required.
For simplicity, only thechar* version will be demonstrated here; thetype of the name parameter is the only difference between thechar*andPyObject* flavors of the interface. This example effectively doesthe same thing as the generic example above, but does not use the genericsupport added in Python 2.2. It explains how the handler functions arecalled, so that if you do need to extend their functionality, you’ll understandwhat needs to be done.
Thetp_getattr handler is called when the object requires an attributelook-up. It is called in the same situations where the__getattr__()method of a class would be called.
Here is an example:
staticPyObject*newdatatype_getattr(newdatatypeobject*obj,char*name){if(strcmp(name,"data")==0){returnPyInt_FromLong(obj->data);}PyErr_Format(PyExc_AttributeError,"'%.50s' object has no attribute '%.400s'",tp->tp_name,name);returnNULL;}
Thetp_setattr handler is called when the__setattr__() or__delattr__() method of a class instance would be called. When anattribute should be deleted, the third parameter will beNULL. Here is anexample that simply raises an exception; if this were really all you wanted, thetp_setattr handler should be set toNULL.
staticintnewdatatype_setattr(newdatatypeobject*obj,char*name,PyObject*v){(void)PyErr_Format(PyExc_RuntimeError,"Read-only attribute: \%s",name);return-1;}
richcmpfunctp_richcompare;
Thetp_richcompare handler is called when comparisons are needed. It isanalogous to therich comparison methods, like__lt__(), and also called byPyObject_RichCompare() andPyObject_RichCompareBool().
This function is called with two Python objects and the operator as arguments,where the operator is one ofPy_EQ,Py_NE,Py_LE,Py_GT,Py_LT orPy_GT. It should compare the two objects with respect to thespecified operator and returnPy_True orPy_False if the comparison issuccessful,Py_NotImplemented to indicate that comparison is notimplemented and the other object’s comparison method should be tried, orNULLif an exception was set.
Here is a sample implementation, for a datatype that is considered equal if thesize of an internal pointer is equal:
staticPyObject*newdatatype_richcmp(PyObject*obj1,PyObject*obj2,intop){PyObject*result;intc,size1,size2;/* code to make sure that both arguments are of type newdatatype omitted */size1=obj1->obj_UnderlyingDatatypePtr->size;size2=obj2->obj_UnderlyingDatatypePtr->size;switch(op){casePy_LT:c=size1<size2;break;casePy_LE:c=size1<=size2;break;casePy_EQ:c=size1==size2;break;casePy_NE:c=size1!=size2;break;casePy_GT:c=size1>size2;break;casePy_GE:c=size1>=size2;break;}result=c?Py_True:Py_False;Py_INCREF(result);returnresult;}
Python supports a variety ofabstract ‘protocols;’ the specific interfacesprovided to use these interfaces are documented inAbstract Objects Layer.
A number of these abstract interfaces were defined early in the development ofthe Python implementation. In particular, the number, mapping, and sequenceprotocols have been part of Python since the beginning. Other protocols havebeen added over time. For protocols which depend on several handler routinesfrom the type implementation, the older protocols have been defined as optionalblocks of handlers referenced by the type object. For newer protocols there areadditional slots in the main type object, with a flag bit being set to indicatethat the slots are present and should be checked by the interpreter. (The flagbit does not indicate that the slot values are non-NULL. The flag may be setto indicate the presence of a slot, but a slot may still be unfilled.)
PyNumberMethods*tp_as_number;PySequenceMethods*tp_as_sequence;PyMappingMethods*tp_as_mapping;
If you wish your object to be able to act like a number, a sequence, or amapping object, then you place the address of a structure that implements the CtypePyNumberMethods,PySequenceMethods, orPyMappingMethods, respectively. It is up to you to fill in thisstructure with appropriate values. You can find examples of the use of each ofthese in theObjects directory of the Python source distribution.
hashfunctp_hash;
This function, if you choose to provide it, should return a hash number for aninstance of your data type. Here is a moderately pointless example:
staticlongnewdatatype_hash(newdatatypeobject*obj){longresult;result=obj->obj_UnderlyingDatatypePtr->size;result=result*3;returnresult;}
ternaryfunctp_call;
This function is called when an instance of your data type is “called”, forexample, ifobj1 is an instance of your data type and the Python scriptcontainsobj1('hello'), thetp_call handler is invoked.
This function takes three arguments:
Here is a desultory example of the implementation of the call function.
/* Implement the call function. * obj1 is the instance receiving the call. * obj2 is a tuple containing the arguments to the call, in this * case 3 strings. */staticPyObject*newdatatype_call(newdatatypeobject*obj,PyObject*args,PyObject*other){PyObject*result;char*arg1;char*arg2;char*arg3;if(!PyArg_ParseTuple(args,"sss:call",&arg1,&arg2,&arg3)){returnNULL;}result=PyUnicode_FromFormat("Returning -- value: [\%d] arg1: [\%s] arg2: [\%s] arg3: [\%s]\n",obj->obj_UnderlyingDatatypePtr->size,arg1,arg2,arg3);returnresult;}
/* Iterators */getiterfunctp_iter;iternextfunctp_iternext;
These functions provide support for the iterator protocol. Any object whichwishes to support iteration over its contents (which may be generated duringiteration) must implement thetp_iter handler. Objects which are returnedby atp_iter handler must implement both thetp_iter andtp_iternexthandlers. Both handlers take exactly one parameter, the instance for which theyare being called, and return a new reference. In the case of an error, theyshould set an exception and returnNULL.
For an object which represents an iterable collection, thetp_iter handlermust return an iterator object. The iterator object is responsible formaintaining the state of the iteration. For collections which can supportmultiple iterators which do not interfere with each other (as lists and tuplesdo), a new iterator should be created and returned. Objects which can only beiterated over once (usually due to side effects of iteration) should implementthis handler by returning a new reference to themselves, and should alsoimplement thetp_iternext handler. File objects are an example of such aniterator.
Iterator objects should implement both handlers. Thetp_iter handler shouldreturn a new reference to the iterator (this is the same as thetp_iterhandler for objects which can only be iterated over destructively). Thetp_iternext handler should return a new reference to the next object in theiteration if there is one. If the iteration has reached the end, it may returnNULL without setting an exception or it may setStopIteration; avoidingthe exception can yield slightly better performance. If an actual error occurs,it should set an exception and returnNULL.
One of the goals of Python’s weak-reference implementation is to allow any typeto participate in the weak reference mechanism without incurring the overhead onthose objects which do not benefit by weak referencing (such as numbers).
For an object to be weakly referencable, the extension must include aPyObject* field in the instance structure for the use of the weakreference mechanism; it must be initialized toNULL by the object’sconstructor. It must also set thetp_weaklistoffset field of thecorresponding type object to the offset of the field. For example, the instancetype is defined with the following structure:
typedefstruct{PyObject_HEADPyClassObject*in_class;/* The class object */PyObject*in_dict;/* A dictionary */PyObject*in_weakreflist;/* List of weak references */}PyInstanceObject;
The statically-declared type object for instances is defined this way:
PyTypeObjectPyInstance_Type={PyVarObject_HEAD_INIT(&PyType_Type,0)0,"module.instance",/* Lots of stuff omitted for brevity... */Py_TPFLAGS_DEFAULT,/* tp_flags */0,/* tp_doc */0,/* tp_traverse */0,/* tp_clear */0,/* tp_richcompare */offsetof(PyInstanceObject,in_weakreflist),/* tp_weaklistoffset */};
The type constructor is responsible for initializing the weak reference list toNULL:
staticPyObject*instance_new(){/* Other initialization stuff omitted for brevity */self->in_weakreflist=NULL;return(PyObject*)self;}
The only further addition is that the destructor needs to call the weakreference manager to clear any weak references. This is only required if theweak reference list is non-NULL:
staticvoidinstance_dealloc(PyInstanceObject*inst){/* Allocate temporaries if needed, but do not begin destruction just yet. */if(inst->in_weakreflist!=NULL)PyObject_ClearWeakRefs((PyObject*)inst);/* Proceed with object destruction normally. */}
Remember that you can omit most of these functions, in which case you provide0 as a value. There are type definitions for each of the functions you mustprovide. They are inobject.h in the Python include directory thatcomes with the source distribution of Python.
In order to learn how to implement any specific method for your new data type,do the following: Download and unpack the Python source distribution. Go totheObjects directory, then search the C source files fortp_ plusthe function you want (for example,tp_richcompare). You will find examplesof the function you want to implement.
When you need to verify that an object is an instance of the type you areimplementing, use thePyObject_TypeCheck() function. A sample of its usemight be something like the following:
if(!PyObject_TypeCheck(some_object,&MyType)){PyErr_SetString(PyExc_TypeError,"arg #1 not a mything");returnNULL;}
Footnotes
| [1] | This is true when we know that the object is a basic type, like a string or afloat. |
| [2] | We relied on this in thetp_dealloc handler in this example, because ourtype doesn’t support garbage collection. Even if a type supports garbagecollection, there are calls that can be made to “untrack” the object fromgarbage collection, however, these calls are advanced and not covered here. |
| [3] | We now know that the first and last members are strings, so perhaps we could beless careful about decrementing their reference counts, however, we acceptinstances of string subclasses. Even though deallocating normal strings won’tcall back into our objects, we can’t guarantee that deallocating an instance ofa string subclass won’t call back into our objects. |
| [4] | Even in the third version, we aren’t guaranteed to avoid cycles. Instances ofstring subclasses are allowed and string subclasses could allow cycles even ifnormal strings don’t. |
1. Extending Python with C or C++
3. Building C and C++ Extensions with distutils
Enter search terms or a module, class or function name.