diff --git a/.appveyor.yml b/.appveyor.yml index 23e61e71..0fe77760 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -46,6 +46,7 @@ install: before_build: - cmd: git submodule update --init + - cmd: python scripts\compile_python_resources.py # Build config build_script: diff --git a/scripts/compile_python_resources.py b/scripts/compile_python_resources.py new file mode 100644 index 00000000..7c05c17e --- /dev/null +++ b/scripts/compile_python_resources.py @@ -0,0 +1,10 @@ +import compileall +import os + +root = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)) +compileall.compile_dir(os.path.join(root, 'src', 'python'), legacy=True, optimize=2) +with open(os.path.join(root, 'src', 'resources.qrc'), 'r+b') as f: + data = f.read() + data = data.replace(b'.py<', b'.pyc<') + f.seek(0) + f.write(data) diff --git a/src/utils/JupyterConnection.cpp b/src/utils/JupyterConnection.cpp index c2de55a0..e5fd0862 100644 --- a/src/utils/JupyterConnection.cpp +++ b/src/utils/JupyterConnection.cpp @@ -1,6 +1,7 @@ #ifdef CUTTER_ENABLE_JUPYTER #include +#include #include #include @@ -85,13 +86,23 @@ void JupyterConnection::createCutterJupyterModule() PyEval_RestoreThread(pyThreadState); } - QFile moduleFile(":/python/cutter_jupyter.py"); + QFile moduleFile(":/python/cutter_jupyter.pyc"); + bool isBytecode = moduleFile.exists(); + if (!isBytecode) { + moduleFile.setFileName(":/python/cutter_jupyter.py"); + } moduleFile.open(QIODevice::ReadOnly); QByteArray moduleCode = moduleFile.readAll(); moduleFile.close(); - auto moduleCodeObject = Py_CompileString(moduleCode.constData(), "cutter_jupyter.py", - Py_file_input); + PyObject *moduleCodeObject; + if (isBytecode) { + moduleCodeObject = PyMarshal_ReadObjectFromString(moduleCode.constData() + 12, + moduleCode.size() - 12); + } else { + moduleCodeObject = Py_CompileString(moduleCode.constData(), "cutter_jupyter.py", + Py_file_input); + } if (!moduleCodeObject) { PyErr_Print(); qWarning() << "Could not compile cutter_jupyter."; diff --git a/src/utils/NestedIPyKernel.cpp b/src/utils/NestedIPyKernel.cpp index 496fd9a1..f72dbffb 100644 --- a/src/utils/NestedIPyKernel.cpp +++ b/src/utils/NestedIPyKernel.cpp @@ -2,6 +2,7 @@ #ifdef CUTTER_ENABLE_JUPYTER #include +#include #include #include @@ -19,13 +20,23 @@ NestedIPyKernel *NestedIPyKernel::start(const QStringList &argv) return nullptr; } - QFile moduleFile(":/python/cutter_ipykernel.py"); + QFile moduleFile(":/python/cutter_ipykernel.pyc"); + bool isBytecode = moduleFile.exists(); + if (!isBytecode) { + moduleFile.setFileName(":/python/cutter_ipykernel.py"); + } moduleFile.open(QIODevice::ReadOnly); QByteArray moduleCode = moduleFile.readAll(); moduleFile.close(); - auto moduleCodeObject = Py_CompileString(moduleCode.constData(), "cutter_ipykernel.py", - Py_file_input); + PyObject *moduleCodeObject; + if (isBytecode) { + moduleCodeObject = PyMarshal_ReadObjectFromString(moduleCode.constData() + 12, + moduleCode.size() - 12); + } else { + moduleCodeObject = Py_CompileString(moduleCode.constData(), "cutter_ipykernel.py", + Py_file_input); + } if (!moduleCodeObject) { qWarning() << "Could not compile cutter_ipykernel."; return nullptr;