question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[PySide2] QObject.sender() not working

See original GitHub issue

0.6.13rc6 (Factory) Python: 3.8.5 (default, Jul 28 2020, 12:59:40) OS: Linux | Arch: x86_64

QObject.sender() is returning “None” instead of a pointer to the object which sent the signal.

QObject.sender() returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns nullptr. The pointer is valid only during the execution of the slot that calls this function from this object’s thread context. https://doc.qt.io/qt-5/qobject.html#sender

Minimal Example:

import sys
from PySide2.QtWidgets import QApplication
from PySide2.QtCore import Slot, Signal, QProcess, QObject


class Main(QObject):

    def __init__(self):
        QObject.__init__(self)

    def ping(self):
        proc = QProcess(self)
        proc.readyReadStandardOutput.connect(self.ping_read)
        proc.start('ping', ['8.8.8.8', '-c', '4'])

    def ping_read(self):
        proc = self.sender() // <---- "None" is returned by self.sender()
        data = bytearray(proc.readAllStandardOutput()).decode()
        print(data, end='')


if __name__ == "__main__":
    app = QApplication([])
    main = Main()
    main.ping()
    sys.exit(app.exec_())

Output:

Traceback (most recent call last):
  File "main.py", line 24, in ping_read
    data = bytearray(proc.readAllStandardOutput()).decode()
AttributeError: 'NoneType' object has no attribute 'readAllStandardOutput'

Compilation:

ubuntu@ubuntu:~/$ sudo python3 -m nuitka --enable-plugin=pyside2 main.py
Nuitka:INFO: Starting Python compilation.
Nuitka:INFO: Completed Python level compilation and optimization.                                           
Nuitka:INFO: Generating source code for C backend compiler.
Nuitka:INFO: Running data composer tool for optimal constant value handling.                                
Nuitka:INFO: Running C level backend compilation via Scons.
Nuitka-Scons:INFO: Backend C compiler: gcc (gcc).
Nuitka-Scons:INFO: Compiled 12 C files using ccache.                                                        
Nuitka-Scons:INFO: Cached C files (using ccache) with result 'cache hit': 10
Nuitka-Scons:INFO: Cached C files (using ccache) with result 'disabled': 1
Nuitka-Scons:INFO: Cached C files (using ccache) with result 'cache miss': 1
Nuitka:INFO: Keeping build directory 'main.build'.
Nuitka:INFO: Successfully created 'main.bin'.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
kayhayencommented, Mar 17, 2021

Ok, I am investigating this, and it seems that there is a builtin method object, but Nuitka ends up calling not that as a method call, but the built-in function with a single argument, so definitely incorrectly, there probably is a way to call these. There is an optimization not done, when call arguments have side effects, because of a reordering of code.

a.something_with_side_effect(another_side_effect())

In our case, the lookup of connect is not known to not have a side effect, esp. not on another attribute of the proc object, so that explains it. However, the bound objects should be good enough.

In these cases, we have to do the calling manual. But I think the self of the method object should get passed and be correct, so it’s still not explained, and I actually verified that. Also even if I wrap all functions, including the ping ones, they become proper method objects as an argument, and should allow the lookup, so this must be a case of sender using something else.

0reactions
RESP3CT88commented, Mar 22, 2021

@kayhayen That’s great! Thank you for those patches, I appreciate the time you spent on this. Hopefully they will merge your patches soon.

Read more comments on GitHub >

github_iconTop Results From Across the Web

PySide QtCore.Slot decorator does not work with self.sender ...
The problem is that the object that receive the signal (your Worker class) lives in another thread. From the Qt docs: QObject ...
Read more >
PySide Pitfalls - Qt Wiki
sender() # THIS DOES NOT WORK! a.someSignal.connect(on_signal). To achieve the same effect, you have to create a QObject subclass with ...
Read more >
Bug 344 – sender() returns None when use partial or lambda
If you are using a lambda as slot you can not have the sender object because PySide does not know for which QObject...
Read more >
Events and Signals in PySide - ZetCode
The sender is an object that sends a signal. The receiver is the object, that receives the signal. The slot is the method...
Read more >
Transmit extra data with signals in PyQt5 - Python GUIs
The problem is the line lambda: self.button_pressed(a) where we pass a to the final button_pressed slot. In this context, a is bound to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found