Blame pykde4-4.11.2-pythonpluginfactory_python3.patch

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