Skip to content

Commit 59f9396

Browse files
committed
Remove thin wrapper over qInstallMessageHandler
1 parent 7b70994 commit 59f9396

File tree

3 files changed

+54
-25
lines changed

3 files changed

+54
-25
lines changed

pytestqt/_tests/test_logging.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
pytest_plugins = 'pytester'
99

1010

11-
@pytest.mark.parametrize('test_succeds, qt_log',
11+
@pytest.mark.parametrize('test_succeeds, qt_log',
1212
[(True, True), (True, False), (False, False),
1313
(False, True)])
14-
def test_basic_logging(testdir, test_succeds, qt_log):
14+
def test_basic_logging(testdir, test_succeeds, qt_log):
1515
"""
1616
Test Qt logging capture output.
1717
@@ -26,10 +26,10 @@ def test_types():
2626
qWarning('this is a WARNING message')
2727
qCritical('this is a CRITICAL message')
2828
assert {0}
29-
""".format(test_succeds)
29+
""".format(test_succeeds)
3030
)
3131
res = testdir.runpytest(*(['--no-qt-log'] if not qt_log else []))
32-
if test_succeds:
32+
if test_succeeds:
3333
assert 'Captured Qt messages' not in res.stdout.str()
3434
assert 'Captured stderr call' not in res.stdout.str()
3535
else:
@@ -406,7 +406,8 @@ def test_context_none(testdir):
406406
def test_foo(request):
407407
log_capture = request.node.qt_log_capture
408408
context = log_capture._Context(None, None, None)
409-
log_capture._handle(QtWarningMsg, "WARNING message", context)
409+
log_capture._handle_with_context(QtWarningMsg,
410+
context, "WARNING message")
410411
assert 0
411412
"""
412413
)

pytestqt/plugin.py

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
import re
1414

1515
from pytestqt.qt_compat import QtCore, QtTest, QApplication, QT_API, \
16-
qInstallMsgHandler, QtDebugMsg, QtWarningMsg, QtCriticalMsg, QtFatalMsg
16+
qInstallMsgHandler, qInstallMessageHandler, QtDebugMsg, QtWarningMsg, \
17+
QtCriticalMsg, QtFatalMsg
1718

1819

1920
def _inject_qtest_methods(cls):
@@ -692,7 +693,7 @@ def pytest_runtest_makereport(self, item, call):
692693
class _QtMessageCapture(object):
693694
"""
694695
Captures Qt messages when its `handle` method is installed using
695-
qInstallMsgHandler, and stores them into `messages` attribute.
696+
qInstallMsgHandler, and stores them into `records` attribute.
696697
697698
:attr _records: list of Record instances.
698699
:attr _ignore_regexes: list of regexes (as strings) that define if a record
@@ -705,10 +706,26 @@ def __init__(self, ignore_regexes):
705706
self._previous_handler = None
706707

707708
def _start(self):
708-
self._previous_handler = qInstallMsgHandler(self._handle)
709+
"""
710+
Start receiving messages from Qt.
711+
"""
712+
if qInstallMsgHandler:
713+
previous_handler = qInstallMsgHandler(self._handle_no_context)
714+
else:
715+
assert qInstallMessageHandler
716+
previous_handler = qInstallMessageHandler(self._handle_with_context)
717+
self._previous_handler = previous_handler
709718

710719
def _stop(self):
711-
qInstallMsgHandler(self._previous_handler)
720+
"""
721+
Stop receiving messages from Qt, restoring the previously installed
722+
handler.
723+
"""
724+
if qInstallMsgHandler:
725+
qInstallMsgHandler(self._previous_handler)
726+
else:
727+
assert qInstallMessageHandler
728+
qInstallMessageHandler(self._previous_handler)
712729

713730
@contextmanager
714731
def disabled(self):
@@ -724,10 +741,13 @@ def disabled(self):
724741

725742
_Context = namedtuple('_Context', 'file function line')
726743

727-
def _handle(self, msg_type, message, context=None):
744+
def _append_new_record(self, msg_type, message, context):
728745
"""
729-
Method to be installed using qInstallMsgHandler, stores each message
730-
into the `messages` attribute.
746+
Creates a new Record instance and stores it.
747+
748+
:param msg_type: Qt message typ
749+
:param message: message string, if bytes it will be converted to str.
750+
:param context: QMessageLogContext object or None
731751
"""
732752
def to_unicode(s):
733753
if isinstance(s, bytes):
@@ -751,6 +771,20 @@ def to_unicode(s):
751771

752772
self._records.append(Record(msg_type, message, ignored, context))
753773

774+
def _handle_no_context(self, msg_type, message):
775+
"""
776+
Method to be installed using qInstallMsgHandler (Qt4),
777+
stores each message into the `_records` attribute.
778+
"""
779+
self._append_new_record(msg_type, message, context=None)
780+
781+
def _handle_with_context(self, msg_type, context, message):
782+
"""
783+
Method to be installed using qInstallMessageHandler (Qt5),
784+
stores each message into the `_records` attribute.
785+
"""
786+
self._append_new_record(msg_type, message, context=context)
787+
754788
@property
755789
def records(self):
756790
"""Access messages captured so far.

pytestqt/qt_compat.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ def _import_module(module_name):
7676
QtCriticalMsg = QtCore.QtCriticalMsg
7777
QtFatalMsg = QtCore.QtFatalMsg
7878

79+
# Qt4 and Qt5 have different functions to install a message handler;
80+
# the plugin will try to use the one that is not None
81+
qInstallMsgHandler = None
82+
qInstallMessageHandler = None
83+
7984
if QT_API == 'pyside':
8085
Signal = QtCore.Signal
8186
Slot = QtCore.Slot
@@ -93,19 +98,7 @@ def _import_module(module_name):
9398
_QtWidgets = _import_module('QtWidgets')
9499
QApplication = _QtWidgets.QApplication
95100
QWidget = _QtWidgets.QWidget
96-
97-
def qInstallMsgHandler(handler):
98-
"""
99-
Installs the given function as a message handler. This
100-
will adapt Qt5 message handler signature into Qt4
101-
message handler's signature.
102-
"""
103-
def _Qt5MessageHandler(msg_type, context, msg):
104-
handler(msg_type, msg, context)
105-
if handler is not None:
106-
return QtCore.qInstallMessageHandler(_Qt5MessageHandler)
107-
else:
108-
return QtCore.qInstallMessageHandler(None)
101+
qInstallMessageHandler = QtCore.qInstallMessageHandler
109102
else:
110103
QApplication = QtGui.QApplication
111104
QWidget = QtGui.QWidget
@@ -141,6 +134,7 @@ def __getattr__(cls, name):
141134
QApplication = Mock()
142135
QWidget = Mock()
143136
qInstallMsgHandler = Mock()
137+
qInstallMessageHandler = Mock()
144138
qDebug = Mock()
145139
qWarning = Mock()
146140
qCritical = Mock()

0 commit comments

Comments
 (0)