/Doc/includes/noddy4.c

http://unladen-swallow.googlecode.com/ · C · 224 lines · 187 code · 37 blank · 0 comment · 28 complexity · 6278a33e4c6f056496098aad972b97e6 MD5 · raw file

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