Commit 27d29471 authored by Nikita Sirgienko's avatar Nikita Sirgienko
Browse files

[Python] Few improvments

  - Use proper ImportError in Python2 server code (and still use ModuleNotFoundError in Python3)
  - Remove importing "default" modules (numpy), because pretty useless
  - Update tests, change testImportNumpy to testImportStatement (and import sys module, which always present)
parent 737b10be
try:
import numpy
except ModuleNotFoundError:
pass
<RCC>
<qresource prefix="/py">
<file>import_default_modules.py</file>
<file>variables_cleaner.py</file>
<file>variables_loader.py</file>
<file>variables_saver.py</file>
......
......@@ -151,7 +151,11 @@ QString PythonServer::variables(bool parseValue) const
" import numpy \n"
" __cantor_numpy_internal__ = numpy.get_printoptions()['threshold'] \n"
" numpy.set_printoptions(threshold=100000000) \n"
#if PY_MAJOR_VERSION == 3
"except ModuleNotFoundError: \n"
#elif PY_MAJOR_VERSION == 2
"except ImportError: \n"
#endif
" pass \n"
);
......@@ -193,7 +197,11 @@ QString PythonServer::variables(bool parseValue) const
" import numpy \n"
" numpy.set_printoptions(threshold=__cantor_numpy_internal__) \n"
" del __cantor_numpy_internal__ \n"
#if PY_MAJOR_VERSION == 3
"except ModuleNotFoundError: \n"
#elif PY_MAJOR_VERSION == 2
"except ImportError: \n"
#endif
" pass \n"
);
......
......@@ -99,11 +99,6 @@ void PythonSession::login()
variableModel()->update();
}
const QString& importerFile = QLatin1String(":py/import_default_modules.py");
evaluateExpression(fromSource(importerFile), Cantor::Expression::DeleteOnFinish, true);
changeStatus(Session::Done);
emit loginDone();
}
......
......@@ -54,9 +54,9 @@ void TestPython2::testCommandQueue()
QCOMPARE(cleanOutput(e3->result()->data().toString()), QLatin1String("3"));
}
void TestPython2::testImportNumpy()
void TestPython2::testImportStatement()
{
Cantor::Expression* e = evalExp(QLatin1String("import numpy"));
Cantor::Expression* e = evalExp(QLatin1String("import sys"));
QVERIFY(e != nullptr);
QCOMPARE(e->status(), Cantor::Expression::Done);
......
......@@ -35,7 +35,7 @@ class TestPython2 : public BackendTest
void testSimplePlot();
void testImportNumpy();
void testImportStatement();
void testInvalidSyntax();
void testSimpleExpressionWithComment();
......
......@@ -129,9 +129,9 @@ void TestPython3::testCompletion()
}
void TestPython3::testImportNumpy()
void TestPython3::testImportStatement()
{
Cantor::Expression* e = evalExp(QLatin1String("import numpy"));
Cantor::Expression* e = evalExp(QLatin1String("import sys"));
QVERIFY(e != nullptr);
QCOMPARE(e->status(), Cantor::Expression::Done);
......
......@@ -34,7 +34,7 @@ class TestPython3 : public BackendTest
void testSimplePlot();
void testImportNumpy();
void testImportStatement();
void testPython3Code();
void testInvalidSyntax();
......
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