diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2015-04-20 12:15:37 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2015-04-20 12:15:37 -0400 |
commit | 30a6a037158eebf062c7da735e0cb905a489d21c (patch) | |
tree | 851e6073d790fa7d1b6d1fd69e9d89345b0b1e81 /coverage/pytracer.py | |
parent | 6246b2bb47198e68341cfc8967274edbb751c43e (diff) | |
download | python-coveragepy-30a6a037158eebf062c7da735e0cb905a489d21c.tar.gz |
Fix branch coverage for yield statements. #308 #324
Turns out the "call" and "return" trace events are really "start frame" and
"end frame". They happen not only when functions are entered and left, but
when generators yield and resume. We aren't interested in arcs into and out
of yield statements, so the trace functions look more closely to see what's
really happening, and record an arc in human-friendly terms.
Thanks for Mickie Betz for pushing on this bug, although her code is no
longer here. :(
Diffstat (limited to 'coverage/pytracer.py')
-rw-r--r-- | coverage/pytracer.py | 24 |
1 files changed, 19 insertions, 5 deletions
diff --git a/coverage/pytracer.py b/coverage/pytracer.py index 0eafbef..3f03aaf 100644 --- a/coverage/pytracer.py +++ b/coverage/pytracer.py @@ -1,7 +1,15 @@ """Raw data collector for Coverage.""" +import dis import sys +from coverage import env + +# We need the YIELD_VALUE opcode below, in a comparison-friendly form. +YIELD_VALUE = dis.opmap['YIELD_VALUE'] +if env.PY2: + YIELD_VALUE = chr(YIELD_VALUE) + class PyTracer(object): """Python implementation of the raw data tracer.""" @@ -79,9 +87,11 @@ class PyTracer(object): if tracename not in self.data: self.data[tracename] = {} self.cur_file_dict = self.data[tracename] - # Set the last_line to -1 because the next arc will be entering a - # code block, indicated by (-1, n). - self.last_line = -1 + # The call event is really a "start frame" event, and happens for + # function calls and re-entering generators. The f_lasti field is + # -1 for calls, and a real offset for generators. Use -1 as the + # line number for calls, and the real line number for generators. + self.last_line = -1 if (frame.f_lasti < 0) else frame.f_lineno elif event == 'line': # Record an executed line. if self.cur_file_dict is not None: @@ -93,8 +103,12 @@ class PyTracer(object): self.last_line = lineno elif event == 'return': if self.arcs and self.cur_file_dict: - first = frame.f_code.co_firstlineno - self.cur_file_dict[(self.last_line, -first)] = None + # Record an arc leaving the function, but beware that a + # "return" event might just mean yielding from a generator. + bytecode = frame.f_code.co_code[frame.f_lasti] + if bytecode != YIELD_VALUE: + first = frame.f_code.co_firstlineno + self.cur_file_dict[(self.last_line, -first)] = None # Leaving this function, pop the filename stack. self.cur_file_dict, self.last_line = self.data_stack.pop() elif event == 'exception': |