diff options
author | Eike Ziller <eike.ziller@qt.io> | 2018-09-11 15:15:46 +0200 |
---|---|---|
committer | Eike Ziller <eike.ziller@qt.io> | 2018-09-12 11:23:46 +0000 |
commit | 0dd71b40f6c86a8cf1d3ff442edfe9a6db13379f (patch) | |
tree | f2d3a62c64541aba1f55448f60a557d6eebcd9db /plugins | |
parent | 56d0cb5bd02d0626bd54e4db9cec8a2fccc892f0 (diff) |
Pull out running string as script as local function
Change-Id: Iae9e5723cb95ad96e03bdfdbb1b9c1367050c203
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/pythonextensions/pyutil.cpp | 30 |
1 files changed, 14 insertions, 16 deletions
diff --git a/plugins/pythonextensions/pyutil.cpp b/plugins/pythonextensions/pyutil.cpp index 8f2728f..169c8a0 100644 --- a/plugins/pythonextensions/pyutil.cpp +++ b/plugins/pythonextensions/pyutil.cpp @@ -76,6 +76,17 @@ namespace PyUtil { static State state = PythonUninitialized; +static bool runSimpleString(const std::string &script) +{ + if (PyRun_SimpleString(script.c_str()) == -1) { + if (PyErr_Occurred()) + PyErr_Print(); + return false; + } + + return true; +} + static void cleanup() { if (state > PythonUninitialized) { @@ -127,12 +138,8 @@ State init() // The Python interpreter eats SIGINT, which is not what we want. // This stops it from happening. // See https://mail.python.org/pipermail/cplusplus-sig/2012-December/016858.html - if (PyRun_SimpleString(std::string( - "import signal as " privateName("signal") "\n" - "" privateName("signal") ".signal(" privateName("signal") ".SIGINT, " privateName("signal") ".SIG_DFL)" - ).c_str()) == -1) { - if (PyErr_Occurred()) - PyErr_Print(); + if (!runSimpleString("import signal as " privateName("signal") "\n" + "" privateName("signal") ".signal(" privateName("signal") ".SIGINT, " privateName("signal") ".SIG_DFL)")) { qWarning("Failed to prevent SIGINT capture."); } @@ -222,16 +229,7 @@ bool bindSubPyObject(const QString &moduleName, const QString &name, void *obj) bool runScript(const std::string &script) { - if (init() == PythonUninitialized) - return false; - - if (PyRun_SimpleString(script.c_str()) == -1) { - if (PyErr_Occurred()) - PyErr_Print(); - return false; - } - - return true; + return (init() >= PythonInitialized && runSimpleString(script)); } bool runScriptWithPath(const std::string &script, const std::string &path) |