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 883 - Cannot use operator<< on QDataStream and Python integer
: Cannot use operator<< on QDataStream and Python integer
Status: CLOSED INVALID
Product: PySide
Classification: Unclassified
Component: Shiboken
: HEAD
: PC Linux
: P5 enhancement
Assigned To: Marcelo Lira
:
:
:
  Show dependency treegraph
 
Reported: 2011-06-13 15:08 EEST by Thomas Perl
Modified: 2011-06-22 20:25 EEST (History)
8 users (show)

See Also:


Attachments
encoder.cpp - Working C++ example (307 bytes, text/x-c++src)
2011-06-13 15:11 EEST, Thomas Perl
Details
encoder.pro - qmake project file for use with encoder.cpp (40 bytes, application/vnd.nokia.qt.qmakeprofile)
2011-06-13 15:11 EEST, Thomas Perl
Details
encoder.py - Python/PySide example (198 bytes, text/x-python)
2011-06-13 15:13 EEST, Thomas Perl
Details
Example for proper usage (515 bytes, text/x-python)
2011-06-14 11:24 EEST, Thomas Perl
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Thomas Perl 2011-06-13 15:08:53 EEST
For mimicking the encoding done by MRemoteAction::toString(), I need to encode
a QVariant in a special way, as seen here:

http://apidocs.meego.com/1.0/mtf/mremoteaction_8cpp_source.html

In C++, everything works as expected, because I can box an int into a QVariant
and use operator<< on this. However, using PySide, this is not possible:

Traceback (most recent call last):
  File "encoder.py", line 7, in <module>
    stream << 10
TypeError: 'PySide.QtCore.QDataStream.__lshift__' called with wrong argument
types:
  PySide.QtCore.QDataStream.__lshift__(int)
Supported signatures:
  PySide.QtCore.QDataStream.__lshift__(PySide.QtCore.QBitArray)
  PySide.QtCore.QDataStream.__lshift__(PySide.QtCore.QByteArray)
  PySide.QtCore.QDataStream.__lshift__(PySide.QtCore.QDate)
  PySide.QtCore.QDataStream.__lshift__(PySide.QtCore.QDateTime)
  PySide.QtCore.QDataStream.__lshift__(PySide.QtCore.QEasingCurve)
  PySide.QtCore.QDataStream.__lshift__(PySide.QtCore.QLine)
  PySide.QtCore.QDataStream.__lshift__(PySide.QtCore.QLineF)
  PySide.QtCore.QDataStream.__lshift__(PySide.QtCore.QLocale)
  PySide.QtCore.QDataStream.__lshift__(PySide.QtCore.QPoint)
  PySide.QtCore.QDataStream.__lshift__(PySide.QtCore.QPointF)
  PySide.QtCore.QDataStream.__lshift__(PySide.QtCore.QRect)
  PySide.QtCore.QDataStream.__lshift__(PySide.QtCore.QRectF)
  PySide.QtCore.QDataStream.__lshift__(PySide.QtCore.QRegExp)
  PySide.QtCore.QDataStream.__lshift__(PySide.QtCore.QSize)
  PySide.QtCore.QDataStream.__lshift__(PySide.QtCore.QSizeF)
  PySide.QtCore.QDataStream.__lshift__(PySide.QtCore.QTime)
  PySide.QtCore.QDataStream.__lshift__(PySide.QtCore.QUrl)
  PySide.QtCore.QDataStream.__lshift__(PySide.QtCore.QUuid)


I will attach a working C++ example and the non-working Python example.
Comment 1 Thomas Perl 2011-06-13 15:11:18 EEST
Created attachment 341 [details]
encoder.cpp - Working C++ example
Comment 2 Thomas Perl 2011-06-13 15:11:40 EEST
Created attachment 342 [details]
encoder.pro - qmake project file for use with encoder.cpp
Comment 3 Thomas Perl 2011-06-13 15:13:42 EEST
Created attachment 343 [details]
encoder.py - Python/PySide example
Comment 4 Hugo Parente Lima 2011-06-13 22:02:11 EEST
Those function were removed because Python doesn't provide the same information
about the type as C++ does, e.g. if you do:

s = QDataStream(...)
s << 2

We don't know if you wanted to write a short, int, or long, and the choice
makes different here because this is a binary stream, so we provide the
following functions:

 - writeBool(value)
 - writeDouble(value)
 - writeFloat(value)
 - writeInt16(value)
 - writeInt32(value)
 - writeInt64(value)
 - writeInt8(value)
 - writeUInt16(value)
 - writeUInt32(value)
 - writeUInt64(value)
 - writeUInt8(value)

Use them instead of operator<< to write numbers to data streams.

Regards.
Comment 5 Thomas Perl 2011-06-14 11:14:05 EEST
(In reply to comment #4)
> We don't know if you wanted to write a short, int, or long, and the choice
> makes different here because this is a binary stream, so we provide the
> following functions:

Ok, thanks - that can work for me. Now, for reading from a stream, the
serialized format of QVariant contains the type information(?). Shouldn't it be
possible to get "the right value" using something like stream.readQVariant()?
In my test (PySide 1.0.3) I wasn't able to do so:

from PySide.QtCore import *

buffer = QBuffer()
buffer.open(QIODevice.ReadWrite)
stream = QDataStream(buffer)
stream.writeUInt64(10L)
buffer.close()
print 'encoded:', repr(buffer.buffer().toBase64().data())

data = buffer.buffer().toBase64().data()
byteArray = QByteArray.fromBase64(data)
buffer = QBuffer(byteArray)
buffer.open(QIODevice.ReadOnly)
stream = QDataStream(buffer)
#print repr(stream.readUInt64())   # <- this works as expected
print repr(stream.readQVariant())  # <- returns None
buffer.close()

(this is to implement the fromString method in
http://apidocs.meego.com/1.0/mtf/mremoteaction_8cpp_source.html)

Should I file a new bug about this or am I just using the API in a wrong way?
Comment 6 Thomas Perl 2011-06-14 11:23:06 EEST
(In reply to comment #5)
> (In reply to comment #4)
> > We don't know if you wanted to write a short, int, or long, and the choice
> > makes different here because this is a binary stream, so we provide the
> > following functions:
> 
> Ok, thanks - that can work for me. Now, for reading from a stream, the
> serialized format of QVariant contains the type information(?). Shouldn't it be
> possible to get "the right value" using something like stream.readQVariant()?
> In my test (PySide 1.0.3) I wasn't able to do so:

Please ignore the last comment. I just figured out how to use it:

- stream.writeQVariant
- stream.readQVariant

It seems to also take care of doing proper type detection (i.e. both
writeQVariant(10) and writeQVariant('hi') do the right thing). Now, if that's
possible, wouldn't it be possible to use writeQVariant and readQVariant for the
operators << and >> or at least make the error message so that it's kind of
obvious (or emit a warning "Warning: Using QVariant to serialize this value. If
you want to have a special data type, please use the write* functions).
Comment 7 Thomas Perl 2011-06-14 11:24:29 EEST
Created attachment 344 [details]
Example for proper usage

Attaching an example for other developers on how to use this just in case this
bug comes up in a search for somebody from the future.
Comment 8 renato filho 2011-06-22 20:25:13 EEST
release 1.0.4