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