blob: 964155845fee83e53275468e41ffa89ef103f19f [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001#include <Python.h>
2#include "structmember.h"
3
4typedef struct {
5 PyObject_HEAD
6 PyObject *first; /* first name */
7 PyObject *last; /* last name */
8 int number;
9} Noddy;
10
11static void
12Noddy_dealloc(Noddy* self)
13{
14 Py_XDECREF(self->first);
15 Py_XDECREF(self->last);
Georg Brandl913b2a32008-12-05 15:12:15 +000016 Py_TYPE(self)->tp_free((PyObject*)self);
Georg Brandl116aa622007-08-15 14:28:22 +000017}
18
19static PyObject *
20Noddy_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 Norwitzbed67842007-10-27 04:00:45 +000026 self->first = PyUnicode_FromString("");
Eli Bendersky47fe5c02011-08-12 11:40:39 +030027 if (self->first == NULL) {
Georg Brandl116aa622007-08-15 14:28:22 +000028 Py_DECREF(self);
29 return NULL;
Eli Bendersky47fe5c02011-08-12 11:40:39 +030030 }
31
Neal Norwitzbed67842007-10-27 04:00:45 +000032 self->last = PyUnicode_FromString("");
Eli Bendersky47fe5c02011-08-12 11:40:39 +030033 if (self->last == NULL) {
Georg Brandl116aa622007-08-15 14:28:22 +000034 Py_DECREF(self);
35 return NULL;
Eli Bendersky47fe5c02011-08-12 11:40:39 +030036 }
Georg Brandl116aa622007-08-15 14:28:22 +000037
38 self->number = 0;
39 }
40
41 return (PyObject *)self;
42}
43
44static int
45Noddy_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
Eli Bendersky47fe5c02011-08-12 11:40:39 +030051 if (! PyArg_ParseTupleAndKeywords(args, kwds, "|OOi", kwlist,
52 &first, &last,
Georg Brandl116aa622007-08-15 14:28:22 +000053 &self->number))
Eli Bendersky47fe5c02011-08-12 11:40:39 +030054 return -1;
Georg Brandl116aa622007-08-15 14:28:22 +000055
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
74static 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
84static PyObject *
85Noddy_name(Noddy* self)
86{
Georg Brandl116aa622007-08-15 14:28:22 +000087 if (self->first == NULL) {
88 PyErr_SetString(PyExc_AttributeError, "first");
89 return NULL;
90 }
91
92 if (self->last == NULL) {
93 PyErr_SetString(PyExc_AttributeError, "last");
94 return NULL;
95 }
96
Amaury Forgeot d'Arc864741b2011-11-06 15:10:48 +010097 return PyUnicode_FromFormat("%S %S", self->first, self->last);
Georg Brandl116aa622007-08-15 14:28:22 +000098}
99
100static PyMethodDef Noddy_methods[] = {
101 {"name", (PyCFunction)Noddy_name, METH_NOARGS,
102 "Return the name, combining the first and last name"
103 },
104 {NULL} /* Sentinel */
105};
106
107static PyTypeObject NoddyType = {
Georg Brandl51335292009-05-05 07:55:26 +0000108 PyVarObject_HEAD_INIT(NULL, 0)
Georg Brandl913b2a32008-12-05 15:12:15 +0000109 "noddy.Noddy", /* tp_name */
110 sizeof(Noddy), /* tp_basicsize */
111 0, /* tp_itemsize */
112 (destructor)Noddy_dealloc, /* tp_dealloc */
113 0, /* tp_print */
114 0, /* tp_getattr */
115 0, /* tp_setattr */
Mark Dickinson9f989262009-02-02 21:29:40 +0000116 0, /* tp_reserved */
Georg Brandl913b2a32008-12-05 15:12:15 +0000117 0, /* tp_repr */
118 0, /* tp_as_number */
119 0, /* tp_as_sequence */
120 0, /* tp_as_mapping */
121 0, /* tp_hash */
122 0, /* tp_call */
123 0, /* tp_str */
124 0, /* tp_getattro */
125 0, /* tp_setattro */
126 0, /* tp_as_buffer */
127 Py_TPFLAGS_DEFAULT |
128 Py_TPFLAGS_BASETYPE, /* tp_flags */
Georg Brandl116aa622007-08-15 14:28:22 +0000129 "Noddy objects", /* tp_doc */
Eli Bendersky47fe5c02011-08-12 11:40:39 +0300130 0, /* tp_traverse */
131 0, /* tp_clear */
132 0, /* tp_richcompare */
133 0, /* tp_weaklistoffset */
134 0, /* tp_iter */
135 0, /* tp_iternext */
Georg Brandl116aa622007-08-15 14:28:22 +0000136 Noddy_methods, /* tp_methods */
137 Noddy_members, /* tp_members */
138 0, /* tp_getset */
139 0, /* tp_base */
140 0, /* tp_dict */
141 0, /* tp_descr_get */
142 0, /* tp_descr_set */
143 0, /* tp_dictoffset */
144 (initproc)Noddy_init, /* tp_init */
145 0, /* tp_alloc */
146 Noddy_new, /* tp_new */
147};
148
Georg Brandl913b2a32008-12-05 15:12:15 +0000149static PyModuleDef noddy2module = {
150 PyModuleDef_HEAD_INIT,
151 "noddy2",
152 "Example module that creates an extension type.",
153 -1,
154 NULL, NULL, NULL, NULL, NULL
Georg Brandl116aa622007-08-15 14:28:22 +0000155};
156
Georg Brandl116aa622007-08-15 14:28:22 +0000157PyMODINIT_FUNC
Eli Bendersky47fe5c02011-08-12 11:40:39 +0300158PyInit_noddy2(void)
Georg Brandl116aa622007-08-15 14:28:22 +0000159{
160 PyObject* m;
161
162 if (PyType_Ready(&NoddyType) < 0)
Georg Brandl913b2a32008-12-05 15:12:15 +0000163 return NULL;
Georg Brandl116aa622007-08-15 14:28:22 +0000164
Georg Brandl913b2a32008-12-05 15:12:15 +0000165 m = PyModule_Create(&noddy2module);
Georg Brandl116aa622007-08-15 14:28:22 +0000166 if (m == NULL)
Georg Brandl913b2a32008-12-05 15:12:15 +0000167 return NULL;
Georg Brandl116aa622007-08-15 14:28:22 +0000168
169 Py_INCREF(&NoddyType);
170 PyModule_AddObject(m, "Noddy", (PyObject *)&NoddyType);
Benjamin Peterson71e30a02008-12-24 16:27:25 +0000171 return m;
Georg Brandl116aa622007-08-15 14:28:22 +0000172}