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

Nigel Stewart ns at fluent.com
Thu Oct 6 19:17:35 BST 2005


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?  I would certainly
prefer that PyQt always returned a python string,
treating QString::isNull() and QString::isEmpty()
as the same case.  I'm interested in the broader
PyKde/PyQt opinion, is it "pythonic" to return
different types of objects, depending on the
input?

I'm using Python 2.4, Sip 4.1.1 and PyQt 3.13.

Regards,

Nigel Stewart




More information about the PyQt mailing list