Commit b85683ca authored by Minh Ngo's avatar Minh Ngo
Browse files

Porting Python2 backend to Python3

parent c9d2c3d5
find_package(PkgConfig)
pkg_check_modules(PYTHONLIBS3 QUIET python3)
set(PYTHONLIBS3_DEFINITIONS ${PYTHONLIBS3_CFLAGS_OTHER})
find_path(PYTHONLIBS3_INCLUDE_DIR Python.h
HINTS ${PYTHONLIBS3_INCLUDEDIR} ${PYTHONLIBS3_INCLUDE_DIRS})
find_library(PYTHONLIBS3_LIBRARY NAMES python3.2m python3.3m python3.4m
HINTS ${PYTHONLIBS3_LIBDIR} ${PYTHONLIBS3_LIBRARY_DIRS} )
set(PYTHONLIBS3_LIBRARIES ${PYTHONLIBS3_LIBRARY} )
set(PYTHONLIBS3_INCLUDE_DIRS ${PYTHONLIBS3_INCLUDE_DIR} )
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Python3 DEFAULT_MSG
PYTHONLIBS3_LIBRARY PYTHONLIBS3_INCLUDE_DIR)
mark_as_advanced(PYTHONLIBS3_INCLUDE_DIR PYTHONLIBS3_LIBRARY)
......@@ -47,6 +47,16 @@ if(PYTHONLIBS_FOUND)
add_subdirectory(python2)
endif(PYTHONLIBS_FOUND)
set_package_properties(PythonLibs3 PROPERTIES DESCRIPTION "A powerful dynamic programming language."
URL "https://www.python.org/"
TYPE OPTIONAL
PURPOSE "Backend to use Python 3 with Cantor.")
find_package(PythonLibs3)
if(PYTHONLIBS3_FOUND)
add_subdirectory(python3)
endif(PYTHONLIBS3_FOUND)
set_package_properties(LuaJIT PROPERTIES DESCRIPTION "A lightweight, extensible programming language (luajit implementation)."
URL "http://www.lua.org/"
TYPE OPTIONAL
......
[KNewStuff3]
ProvidersUrl=http://download.kde.org/ocs/providers.xml
Categories=Cantor (Maxima),Cantor (Sage),Cantor (KAlgebra),Cantor (Qalculate),Cantor (Python2),Cantor (Scilab),Cantor (Octave),Cantor (R),Cantor (Lua)
Categories=Cantor (Maxima),Cantor (Sage),Cantor (KAlgebra),Cantor (Qalculate),Cantor (Python2),Cantor (Python3),Cantor (Scilab),Cantor (Octave),Cantor (R),Cantor (Lua)
UploadCategories=Cantor (R)
TargetDir=cantor/examples
Uncompress=never
......
[KNewStuff3]
ProvidersUrl=http://download.kde.org/ocs/providers.xml
Categories=Cantor (Maxima),Cantor (Sage),Cantor (KAlgebra),Cantor (Qalculate),Cantor (Python2),Cantor (Scilab),Cantor (Octave),Cantor (R),Cantor (Lua)
Categories=Cantor (Maxima),Cantor (Sage),Cantor (KAlgebra),Cantor (Qalculate),Cantor (Python2),Cantor (Python3),Cantor (Scilab),Cantor (Octave),Cantor (R),Cantor (Lua)
UploadCategories=Cantor (KAlgebra)
TargetDir=cantor/examples
Uncompress=never
......
[KNewStuff3]
ProvidersUrl=http://download.kde.org/ocs/providers.xml
Categories=Cantor (Maxima),Cantor (Sage),Cantor (KAlgebra),Cantor (Qalculate),Cantor (Python2),Cantor (Scilab),Cantor (Octave),Cantor (R),Cantor (Lua)
Categories=Cantor (Maxima),Cantor (Sage),Cantor (KAlgebra),Cantor (Qalculate),Cantor (Python2),Cantor (Python3),Cantor (Scilab),Cantor (Octave),Cantor (R),Cantor (Lua)
UploadCategories=Cantor (Lua)
TargetDir=cantor/examples
Uncompress=never
......
[KNewStuff3]
ProvidersUrl=http://download.kde.org/ocs/providers.xml
Categories=Cantor (Maxima),Cantor (Sage),Cantor (KAlgebra),Cantor (Qalculate),Cantor (Python2),Cantor (Scilab),Cantor (Octave),Cantor (R),Cantor (Lua)
Categories=Cantor (Maxima),Cantor (Sage),Cantor (KAlgebra),Cantor (Qalculate),Cantor (Python2),Cantor (Python3),Cantor (Scilab),Cantor (Octave),Cantor (R),Cantor (Lua)
UploadCategories=Cantor (Maxima)
TargetDir=cantor/examples
Uncompress=never
......
[KNewStuff3]
ProvidersUrl=http://download.kde.org/ocs/providers.xml
Categories=Cantor (Maxima),Cantor (Sage),Cantor (KAlgebra),Cantor (Qalculate),Cantor (Python2),Cantor (Scilab),Cantor (Octave),Cantor (R),Cantor (Lua)
Categories=Cantor (Maxima),Cantor (Sage),Cantor (KAlgebra),Cantor (Qalculate),Cantor (Python2),Cantor (Python3),Cantor (Scilab),Cantor (Octave),Cantor (R),Cantor (Lua)
UploadCategories=Cantor (Octave)
TargetDir=cantor/examples
Uncompress=never
......
......@@ -24,6 +24,7 @@
#include "pythonextensions.h"
#include "settings.h"
#include "ui_settings.h"
#include "pythonutils.h"
#include <QDebug>
#include <QWidget>
......@@ -34,7 +35,7 @@
PythonBackend::PythonBackend(QObject* parent,const QList<QVariant> args ) : Cantor::Backend( parent,args)
{
qDebug()<<"Creating Python2Backend";
qDebug()<< toPythonVersionSpecific("Creating Python2Backend");
new PythonLinearAlgebraExtension(this);
new PythonPackagingExtension(this);
......@@ -42,37 +43,41 @@ PythonBackend::PythonBackend(QObject* parent,const QList<QVariant> args ) : Cant
new PythonScriptExtension(this);
new PythonVariableManagementExtension(this);
setObjectName(QLatin1String("python2backend"));
setObjectName(toPythonVersionSpecific("python2backend"));
// Because the plugin may not have been loaded with
// ExportExternalSymbols, we load the python symbols again
// to make sure that python modules such as numpy see them
// (see bug #330032)
#ifdef BUILD_WITH_PYTHON3
QLibrary pythonLib(QLatin1String(PYTHON3_LIB_NAME));
#else
QLibrary pythonLib(QLatin1String("python2.7"));
#endif
pythonLib.setLoadHints(QLibrary::ExportExternalSymbolsHint);
pythonLib.load();
}
PythonBackend::~PythonBackend()
{
qDebug()<<"Destroying Python2Backend";
qDebug()<<toPythonVersionSpecific("Destroying Python2Backend");
}
QString PythonBackend::id() const
{
return QLatin1String("python2");
return toPythonVersionSpecific("python2");
}
Cantor::Session* PythonBackend::createSession()
{
qDebug()<<"Spawning a new Python 2 session";
qDebug()<<toPythonVersionSpecific("Spawning a new Python 2 session");
return new PythonSession(this);
}
Cantor::Backend::Capabilities PythonBackend::capabilities() const
{
qDebug()<<"Requesting capabilities of Python2Session";
qDebug()<<toPythonVersionSpecific("Requesting capabilities of Python2Session");
return Cantor::Backend::SyntaxHighlighting |
Cantor::Backend::Completion |
......@@ -95,16 +100,30 @@ KConfigSkeleton* PythonBackend::config() const
KUrl PythonBackend::helpUrl() const
{
#ifdef BUILD_WITH_PYTHON3
return i18nc("the url to the documentation Python 3", "http://docs.python.org/3/");
#else
return i18nc("the url to the documentation Python 2", "http://docs.python.org/2/");
#endif
}
QString PythonBackend::description() const
{
#ifdef BUILD_WITH_PYTHON3
return i18n("<p>Python is a remarkably powerful dynamic programming language that is used in a wide variety of application domains. " \
"There are several Python packages to scientific programming.</p>" \
"<p>This backend supports Python 3.</p>");
#else
return i18n("<p>Python is a remarkably powerful dynamic programming language that is used in a wide variety of application domains. " \
"There are several Python packages to scientific programming.</p>" \
"<p>This backend supports Python 2.</p>");
#endif
}
#ifdef BUILD_WITH_PYTHON3
K_EXPORT_CANTOR_PLUGIN(python3backend, PythonBackend)
#else
K_EXPORT_CANTOR_PLUGIN(python2backend, PythonBackend)
#endif
#include "pythonbackend.moc"
......@@ -30,7 +30,7 @@
#include <QFile>
#include "pythonsession.h"
#include "settings.h"
#include "pythonutils.h"
#include "imageresult.h"
#include <QDir>
typedef Cantor::ImageResult PythonPlotResult;
......@@ -94,7 +94,7 @@ void PythonExpression::parsePlotFile(QString filename)
{
qDebug() << "parsePlotFile";
qDebug() << "Python2Expression::parsePlotFile: " << filename;
qDebug() << toPythonVersionSpecific("Python2Expression::parsePlotFile: ") << filename;
setResult(new PythonPlotResult(filename));
......
......@@ -176,8 +176,7 @@ void PythonSession::runExpression(PythonExpression* expr)
if((!PythonKeywords::instance()->keywords().contains(firstLineWord)) && (!commandLine.at(contLine).contains(QLatin1String("="))) &&
(!commandLine.at(contLine).endsWith(QLatin1String(":"))) && (!commandLine.at(contLine).startsWith(QLatin1String(" ")))){
commandProcessing += QLatin1String("print ") + commandLine.at(contLine) + QLatin1String("\n");
commandProcessing += QLatin1String("print(") + commandLine.at(contLine) + QLatin1String(")\n");
continue;
}
......@@ -204,12 +203,10 @@ void PythonSession::runExpression(PythonExpression* expr)
qDebug() << "Insert print in " << contIdentationSpace << "space";
qDebug() << "commandIdentation before insert " << commandIdentation;
commandIdentation.insert(contIdentationSpace, QLatin1String("print "));
commandIdentation.insert(contIdentationSpace, QLatin1String("print("));
qDebug() << "commandIdentation after insert" << commandIdentation;
commandProcessing += commandIdentation + QLatin1String("\n");
commandProcessing += commandIdentation + QLatin1String(")\n");
continue;
}
......@@ -246,12 +243,19 @@ void PythonSession::getPythonCommandOutput(QString commandProcessing)
PyObject *outputPython = PyObject_GetAttrString(m_pModule, "outputPythonBackend");
PyObject *output = PyObject_GetAttrString(outputPython, "value");
#ifdef BUILD_WITH_PYTHON3
string outputString = PyUnicode_AsUTF8(output);
#else
string outputString = PyString_AsString(output);
#endif
PyObject *errorPython = PyObject_GetAttrString(m_pModule, "errorPythonBackend");
PyObject *error = PyObject_GetAttrString(errorPython, "value");
#ifdef BUILD_WITH_PYTHON3
string errorString = PyUnicode_AsUTF8(error);
#else
string errorString = PyString_AsString(error);
#endif
m_output = QString::fromLocal8Bit(outputString.c_str());
m_error = QString::fromLocal8Bit(errorString.c_str());
......@@ -287,12 +291,12 @@ bool PythonSession::identifyKeywords(QString command)
if(moduleVariable.isEmpty() && (command.endsWith(QLatin1String("*")))){
listKeywords += QString::fromLatin1("import %1\n" \
"print dir(%1)\n" \
"print(dir(%1))\n" \
"del %1\n").arg(moduleImported);
}
if(!moduleVariable.isEmpty()){
listKeywords += QLatin1String("print dir(") + moduleVariable + QLatin1String(")\n");
listKeywords += QLatin1String("print(dir(") + moduleVariable + QLatin1String("))\n");
}
if(!listKeywords.isEmpty()){
......@@ -400,7 +404,7 @@ void PythonSession::plotFileChanged(QString filename)
void PythonSession::listVariables()
{
QString listVariableCommand;
listVariableCommand += QLatin1String("print globals()\n");
listVariableCommand += QLatin1String("print(globals())\n");
getPythonCommandOutput(listVariableCommand);
......
#pragma once
#include <QString>
inline QString toPythonVersionSpecific(const char *str)
{
#ifdef BUILD_WITH_PYTHON3
QString result(QString::fromUtf8(str));
result.replace(QString::fromUtf8("ython2"), QString::fromUtf8("ython3"));
return result;
#else
return QString::fromUtf8(str);
#endif
}
\ No newline at end of file
......@@ -32,6 +32,8 @@ target_link_libraries(cantor_python2backend
add_executable(testpython2 testpython2.cpp)
target_link_libraries( testpython2 ${QT_QTTEST_LIBRARY} cantorlibs cantortest )
add_test(NAME testpython2 COMMAND testpython2)
install(FILES cantor_python2.knsrc DESTINATION ${CONFIG_INSTALL_DIR})
install(FILES python2backend.desktop DESTINATION ${SERVICES_INSTALL_DIR}/cantor)
install(FILES ../python/keywords.xml DESTINATION ${DATA_INSTALL_DIR}/cantor/python2backend)
......
[KNewStuff3]
ProvidersUrl=http://download.kde.org/ocs/providers.xml
Categories=Cantor (Maxima),Cantor (Sage),Cantor (KAlgebra),Cantor (Qalculate),Cantor (Python2),Cantor (Scilab),Cantor (Octave),Cantor (R),Cantor (Lua)
Categories=Cantor (Maxima),Cantor (Sage),Cantor (KAlgebra),Cantor (Qalculate),Cantor (Python2),Cantor (Python3),Cantor (Scilab),Cantor (Octave),Cantor (R),Cantor (Lua)
UploadCategories=Cantor (Python2)
TargetDir=cantor/examples
Uncompress=never
......
add_definitions(-DBUILD_WITH_PYTHON3)
set( Python3Backend_SRCS
../python/pythonbackend.cpp
../python/pythonsession.cpp
../python/pythonexpression.cpp
../python/pythonkeywords.cpp
../python/pythonhighlighter.cpp
../python/pythoncompletionobject.cpp
../python/pythonextensions.cpp
)
if(MSVC)
# ssize_t is typedef'd in both kdewin and python headers, this prevents using the kdewin one
add_definitions(-D_SSIZE_T_DEFINED)
endif(MSVC)
string(REPLACE "-l" "" PYTHON3_LIB_NAME ${PYTHONLIBS3_LDFLAGS})
add_definitions(-DPYTHON3_LIB_NAME="${PYTHON3_LIB_NAME}")
include_directories(${PYTHONLIBS3_INCLUDE_DIRS})
kconfig_add_kcfg_files(Python3Backend_SRCS ../python/settings.kcfgc)
install(FILES ../python/pythonbackend.kcfg DESTINATION ${KCFG_INSTALL_DIR})
ki18n_wrap_ui(Python3Backend_SRCS ../python/settings.ui)
add_library(cantor_python3backend MODULE ${Python3Backend_SRCS})
target_link_libraries(cantor_python3backend
${PYTHONLIBS3_LIBRARIES}
cantorlibs
KF5::KIOCore
KF5::ConfigCore
KF5::ConfigGui)
install(FILES cantor_python3.knsrc DESTINATION ${CONFIG_INSTALL_DIR})
install(FILES python3backend.desktop DESTINATION ${SERVICES_INSTALL_DIR}/cantor)
install(FILES ../python/keywords.xml DESTINATION ${DATA_INSTALL_DIR}/cantor/python3backend)
install(TARGETS cantor_python3backend DESTINATION ${PLUGIN_INSTALL_DIR})
\ No newline at end of file
[KNewStuff3]
ProvidersUrl=http://download.kde.org/ocs/providers.xml
Categories=Cantor (Maxima),Cantor (Sage),Cantor (KAlgebra),Cantor (Qalculate),Cantor (Python2),Cantor (Python3),Cantor (Scilab),Cantor (Octave),Cantor (R),Cantor (Lua)
UploadCategories=Cantor (Python3)
TargetDir=cantor/examples
Uncompress=never
CustomName=true
\ No newline at end of file
[Desktop Entry]
Type=Service
Icon=pythonbackend
Name=Python3
Name[bs]=Python3
Name[ca]=Python3
Name[ca@valencia]=Python3
Name[cs]=Python3
Name[da]=Python3
Name[de]=Python3
Name[el]=Python3
Name[en_GB]=Python3
Name[es]=Python3
Name[et]=Python3
Name[fi]=Python3
Name[fr]=Python3
Name[gl]=Python3
Name[hu]=Python3
Name[it]=Python3
Name[kk]=Python3
Name[ko]=Python3
Name[nb]=Python3
Name[nds]=Python3
Name[nl]=Python3
Name[pl]=Python3
Name[pt]=Python3
Name[pt_BR]=Python3
Name[ru]=Python3
Name[sk]=Python3
Name[sl]=Python3
Name[sv]=Python 3
Name[te]=ప్యితన్3
Name[tr]=Python3
Name[uk]=Python3
Name[x-test]=xxPython3xx
Name[zh_TW]=Python3
X-KDE-PluginInfo-Website=http://python.org/
X-KDE-ServiceTypes=Cantor/Backend
X-KDE-Library=cantor_python3backend
X-KDE-PluginInfo-Name=Python3
X-KDE-PluginInfo-Depends=
X-KDE-PluginInfo-License=GPL
Comment=Backend for Python 3 Scientific Programming
Comment[bs]=Pozadina za Python 3 naučno programiranje
Comment[ca]=Dorsal per a la programació científica Python3
Comment[ca@valencia]=Dorsal per a la programació científica Python3
Comment[cs]=Podpůrná vrstva pro vědecké programovací v Python 3
Comment[da]=Backend til videnskabelige programmering med Python 3
Comment[de]=Modul für die wissenschaftliche Programmierumgebung Python 3
Comment[el]=Σύστημα υποστήριξης για το επιστημονικό προγραμματιστικό περιβάλλον Python 3
Comment[en_GB]=Backend for Python 3 Scientific Programming
Comment[es]=Motor para programación científica en Python 3
Comment[et]=Python3 teadusliku programmeerimise taustaprogramm
Comment[fi]=Python3-pohjaisen tieteellisen ohjelmointiympäristön taustajärjestelmä
Comment[fr]=Moteur pour la programmation scientifique Python 3
Comment[gl]=Infraestrutura para o programación científica con Python 3
Comment[hu]=Háttérprogram a Python 3 tudományos programozáshoz
Comment[it]=Backend per l'ambiente scientifico di programmazione Python 3
Comment[kk]=Python 3 ғылыми программалау тетігі
Comment[ko]=파이썬 3 과학 프로그래밍 백엔드
Comment[nb]=Bakgrunnsmotor for Python 3 Scientific Programming
Comment[nds]=Hülpprogramm för't Schrieven vun wetenschaplich Programmen mit Python3
Comment[nl]=Backend voor wetenschappelijke programmeeromgeving Python 3
Comment[pl]=Silnika dla naukowego środowiska programistycznego Python 3
Comment[pt]=Infra-Estrutura de Programação Científica com Python 3
Comment[pt_BR]=Infraestrutura de programação científica Python 3
Comment[ru]=Поддержка языка Python 3 и инженерных и научных расчётов на нём
Comment[sk]=Backend pre vedecké programovanie Python 3
Comment[sl]=Zaledje za znanstveno programersko okolje Python 3
Comment[sv]=Bakgrundsprogram för Python 3 vetenskaplig programmeringsmiljö
Comment[tr]=Python 3 Bilimsel Programlama için arka uç
Comment[uk]=Модуль наукового програмування мовою Python 3
Comment[x-test]=xxBackend for Python 3 Scientific Programmingxx
Comment[zh_TW]=Python 3 科學程式環境的後端介面
\ No newline at end of file
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