Fixed pymod initialization for Python 2

Python 2 is a bit more picky with respect to
the correct name of the module. If passing "db"
to a module called dbcore, it will exit with
a SystemError.
This commit is contained in:
Matthias Koefferlein 2018-10-22 23:28:55 +02:00
parent 994ea47146
commit 58239650aa
2 changed files with 11 additions and 8 deletions

View File

@ -27,10 +27,10 @@
// to force linking of the db module // to force linking of the db module
#include "../../db/db/dbForceLink.h" #include "../../db/db/dbForceLink.h"
static PyObject *db_module_init (const char *mod_name, const char *mod_description) static PyObject *db_module_init (const char *pymod_name, const char *mod_name, const char *mod_description)
{ {
db::init (); db::init ();
return module_init (mod_name, mod_description); return module_init (pymod_name, mod_name, mod_description);
} }
DEFINE_PYMOD_WITH_INIT(dbcore, "db", "KLayout core module 'db'", db_module_init) DEFINE_PYMOD_WITH_INIT(dbcore, "db", "KLayout core module 'db'", db_module_init)

View File

@ -39,7 +39,7 @@
#include "gsiExpression.h" #include "gsiExpression.h"
static PyObject * static PyObject *
module_init (const char *mod_name, const char *mod_description) module_init (const char *pymod_name, const char *mod_name, const char *mod_description)
{ {
static pya::PythonModule module; static pya::PythonModule module;
@ -50,7 +50,7 @@ module_init (const char *mod_name, const char *mod_description)
// required for the tiling processor for example // required for the tiling processor for example
gsi::initialize_expressions (); gsi::initialize_expressions ();
module.init (mod_name, mod_description); module.init (pymod_name, mod_description);
module.make_classes (mod_name); module.make_classes (mod_name);
return module.take_module (); return module.take_module ();
@ -60,6 +60,9 @@ module_init (const char *mod_name, const char *mod_description)
return 0; return 0;
} }
#define STRINGIFY(s) _STRINGIFY(s)
#define _STRINGIFY(s) #s
#if PY_MAJOR_VERSION < 3 #if PY_MAJOR_VERSION < 3
#define DEFINE_PYMOD(__name__, __name_str__, __description__) \ #define DEFINE_PYMOD(__name__, __name_str__, __description__) \
@ -67,7 +70,7 @@ module_init (const char *mod_name, const char *mod_description)
DEF_INSIDE_PUBLIC \ DEF_INSIDE_PUBLIC \
void init##__name__ () \ void init##__name__ () \
{ \ { \
module_init (__name_str__, __description__); \ module_init (STRINGIFY(__name__), __name_str__, __description__); \
} \ } \
#define DEFINE_PYMOD_WITH_INIT(__name__, __name_str__, __description__, __init__) \ #define DEFINE_PYMOD_WITH_INIT(__name__, __name_str__, __description__, __init__) \
@ -75,7 +78,7 @@ module_init (const char *mod_name, const char *mod_description)
DEF_INSIDE_PUBLIC \ DEF_INSIDE_PUBLIC \
void init##__name__ () \ void init##__name__ () \
{ \ { \
__init__ (__name_str__, __description__); \ __init__ (STRINGIFY(__name__), __name_str__, __description__); \
} \ } \
#else #else
@ -85,7 +88,7 @@ module_init (const char *mod_name, const char *mod_description)
DEF_INSIDE_PUBLIC \ DEF_INSIDE_PUBLIC \
PyObject *PyInit_##__name__ () \ PyObject *PyInit_##__name__ () \
{ \ { \
return module_init (__name_str__, __description__); \ return module_init (STRINGIFY(__name__), __name_str__, __description__); \
} \ } \
#define DEFINE_PYMOD_WITH_INIT(__name__, __name_str__, __description__, __init__) \ #define DEFINE_PYMOD_WITH_INIT(__name__, __name_str__, __description__, __init__) \
@ -93,7 +96,7 @@ module_init (const char *mod_name, const char *mod_description)
DEF_INSIDE_PUBLIC \ DEF_INSIDE_PUBLIC \
PyObject *PyInit_##__name__ () \ PyObject *PyInit_##__name__ () \
{ \ { \
return __init__ (__name_str__, __description__); \ return __init__ (STRINGIFY(__name__), __name_str__, __description__); \
} \ } \
#endif #endif