/Doc/includes/noddy2.c

http://unladen-swallow.googlecode.com/ · C · 190 lines · 160 code · 30 blank · 0 comment · 22 complexity · d66866a1e1bb89b6aa1043eebdbe8419 MD5 · raw file

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