1+ from collections import namedtuple
12from contextlib import contextmanager
23import functools
34import sys
@@ -568,8 +569,12 @@ def pytest_addoption(parser):
568569
569570 parser .addoption ('--no-qt-log' , dest = 'qt_log' , action = 'store_false' ,
570571 default = True )
571- parser .addoption ('--qt-log-format' , dest = 'qt_log_format' ,
572- default = '{rec.type_name}: {rec.message}' )
572+ if QT_API == 'pyqt5' :
573+ default = '{rec.context.file}:{rec.context.function}:' \
574+ '{rec.context.line}:\n {rec.type_name}: {rec.message}'
575+ else :
576+ default = '{rec.type_name}: {rec.message}'
577+ parser .addoption ('--qt-log-format' , dest = 'qt_log_format' , default = default )
573578
574579
575580@pytest .mark .hookwrapper
@@ -671,7 +676,8 @@ def pytest_runtest_makereport(self, item, call):
671676 lines = []
672677 for rec in item .qt_log_capture .records :
673678 suffix = ' (IGNORED)' if rec .ignored else ''
674- lines .append (log_format .format (rec = rec ) + suffix )
679+ line = log_format .format (rec = rec ) + suffix
680+ lines .append (line )
675681 if lines :
676682 long_repr .addsection ('Captured Qt messages' ,
677683 '\n ' .join (lines ))
@@ -695,21 +701,34 @@ def __init__(self, ignore_regexes):
695701 self ._records = []
696702 self ._ignore_regexes = ignore_regexes or []
697703
698- def _handle (self , msg_type , message ):
704+ _Context = namedtuple ('_Context' , 'file function line' )
705+
706+ def _handle (self , msg_type , message , context = None ):
699707 """
700708 Method to be installed using qInstallMsgHandler, stores each message
701709 into the `messages` attribute.
702710 """
703- if isinstance (message , bytes ):
704- message = message .decode ('utf-8' , 'replace' )
711+ def to_unicode (s ):
712+ if isinstance (s , bytes ):
713+ s = s .decode ('utf-8' , 'replace' )
714+ return s
715+
716+ message = to_unicode (message )
705717
706718 ignored = False
707719 for regex in self ._ignore_regexes :
708720 if re .search (regex , message ) is not None :
709721 ignored = True
710722 break
711723
712- self ._records .append (Record (msg_type , message , ignored ))
724+ if context is not None :
725+ context = self ._Context (
726+ to_unicode (context .file ),
727+ to_unicode (context .function ),
728+ context .line ,
729+ )
730+
731+ self ._records .append (Record (msg_type , message , ignored , context ))
713732
714733 @property
715734 def records (self ):
@@ -733,22 +752,26 @@ class Record(object):
733752 :ivar datetime.datetime when: when the message was captured
734753 :ivar bool ignored: If this record matches a regex from the "qt_log_ignore"
735754 option.
755+ :ivar context: a namedtuple containing the attributes ``file``,
756+ ``function``, ``line``. Only available in Qt5, otherwise is None.
736757 """
737758
738- def __init__ (self , msg_type , message , ignored ):
759+ def __init__ (self , msg_type , message , ignored , context ):
739760 self ._type = msg_type
740761 self ._message = message
741762 self ._type_name = self ._get_msg_type_name (msg_type )
742763 self ._log_type_name = self ._get_log_type_name (msg_type )
743764 self ._when = datetime .datetime .now ()
744765 self ._ignored = ignored
766+ self ._context = context
745767
746768 message = property (lambda self : self ._message )
747769 type = property (lambda self : self ._type )
748770 type_name = property (lambda self : self ._type_name )
749771 log_type_name = property (lambda self : self ._log_type_name )
750772 when = property (lambda self : self ._when )
751773 ignored = property (lambda self : self ._ignored )
774+ context = property (lambda self : self ._context )
752775
753776 @classmethod
754777 def _get_msg_type_name (cls , msg_type ):
@@ -799,8 +822,9 @@ class _QtLogLevelErrorRepr(TerminalRepr):
799822
800823 def __init__ (self , item , level ):
801824 msg = 'Failure: Qt messages with level {0} or above emitted'
802- path , line , _ = item .location
803- self .fileloc = ReprFileLocation (path , line , msg .format (level .upper ()))
825+ path , line_index , _ = item .location
826+ self .fileloc = ReprFileLocation (path , lineno = line_index + 1 ,
827+ message = msg .format (level .upper ()))
804828 self .sections = []
805829
806830 def addsection (self , name , content , sep = "-" ):
0 commit comments