From 8b10fe78a600c2ace69386dd497f3540a879eafe Mon Sep 17 00:00:00 2001 From: Vincent Castellano Date: Thu, 7 May 2015 00:20:13 -0700 Subject: [PATCH] Support loading the script from any path, truncate .py if needed. --- python-tg.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/python-tg.c b/python-tg.c index 5800620..3229043 100644 --- a/python-tg.c +++ b/python-tg.c @@ -28,7 +28,7 @@ #include #include - +#include #include #ifdef EVENT_V2 @@ -1271,18 +1271,26 @@ void inittgl() void py_init (const char *file) { if (!file) { return; } python_loaded = 0; - PyObject *pModule, *pDict; + PyObject *pModule, *pDict; + + // Get a copy of the filename for dirname, which may modify the string. + char filename[100]; + strncpy(filename, file, 100); + Py_Initialize(); inittgl(); - PyObject* sysPath = PySys_GetObject((char*)"path"); - PyList_Append(sysPath, PyString_FromString(".")); - - + PyList_Append(sysPath, PyString_FromString(dirname(filename))); + + // remove .py extension from file, if any + char* dot = strrchr(file, '.'); + if (dot && strcmp(dot, ".py") == 0) + *dot = 0; pModule = PyImport_Import(PyString_FromString(file)); - if(PyErr_Occurred()) { // Error loading script + + if(pModule == NULL || PyErr_Occurred()) { // Error loading script logprintf("Failed to load python script\n"); PyErr_Print(); exit(1);