From 897ab638aac62e6b93a0c6cc626379fdfe1c7ea3 Mon Sep 17 00:00:00 2001 From: Zach Pearson Date: Sat, 16 Jul 2022 10:49:55 -0700 Subject: [PATCH] compat.py: Add wrapper around sip/shiboken isdeleted/isvalid --- qtpy/compat.py | 21 +++++++++++++++++++++ qtpy/tests/test_compat.py | 11 +++++++++++ 2 files changed, 32 insertions(+) create mode 100644 qtpy/tests/test_compat.py diff --git a/qtpy/compat.py b/qtpy/compat.py index 45e11338..c1f1c54b 100644 --- a/qtpy/compat.py +++ b/qtpy/compat.py @@ -7,6 +7,14 @@ """ import sys +from . import ( + PYQT5, + PYQT6, + PYSIDE2, + PYSIDE6, + QtBindingsNotFoundError, +) + from .QtWidgets import QFileDialog @@ -129,3 +137,16 @@ def getsavefilename(parent=None, caption='', basedir='', filters='', caption=caption, basedir=basedir, filters=filters, selectedfilter=selectedfilter, options=options) + +# ============================================================================= +def isalive(object): + """Wrapper around sip.isdeleted and shiboken.isValid which tests whether + an object is currently alive.""" + if PYQT5 or PYQT6: + from . import sip + return sip.isdeleted(object) + elif PYSIDE2 or PYSIDE6: + from . import shiboken + return shiboken.isValid(object) + else: + raise QtBindingsNotFoundError() diff --git a/qtpy/tests/test_compat.py b/qtpy/tests/test_compat.py new file mode 100644 index 00000000..975774a6 --- /dev/null +++ b/qtpy/tests/test_compat.py @@ -0,0 +1,11 @@ +"""Test the compat module.""" + +from qtpy import compat, QtWidgets + +def test_isalive(qtbot): + """Test compat.isalive""" + test_widget = QtWidgets.QWidget() + assert compat.isalive(test_widget) == True + with qtbot.waitSignal(test_widget.destroyed): + test_widget.deleteLater() + assert compat.isalive(test_widget) == False