Fix the recording of arcs for non-Python files
Hi,
While developing new coverage plugins I found that they received invalid arcs when branch coverage was turned on.
An example of an obviously invalid arc is one which contains a line number greater than the actual number of lines in the file. This can happen when the generated python code being executed is longer than the source code it was generated from.
This problem has two causes:
- The tracer mixes mapped and unmapped line numbers. In other words, not all line numbers are passed through the tracer's
line_number_rangemethod before being recorded. - The
DataStackEntrystruct isn't cleared before being reused. Not clearing itslast_lineattribute results in bogus arcs being recorded.
My patch fixes both problems.
I don't often write C code, so please let me know if you see something that I could have done better.
Codecov Report
Merging #1051 (c4f13e0) into master (3274cba) will increase coverage by
0.11%. The diff coverage isn/a.
@@ Coverage Diff @@
## master #1051 +/- ##
==========================================
+ Coverage 93.97% 94.09% +0.11%
==========================================
Files 179 86 -93
Lines 25140 12235 -12905
Branches 2613 1230 -1383
==========================================
- Hits 23626 11512 -12114
+ Misses 1210 582 -628
+ Partials 304 141 -163
Continue to review full report at Codecov.
Legend - Click here to learn more
Δ = absolute <relative> (impact),ø = not affected,? = missing dataPowered by Codecov. Last update 3274cba...c4f13e0. Read the comment docs.
Thanks, I don't often get contributions to the C code! Sorry it's taken me so long to respond. Do you have a way to reproduce the problem this is fixing?
BTW, separately, I'd be interested to hear about the plugin you are working on.
I don't have a simple way of reproducing the problem. I discovered it when running liberapay's test suite with the experimental coverage plugins, and I confirmed that the changes in this branch fix the problem, but I didn't write test code that reproduces it and could be used to prevent regressions in the future.
The plugins I worked on are for Jinja2 and simplates. I obtained some encouraging results, but the work isn't finished, I paused after I opened this pull request.
I've rebased this branch on master, replacing frame->f_lineno with PyFrame_GetLineNumber(frame) like in 7e5e28f1aba87c10b96d0ae1244352f4c520aedc.
since this is so old, updates have happened since, and 3 checks are not being met ... is this something that can be closed?
This patch is still needed. I have now rebased it on master once again, and confirmed that it still fixes the problem.
The patch has become a bit smaller because its previous version modified the CTracer_check_missing_return function which was dropped in 8021196662dcadf161cbeaebb3be4b0392b51803.
The patch has also been modified to use MyFrame_GetCode(frame) instead of frame->f_code, per ab48a7bf66c04754e9964587b2b4f790bb6af8d4.
Can you give me very specific instructions for cloning and running the test suite, and specific pointers to places where the coverage information is wrong? I took a quick look in the liberapay repo and couldn't find a coverage plugin.
The unfinished coverage plugins are now publicly available in the coverage-plugins branch of the liberapay.com repository.
To run the tests, a PostgreSQL server is required. By default the tests try to use the liberapay_tests database of the local server. Once the database is ready, running the tests with coverage tracking should be as simple as running make pytest-cov.
Results with the unpatched version of coverage
- The pytest output contains quite a few warnings like this:
simplate_coverage.py:241: UserWarning: got out of bounds line numbers: [136] > 112 simplate_coverage.py:323: UserWarning: got out of bounds line numbers for file www/on/%platform/index.spt: [(-126, 87), (136, -62)] > 112 - The
htmlcov/index.htmlfile reports a total of 3748 non-covered ("missing") lines. - The HTML coverage report for the
templates/exceptions/EmailAddressError.htmlfile erroneously claims that line 26 has been run while the surrounding lines in the same code branch haven't been.
Results with the patch
- There are no
out of boundswarnings in the pytest output. - The
htmlcov/index.htmlfile reports a total of 3857 non-covered ("missing") lines. - The HTML coverage report for the
templates/exceptions/EmailAddressError.htmlfile correctly shows that line 26 has not been run.