Enable kpythonpluginfactory to build for Python 3 as well. Should be upstreamed after more testing. Note: It is OK for the Python and Python3 versions to have the same name because of the version specific ABI tags in Python 3 so files and the fact that Python 3 interpreters will search for those first. Copyright (C) 2012 Barry A. Warsaw Index: pykde4-4.13.97/kpythonpluginfactory/kpythonpluginfactory.cpp =================================================================== --- kpythonpluginfactory/kpythonpluginfactory.cpp.orig +++ kpythonpluginfactory/kpythonpluginfactory.cpp @@ -29,6 +29,10 @@ #include #include +#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); Index: pykde4-4.13.97/CMakeLists.txt =================================================================== --- CMakeLists.txt.orig +++ CMakeLists.txt @@ -275,8 +275,10 @@ endif(DEFAULT_PYTHON_VERSION) add_subdirectory(tools) #add_subdirectory(docs) add_subdirectory(examples) -if (PYTHON_VERSION_MAJOR LESS 3 AND DEFAULT_PYTHON_VERSION) +# 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 (PYTHON_VERSION_MAJOR LESS 3 AND DEFAULT_PYTHON_VERSION) +endif ((PYTHON_VERSION_MAJOR LESS 3 AND DEFAULT_PYTHON_VERSION) OR PYTHON_VERSION_MAJOR GREATER 2) feature_summary(WHAT ALL FATAL_ON_MISSING_REQUIRED_PACKAGES)