@@ -70,22 +70,24 @@ object itself needs to be freed here as well.  Here is an example of this
7070function::
7171
7272   static void 
73-    newdatatype_dealloc(newdatatypeobject *obj ) 
73+    newdatatype_dealloc(PyObject *op ) 
7474   { 
75-        free(obj->obj_UnderlyingDatatypePtr); 
76-        Py_TYPE(obj)->tp_free((PyObject *)obj); 
75+        newdatatypeobject *self = (newdatatypeobject *) op; 
76+        free(self->obj_UnderlyingDatatypePtr); 
77+        Py_TYPE(self)->tp_free(self); 
7778   } 
7879
7980If your type supports garbage collection, the destructor should call
8081:c:func: `PyObject_GC_UnTrack ` before clearing any member fields::
8182
8283   static void 
83-    newdatatype_dealloc(newdatatypeobject *obj ) 
84+    newdatatype_dealloc(PyObject *op ) 
8485   { 
85-        PyObject_GC_UnTrack(obj); 
86-        Py_CLEAR(obj->other_obj); 
86+        newdatatypeobject *self = (newdatatypeobject *) op; 
87+        PyObject_GC_UnTrack(op); 
88+        Py_CLEAR(self->other_obj); 
8789       ... 
88-        Py_TYPE(obj )->tp_free((PyObject *)obj ); 
90+        Py_TYPE(self )->tp_free(self ); 
8991   } 
9092
9193.. index ::
@@ -117,17 +119,19 @@ done.  This can be done using the :c:func:`PyErr_Fetch` and
117119           PyErr_Fetch(&err_type, &err_value, &err_traceback); 
118120
119121           cbresult = PyObject_CallNoArgs(self->my_callback); 
120-            if (cbresult == NULL) 
121-                PyErr_WriteUnraisable(self->my_callback); 
122-            else 
122+            if (cbresult == NULL) { 
123+               PyErr_WriteUnraisable(self->my_callback); 
124+            } 
125+            else { 
123126               Py_DECREF(cbresult); 
127+            } 
124128
125129           /* This restores the saved exception state */ 
126130           PyErr_Restore(err_type, err_value, err_traceback); 
127131
128132           Py_DECREF(self->my_callback); 
129133       } 
130-        Py_TYPE(obj )->tp_free((PyObject*) self); 
134+        Py_TYPE(self )->tp_free(self); 
131135   } 
132136
133137.. note ::
@@ -168,10 +172,11 @@ representation of the instance for which it is called.  Here is a simple
168172example::
169173
170174   static PyObject * 
171-    newdatatype_repr(newdatatypeobject *obj ) 
175+    newdatatype_repr(PyObject *op ) 
172176   { 
177+        newdatatypeobject *self = (newdatatypeobject *) op; 
173178       return PyUnicode_FromFormat("Repr-ified_newdatatype{{size:%d}}", 
174-                                    obj ->obj_UnderlyingDatatypePtr->size); 
179+                                    self ->obj_UnderlyingDatatypePtr->size); 
175180   } 
176181
177182If no :c:member: `~PyTypeObject.tp_repr ` handler is specified, the interpreter will supply a
@@ -188,10 +193,11 @@ used instead.
188193Here is a simple example::
189194
190195   static PyObject * 
191-    newdatatype_str(newdatatypeobject *obj ) 
196+    newdatatype_str(PyObject *op ) 
192197   { 
198+        newdatatypeobject *self = (newdatatypeobject *) op; 
193199       return PyUnicode_FromFormat("Stringified_newdatatype{{size:%d}}", 
194-                                    obj ->obj_UnderlyingDatatypePtr->size); 
200+                                    self ->obj_UnderlyingDatatypePtr->size); 
195201   } 
196202
197203
@@ -329,16 +335,16 @@ method of a class would be called.
329335Here is an example::
330336
331337   static PyObject * 
332-    newdatatype_getattr(newdatatypeobject *obj , char *name) 
338+    newdatatype_getattr(PyObject *op , char *name) 
333339   { 
334-        if (strcmp(name, "data") == 0)  
335-        { 
336-            return PyLong_FromLong(obj ->data); 
340+        newdatatypeobject *self = (newdatatypeobject *) op;  
341+        if (strcmp(name, "data") == 0)  { 
342+            return PyLong_FromLong(self ->data); 
337343       } 
338344
339345       PyErr_Format(PyExc_AttributeError, 
340346                    "'%.100s' object has no attribute '%.400s'", 
341-                     Py_TYPE(obj )->tp_name, name); 
347+                     Py_TYPE(self )->tp_name, name); 
342348       return NULL; 
343349   } 
344350
@@ -349,7 +355,7 @@ example that simply raises an exception; if this were really all you wanted, the
349355:c:member: `~PyTypeObject.tp_setattr ` handler should be set to ``NULL ``. ::
350356
351357   static int 
352-    newdatatype_setattr(newdatatypeobject *obj , char *name, PyObject *v) 
358+    newdatatype_setattr(PyObject *op , char *name, PyObject *v) 
353359   { 
354360       PyErr_Format(PyExc_RuntimeError, "Read-only attribute: %s", name); 
355361       return -1; 
@@ -379,8 +385,10 @@ Here is a sample implementation, for a datatype that is considered equal if the
379385size of an internal pointer is equal::
380386
381387   static PyObject * 
382-    newdatatype_richcmp(newdatatypeobject *obj1, newdatatypeobject *obj2 , int op) 
388+    newdatatype_richcmp(PyObject *lhs, PyObject *rhs , int op) 
383389   { 
390+        newdatatypeobject *obj1 = (newdatatypeobject *) lhs; 
391+        newdatatypeobject *obj2 = (newdatatypeobject *) rhs; 
384392       PyObject *result; 
385393       int c, size1, size2; 
386394
@@ -399,8 +407,7 @@ size of an internal pointer is equal::
399407       case Py_GE: c = size1 >= size2; break; 
400408       } 
401409       result = c ? Py_True : Py_False; 
402-        Py_INCREF(result); 
403-        return result; 
410+        return Py_NewRef(result); 
404411    } 
405412
406413
@@ -439,12 +446,14 @@ This function, if you choose to provide it, should return a hash number for an
439446instance of your data type. Here is a simple example::
440447
441448   static Py_hash_t 
442-    newdatatype_hash(newdatatypeobject *obj ) 
449+    newdatatype_hash(PyObject *op ) 
443450   { 
451+        newdatatypeobject *self = (newdatatypeobject *) op; 
444452       Py_hash_t result; 
445-        result = obj->some_size + 32767 * obj->some_number; 
446-        if (result == -1) 
447-           result = -2; 
453+        result = self->some_size + 32767 * self->some_number; 
454+        if (result == -1) { 
455+            result = -2; 
456+        } 
448457       return result; 
449458   } 
450459
@@ -478,8 +487,9 @@ This function takes three arguments:
478487Here is a toy ``tp_call `` implementation::
479488
480489   static PyObject * 
481-    newdatatype_call(newdatatypeobject *obj , PyObject *args, PyObject *kwds) 
490+    newdatatype_call(PyObject *op , PyObject *args, PyObject *kwds) 
482491   { 
492+        newdatatypeobject *self = (newdatatypeobject *) op; 
483493       PyObject *result; 
484494       const char *arg1; 
485495       const char *arg2; 
@@ -490,7 +500,7 @@ Here is a toy ``tp_call`` implementation::
490500       } 
491501       result = PyUnicode_FromFormat( 
492502           "Returning -- value: [%d] arg1: [%s] arg2: [%s] arg3: [%s]\n", 
493-            obj ->obj_UnderlyingDatatypePtr->size, 
503+            self ->obj_UnderlyingDatatypePtr->size, 
494504           arg1, arg2, arg3); 
495505       return result; 
496506   } 
@@ -563,12 +573,12 @@ The only further addition is that ``tp_dealloc`` needs to clear any weak
563573references (by calling :c:func: `PyObject_ClearWeakRefs `)::
564574
565575   static void 
566-    Trivial_dealloc(TrivialObject *self ) 
576+    Trivial_dealloc(PyObject *op ) 
567577   { 
568578       /* Clear weakrefs first before calling any destructors */ 
569-        PyObject_ClearWeakRefs((PyObject *) self ); 
579+        PyObject_ClearWeakRefs(op ); 
570580       /* ... remainder of destruction code omitted for brevity ... */ 
571-        Py_TYPE(self )->tp_free((PyObject *) self ); 
581+        Py_TYPE(op )->tp_free(op ); 
572582   } 
573583
574584
0 commit comments