diff --git a/src/main.c b/src/main.c index 6ac8ec908..a859b794d 100644 --- a/src/main.c +++ b/src/main.c @@ -95,6 +95,7 @@ extern sigjmp_buf alarmret; time_t now; static int argc; static char **argv; +char *argv0; /* * Please use the PATCH macro instead of directly altering the version @@ -1034,6 +1035,7 @@ int main(int arg_c, char **arg_v) argc = arg_c; argv = arg_v; + argv0 = argv[0]; /* Version info! */ #ifdef EGG_PATCH diff --git a/src/mod/module.h b/src/mod/module.h index 9b674577b..a7108218f 100644 --- a/src/mod/module.h +++ b/src/mod/module.h @@ -531,6 +531,7 @@ typedef void (*chanout_butfunc)(int, int, const char *, ...) ATTRIBUTE_FORMAT(pr /* 320 - 323 */ #define bind_bind_entry ((int(*)(tcl_bind_list_t *, const char *, const char *, const char *))global[320]) #define unbind_bind_entry ((int(*)(tcl_bind_list_t *, const char *, const char *, const char *))global[321]) +#define argv0 ((char *)global[322]) diff --git a/src/mod/python.mod/pycmds.c b/src/mod/python.mod/pycmds.c index 8626785df..2855ed0a3 100644 --- a/src/mod/python.mod/pycmds.c +++ b/src/mod/python.mod/pycmds.c @@ -79,18 +79,20 @@ static void cmd_python(struct userrec *u, int idx, char *par) { if (pyfunc && PyCallable_Check(pyfunc)) { pyval = PyObject_CallFunctionObjArgs(pyfunc, ptype, pvalue, ptraceback, NULL); // Check if traceback is a list and handle as such - if (PyList_Check(pyval)) { - n = PyList_Size(pyval); - for (i = 0; i < n; i++) { - item = PyList_GetItem(pyval, i); - pystr = PyObject_Str(item); + if (pyval) { + if (pyval && PyList_Check(pyval)) { + n = PyList_Size(pyval); + for (i = 0; i < n; i++) { + item = PyList_GetItem(pyval, i); + pystr = PyObject_Str(item); + dprintf(idx, "%s", PyUnicode_AsUTF8(pystr)); + } + } else { + pystr = PyObject_Str(pyval); dprintf(idx, "%s", PyUnicode_AsUTF8(pystr)); } - } else { - pystr = PyObject_Str(pyval); - dprintf(idx, "%s", PyUnicode_AsUTF8(pystr)); + Py_DECREF(pyval); } - Py_DECREF(pyval); } } return; diff --git a/src/mod/python.mod/python.c b/src/mod/python.mod/python.c index e68197734..b33e484dc 100644 --- a/src/mod/python.mod/python.c +++ b/src/mod/python.mod/python.c @@ -66,18 +66,37 @@ static int python_expmem() static void init_python() { PyObject *pmodule; - wchar_t *program = Py_DecodeLocale("eggdrop", NULL); + PyStatus status; + PyConfig config; + wchar_t *program; - if (program == NULL) { - fprintf(stderr, "Python: Fatal error: This should never happen\n"); + if (PY_VERSION_HEX < 0x0308) { + fprintf(stderr, "Python: Python version %d is lower than 3.8, not loading Python module", PY_VERSION_HEX); + return; + } + PyConfig_InitPythonConfig(&config); + program = Py_DecodeLocale(argv0, NULL); + if (!program) { + PyConfig_Clear(&config); + fprintf(stderr, "Python: Fatal error: This should never happen\n"); + fatal(1); + } + status = PyConfig_SetString(&config, &config.program_name, program); + if (PyStatus_Exception(status)) { + PyConfig_Clear(&config); + fprintf(stderr, "Python: Fatal error: Could not set program base path\n"); fatal(1); } - Py_SetProgramName(program); /* optional but recommended */ if (PyImport_AppendInittab("eggdrop", &PyInit_eggdrop) == -1) { fprintf(stderr, "Error: could not extend in-built modules table\n"); exit(1); } - Py_Initialize(); + status = Py_InitializeFromConfig(&config); + if (PyStatus_Exception(status)) { + PyConfig_Clear(&config); + fprintf(stderr, "Python: Fatal error: Could not initialize config\n"); + fatal(1); + } PyDateTime_IMPORT; pmodule = PyImport_ImportModule("eggdrop"); if (!pmodule) { diff --git a/src/mod/python.mod/tclpython.c b/src/mod/python.mod/tclpython.c index 7abdeb998..6218bfe3b 100644 --- a/src/mod/python.mod/tclpython.c +++ b/src/mod/python.mod/tclpython.c @@ -29,7 +29,8 @@ static int tcl_pysource STDVAR PyObject *pobj, *pstr, *ptype, *pvalue, *ptraceback; PyObject *pystr, *module_name, *pymodule, *pyfunc, *pyval, *item; Py_ssize_t n; - char *res = NULL; + const char *res = NULL; + char *res2; int i; if (!(fp = fopen(argv[1], "r"))) { @@ -60,11 +61,17 @@ static int tcl_pysource STDVAR for (i = 0; i < n; i++) { item = PyList_GetItem(pyval, i); pystr = PyObject_Str(item); + //Python returns a const char but we need to remove the \n res = PyUnicode_AsUTF8(pystr); if (res[strlen(res) - 1]) { - res[strlen(res) - 1] = '\0'; + res2 = (char*) nmalloc(strlen(res)+1); + strncpy(res2, res, strlen(res)); + res2[strlen(res) - 1] = '\0'; } putlog(LOG_MISC, "*", "%s", res); + if (res2) { + nfree(res2); + } } } else { pystr = PyObject_Str(pyval); diff --git a/src/modules.c b/src/modules.c index 5fe8b0543..754ac9f5d 100644 --- a/src/modules.c +++ b/src/modules.c @@ -98,6 +98,8 @@ extern time_t now, online_since; extern tand_t *tandbot; extern Tcl_Interp *interp; extern sock_list *socklist; +extern char argv0; + int xtra_kill(); int xtra_unpack(); @@ -629,7 +631,8 @@ Function global_table[] = { (Function) check_tcl_event_arg, /* 320 - 323 */ (Function) bind_bind_entry, - (Function) unbind_bind_entry + (Function) unbind_bind_entry, + (Function) & argv0 }; void init_modules(void)