Jim Fulton | aed0a4a | 2003-05-07 19:48:13 +0000 | [diff] [blame] | 1 | #include <Python.h> |
| 2 | #include "structmember.h" |
| 3 | |
| 4 | typedef struct { |
| 5 | PyObject_HEAD |
| 6 | PyObject *first; |
| 7 | PyObject *last; |
| 8 | int number; |
| 9 | } Noddy; |
| 10 | |
| 11 | static void |
| 12 | Noddy_dealloc(Noddy* self) |
| 13 | { |
| 14 | Py_XDECREF(self->first); |
| 15 | Py_XDECREF(self->last); |
Jim Fulton | 1f32556 | 2003-05-16 13:53:43 +0000 | [diff] [blame^] | 16 | self->ob_type->tp_free((PyObject*)self); |
Jim Fulton | aed0a4a | 2003-05-07 19:48:13 +0000 | [diff] [blame] | 17 | } |
| 18 | |
| 19 | static PyObject * |
| 20 | Noddy_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 21 | { |
| 22 | Noddy *self; |
| 23 | |
| 24 | self = (Noddy *)type->tp_alloc(type, 0); |
| 25 | if (self != NULL) { |
| 26 | self->first = PyString_FromString(""); |
| 27 | if (self->first == NULL) |
| 28 | { |
| 29 | Py_DECREF(self); |
| 30 | return NULL; |
| 31 | } |
| 32 | |
| 33 | self->last = PyString_FromString(""); |
| 34 | if (self->last == NULL) |
| 35 | { |
| 36 | Py_DECREF(self); |
| 37 | return NULL; |
| 38 | } |
| 39 | |
| 40 | self->number = 0; |
| 41 | } |
| 42 | |
| 43 | return (PyObject *)self; |
| 44 | } |
| 45 | |
| 46 | static PyObject * |
| 47 | Noddy_init(Noddy *self, PyObject *args, PyObject *kwds) |
| 48 | { |
| 49 | PyObject *first=NULL, *last=NULL; |
| 50 | |
| 51 | static char *kwlist[] = {"first", "last", "number", NULL}; |
| 52 | |
| 53 | if (! PyArg_ParseTupleAndKeywords(args, kwds, "|OOi", kwlist, |
| 54 | &first, &last, |
| 55 | &self->number)) |
| 56 | return NULL; |
| 57 | |
| 58 | if (first) { |
| 59 | Py_XDECREF(self->first); |
| 60 | Py_INCREF(first); |
| 61 | self->first = first; |
| 62 | } |
| 63 | |
| 64 | if (last) { |
| 65 | Py_XDECREF(self->last); |
| 66 | Py_INCREF(last); |
| 67 | self->last = last; |
| 68 | } |
| 69 | |
| 70 | Py_INCREF(Py_None); |
| 71 | return Py_None; |
| 72 | } |
| 73 | |
| 74 | |
| 75 | static PyMemberDef Noddy_members[] = { |
| 76 | {"first", T_OBJECT_EX, offsetof(Noddy, first), 0, |
| 77 | "first name"}, |
| 78 | {"last", T_OBJECT_EX, offsetof(Noddy, last), 0, |
| 79 | "last name"}, |
| 80 | {"number", T_INT, offsetof(Noddy, number), 0, |
| 81 | "noddy number"}, |
| 82 | {NULL} /* Sentinel */ |
| 83 | }; |
| 84 | |
| 85 | static PyObject * |
| 86 | Noddy_name(Noddy* self) |
| 87 | { |
| 88 | static PyObject *format = NULL; |
| 89 | PyObject *args, *result; |
| 90 | |
| 91 | if (format == NULL) { |
| 92 | format = PyString_FromString("%s %s"); |
| 93 | if (format == NULL) |
| 94 | return NULL; |
| 95 | } |
| 96 | |
| 97 | if (self->first == NULL) { |
| 98 | PyErr_SetString(PyExc_AttributeError, "first"); |
| 99 | return NULL; |
| 100 | } |
| 101 | |
| 102 | if (self->last == NULL) { |
| 103 | PyErr_SetString(PyExc_AttributeError, "last"); |
| 104 | return NULL; |
| 105 | } |
| 106 | |
| 107 | args = Py_BuildValue("OO", self->first, self->last); |
| 108 | if (args == NULL) |
| 109 | return NULL; |
| 110 | |
| 111 | result = PyString_Format(format, args); |
| 112 | Py_DECREF(args); |
| 113 | |
| 114 | return result; |
| 115 | } |
| 116 | |
| 117 | static PyMethodDef Noddy_methods[] = { |
| 118 | {"name", (PyCFunction)Noddy_name, METH_NOARGS, |
| 119 | "Return the name, combining the first and last name" |
| 120 | }, |
| 121 | {NULL} /* Sentinel */ |
| 122 | }; |
| 123 | |
| 124 | static PyTypeObject NoddyType = { |
| 125 | PyObject_HEAD_INIT(NULL) |
| 126 | 0, /*ob_size*/ |
| 127 | "noddy.Noddy", /*tp_name*/ |
| 128 | sizeof(Noddy), /*tp_basicsize*/ |
| 129 | 0, /*tp_itemsize*/ |
| 130 | (destructor)Noddy_dealloc, /*tp_dealloc*/ |
| 131 | 0, /*tp_print*/ |
| 132 | 0, /*tp_getattr*/ |
| 133 | 0, /*tp_setattr*/ |
| 134 | 0, /*tp_compare*/ |
| 135 | 0, /*tp_repr*/ |
| 136 | 0, /*tp_as_number*/ |
| 137 | 0, /*tp_as_sequence*/ |
| 138 | 0, /*tp_as_mapping*/ |
| 139 | 0, /*tp_hash */ |
| 140 | 0, /*tp_call*/ |
| 141 | 0, /*tp_str*/ |
| 142 | 0, /*tp_getattro*/ |
| 143 | 0, /*tp_setattro*/ |
| 144 | 0, /*tp_as_buffer*/ |
| 145 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
| 146 | "Noddy objects", /* tp_doc */ |
| 147 | 0, /* tp_traverse */ |
| 148 | 0, /* tp_clear */ |
| 149 | 0, /* tp_richcompare */ |
| 150 | 0, /* tp_weaklistoffset */ |
| 151 | 0, /* tp_iter */ |
| 152 | 0, /* tp_iternext */ |
| 153 | Noddy_methods, /* tp_methods */ |
| 154 | Noddy_members, /* tp_members */ |
| 155 | 0, /* tp_getset */ |
| 156 | 0, /* tp_base */ |
| 157 | 0, /* tp_dict */ |
| 158 | 0, /* tp_descr_get */ |
| 159 | 0, /* tp_descr_set */ |
| 160 | 0, /* tp_dictoffset */ |
| 161 | (initproc)Noddy_init, /* tp_init */ |
| 162 | 0, /* tp_alloc */ |
| 163 | Noddy_new, /* tp_new */ |
| 164 | }; |
| 165 | |
| 166 | static PyMethodDef module_methods[] = { |
| 167 | {NULL} /* Sentinel */ |
| 168 | }; |
| 169 | |
Jim Fulton | 1f32556 | 2003-05-16 13:53:43 +0000 | [diff] [blame^] | 170 | #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */ |
| 171 | #define PyMODINIT_FUNC void |
| 172 | #endif |
Jim Fulton | aed0a4a | 2003-05-07 19:48:13 +0000 | [diff] [blame] | 173 | PyMODINIT_FUNC |
| 174 | initnoddy2(void) |
| 175 | { |
| 176 | PyObject* m; |
| 177 | |
| 178 | if (PyType_Ready(&NoddyType) < 0) |
| 179 | return; |
| 180 | |
| 181 | m = Py_InitModule3("noddy2", module_methods, |
| 182 | "Example module that creates an extension type."); |
| 183 | |
| 184 | if (m == NULL) |
| 185 | return; |
| 186 | |
| 187 | PyModule_AddObject(m, "Noddy", (PyObject *)&NoddyType); |
| 188 | } |