|
1 # Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 |
|
2 # For details: https://github.com/nedbat/coveragepy/blob/master/NOTICE.txt |
|
3 |
|
4 """Control of and utilities for debugging.""" |
|
5 |
|
6 import contextlib |
|
7 import functools |
|
8 import inspect |
|
9 import io |
|
10 import itertools |
|
11 import os |
|
12 import pprint |
|
13 import reprlib |
|
14 import sys |
|
15 import _thread |
|
16 |
|
17 from coverage.misc import isolate_module |
|
18 |
|
19 os = isolate_module(os) |
|
20 |
|
21 |
|
22 # When debugging, it can be helpful to force some options, especially when |
|
23 # debugging the configuration mechanisms you usually use to control debugging! |
|
24 # This is a list of forced debugging options. |
|
25 FORCED_DEBUG = [] |
|
26 FORCED_DEBUG_FILE = None |
|
27 |
|
28 |
|
29 class DebugControl: |
|
30 """Control and output for debugging.""" |
|
31 |
|
32 show_repr_attr = False # For SimpleReprMixin |
|
33 |
|
34 def __init__(self, options, output): |
|
35 """Configure the options and output file for debugging.""" |
|
36 self.options = list(options) + FORCED_DEBUG |
|
37 self.suppress_callers = False |
|
38 |
|
39 filters = [] |
|
40 if self.should('pid'): |
|
41 filters.append(add_pid_and_tid) |
|
42 self.output = DebugOutputFile.get_one( |
|
43 output, |
|
44 show_process=self.should('process'), |
|
45 filters=filters, |
|
46 ) |
|
47 self.raw_output = self.output.outfile |
|
48 |
|
49 def __repr__(self): |
|
50 return f"<DebugControl options={self.options!r} raw_output={self.raw_output!r}>" |
|
51 |
|
52 def should(self, option): |
|
53 """Decide whether to output debug information in category `option`.""" |
|
54 if option == "callers" and self.suppress_callers: |
|
55 return False |
|
56 return (option in self.options) |
|
57 |
|
58 @contextlib.contextmanager |
|
59 def without_callers(self): |
|
60 """A context manager to prevent call stacks from being logged.""" |
|
61 old = self.suppress_callers |
|
62 self.suppress_callers = True |
|
63 try: |
|
64 yield |
|
65 finally: |
|
66 self.suppress_callers = old |
|
67 |
|
68 def write(self, msg): |
|
69 """Write a line of debug output. |
|
70 |
|
71 `msg` is the line to write. A newline will be appended. |
|
72 |
|
73 """ |
|
74 self.output.write(msg+"\n") |
|
75 if self.should('self'): |
|
76 caller_self = inspect.stack()[1][0].f_locals.get('self') |
|
77 if caller_self is not None: |
|
78 self.output.write(f"self: {caller_self!r}\n") |
|
79 if self.should('callers'): |
|
80 dump_stack_frames(out=self.output, skip=1) |
|
81 self.output.flush() |
|
82 |
|
83 |
|
84 class DebugControlString(DebugControl): |
|
85 """A `DebugControl` that writes to a StringIO, for testing.""" |
|
86 def __init__(self, options): |
|
87 super().__init__(options, io.StringIO()) |
|
88 |
|
89 def get_output(self): |
|
90 """Get the output text from the `DebugControl`.""" |
|
91 return self.raw_output.getvalue() |
|
92 |
|
93 |
|
94 class NoDebugging: |
|
95 """A replacement for DebugControl that will never try to do anything.""" |
|
96 def should(self, option): # pylint: disable=unused-argument |
|
97 """Should we write debug messages? Never.""" |
|
98 return False |
|
99 |
|
100 |
|
101 def info_header(label): |
|
102 """Make a nice header string.""" |
|
103 return "--{:-<60s}".format(" "+label+" ") |
|
104 |
|
105 |
|
106 def info_formatter(info): |
|
107 """Produce a sequence of formatted lines from info. |
|
108 |
|
109 `info` is a sequence of pairs (label, data). The produced lines are |
|
110 nicely formatted, ready to print. |
|
111 |
|
112 """ |
|
113 info = list(info) |
|
114 if not info: |
|
115 return |
|
116 label_len = 30 |
|
117 assert all(len(l) < label_len for l, _ in info) |
|
118 for label, data in info: |
|
119 if data == []: |
|
120 data = "-none-" |
|
121 if isinstance(data, tuple) and len(repr(tuple(data))) < 30: |
|
122 # Convert to tuple to scrub namedtuples. |
|
123 yield "%*s: %r" % (label_len, label, tuple(data)) |
|
124 elif isinstance(data, (list, set, tuple)): |
|
125 prefix = "%*s:" % (label_len, label) |
|
126 for e in data: |
|
127 yield "%*s %s" % (label_len+1, prefix, e) |
|
128 prefix = "" |
|
129 else: |
|
130 yield "%*s: %s" % (label_len, label, data) |
|
131 |
|
132 |
|
133 def write_formatted_info(write, header, info): |
|
134 """Write a sequence of (label,data) pairs nicely. |
|
135 |
|
136 `write` is a function write(str) that accepts each line of output. |
|
137 `header` is a string to start the section. `info` is a sequence of |
|
138 (label, data) pairs, where label is a str, and data can be a single |
|
139 value, or a list/set/tuple. |
|
140 |
|
141 """ |
|
142 write(info_header(header)) |
|
143 for line in info_formatter(info): |
|
144 write(f" {line}") |
|
145 |
|
146 |
|
147 def short_stack(limit=None, skip=0): |
|
148 """Return a string summarizing the call stack. |
|
149 |
|
150 The string is multi-line, with one line per stack frame. Each line shows |
|
151 the function name, the file name, and the line number: |
|
152 |
|
153 ... |
|
154 start_import_stop : /Users/ned/coverage/trunk/tests/coveragetest.py @95 |
|
155 import_local_file : /Users/ned/coverage/trunk/tests/coveragetest.py @81 |
|
156 import_local_file : /Users/ned/coverage/trunk/coverage/backward.py @159 |
|
157 ... |
|
158 |
|
159 `limit` is the number of frames to include, defaulting to all of them. |
|
160 |
|
161 `skip` is the number of frames to skip, so that debugging functions can |
|
162 call this and not be included in the result. |
|
163 |
|
164 """ |
|
165 stack = inspect.stack()[limit:skip:-1] |
|
166 return "\n".join("%30s : %s:%d" % (t[3], t[1], t[2]) for t in stack) |
|
167 |
|
168 |
|
169 def dump_stack_frames(limit=None, out=None, skip=0): |
|
170 """Print a summary of the stack to stdout, or someplace else.""" |
|
171 out = out or sys.stdout |
|
172 out.write(short_stack(limit=limit, skip=skip+1)) |
|
173 out.write("\n") |
|
174 |
|
175 |
|
176 def clipped_repr(text, numchars=50): |
|
177 """`repr(text)`, but limited to `numchars`.""" |
|
178 r = reprlib.Repr() |
|
179 r.maxstring = numchars |
|
180 return r.repr(text) |
|
181 |
|
182 |
|
183 def short_id(id64): |
|
184 """Given a 64-bit id, make a shorter 16-bit one.""" |
|
185 id16 = 0 |
|
186 for offset in range(0, 64, 16): |
|
187 id16 ^= id64 >> offset |
|
188 return id16 & 0xFFFF |
|
189 |
|
190 |
|
191 def add_pid_and_tid(text): |
|
192 """A filter to add pid and tid to debug messages.""" |
|
193 # Thread ids are useful, but too long. Make a shorter one. |
|
194 tid = f"{short_id(_thread.get_ident()):04x}" |
|
195 text = f"{os.getpid():5d}.{tid}: {text}" |
|
196 return text |
|
197 |
|
198 |
|
199 class SimpleReprMixin: |
|
200 """A mixin implementing a simple __repr__.""" |
|
201 simple_repr_ignore = ['simple_repr_ignore', '$coverage.object_id'] |
|
202 |
|
203 def __repr__(self): |
|
204 show_attrs = ( |
|
205 (k, v) for k, v in self.__dict__.items() |
|
206 if getattr(v, "show_repr_attr", True) |
|
207 and not callable(v) |
|
208 and k not in self.simple_repr_ignore |
|
209 ) |
|
210 return "<{klass} @0x{id:x} {attrs}>".format( |
|
211 klass=self.__class__.__name__, |
|
212 id=id(self), |
|
213 attrs=" ".join(f"{k}={v!r}" for k, v in show_attrs), |
|
214 ) |
|
215 |
|
216 |
|
217 def simplify(v): # pragma: debugging |
|
218 """Turn things which are nearly dict/list/etc into dict/list/etc.""" |
|
219 if isinstance(v, dict): |
|
220 return {k:simplify(vv) for k, vv in v.items()} |
|
221 elif isinstance(v, (list, tuple)): |
|
222 return type(v)(simplify(vv) for vv in v) |
|
223 elif hasattr(v, "__dict__"): |
|
224 return simplify({'.'+k: v for k, v in v.__dict__.items()}) |
|
225 else: |
|
226 return v |
|
227 |
|
228 |
|
229 def pp(v): # pragma: debugging |
|
230 """Debug helper to pretty-print data, including SimpleNamespace objects.""" |
|
231 # Might not be needed in 3.9+ |
|
232 pprint.pprint(simplify(v)) |
|
233 |
|
234 |
|
235 def filter_text(text, filters): |
|
236 """Run `text` through a series of filters. |
|
237 |
|
238 `filters` is a list of functions. Each takes a string and returns a |
|
239 string. Each is run in turn. |
|
240 |
|
241 Returns: the final string that results after all of the filters have |
|
242 run. |
|
243 |
|
244 """ |
|
245 clean_text = text.rstrip() |
|
246 ending = text[len(clean_text):] |
|
247 text = clean_text |
|
248 for fn in filters: |
|
249 lines = [] |
|
250 for line in text.splitlines(): |
|
251 lines.extend(fn(line).splitlines()) |
|
252 text = "\n".join(lines) |
|
253 return text + ending |
|
254 |
|
255 |
|
256 class CwdTracker: # pragma: debugging |
|
257 """A class to add cwd info to debug messages.""" |
|
258 def __init__(self): |
|
259 self.cwd = None |
|
260 |
|
261 def filter(self, text): |
|
262 """Add a cwd message for each new cwd.""" |
|
263 cwd = os.getcwd() |
|
264 if cwd != self.cwd: |
|
265 text = f"cwd is now {cwd!r}\n" + text |
|
266 self.cwd = cwd |
|
267 return text |
|
268 |
|
269 |
|
270 class DebugOutputFile: # pragma: debugging |
|
271 """A file-like object that includes pid and cwd information.""" |
|
272 def __init__(self, outfile, show_process, filters): |
|
273 self.outfile = outfile |
|
274 self.show_process = show_process |
|
275 self.filters = list(filters) |
|
276 |
|
277 if self.show_process: |
|
278 self.filters.insert(0, CwdTracker().filter) |
|
279 self.write(f"New process: executable: {sys.executable!r}\n") |
|
280 self.write("New process: cmd: {!r}\n".format(getattr(sys, 'argv', None))) |
|
281 if hasattr(os, 'getppid'): |
|
282 self.write(f"New process: pid: {os.getpid()!r}, parent pid: {os.getppid()!r}\n") |
|
283 |
|
284 SYS_MOD_NAME = '$coverage.debug.DebugOutputFile.the_one' |
|
285 |
|
286 @classmethod |
|
287 def get_one(cls, fileobj=None, show_process=True, filters=(), interim=False): |
|
288 """Get a DebugOutputFile. |
|
289 |
|
290 If `fileobj` is provided, then a new DebugOutputFile is made with it. |
|
291 |
|
292 If `fileobj` isn't provided, then a file is chosen |
|
293 (COVERAGE_DEBUG_FILE, or stderr), and a process-wide singleton |
|
294 DebugOutputFile is made. |
|
295 |
|
296 `show_process` controls whether the debug file adds process-level |
|
297 information, and filters is a list of other message filters to apply. |
|
298 |
|
299 `filters` are the text filters to apply to the stream to annotate with |
|
300 pids, etc. |
|
301 |
|
302 If `interim` is true, then a future `get_one` can replace this one. |
|
303 |
|
304 """ |
|
305 if fileobj is not None: |
|
306 # Make DebugOutputFile around the fileobj passed. |
|
307 return cls(fileobj, show_process, filters) |
|
308 |
|
309 # Because of the way igor.py deletes and re-imports modules, |
|
310 # this class can be defined more than once. But we really want |
|
311 # a process-wide singleton. So stash it in sys.modules instead of |
|
312 # on a class attribute. Yes, this is aggressively gross. |
|
313 the_one, is_interim = sys.modules.get(cls.SYS_MOD_NAME, (None, True)) |
|
314 if the_one is None or is_interim: |
|
315 if fileobj is None: |
|
316 debug_file_name = os.environ.get("COVERAGE_DEBUG_FILE", FORCED_DEBUG_FILE) |
|
317 if debug_file_name in ("stdout", "stderr"): |
|
318 fileobj = getattr(sys, debug_file_name) |
|
319 elif debug_file_name: |
|
320 fileobj = open(debug_file_name, "a") |
|
321 else: |
|
322 fileobj = sys.stderr |
|
323 the_one = cls(fileobj, show_process, filters) |
|
324 sys.modules[cls.SYS_MOD_NAME] = (the_one, interim) |
|
325 return the_one |
|
326 |
|
327 def write(self, text): |
|
328 """Just like file.write, but filter through all our filters.""" |
|
329 self.outfile.write(filter_text(text, self.filters)) |
|
330 self.outfile.flush() |
|
331 |
|
332 def flush(self): |
|
333 """Flush our file.""" |
|
334 self.outfile.flush() |
|
335 |
|
336 |
|
337 def log(msg, stack=False): # pragma: debugging |
|
338 """Write a log message as forcefully as possible.""" |
|
339 out = DebugOutputFile.get_one(interim=True) |
|
340 out.write(msg+"\n") |
|
341 if stack: |
|
342 dump_stack_frames(out=out, skip=1) |
|
343 |
|
344 |
|
345 def decorate_methods(decorator, butnot=(), private=False): # pragma: debugging |
|
346 """A class decorator to apply a decorator to methods.""" |
|
347 def _decorator(cls): |
|
348 for name, meth in inspect.getmembers(cls, inspect.isroutine): |
|
349 if name not in cls.__dict__: |
|
350 continue |
|
351 if name != "__init__": |
|
352 if not private and name.startswith("_"): |
|
353 continue |
|
354 if name in butnot: |
|
355 continue |
|
356 setattr(cls, name, decorator(meth)) |
|
357 return cls |
|
358 return _decorator |
|
359 |
|
360 |
|
361 def break_in_pudb(func): # pragma: debugging |
|
362 """A function decorator to stop in the debugger for each call.""" |
|
363 @functools.wraps(func) |
|
364 def _wrapper(*args, **kwargs): |
|
365 import pudb |
|
366 sys.stdout = sys.__stdout__ |
|
367 pudb.set_trace() |
|
368 return func(*args, **kwargs) |
|
369 return _wrapper |
|
370 |
|
371 |
|
372 OBJ_IDS = itertools.count() |
|
373 CALLS = itertools.count() |
|
374 OBJ_ID_ATTR = "$coverage.object_id" |
|
375 |
|
376 def show_calls(show_args=True, show_stack=False, show_return=False): # pragma: debugging |
|
377 """A method decorator to debug-log each call to the function.""" |
|
378 def _decorator(func): |
|
379 @functools.wraps(func) |
|
380 def _wrapper(self, *args, **kwargs): |
|
381 oid = getattr(self, OBJ_ID_ATTR, None) |
|
382 if oid is None: |
|
383 oid = f"{os.getpid():08d} {next(OBJ_IDS):04d}" |
|
384 setattr(self, OBJ_ID_ATTR, oid) |
|
385 extra = "" |
|
386 if show_args: |
|
387 eargs = ", ".join(map(repr, args)) |
|
388 ekwargs = ", ".join("{}={!r}".format(*item) for item in kwargs.items()) |
|
389 extra += "(" |
|
390 extra += eargs |
|
391 if eargs and ekwargs: |
|
392 extra += ", " |
|
393 extra += ekwargs |
|
394 extra += ")" |
|
395 if show_stack: |
|
396 extra += " @ " |
|
397 extra += "; ".join(_clean_stack_line(l) for l in short_stack().splitlines()) |
|
398 callid = next(CALLS) |
|
399 msg = f"{oid} {callid:04d} {func.__name__}{extra}\n" |
|
400 DebugOutputFile.get_one(interim=True).write(msg) |
|
401 ret = func(self, *args, **kwargs) |
|
402 if show_return: |
|
403 msg = f"{oid} {callid:04d} {func.__name__} return {ret!r}\n" |
|
404 DebugOutputFile.get_one(interim=True).write(msg) |
|
405 return ret |
|
406 return _wrapper |
|
407 return _decorator |
|
408 |
|
409 |
|
410 def _clean_stack_line(s): # pragma: debugging |
|
411 """Simplify some paths in a stack trace, for compactness.""" |
|
412 s = s.strip() |
|
413 s = s.replace(os.path.dirname(__file__) + '/', '') |
|
414 s = s.replace(os.path.dirname(os.__file__) + '/', '') |
|
415 s = s.replace(sys.prefix + '/', '') |
|
416 return s |