PySide Bugzilla Closed for New Bugs

PySide is now a Qt Add-on and uses the Qt Project's JIRA Bug Tracker instead of this Bugzilla instance. This Bugzilla is left for reference purposes.

Bug 1069 - QtCore.QDataStream silently fails on writing Python string
: QtCore.QDataStream silently fails on writing Python string
Status: CLOSED FIXED
Product: PySide
Classification: Unclassified
Component: QtCore
: 1.0.8
: PC MS Windows XP/Vista/7
: P3 normal
Assigned To: Paulo Alcantara
:
:
:
  Show dependency treegraph
 
Reported: 2011-11-25 15:36 EET by Vladimir Rutsky
Modified: 2012-01-02 21:43 EET (History)
10 users (show)

See Also:


Attachments
example for this bug (1.38 KB, text/x-python)
2011-11-25 15:36 EET, Vladimir Rutsky
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Vladimir Rutsky 2011-11-25 15:36:53 EET
Created attachment 455 [details]
example for this bug

QDataStream allows to write Python string (str) into it:

    ba = QByteArray()
    stream = QDataStream(ba, QIODevice.WriteOnly)
    stream << "hello"

After that `ba' contains null-bytes:
    print [c for c in ba]
['\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
'\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00']

So following read of string/QByteArray silently fails:

    read_stream = QDataStream(ba, QIODevice.ReadOnly)
    res = ""
    read_stream >> res

res remains equal to empty string.

Please see attached example based on
pyside-git\tests\QtCore\qdatastream_test.py unit-test.

QtCore.QDataStream should support `<< str(...)' operation (like PyQt does) or
at least give user warning/error when someone tries to do such unsupported
operation.

P.S. As workaround I saw in unit-test that working way of passing string
through QDataStream is to wrap all string items in QByteArray:

    ba = QByteArray()
    stream = QDataStream(ba, QIODevice.WriteOnly)
    stream << QByteArray("hello")

    read_stream = QDataStream(ba, QIODevice.ReadOnly)
    res = QByteArray()
    read_stream >> res
Comment 1 Hugo Parente Lima 2011-11-25 21:22:50 EET
operator>>(string) can't work on Python because Python strings are immutable
objects, so use readString instead.

So a fix for this bug would be remove the >> operator for strings/
Comment 2 Paulo Alcantara 2011-11-30 01:11:12 EET
Hi,

Thanks for the report!

Fixed on PySide commit e20060d9595473f5c325c25d8d903e18be7f3d5d.