| 1 |
#define PY_SSIZE_T_CLEAN |
| 2 |
#include <Python.h> |
| 3 |
#include <stddef.h> /* for offsetof() */ |
| 4 |
|
| 5 |
typedef struct { |
| 6 |
PyObject_HEAD |
| 7 |
PyObject *first; /* first name */ |
| 8 |
PyObject *last; /* last name */ |
| 9 |
int number; |
| 10 |
} CustomObject; |
| 11 |
|
| 12 |
static int |
| 13 |
Custom_traverse(PyObject *op, visitproc visit, void *arg) |
| 14 |
{ |
| 15 |
CustomObject *self = (CustomObject *) op; |
| 16 |
Py_VISIT(self->first); |
| 17 |
Py_VISIT(self->last); |
| 18 |
return 0; |
| 19 |
} |
| 20 |
|
| 21 |
static int |
| 22 |
Custom_clear(PyObject *op) |
| 23 |
{ |
| 24 |
CustomObject *self = (CustomObject *) op; |
| 25 |
Py_CLEAR(self->first); |
| 26 |
Py_CLEAR(self->last); |
| 27 |
return 0; |
| 28 |
} |
| 29 |
|
| 30 |
static void |
| 31 |
Custom_dealloc(PyObject *op) |
| 32 |
{ |
| 33 |
PyObject_GC_UnTrack(op); |
| 34 |
(void)Custom_clear(op); |
| 35 |
Py_TYPE(op)->tp_free(op); |
| 36 |
} |
| 37 |
|
| 38 |
static PyObject * |
| 39 |
Custom_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 40 |
{ |
| 41 |
CustomObject *self; |
| 42 |
self = (CustomObject *) type->tp_alloc(type, 0); |
| 43 |
if (self != NULL) { |
| 44 |
self->first = Py_GetConstant(Py_CONSTANT_EMPTY_STR); |
| 45 |
if (self->first == NULL) { |
| 46 |
Py_DECREF(self); |
| 47 |
return NULL; |
| 48 |
} |
| 49 |
self->last = Py_GetConstant(Py_CONSTANT_EMPTY_STR); |
| 50 |
if (self->last == NULL) { |
| 51 |
Py_DECREF(self); |
| 52 |
return NULL; |
| 53 |
} |
| 54 |
self->number = 0; |
| 55 |
} |
| 56 |
return (PyObject *) self; |
| 57 |
} |
| 58 |
|
| 59 |
static int |
| 60 |
Custom_init(PyObject *op, PyObject *args, PyObject *kwds) |
| 61 |
{ |
| 62 |
CustomObject *self = (CustomObject *) op; |
| 63 |
static char *kwlist[] = {"first", "last", "number", NULL}; |
| 64 |
PyObject *first = NULL, *last = NULL; |
| 65 |
|
| 66 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|UUi", kwlist, |
| 67 |
&first, &last, |
| 68 |
&self->number)) |
| 69 |
return -1; |
| 70 |
|
| 71 |
if (first) { |
| 72 |
Py_SETREF(self->first, Py_NewRef(first)); |
| 73 |
} |
| 74 |
if (last) { |
| 75 |
Py_SETREF(self->last, Py_NewRef(last)); |
| 76 |
} |
| 77 |
return 0; |
| 78 |
} |
| 79 |
|
| 80 |
static PyMemberDef Custom_members[] = { |
| 81 |
{"number", Py_T_INT, offsetof(CustomObject, number), 0, |
| 82 |
"custom number"}, |
| 83 |
{NULL} /* Sentinel */ |
| 84 |
}; |
| 85 |
|
| 86 |
static PyObject * |
| 87 |
Custom_getfirst(PyObject *op, void *closure) |
| 88 |
{ |
| 89 |
CustomObject *self = (CustomObject *) op; |
| 90 |
return Py_NewRef(self->first); |
| 91 |
} |
| 92 |
|
| 93 |
static int |
| 94 |
Custom_setfirst(PyObject *op, PyObject *value, void *closure) |
| 95 |
{ |
| 96 |
CustomObject *self = (CustomObject *) op; |
| 97 |
if (value == NULL) { |
| 98 |
PyErr_SetString(PyExc_TypeError, "Cannot delete the first attribute"); |
| 99 |
return -1; |
| 100 |
} |
| 101 |
if (!PyUnicode_Check(value)) { |
| 102 |
PyErr_SetString(PyExc_TypeError, |
| 103 |
"The first attribute value must be a string"); |
| 104 |
return -1; |
| 105 |
} |
| 106 |
Py_XSETREF(self->first, Py_NewRef(value)); |
| 107 |
return 0; |
| 108 |
} |
| 109 |
|
| 110 |
static PyObject * |
| 111 |
Custom_getlast(PyObject *op, void *closure) |
| 112 |
{ |
| 113 |
CustomObject *self = (CustomObject *) op; |
| 114 |
return Py_NewRef(self->last); |
| 115 |
} |
| 116 |
|
| 117 |
static int |
| 118 |
Custom_setlast(PyObject *op, PyObject *value, void *closure) |
| 119 |
{ |
| 120 |
CustomObject *self = (CustomObject *) op; |
| 121 |
if (value == NULL) { |
| 122 |
PyErr_SetString(PyExc_TypeError, "Cannot delete the last attribute"); |
| 123 |
return -1; |
| 124 |
} |
| 125 |
if (!PyUnicode_Check(value)) { |
| 126 |
PyErr_SetString(PyExc_TypeError, |
| 127 |
"The last attribute value must be a string"); |
| 128 |
return -1; |
| 129 |
} |
| 130 |
Py_XSETREF(self->last, Py_NewRef(value)); |
| 131 |
return 0; |
| 132 |
} |
| 133 |
|
| 134 |
static PyGetSetDef Custom_getsetters[] = { |
| 135 |
{"first", Custom_getfirst, Custom_setfirst, |
| 136 |
"first name", NULL}, |
| 137 |
{"last", Custom_getlast, Custom_setlast, |
| 138 |
"last name", NULL}, |
| 139 |
{NULL} /* Sentinel */ |
| 140 |
}; |
| 141 |
|
| 142 |
static PyObject * |
| 143 |
Custom_name(PyObject *op, PyObject *Py_UNUSED(dummy)) |
| 144 |
{ |
| 145 |
CustomObject *self = (CustomObject *) op; |
| 146 |
return PyUnicode_FromFormat("%S %S", self->first, self->last); |
| 147 |
} |
| 148 |
|
| 149 |
static PyMethodDef Custom_methods[] = { |
| 150 |
{"name", Custom_name, METH_NOARGS, |
| 151 |
"Return the name, combining the first and last name" |
| 152 |
}, |
| 153 |
{NULL} /* Sentinel */ |
| 154 |
}; |
| 155 |
|
| 156 |
static PyTypeObject CustomType = { |
| 157 |
.ob_base = PyVarObject_HEAD_INIT(NULL, 0) |
| 158 |
.tp_name = "custom4.Custom", |
| 159 |
.tp_doc = PyDoc_STR("Custom objects"), |
| 160 |
.tp_basicsize = sizeof(CustomObject), |
| 161 |
.tp_itemsize = 0, |
| 162 |
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 163 |
.tp_new = Custom_new, |
| 164 |
.tp_init = Custom_init, |
| 165 |
.tp_dealloc = Custom_dealloc, |
| 166 |
.tp_traverse = Custom_traverse, |
| 167 |
.tp_clear = Custom_clear, |
| 168 |
.tp_members = Custom_members, |
| 169 |
.tp_methods = Custom_methods, |
| 170 |
.tp_getset = Custom_getsetters, |
| 171 |
}; |
| 172 |
|
| 173 |
static int |
| 174 |
custom_module_exec(PyObject *m) |
| 175 |
{ |
| 176 |
if (PyType_Ready(&CustomType) < 0) { |
| 177 |
return -1; |
| 178 |
} |
| 179 |
|
| 180 |
if (PyModule_AddObjectRef(m, "Custom", (PyObject *) &CustomType) < 0) { |
| 181 |
return -1; |
| 182 |
} |
| 183 |
|
| 184 |
return 0; |
| 185 |
} |
| 186 |
|
| 187 |
static PyModuleDef_Slot custom_module_slots[] = { |
| 188 |
{Py_mod_exec, custom_module_exec}, |
| 189 |
{Py_mod_multiple_interpreters, Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED}, |
| 190 |
{0, NULL} |
| 191 |
}; |
| 192 |
|
| 193 |
static PyModuleDef custom_module = { |
| 194 |
.m_base = PyModuleDef_HEAD_INIT, |
| 195 |
.m_name = "custom4", |
| 196 |
.m_doc = "Example module that creates an extension type.", |
| 197 |
.m_size = 0, |
| 198 |
.m_slots = custom_module_slots, |
| 199 |
}; |
| 200 |
|
| 201 |
PyMODINIT_FUNC |
| 202 |
PyInit_custom4(void) |
| 203 |
{ |
| 204 |
return PyModuleDef_Init(&custom_module); |
| 205 |
} |