klayout/src/pymod/unit_tests/pymod_tests.cc

148 lines
4.0 KiB
C++
Raw Normal View History

/*
KLayout Layout Viewer
2023-01-01 22:27:09 +01:00
Copyright (C) 2006-2023 Matthias Koefferlein
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// Oh my god ... STRINGIFY(s) will get the argument with MACROS REPLACED.
// So if the PYTHONPATH is something like build.linux-released, the "linux" macro
// set to 1 will make this "build.1-release". So STRINGIFY isn't a real solution.
// On the other hand that is the only documented way to turn a macro into a string.
// This will prevent that issue:
#undef linux
#define STRINGIFY(s) _STRINGIFY(s)
#define _STRINGIFY(s) #s
#include "tlUnitTest.h"
#include "tlStream.h"
#include "tlEnv.h"
int run_pymodtest (tl::TestBase *_this, const std::string &fn)
{
std::string pypath = STRINGIFY (PYTHONPATH);
tl::set_env ("PYTHONPATH", pypath);
tl::info << pypath;
std::string fp (tl::testdata ());
fp += "/pymod/";
fp += fn;
std::string text;
{
std::string cmd;
#if defined(__APPLE__)
// NOTE: because of system integrity, MacOS does not inherit DYLD_LIBRARY_PATH to child
// processes like sh. We need to port this variable explicitly.
const char *ldpath_name = "DYLD_LIBRARY_PATH";
const char *ldpath = getenv (ldpath_name);
if (ldpath) {
cmd += std::string (ldpath_name) + "=\"" + ldpath + "\"; export " + ldpath_name + "; ";
}
#endif
cmd += std::string ("\"") + STRINGIFY (PYTHON) + "\" " + fp + " 2>&1";
tl::info << cmd;
tl::InputPipe pipe (cmd);
tl::InputStream is (pipe);
text = is.read_all ();
// subprocess exits without error
EXPECT_EQ (pipe.wait(), 0);
}
tl::info << text;
EXPECT_EQ (text.find ("OK") != std::string::npos, true);
return 0;
}
#define PYMODTEST(n, file) \
TEST(n) { EXPECT_EQ (run_pymodtest(_this, file), 0); }
PYMODTEST (bridge, "bridge.py")
PYMODTEST (import_tl, "import_tl.py")
PYMODTEST (import_db, "import_db.py")
PYMODTEST (import_rdb, "import_rdb.py")
PYMODTEST (import_lay, "import_lay.py")
// others
PYMODTEST (issue1327, "issue1327.py")
#if defined(HAVE_QT) && defined(HAVE_QTBINDINGS)
PYMODTEST (import_QtCore, "import_QtCore.py")
2021-12-10 01:23:54 +01:00
#if QT_VERSION >= 0x60000
PYMODTEST (import_QtGui, "import_QtGui_Qt6.py")
#else
PYMODTEST (import_QtGui, "import_QtGui.py")
2021-12-10 01:23:54 +01:00
#endif
#if defined(HAVE_QT_XML)
PYMODTEST (import_QtXml, "import_QtXml.py")
#endif
#if defined(HAVE_QT_SQL)
PYMODTEST (import_QtSql, "import_QtSql.py")
#endif
#if defined(HAVE_QT_NETWORK)
PYMODTEST (import_QtNetwork, "import_QtNetwork.py")
#endif
2021-12-10 01:23:54 +01:00
#if defined(HAVE_QT_DESIGNER) && QT_VERSION < 0x60000
PYMODTEST (import_QtDesigner, "import_QtDesigner.py")
#endif
#if defined(HAVE_QT_UITOOLS)
#730: providing a new Qt module named QtUiTools for QUiLoader class s… (#735) * #730: providing a new Qt module named QtUiTools for QUiLoader class support. * Fixed a compile error on Mac * Added QtUiTools to some more places * Fixed a linker issue in the QtUiTools Python lib * On occasion fixed a infinite recursion problem in the debugger The recursion happened because by mistake I instantiated a QApplication inside an in-application Python script. This crashed the debugger due to infinite recursion. This is not a real use case but to prevent similar issues, a recursion sentinel was added. * Removed QCoreApplication#notify from script bindings Reasoning: "notify" made standalone scripts using QApplication and QUiLoader virtually impossible. Problem description: - When a QApplication object is instantiated, e.g. in Python, the Qt binding will install reimplementation hooks as the object may be dynamically extended. - A notify is virtual this means the *every* "notify" call in the application is routed through the interpreter. - For one thing this will slow down the application - But as "notify" is called a zillion times this has more than this side effect. - Specifically "notify" is called from within the QWidget constructor to indicate a new widget. Then, if a QDialog for example is instatiated, it's base class constructor will call "notify" when the object isn't ready yet. - This has another severe side effect: as the object isn't ready yet, it gets registered in the Python space with the wrong class and QDialog is not visible as such. To mitigate these problems, the most efficient solution is to disable "notify" in general. There is hardly any use case in a script environment (in C++, apart from hacking the only reasonable use case is exception handling, but this does not apply to scripts). For providing the call functionality of "notify" you should better use "postEvent" or "sendEvent" anyway. So farewell QCoreApplication.notify ... * Fixed python test for QtUiTools module * Fixed UiTools test on Qt4 - QUiLoader needs an application object Co-authored-by: Kazunari Sekigawa <kazunari.sekigawa@gmail.com>
2021-02-25 21:29:21 +01:00
PYMODTEST (import_QtUiTools, "import_QtUiTools.py")
#endif
#if QT_VERSION >= 0x50000
2021-12-10 01:23:54 +01:00
#if QT_VERSION >= 0x60000
PYMODTEST (import_QtWidgets, "import_QtWidgets_Qt6.py")
#else
PYMODTEST (import_QtWidgets, "import_QtWidgets.py")
2021-12-10 01:23:54 +01:00
#endif
#if defined(HAVE_QT_MULTIMEDIA)
PYMODTEST (import_QtMultimedia, "import_QtMultimedia.py")
#endif
#if defined(HAVE_QT_PRINTSUPPORT)
PYMODTEST (import_QtPrintSupport, "import_QtPrintSupport.py")
#endif
#if defined(HAVE_QT_SVG)
2021-12-10 01:23:54 +01:00
#if QT_VERSION >= 0x60000
PYMODTEST (import_QtSvg, "import_QtSvg_Qt6.py")
#else
PYMODTEST (import_QtSvg, "import_QtSvg.py")
#endif
2021-12-10 01:23:54 +01:00
#endif
#if defined(HAVE_QT_XML) && QT_VERSION < 0x60000
PYMODTEST (import_QtXmlPatterns, "import_QtXmlPatterns.py")
#endif
2021-12-11 15:17:16 +01:00
#if QT_VERSION >= 0x60000
2021-12-10 01:23:54 +01:00
PYMODTEST (import_QtCore5Compat, "import_QtCore5Compat.py")
#endif
#endif
PYMODTEST (import_pya, "pya_tests.py")
#endif