Hello,

while testing PyKDE4's modules with Python3, I noticed that the i18n() calls, 
which accept const char*, are not properly handled in Python 3 (both with API 
version 1 and 2):

In [2]: from PyKDE4.kdecore import i18n

In [3]: i18n("Test")
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-00970cb5d435> in <module>()
----> 1 i18n("Test")

TypeError: i18n(): argument 1 has unexpected type 'str'

It does work if I pass bytes, but that's not correct, as we're dealing with 
strings here.

PyQt documentation states that char* or const char* would be acceptable with 
strings (Python 3 strings). 

As the original SIP file had custom code inside, I'm attaching it here. I 
looked at it briefly, but I couldn't find why it didn't work.

Can anyone shed a light on this issue?


-- 
Luca Beltrame - KDE Forums team
KDE Science supporter
GPG key ID: 6E1A4E79
//
//     Copyright 2008 Jim Bublitz <jbubl...@nwinternet.com>
//     Earlier copyrights 1998 - 2007 Jim Bublitz also apply

//                 Generated by twine

// This file is part of PyKDE4.

// PyKDE4 is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of
// the License, or (at your option) any later version.

// PyKDE4 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.


class KLocalizedString
{
%TypeHeaderCode
#include <klocalizedstring.h>
%End


public:
    explicit                KLocalizedString ();
                            KLocalizedString (const KLocalizedString& rhs);
    QString                 toString () const;
    QString                 toString (const KLocale* locale) const;
    bool                    isEmpty () const;
//ig    KLocalizedString        subs (int a, int fieldWidth = 0, int base = 10, const QChar& fillChar = QLatin1Char(' ')) const;
//ig    KLocalizedString        subs (uint a, int fieldWidth = 0, int base = 10, const QChar& fillChar = QLatin1Char(' ')) const;
//ig    KLocalizedString        subs (long a, int fieldWidth = 0, int base = 10, const QChar& fillChar = QLatin1Char(' ')) const;
//ig    KLocalizedString        subs (ulong a, int fieldWidth = 0, int base = 10, const QChar& fillChar = QLatin1Char(' ')) const;
    KLocalizedString        subs (qlonglong a, int fieldWidth = 0, int base = 10, const QChar& fillChar = QLatin1Char(' ')) const;
//ig    KLocalizedString        subs (qulonglong a, int fieldWidth = 0, int base = 10, const QChar& fillChar = QLatin1Char(' ')) const;
//ig    KLocalizedString        subs (double a, int fieldWidth = 0, char format = 'g', int precision = -1, const QChar& fillChar = QLatin1Char(' ')) const;
    KLocalizedString        subs (QChar a, int fieldWidth = 0, const QChar& fillChar = QLatin1Char(' ')) const;
    KLocalizedString        subs (const QString& a, int fieldWidth = 0, const QChar& fillChar = QLatin1Char(' ')) const;
    KLocalizedString        inContext (const QString& key, const QString& text) const;
//ig    static void             notifyCatalogsUpdated (const QStringList& languages, const QList<KCatalogName>& catalogs);

private:
                            KLocalizedString (const char* ctxt, const char* msg, const char* plural);
public:
    ~KLocalizedString ();
    QString                 toString (const QString& catalogName) const;
    QString                 toString (const KLocale* locale, const QString& catalogName) const;
};
// KLocalizedString

KLocalizedString        ki18n (const char* msg);
KLocalizedString        ki18nc (const char* ctxt, const char* msg);
KLocalizedString        ki18np (const char* singular, const char* plural);
KLocalizedString        ki18ncp (const char* ctxt, const char* singular, const char* plural);
QString                 tr2i18n (const char* message, const char* comment = 0);

//ig class I18nTypeCheck;

%ModuleCode
QString klocalizedstring_i18n_template(KLocalizedString base, PyObject *list,int *sipIsErr) {
    KLocalizedString result = base;    
    QString *arg;
    long long_arg;
    double double_arg;
    int iserr = 0;

    for (int i=0; i < PyTuple_Size(list); i++) {
        PyObject *pyarg = PyTuple_GET_ITEM (list, i);
#if PY_MAJOR_VERSION >= 3
        if (PyLong_Check(pyarg)) {
            long_arg = PyLong_AsLong(pyarg);
#else
        if (PyInt_Check(pyarg)) {
            long_arg = PyInt_AsLong(pyarg);
#endif            
            if (long_arg==-1 && PyErr_Occurred()) {
                *sipIsErr = 1;
                return QString();
            }
            result = result.subs(long_arg);

#if PY_MAJOR_VERSION >= 3
        } else if (PyNumber_Check(pyarg)) {
            PyObject *long_py = PyNumber_Long(pyarg);
            long_arg = PyLong_AsLong(long_py);
            Py_DECREF(long_py);
#else
        } else if (PyLong_Check(pyarg)) {
            long_arg = PyLong_AsLong(pyarg);
#endif
            if (long_arg==-1 && PyErr_Occurred()) {
                *sipIsErr = 1;
                return QString();
            }
            result = result.subs(long_arg);

        } else if (PyFloat_Check(pyarg)) {
            double_arg = PyFloat_AsDouble(pyarg);
            result = result.subs(double_arg);

        } else {
          int state = 0;
          arg = (QString *)sipForceConvertToType(pyarg, sipType_QString, NULL, SIP_NOT_NONE, &state, &iserr);
          if (iserr) {
              *sipIsErr = 1;
              return QString();
          }
  
          result = result.subs(*arg);
          sipReleaseType(arg,sipType_QString,state);
          arg = 0;
          }
    }

    return result.toString();
}
%End

//ig class I18nTypeCheck;

//force
QString                 i18n (const char* text, ...);
%MethodCode
    QString result = klocalizedstring_i18n_template(ki18n(a0),a1,&sipIsErr);
    if (!sipIsErr) {
        sipRes = new QString(result);
    }
%End

QString                 i18nc (const char* ctxt, const char* text, ...);
%MethodCode
    QString result = klocalizedstring_i18n_template(ki18nc(a0,a1),a2,&sipIsErr);
    if (!sipIsErr) {
        sipRes = new QString(result);
    }
%End

QString                 i18np (const char* sing, const char* plur, ...);
%MethodCode
    QString result = klocalizedstring_i18n_template(ki18np(a0,a1),a2,&sipIsErr);
    if (!sipIsErr) {
        sipRes = new QString(result);
    }
%End

QString                 i18ncp (const char* ctxt, const char* sing, const char* plur, ...);
%MethodCode
    QString result = klocalizedstring_i18n_template(ki18ncp(a0,a1,a2),a3,&sipIsErr);
    if (!sipIsErr) {
        sipRes = new QString(result);
    }
%End
//end
%ModuleHeaderCode
#include <klocalizedstring.h>
%End


//ig QString                 i18n (const char* text);
//ig QString                 i18nc (const char* ctxt, const char* text);

Attachment: signature.asc
Description: This is a digitally signed message part.

_______________________________________________
PyQt mailing list    PyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

Reply via email to