Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR: Use static calls of exec_ elsewhere where needed, and test them #422

Merged
merged 17 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions qtpy/QtCore.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from typing import TYPE_CHECKING

from . import PYQT6, PYQT5, PYSIDE2, PYSIDE6
from .utils import possibly_static_exec

if PYQT5:
from PyQt5.QtCore import *
Expand Down Expand Up @@ -54,7 +55,7 @@
pass

# Map missing methods
QCoreApplication.exec_ = QCoreApplication.exec
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)

Expand Down Expand Up @@ -105,7 +106,7 @@
Qt.MidButton = Qt.MiddleButton

# Map DeprecationWarning methods
QCoreApplication.exec_ = QCoreApplication.exec
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)
Expand Down
6 changes: 3 additions & 3 deletions qtpy/QtGui.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"""Provides QtGui classes and functions."""

from . import PYQT6, PYQT5, PYSIDE2, PYSIDE6, QtModuleNotInstalledError
from .utils import getattr_missing_optional_dep
from .utils import possibly_static_exec, getattr_missing_optional_dep


_missing_optional_names = {}
Expand Down Expand Up @@ -65,7 +65,7 @@ def __getattr__(name):

# Map missing/renamed methods
QDrag.exec_ = lambda self, *args, **kwargs: self.exec(*args, **kwargs)
QGuiApplication.exec_ = QGuiApplication.exec
QGuiApplication.exec_ = lambda *args, **kwargs: possibly_static_exec(QGuiApplication, *args, **kwargs)
QTextDocument.print_ = lambda self, *args, **kwargs: self.print(*args, **kwargs)

# Allow unscoped access for enums inside the QtGui module
Expand Down Expand Up @@ -105,7 +105,7 @@ def __getattr__(name):

# Map DeprecationWarning methods
QDrag.exec_ = lambda self, *args, **kwargs: self.exec(*args, **kwargs)
QGuiApplication.exec_ = QGuiApplication.exec
QGuiApplication.exec_ = lambda *args, **kwargs: possibly_static_exec(QGuiApplication, *args, **kwargs)

if PYSIDE2 or PYSIDE6:
# PySide{2,6} do not accept the `mode` keyword argument in
Expand Down
12 changes: 6 additions & 6 deletions qtpy/QtWidgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
"""Provides widget classes and functions."""

from . import PYQT5, PYQT6, PYSIDE2, PYSIDE6, QtModuleNotInstalledError
from .utils import getattr_missing_optional_dep
from .utils import possibly_static_exec, getattr_missing_optional_dep


_missing_optional_names = {}


def __getattr__(name):
"""Custom getattr to chain and wrap errors due to missing optional deps."""
raise getattr_missing_optional_dep(
Expand Down Expand Up @@ -46,9 +47,9 @@ def __getattr__(name):
QPlainTextEdit.setTabStopWidth = lambda self, *args, **kwargs: self.setTabStopDistance(*args, **kwargs)
QPlainTextEdit.tabStopWidth = lambda self, *args, **kwargs: self.tabStopDistance(*args, **kwargs)
QPlainTextEdit.print_ = lambda self, *args, **kwargs: self.print(*args, **kwargs)
QApplication.exec_ = QApplication.exec
QApplication.exec_ = lambda *args, **kwargs: possibly_static_exec(QApplication, *args, **kwargs)
QDialog.exec_ = lambda self, *args, **kwargs: self.exec(*args, **kwargs)
QMenu.exec_ = lambda self, *args, **kwargs: self.exec(*args, **kwargs)
QMenu.exec_ = lambda *args, **kwargs: possibly_static_exec(QMenu, *args, **kwargs)
QLineEdit.getTextMargins = lambda self: (self.textMargins().left(), self.textMargins().top(), self.textMargins().right(), self.textMargins().bottom())

# Allow unscoped access for enums inside the QtWidgets module
Expand Down Expand Up @@ -81,7 +82,6 @@ def __getattr__(name):
QLineEdit.getTextMargins = lambda self: (self.textMargins().left(), self.textMargins().top(), self.textMargins().right(), self.textMargins().bottom())

# Map DeprecationWarning methods
QApplication.exec_ = QApplication.exec
QApplication.exec_ = lambda *args, **kwargs: possibly_static_exec(QApplication, *args, **kwargs)
QDialog.exec_ = lambda self, *args, **kwargs: self.exec(*args, **kwargs)
QMenu.exec_ = lambda self, *args, **kwargs: self.exec(*args, **kwargs)

QMenu.exec_ = lambda *args, **kwargs: possibly_static_exec(QMenu, *args, **kwargs)
13 changes: 13 additions & 0 deletions qtpy/tests/test_qtcore.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,19 @@ def test_QLibraryInfo_LibraryLocation_and_LibraryPath():
assert QtCore.QLibraryInfo.LibraryPath is not None


def test_QCoreApplication_exec_():
"""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_():
Expand Down
15 changes: 13 additions & 2 deletions qtpy/tests/test_qtgui.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,20 @@ def test_qdrag_functions(qtbot):
drag.exec_()


def test_qguiapplication_functions():
"""Test functions mapping for QtGui.QGuiApplication."""
@pytest.mark.skipif(
sys.platform.startswith('linux') and not_using_conda(),
reason="Fatal Python error: Aborted on Linux CI when not using conda")
def test_QGuiApplication_exec_():
"""Test `QtGui.QGuiApplication.exec_`"""
assert QtGui.QGuiApplication.exec_ is not None
app = QtGui.QGuiApplication.instance() or QtGui.QGuiApplication([sys.executable, __file__])
assert app is not None
QtCore.QTimer.singleShot(100, QtGui.QGuiApplication.instance().quit)
QtGui.QGuiApplication.exec_()
app = QtGui.QGuiApplication.instance() or QtGui.QGuiApplication([sys.executable, __file__])
assert app is not None
QtCore.QTimer.singleShot(100, QtGui.QGuiApplication.instance().quit)
app.exec_()


def test_what_moved_to_qtgui_in_qt6():
Expand Down
43 changes: 33 additions & 10 deletions qtpy/tests/test_qtwidgets.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Test QtWidgets."""

import sys

import pytest
Expand Down Expand Up @@ -54,9 +53,20 @@ def test_qplaintextedit_functions(qtbot, pdf_writer):
assert output_path.exists()


def test_qapplication_functions():
"""Test functions mapping for QtWidgets.QApplication."""
assert QtWidgets.QApplication.exec_
@pytest.mark.skipif(
sys.platform.startswith('linux') and not_using_conda(),
reason="Fatal Python error: Aborted on Linux CI when not using conda")
def test_QApplication_exec_():
"""Test `QtWidgets.QApplication.exec_`"""
assert QtWidgets.QApplication.exec_ is not None
app = QtWidgets.QApplication.instance() or QtWidgets.QApplication([sys.executable, __file__])
assert app is not None
QtCore.QTimer.singleShot(100, QtWidgets.QApplication.instance().quit)
QtWidgets.QApplication.exec_()
app = QtWidgets.QApplication.instance() or QtWidgets.QApplication([sys.executable, __file__])
assert app is not None
QtCore.QTimer.singleShot(100, QtWidgets.QApplication.instance().quit)
app.exec_()


@pytest.mark.skipif(
Expand Down Expand Up @@ -98,12 +108,25 @@ def __init__(self):
@pytest.mark.skipif(
sys.platform == 'darwin' and sys.version_info[:2] == (3, 7),
reason="Stalls on macOS CI with Python 3.7")
def test_qmenu_functions(qtbot):
"""Test functions mapping for QtWidgets.QDialog."""
assert QtWidgets.QMenu.exec_
menu = QtWidgets.QMenu(None)
QtCore.QTimer.singleShot(100, menu.close)
menu.exec_()
def test_QMenu_functions(qtbot):
"""Test functions mapping for `QtWidgets.QMenu`."""
# A window is required for static calls
window = QtWidgets.QMainWindow()
menu = QtWidgets.QMenu(window)
menu.addAction('QtPy')
window.show()

with qtbot.waitExposed(window):
# Call `exec_` of a `QMenu` instance
QtCore.QTimer.singleShot(100, menu.close)
menu.exec_()

# Call static `QMenu.exec_`
QtCore.QTimer.singleShot(100, lambda: qtbot.keyClick(
QtWidgets.QApplication.widgetAt(1, 1),
QtCore.Qt.Key_Escape)
)
QtWidgets.QMenu.exec_(menu.actions(), QtCore.QPoint(1, 1))


@pytest.mark.skipif(PYQT5 and PYQT_VERSION.startswith('5.9'),
Expand Down
14 changes: 14 additions & 0 deletions qtpy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,17 @@ def getattr_missing_optional_dep(name, module_name, optional_names):
if name in optional_names:
return _wrap_missing_optional_dep_error(attr_error, **optional_names[name])
return attr_error


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)