[PyKDE] QString -> python string via QString::latin1()

Phil Thompson phil at riverbankcomputing.co.uk
Thu Oct 6 20:26:00 BST 2005


On Thursday 06 October 2005 7:17 pm, Nigel Stewart wrote:
> Hello all,
>
> I came across something unexpected recently,
> that was causing us occasional python
>
> exceptions:
>  >> print type(qt.QString("foo").latin1())
>
> <type 'str'>
>
>  >> print type(qt.QString("").latin1())
>
> <type 'str'>
>
>  >> print type(qt.QString.null.latin1())
>
> <type 'NoneType'>
>
>  >> print type(qt.QString().latin1())
>
> <type 'NoneType'>
>
> The type of result returned by the PyQt
> binding for QString::latin1() depends
> on whether the string is empty or null.
>
> Contrast this with the PyQt binding for
>
> the str() converter:
>  >> print type(str(qt.QString("foo")))
>
> <type 'str'>
>
>  >> print type(str(qt.QString("")))
>
> <type 'str'>
>
>  >> print type(str(qt.QString.null))
>
> <type 'str'>
>
>  >> print type(str(qt.QString()))
>
> <type 'str'>
>
> In this case, conversion to a python
> string always succeeds, and never
> results in a NoneType.  PyQt is checking
> for the QString::null in this case,
> but for latin1() is defaulting to
> the behaviour of PyString_FromString:
>
> PyObject *PyString_FromString(const char *v)
>    Return value: New reference.
>    Returns a new string object with the value v
>    on success, and NULL on failure. The parameter
>    v must not be NULL; it will not be checked.
>
> Overall, a bug, or a feature?

Feature.

> I would certainly 
> prefer that PyQt always returned a python string,
> treating QString::isNull() and QString::isEmpty()
> as the same case.

Null and empty QStrings are different and the PyQt behaviour reflects the Qt 
behaviour.

> I'm interested in the broader 
> PyKde/PyQt opinion, is it "pythonic" to return
> different types of objects, depending on the
> input?

str() is defined to return a string object. If there is a bug then it's that 
str() of a null string should raise an exception. I'd be willing to implement 
that for PyQt4

Phil




More information about the PyQt mailing list