diff --git a/cantor-4.10.3-sage58.patch b/cantor-4.10.3-sage58.patch
new file mode 100644
index 0000000..d17b55a
--- /dev/null
+++ b/cantor-4.10.3-sage58.patch
@@ -0,0 +1,123 @@
+diff --git a/src/backends/sage/sagecompletionobject.cpp b/src/backends/sage/sagecompletionobject.cpp
+index dec0c8f..a132295 100644
+--- a/src/backends/sage/sagecompletionobject.cpp
++++ b/src/backends/sage/sagecompletionobject.cpp
+@@ -52,7 +52,7 @@ void SageCompletionObject::fetchCompletions()
+
+ //cache the value of the "_" variable into __hist_tmp__, so we can restore the previous result
+ //after complete() was evaluated
+- m_expression=session()->evaluateExpression("__hist_tmp__=_; __IPYTHON__.complete(\""+command()+"\");_=__hist_tmp__");
++ m_expression=session()->evaluateExpression("__hist_tmp__=_; __CANTOR_IPYTHON_SHELL__.complete(\""+command()+"\");_=__hist_tmp__");
+ connect(m_expression, SIGNAL(gotResult()), this,
+ SLOT(extractCompletions()));
+
+@@ -71,11 +71,19 @@ void SageCompletionObject::extractCompletions()
+ kDebug()<<"something went wrong fetching tab completion";
+ return;
+ }
+-
++
+ //the result looks like "['comp1', 'comp2']" parse it
++
++ //for sage version 5.7 this looks like
++ //('s1', ['comp1','comp2']) where s1 is the string we called complete with
++
+ QString txt=res->toHtml().trimmed();
++ txt.remove("
");
++ txt=txt.mid(txt.indexOf(command())+command().length()+2).trimmed();
+ txt=txt.mid(1); //remove [
+- txt.chop(1); //remove ]
++ txt.chop(2); //remove ]
++
++ kDebug()<<"completion string: "<name();
+- if(type->name().contains("image"))
++ if(m_imagePath.isEmpty()||type->name().contains("image")||path.endsWith(".png")||path.endsWith(".gif"))
+ {
+- kDebug()<<"adding file "<is("image/gif"))
+ setResult( new Cantor::AnimationResult( KUrl(m_imagePath ),i18n("Result of %1" , command() ) ) );
+ else
+diff --git a/src/backends/sage/sagesession.cpp b/src/backends/sage/sagesession.cpp
+index a535277..d8ac325 100644
+--- a/src/backends/sage/sagesession.cpp
++++ b/src/backends/sage/sagesession.cpp
+@@ -38,10 +38,13 @@ const QByteArray SageSession::SageAlternativePrompt="....: "; //Text, sage outpu
+ static QByteArray initCmd="os.environ['PAGER'] = 'cat' \n "\
+ "sage.misc.pager.EMBEDDED_MODE = True \n "\
+ "sage.misc.viewer.BROWSER='' \n "\
++ "sage.misc.viewer.viewer.png_viewer('') \n" \
+ "sage.plot.plot3d.base.SHOW_DEFAULTS['viewer'] = 'tachyon' \n"\
+ "sage.misc.latex.EMBEDDED_MODE = True \n "\
+ "os.environ['PAGER'] = 'cat' \n "\
+- " __IPYTHON__.shell.autoindent=False \n "\
++ "%colors nocolor \n "\
++ "__CANTOR_IPYTHON_SHELL__=get_ipython() \n "\
++ "__CANTOR_IPYTHON_SHELL__.autoindent=False \n "\
+ "print '____TMP_DIR____', sage.misc.misc.SAGE_TMP\n"\
+ "print '____END_OF_INIT____' \n ";
+
+@@ -277,6 +280,7 @@ void SageSession::waitForNextPrompt()
+
+ void SageSession::fileCreated( const QString& path )
+ {
++ kDebug()<<"got a file "<addFileResult( path );
+@@ -290,9 +294,9 @@ void SageSession::setTypesettingEnabled(bool enable)
+ //the _ and __IP.outputcache() are needed to keep the
+ // _ operator working
+ if (enable)
+- evaluateExpression("sage.misc.latex.pretty_print_default(true);_;__IP.outputcache()", Cantor::Expression::DeleteOnFinish);
++ evaluateExpression("sage.misc.latex.pretty_print_default(true)", Cantor::Expression::DeleteOnFinish);
+ else
+- evaluateExpression("sage.misc.latex.pretty_print_default(false);_;__IP.outputcache()", Cantor::Expression::DeleteOnFinish);
++ evaluateExpression("sage.misc.latex.pretty_print_default(false)", Cantor::Expression::DeleteOnFinish);
+ }
+
+ Cantor::CompletionObject* SageSession::completionFor(const QString& command, int index)
diff --git a/cantor.spec b/cantor.spec
index ac81327..6f86814 100644
--- a/cantor.spec
+++ b/cantor.spec
@@ -1,7 +1,7 @@
Name: cantor
Summary: KDE Frontend to Mathematical Software
Version: 4.10.3
-Release: 1%{?dist}
+Release: 2%{?dist}
License: GPLv2+
URL: https://projects.kde.org/projects/kde/kdeedu/cantor
@@ -13,6 +13,9 @@ URL: https://projects.kde.org/projects/kde/kdeedu/cantor
%endif
Source0: http://download.kde.org/%{stable}/%{version}/src/%{name}-%{version}.tar.xz
+# fix SAGE backend for SAGE 5.8 (kde#316299), preliminary patch from upstream
+Patch0: cantor-4.10.3-sage58.patch
+
## upstream patches
BuildRequires: analitza-devel >= %{version}
@@ -58,6 +61,7 @@ Requires: kdelibs4-devel
%prep
%setup -q
+%patch0 -p1 -b .sage58
%build
@@ -136,6 +140,9 @@ fi
%changelog
+* Sun May 26 2013 Kevin Kofler - 4.10.3-2
+- fix SAGE backend for SAGE 5.8 (kde#316299), patch from upstream bugs.kde.org
+
* Mon May 06 2013 Than Ngo - 4.10.3-1
- 4.10.3