KeyError: sub.py · Issue #340 · nedbat/coveragepy (original) (raw)
Originally reported by Buck Evan (Bitbucket: bukzor, GitHub: bukzor)
This reproduces when using the ubuntu 2.7 from precise forward.
ERROR: test_subprocess_with_pth_files (tests.test_process.ProcessStartupTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/buck/trees/mine/coverage.py/tests/test_process.py", line 673, in test_subprocess_with_pth_files
self.assertEqual(data.summary()['sub.py'], 2)
KeyError: 'sub.py'
You can reproduce the scenario with:
mkdir $VIRTUALENV/local
ln -s <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>V</mi><mi>I</mi><mi>R</mi><mi>T</mi><mi>U</mi><mi>A</mi><mi>L</mi><mi>E</mi><mi>N</mi><mi>V</mi><mi mathvariant="normal">/</mi><mi>l</mi><mi>i</mi><mi>b</mi></mrow><annotation encoding="application/x-tex">VIRTUALENV/lib </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.22222em;">V</span><span class="mord mathnormal" style="margin-right:0.07847em;">I</span><span class="mord mathnormal" style="margin-right:0.13889em;">RT</span><span class="mord mathnormal" style="margin-right:0.10903em;">U</span><span class="mord mathnormal">A</span><span class="mord mathnormal">L</span><span class="mord mathnormal" style="margin-right:0.10903em;">EN</span><span class="mord mathnormal" style="margin-right:0.22222em;">V</span><span class="mord">/</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">ib</span></span></span></span>VIRTUALENV/local/lib
The essential cause is that site.py is seeing and executing subcover.py
twice because of the symlink, first for local/lib/ then for lib/.
MAKEPATH: ('/home/buck/venv/coverage.py/local/lib/python2.7/site-packages',)
ADDPACKAGE: subcover.pth
MAKEPATH: ('/home/buck/venv/coverage.py/lib/python2.7/site-packages',)
ADDPACKAGE: subcover.pth
I don't quite follow the part in between, but the result is that only the data from the first trace persists, meaning we don't get the data we want. Presumably the trace of sub.py is written first, then the earlier trace data written on top of it.
The essential problem is that if a single process calls process_startup() twice, data is lost.
Worse, data generated by the second call is clobbered by data from the first.
I believe the fix would be to make coverage process_startup() idempotent.
Failing that, ensuring a last-one-wins behavior would also produce desirable behavior in this case.
@nedbat: How would you go about solving this problem?
(Most of this information copied from an older ticket centered on another topic: issue #337)