Commit 4bc20243 authored by Nikita Sirgienko's avatar Nikita Sirgienko
Browse files

[Python] Show Python warnings not as errors, but as text results

BUG: 409240
FIXED-IN: 19.08
parent b441bebc
......@@ -123,6 +123,12 @@ void PythonExpression::parseError(QString error)
setStatus(Cantor::Expression::Error);
}
void PythonExpression::parseWarning(QString warning)
{
if (!warning.isEmpty())
addResult(new Cantor::TextResult(warning));
}
void PythonExpression::imageChanged()
{
if(m_tempFile->size() <= 0)
......
......@@ -37,6 +37,7 @@ class PythonExpression : public Cantor::Expression
QString internalCommand() override;
void parseOutput(QString output);
void parseWarning(QString warning);
void parseError(QString error);
private:
......
......@@ -52,9 +52,10 @@ void PythonServer::interrupt()
PyErr_SetInterrupt();
}
void PythonServer::runPythonCommand(const string& command) const
void PythonServer::runPythonCommand(const string& command)
{
PyObject* py_dict = PyModule_GetDict(m_pModule);
m_error = false;
const char* prepareCommand =
"import sys;\n"\
......@@ -82,6 +83,7 @@ void PythonServer::runPythonCommand(const string& command) const
if (PyErr_Occurred())
{
// We now know, that we have a syntax error, so print the traceback and exit
m_error = true;
PyErr_PrintEx(0);
return;
}
......@@ -94,6 +96,7 @@ void PythonServer::runPythonCommand(const string& command) const
PyObject* codeFile = Py_CompileString(command.c_str(), filePath.c_str(), Py_file_input);
if (PyErr_Occurred())
{
m_error = true;
PyErr_PrintEx(0);
return;
}
......@@ -123,7 +126,10 @@ void PythonServer::runPythonCommand(const string& command) const
#warning Unknown Python version
#endif
if (PyErr_Occurred())
{
m_error = true;
PyErr_PrintEx(0);
}
}
string PythonServer::getError() const
......@@ -225,4 +231,9 @@ string PythonServer::variables(bool parseValue) const
return result;
}
bool PythonServer::isError() const
{
return m_error;
}
......@@ -34,13 +34,15 @@ class PythonServer
void login();
void interrupt();
void setFilePath(const std::string& path, const std::string& dir);
void runPythonCommand(const std::string& command) const;
void runPythonCommand(const std::string& command);
std::string getOutput() const;
std::string getError() const;
bool isError() const;
std::string variables(bool parseValue) const;
private:
PyObject* m_pModule{nullptr};
bool m_error{false};
std::string filePath;
};
......
......@@ -103,6 +103,8 @@ int main()
server.getOutput()
+ unitSep
+ server.getError()
+ unitSep
+ to_string((int)server.isError())
+ messageEnd;
std::cout << result.c_str();
......
......@@ -236,10 +236,19 @@ void PythonSession::readOutput()
const QString& output = message.section(unitSep, 0, 0);
const QString& error = message.section(unitSep, 1, 1);
if(error.isEmpty()){
bool isError = message.section(unitSep, 2, 2).toInt();
if (isError)
{
if(error.isEmpty()){
static_cast<PythonExpression*>(expressionQueue().first())->parseOutput(output);
} else {
static_cast<PythonExpression*>(expressionQueue().first())->parseError(error);
}
}
else
{
static_cast<PythonExpression*>(expressionQueue().first())->parseWarning(error);
static_cast<PythonExpression*>(expressionQueue().first())->parseOutput(output);
} else {
static_cast<PythonExpression*>(expressionQueue().first())->parseError(error);
}
finishFirstExpression(true);
}
......
......@@ -310,5 +310,14 @@ void TestPython2::testInterrupt()
QCOMPARE(e->result()->data().toString(), QLatin1String("4"));
}
void TestPython2::testWarning()
{
Cantor::Expression* e = evalExp(QLatin1String("import warnings; warnings.warn('Test')"));
QVERIFY(e != nullptr);
QCOMPARE(e->status(), Cantor::Expression::Status::Done);
QCOMPARE(e->results().size(), 1);
}
QTEST_MAIN(TestPython2)
......@@ -48,6 +48,8 @@ class TestPython2 : public BackendTest
void testCompletion();
void testInterrupt();
void testWarning();
private:
QString backendName() override;
};
......
......@@ -322,5 +322,14 @@ void TestPython3::testInterrupt()
QCOMPARE(e->result()->data().toString(), QLatin1String("4"));
}
void TestPython3::testWarning()
{
Cantor::Expression* e = evalExp(QLatin1String("import warnings; warnings.warn('Test')"));
QVERIFY(e != nullptr);
QCOMPARE(e->status(), Cantor::Expression::Status::Done);
QCOMPARE(e->results().size(), 1);
}
QTEST_MAIN(TestPython3)
......@@ -48,6 +48,8 @@ class TestPython3 : public BackendTest
void testCompletion();
void testInterrupt();
void testWarning();
private:
QString backendName() override;
};
......
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