Skip to content

Commit

Permalink
compat.py: Add wrapper around sip/shiboken isdeleted/isvalid
Browse files Browse the repository at this point in the history
  • Loading branch information
zjp committed Jul 18, 2022
1 parent 9588c9c commit 897ab63
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
21 changes: 21 additions & 0 deletions qtpy/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
"""
import sys

from . import (
PYQT5,
PYQT6,
PYSIDE2,
PYSIDE6,
QtBindingsNotFoundError,
)

from .QtWidgets import QFileDialog


Expand Down Expand Up @@ -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()
11 changes: 11 additions & 0 deletions qtpy/tests/test_compat.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 897ab63

Please sign in to comment.