@@ -70,22 +70,24 @@ object itself needs to be freed here as well. Here is an example of this
70
70
function::
71
71
72
72
static void
73
- newdatatype_dealloc(newdatatypeobject *obj )
73
+ newdatatype_dealloc(PyObject *op )
74
74
{
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);
77
78
}
78
79
79
80
If your type supports garbage collection, the destructor should call
80
81
:c:func: `PyObject_GC_UnTrack ` before clearing any member fields::
81
82
82
83
static void
83
- newdatatype_dealloc(newdatatypeobject *obj )
84
+ newdatatype_dealloc(PyObject *op )
84
85
{
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);
87
89
...
88
- Py_TYPE(obj )->tp_free((PyObject *)obj );
90
+ Py_TYPE(self )->tp_free(self );
89
91
}
90
92
91
93
.. index ::
@@ -117,17 +119,19 @@ done. This can be done using the :c:func:`PyErr_Fetch` and
117
119
PyErr_Fetch(&err_type, &err_value, &err_traceback);
118
120
119
121
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 {
123
126
Py_DECREF(cbresult);
127
+ }
124
128
125
129
/* This restores the saved exception state */
126
130
PyErr_Restore(err_type, err_value, err_traceback);
127
131
128
132
Py_DECREF(self->my_callback);
129
133
}
130
- Py_TYPE(obj )->tp_free((PyObject*) self);
134
+ Py_TYPE(self )->tp_free(self);
131
135
}
132
136
133
137
.. note ::
@@ -168,10 +172,11 @@ representation of the instance for which it is called. Here is a simple
168
172
example::
169
173
170
174
static PyObject *
171
- newdatatype_repr(newdatatypeobject *obj )
175
+ newdatatype_repr(PyObject *op )
172
176
{
177
+ newdatatypeobject *self = (newdatatypeobject *) op;
173
178
return PyUnicode_FromFormat("Repr-ified_newdatatype{{size:%d}}",
174
- obj ->obj_UnderlyingDatatypePtr->size);
179
+ self ->obj_UnderlyingDatatypePtr->size);
175
180
}
176
181
177
182
If no :c:member: `~PyTypeObject.tp_repr ` handler is specified, the interpreter will supply a
@@ -188,10 +193,11 @@ used instead.
188
193
Here is a simple example::
189
194
190
195
static PyObject *
191
- newdatatype_str(newdatatypeobject *obj )
196
+ newdatatype_str(PyObject *op )
192
197
{
198
+ newdatatypeobject *self = (newdatatypeobject *) op;
193
199
return PyUnicode_FromFormat("Stringified_newdatatype{{size:%d}}",
194
- obj ->obj_UnderlyingDatatypePtr->size);
200
+ self ->obj_UnderlyingDatatypePtr->size);
195
201
}
196
202
197
203
@@ -329,16 +335,16 @@ method of a class would be called.
329
335
Here is an example::
330
336
331
337
static PyObject *
332
- newdatatype_getattr(newdatatypeobject *obj , char *name)
338
+ newdatatype_getattr(PyObject *op , char *name)
333
339
{
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);
337
343
}
338
344
339
345
PyErr_Format(PyExc_AttributeError,
340
346
"'%.100s' object has no attribute '%.400s'",
341
- Py_TYPE(obj )->tp_name, name);
347
+ Py_TYPE(self )->tp_name, name);
342
348
return NULL;
343
349
}
344
350
@@ -349,7 +355,7 @@ example that simply raises an exception; if this were really all you wanted, the
349
355
:c:member: `~PyTypeObject.tp_setattr ` handler should be set to ``NULL ``. ::
350
356
351
357
static int
352
- newdatatype_setattr(newdatatypeobject *obj , char *name, PyObject *v)
358
+ newdatatype_setattr(PyObject *op , char *name, PyObject *v)
353
359
{
354
360
PyErr_Format(PyExc_RuntimeError, "Read-only attribute: %s", name);
355
361
return -1;
@@ -379,8 +385,10 @@ Here is a sample implementation, for a datatype that is considered equal if the
379
385
size of an internal pointer is equal::
380
386
381
387
static PyObject *
382
- newdatatype_richcmp(newdatatypeobject *obj1, newdatatypeobject *obj2 , int op)
388
+ newdatatype_richcmp(PyObject *lhs, PyObject *rhs , int op)
383
389
{
390
+ newdatatypeobject *obj1 = (newdatatypeobject *) lhs;
391
+ newdatatypeobject *obj2 = (newdatatypeobject *) rhs;
384
392
PyObject *result;
385
393
int c, size1, size2;
386
394
@@ -399,8 +407,7 @@ size of an internal pointer is equal::
399
407
case Py_GE: c = size1 >= size2; break;
400
408
}
401
409
result = c ? Py_True : Py_False;
402
- Py_INCREF(result);
403
- return result;
410
+ return Py_NewRef(result);
404
411
}
405
412
406
413
@@ -439,12 +446,14 @@ This function, if you choose to provide it, should return a hash number for an
439
446
instance of your data type. Here is a simple example::
440
447
441
448
static Py_hash_t
442
- newdatatype_hash(newdatatypeobject *obj )
449
+ newdatatype_hash(PyObject *op )
443
450
{
451
+ newdatatypeobject *self = (newdatatypeobject *) op;
444
452
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
+ }
448
457
return result;
449
458
}
450
459
@@ -478,8 +487,9 @@ This function takes three arguments:
478
487
Here is a toy ``tp_call `` implementation::
479
488
480
489
static PyObject *
481
- newdatatype_call(newdatatypeobject *obj , PyObject *args, PyObject *kwds)
490
+ newdatatype_call(PyObject *op , PyObject *args, PyObject *kwds)
482
491
{
492
+ newdatatypeobject *self = (newdatatypeobject *) op;
483
493
PyObject *result;
484
494
const char *arg1;
485
495
const char *arg2;
@@ -490,7 +500,7 @@ Here is a toy ``tp_call`` implementation::
490
500
}
491
501
result = PyUnicode_FromFormat(
492
502
"Returning -- value: [%d] arg1: [%s] arg2: [%s] arg3: [%s]\n",
493
- obj ->obj_UnderlyingDatatypePtr->size,
503
+ self ->obj_UnderlyingDatatypePtr->size,
494
504
arg1, arg2, arg3);
495
505
return result;
496
506
}
@@ -563,12 +573,12 @@ The only further addition is that ``tp_dealloc`` needs to clear any weak
563
573
references (by calling :c:func: `PyObject_ClearWeakRefs `)::
564
574
565
575
static void
566
- Trivial_dealloc(TrivialObject *self )
576
+ Trivial_dealloc(PyObject *op )
567
577
{
568
578
/* Clear weakrefs first before calling any destructors */
569
- PyObject_ClearWeakRefs((PyObject *) self );
579
+ PyObject_ClearWeakRefs(op );
570
580
/* ... remainder of destruction code omitted for brevity ... */
571
- Py_TYPE(self )->tp_free((PyObject *) self );
581
+ Py_TYPE(op )->tp_free(op );
572
582
}
573
583
574
584
0 commit comments