From 5ba09ddb937eef2a5d09387f303d11c88df11069 Mon Sep 17 00:00:00 2001 From: Andy Kittner Date: Sat, 29 Jun 2013 14:31:11 +0200 Subject: [PATCH] Implement #791: dynamically load python27.dll from DLLs/ directory --- extras/win32-portable/openslides.c | 125 +++++++++++++++++++--- extras/win32-portable/openslides.exe | Bin 31232 -> 31744 bytes extras/win32-portable/prepare_portable.py | 7 +- 3 files changed, 112 insertions(+), 20 deletions(-) diff --git a/extras/win32-portable/openslides.c b/extras/win32-portable/openslides.c index 7e6abdcc4..e69d9824b 100644 --- a/extras/win32-portable/openslides.c +++ b/extras/win32-portable/openslides.c @@ -12,6 +12,17 @@ #include +#define PYTHON_DLL_PATH "\\Dlls\\python27.dll" + +static void (*py_initialize)(void) = 0; +static void (*py_finalize)(void) = 0; +static void (*py_set_program_name)(char *) = 0; +static void (*py_set_python_home)(char *) = 0; +static int (*py_run_simple_string_flags)(const char *, PyCompilerFlags *) = 0; +static void (*py_sys_set_argv_ex)(int, char **, int) = 0; +static int (*py_main)(int, char **) = 0; +static int *py_ignore_environment_flag = 0; + static const char *run_openslides_code = "import openslides_gui.gui;" "openslides_gui.gui.main()"; @@ -71,14 +82,94 @@ _get_module_name() return NULL; } +static void +_fatal_error(const char *text) +{ + MessageBoxA(NULL, text, "Fatal error", MB_OK | MB_ICONERROR); + exit(1); +} + +static void +_fatal_error_fmt(const char *fmt, ...) +{ + int size = 512; + char *buf = malloc(size); + va_list args; + int bytes_written; + + if (!buf) + abort(); + + va_start(args, fmt); + for (;;) + { + bytes_written = vsnprintf(buf, size, fmt, args); + if (bytes_written > -1 && bytes_written < size) + break; + else if (bytes_written > size) + size = bytes_written + 1; + else + size *= 2; + + buf = realloc(buf, size); + if (!buf) + abort(); + } + va_end(args); + + _fatal_error(buf); +} + +static void * +_load_func(HMODULE module, const char *name) +{ + void *address = GetProcAddress(module, name); + if (!address) + _fatal_error_fmt("Failed to look up symbol %s", name); + return address; +} + +static void +_load_python(const char *pyhome) +{ + size_t pyhome_len = strlen(pyhome); + size_t size = pyhome_len + strlen(PYTHON_DLL_PATH) + 1; + char *buf = malloc(size); + HMODULE py_dll; + + if (!buf) + abort(); + memcpy(buf, pyhome, pyhome_len); + memcpy(buf + pyhome_len, PYTHON_DLL_PATH, sizeof(PYTHON_DLL_PATH)); + buf[size - 1] = '\0'; + + py_dll = LoadLibrary(buf); + if (!py_dll) + { + DWORD error = GetLastError(); + _fatal_error_fmt("Failed to load %s (error %d)", buf, error); + } + + py_initialize = (void (*)(void))_load_func(py_dll, "Py_Initialize"); + py_finalize = (void (*)(void))_load_func(py_dll, "Py_Finalize"); + py_set_program_name = (void (*)(char *)) + _load_func(py_dll, "Py_SetProgramName"); + py_set_python_home = (void (*)(char *)) + _load_func(py_dll, "Py_SetPythonHome"); + py_run_simple_string_flags = (int (*)(const char *, PyCompilerFlags *)) + _load_func(py_dll, "PyRun_SimpleStringFlags"); + py_sys_set_argv_ex = (void (*)(int, char **, int)) + _load_func(py_dll, "PySys_SetArgvEx"); + py_main = (int (*)(int, char **))_load_func(py_dll, "Py_Main"); + py_ignore_environment_flag = (int *) + _load_func(py_dll, "Py_IgnoreEnvironmentFlag"); +} + static int _run() { - if (PyRun_SimpleString(run_openslides_code) != 0) - { - fprintf(stderr, "ERROR: failed to execute openslides\n"); - return 1; - } + if (py_run_simple_string_flags(run_openslides_code, NULL) != 0) + _fatal_error("Failed to execute openslides"); return 0; } @@ -91,33 +182,33 @@ WinMain(HINSTANCE inst, HINSTANCE prev_inst, LPSTR cmdline, int show) int run_py_main = __argc > 1; char *py_home, *sep = NULL; - Py_SetProgramName(__argv[0]); - py_home = _get_module_name(); + if (!py_home) + _fatal_error("Could not determine portable root directory"); - if (py_home) - sep = strrchr(py_home, '\\'); + sep = strrchr(py_home, '\\'); /* should always be the true */ if (sep) - { *sep = '\0'; - Py_SetPythonHome(py_home); - Py_IgnoreEnvironmentFlag = 1; - } + + _load_python(py_home); + py_set_program_name(__argv[0]); + py_set_python_home(py_home); + *py_ignore_environment_flag = 1; if (run_py_main) { /* we where given extra arguments, behave like python.exe */ - returncode = Py_Main(__argc, __argv); + returncode = py_main(__argc, __argv); } else { /* no arguments given => start openslides gui */ - Py_Initialize(); - PySys_SetArgvEx(__argc, __argv, 0); + py_initialize(); + py_sys_set_argv_ex(__argc, __argv, 0); returncode = _run(); - Py_Finalize(); + py_finalize(); } free(py_home); diff --git a/extras/win32-portable/openslides.exe b/extras/win32-portable/openslides.exe index 812eabdc0cf2064e4ac60720fa1031ec9fdd55fe..7d1bd450fe74ab330c2696a45f2a87f949bfcba8 100755 GIT binary patch delta 3692 zcmd^Ck53fY9e=Y7vdW@>JGz2;%L!L&5z!T^r_fbd^-erdaFGLnl^vPgF1vJg*Mhm; z>a8^599MOWZBpBFv)9HZrZMHNF^y@r2LbO&jmc4KTGAt_ZN^|)dc9a|xto5zvvAnI zp<_Pp``-8a`Tls{H*Z`{f6ASn<}U2urY=_QKDy$!H$ORc<4=h>)Q=rVv|?R9wm;Eq z$XAW^DgTcWM-AS`39q3)me_B|muGng4SCqtE>WzDMYeUdF2-s&p1JScJ;Y|836{ZS za|;<;423_f+__xM=gLfN9(5PD$z*F$&zN3RZ*o>tSX8J^aON$SEnwyXI}dSDOm9G-qO34w!%f;F-tCSgK48%k9wFS};Jt+JKb7sfGj>q6SiI_Kukl_DV4`oYz}@N>E_3OPFa)E#j*{E~`?YYKX0 z8aaFQ=z^Fo4AFYk9U9kDp_3W?FEOR2*xFxRY^y!wZqPr959P+E_1F3B!>`%555Jz- z-^ERuGM>QnJBnmRq`T=rlf%l07SeScl{L*}hjgR#tq9(mie|f|)2LPRfi)PLu`0gYsGbK@J6+&)R!76@B`Zr~R!<>t>5a2l?J7(FN&L4M*JC6%%8LtBBUeS1CEy zXi`(fE5bW8tFJOAuG6Ht%5p-w^tQ++E*uxTIp58|i1Zjdy$vYcySZO;c()Kvn9~<`3c{?d! zk0C=l5g1Gj$$RqzQltayCcD+JkImajk+!cPd#Pc6FmESC+MYr79fo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