Skip to content

Commit

Permalink
Update config API; check minimum python
Browse files Browse the repository at this point in the history
  • Loading branch information
vanosg committed Dec 20, 2023
1 parent c5499d2 commit a8f2c9c
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 17 deletions.
2 changes: 2 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/mod/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -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])



Expand Down
20 changes: 11 additions & 9 deletions src/mod/python.mod/pycmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
29 changes: 24 additions & 5 deletions src/mod/python.mod/python.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 9 additions & 2 deletions src/mod/python.mod/tclpython.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"))) {
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion src/modules.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit a8f2c9c

Please sign in to comment.