[PyQt] sipForceConvertToType and Unicode in PyKDE4

Phil Thompson phil at riverbankcomputing.com
Mon May 6 22:22:00 BST 2013


On Mon, 06 May 2013 20:20:11 +0200, Luca Beltrame
<einar at heavensinferno.net> wrote:
> Hello,                                                                  

>    
> 
> recently it turned out that KLocalizedString in PyKDE4 is not working
> properly
> in Python 3 and Python 2. The code path hasn't changed since 2008.      

>    
> 
> What it looks like is that unicode strings are passed with "ascii", then
> encoded with utf-8. In Py2, if this process fails fails, the object gets
> passed as bytes, and works.In Py3, it doesn't work because encoding with
> ascii
> raises and exception. It only works if the object is passed in bytes.
> 
> IOW, unicode-containing strings that weren't manually encoded to utf-8
byte
> strings will not work. (analysis here:
> https://gist.github.com/flying-sheep/5520997 )
> 
> Ultimately, this is what the code does in PyKDE4:
> 
> KLocalizedString result = base;
> QString *arg;                                                           

>    
> int iserr = 0;                                                          

>    
> 
> 
> for (int i=0; i < PyTuple_Size(list); i++) {                            

>    
>      PyObject *pyarg = PyTuple_GET_ITEM (list, i);
> 
> // ... further down in the code  
> 
>    }
> 
>     result = result.subs(*arg);
>     sipReleaseType(arg,sipType_QString,state);
>     arg = 0;
> }                                                                       

>    
> 
> return result.toString();                                               

>    
> 
> "base" is a  KlocalizedString passed to the function, and so is
sipIsErr.
> *list is a PyObject.                                                    

>    
> 
> At least from the initial investigation, the error seems to fall here in
> this 
> code. Phil, where should I look to debug this problem further? Or are
there
> alternatives to debug these conversions so I can find the real culprit?

I don't think the above code has anything to do with it as the tuple will
always be empty in your tests.

You should look at the first argument to i18n() and read up on the
/Encoding/ argument annotation...

http://pyqt.sourceforge.net/Docs/sip4/annotations.html#argument-annotations

Note that with Python2 PyQt sets the default encoding to "None" and with
Python3 to "ASCII". I don't know if PyKDE changes the default. Python3
needs to know the correct encoding of every char/char * argument - maybe
nobody went through PyKDE to check these when Python3 support was added.

Phil


More information about the PyQt mailing list