From f7be8f7dfcbe4b24efa6876c67fcae5c649dc3b9 Mon Sep 17 00:00:00 2001 From: "M. Levinson" Date: Fri, 8 Dec 2006 23:29:00 +0100 Subject: [PATCH] Python: Don't complain if hooks.py does not exist. Nor if -no-home prevents it from being found. This patch is from bug 880, comment 5, attachment 305. http://bugzilla.elinks.cz/show_bug.cgi?id=880#c5 --- src/scripting/python/core.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/scripting/python/core.c b/src/scripting/python/core.c index 33bb5c82..44591225 100644 --- a/src/scripting/python/core.c +++ b/src/scripting/python/core.c @@ -121,6 +121,41 @@ end: return result; } +/* Determine whether or not a user-supplied hooks module exists. */ + +static int +hooks_module_exists(void) +{ + PyObject *imp_module = NULL, *result = NULL; + int found_hooks = 0; + + /* + * Use the find_module() function in Python's imp module to look for + * the hooks module in Python's search path. An ImportError exception + * indicates that no such module was found; any other exception will + * be reported as an error. + */ + imp_module = PyImport_ImportModule("imp"); + if (!imp_module) goto python_error; + + result = PyObject_CallMethod(imp_module, "find_module", "s", "hooks"); + if (result) { + found_hooks = 1; + goto end; + } else if (PyErr_ExceptionMatches(PyExc_ImportError)) { + PyErr_Clear(); + goto end; + } + +python_error: + alert_python_error(); + +end: + Py_XDECREF(imp_module); + Py_XDECREF(result); + return found_hooks; +} + /* Module-level documentation for the Python interpreter's elinks module. */ static char module_doc[] = @@ -169,6 +204,8 @@ init_python(struct module *module) Py_Initialize(); + if (!hooks_module_exists()) return; + elinks_module = Py_InitModule3("elinks", NULL, module_doc); if (!elinks_module) goto python_error; -- 2.11.4.GIT