From e58320653e3e455685636b58750088f806b98533 Mon Sep 17 00:00:00 2001 From: Cristian Maureira-Fredes Date: Tue, 5 Jun 2018 15:02:48 +0200 Subject: scriptableapplication: execution as one line The previous approach executed the entered script line-by-line, it was not possible to execute multi-line statements, for example: for i in range(0, 10): print(i) because PyRun_SimpleString was complaining about the colon. To avoid all these extra steps we can concatenate all the lines into one, and then execute it, instead of creating a temporary file. This will delegate error handling to Python. Change-Id: Idda572aa1b2e3daad2ba1ed9d70f0a8714b8c995 Reviewed-by: Alexandru Croitor --- examples/scriptableapplication/pythonutils.cpp | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/examples/scriptableapplication/pythonutils.cpp b/examples/scriptableapplication/pythonutils.cpp index 2d5be552c..f546a5a6c 100644 --- a/examples/scriptableapplication/pythonutils.cpp +++ b/examples/scriptableapplication/pythonutils.cpp @@ -54,6 +54,8 @@ #include #include #include +#include +#include #include #include @@ -152,17 +154,22 @@ bool runScript(const QStringList &script) { if (init() == PythonUninitialized) return false; + + // Concatenating all the lines + QString content; + QTextStream ss(&content); + for (const QString &line: script) + ss << line << "\n"; + + // Executing the whole script as one line bool result = true; - for (const QString& lineS : script) { - const QByteArray line = lineS.toUtf8(); - if (PyRun_SimpleString(line.constData()) == -1) { - if (PyErr_Occurred()) - PyErr_Print(); - qWarning() << __FUNCTION__ << "Error at" << line; - result = false; - break; - } + const QByteArray line = content.toUtf8(); + if (PyRun_SimpleString(line.constData()) == -1) { + if (PyErr_Occurred()) + PyErr_Print(); + result = false; } + return result; } -- cgit v1.2.3