From fc9783432bea387b9a46bd475d87013351c1cb36 Mon Sep 17 00:00:00 2001 From: Matthias Koefferlein Date: Sun, 15 Jul 2018 14:14:14 +0200 Subject: [PATCH] Bugfix: db_plugin modules were not loaded for python modules * Added unit test for this * Provided an option for easier debugging such issues: Setting env var KLAYOUT_VERBOSITY will enable debug levels on Python modules (and all other binaries). Plugin loading issues can be debugged by setting KLAYOUT_VERBOSITY=21. --- src/pymod/db/dbMain.cc | 10 +++++++++- src/pymod/pymodHelper.h | 16 ++++++++++++++++ src/tl/tl/tlLog.cc | 35 ++++++++++++++++++++++++++++++++--- testdata/pymod/import_db.py | 7 +++++++ 4 files changed, 64 insertions(+), 4 deletions(-) diff --git a/src/pymod/db/dbMain.cc b/src/pymod/db/dbMain.cc index 686fa149c..9a773e0e6 100644 --- a/src/pymod/db/dbMain.cc +++ b/src/pymod/db/dbMain.cc @@ -22,7 +22,15 @@ #include "../pymodHelper.h" +#include "../../db/db/dbInit.h" + // to force linking of the db module #include "../../db/db/dbForceLink.h" -DEFINE_PYMOD(db, "db", "KLayout core module 'db'") +static PyObject *db_module_init (const char *mod_name, const char *mod_description) +{ + db::init (); + return module_init (mod_name, mod_description); +} + +DEFINE_PYMOD_WITH_INIT(db, "db", "KLayout core module 'db'", db_module_init) diff --git a/src/pymod/pymodHelper.h b/src/pymod/pymodHelper.h index 0c921c3f9..c9c41a6a8 100644 --- a/src/pymod/pymodHelper.h +++ b/src/pymod/pymodHelper.h @@ -70,6 +70,14 @@ module_init (const char *mod_name, const char *mod_description) module_init (__name_str__, __description__); \ } \ +#define DEFINE_PYMOD_WITH_INIT(__name__, __name_str__, __description__, __init__) \ + extern "C" \ + DEF_INSIDE_PUBLIC \ + void init##__name__ () \ + { \ + __init__ (__name_str__, __description__); \ + } \ + #else #define DEFINE_PYMOD(__name__, __name_str__, __description__) \ @@ -80,5 +88,13 @@ module_init (const char *mod_name, const char *mod_description) return module_init (__name_str__, __description__); \ } \ +#define DEFINE_PYMOD_WITH_INIT(__name__, __name_str__, __description__, __init__) \ + extern "C" \ + DEF_INSIDE_PUBLIC \ + PyObject *PyInit_##__name__ () \ + { \ + return __init__ (__name_str__, __description__); \ + } \ + #endif diff --git a/src/tl/tl/tlLog.cc b/src/tl/tl/tlLog.cc index 22f574d04..9b36bce9d 100644 --- a/src/tl/tl/tlLog.cc +++ b/src/tl/tl/tlLog.cc @@ -21,19 +21,48 @@ */ -#include - #include "tlLog.h" +#include "tlString.h" +#include #include +#if defined(_WIN32) +# include +#endif + namespace tl { // ------------------------------------------------ // Verbosity implementation -static int m_verbosity_level = 0; +static int default_verbosity () +{ + const char *verbosity_str = 0; + +#if defined(_WIN32) + const wchar_t *verbosity_wstr = _wgetenv (L"KLAYOUT_VERBOSITY")); + std::string vs; + if (verbosity_wstr) { + vs = tl::to_string (std::wstring (verbosity_wstr)); + verbosity_str = vs.c_str (); + } +#else + verbosity_str = getenv ("KLAYOUT_VERBOSITY"); +#endif + + int verbosity = 0; + if (verbosity_str) { + try { + tl::from_string (verbosity_str, verbosity); + } catch (...) { + } + } + return verbosity; +} + +static int m_verbosity_level = default_verbosity (); void verbosity (int level) diff --git a/testdata/pymod/import_db.py b/testdata/pymod/import_db.py index 5a4e31c83..7bb5b97fc 100644 --- a/testdata/pymod/import_db.py +++ b/testdata/pymod/import_db.py @@ -19,6 +19,7 @@ import klayout.db as db import unittest import sys +import os # Tests the basic abilities of the module @@ -34,6 +35,12 @@ class BasicTest(unittest.TestCase): v = db.Box(1, 2, 3, 4) self.assertEqual(str(v), "(1,2;3,4)") + def test_3(self): + # db plugins loaded? + v = db.Layout() + v.read(os.path.join(os.path.dirname(__file__), "..", "gds", "t10.gds")) + self.assertEqual(v.top_cell().name, "RINGO") + # run unit tests if __name__ == '__main__': suite = unittest.TestSuite()