13
13
import re
14
14
15
15
from pytestqt .qt_compat import QtCore , QtTest , QApplication , QT_API , \
16
- qInstallMsgHandler , QtDebugMsg , QtWarningMsg , QtCriticalMsg , QtFatalMsg
16
+ qInstallMsgHandler , qInstallMessageHandler , QtDebugMsg , QtWarningMsg , \
17
+ QtCriticalMsg , QtFatalMsg
17
18
18
19
19
20
def _inject_qtest_methods (cls ):
@@ -692,7 +693,7 @@ def pytest_runtest_makereport(self, item, call):
692
693
class _QtMessageCapture (object ):
693
694
"""
694
695
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.
696
697
697
698
:attr _records: list of Record instances.
698
699
:attr _ignore_regexes: list of regexes (as strings) that define if a record
@@ -705,10 +706,26 @@ def __init__(self, ignore_regexes):
705
706
self ._previous_handler = None
706
707
707
708
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
709
718
710
719
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 )
712
729
713
730
@contextmanager
714
731
def disabled (self ):
@@ -724,10 +741,13 @@ def disabled(self):
724
741
725
742
_Context = namedtuple ('_Context' , 'file function line' )
726
743
727
- def _handle (self , msg_type , message , context = None ):
744
+ def _append_new_record (self , msg_type , message , context ):
728
745
"""
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
731
751
"""
732
752
def to_unicode (s ):
733
753
if isinstance (s , bytes ):
@@ -751,6 +771,20 @@ def to_unicode(s):
751
771
752
772
self ._records .append (Record (msg_type , message , ignored , context ))
753
773
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
+
754
788
@property
755
789
def records (self ):
756
790
"""Access messages captured so far.
0 commit comments