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
- relates to
-
PYSIDE-2473 Importing PySide6 breaks using unittest.mock.patch.dict to mock imports
- Closed
For Gerrit Dashboard: PYSIDE-1755 | ||||||
---|---|---|---|---|---|---|
# | Subject | Branch | Project | Status | CR | V |
389191,3 | Use Python Mocks as Slots | dev | pyside/pyside-setup | Status: ABANDONED | 0 | 0 |
389206,20 | Safe distinction of Nuitka compiled methods | 6.2.2 | pyside/pyside-setup | Status: ABANDONED | +2 | 0 |
390553,2 | Safe distinction of Nuitka compiled methods | dev | pyside/pyside-setup | Status: MERGED | +2 | 0 |
390648,10 | Safe distinction of Nuitka compiled methods | 6.2 | pyside/pyside-setup | Status: MERGED | +2 | 0 |
390897,2 | libpyside: Move helper isCompiledMethod() to pysideutils.h | dev | pyside/pyside-setup | Status: MERGED | +2 | 0 |