PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/scripting/python/core.c

https://bitbucket.org/tifan/elinks
C | 280 lines | 190 code | 56 blank | 34 comment | 46 complexity | 8ec96411d06613d61ced931a068fffb9 MD5 | raw file
Possible License(s): GPL-2.0
  1. /* Python scripting engine */
  2. #ifdef HAVE_CONFIG_H
  3. #include "config.h"
  4. #endif
  5. #include <Python.h>
  6. #include <osdefs.h>
  7. #include <stdlib.h>
  8. #include "elinks.h"
  9. #include "config/home.h"
  10. #include "main/module.h"
  11. #include "scripting/python/core.h"
  12. #include "scripting/python/dialogs.h"
  13. #include "scripting/python/document.h"
  14. #include "scripting/python/keybinding.h"
  15. #include "scripting/python/load.h"
  16. #include "scripting/python/menu.h"
  17. #include "scripting/python/open.h"
  18. #include "scripting/python/python.h"
  19. #include "scripting/scripting.h"
  20. #include "session/session.h"
  21. #include "util/env.h"
  22. #include "util/string.h"
  23. struct session *python_ses = NULL;
  24. PyObject *python_elinks_err = NULL;
  25. PyObject *python_hooks = NULL;
  26. /* Error reporting. */
  27. void
  28. alert_python_error(void)
  29. {
  30. unsigned char *msg = "(no traceback available)";
  31. PyObject *err_type = NULL, *err_value = NULL, *err_traceback = NULL;
  32. PyObject *tb_module = NULL;
  33. PyObject *msg_list = NULL;
  34. PyObject *empty_string = NULL;
  35. PyObject *msg_string = NULL;
  36. unsigned char *temp;
  37. /*
  38. * Retrieve the current error indicator and use the format_exception()
  39. * function in Python's traceback module to produce an informative
  40. * error message. It returns a list of Python string objects.
  41. */
  42. PyErr_Fetch(&err_type, &err_value, &err_traceback);
  43. PyErr_NormalizeException(&err_type, &err_value, &err_traceback);
  44. if (!err_type) goto end;
  45. tb_module = PyImport_ImportModule("traceback");
  46. if (!tb_module) goto end;
  47. msg_list = PyObject_CallMethod(tb_module, "format_exception", "OOO",
  48. err_type,
  49. err_value ? err_value : Py_None,
  50. err_traceback ? err_traceback : Py_None);
  51. if (!msg_list) goto end;
  52. /*
  53. * Use the join() method of an empty Python string to join the list
  54. * of strings into one Python string containing the entire error
  55. * message. Then get the contents of the Python string.
  56. */
  57. empty_string = PyString_FromString("");
  58. if (!empty_string) goto end;
  59. msg_string = PyObject_CallMethod(empty_string, "join", "O", msg_list);
  60. if (!msg_string) goto end;
  61. temp = (unsigned char *) PyString_AsString(msg_string);
  62. if (temp) msg = temp;
  63. end:
  64. report_scripting_error(&python_scripting_module, python_ses, msg);
  65. Py_XDECREF(err_type);
  66. Py_XDECREF(err_value);
  67. Py_XDECREF(err_traceback);
  68. Py_XDECREF(tb_module);
  69. Py_XDECREF(msg_list);
  70. Py_XDECREF(empty_string);
  71. Py_XDECREF(msg_string);
  72. /* In case another error occurred while reporting the original error: */
  73. PyErr_Clear();
  74. }
  75. /* Prepend ELinks directories to Python's search path. */
  76. static int
  77. set_python_search_path(void)
  78. {
  79. struct string new_python_path;
  80. unsigned char *old_python_path;
  81. int result = -1;
  82. if (!init_string(&new_python_path)) return result;
  83. if (elinks_home && !add_format_to_string(&new_python_path, "%s%c",
  84. elinks_home, DELIM))
  85. goto end;
  86. if (!add_to_string(&new_python_path, CONFDIR))
  87. goto end;
  88. old_python_path = (unsigned char *) getenv("PYTHONPATH");
  89. if (old_python_path && !add_format_to_string(&new_python_path, "%c%s",
  90. DELIM, old_python_path))
  91. goto end;
  92. result = env_set("PYTHONPATH", new_python_path.source, -1);
  93. end:
  94. done_string(&new_python_path);
  95. return result;
  96. }
  97. /* Determine whether or not a user-supplied hooks module exists. */
  98. static int
  99. hooks_module_exists(void)
  100. {
  101. PyObject *imp_module = NULL, *result = NULL;
  102. int found_hooks = 0;
  103. /*
  104. * Use the find_module() function in Python's imp module to look for
  105. * the hooks module in Python's search path. An ImportError exception
  106. * indicates that no such module was found; any other exception will
  107. * be reported as an error.
  108. */
  109. imp_module = PyImport_ImportModule("imp");
  110. if (!imp_module) goto python_error;
  111. result = PyObject_CallMethod(imp_module, "find_module", "s", "hooks");
  112. if (result) {
  113. found_hooks = 1;
  114. goto end;
  115. } else if (PyErr_ExceptionMatches(PyExc_ImportError)) {
  116. PyErr_Clear();
  117. goto end;
  118. }
  119. python_error:
  120. alert_python_error();
  121. end:
  122. Py_XDECREF(imp_module);
  123. Py_XDECREF(result);
  124. return found_hooks;
  125. }
  126. /* Module-level documentation for the Python interpreter's elinks module. */
  127. static char module_doc[] =
  128. PYTHON_DOCSTRING("Interface to the ELinks web browser.\n\
  129. \n\
  130. Functions:\n\
  131. \n\
  132. bind_key() -- Bind a keystroke to a callable object.\n\
  133. current_document() -- Return the body of the document being viewed.\n\
  134. current_header() -- Return the header of the document being viewed.\n\
  135. current_link_url() -- Return the URL of the currently selected link.\n\
  136. current_title() -- Return the title of the document being viewed.\n\
  137. current_url() -- Return the URL of the document being viewed.\n\
  138. info_box() -- Display information to the user.\n\
  139. input_box() -- Prompt for user input.\n\
  140. load() -- Load a document into the ELinks cache.\n\
  141. menu() -- Display a menu.\n\
  142. open() -- View a document.\n\
  143. \n\
  144. Exception classes:\n\
  145. \n\
  146. error -- Errors internal to ELinks.\n\
  147. \n\
  148. Other public objects:\n\
  149. \n\
  150. home -- A string containing the pathname of the ~/.elinks directory, or\n\
  151. None if ELinks has no configuration directory.\n");
  152. void
  153. init_python(struct module *module)
  154. {
  155. PyObject *elinks_module, *module_dict, *module_name;
  156. if (set_python_search_path() != 0) return;
  157. /* Treat warnings as errors so they can be caught and handled;
  158. * otherwise they would be printed to stderr.
  159. *
  160. * NOTE: PySys_ResetWarnOptions() and PySys_AddWarnOption() have been
  161. * available and stable for many years but they're not officially
  162. * documented as part of Python's public API, so in theory these two
  163. * functions might no longer be available in some hypothetical future
  164. * version of Python. */
  165. PySys_ResetWarnOptions();
  166. PySys_AddWarnOption("error");
  167. Py_Initialize();
  168. if (!hooks_module_exists()) return;
  169. elinks_module = Py_InitModule3("elinks", NULL, module_doc);
  170. if (!elinks_module) goto python_error;
  171. /* If @elinks_home is NULL, Py_BuildValue() returns a None reference. */
  172. if (PyModule_AddObject(elinks_module, "home",
  173. Py_BuildValue("s", elinks_home)) != 0)
  174. goto python_error;
  175. python_elinks_err = PyErr_NewException("elinks.error", NULL, NULL);
  176. if (!python_elinks_err) goto python_error;
  177. if (PyModule_AddObject(elinks_module, "error", python_elinks_err) != 0)
  178. goto python_error;
  179. module_dict = PyModule_GetDict(elinks_module);
  180. if (!module_dict) goto python_error;
  181. module_name = PyString_FromString("elinks");
  182. if (!module_name) goto python_error;
  183. if (python_init_dialogs_interface(module_dict, module_name) != 0
  184. || python_init_document_interface(module_dict, module_name) != 0
  185. || python_init_keybinding_interface(module_dict, module_name) != 0
  186. || python_init_load_interface(module_dict, module_name) != 0
  187. || python_init_menu_interface(module_dict, module_name) != 0
  188. || python_init_open_interface(module_dict, module_name) != 0)
  189. goto python_error;
  190. python_hooks = PyImport_ImportModule("hooks");
  191. if (!python_hooks) goto python_error;
  192. return;
  193. python_error:
  194. alert_python_error();
  195. }
  196. void
  197. cleanup_python(struct module *module)
  198. {
  199. if (Py_IsInitialized()) {
  200. PyObject *temp;
  201. python_done_keybinding_interface();
  202. /* This is equivalent to Py_CLEAR(), but it works with older
  203. * versions of Python predating that macro: */
  204. temp = python_hooks;
  205. python_hooks = NULL;
  206. Py_XDECREF(temp);
  207. Py_Finalize();
  208. }
  209. }
  210. /* Add methods to a Python extension module. */
  211. int
  212. add_python_methods(PyObject *dict, PyObject *name, PyMethodDef *methods)
  213. {
  214. PyMethodDef *method;
  215. for (method = methods; method && method->ml_name; method++) {
  216. PyObject *function = PyCFunction_NewEx(method, NULL, name);
  217. int result;
  218. if (!function) return -1;
  219. result = PyDict_SetItemString(dict, method->ml_name, function);
  220. Py_DECREF(function);
  221. if (result != 0) return -1;
  222. }
  223. return 0;
  224. }