[Kde-bindings] [Bug 209046] krosspython: UTF-8 python strings are encoded as ASCII when converted to QString

Daniel Calviño Sánchez danxuliu at gmail.com
Wed Sep 30 22:42:48 UTC 2009


https://bugs.kde.org/show_bug.cgi?id=209046





--- Comment #2 from Daniel Calviño Sánchez <danxuliu gmail com>  2009-10-01 00:42:47 ---
Created an attachment (id=37272)
 --> (http://bugs.kde.org/attachment.cgi?id=37272)
Patch for pythonvariant.h with a workaround to the bug

Here it is a workaround for the bug. I am not familiar with Kross code, so it
may be a better way to do it. The problem was that
Py::String(obj).as_string().c_str(); returned the const char* data of a
std::string, and therefore the UTF-8 characters were split in several C++ char.
As toVariant(const Py::Object& obj) (the method the sentence belongs to)
returns a QString, the const char* was "casted" to QString using QString(const
char * str) constructor, which expects an ASCII string and thus ignores that
there are multibyte characters.

In order to initialize a QString from a UTF-8 const char* taking into account
multibyte characters, static method QString::fromUtf8(const char*) has to be
used. As 7 bit characters in UTF-8 are the same as in ASCII, this keeps
backwards compatibility with already existing code that used ASCII.

Of course, when a different encoding than UTF-8 is used in Python strings the
same problem of bad encoded QStrings arises again, although personally I think
that it is pretty safe to assume that the majority of strings will be encoded
in UTF-8.

-- 
Configure bugmail: https://bugs.kde.org/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.


More information about the Kde-bindings mailing list