Ticket #47125: pythonpluginfactory_python3.diff

File pythonpluginfactory_python3.diff, 3.7 KB (added by RJVB (René Bertin), 9 years ago)
  • kpythonpluginfactory/kpythonpluginfactory.cpp

    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 <barry@python.org>
    old new  
    2929#include <kcomponentdata.h>
    3030#include <kdebug.h>
    3131
     32#if PY_MAJOR_VERSION >= 3
     33#define PY3
     34#endif
     35
    3236/*
    3337This implements a plugin factory for running Python plugins. It also
    3438supports io-slaves with a kdemain() entry point.
    int kdemain( int argc, char **argv ) 
    347351    PyObject *pModule;
    348352    char *protocol = argv[1];
    349353
     354#ifdef PY3
     355    /* Python 3 requires wchar_t*s for its Py_SetProgramName() and
     356       PySys_SetArgv() calls.  Python 2 uses the typical char*s.  This is
     357       probably not the best way to do it, but the algorithm is based on
     358       Python 3's main().
     359    */
     360    wchar_t **program_args = (wchar_t **)PyMem_Malloc(
     361        sizeof(wchar_t *) * (argc + 1));
     362    char *old_locale;
     363    const char *argv_i;
     364
     365    if (!program_args) {
     366        /* out of memory */
     367        Py_FatalError("out of memory");
     368    }
     369    for (int i = 0; i < argc; i++) {
     370        program_args[i] = (wchar_t *)PyMem_Malloc(
     371            sizeof(wchar_t) * (strlen(argv[i]) + 1));
     372        if (!program_args[i]) {
     373            Py_FatalError("out of memory");
     374        }
     375        argv_i = argv[i];
     376        if (mbsrtowcs(program_args[i], &argv_i, strlen(argv[i]), NULL) < 0) {
     377            /* The conversion failed. */
     378            Py_FatalError("conversion to wchar_t* failed");
     379        }
     380    }
     381#else
     382    char **program_args = argv;
     383#endif  // PY3
     384
    350385    kDebug() << "Python kioslave starting";
    351386    KComponentData slave(protocol);
    352387    kDebug() << "Created KComponentData for protocol " << protocol;
    353388
    354389    QLibrary *pyLib = LoadPythonLibrary();
    355390
    356     Py_SetProgramName(argv[0]);
     391    Py_SetProgramName(program_args[0]);
    357392    Py_Initialize();
    358393
    359394    //PyEval_InitThreads();
    360     PySys_SetArgv(1, argv);
     395    PySys_SetArgv(1, program_args);
    361396
    362397    QString completePath = KStandardDirs::locate("data", QString("kio_python/%1/%2.py").arg(protocol).arg(protocol));
    363398    kDebug() << "Path to Python kioslace is " << completePath;
    int kdemain( int argc, char **argv ) 
    388423    }
    389424    PyObject *pClass, *pArgs, *pArg1, *pArg2;
    390425    pArgs = PyTuple_New(2);
    391     pArg1 = PyString_FromString(argv[2]);
    392     pArg2 = PyString_FromString(argv[3]);
     426    pArg1 = PyBytes_FromString(argv[2]);
     427    pArg2 = PyBytes_FromString(argv[3]);
    393428    PyTuple_SetItem(pArgs, 0, pArg1);
    394429    PyTuple_SetItem(pArgs, 1, pArg2);
    395430    RunFunction(factoryFunction, pArgs);
  • CMakeLists.txt

    old new endif(DEFAULT_PYTHON_VERSION) 
    275275add_subdirectory(tools)
    276276#add_subdirectory(docs)
    277277add_subdirectory(examples)
    278 if (PYTHON_VERSION_MAJOR LESS 3 AND DEFAULT_PYTHON_VERSION)
     278# Due to version specific ABI tagging in Python 3 so files, we can build for
     279# all Python 3 versions without a problem.
     280if ((PYTHON_VERSION_MAJOR LESS 3 AND DEFAULT_PYTHON_VERSION) OR PYTHON_VERSION_MAJOR GREATER 2)
    279281    add_subdirectory(kpythonpluginfactory)
    280 endif (PYTHON_VERSION_MAJOR LESS 3 AND DEFAULT_PYTHON_VERSION)
     282endif ((PYTHON_VERSION_MAJOR LESS 3 AND DEFAULT_PYTHON_VERSION) OR PYTHON_VERSION_MAJOR GREATER 2)
    281283
    282284feature_summary(WHAT ALL FATAL_ON_MISSING_REQUIRED_PACKAGES)