1
+ from collections import namedtuple
1
2
from contextlib import contextmanager
2
3
import functools
3
4
import sys
@@ -568,8 +569,12 @@ def pytest_addoption(parser):
568
569
569
570
parser .addoption ('--no-qt-log' , dest = 'qt_log' , action = 'store_false' ,
570
571
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}: {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 )
573
578
574
579
575
580
@pytest .mark .hookwrapper
@@ -671,7 +676,8 @@ def pytest_runtest_makereport(self, item, call):
671
676
lines = []
672
677
for rec in item .qt_log_capture .records :
673
678
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 )
675
681
if lines :
676
682
long_repr .addsection ('Captured Qt messages' ,
677
683
'\n ' .join (lines ))
@@ -695,21 +701,34 @@ def __init__(self, ignore_regexes):
695
701
self ._records = []
696
702
self ._ignore_regexes = ignore_regexes or []
697
703
698
- def _handle (self , msg_type , message ):
704
+ _Context = namedtuple ('_Context' , 'file function line' )
705
+
706
+ def _handle (self , msg_type , message , context = None ):
699
707
"""
700
708
Method to be installed using qInstallMsgHandler, stores each message
701
709
into the `messages` attribute.
702
710
"""
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 )
705
717
706
718
ignored = False
707
719
for regex in self ._ignore_regexes :
708
720
if re .search (regex , message ) is not None :
709
721
ignored = True
710
722
break
711
723
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 ))
713
732
714
733
@property
715
734
def records (self ):
@@ -733,22 +752,26 @@ class Record(object):
733
752
:ivar datetime.datetime when: when the message was captured
734
753
:ivar bool ignored: If this record matches a regex from the "qt_log_ignore"
735
754
option.
755
+ :ivar context: a namedtuple containing the attributes ``file``,
756
+ ``function``, ``line``. Only available in Qt5, otherwise is None.
736
757
"""
737
758
738
- def __init__ (self , msg_type , message , ignored ):
759
+ def __init__ (self , msg_type , message , ignored , context ):
739
760
self ._type = msg_type
740
761
self ._message = message
741
762
self ._type_name = self ._get_msg_type_name (msg_type )
742
763
self ._log_type_name = self ._get_log_type_name (msg_type )
743
764
self ._when = datetime .datetime .now ()
744
765
self ._ignored = ignored
766
+ self ._context = context
745
767
746
768
message = property (lambda self : self ._message )
747
769
type = property (lambda self : self ._type )
748
770
type_name = property (lambda self : self ._type_name )
749
771
log_type_name = property (lambda self : self ._log_type_name )
750
772
when = property (lambda self : self ._when )
751
773
ignored = property (lambda self : self ._ignored )
774
+ context = property (lambda self : self ._context )
752
775
753
776
@classmethod
754
777
def _get_msg_type_name (cls , msg_type ):
0 commit comments