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

__code__ inspection on slot connection is buggy (breaks, e.g. MagicMock testing)

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • Not Evaluated
    • 6.2.9, 6.3
    • 6.2.2
    • PySide
    • None
    • All

    Description

      using unittest.mock.MagicMock from the python standard library is a very commonly-used object when testing python code, and until commit " Nuitka: Allow for compiled functions and methods too" 773e1105251a03131cbbdf4abcb0580cb425ff60 https://codereview.qt-project.org/c/pyside/pyside-setup/+/340108 , it was very helpful in testing PySide code.  Unfortunately, the introspection now done in extractFunctionArgumentsFromSlot appears to have broken it when it looks up _code_ on the object.

       

      example:

      from PySide6.QtWidgets import QPushButton, QApplication
      from unittest.mock import MagicMock
      
      app = QApplication([])
      btn = QPushButton()
      mock = MagicMock()
      btn.clicked.connect(mock)  # this now errors
      
      # this used to be helpful
      btn.clicked.emit()
      mock.assert_called_once()
      

       
      the error comes from the unittest.mock module:

      ---------------------------------------------------------------------------
      AttributeError                            Traceback (most recent call last)
      <ipython-input-37-5b96e0f4df61> in <module>
      ----> 1 btn.clicked.connect(mock)
      
      ~/miniconda3/envs/qt/lib/python3.9/unittest/mock.py in __getattr__(self, name)
          630                 raise AttributeError("Mock object has no attribute %r" % name)
          631         elif _is_magic(name):
      --> 632             raise AttributeError(name)
          633         if not self._mock_unsafe:
          634             if name.startswith(('assert', 'assret')):
      
      AttributeError: __code__
      
      

      a MagicMock is neither a function nor a method (i.e. it passes neither PyMethod_Check(slot) or PyFunction_Check(slot), however, it IS a valid callable and should be allowed as a slot. But line ~373 of pysidesignal.cpp tries to access the `_code_` object (which not all callables have)

      PyObject_GetAttr(function, PySide::PyMagicName::code())
      

       

      Attachments

        Issue Links

          For Gerrit Dashboard: PYSIDE-1755
          # Subject Branch Project Status CR V

          Activity

            People

              shpremna Shyamnath Premnadh
              talley Talley Lambert
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes