_test_extension.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. if (result == NULL) {
  95. return NULL;
  96. }
  97. Py_INCREF(result);
  98. return result;
  99. }
  100. static PyObject*
  101. test_raise_dead_greenlet(PyObject* self)
  102. {
  103. PyErr_SetString(PyExc_GreenletExit, "test GreenletExit exception.");
  104. return NULL;
  105. }
  106. static PyObject*
  107. test_raise_greenlet_error(PyObject* self)
  108. {
  109. PyErr_SetString(PyExc_GreenletError, "test greenlet.error exception");
  110. return NULL;
  111. }
  112. static PyObject*
  113. test_throw(PyObject* self, PyGreenlet* g)
  114. {
  115. const char msg[] = "take that sucka!";
  116. PyObject* msg_obj = Py_BuildValue("s", msg);
  117. PyGreenlet_Throw(g, PyExc_ValueError, msg_obj, NULL);
  118. Py_DECREF(msg_obj);
  119. Py_RETURN_NONE;
  120. }
  121. static PyMethodDef test_methods[] = {
  122. {"test_switch",
  123. (PyCFunction)test_switch,
  124. METH_O,
  125. "Switch to the provided greenlet sending provided arguments, and \n"
  126. "return the results."},
  127. {"test_switch_kwargs",
  128. (PyCFunction)test_switch_kwargs,
  129. METH_VARARGS | METH_KEYWORDS,
  130. "Switch to the provided greenlet sending the provided keyword args."},
  131. {"test_getcurrent",
  132. (PyCFunction)test_getcurrent,
  133. METH_NOARGS,
  134. "Test PyGreenlet_GetCurrent()"},
  135. {"test_setparent",
  136. (PyCFunction)test_setparent,
  137. METH_O,
  138. "Se the parent of the provided greenlet and switch to it."},
  139. {"test_new_greenlet",
  140. (PyCFunction)test_new_greenlet,
  141. METH_O,
  142. "Test PyGreenlet_New()"},
  143. {"test_raise_dead_greenlet",
  144. (PyCFunction)test_raise_dead_greenlet,
  145. METH_NOARGS,
  146. "Just raise greenlet.GreenletExit"},
  147. {"test_raise_greenlet_error",
  148. (PyCFunction)test_raise_greenlet_error,
  149. METH_NOARGS,
  150. "Just raise greenlet.error"},
  151. {"test_throw",
  152. (PyCFunction)test_throw,
  153. METH_O,
  154. "Throw a ValueError at the provided greenlet"},
  155. {NULL, NULL, 0, NULL}};
  156. #if PY_MAJOR_VERSION >= 3
  157. # define INITERROR return NULL
  158. static struct PyModuleDef moduledef = {PyModuleDef_HEAD_INIT,
  159. TEST_MODULE_NAME,
  160. NULL,
  161. 0,
  162. test_methods,
  163. NULL,
  164. NULL,
  165. NULL,
  166. NULL};
  167. PyMODINIT_FUNC
  168. PyInit__test_extension(void)
  169. #else
  170. # define INITERROR return
  171. PyMODINIT_FUNC
  172. init_test_extension(void)
  173. #endif
  174. {
  175. PyObject* module = NULL;
  176. #if PY_MAJOR_VERSION >= 3
  177. module = PyModule_Create(&moduledef);
  178. #else
  179. module = Py_InitModule(TEST_MODULE_NAME, test_methods);
  180. #endif
  181. if (module == NULL) {
  182. INITERROR;
  183. }
  184. PyGreenlet_Import();
  185. #if PY_MAJOR_VERSION >= 3
  186. return module;
  187. #endif
  188. }