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.

346 lines
8.7 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 pythonrun.c's atexit mechanism */
  52. static void
  53. atexit_callfuncs(void)
  54. {
  55. PyObject *exc_type = NULL, *exc_value, *exc_tb, *r;
  56. atexit_callback *cb;
  57. PyObject *module;
  58. atexitmodule_state *modstate;
  59. int i;
  60. module = PyState_FindModule(&atexitmodule);
  61. if (module == NULL)
  62. return;
  63. modstate = GET_ATEXIT_STATE(module);
  64. if (modstate->ncallbacks == 0)
  65. return;
  66. for (i = modstate->ncallbacks - 1; i >= 0; i--)
  67. {
  68. cb = modstate->atexit_callbacks[i];
  69. if (cb == NULL)
  70. continue;
  71. r = PyObject_Call(cb->func, cb->args, cb->kwargs);
  72. Py_XDECREF(r);
  73. if (r == NULL) {
  74. /* Maintain the last exception, but don't leak if there are
  75. multiple exceptions. */
  76. if (exc_type) {
  77. Py_DECREF(exc_type);
  78. Py_XDECREF(exc_value);
  79. Py_XDECREF(exc_tb);
  80. }
  81. PyErr_Fetch(&exc_type, &exc_value, &exc_tb);
  82. if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
  83. PySys_WriteStderr("Error in atexit._run_exitfuncs:\n");
  84. PyErr_NormalizeException(&exc_type, &exc_value, &exc_tb);
  85. PyErr_Display(exc_type, exc_value, exc_tb);
  86. }
  87. }
  88. }
  89. atexit_cleanup(modstate);
  90. if (exc_type)
  91. PyErr_Restore(exc_type, exc_value, exc_tb);
  92. }
  93. /* ===================================================================== */
  94. /* Module methods. */
  95. PyDoc_STRVAR(atexit_register__doc__,
  96. "register(func, *args, **kwargs) -> func\n\
  97. \n\
  98. Register a function to be executed upon normal program termination\n\
  99. \n\
  100. func - function to be called at exit\n\
  101. args - optional arguments to pass to func\n\
  102. kwargs - optional keyword arguments to pass to func\n\
  103. \n\
  104. func is returned to facilitate usage as a decorator.");
  105. static PyObject *
  106. atexit_register(PyObject *self, PyObject *args, PyObject *kwargs)
  107. {
  108. atexitmodule_state *modstate;
  109. atexit_callback *new_callback;
  110. PyObject *func = NULL;
  111. modstate = GET_ATEXIT_STATE(self);
  112. if (modstate->ncallbacks >= modstate->callback_len) {
  113. atexit_callback **r;
  114. modstate->callback_len += 16;
  115. r = (atexit_callback**)PyMem_Realloc(modstate->atexit_callbacks,
  116. sizeof(atexit_callback*) * modstate->callback_len);
  117. if (r == NULL)
  118. return PyErr_NoMemory();
  119. modstate->atexit_callbacks = r;
  120. }
  121. if (PyTuple_GET_SIZE(args) == 0) {
  122. PyErr_SetString(PyExc_TypeError,
  123. "register() takes at least 1 argument (0 given)");
  124. return NULL;
  125. }
  126. func = PyTuple_GET_ITEM(args, 0);
  127. if (!PyCallable_Check(func)) {
  128. PyErr_SetString(PyExc_TypeError,
  129. "the first argument must be callable");
  130. return NULL;
  131. }
  132. new_callback = PyMem_Malloc(sizeof(atexit_callback));
  133. if (new_callback == NULL)
  134. return PyErr_NoMemory();
  135. new_callback->args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
  136. if (new_callback->args == NULL) {
  137. PyMem_Free(new_callback);
  138. return NULL;
  139. }
  140. new_callback->func = func;
  141. new_callback->kwargs = kwargs;
  142. Py_INCREF(func);
  143. Py_XINCREF(kwargs);
  144. modstate->atexit_callbacks[modstate->ncallbacks++] = new_callback;
  145. Py_INCREF(func);
  146. return func;
  147. }
  148. PyDoc_STRVAR(atexit_run_exitfuncs__doc__,
  149. "_run_exitfuncs() -> None\n\
  150. \n\
  151. Run all registered exit functions.");
  152. static PyObject *
  153. atexit_run_exitfuncs(PyObject *self, PyObject *unused)
  154. {
  155. atexit_callfuncs();
  156. if (PyErr_Occurred())
  157. return NULL;
  158. Py_RETURN_NONE;
  159. }
  160. PyDoc_STRVAR(atexit_clear__doc__,
  161. "_clear() -> None\n\
  162. \n\
  163. Clear the list of previously registered exit functions.");
  164. static PyObject *
  165. atexit_clear(PyObject *self, PyObject *unused)
  166. {
  167. atexit_cleanup(GET_ATEXIT_STATE(self));
  168. Py_RETURN_NONE;
  169. }
  170. PyDoc_STRVAR(atexit_ncallbacks__doc__,
  171. "_ncallbacks() -> int\n\
  172. \n\
  173. Return the number of registered exit functions.");
  174. static PyObject *
  175. atexit_ncallbacks(PyObject *self, PyObject *unused)
  176. {
  177. atexitmodule_state *modstate;
  178. modstate = GET_ATEXIT_STATE(self);
  179. return PyLong_FromSsize_t(modstate->ncallbacks);
  180. }
  181. static int
  182. atexit_m_traverse(PyObject *self, visitproc visit, void *arg)
  183. {
  184. int i;
  185. atexitmodule_state *modstate;
  186. modstate = GET_ATEXIT_STATE(self);
  187. for (i = 0; i < modstate->ncallbacks; i++) {
  188. atexit_callback *cb = modstate->atexit_callbacks[i];
  189. if (cb == NULL)
  190. continue;
  191. Py_VISIT(cb->func);
  192. Py_VISIT(cb->args);
  193. Py_VISIT(cb->kwargs);
  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. atexit_cleanup(modstate);
  203. return 0;
  204. }
  205. static void
  206. atexit_free(PyObject *m)
  207. {
  208. atexitmodule_state *modstate;
  209. modstate = GET_ATEXIT_STATE(m);
  210. atexit_cleanup(modstate);
  211. PyMem_Free(modstate->atexit_callbacks);
  212. }
  213. PyDoc_STRVAR(atexit_unregister__doc__,
  214. "unregister(func) -> None\n\
  215. \n\
  216. Unregister a exit function which was previously registered using\n\
  217. atexit.register\n\
  218. \n\
  219. func - function to be unregistered");
  220. static PyObject *
  221. atexit_unregister(PyObject *self, PyObject *func)
  222. {
  223. atexitmodule_state *modstate;
  224. atexit_callback *cb;
  225. int i, eq;
  226. modstate = GET_ATEXIT_STATE(self);
  227. for (i = 0; i < modstate->ncallbacks; i++)
  228. {
  229. cb = modstate->atexit_callbacks[i];
  230. if (cb == NULL)
  231. continue;
  232. eq = PyObject_RichCompareBool(cb->func, func, Py_EQ);
  233. if (eq < 0)
  234. return NULL;
  235. if (eq)
  236. atexit_delete_cb(modstate, i);
  237. }
  238. Py_RETURN_NONE;
  239. }
  240. static PyMethodDef atexit_methods[] = {
  241. {"register", (PyCFunction) atexit_register, METH_VARARGS|METH_KEYWORDS,
  242. atexit_register__doc__},
  243. {"_clear", (PyCFunction) atexit_clear, METH_NOARGS,
  244. atexit_clear__doc__},
  245. {"unregister", (PyCFunction) atexit_unregister, METH_O,
  246. atexit_unregister__doc__},
  247. {"_run_exitfuncs", (PyCFunction) atexit_run_exitfuncs, METH_NOARGS,
  248. atexit_run_exitfuncs__doc__},
  249. {"_ncallbacks", (PyCFunction) atexit_ncallbacks, METH_NOARGS,
  250. atexit_ncallbacks__doc__},
  251. {NULL, NULL} /* sentinel */
  252. };
  253. /* ===================================================================== */
  254. /* Initialization function. */
  255. PyDoc_STRVAR(atexit__doc__,
  256. "allow programmer to define multiple exit functions to be executed\
  257. upon normal program termination.\n\
  258. \n\
  259. Two public functions, register and unregister, are defined.\n\
  260. ");
  261. static struct PyModuleDef atexitmodule = {
  262. PyModuleDef_HEAD_INIT,
  263. "atexit",
  264. atexit__doc__,
  265. sizeof(atexitmodule_state),
  266. atexit_methods,
  267. NULL,
  268. atexit_m_traverse,
  269. atexit_m_clear,
  270. (freefunc)atexit_free
  271. };
  272. PyMODINIT_FUNC
  273. PyInit_atexit(void)
  274. {
  275. PyObject *m;
  276. atexitmodule_state *modstate;
  277. m = PyModule_Create(&atexitmodule);
  278. if (m == NULL)
  279. return NULL;
  280. modstate = GET_ATEXIT_STATE(m);
  281. modstate->callback_len = 32;
  282. modstate->ncallbacks = 0;
  283. modstate->atexit_callbacks = PyMem_New(atexit_callback*,
  284. modstate->callback_len);
  285. if (modstate->atexit_callbacks == NULL)
  286. return NULL;
  287. _Py_PyAtExit(atexit_callfuncs);
  288. return m;
  289. }