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 446 - QTcpSocket.readData(str, int) crashes interpreter
: QTcpSocket.readData(str, int) crashes interpreter
Status: CLOSED FIXED
Product: PySide
Classification: Unclassified
Component: QtNetwork
: HEAD
: PC MS Windows XP/Vista/7
: P3 critical
Assigned To: renato filho
:
:
:
  Show dependency treegraph
 
Reported: 2010-10-31 11:30 EET by Nikolay Saiko
Modified: 2010-11-25 17:48 EET (History)
8 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Nikolay Saiko 2010-10-31 11:30:12 EET
in help on QAbstractSocket (PySide/QtNetwork/QAbstractSocket.html), section
"Detailed description" I see something totally unpytonic:

numRead = 0
numReadTotal = 0
buffer = ""
while(True):
    numRead  = socket.read(buffer, 50)
    # do whatever with array
    numReadTotal += numRead
    if (numRead == 0 && !socket.waitForReadyRead()):
        break

1) type of buffer is str - so it is unmutable. So there is now way it can be
used here:
 - if buffer is changed inside socket.read(), it violates Python`s principle of
immutabiliti of srting. (also it could cause a crash)
 - if buffer is not changing inside - there is no sense of putting it as
argument ;)
I would suggest using Python`s native bytearray type here as buffer, if
possible, because it is mutable.

2) PySide tells me that socket.read takes only one argument:
  QByteArray read(long long)
so this example is incorrect.
But if you change 'read' to 'readData', it will work (but interpretator will
crush after closing application)
Comment 1 Nikolay Saiko 2010-11-03 15:02:34 EET
Reformulating what was said above:
definiton of QTcpSocket.readData(str, int)->None is invalid, because str is
immutable (so its not possible to use it as buffer for result).
look at PyQt: QTcpSocket.readData(int)->str. That definition is much more
pythonic, and it works correctly.

Also, about QTcpSocket: 
  PySide`s code: QTcpSocket.writeData(str, int)
  PyQt`s code:   QTcpSocket.writeData(str)
in PySide`s variant 2dn param is always = len(first_param), because str already
knows it`s length (which is impossible for char* in C)... Maybe that parameter
should be removed here?
Comment 2 renato filho 2010-11-19 20:37:01 EET
Hi Nikoaly

Thanks for you report. I will fix the documentation about the read function.

The problem is only with documentation, I created a small unit test to check if
the function is working fine, you can check this on the link[1] above.

The result of the read function is a QByteArray and you can use that to get the
size returned.

I think the signature: PySide`s code: QTcpSocket.writeData(str, int)
is the correct, because you can choose to send only a small piece of the
buffer. You can control that using the int parameter. But we can discuss this
on pyside ML.

Thanks

[1]http://qt.gitorious.org/pyside/pyside/blobs/master/tests/QtNetwork/bug_446.py
Comment 3 renato filho 2010-11-19 20:37:42 EET
fixed documentaion on pyside commit:

commit ff7f570599f88b022dffa9e80d9b28c2933cc053
Author: Renato Araujo Oliveira Filho <renato.filho@openbossa.org>
Date:   Fri Nov 19 20:18:55 2010 -0300


This will published on next release
Comment 4 renato filho 2010-11-25 17:48:19 EET
released on 1.0.0~beta1