xxmodule.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /* Use this file as a template to start implementing a module that
  2. also declares object types. All occurrences of 'Xxo' should be changed
  3. to something reasonable for your objects. After that, all other
  4. occurrences of 'xx' should be changed to something reasonable for your
  5. module. If your module is named foo your sourcefile should be named
  6. foomodule.c.
  7. You will probably want to delete all references to 'x_attr' and add
  8. your own types of attributes instead. Maybe you want to name your
  9. local variables other than 'self'. If your object type is needed in
  10. other files, you'll have to create a file "foobarobject.h"; see
  11. intobject.h for an example. */
  12. /* Xxo objects */
  13. #include "Python.h"
  14. static PyObject *ErrorObject;
  15. typedef struct {
  16. PyObject_HEAD
  17. PyObject *x_attr; /* Attributes dictionary */
  18. } XxoObject;
  19. static PyTypeObject Xxo_Type;
  20. #define XxoObject_Check(v) (Py_TYPE(v) == &Xxo_Type)
  21. static XxoObject *
  22. newXxoObject(PyObject *arg)
  23. {
  24. XxoObject *self;
  25. self = PyObject_New(XxoObject, &Xxo_Type);
  26. if (self == NULL)
  27. return NULL;
  28. self->x_attr = NULL;
  29. return self;
  30. }
  31. /* Xxo methods */
  32. static void
  33. Xxo_dealloc(XxoObject *self)
  34. {
  35. Py_XDECREF(self->x_attr);
  36. PyObject_Del(self);
  37. }
  38. static PyObject *
  39. Xxo_demo(XxoObject *self, PyObject *args)
  40. {
  41. if (!PyArg_ParseTuple(args, ":demo"))
  42. return NULL;
  43. Py_INCREF(Py_None);
  44. return Py_None;
  45. }
  46. static PyMethodDef Xxo_methods[] = {
  47. {"demo", (PyCFunction)Xxo_demo, METH_VARARGS,
  48. PyDoc_STR("demo() -> None")},
  49. {NULL, NULL} /* sentinel */
  50. };
  51. static PyObject *
  52. Xxo_getattr(XxoObject *self, char *name)
  53. {
  54. if (self->x_attr != NULL) {
  55. PyObject *v = PyDict_GetItemString(self->x_attr, name);
  56. if (v != NULL) {
  57. Py_INCREF(v);
  58. return v;
  59. }
  60. }
  61. return Py_FindMethod(Xxo_methods, (PyObject *)self, name);
  62. }
  63. static int
  64. Xxo_setattr(XxoObject *self, char *name, PyObject *v)
  65. {
  66. if (self->x_attr == NULL) {
  67. self->x_attr = PyDict_New();
  68. if (self->x_attr == NULL)
  69. return -1;
  70. }
  71. if (v == NULL) {
  72. int rv = PyDict_DelItemString(self->x_attr, name);
  73. if (rv < 0)
  74. PyErr_SetString(PyExc_AttributeError,
  75. "delete non-existing Xxo attribute");
  76. return rv;
  77. }
  78. else
  79. return PyDict_SetItemString(self->x_attr, name, v);
  80. }
  81. static PyTypeObject Xxo_Type = {
  82. /* The ob_type field must be initialized in the module init function
  83. * to be portable to Windows without using C++. */
  84. PyVarObject_HEAD_INIT(NULL, 0)
  85. "xxmodule.Xxo", /*tp_name*/
  86. sizeof(XxoObject), /*tp_basicsize*/
  87. 0, /*tp_itemsize*/
  88. /* methods */
  89. (destructor)Xxo_dealloc, /*tp_dealloc*/
  90. 0, /*tp_print*/
  91. (getattrfunc)Xxo_getattr, /*tp_getattr*/
  92. (setattrfunc)Xxo_setattr, /*tp_setattr*/
  93. 0, /*tp_compare*/
  94. 0, /*tp_repr*/
  95. 0, /*tp_as_number*/
  96. 0, /*tp_as_sequence*/
  97. 0, /*tp_as_mapping*/
  98. 0, /*tp_hash*/
  99. 0, /*tp_call*/
  100. 0, /*tp_str*/
  101. 0, /*tp_getattro*/
  102. 0, /*tp_setattro*/
  103. 0, /*tp_as_buffer*/
  104. Py_TPFLAGS_DEFAULT, /*tp_flags*/
  105. 0, /*tp_doc*/
  106. 0, /*tp_traverse*/
  107. 0, /*tp_clear*/
  108. 0, /*tp_richcompare*/
  109. 0, /*tp_weaklistoffset*/
  110. 0, /*tp_iter*/
  111. 0, /*tp_iternext*/
  112. 0, /*tp_methods*/
  113. 0, /*tp_members*/
  114. 0, /*tp_getset*/
  115. 0, /*tp_base*/
  116. 0, /*tp_dict*/
  117. 0, /*tp_descr_get*/
  118. 0, /*tp_descr_set*/
  119. 0, /*tp_dictoffset*/
  120. 0, /*tp_init*/
  121. 0, /*tp_alloc*/
  122. 0, /*tp_new*/
  123. 0, /*tp_free*/
  124. 0, /*tp_is_gc*/
  125. };
  126. /* --------------------------------------------------------------------- */
  127. /* Function of two integers returning integer */
  128. PyDoc_STRVAR(xx_foo_doc,
  129. "foo(i,j)\n\
  130. \n\
  131. Return the sum of i and j.");
  132. static PyObject *
  133. xx_foo(PyObject *self, PyObject *args)
  134. {
  135. long i, j;
  136. long res;
  137. if (!PyArg_ParseTuple(args, "ll:foo", &i, &j))
  138. return NULL;
  139. res = i+j; /* XXX Do something here */
  140. return PyInt_FromLong(res);
  141. }
  142. /* Function of no arguments returning new Xxo object */
  143. static PyObject *
  144. xx_new(PyObject *self, PyObject *args)
  145. {
  146. XxoObject *rv;
  147. if (!PyArg_ParseTuple(args, ":new"))
  148. return NULL;
  149. rv = newXxoObject(args);
  150. if (rv == NULL)
  151. return NULL;
  152. return (PyObject *)rv;
  153. }
  154. /* Example with subtle bug from extensions manual ("Thin Ice"). */
  155. static PyObject *
  156. xx_bug(PyObject *self, PyObject *args)
  157. {
  158. PyObject *list, *item;
  159. if (!PyArg_ParseTuple(args, "O:bug", &list))
  160. return NULL;
  161. item = PyList_GetItem(list, 0);
  162. /* Py_INCREF(item); */
  163. PyList_SetItem(list, 1, PyInt_FromLong(0L));
  164. PyObject_Print(item, stdout, 0);
  165. printf("\n");
  166. /* Py_DECREF(item); */
  167. Py_INCREF(Py_None);
  168. return Py_None;
  169. }
  170. /* Test bad format character */
  171. static PyObject *
  172. xx_roj(PyObject *self, PyObject *args)
  173. {
  174. PyObject *a;
  175. long b;
  176. if (!PyArg_ParseTuple(args, "O#:roj", &a, &b))
  177. return NULL;
  178. Py_INCREF(Py_None);
  179. return Py_None;
  180. }
  181. /* ---------- */
  182. static PyTypeObject Str_Type = {
  183. /* The ob_type field must be initialized in the module init function
  184. * to be portable to Windows without using C++. */
  185. PyVarObject_HEAD_INIT(NULL, 0)
  186. "xxmodule.Str", /*tp_name*/
  187. 0, /*tp_basicsize*/
  188. 0, /*tp_itemsize*/
  189. /* methods */
  190. 0, /*tp_dealloc*/
  191. 0, /*tp_print*/
  192. 0, /*tp_getattr*/
  193. 0, /*tp_setattr*/
  194. 0, /*tp_compare*/
  195. 0, /*tp_repr*/
  196. 0, /*tp_as_number*/
  197. 0, /*tp_as_sequence*/
  198. 0, /*tp_as_mapping*/
  199. 0, /*tp_hash*/
  200. 0, /*tp_call*/
  201. 0, /*tp_str*/
  202. 0, /*tp_getattro*/
  203. 0, /*tp_setattro*/
  204. 0, /*tp_as_buffer*/
  205. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
  206. 0, /*tp_doc*/
  207. 0, /*tp_traverse*/
  208. 0, /*tp_clear*/
  209. 0, /*tp_richcompare*/
  210. 0, /*tp_weaklistoffset*/
  211. 0, /*tp_iter*/
  212. 0, /*tp_iternext*/
  213. 0, /*tp_methods*/
  214. 0, /*tp_members*/
  215. 0, /*tp_getset*/
  216. 0, /* see initxx */ /*tp_base*/
  217. 0, /*tp_dict*/
  218. 0, /*tp_descr_get*/
  219. 0, /*tp_descr_set*/
  220. 0, /*tp_dictoffset*/
  221. 0, /*tp_init*/
  222. 0, /*tp_alloc*/
  223. 0, /*tp_new*/
  224. 0, /*tp_free*/
  225. 0, /*tp_is_gc*/
  226. };
  227. /* ---------- */
  228. static PyObject *
  229. null_richcompare(PyObject *self, PyObject *other, int op)
  230. {
  231. Py_INCREF(Py_NotImplemented);
  232. return Py_NotImplemented;
  233. }
  234. static PyTypeObject Null_Type = {
  235. /* The ob_type field must be initialized in the module init function
  236. * to be portable to Windows without using C++. */
  237. PyVarObject_HEAD_INIT(NULL, 0)
  238. "xxmodule.Null", /*tp_name*/
  239. 0, /*tp_basicsize*/
  240. 0, /*tp_itemsize*/
  241. /* methods */
  242. 0, /*tp_dealloc*/
  243. 0, /*tp_print*/
  244. 0, /*tp_getattr*/
  245. 0, /*tp_setattr*/
  246. 0, /*tp_compare*/
  247. 0, /*tp_repr*/
  248. 0, /*tp_as_number*/
  249. 0, /*tp_as_sequence*/
  250. 0, /*tp_as_mapping*/
  251. 0, /*tp_hash*/
  252. 0, /*tp_call*/
  253. 0, /*tp_str*/
  254. 0, /*tp_getattro*/
  255. 0, /*tp_setattro*/
  256. 0, /*tp_as_buffer*/
  257. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
  258. 0, /*tp_doc*/
  259. 0, /*tp_traverse*/
  260. 0, /*tp_clear*/
  261. null_richcompare, /*tp_richcompare*/
  262. 0, /*tp_weaklistoffset*/
  263. 0, /*tp_iter*/
  264. 0, /*tp_iternext*/
  265. 0, /*tp_methods*/
  266. 0, /*tp_members*/
  267. 0, /*tp_getset*/
  268. 0, /* see initxx */ /*tp_base*/
  269. 0, /*tp_dict*/
  270. 0, /*tp_descr_get*/
  271. 0, /*tp_descr_set*/
  272. 0, /*tp_dictoffset*/
  273. 0, /*tp_init*/
  274. 0, /*tp_alloc*/
  275. 0, /* see initxx */ /*tp_new*/
  276. 0, /*tp_free*/
  277. 0, /*tp_is_gc*/
  278. };
  279. /* ---------- */
  280. /* List of functions defined in the module */
  281. static PyMethodDef xx_methods[] = {
  282. {"roj", xx_roj, METH_VARARGS,
  283. PyDoc_STR("roj(a,b) -> None")},
  284. {"foo", xx_foo, METH_VARARGS,
  285. xx_foo_doc},
  286. {"new", xx_new, METH_VARARGS,
  287. PyDoc_STR("new() -> new Xx object")},
  288. {"bug", xx_bug, METH_VARARGS,
  289. PyDoc_STR("bug(o) -> None")},
  290. {NULL, NULL} /* sentinel */
  291. };
  292. PyDoc_STRVAR(module_doc,
  293. "This is a template module just for instruction.");
  294. /* Initialization function for the module (*must* be called initxx) */
  295. PyMODINIT_FUNC
  296. initxx(void)
  297. {
  298. PyObject *m;
  299. /* Due to cross platform compiler issues the slots must be filled
  300. * here. It's required for portability to Windows without requiring
  301. * C++. */
  302. Null_Type.tp_base = &PyBaseObject_Type;
  303. Null_Type.tp_new = PyType_GenericNew;
  304. Str_Type.tp_base = &PyUnicode_Type;
  305. /* Finalize the type object including setting type of the new type
  306. * object; doing it here is required for portability, too. */
  307. if (PyType_Ready(&Xxo_Type) < 0)
  308. return;
  309. /* Create the module and add the functions */
  310. m = Py_InitModule3("xx", xx_methods, module_doc);
  311. if (m == NULL)
  312. return;
  313. /* Add some symbolic constants to the module */
  314. if (ErrorObject == NULL) {
  315. ErrorObject = PyErr_NewException("xx.error", NULL, NULL);
  316. if (ErrorObject == NULL)
  317. return;
  318. }
  319. Py_INCREF(ErrorObject);
  320. PyModule_AddObject(m, "error", ErrorObject);
  321. /* Add Str */
  322. if (PyType_Ready(&Str_Type) < 0)
  323. return;
  324. PyModule_AddObject(m, "Str", (PyObject *)&Str_Type);
  325. /* Add Null */
  326. if (PyType_Ready(&Null_Type) < 0)
  327. return;
  328. PyModule_AddObject(m, "Null", (PyObject *)&Null_Type);
  329. }