.. code-block:: c
- static void foo_dealloc(foo_object *self) {
+ static void
+ foo_dealloc(PyObject *op)
+ {
+ foo_object *self = (foo_object *) op;
PyObject_GC_UnTrack(self);
Py_CLEAR(self->ref);
- Py_TYPE(self)->tp_free((PyObject *)self);
+ Py_TYPE(self)->tp_free(self);
}
Finally, if the type is heap allocated (:c:macro:`Py_TPFLAGS_HEAPTYPE`), the
.. code-block:: c
- static void foo_dealloc(foo_object *self) {
- PyTypeObject *tp = Py_TYPE(self);
+ static void
+ foo_dealloc(PyObject *op)
+ {
+ PyTypeObject *tp = Py_TYPE(op);
// free references and buffers here
- tp->tp_free(self);
+ tp->tp_free(op);
Py_DECREF(tp);
}
:mod:`!_thread` extension module::
static int
- local_traverse(localobject *self, visitproc visit, void *arg)
+ local_traverse(PyObject *op, visitproc visit, void *arg)
{
+ localobject *self = (localobject *) op;
Py_VISIT(self->args);
Py_VISIT(self->kw);
Py_VISIT(self->dict);
members to ``NULL``, as in the following example::
static int
- local_clear(localobject *self)
+ local_clear(PyObject *op)
{
+ localobject *self = (localobject *) op;
Py_CLEAR(self->key);
Py_CLEAR(self->args);
Py_CLEAR(self->kw);
function::
static void
- newdatatype_dealloc(newdatatypeobject *obj)
+ newdatatype_dealloc(PyObject *op)
{
- free(obj->obj_UnderlyingDatatypePtr);
- Py_TYPE(obj)->tp_free((PyObject *)obj);
+ newdatatypeobject *self = (newdatatypeobject *) op;
+ free(self->obj_UnderlyingDatatypePtr);
+ Py_TYPE(self)->tp_free(self);
}
If your type supports garbage collection, the destructor should call
:c:func:`PyObject_GC_UnTrack` before clearing any member fields::
static void
- newdatatype_dealloc(newdatatypeobject *obj)
+ newdatatype_dealloc(PyObject *op)
{
- PyObject_GC_UnTrack(obj);
- Py_CLEAR(obj->other_obj);
+ newdatatypeobject *self = (newdatatypeobject *) op;
+ PyObject_GC_UnTrack(op);
+ Py_CLEAR(self->other_obj);
...
- Py_TYPE(obj)->tp_free((PyObject *)obj);
+ Py_TYPE(self)->tp_free(self);
}
.. index::
PyErr_Fetch(&err_type, &err_value, &err_traceback);
cbresult = PyObject_CallNoArgs(self->my_callback);
- if (cbresult == NULL)
- PyErr_WriteUnraisable(self->my_callback);
- else
+ if (cbresult == NULL) {
+ PyErr_WriteUnraisable(self->my_callback);
+ }
+ else {
Py_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);
+ Py_TYPE(self)->tp_free(self);
}
.. note::
example::
static PyObject *
- newdatatype_repr(newdatatypeobject *obj)
+ newdatatype_repr(PyObject *op)
{
+ newdatatypeobject *self = (newdatatypeobject *) op;
return PyUnicode_FromFormat("Repr-ified_newdatatype{{size:%d}}",
- obj->obj_UnderlyingDatatypePtr->size);
+ self->obj_UnderlyingDatatypePtr->size);
}
If no :c:member:`~PyTypeObject.tp_repr` handler is specified, the interpreter will supply a
Here is a simple example::
static PyObject *
- newdatatype_str(newdatatypeobject *obj)
+ newdatatype_str(PyObject *op)
{
+ newdatatypeobject *self = (newdatatypeobject *) op;
return PyUnicode_FromFormat("Stringified_newdatatype{{size:%d}}",
- obj->obj_UnderlyingDatatypePtr->size);
+ self->obj_UnderlyingDatatypePtr->size);
}
Here is an example::
static PyObject *
- newdatatype_getattr(newdatatypeobject *obj, char *name)
+ newdatatype_getattr(PyObject *op, char *name)
{
- if (strcmp(name, "data") == 0)
- {
- return PyLong_FromLong(obj->data);
+ newdatatypeobject *self = (newdatatypeobject *) op;
+ if (strcmp(name, "data") == 0) {
+ return PyLong_FromLong(self->data);
}
PyErr_Format(PyExc_AttributeError,
"'%.100s' object has no attribute '%.400s'",
- Py_TYPE(obj)->tp_name, name);
+ Py_TYPE(self)->tp_name, name);
return NULL;
}
:c:member:`~PyTypeObject.tp_setattr` handler should be set to ``NULL``. ::
static int
- newdatatype_setattr(newdatatypeobject *obj, char *name, PyObject *v)
+ newdatatype_setattr(PyObject *op, char *name, PyObject *v)
{
PyErr_Format(PyExc_RuntimeError, "Read-only attribute: %s", name);
return -1;
size of an internal pointer is equal::
static PyObject *
- newdatatype_richcmp(newdatatypeobject *obj1, newdatatypeobject *obj2, int op)
+ newdatatype_richcmp(PyObject *lhs, PyObject *rhs, int op)
{
+ newdatatypeobject *obj1 = (newdatatypeobject *) lhs;
+ newdatatypeobject *obj2 = (newdatatypeobject *) rhs;
PyObject *result;
int c, size1, size2;
case Py_GE: c = size1 >= size2; break;
}
result = c ? Py_True : Py_False;
- Py_INCREF(result);
- return result;
+ return Py_NewRef(result);
}
instance of your data type. Here is a simple example::
static Py_hash_t
- newdatatype_hash(newdatatypeobject *obj)
+ newdatatype_hash(PyObject *op)
{
+ newdatatypeobject *self = (newdatatypeobject *) op;
Py_hash_t result;
- result = obj->some_size + 32767 * obj->some_number;
- if (result == -1)
- result = -2;
+ result = self->some_size + 32767 * self->some_number;
+ if (result == -1) {
+ result = -2;
+ }
return result;
}
Here is a toy ``tp_call`` implementation::
static PyObject *
- newdatatype_call(newdatatypeobject *obj, PyObject *args, PyObject *kwds)
+ newdatatype_call(PyObject *op, PyObject *args, PyObject *kwds)
{
+ newdatatypeobject *self = (newdatatypeobject *) op;
PyObject *result;
const char *arg1;
const char *arg2;
}
result = PyUnicode_FromFormat(
"Returning -- value: [%d] arg1: [%s] arg2: [%s] arg3: [%s]\n",
- obj->obj_UnderlyingDatatypePtr->size,
+ self->obj_UnderlyingDatatypePtr->size,
arg1, arg2, arg3);
return result;
}
references (by calling :c:func:`PyObject_ClearWeakRefs`)::
static void
- Trivial_dealloc(TrivialObject *self)
+ Trivial_dealloc(PyObject *op)
{
/* Clear weakrefs first before calling any destructors */
- PyObject_ClearWeakRefs((PyObject *) self);
+ PyObject_ClearWeakRefs(op);
/* ... remainder of destruction code omitted for brevity ... */
- Py_TYPE(self)->tp_free((PyObject *) self);
+ Py_TYPE(op)->tp_free(op);
}