[PyQt] Problem with conversion of QString to str for Python 3.3 and higher (PEP 393)

Mathias.Born at gmx.de Mathias.Born at gmx.de
Sat Feb 1 21:15:44 GMT 2014


Hi,

Since upgrading to Python 3.3/3.4 I have a new problem.

Run this small program in a debug version of Python 3.3 or higher on Windows:

from PyQt5.QtCore import *
o = QObject()
o.setObjectName('abc')
b = o.objectName()
print("test %s" % b)

The last line will make Python abort with the message:

Assertion failed: maxchar >= 128, file ..\Objects\unicodeobject.c, line 403

This doesn't apply to the release version.

The assert comes from function _PyUnicode_CheckConsistency in unicodeobject.c,
which only exists in the debug version.
It is the "assert(maxchar >= 128);" line in that function:

        if (kind == PyUnicode_1BYTE_KIND) {
            if (ascii->state.ascii == 0) {
                assert(maxchar >= 128);
                assert(maxchar <= 255);
            }

which fails.

I believe this may be caused by PyQt not constructing the Python string
correctly in the "o.objectName()" part of the above script.

In PyQt-file qpy\QtCore\qpycore_qstring.cpp, in function
qpycore_PyObject_FromQString, I see that the Python string is created by

obj = PyUnicode_New(py_len, 0x00ff)

The 0x00ff is the maximum character code occuring in the string.
However, the string in the failing example is pure ASCII.
I'm not familiar with Python's unicode internals, but to me it looks
like the code expects this fact to be represented by "ascii->state.ascii == 1",
not "ascii->state.ascii == 0", which as it seems must be taken care of
during creation.

Best Regards,
Mathias



More information about the PyQt mailing list