From 465857c2e9a058b15967c32b28ac75b1baa3d427 Mon Sep 17 00:00:00 2001 From: robo9k Date: Sun, 1 Jun 2014 16:52:39 +0200 Subject: [PATCH] Configure entry point via environment variables In preparation for issue #11 the module and method name for the dllEntry() Python entry point are now read from environment variables instead of being hardcoded. --- include/q3py.h | 13 +++++++++++++ src/q3py.c | 28 ++++++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/include/q3py.h b/include/q3py.h index c650c7d..766a8e8 100644 --- a/include/q3py.h +++ b/include/q3py.h @@ -106,6 +106,19 @@ intptr_t q3py_syscall(intptr_t number, ...); #define Q3PY_API_pointers 1 +/* TODO: Both of these need to be configurable per module, see issue #10 */ +/** + * The name of the environment variable used to set the module + * for the dllMain entry point. + */ +#define Q3PY_ENV_ENTRYPOINT_MODULE "Q3PY_MODULE" +/** + * The name of the environment variable used to set the method + * for the dllMain entry point. + */ +#define Q3PY_ENV_ENTRYPOINT_METHOD "Q3PY_METHOD" + + /** * If this header is included while building another Python * extension module, expose q3py's C API via a capsule. diff --git a/src/q3py.c b/src/q3py.c index 844c263..3e25c3e 100644 --- a/src/q3py.c +++ b/src/q3py.c @@ -194,6 +194,7 @@ Q3_API intptr_t vmMain(int command, int arg0, int arg1, int arg2, return -1; } + /** * Initializes the embedded Python. * @@ -210,8 +211,31 @@ static void init_python() { Py_Initialize(); - /* TODO: Those should be configureable (e.g. like setuptools entry points) */ - const char *funcname = "dllentry", *modname = "q3py_hello"; + + /* + * NOTE: Having one env var (e.g. "Q3PY_ENTRYPOINT") with value + * "module:method" might be easier for the user to configure, but + * C's string handling is meh. + */ + char *modname = getenv(Q3PY_ENV_ENTRYPOINT_MODULE); + if (NULL == modname) { + q3py_error("Entry point module (" Q3PY_ENV_ENTRYPOINT_MODULE ") " + "is not set"); + q3py_exit(); + } + + char *funcname = getenv(Q3PY_ENV_ENTRYPOINT_METHOD); + if (NULL == modname) { + q3py_error("Entry point method (" Q3PY_ENV_ENTRYPOINT_METHOD ") " + "is not set"); + q3py_exit(); + } + + char entrypoint_buffer[128]; + snprintf(entrypoint_buffer, sizeof(entrypoint_buffer), + "Entry point is '%s:%s'", modname, funcname); + q3py_log(entrypoint_buffer); + /* See https://docs.python.org/3/extending/embedding.html#pure-embedding */