Uploaded image for project: 'Qt for Python'
  1. Qt for Python
  2. PYSIDE-1422

Signal crashes when class defined __eq__

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P2: Important P2: Important
    • 5.15.4
    • 5.15.1
    • PySide
    • None
    • 28fe4291c29d3f8333a126c6e58f1ecb163e3ea6 (pyside/pyside-setup/dev) 3f55b0f6c6e566bf83a0ffcc626d085ae01d640c (pyside/tqtc-pyside-setup/5.15)

      When a class defines __eq__, a signal on this class will crash immediately when connected.
      Example code:

      from PySide6 import QtCore
      from PySide6 import QtWidgets
      
      class Main:
          def setup(self):
              self.input_dialog = QtWidgets.QInputDialog()
      
              print('before', flush=True)
              self.input_dialog.accepted.connect(self.input_accepted)
              print('after', flush=True)
      
          def input_accepted(self):
              pass
      
          def __eq__(self, other): return super().__eq__(other)
          # def __hash__(self): return id(self)
      
      def main():
          application = QtWidgets.QApplication([])
          main_object = Main()
          main_object.setup()
          QtCore.QTimer.singleShot(0, application.quit)
          application.exec_()
      
      if __name__ == "__main__":
          main()
      

      When we also define a __hash__ function, everything works, again.

      What happens?

      In the implementation, PyObject_Hash is called, both on the signal function and on the class instance.
      The class instance by default is hashable by PyObject s default, but when __eq__ gets explicitly
      defined, the default __hash__ is gone.

      The second hash call then creates an undetected error, which in turn has the effect that new types
      cannot be created, and we segfault.

      Solution: We don't call __hash__ a second time, but use the object address directly.
      That has no negative effect on the hash function since random plus some offset remains random.

        1. pyside1422.py
          0.7 kB
          Friedemann Kleint
        2. pyside1422_stack.txt
          6 kB
          Friedemann Kleint
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            ctismer Christian Tismer
            ctismer Christian Tismer
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes