diff options
author | Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io> | 2018-06-05 15:02:48 +0200 |
---|---|---|
committer | Alexandru Croitor <alexandru.croitor@qt.io> | 2018-06-06 14:05:38 +0000 |
commit | e58320653e3e455685636b58750088f806b98533 (patch) | |
tree | be097fd52b4f57d22911abe8c1b06b18e966cafa /examples | |
parent | b4d260cad13b9f65a7efd388bc16dcea9048496d (diff) |
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 <alexandru.croitor@qt.io>
Diffstat (limited to 'examples')
-rw-r--r-- | examples/scriptableapplication/pythonutils.cpp | 25 |
1 files 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 <QtCore/QCoreApplication> #include <QtCore/QDebug> #include <QtCore/QStringList> +#include <QtCore/QTemporaryFile> +#include <QtCore/QDir> #include <sbkpython.h> #include <sbkconverter.h> @@ -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; } |