Skip to content

Commit

Permalink
Make exec() for PySide2 (#438)
Browse files Browse the repository at this point in the history
  • Loading branch information
dalthviz authored Jul 5, 2023
2 parents 1749f6c + 6d6f8bd commit b303b62
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
7 changes: 6 additions & 1 deletion qtpy/QtCore.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from typing import TYPE_CHECKING

from . import PYQT6, PYQT5, PYSIDE2, PYSIDE6
from ._utils import possibly_static_exec
from ._utils import possibly_static_exec, possibly_static_exec_

if PYQT5:
from PyQt5.QtCore import *
Expand Down Expand Up @@ -77,6 +77,11 @@
# Fails with PySide2 5.12.0
pass

QCoreApplication.exec = lambda *args, **kwargs: possibly_static_exec_(QCoreApplication, *args, **kwargs)
QEventLoop.exec = lambda self, *args, **kwargs: self.exec_(*args, **kwargs)
QThread.exec = lambda self, *args, **kwargs: self.exec_(*args, **kwargs)
QTextStreamManipulator.exec = lambda self, *args, **kwargs: self.exec_(*args, **kwargs)

elif PYSIDE6:
from PySide6.QtCore import *
import PySide6.QtCore
Expand Down
14 changes: 14 additions & 0 deletions qtpy/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,17 @@ def possibly_static_exec(cls, *args, **kwargs):
return args[0].exec(*args[1:], **kwargs)
else:
return cls.exec(*args, **kwargs)


def possibly_static_exec_(cls, *args, **kwargs):
"""Call `self.exec` when `self` is given or a static method otherwise."""
if not args and not kwargs:
# A special case (`cls.exec()`) to avoid the function resolving error
return cls.exec_()
if isinstance(args[0], cls):
if len(args) == 1 and not kwargs:
# A special case (`self.exec()`) to avoid the function resolving error
return args[0].exec_()
return args[0].exec_(*args[1:], **kwargs)
else:
return cls.exec_(*args, **kwargs)
32 changes: 25 additions & 7 deletions qtpy/tests/test_qtcore.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,21 @@ def test_QTime_toPython_and_toPyTime(method):
assert py_time == NOW.time()


def test_qeventloop_exec_(qtbot):
"""Test QEventLoop.exec_"""
def test_qeventloop_exec(qtbot):
"""Test `QEventLoop.exec_` and `QEventLoop.exec`"""
assert QtCore.QEventLoop.exec_ is not None
assert QtCore.QEventLoop.exec is not None
event_loop = QtCore.QEventLoop(None)
QtCore.QTimer.singleShot(100, event_loop.quit)
event_loop.exec_()
QtCore.QTimer.singleShot(100, event_loop.quit)
event_loop.exec()


def test_qthread_exec_():
"""Test QThread.exec_"""
def test_qthread_exec():
"""Test `QThread.exec_` and `QThread.exec_`"""
assert QtCore.QThread.exec_ is not None
assert QtCore.QThread.exec is not None


def test_QLibraryInfo_location_and_path():
Expand Down Expand Up @@ -93,11 +97,25 @@ def test_QCoreApplication_exec_(qapp):
app.exec_()


def test_QCoreApplication_exec(qapp):
"""Test `QtCore.QCoreApplication.exec`"""
assert QtCore.QCoreApplication.exec is not None
app = QtCore.QCoreApplication.instance() or QtCore.QCoreApplication([sys.executable, __file__])
assert app is not None
QtCore.QTimer.singleShot(100, QtCore.QCoreApplication.instance().quit)
QtCore.QCoreApplication.exec()
app = QtCore.QCoreApplication.instance() or QtCore.QCoreApplication([sys.executable, __file__])
assert app is not None
QtCore.QTimer.singleShot(100, QtCore.QCoreApplication.instance().quit)
app.exec()


@pytest.mark.skipif(PYQT5 or PYQT6,
reason="Doesn't seem to be present on PyQt5 and PyQt6")
def test_qtextstreammanipulator_exec_():
"""Test QTextStreamManipulator.exec_"""
QtCore.QTextStreamManipulator.exec_ is not None
def test_qtextstreammanipulator_exec():
"""Test `QTextStreamManipulator.exec_` and `QTextStreamManipulator.exec`"""
assert QtCore.QTextStreamManipulator.exec_ is not None
assert QtCore.QTextStreamManipulator.exec is not None


@pytest.mark.skipif(PYSIDE2 or PYQT6,
Expand Down

0 comments on commit b303b62

Please sign in to comment.