Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | #include <Python.h> |
| 2 | #include "structmember.h" |
| 3 | |
| 4 | typedef struct { |
| 5 | PyObject_HEAD |
| 6 | PyObject *first; /* first name */ |
| 7 | PyObject *last; /* last name */ |
| 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); |
Georg Brandl | 913b2a3 | 2008-12-05 15:12:15 +0000 | [diff] [blame] | 16 | Py_TYPE(self)->tp_free((PyObject*)self); |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +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) { |
Neal Norwitz | bed6784 | 2007-10-27 04:00:45 +0000 | [diff] [blame] | 26 | self->first = PyUnicode_FromString(""); |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 27 | if (self->first == NULL) |
| 28 | { |
| 29 | Py_DECREF(self); |
| 30 | return NULL; |
| 31 | } |
| 32 | |
Neal Norwitz | bed6784 | 2007-10-27 04:00:45 +0000 | [diff] [blame] | 33 | self->last = PyUnicode_FromString(""); |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 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 int |
| 47 | Noddy_init(Noddy *self, PyObject *args, PyObject *kwds) |
| 48 | { |
| 49 | PyObject *first=NULL, *last=NULL, *tmp; |
| 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 -1; |
| 57 | |
| 58 | if (first) { |
| 59 | tmp = self->first; |
| 60 | Py_INCREF(first); |
| 61 | self->first = first; |
| 62 | Py_XDECREF(tmp); |
| 63 | } |
| 64 | |
| 65 | if (last) { |
| 66 | tmp = self->last; |
| 67 | Py_INCREF(last); |
| 68 | self->last = last; |
| 69 | Py_XDECREF(tmp); |
| 70 | } |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | |
| 76 | static PyMemberDef Noddy_members[] = { |
| 77 | {"first", T_OBJECT_EX, offsetof(Noddy, first), 0, |
| 78 | "first name"}, |
| 79 | {"last", T_OBJECT_EX, offsetof(Noddy, last), 0, |
| 80 | "last name"}, |
| 81 | {"number", T_INT, offsetof(Noddy, number), 0, |
| 82 | "noddy number"}, |
| 83 | {NULL} /* Sentinel */ |
| 84 | }; |
| 85 | |
| 86 | static PyObject * |
| 87 | Noddy_name(Noddy* self) |
| 88 | { |
| 89 | static PyObject *format = NULL; |
| 90 | PyObject *args, *result; |
| 91 | |
| 92 | if (format == NULL) { |
Neal Norwitz | bed6784 | 2007-10-27 04:00:45 +0000 | [diff] [blame] | 93 | format = PyUnicode_FromString("%s %s"); |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 94 | if (format == NULL) |
| 95 | return NULL; |
| 96 | } |
| 97 | |
| 98 | if (self->first == NULL) { |
| 99 | PyErr_SetString(PyExc_AttributeError, "first"); |
| 100 | return NULL; |
| 101 | } |
| 102 | |
| 103 | if (self->last == NULL) { |
| 104 | PyErr_SetString(PyExc_AttributeError, "last"); |
| 105 | return NULL; |
| 106 | } |
| 107 | |
| 108 | args = Py_BuildValue("OO", self->first, self->last); |
| 109 | if (args == NULL) |
| 110 | return NULL; |
| 111 | |
Neal Norwitz | bed6784 | 2007-10-27 04:00:45 +0000 | [diff] [blame] | 112 | result = PyUnicode_Format(format, args); |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 113 | Py_DECREF(args); |
| 114 | |
| 115 | return result; |
| 116 | } |
| 117 | |
| 118 | static PyMethodDef Noddy_methods[] = { |
| 119 | {"name", (PyCFunction)Noddy_name, METH_NOARGS, |
| 120 | "Return the name, combining the first and last name" |
| 121 | }, |
| 122 | {NULL} /* Sentinel */ |
| 123 | }; |
| 124 | |
| 125 | static PyTypeObject NoddyType = { |
Georg Brandl | 5133529 | 2009-05-05 07:55:26 +0000 | [diff] [blame] | 126 | PyVarObject_HEAD_INIT(NULL, 0) |
Georg Brandl | 913b2a3 | 2008-12-05 15:12:15 +0000 | [diff] [blame] | 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 */ |
Mark Dickinson | 9f98926 | 2009-02-02 21:29:40 +0000 | [diff] [blame] | 134 | 0, /* tp_reserved */ |
Georg Brandl | 913b2a3 | 2008-12-05 15:12:15 +0000 | [diff] [blame] | 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 | |
| 146 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 147 | "Noddy objects", /* tp_doc */ |
| 148 | 0, /* tp_traverse */ |
| 149 | 0, /* tp_clear */ |
| 150 | 0, /* tp_richcompare */ |
| 151 | 0, /* tp_weaklistoffset */ |
| 152 | 0, /* tp_iter */ |
| 153 | 0, /* tp_iternext */ |
| 154 | Noddy_methods, /* tp_methods */ |
| 155 | Noddy_members, /* tp_members */ |
| 156 | 0, /* tp_getset */ |
| 157 | 0, /* tp_base */ |
| 158 | 0, /* tp_dict */ |
| 159 | 0, /* tp_descr_get */ |
| 160 | 0, /* tp_descr_set */ |
| 161 | 0, /* tp_dictoffset */ |
| 162 | (initproc)Noddy_init, /* tp_init */ |
| 163 | 0, /* tp_alloc */ |
| 164 | Noddy_new, /* tp_new */ |
| 165 | }; |
| 166 | |
Georg Brandl | 913b2a3 | 2008-12-05 15:12:15 +0000 | [diff] [blame] | 167 | static PyModuleDef noddy2module = { |
| 168 | PyModuleDef_HEAD_INIT, |
| 169 | "noddy2", |
| 170 | "Example module that creates an extension type.", |
| 171 | -1, |
| 172 | NULL, NULL, NULL, NULL, NULL |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 173 | }; |
| 174 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 175 | PyMODINIT_FUNC |
Georg Brandl | 913b2a3 | 2008-12-05 15:12:15 +0000 | [diff] [blame] | 176 | PyInit_noddy2(void) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 177 | { |
| 178 | PyObject* m; |
| 179 | |
| 180 | if (PyType_Ready(&NoddyType) < 0) |
Georg Brandl | 913b2a3 | 2008-12-05 15:12:15 +0000 | [diff] [blame] | 181 | return NULL; |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 182 | |
Georg Brandl | 913b2a3 | 2008-12-05 15:12:15 +0000 | [diff] [blame] | 183 | m = PyModule_Create(&noddy2module); |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 184 | if (m == NULL) |
Georg Brandl | 913b2a3 | 2008-12-05 15:12:15 +0000 | [diff] [blame] | 185 | return NULL; |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 186 | |
| 187 | Py_INCREF(&NoddyType); |
| 188 | PyModule_AddObject(m, "Noddy", (PyObject *)&NoddyType); |
Benjamin Peterson | 71e30a0 | 2008-12-24 16:27:25 +0000 | [diff] [blame] | 189 | return m; |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 190 | } |