1313import re
1414
1515from 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
1920def _inject_qtest_methods (cls ):
@@ -692,7 +693,7 @@ def pytest_runtest_makereport(self, item, call):
692693class _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.
0 commit comments