Comments (10)
I changed the code parsing to ignore the branch from the except clause to the next. Fixed in <<changeset e01bfcdcc17f (bb)>>.
from coveragepy.
Original comment by Gary Bernhardt (Bitbucket: garybernhardt, GitHub: garybernhardt)
Here's my reasoning about it: In Python, any line could potentially throw an exception. Code within a try without a catch-all except isn't actually any different in this regard (unless I'm missing something). So if we count potentially uncaught exceptions within a try as branches, we really should count every non-trivial line of code in that way, which we clearly don't want. :)
from coveragepy.
Gary, there's another possibility: the bytecode for except clauses has a distinctive signature:
COMPARE_OP 10 (exception match)
But I'm also wondering whether it's right to hide the comparison being done here. And underlying all the uncertainty is the fact that no coverage theory out there seems to consider exception handling in the first place...
from coveragepy.
Gary, that might be what's required. In fact, it's very similar to the way I'm dealing with the "class is a branch" problem (<<issue 32>>).
from coveragepy.
Original comment by Gary Bernhardt (Bitbucket: garybernhardt, GitHub: garybernhardt)
I spent some more time trying to fix it, but it looks quite hard because it mixes levels in a way that coverage doesn't have to deal with currently. The generic except check is done by comparing the name of the thrown exception to each of the named except: blocks (LOAD_NAME/COMPARE_OP/JUMP_IF_FALSE). I tried to put in a horrible hack where I cross-referenced the line number of the current byte code against the original text of the code, and avoided adding it as an exit point if I was at the except: line. But then I started getting a headache and gave up. :(
This probably isn't useful to you, given the amount of time you've spent with this stuff, but I thought I'd share. :) Thanks for all of the work you've done so far; I use coverage hundreds of times in an average day.
from coveragepy.
Gary, I admire your fortitude attempting to add that test! The check_coverage method won't even attempt branch measurement if the arcz argument is omitted.
from coveragepy.
Original comment by Gary Bernhardt (Bitbucket: garybernhardt, GitHub: garybernhardt)
That explanation makes sense, although I still don't understand why my test passed. It seems like the third invisible branch should've been in arcz_missing. :)
from coveragepy.
Unfortunately, an except clause with a type is actually a three-way branch. The case you are missing is a non-IndexError exception. But I agree that this is very counterintuitive and probably needs to be fixed. A few details are at http://nedbatchelder.com/code/coverage/branches_html/branches.html
from coveragepy.
Original comment by Gary Bernhardt (Bitbucket: garybernhardt, GitHub: garybernhardt)
I started trying to figure this out, but became quite confused when the following test passed without making changes to coverage:
#!python
def test_try_except_when_both_are_executed(self):
self.check_coverage("""\
a = []
def foo(a_list):
try:
a_list[0]
a.append('first')
except IndexError:
a.append('second')
foo([0])
foo([])
assert a == ['first', 'second']
""",
arcz_missing="")
It seems like the transitions are being recorded at a lower level, but the high-level reporter doesn't report that.
from coveragepy.
Original comment by Gary Bernhardt (Bitbucket: garybernhardt, GitHub: garybernhardt)
Let's try that again...
#!python
def foo(a_list):
try:
a_list[0]
except IndexError:
pass
foo([])
foo([0])
from coveragepy.
Related Issues (20)
- Exclude code coverage based on operating system HOT 3
- Branches shown where there are none
- [FR] Loading/disabling plugins conditionally HOT 1
- is this option `COVERAGE_STORAGE=json` still available on latest version HOT 2
- Out-of-the-box behavior unexpected by new users / Modify FAQ/QuickStart/Doc HOT 6
- Question: Include all files in --source even unloaded HOT 6
- cannot create an xml report HOT 9
- Ignored warning induced bug
- coverage-7.3.3 broke pragma: no cover in match-case statement HOT 18
- Move from pylint to ruff? HOT 1
- Unable to collect code coverage when using `daphne` / `ChannelsLiveServerTestCase`
- no coverage file when run python file with supervisor
- 7.3.3 isn't "latest" on Read the Docs HOT 2
- Consider changing default value for precision from 0 to 2 HOT 3
- 7.3.4: pytest is failing im many units HOT 7
- Allow pyproject.toml to override a global coveragerc
- sysmon coverage core is slower than default HOT 4
- HTML report filter not working properly HOT 2
- `Erase` fails with local plugins due to incorrect path
- 100% covered when much is uncovered HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from coveragepy.