1- import dis
2- import inspect
31import logging
42import os
53import pathlib
@@ -19,7 +17,14 @@ def _modname(path):
1917
2018
2119class UtCoverageSender :
22- def __init__ (self , coverage_id : str , host : str , port : int , use_thread : bool = False , send_coverage : bool = True ):
20+ def __init__ (
21+ self ,
22+ coverage_id : str ,
23+ host : str ,
24+ port : int ,
25+ use_thread : bool = False ,
26+ send_coverage : bool = True ,
27+ ):
2328 self .coverage_id = coverage_id
2429 self .host = host
2530 self .port = port
@@ -59,7 +64,9 @@ def put_message(self, key: str):
5964
6065class PureSender (UtCoverageSender ):
6166 def __init__ (self ):
62- super ().__init__ ("000000" , "localhost" , 0 , use_thread = False , send_coverage = False )
67+ super ().__init__ (
68+ "000000" , "localhost" , 0 , use_thread = False , send_coverage = False
69+ )
6370
6471
6572class UtTracer :
@@ -72,6 +79,7 @@ def __init__(
7279 ):
7380 self .tested_file = tested_file
7481 self .counts : dict [UtInstruction , int ] = {}
82+ self .instructions : list [UtInstruction ] = []
7583 self .localtrace = self .localtrace_count
7684 self .globaltrace = self .globaltrace_lt
7785 self .ignore_dirs = ignore_dirs
@@ -88,10 +96,6 @@ def runfunc(self, func, /, *args, **kw):
8896 sys .settrace (None )
8997 return result
9098
91- def coverage (self , filename : str ) -> typing .List [int ]:
92- filename = _modname (filename )
93- return [line for file , line in self .counts .keys () if file == filename ]
94-
9599 def localtrace_count (self , frame , why , arg ):
96100 filename = frame .f_code .co_filename
97101 lineno = frame .f_lineno
@@ -108,15 +112,18 @@ def localtrace_count(self, frame, why, arg):
108112 except Exception :
109113 pass
110114 self .counts [key ] = self .counts .get (key , 0 ) + 1
115+ self .instructions .append (key )
111116 return self .localtrace
112117
113118 def globaltrace_lt (self , frame , why , arg ):
114- if why == ' call' :
119+ if why == " call" :
115120 if self .mode == TraceMode .Instructions :
116121 frame .f_trace_opcodes = True
117122 frame .f_trace_lines = False
118- filename = frame .f_globals .get ('__file__' , None )
119- if filename and all (not filename .startswith (d + os .sep ) for d in self .ignore_dirs ):
123+ filename = frame .f_globals .get ("__file__" , None )
124+ if filename and all (
125+ not filename .startswith (d + os .sep ) for d in self .ignore_dirs
126+ ):
120127 modulename = _modname (filename )
121128 if modulename is not None :
122129 return self .localtrace
@@ -140,10 +147,11 @@ def f(x):
140147 def g (x ):
141148 xs = [[j for j in range (i )] for i in range (10 )]
142149 return x * 2
150+
143151 return g1 (x ) * g (x ) + 2
144152
145153
146154if __name__ == "__main__" :
147155 tracer = UtTracer (pathlib .Path (__file__ ), [], PureSender ())
148156 tracer .runfunc (f , 2 )
149- print (tracer .counts )
157+ print (tracer .counts )
0 commit comments