| 1 | n/a | #include <Python.h> |
|---|
| 2 | n/a | #include "structmember.h" |
|---|
| 3 | n/a | |
|---|
| 4 | n/a | typedef struct { |
|---|
| 5 | n/a | PyObject_HEAD |
|---|
| 6 | n/a | PyObject *first; |
|---|
| 7 | n/a | PyObject *last; |
|---|
| 8 | n/a | int number; |
|---|
| 9 | n/a | } Noddy; |
|---|
| 10 | n/a | |
|---|
| 11 | n/a | static int |
|---|
| 12 | n/a | Noddy_traverse(Noddy *self, visitproc visit, void *arg) |
|---|
| 13 | n/a | { |
|---|
| 14 | n/a | int vret; |
|---|
| 15 | n/a | |
|---|
| 16 | n/a | if (self->first) { |
|---|
| 17 | n/a | vret = visit(self->first, arg); |
|---|
| 18 | n/a | if (vret != 0) |
|---|
| 19 | n/a | return vret; |
|---|
| 20 | n/a | } |
|---|
| 21 | n/a | if (self->last) { |
|---|
| 22 | n/a | vret = visit(self->last, arg); |
|---|
| 23 | n/a | if (vret != 0) |
|---|
| 24 | n/a | return vret; |
|---|
| 25 | n/a | } |
|---|
| 26 | n/a | |
|---|
| 27 | n/a | return 0; |
|---|
| 28 | n/a | } |
|---|
| 29 | n/a | |
|---|
| 30 | n/a | static int |
|---|
| 31 | n/a | Noddy_clear(Noddy *self) |
|---|
| 32 | n/a | { |
|---|
| 33 | n/a | PyObject *tmp; |
|---|
| 34 | n/a | |
|---|
| 35 | n/a | tmp = self->first; |
|---|
| 36 | n/a | self->first = NULL; |
|---|
| 37 | n/a | Py_XDECREF(tmp); |
|---|
| 38 | n/a | |
|---|
| 39 | n/a | tmp = self->last; |
|---|
| 40 | n/a | self->last = NULL; |
|---|
| 41 | n/a | Py_XDECREF(tmp); |
|---|
| 42 | n/a | |
|---|
| 43 | n/a | return 0; |
|---|
| 44 | n/a | } |
|---|
| 45 | n/a | |
|---|
| 46 | n/a | static void |
|---|
| 47 | n/a | Noddy_dealloc(Noddy* self) |
|---|
| 48 | n/a | { |
|---|
| 49 | n/a | Noddy_clear(self); |
|---|
| 50 | n/a | Py_TYPE(self)->tp_free((PyObject*)self); |
|---|
| 51 | n/a | } |
|---|
| 52 | n/a | |
|---|
| 53 | n/a | static PyObject * |
|---|
| 54 | n/a | Noddy_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|---|
| 55 | n/a | { |
|---|
| 56 | n/a | Noddy *self; |
|---|
| 57 | n/a | |
|---|
| 58 | n/a | self = (Noddy *)type->tp_alloc(type, 0); |
|---|
| 59 | n/a | if (self != NULL) { |
|---|
| 60 | n/a | self->first = PyUnicode_FromString(""); |
|---|
| 61 | n/a | if (self->first == NULL) { |
|---|
| 62 | n/a | Py_DECREF(self); |
|---|
| 63 | n/a | return NULL; |
|---|
| 64 | n/a | } |
|---|
| 65 | n/a | |
|---|
| 66 | n/a | self->last = PyUnicode_FromString(""); |
|---|
| 67 | n/a | if (self->last == NULL) { |
|---|
| 68 | n/a | Py_DECREF(self); |
|---|
| 69 | n/a | return NULL; |
|---|
| 70 | n/a | } |
|---|
| 71 | n/a | |
|---|
| 72 | n/a | self->number = 0; |
|---|
| 73 | n/a | } |
|---|
| 74 | n/a | |
|---|
| 75 | n/a | return (PyObject *)self; |
|---|
| 76 | n/a | } |
|---|
| 77 | n/a | |
|---|
| 78 | n/a | static int |
|---|
| 79 | n/a | Noddy_init(Noddy *self, PyObject *args, PyObject *kwds) |
|---|
| 80 | n/a | { |
|---|
| 81 | n/a | PyObject *first=NULL, *last=NULL, *tmp; |
|---|
| 82 | n/a | |
|---|
| 83 | n/a | static char *kwlist[] = {"first", "last", "number", NULL}; |
|---|
| 84 | n/a | |
|---|
| 85 | n/a | if (! PyArg_ParseTupleAndKeywords(args, kwds, "|OOi", kwlist, |
|---|
| 86 | n/a | &first, &last, |
|---|
| 87 | n/a | &self->number)) |
|---|
| 88 | n/a | return -1; |
|---|
| 89 | n/a | |
|---|
| 90 | n/a | if (first) { |
|---|
| 91 | n/a | tmp = self->first; |
|---|
| 92 | n/a | Py_INCREF(first); |
|---|
| 93 | n/a | self->first = first; |
|---|
| 94 | n/a | Py_XDECREF(tmp); |
|---|
| 95 | n/a | } |
|---|
| 96 | n/a | |
|---|
| 97 | n/a | if (last) { |
|---|
| 98 | n/a | tmp = self->last; |
|---|
| 99 | n/a | Py_INCREF(last); |
|---|
| 100 | n/a | self->last = last; |
|---|
| 101 | n/a | Py_XDECREF(tmp); |
|---|
| 102 | n/a | } |
|---|
| 103 | n/a | |
|---|
| 104 | n/a | return 0; |
|---|
| 105 | n/a | } |
|---|
| 106 | n/a | |
|---|
| 107 | n/a | |
|---|
| 108 | n/a | static PyMemberDef Noddy_members[] = { |
|---|
| 109 | n/a | {"first", T_OBJECT_EX, offsetof(Noddy, first), 0, |
|---|
| 110 | n/a | "first name"}, |
|---|
| 111 | n/a | {"last", T_OBJECT_EX, offsetof(Noddy, last), 0, |
|---|
| 112 | n/a | "last name"}, |
|---|
| 113 | n/a | {"number", T_INT, offsetof(Noddy, number), 0, |
|---|
| 114 | n/a | "noddy number"}, |
|---|
| 115 | n/a | {NULL} /* Sentinel */ |
|---|
| 116 | n/a | }; |
|---|
| 117 | n/a | |
|---|
| 118 | n/a | static PyObject * |
|---|
| 119 | n/a | Noddy_name(Noddy* self) |
|---|
| 120 | n/a | { |
|---|
| 121 | n/a | if (self->first == NULL) { |
|---|
| 122 | n/a | PyErr_SetString(PyExc_AttributeError, "first"); |
|---|
| 123 | n/a | return NULL; |
|---|
| 124 | n/a | } |
|---|
| 125 | n/a | |
|---|
| 126 | n/a | if (self->last == NULL) { |
|---|
| 127 | n/a | PyErr_SetString(PyExc_AttributeError, "last"); |
|---|
| 128 | n/a | return NULL; |
|---|
| 129 | n/a | } |
|---|
| 130 | n/a | |
|---|
| 131 | n/a | return PyUnicode_FromFormat("%S %S", self->first, self->last); |
|---|
| 132 | n/a | } |
|---|
| 133 | n/a | |
|---|
| 134 | n/a | static PyMethodDef Noddy_methods[] = { |
|---|
| 135 | n/a | {"name", (PyCFunction)Noddy_name, METH_NOARGS, |
|---|
| 136 | n/a | "Return the name, combining the first and last name" |
|---|
| 137 | n/a | }, |
|---|
| 138 | n/a | {NULL} /* Sentinel */ |
|---|
| 139 | n/a | }; |
|---|
| 140 | n/a | |
|---|
| 141 | n/a | static PyTypeObject NoddyType = { |
|---|
| 142 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
|---|
| 143 | n/a | "noddy.Noddy", /* tp_name */ |
|---|
| 144 | n/a | sizeof(Noddy), /* tp_basicsize */ |
|---|
| 145 | n/a | 0, /* tp_itemsize */ |
|---|
| 146 | n/a | (destructor)Noddy_dealloc, /* tp_dealloc */ |
|---|
| 147 | n/a | 0, /* tp_print */ |
|---|
| 148 | n/a | 0, /* tp_getattr */ |
|---|
| 149 | n/a | 0, /* tp_setattr */ |
|---|
| 150 | n/a | 0, /* tp_reserved */ |
|---|
| 151 | n/a | 0, /* tp_repr */ |
|---|
| 152 | n/a | 0, /* tp_as_number */ |
|---|
| 153 | n/a | 0, /* tp_as_sequence */ |
|---|
| 154 | n/a | 0, /* tp_as_mapping */ |
|---|
| 155 | n/a | 0, /* tp_hash */ |
|---|
| 156 | n/a | 0, /* tp_call */ |
|---|
| 157 | n/a | 0, /* tp_str */ |
|---|
| 158 | n/a | 0, /* tp_getattro */ |
|---|
| 159 | n/a | 0, /* tp_setattro */ |
|---|
| 160 | n/a | 0, /* tp_as_buffer */ |
|---|
| 161 | n/a | Py_TPFLAGS_DEFAULT | |
|---|
| 162 | n/a | Py_TPFLAGS_BASETYPE | |
|---|
| 163 | n/a | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
|---|
| 164 | n/a | "Noddy objects", /* tp_doc */ |
|---|
| 165 | n/a | (traverseproc)Noddy_traverse, /* tp_traverse */ |
|---|
| 166 | n/a | (inquiry)Noddy_clear, /* tp_clear */ |
|---|
| 167 | n/a | 0, /* tp_richcompare */ |
|---|
| 168 | n/a | 0, /* tp_weaklistoffset */ |
|---|
| 169 | n/a | 0, /* tp_iter */ |
|---|
| 170 | n/a | 0, /* tp_iternext */ |
|---|
| 171 | n/a | Noddy_methods, /* tp_methods */ |
|---|
| 172 | n/a | Noddy_members, /* tp_members */ |
|---|
| 173 | n/a | 0, /* tp_getset */ |
|---|
| 174 | n/a | 0, /* tp_base */ |
|---|
| 175 | n/a | 0, /* tp_dict */ |
|---|
| 176 | n/a | 0, /* tp_descr_get */ |
|---|
| 177 | n/a | 0, /* tp_descr_set */ |
|---|
| 178 | n/a | 0, /* tp_dictoffset */ |
|---|
| 179 | n/a | (initproc)Noddy_init, /* tp_init */ |
|---|
| 180 | n/a | 0, /* tp_alloc */ |
|---|
| 181 | n/a | Noddy_new, /* tp_new */ |
|---|
| 182 | n/a | }; |
|---|
| 183 | n/a | |
|---|
| 184 | n/a | static PyModuleDef noddy4module = { |
|---|
| 185 | n/a | PyModuleDef_HEAD_INIT, |
|---|
| 186 | n/a | "noddy4", |
|---|
| 187 | n/a | "Example module that creates an extension type.", |
|---|
| 188 | n/a | -1, |
|---|
| 189 | n/a | NULL, NULL, NULL, NULL, NULL |
|---|
| 190 | n/a | }; |
|---|
| 191 | n/a | |
|---|
| 192 | n/a | PyMODINIT_FUNC |
|---|
| 193 | n/a | PyInit_noddy4(void) |
|---|
| 194 | n/a | { |
|---|
| 195 | n/a | PyObject* m; |
|---|
| 196 | n/a | |
|---|
| 197 | n/a | if (PyType_Ready(&NoddyType) < 0) |
|---|
| 198 | n/a | return NULL; |
|---|
| 199 | n/a | |
|---|
| 200 | n/a | m = PyModule_Create(&noddy4module); |
|---|
| 201 | n/a | if (m == NULL) |
|---|
| 202 | n/a | return NULL; |
|---|
| 203 | n/a | |
|---|
| 204 | n/a | Py_INCREF(&NoddyType); |
|---|
| 205 | n/a | PyModule_AddObject(m, "Noddy", (PyObject *)&NoddyType); |
|---|
| 206 | n/a | return m; |
|---|
| 207 | n/a | } |
|---|