Blame pykde4-4.11.2-pythonpluginfactory_python3.patch

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