Blob Blame Raw
diff -up pykde4-4.11.2/CMakeLists.txt.207 pykde4-4.11.2/CMakeLists.txt
--- pykde4-4.11.2/CMakeLists.txt.207	2013-10-16 14:59:36.027327310 -0500
+++ pykde4-4.11.2/CMakeLists.txt	2013-10-16 15:00:13.068628265 -0500
@@ -283,7 +283,9 @@ install(DIRECTORY sip/ DESTINATION ${SIP
 add_subdirectory(tools)
 #add_subdirectory(docs)
 add_subdirectory(examples)
-if (PYTHON_VERSION_MAJOR LESS 3)
+# Due to version specific ABI tagging in Python 3 so files, we can build for
+# all Python 3 versions without a problem.
+if ((PYTHON_VERSION_MAJOR LESS 3 AND DEFAULT_PYTHON_VERSION) OR PYTHON_VERSION_MAJOR GREATER 2)
     add_subdirectory(kpythonpluginfactory)
 endif ()
 
diff -up pykde4-4.11.2/kpythonpluginfactory/kpythonpluginfactory.cpp.207 pykde4-4.11.2/kpythonpluginfactory/kpythonpluginfactory.cpp
--- pykde4-4.11.2/kpythonpluginfactory/kpythonpluginfactory.cpp.207	2013-10-16 14:59:36.023327383 -0500
+++ pykde4-4.11.2/kpythonpluginfactory/kpythonpluginfactory.cpp	2013-10-16 14:59:36.026327328 -0500
@@ -29,6 +29,10 @@
 #include <kcomponentdata.h>
 #include <kdebug.h>
 
+#if PY_MAJOR_VERSION >= 3
+#define PY3
+#endif
+
 /*
 This implements a plugin factory for running Python plugins. It also
 supports io-slaves with a kdemain() entry point.
@@ -347,17 +351,48 @@ int kdemain( int argc, char **argv )
     PyObject *pModule;
     char *protocol = argv[1];
 
+#ifdef PY3
+    /* Python 3 requires wchar_t*s for its Py_SetProgramName() and
+       PySys_SetArgv() calls.  Python 2 uses the typical char*s.  This is
+       probably not the best way to do it, but the algorithm is based on
+       Python 3's main().
+    */
+    wchar_t **program_args = (wchar_t **)PyMem_Malloc(
+        sizeof(wchar_t *) * (argc + 1));
+    char *old_locale;
+    const char *argv_i;
+
+    if (!program_args) {
+        /* out of memory */
+        Py_FatalError("out of memory");
+    }
+    for (int i = 0; i < argc; i++) {
+        program_args[i] = (wchar_t *)PyMem_Malloc(
+            sizeof(wchar_t) * (strlen(argv[i]) + 1));
+        if (!program_args[i]) {
+            Py_FatalError("out of memory");
+        }
+        argv_i = argv[i];
+        if (mbsrtowcs(program_args[i], &argv_i, strlen(argv[i]), NULL) < 0) {
+            /* The conversion failed. */
+            Py_FatalError("conversion to wchar_t* failed");
+        }
+    }
+#else
+    char **program_args = argv;
+#endif  // PY3
+
     kDebug() << "Python kioslave starting";
     KComponentData slave(protocol);
     kDebug() << "Created KComponentData for protocol " << protocol;
 
     QLibrary *pyLib = LoadPythonLibrary();
 
-    Py_SetProgramName(argv[0]);
+    Py_SetProgramName(program_args[0]);
     Py_Initialize();
 
     //PyEval_InitThreads();
-    PySys_SetArgv(1, argv);
+    PySys_SetArgv(1, program_args);
 
     QString completePath = KStandardDirs::locate("data", QString("kio_python/%1/%2.py").arg(protocol).arg(protocol));
     kDebug() << "Path to Python kioslace is " << completePath;
@@ -388,8 +423,8 @@ int kdemain( int argc, char **argv )
     }
     PyObject *pClass, *pArgs, *pArg1, *pArg2;
     pArgs = PyTuple_New(2);
-    pArg1 = PyString_FromString(argv[2]);
-    pArg2 = PyString_FromString(argv[3]);
+    pArg1 = PyBytes_FromString(argv[2]);
+    pArg2 = PyBytes_FromString(argv[3]);
     PyTuple_SetItem(pArgs, 0, pArg1);
     PyTuple_SetItem(pArgs, 1, pArg2);
     RunFunction(factoryFunction, pArgs);