Commit 612d0bfb authored by Alexander Semke's avatar Alexander Semke
Browse files

[python] minor code cleanup and trying to fix a python test failing on CI.

parent 3f1da4d8
Pipeline #123713 passed with stage
in 23 minutes and 37 seconds
......@@ -13,11 +13,6 @@
#include "session.h"
#include "settings.h"
#include <KIconLoader>
#include <QDir>
#include <QFileSystemWatcher>
#include <QTemporaryFile>
#include <QFile>
#include <QDebug>
#include "pythonsession.h"
......@@ -40,10 +35,10 @@ QString PythonExpression::internalCommand()
if (PythonSettings::integratePlots())
{
PythonSession* pySession = static_cast<PythonSession*>(session());
auto* pySession = static_cast<PythonSession*>(session());
const QString& filepath = pySession->plotFilePrefixPath() + QString::number(pySession->plotFileCounter()) + QLatin1String(".png");
for(const Cantor::GraphicPackage& package : session()->enabledGraphicPackages())
for(const auto& package : session()->enabledGraphicPackages())
{
if (package.isHavePlotCommand())
{
......@@ -54,7 +49,6 @@ QString PythonExpression::internalCommand()
}
QStringList commandLine = cmd.split(QLatin1String("\n"));
QString commandProcessing;
for(const QString& command : commandLine){
......@@ -90,7 +84,7 @@ void PythonExpression::parseOutput(QString output)
}
else if (!output.isEmpty())
{
PythonSession* pySession = static_cast<PythonSession*>(session());
auto* pySession = static_cast<PythonSession*>(session());
const QString& plotFilePrefixPath = pySession->plotFilePrefixPath();
const QString& searchPrefixPath = QLatin1String("INNER PLOT INFO CANTOR: ") + plotFilePrefixPath;
......@@ -118,7 +112,7 @@ void PythonExpression::parseOutput(QString output)
setStatus(Cantor::Expression::Done);
}
void PythonExpression::parseError(QString error)
void PythonExpression::parseError(const QString& error)
{
qDebug() << "expression error: " << error;
setErrorMessage(error);
......@@ -126,11 +120,11 @@ void PythonExpression::parseError(QString error)
setStatus(Cantor::Expression::Error);
}
void PythonExpression::parseWarning(QString warning)
void PythonExpression::parseWarning(const QString& warning)
{
if (!warning.isEmpty())
{
Cantor::TextResult* result = new Cantor::TextResult(warning);
auto* result = new Cantor::TextResult(warning);
result->setStdErr(true);
addResult(result);
}
......
......@@ -8,22 +8,20 @@
#include "expression.h"
class QTemporaryFile;
class PythonExpression : public Cantor::Expression
{
Q_OBJECT
public:
PythonExpression(Cantor::Session* session, bool internal);
PythonExpression(Cantor::Session*, bool internal);
~PythonExpression() override;
void evaluate() override;
void interrupt() override;
QString internalCommand() override;
void parseOutput(QString output);
void parseWarning(QString warning);
void parseError(QString error);
void parseOutput(QString);
void parseWarning(const QString&);
void parseError(const QString&);
};
#endif /* _PYTHONEXPRESSION_H */
......@@ -172,7 +172,6 @@ string PythonServer::variables(bool parseValue)
" pass \n", nullptr
);
string result;
for (const string& s : vars)
result += s + char(18);
......@@ -184,5 +183,3 @@ bool PythonServer::isError() const
{
return m_error;
}
......@@ -74,7 +74,7 @@ int main()
}
if (records[0] == FILEPATH)
{
vector<string> args = split(records[1], unitSep);
const vector<string>& args = split(records[1], unitSep);
if (args.size() == 2)
server.setFilePath(args[0], args[1]);
}
......
......@@ -161,7 +161,7 @@ void PythonSession::interrupt()
; //TODO: interrupt the process on windows
#endif
}
for (Cantor::Expression* expression : expressionQueue())
for (auto* expression : expressionQueue())
expression->setStatus(Cantor::Expression::Interrupted);
expressionQueue().clear();
......@@ -182,7 +182,7 @@ Cantor::Expression* PythonSession::evaluateExpression(const QString& cmd, Cantor
updateGraphicPackagesFromSettings();
qDebug() << "evaluating: " << cmd;
PythonExpression* expr = new PythonExpression(this, internal);
auto* expr = new PythonExpression(this, internal);
changeStatus(Cantor::Session::Running);
......@@ -208,8 +208,8 @@ void PythonSession::runFirstExpression()
if (expressionQueue().isEmpty())
return;
Cantor::Expression* expr = expressionQueue().first();
const QString command = expr->internalCommand();
auto* expr = expressionQueue().first();
const QString& command = expr->internalCommand();
qDebug() << "run first expression" << command;
expr->setStatus(Cantor::Expression::Computing);
......@@ -255,18 +255,19 @@ void PythonSession::readOutput()
const QString& output = message.section(unitSep, 0, 0);
const QString& error = message.section(unitSep, 1, 1);
bool isError = message.section(unitSep, 2, 2).toInt();
auto* expr = static_cast<PythonExpression*>(expressionQueue().first());
if (isError)
{
if(error.isEmpty()){
static_cast<PythonExpression*>(expressionQueue().first())->parseOutput(output);
expr->parseOutput(output);
} else {
static_cast<PythonExpression*>(expressionQueue().first())->parseError(error);
expr->parseError(error);
}
}
else
{
static_cast<PythonExpression*>(expressionQueue().first())->parseWarning(error);
static_cast<PythonExpression*>(expressionQueue().first())->parseOutput(output);
expr->parseWarning(error);
expr->parseOutput(output);
}
finishFirstExpression(true);
}
......
/*
SPDX-License-Identifier: GPL-2.0-or-later
SPDX-FileCopyrightText: 2015 Minh Ngo <minh@fedoraproject.org>
SPDX-FileCopyrightText: 2021-2022 Alexander Semke <alexander.semke@web.de>
*/
#include "testpython.h"
#include "session.h"
#include "backend.h"
#include "expression.h"
#include "imageresult.h"
#include "defaultvariablemodel.h"
......@@ -21,7 +21,7 @@ QString TestPython3::backendName()
void TestPython3::testSimpleCommand()
{
Cantor::Expression* e = evalExp(QLatin1String("2+2"));
auto* e = evalExp(QLatin1String("2+2"));
QVERIFY(e != nullptr);
QVERIFY(e->result());
......@@ -30,7 +30,7 @@ void TestPython3::testSimpleCommand()
void TestPython3::testMultilineCommand()
{
Cantor::Expression* e = evalExp(QLatin1String("print(2+2)\nprint(7*5)"));
auto* e = evalExp(QLatin1String("print(2+2)\nprint(7*5)"));
QVERIFY(e != nullptr);
QVERIFY(e->result());
......@@ -39,13 +39,13 @@ void TestPython3::testMultilineCommand()
void TestPython3::testCommandQueue()
{
Cantor::Expression* e1=session()->evaluateExpression(QLatin1String("0+1"));
Cantor::Expression* e2=session()->evaluateExpression(QLatin1String("1+1"));
Cantor::Expression* e3=evalExp(QLatin1String("1+2"));
auto* e1 = session()->evaluateExpression(QLatin1String("0+1"));
auto* e2 = session()->evaluateExpression(QLatin1String("1+1"));
auto* e3 = evalExp(QLatin1String("1+2"));
QVERIFY(e1!=nullptr);
QVERIFY(e2!=nullptr);
QVERIFY(e3!=nullptr);
QVERIFY(e1 != nullptr);
QVERIFY(e2 != nullptr);
QVERIFY(e3 != nullptr);
QVERIFY(e1->result());
QVERIFY(e2->result());
......@@ -58,7 +58,7 @@ void TestPython3::testCommandQueue()
void TestPython3::testCommentExpression()
{
Cantor::Expression* e = evalExp(QLatin1String("#only comment"));
auto* e = evalExp(QLatin1String("#only comment"));
QVERIFY(e != nullptr);
QCOMPARE(e->status(), Cantor::Expression::Status::Done);
......@@ -67,7 +67,7 @@ void TestPython3::testCommentExpression()
void TestPython3::testSimpleExpressionWithComment()
{
Cantor::Expression* e = evalExp(QLatin1String("2+2 # comment"));
auto* e = evalExp(QLatin1String("2+2 # comment"));
QVERIFY(e != nullptr);
QVERIFY(e->result());
......@@ -76,7 +76,7 @@ void TestPython3::testSimpleExpressionWithComment()
void TestPython3::testMultilineCommandWithComment()
{
Cantor::Expression* e = evalExp(QLatin1String(
auto* e = evalExp(QLatin1String(
"print(2+2) \n"
"#comment in middle \n"
"print(7*5)"));
......@@ -88,7 +88,7 @@ void TestPython3::testMultilineCommandWithComment()
void TestPython3::testInvalidSyntax()
{
Cantor::Expression* e=evalExp( QLatin1String("2+2*+.") );
auto* e=evalExp( QLatin1String("2+2*+.") );
QVERIFY( e!=nullptr );
QCOMPARE( e->status(), Cantor::Expression::Error );
......@@ -99,11 +99,11 @@ void TestPython3::testCompletion()
if(session()->status()==Cantor::Session::Running)
waitForSignal(session(), SIGNAL(statusChanged(Cantor::Session::Status)));
Cantor::CompletionObject* help = session()->completionFor(QLatin1String("p"), 1);
auto* help = session()->completionFor(QLatin1String("p"), 1);
waitForSignal(help, SIGNAL(fetchingDone()));
// Checks all completions for this request
const QStringList& completions = help->completions();
const auto& completions = help->completions();
QCOMPARE(completions.size(), 4);
QVERIFY(completions.contains(QLatin1String("pass")));
QVERIFY(completions.contains(QLatin1String("pow")));
......@@ -114,7 +114,7 @@ void TestPython3::testCompletion()
void TestPython3::testImportStatement()
{
Cantor::Expression* e = evalExp(QLatin1String("import sys"));
auto* e = evalExp(QLatin1String("import sys"));
QVERIFY(e != nullptr);
QCOMPARE(e->status(), Cantor::Expression::Done);
......@@ -123,7 +123,7 @@ void TestPython3::testImportStatement()
void TestPython3::testCodeWithComments()
{
{
Cantor::Expression* e = evalExp(QLatin1String("#comment\n1+2"));
auto* e = evalExp(QLatin1String("#comment\n1+2"));
QVERIFY(e != nullptr);
QVERIFY(e->result());
......@@ -131,7 +131,7 @@ void TestPython3::testCodeWithComments()
}
{
Cantor::Expression* e = evalExp(QLatin1String(" #comment\n1+2"));
auto* e = evalExp(QLatin1String(" #comment\n1+2"));
QVERIFY(e != nullptr);
QVERIFY(e->result());
......@@ -142,14 +142,14 @@ void TestPython3::testCodeWithComments()
void TestPython3::testPython3Code()
{
{
Cantor::Expression* e = evalExp(QLatin1String("print 1 + 2"));
auto* e = evalExp(QLatin1String("print 1 + 2"));
QVERIFY(e != nullptr);
QVERIFY(!e->errorMessage().isEmpty());
}
{
Cantor::Expression* e = evalExp(QLatin1String("print(1 + 2)"));
auto* e = evalExp(QLatin1String("print(1 + 2)"));
QVERIFY(e != nullptr);
QVERIFY(e->result());
......@@ -162,7 +162,7 @@ void TestPython3::testSimplePlot()
if (!PythonSettings::integratePlots())
QSKIP("This test needs enabled plots integration in Python3 settings", SkipSingle);
Cantor::Expression* e = evalExp(QLatin1String(
auto* e = evalExp(QLatin1String(
"import matplotlib\n"
"import matplotlib.pyplot as plt\n"
"import numpy as np"
......@@ -217,10 +217,10 @@ void TestPython3::testVariablesCreatingFromCode()
QAbstractItemModel* model = session()->variableModel();
QVERIFY(model != nullptr);
Cantor::Expression* e=evalExp(QLatin1String("a = 15; b = 'S';"));
QVERIFY(e!=nullptr);
auto* e = evalExp(QLatin1String("a = 15; b = 'S';"));
QVERIFY(e != nullptr);
if(session()->status()==Cantor::Session::Running)
if(session()->status() == Cantor::Session::Running)
waitForSignal(session(), SIGNAL(statusChanged(Cantor::Session::Status)));
QCOMPARE(2, model->rowCount());
......@@ -236,21 +236,21 @@ void TestPython3::testVariablesCreatingFromCode()
void TestPython3::testVariableCleanupAfterRestart()
{
Cantor::DefaultVariableModel* model = session()->variableModel();
QAbstractItemModel* model = session()->variableModel();
QVERIFY(model != nullptr);
Cantor::Expression* e=evalExp(QLatin1String("a = 15; b = 'S';"));
QVERIFY(e!=nullptr);
auto* e = evalExp(QLatin1String("a = 15; b = 'S';"));
QVERIFY(e != nullptr);
if(session()->status()==Cantor::Session::Running)
waitForSignal(session(), SIGNAL(statusChanged(Cantor::Session::Status)));
QCOMPARE(2, static_cast<QAbstractItemModel*>(model)->rowCount());
QCOMPARE(2, model->rowCount());
session()->logout();
session()->login();
QCOMPARE(0, static_cast<QAbstractItemModel*>(model)->rowCount());
QCOMPARE(0, model->rowCount());
}
void TestPython3::testDictVariable()
......@@ -261,11 +261,11 @@ void TestPython3::testDictVariable()
Cantor::DefaultVariableModel* model = session()->variableModel();
QVERIFY(model != nullptr);
Cantor::Expression* e=evalExp(QLatin1String("d = {'value': 33}"));
auto* e = evalExp(QLatin1String("d = {'value': 33}"));
QVERIFY(e!=nullptr);
QVERIFY(e != nullptr);
if(session()->status()==Cantor::Session::Running)
if(session()->status() == Cantor::Session::Running)
waitForSignal(session(), SIGNAL(statusChanged(Cantor::Session::Status)));
QCOMPARE(1, static_cast<QAbstractItemModel*>(model)->rowCount());
......@@ -277,8 +277,8 @@ void TestPython3::testDictVariable()
void TestPython3::testInterrupt()
{
Cantor::Expression* e1=session()->evaluateExpression(QLatin1String("import time; time.sleep(150)"));
Cantor::Expression* e2=session()->evaluateExpression(QLatin1String("2"));
auto* e1 = session()->evaluateExpression(QLatin1String("import time; time.sleep(150)"));
auto* e2 = session()->evaluateExpression(QLatin1String("2"));
if (e1->status() != Cantor::Expression::Queued)
waitForSignal(e1, SIGNAL(statusChanged(Cantor::Expression::Status)));
......@@ -307,7 +307,7 @@ void TestPython3::testInterrupt()
QCOMPARE(e1->status(), Cantor::Expression::Interrupted);
QCOMPARE(e2->status(), Cantor::Expression::Interrupted);
Cantor::Expression* e = evalExp(QLatin1String("2+2"));
auto* e = evalExp(QLatin1String("2+2"));
QVERIFY(e != nullptr);
QCOMPARE(e->status(), Cantor::Expression::Done);
......@@ -317,11 +317,11 @@ void TestPython3::testInterrupt()
void TestPython3::testWarning()
{
Cantor::Expression* e = evalExp(QLatin1String("import warnings; warnings.warn('Test')"));
auto* e = evalExp(QLatin1String("import warnings; warnings.warn('Test')"));
QVERIFY(e != nullptr);
while(session()->status() != Cantor::Session::Running)
if (session()->status() == Cantor::Session::Running)
waitForSignal(session(), SIGNAL(statusChanged(Cantor::Session::Status)));
QCOMPARE(e->status(), Cantor::Expression::Status::Done);
......@@ -329,4 +329,3 @@ void TestPython3::testWarning()
}
QTEST_MAIN(TestPython3)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment