From 279d71b0068a26c5c5233f057beef2967194b596 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Mon, 2 Jan 2017 11:28:39 -0500 Subject: Small improvements to the debug stuff --- coverage/debug.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/coverage/debug.py b/coverage/debug.py index c5c9924..91ccba7 100644 --- a/coverage/debug.py +++ b/coverage/debug.py @@ -132,6 +132,14 @@ def dump_stack_frames(limit=None, out=None, skip=0): out.write("\n") +def short_id(id64): + """Given a 64-bit id, make a shorter 16-bit one.""" + id16 = 0 + for offset in range(0, 64, 16): + id16 ^= id64 >> offset + return id16 & 0xFFFF + + class DebugOutputFile(object): # pragma: debugging """A file-like object that includes pid and cwd information.""" def __init__(self, outfile): @@ -171,17 +179,13 @@ class DebugOutputFile(object): # pragma: debugging def _write(self, text, stack=False): """The raw text-writer, so that we can use it ourselves.""" # Thread ids are useful, but too long. Make a shorter one. - tid = _thread.get_ident() - stid = 0 - for byte in range(4): - stid ^= tid >> (16 * byte) - stid = "{0:04x}".format(stid & 0xFFFF) + tid = "{0:04x}".format(short_id(_thread.get_ident())) # Aspectlib prints stack traces, but includes its own frames. Scrub those out: # <<< aspectlib/__init__.py:257:function_wrapper < igor.py:143:run_tests < ... text = re.sub(r"(?<= )aspectlib/[^.]+\.py:\d+:\w+ < ", "", text) - self.outfile.write("{0:5d}.{1}: {2}".format(os.getpid(), stid, text)) + self.outfile.write("{0:5d}.{1}: {2}".format(os.getpid(), tid, text)) self.outfile.flush() if stack: dump_stack_frames(out=self.outfile, skip=1) @@ -213,7 +217,9 @@ def enable_aspectlib_maybe(): # pragma: debugging import aspectlib.debug # pylint: disable=import-error aspects_file = DebugOutputFile.the_one() - aspect_log = aspectlib.debug.log(print_to=aspects_file, attributes=['id'], stacktrace=30, use_logging=False) + aspect_log = aspectlib.debug.log( + print_to=aspects_file, attributes=['id'], stacktrace=30, use_logging=False + ) public_methods = re.compile(r'^(__init__|[a-zA-Z].*)$') for aspect in aspects.split(':'): aspectlib.weave(aspect, aspect_log, methods=public_methods) -- cgit v1.2.1