/Doc/includes/shoddy.c

http://unladen-swallow.googlecode.com/ · C · 91 lines · 79 code · 12 blank · 0 comment · 4 complexity · 73941bad0737d160bf44f0abcb2aa230 MD5 · raw file

  1. #include <Python.h>
  2. typedef struct {
  3. PyListObject list;
  4. int state;
  5. } Shoddy;
  6. static PyObject *
  7. Shoddy_increment(Shoddy *self, PyObject *unused)
  8. {
  9. self->state++;
  10. return PyInt_FromLong(self->state);
  11. }
  12. static PyMethodDef Shoddy_methods[] = {
  13. {"increment", (PyCFunction)Shoddy_increment, METH_NOARGS,
  14. PyDoc_STR("increment state counter")},
  15. {NULL, NULL},
  16. };
  17. static int
  18. Shoddy_init(Shoddy *self, PyObject *args, PyObject *kwds)
  19. {
  20. if (PyList_Type.tp_init((PyObject *)self, args, kwds) < 0)
  21. return -1;
  22. self->state = 0;
  23. return 0;
  24. }
  25. static PyTypeObject ShoddyType = {
  26. PyObject_HEAD_INIT(NULL)
  27. 0, /* ob_size */
  28. "shoddy.Shoddy", /* tp_name */
  29. sizeof(Shoddy), /* tp_basicsize */
  30. 0, /* tp_itemsize */
  31. 0, /* tp_dealloc */
  32. 0, /* tp_print */
  33. 0, /* tp_getattr */
  34. 0, /* tp_setattr */
  35. 0, /* tp_compare */
  36. 0, /* tp_repr */
  37. 0, /* tp_as_number */
  38. 0, /* tp_as_sequence */
  39. 0, /* tp_as_mapping */
  40. 0, /* tp_hash */
  41. 0, /* tp_call */
  42. 0, /* tp_str */
  43. 0, /* tp_getattro */
  44. 0, /* tp_setattro */
  45. 0, /* tp_as_buffer */
  46. Py_TPFLAGS_DEFAULT |
  47. Py_TPFLAGS_BASETYPE, /* tp_flags */
  48. 0, /* tp_doc */
  49. 0, /* tp_traverse */
  50. 0, /* tp_clear */
  51. 0, /* tp_richcompare */
  52. 0, /* tp_weaklistoffset */
  53. 0, /* tp_iter */
  54. 0, /* tp_iternext */
  55. Shoddy_methods, /* tp_methods */
  56. 0, /* tp_members */
  57. 0, /* tp_getset */
  58. 0, /* tp_base */
  59. 0, /* tp_dict */
  60. 0, /* tp_descr_get */
  61. 0, /* tp_descr_set */
  62. 0, /* tp_dictoffset */
  63. (initproc)Shoddy_init, /* tp_init */
  64. 0, /* tp_alloc */
  65. 0, /* tp_new */
  66. };
  67. PyMODINIT_FUNC
  68. initshoddy(void)
  69. {
  70. PyObject *m;
  71. ShoddyType.tp_base = &PyList_Type;
  72. if (PyType_Ready(&ShoddyType) < 0)
  73. return;
  74. m = Py_InitModule3("shoddy", NULL, "Shoddy module");
  75. if (m == NULL)
  76. return;
  77. Py_INCREF(&ShoddyType);
  78. PyModule_AddObject(m, "Shoddy", (PyObject *) &ShoddyType);
  79. }