xxmodule.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. floatobject.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_IS_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_getattro(XxoObject *self, PyObject *name)
  53. {
  54. if (self->x_attr != NULL) {
  55. PyObject *v = PyDict_GetItemWithError(self->x_attr, name);
  56. if (v != NULL) {
  57. Py_INCREF(v);
  58. return v;
  59. }
  60. else if (PyErr_Occurred()) {
  61. return NULL;
  62. }
  63. }
  64. return PyObject_GenericGetAttr((PyObject *)self, name);
  65. }
  66. static int
  67. Xxo_setattr(XxoObject *self, const char *name, PyObject *v)
  68. {
  69. if (self->x_attr == NULL) {
  70. self->x_attr = PyDict_New();
  71. if (self->x_attr == NULL)
  72. return -1;
  73. }
  74. if (v == NULL) {
  75. int rv = PyDict_DelItemString(self->x_attr, name);
  76. if (rv < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
  77. PyErr_SetString(PyExc_AttributeError,
  78. "delete non-existing Xxo attribute");
  79. return rv;
  80. }
  81. else
  82. return PyDict_SetItemString(self->x_attr, name, v);
  83. }
  84. static PyTypeObject Xxo_Type = {
  85. /* The ob_type field must be initialized in the module init function
  86. * to be portable to Windows without using C++. */
  87. PyVarObject_HEAD_INIT(NULL, 0)
  88. "xxmodule.Xxo", /*tp_name*/
  89. sizeof(XxoObject), /*tp_basicsize*/
  90. 0, /*tp_itemsize*/
  91. /* methods */
  92. (destructor)Xxo_dealloc, /*tp_dealloc*/
  93. 0, /*tp_vectorcall_offset*/
  94. (getattrfunc)0, /*tp_getattr*/
  95. (setattrfunc)Xxo_setattr, /*tp_setattr*/
  96. 0, /*tp_as_async*/
  97. 0, /*tp_repr*/
  98. 0, /*tp_as_number*/
  99. 0, /*tp_as_sequence*/
  100. 0, /*tp_as_mapping*/
  101. 0, /*tp_hash*/
  102. 0, /*tp_call*/
  103. 0, /*tp_str*/
  104. (getattrofunc)Xxo_getattro, /*tp_getattro*/
  105. 0, /*tp_setattro*/
  106. 0, /*tp_as_buffer*/
  107. Py_TPFLAGS_DEFAULT, /*tp_flags*/
  108. 0, /*tp_doc*/
  109. 0, /*tp_traverse*/
  110. 0, /*tp_clear*/
  111. 0, /*tp_richcompare*/
  112. 0, /*tp_weaklistoffset*/
  113. 0, /*tp_iter*/
  114. 0, /*tp_iternext*/
  115. Xxo_methods, /*tp_methods*/
  116. 0, /*tp_members*/
  117. 0, /*tp_getset*/
  118. 0, /*tp_base*/
  119. 0, /*tp_dict*/
  120. 0, /*tp_descr_get*/
  121. 0, /*tp_descr_set*/
  122. 0, /*tp_dictoffset*/
  123. 0, /*tp_init*/
  124. 0, /*tp_alloc*/
  125. 0, /*tp_new*/
  126. 0, /*tp_free*/
  127. 0, /*tp_is_gc*/
  128. };
  129. /* --------------------------------------------------------------------- */
  130. /* Function of two integers returning integer */
  131. PyDoc_STRVAR(xx_foo_doc,
  132. "foo(i,j)\n\
  133. \n\
  134. Return the sum of i and j.");
  135. static PyObject *
  136. xx_foo(PyObject *self, PyObject *args)
  137. {
  138. long i, j;
  139. long res;
  140. if (!PyArg_ParseTuple(args, "ll:foo", &i, &j))
  141. return NULL;
  142. res = i+j; /* XXX Do something here */
  143. return PyLong_FromLong(res);
  144. }
  145. /* Function of no arguments returning new Xxo object */
  146. static PyObject *
  147. xx_new(PyObject *self, PyObject *args)
  148. {
  149. XxoObject *rv;
  150. if (!PyArg_ParseTuple(args, ":new"))
  151. return NULL;
  152. rv = newXxoObject(args);
  153. if (rv == NULL)
  154. return NULL;
  155. return (PyObject *)rv;
  156. }
  157. /* Example with subtle bug from extensions manual ("Thin Ice"). */
  158. static PyObject *
  159. xx_bug(PyObject *self, PyObject *args)
  160. {
  161. PyObject *list, *item;
  162. if (!PyArg_ParseTuple(args, "O:bug", &list))
  163. return NULL;
  164. item = PyList_GetItem(list, 0);
  165. /* Py_INCREF(item); */
  166. PyList_SetItem(list, 1, PyLong_FromLong(0L));
  167. PyObject_Print(item, stdout, 0);
  168. printf("\n");
  169. /* Py_DECREF(item); */
  170. Py_INCREF(Py_None);
  171. return Py_None;
  172. }
  173. /* Test bad format character */
  174. static PyObject *
  175. xx_roj(PyObject *self, PyObject *args)
  176. {
  177. PyObject *a;
  178. long b;
  179. if (!PyArg_ParseTuple(args, "O#:roj", &a, &b))
  180. return NULL;
  181. Py_INCREF(Py_None);
  182. return Py_None;
  183. }
  184. /* ---------- */
  185. static PyTypeObject Str_Type = {
  186. /* The ob_type field must be initialized in the module init function
  187. * to be portable to Windows without using C++. */
  188. PyVarObject_HEAD_INIT(NULL, 0)
  189. "xxmodule.Str", /*tp_name*/
  190. 0, /*tp_basicsize*/
  191. 0, /*tp_itemsize*/
  192. /* methods */
  193. 0, /*tp_dealloc*/
  194. 0, /*tp_vectorcall_offset*/
  195. 0, /*tp_getattr*/
  196. 0, /*tp_setattr*/
  197. 0, /*tp_as_async*/
  198. 0, /*tp_repr*/
  199. 0, /*tp_as_number*/
  200. 0, /*tp_as_sequence*/
  201. 0, /*tp_as_mapping*/
  202. 0, /*tp_hash*/
  203. 0, /*tp_call*/
  204. 0, /*tp_str*/
  205. 0, /*tp_getattro*/
  206. 0, /*tp_setattro*/
  207. 0, /*tp_as_buffer*/
  208. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
  209. 0, /*tp_doc*/
  210. 0, /*tp_traverse*/
  211. 0, /*tp_clear*/
  212. 0, /*tp_richcompare*/
  213. 0, /*tp_weaklistoffset*/
  214. 0, /*tp_iter*/
  215. 0, /*tp_iternext*/
  216. 0, /*tp_methods*/
  217. 0, /*tp_members*/
  218. 0, /*tp_getset*/
  219. 0, /* see PyInit_xx */ /*tp_base*/
  220. 0, /*tp_dict*/
  221. 0, /*tp_descr_get*/
  222. 0, /*tp_descr_set*/
  223. 0, /*tp_dictoffset*/
  224. 0, /*tp_init*/
  225. 0, /*tp_alloc*/
  226. 0, /*tp_new*/
  227. 0, /*tp_free*/
  228. 0, /*tp_is_gc*/
  229. };
  230. /* ---------- */
  231. static PyObject *
  232. null_richcompare(PyObject *self, PyObject *other, int op)
  233. {
  234. Py_INCREF(Py_NotImplemented);
  235. return Py_NotImplemented;
  236. }
  237. static PyTypeObject Null_Type = {
  238. /* The ob_type field must be initialized in the module init function
  239. * to be portable to Windows without using C++. */
  240. PyVarObject_HEAD_INIT(NULL, 0)
  241. "xxmodule.Null", /*tp_name*/
  242. 0, /*tp_basicsize*/
  243. 0, /*tp_itemsize*/
  244. /* methods */
  245. 0, /*tp_dealloc*/
  246. 0, /*tp_vectorcall_offset*/
  247. 0, /*tp_getattr*/
  248. 0, /*tp_setattr*/
  249. 0, /*tp_as_async*/
  250. 0, /*tp_repr*/
  251. 0, /*tp_as_number*/
  252. 0, /*tp_as_sequence*/
  253. 0, /*tp_as_mapping*/
  254. 0, /*tp_hash*/
  255. 0, /*tp_call*/
  256. 0, /*tp_str*/
  257. 0, /*tp_getattro*/
  258. 0, /*tp_setattro*/
  259. 0, /*tp_as_buffer*/
  260. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
  261. 0, /*tp_doc*/
  262. 0, /*tp_traverse*/
  263. 0, /*tp_clear*/
  264. null_richcompare, /*tp_richcompare*/
  265. 0, /*tp_weaklistoffset*/
  266. 0, /*tp_iter*/
  267. 0, /*tp_iternext*/
  268. 0, /*tp_methods*/
  269. 0, /*tp_members*/
  270. 0, /*tp_getset*/
  271. 0, /* see PyInit_xx */ /*tp_base*/
  272. 0, /*tp_dict*/
  273. 0, /*tp_descr_get*/
  274. 0, /*tp_descr_set*/
  275. 0, /*tp_dictoffset*/
  276. 0, /*tp_init*/
  277. 0, /*tp_alloc*/
  278. PyType_GenericNew, /*tp_new*/
  279. 0, /*tp_free*/
  280. 0, /*tp_is_gc*/
  281. };
  282. /* ---------- */
  283. /* List of functions defined in the module */
  284. static PyMethodDef xx_methods[] = {
  285. {"roj", xx_roj, METH_VARARGS,
  286. PyDoc_STR("roj(a,b) -> None")},
  287. {"foo", xx_foo, METH_VARARGS,
  288. xx_foo_doc},
  289. {"new", xx_new, METH_VARARGS,
  290. PyDoc_STR("new() -> new Xx object")},
  291. {"bug", xx_bug, METH_VARARGS,
  292. PyDoc_STR("bug(o) -> None")},
  293. {NULL, NULL} /* sentinel */
  294. };
  295. PyDoc_STRVAR(module_doc,
  296. "This is a template module just for instruction.");
  297. static int
  298. xx_exec(PyObject *m)
  299. {
  300. /* Slot initialization is subject to the rules of initializing globals.
  301. C99 requires the initializers to be "address constants". Function
  302. designators like 'PyType_GenericNew', with implicit conversion to
  303. a pointer, are valid C99 address constants.
  304. However, the unary '&' operator applied to a non-static variable
  305. like 'PyBaseObject_Type' is not required to produce an address
  306. constant. Compilers may support this (gcc does), MSVC does not.
  307. Both compilers are strictly standard conforming in this particular
  308. behavior.
  309. */
  310. Null_Type.tp_base = &PyBaseObject_Type;
  311. Str_Type.tp_base = &PyUnicode_Type;
  312. /* Finalize the type object including setting type of the new type
  313. * object; doing it here is required for portability, too. */
  314. if (PyType_Ready(&Xxo_Type) < 0)
  315. goto fail;
  316. /* Add some symbolic constants to the module */
  317. if (ErrorObject == NULL) {
  318. ErrorObject = PyErr_NewException("xx.error", NULL, NULL);
  319. if (ErrorObject == NULL)
  320. goto fail;
  321. }
  322. Py_INCREF(ErrorObject);
  323. PyModule_AddObject(m, "error", ErrorObject);
  324. /* Add Str */
  325. if (PyType_Ready(&Str_Type) < 0)
  326. goto fail;
  327. PyModule_AddObject(m, "Str", (PyObject *)&Str_Type);
  328. /* Add Null */
  329. if (PyType_Ready(&Null_Type) < 0)
  330. goto fail;
  331. PyModule_AddObject(m, "Null", (PyObject *)&Null_Type);
  332. return 0;
  333. fail:
  334. Py_XDECREF(m);
  335. return -1;
  336. }
  337. static struct PyModuleDef_Slot xx_slots[] = {
  338. {Py_mod_exec, xx_exec},
  339. {0, NULL},
  340. };
  341. static struct PyModuleDef xxmodule = {
  342. PyModuleDef_HEAD_INIT,
  343. "xx",
  344. module_doc,
  345. 0,
  346. xx_methods,
  347. xx_slots,
  348. NULL,
  349. NULL,
  350. NULL
  351. };
  352. /* Export function for the module (*must* be called PyInit_xx) */
  353. PyMODINIT_FUNC
  354. PyInit_xx(void)
  355. {
  356. return PyModuleDef_Init(&xxmodule);
  357. }