You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

354 lines
8.9 KiB

  1. /*
  2. * atexit - allow programmer to define multiple exit functions to be executed
  3. * upon normal program termination.
  4. *
  5. * Translated from atexit.py by Collin Winter.
  6. + Copyright 2007 Python Software Foundation.
  7. */
  8. #include "Python.h"
  9. /* Forward declaration (for atexit_cleanup) */
  10. static PyObject *atexit_clear(PyObject*, PyObject*);
  11. /* Forward declaration of module object */
  12. static struct PyModuleDef atexitmodule;
  13. /* ===================================================================== */
  14. /* Callback machinery. */
  15. typedef struct {
  16. PyObject *func;
  17. PyObject *args;
  18. PyObject *kwargs;
  19. } atexit_callback;
  20. typedef struct {
  21. atexit_callback **atexit_callbacks;
  22. int ncallbacks;
  23. int callback_len;
  24. } atexitmodule_state;
  25. #define GET_ATEXIT_STATE(mod) ((atexitmodule_state*)PyModule_GetState(mod))
  26. static void
  27. atexit_delete_cb(atexitmodule_state *modstate, int i)
  28. {
  29. atexit_callback *cb;
  30. cb = modstate->atexit_callbacks[i];
  31. modstate->atexit_callbacks[i] = NULL;
  32. Py_DECREF(cb->func);
  33. Py_DECREF(cb->args);
  34. Py_XDECREF(cb->kwargs);
  35. PyMem_Free(cb);
  36. }
  37. /* Clear all callbacks without calling them */
  38. static void
  39. atexit_cleanup(atexitmodule_state *modstate)
  40. {
  41. atexit_callback *cb;
  42. int i;
  43. for (i = 0; i < modstate->ncallbacks; i++) {
  44. cb = modstate->atexit_callbacks[i];
  45. if (cb == NULL)
  46. continue;
  47. atexit_delete_cb(modstate, i);
  48. }
  49. modstate->ncallbacks = 0;
  50. }
  51. /* Installed into pylifecycle.c's atexit mechanism */
  52. static void
  53. atexit_callfuncs(PyObject *module)
  54. {
  55. PyObject *exc_type = NULL, *exc_value, *exc_tb, *r;
  56. atexit_callback *cb;
  57. atexitmodule_state *modstate;
  58. int i;
  59. if (module == NULL)
  60. return;
  61. modstate = GET_ATEXIT_STATE(module);
  62. if (modstate->ncallbacks == 0)
  63. return;
  64. for (i = modstate->ncallbacks - 1; i >= 0; i--)
  65. {
  66. cb = modstate->atexit_callbacks[i];
  67. if (cb == NULL)
  68. continue;
  69. r = PyObject_Call(cb->func, cb->args, cb->kwargs);
  70. Py_XDECREF(r);
  71. if (r == NULL) {
  72. /* Maintain the last exception, but don't leak if there are
  73. multiple exceptions. */
  74. if (exc_type) {
  75. Py_DECREF(exc_type);
  76. Py_XDECREF(exc_value);
  77. Py_XDECREF(exc_tb);
  78. }
  79. PyErr_Fetch(&exc_type, &exc_value, &exc_tb);
  80. if (!PyErr_GivenExceptionMatches(exc_type, PyExc_SystemExit)) {
  81. PySys_WriteStderr("Error in atexit._run_exitfuncs:\n");
  82. PyErr_NormalizeException(&exc_type, &exc_value, &exc_tb);
  83. PyErr_Display(exc_type, exc_value, exc_tb);
  84. }
  85. }
  86. }
  87. atexit_cleanup(modstate);
  88. if (exc_type)
  89. PyErr_Restore(exc_type, exc_value, exc_tb);
  90. }
  91. /* ===================================================================== */
  92. /* Module methods. */
  93. PyDoc_STRVAR(atexit_register__doc__,
  94. "register(func, *args, **kwargs) -> func\n\
  95. \n\
  96. Register a function to be executed upon normal program termination\n\
  97. \n\
  98. func - function to be called at exit\n\
  99. args - optional arguments to pass to func\n\
  100. kwargs - optional keyword arguments to pass to func\n\
  101. \n\
  102. func is returned to facilitate usage as a decorator.");
  103. static PyObject *
  104. atexit_register(PyObject *self, PyObject *args, PyObject *kwargs)
  105. {
  106. atexitmodule_state *modstate;
  107. atexit_callback *new_callback;
  108. PyObject *func = NULL;
  109. modstate = GET_ATEXIT_STATE(self);
  110. if (modstate->ncallbacks >= modstate->callback_len) {
  111. atexit_callback **r;
  112. modstate->callback_len += 16;
  113. r = (atexit_callback**)PyMem_Realloc(modstate->atexit_callbacks,
  114. sizeof(atexit_callback*) * modstate->callback_len);
  115. if (r == NULL)
  116. return PyErr_NoMemory();
  117. modstate->atexit_callbacks = r;
  118. }
  119. if (PyTuple_GET_SIZE(args) == 0) {
  120. PyErr_SetString(PyExc_TypeError,
  121. "register() takes at least 1 argument (0 given)");
  122. return NULL;
  123. }
  124. func = PyTuple_GET_ITEM(args, 0);
  125. if (!PyCallable_Check(func)) {
  126. PyErr_SetString(PyExc_TypeError,
  127. "the first argument must be callable");
  128. return NULL;
  129. }
  130. new_callback = PyMem_Malloc(sizeof(atexit_callback));
  131. if (new_callback == NULL)
  132. return PyErr_NoMemory();
  133. new_callback->args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
  134. if (new_callback->args == NULL) {
  135. PyMem_Free(new_callback);
  136. return NULL;
  137. }
  138. new_callback->func = func;
  139. new_callback->kwargs = kwargs;
  140. Py_INCREF(func);
  141. Py_XINCREF(kwargs);
  142. modstate->atexit_callbacks[modstate->ncallbacks++] = new_callback;
  143. Py_INCREF(func);
  144. return func;
  145. }
  146. PyDoc_STRVAR(atexit_run_exitfuncs__doc__,
  147. "_run_exitfuncs() -> None\n\
  148. \n\
  149. Run all registered exit functions.");
  150. static PyObject *
  151. atexit_run_exitfuncs(PyObject *self, PyObject *unused)
  152. {
  153. atexit_callfuncs(self);
  154. if (PyErr_Occurred())
  155. return NULL;
  156. Py_RETURN_NONE;
  157. }
  158. PyDoc_STRVAR(atexit_clear__doc__,
  159. "_clear() -> None\n\
  160. \n\
  161. Clear the list of previously registered exit functions.");
  162. static PyObject *
  163. atexit_clear(PyObject *self, PyObject *unused)
  164. {
  165. atexit_cleanup(GET_ATEXIT_STATE(self));
  166. Py_RETURN_NONE;
  167. }
  168. PyDoc_STRVAR(atexit_ncallbacks__doc__,
  169. "_ncallbacks() -> int\n\
  170. \n\
  171. Return the number of registered exit functions.");
  172. static PyObject *
  173. atexit_ncallbacks(PyObject *self, PyObject *unused)
  174. {
  175. atexitmodule_state *modstate;
  176. modstate = GET_ATEXIT_STATE(self);
  177. return PyLong_FromSsize_t(modstate->ncallbacks);
  178. }
  179. static int
  180. atexit_m_traverse(PyObject *self, visitproc visit, void *arg)
  181. {
  182. int i;
  183. atexitmodule_state *modstate;
  184. modstate = GET_ATEXIT_STATE(self);
  185. if (modstate != NULL) {
  186. for (i = 0; i < modstate->ncallbacks; i++) {
  187. atexit_callback *cb = modstate->atexit_callbacks[i];
  188. if (cb == NULL)
  189. continue;
  190. Py_VISIT(cb->func);
  191. Py_VISIT(cb->args);
  192. Py_VISIT(cb->kwargs);
  193. }
  194. }
  195. return 0;
  196. }
  197. static int
  198. atexit_m_clear(PyObject *self)
  199. {
  200. atexitmodule_state *modstate;
  201. modstate = GET_ATEXIT_STATE(self);
  202. if (modstate != NULL) {
  203. atexit_cleanup(modstate);
  204. }
  205. return 0;
  206. }
  207. static void
  208. atexit_free(PyObject *m)
  209. {
  210. atexitmodule_state *modstate;
  211. modstate = GET_ATEXIT_STATE(m);
  212. if (modstate != NULL) {
  213. atexit_cleanup(modstate);
  214. PyMem_Free(modstate->atexit_callbacks);
  215. }
  216. }
  217. PyDoc_STRVAR(atexit_unregister__doc__,
  218. "unregister(func) -> None\n\
  219. \n\
  220. Unregister an exit function which was previously registered using\n\
  221. atexit.register\n\
  222. \n\
  223. func - function to be unregistered");
  224. static PyObject *
  225. atexit_unregister(PyObject *self, PyObject *func)
  226. {
  227. atexitmodule_state *modstate;
  228. atexit_callback *cb;
  229. int i, eq;
  230. modstate = GET_ATEXIT_STATE(self);
  231. for (i = 0; i < modstate->ncallbacks; i++)
  232. {
  233. cb = modstate->atexit_callbacks[i];
  234. if (cb == NULL)
  235. continue;
  236. eq = PyObject_RichCompareBool(cb->func, func, Py_EQ);
  237. if (eq < 0)
  238. return NULL;
  239. if (eq)
  240. atexit_delete_cb(modstate, i);
  241. }
  242. Py_RETURN_NONE;
  243. }
  244. static PyMethodDef atexit_methods[] = {
  245. {"register", (PyCFunction) atexit_register, METH_VARARGS|METH_KEYWORDS,
  246. atexit_register__doc__},
  247. {"_clear", (PyCFunction) atexit_clear, METH_NOARGS,
  248. atexit_clear__doc__},
  249. {"unregister", (PyCFunction) atexit_unregister, METH_O,
  250. atexit_unregister__doc__},
  251. {"_run_exitfuncs", (PyCFunction) atexit_run_exitfuncs, METH_NOARGS,
  252. atexit_run_exitfuncs__doc__},
  253. {"_ncallbacks", (PyCFunction) atexit_ncallbacks, METH_NOARGS,
  254. atexit_ncallbacks__doc__},
  255. {NULL, NULL} /* sentinel */
  256. };
  257. /* ===================================================================== */
  258. /* Initialization function. */
  259. PyDoc_STRVAR(atexit__doc__,
  260. "allow programmer to define multiple exit functions to be executed\
  261. upon normal program termination.\n\
  262. \n\
  263. Two public functions, register and unregister, are defined.\n\
  264. ");
  265. static int
  266. atexit_exec(PyObject *m) {
  267. atexitmodule_state *modstate;
  268. modstate = GET_ATEXIT_STATE(m);
  269. modstate->callback_len = 32;
  270. modstate->ncallbacks = 0;
  271. modstate->atexit_callbacks = PyMem_New(atexit_callback*,
  272. modstate->callback_len);
  273. if (modstate->atexit_callbacks == NULL)
  274. return -1;
  275. _Py_PyAtExit(atexit_callfuncs, m);
  276. return 0;
  277. }
  278. static PyModuleDef_Slot atexit_slots[] = {
  279. {Py_mod_exec, atexit_exec},
  280. {0, NULL}
  281. };
  282. static struct PyModuleDef atexitmodule = {
  283. PyModuleDef_HEAD_INIT,
  284. "atexit",
  285. atexit__doc__,
  286. sizeof(atexitmodule_state),
  287. atexit_methods,
  288. atexit_slots,
  289. atexit_m_traverse,
  290. atexit_m_clear,
  291. (freefunc)atexit_free
  292. };
  293. PyMODINIT_FUNC
  294. PyInit_atexit(void)
  295. {
  296. return PyModuleDef_Init(&atexitmodule);
  297. }