_test_extension.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /* This is a set of functions used by test_extension_interface.py to test the
  2. * Greenlet C API.
  3. */
  4. #include "../greenlet.h"
  5. #ifndef Py_RETURN_NONE
  6. # define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
  7. #endif
  8. #define TEST_MODULE_NAME "_test_extension"
  9. static PyObject*
  10. test_switch(PyObject* self, PyObject* greenlet)
  11. {
  12. PyObject* result = NULL;
  13. if (greenlet == NULL || !PyGreenlet_Check(greenlet)) {
  14. PyErr_BadArgument();
  15. return NULL;
  16. }
  17. result = PyGreenlet_Switch((PyGreenlet*)greenlet, NULL, NULL);
  18. if (result == NULL) {
  19. if (!PyErr_Occurred()) {
  20. PyErr_SetString(PyExc_AssertionError,
  21. "greenlet.switch() failed for some reason.");
  22. }
  23. return NULL;
  24. }
  25. Py_INCREF(result);
  26. return result;
  27. }
  28. static PyObject*
  29. test_switch_kwargs(PyObject* self, PyObject* args, PyObject* kwargs)
  30. {
  31. PyGreenlet* g = NULL;
  32. PyObject* result = NULL;
  33. PyArg_ParseTuple(args, "O!", &PyGreenlet_Type, &g);
  34. if (g == NULL || !PyGreenlet_Check(g)) {
  35. PyErr_BadArgument();
  36. return NULL;
  37. }
  38. result = PyGreenlet_Switch(g, NULL, kwargs);
  39. if (result == NULL) {
  40. if (!PyErr_Occurred()) {
  41. PyErr_SetString(PyExc_AssertionError,
  42. "greenlet.switch() failed for some reason.");
  43. }
  44. return NULL;
  45. }
  46. Py_XINCREF(result);
  47. return result;
  48. }
  49. static PyObject*
  50. test_getcurrent(PyObject* self)
  51. {
  52. PyGreenlet* g = PyGreenlet_GetCurrent();
  53. if (g == NULL || !PyGreenlet_Check(g) || !PyGreenlet_ACTIVE(g)) {
  54. PyErr_SetString(PyExc_AssertionError,
  55. "getcurrent() returned an invalid greenlet");
  56. Py_XDECREF(g);
  57. return NULL;
  58. }
  59. Py_DECREF(g);
  60. Py_RETURN_NONE;
  61. }
  62. static PyObject*
  63. test_setparent(PyObject* self, PyObject* arg)
  64. {
  65. PyGreenlet* current;
  66. PyGreenlet* greenlet = NULL;
  67. if (arg == NULL || !PyGreenlet_Check(arg)) {
  68. PyErr_BadArgument();
  69. return NULL;
  70. }
  71. if ((current = PyGreenlet_GetCurrent()) == NULL) {
  72. return NULL;
  73. }
  74. greenlet = (PyGreenlet*)arg;
  75. if (PyGreenlet_SetParent(greenlet, current)) {
  76. Py_DECREF(current);
  77. return NULL;
  78. }
  79. Py_DECREF(current);
  80. if (PyGreenlet_Switch(greenlet, NULL, NULL) == NULL) {
  81. return NULL;
  82. }
  83. Py_RETURN_NONE;
  84. }
  85. static PyObject*
  86. test_new_greenlet(PyObject* self, PyObject* callable)
  87. {
  88. PyObject* result = NULL;
  89. PyGreenlet* greenlet = PyGreenlet_New(callable, NULL);
  90. if (!greenlet) {
  91. return NULL;
  92. }
  93. result = PyGreenlet_Switch(greenlet, NULL, NULL);
  94. Py_CLEAR(greenlet);
  95. if (result == NULL) {
  96. return NULL;
  97. }
  98. Py_INCREF(result);
  99. return result;
  100. }
  101. static PyObject*
  102. test_raise_dead_greenlet(PyObject* self)
  103. {
  104. PyErr_SetString(PyExc_GreenletExit, "test GreenletExit exception.");
  105. return NULL;
  106. }
  107. static PyObject*
  108. test_raise_greenlet_error(PyObject* self)
  109. {
  110. PyErr_SetString(PyExc_GreenletError, "test greenlet.error exception");
  111. return NULL;
  112. }
  113. static PyObject*
  114. test_throw(PyObject* self, PyGreenlet* g)
  115. {
  116. const char msg[] = "take that sucka!";
  117. PyObject* msg_obj = Py_BuildValue("s", msg);
  118. PyGreenlet_Throw(g, PyExc_ValueError, msg_obj, NULL);
  119. Py_DECREF(msg_obj);
  120. if (PyErr_Occurred()) {
  121. return NULL;
  122. }
  123. Py_RETURN_NONE;
  124. }
  125. static PyObject*
  126. test_throw_exact(PyObject* self, PyObject* args)
  127. {
  128. PyGreenlet* g = NULL;
  129. PyObject* typ = NULL;
  130. PyObject* val = NULL;
  131. PyObject* tb = NULL;
  132. if (!PyArg_ParseTuple(args, "OOOO:throw", &g, &typ, &val, &tb)) {
  133. return NULL;
  134. }
  135. PyGreenlet_Throw(g, typ, val, tb);
  136. if (PyErr_Occurred()) {
  137. return NULL;
  138. }
  139. Py_RETURN_NONE;
  140. }
  141. static PyMethodDef test_methods[] = {
  142. {"test_switch",
  143. (PyCFunction)test_switch,
  144. METH_O,
  145. "Switch to the provided greenlet sending provided arguments, and \n"
  146. "return the results."},
  147. {"test_switch_kwargs",
  148. (PyCFunction)test_switch_kwargs,
  149. METH_VARARGS | METH_KEYWORDS,
  150. "Switch to the provided greenlet sending the provided keyword args."},
  151. {"test_getcurrent",
  152. (PyCFunction)test_getcurrent,
  153. METH_NOARGS,
  154. "Test PyGreenlet_GetCurrent()"},
  155. {"test_setparent",
  156. (PyCFunction)test_setparent,
  157. METH_O,
  158. "Se the parent of the provided greenlet and switch to it."},
  159. {"test_new_greenlet",
  160. (PyCFunction)test_new_greenlet,
  161. METH_O,
  162. "Test PyGreenlet_New()"},
  163. {"test_raise_dead_greenlet",
  164. (PyCFunction)test_raise_dead_greenlet,
  165. METH_NOARGS,
  166. "Just raise greenlet.GreenletExit"},
  167. {"test_raise_greenlet_error",
  168. (PyCFunction)test_raise_greenlet_error,
  169. METH_NOARGS,
  170. "Just raise greenlet.error"},
  171. {"test_throw",
  172. (PyCFunction)test_throw,
  173. METH_O,
  174. "Throw a ValueError at the provided greenlet"},
  175. {"test_throw_exact",
  176. (PyCFunction)test_throw_exact,
  177. METH_VARARGS,
  178. "Throw exactly the arguments given at the provided greenlet"},
  179. {NULL, NULL, 0, NULL}
  180. };
  181. #if PY_MAJOR_VERSION >= 3
  182. # define INITERROR return NULL
  183. static struct PyModuleDef moduledef = {PyModuleDef_HEAD_INIT,
  184. TEST_MODULE_NAME,
  185. NULL,
  186. 0,
  187. test_methods,
  188. NULL,
  189. NULL,
  190. NULL,
  191. NULL};
  192. PyMODINIT_FUNC
  193. PyInit__test_extension(void)
  194. #else
  195. # define INITERROR return
  196. PyMODINIT_FUNC
  197. init_test_extension(void)
  198. #endif
  199. {
  200. PyObject* module = NULL;
  201. #if PY_MAJOR_VERSION >= 3
  202. module = PyModule_Create(&moduledef);
  203. #else
  204. module = Py_InitModule(TEST_MODULE_NAME, test_methods);
  205. #endif
  206. if (module == NULL) {
  207. INITERROR;
  208. }
  209. PyGreenlet_Import();
  210. #if PY_MAJOR_VERSION >= 3
  211. return module;
  212. #endif
  213. }