Commit bb961ce6 authored by Nikita Sirgienko's avatar Nikita Sirgienko
Browse files

[Python] Add handler on python server process errors and minor improvments in...

[Python] Add handler on python server process errors and minor improvments in setWorksheetPath logic
parent 56495dcc
......@@ -147,11 +147,18 @@ QString PythonServer::getOutput() const
void PythonServer::setFilePath(const QString& path)
{
this->filePath = path;
PyRun_SimpleString(("import sys; sys.argv = ['" + path.toStdString() + "']").c_str());
QString dir = QFileInfo(path).absoluteDir().absolutePath();
PyRun_SimpleString(("import sys; sys.path.insert(0, '" + dir.toStdString() + "')").c_str());
PyRun_SimpleString(("__file__ = '"+path.toStdString()+"'").c_str());
if (path.isEmpty()) // New session, not from file
{
PyRun_SimpleString("import sys; sys.path.insert(0, '')");
}
else
{
this->filePath = path;
QString dir = QFileInfo(path).absoluteDir().absolutePath();
PyRun_SimpleString(("import sys; sys.path.insert(0, '" + dir.toStdString() + "')").c_str());
PyRun_SimpleString(("__file__ = '"+path.toStdString()+"'").c_str());
}
}
QString PythonServer::variables(bool parseValue) const
......
......@@ -34,6 +34,7 @@
#include <QProcess>
#include <KDirWatch>
#include <KLocalizedString>
#ifndef Q_OS_WIN
......@@ -57,6 +58,7 @@ PythonSession::PythonSession(Cantor::Backend* backend, int pythonVersion, const
PythonSession::~PythonSession()
{
if (m_process) {
disconnect(m_process, &QProcess::errorOccurred, this, &PythonSession::reportServerProcessError);
m_process->kill();
m_process->deleteLater();
m_process = nullptr;
......@@ -89,6 +91,7 @@ void PythonSession::login()
}
connect(m_process, &QProcess::readyReadStandardOutput, this, &PythonSession::readOutput);
connect(m_process, &QProcess::errorOccurred, this, &PythonSession::reportServerProcessError);
sendCommand(QLatin1String("login"));
sendCommand(QLatin1String("setFilePath"), QStringList(worksheetPath));
......@@ -112,6 +115,7 @@ void PythonSession::logout()
sendCommand(QLatin1String("exit"));
if(!m_process->waitForFinished(1000))
{
disconnect(m_process, &QProcess::errorOccurred, this, &PythonSession::reportServerProcessError);
m_process->kill();
qDebug()<<"cantor_python server still running, process kill enforced";
}
......@@ -233,3 +237,22 @@ void PythonSession::setWorksheetPath(const QString& path)
{
worksheetPath = path;
}
void PythonSession::reportServerProcessError(QProcess::ProcessError serverError)
{
switch(serverError)
{
case QProcess::Crashed:
emit error(i18n("Cantor Python server stopped working."));
break;
case QProcess::FailedToStart:
emit error(i18n("Failed to start Cantor python server."));
break;
default:
emit error(i18n("Communication with Cantor python server failed for unknown reasons."));
break;
}
logout();
}
......@@ -25,8 +25,7 @@
#include "session.h"
#include <cantor_pythonbackend_export.h>
#include <QStringList>
class QProcess;
#include <QProcess>
class CANTOR_PYTHONBACKEND_EXPORT PythonSession : public Cantor::Session
{
......@@ -58,9 +57,12 @@ class CANTOR_PYTHONBACKEND_EXPORT PythonSession : public Cantor::Session
QString m_output;
private Q_SLOT:
void readOutput();
void reportServerProcessError(QProcess::ProcessError serverError);
private:
void runFirstExpression() override;
void readOutput();
void sendCommand(const QString& command, const QStringList arguments = QStringList()) const;
};
......
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