Python's stdlib uses context managers to store the trace function before it gets modified and then sets it back later when the test exits; never even attempted to make coverage work while using another tracer. But we also benefit from code in our custom test runner to notice when the trace function is not put back after a test is run.<br>
<br><div class="gmail_quote">On Thu, Dec 29, 2011 at 13:49, Ned Batchelder <span dir="ltr"><<a href="mailto:ned@nedbatchelder.com">ned@nedbatchelder.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div bgcolor="#FFFFFF" text="#000000">
Yeah, sys.settrace() could be more sophisticated. However you are
setting the trace function, I would think you could manage to set it
back somehow. But even if you could, you wouldn't be getting
coverage measurement for the time your trace function was in effect,
unless you also call my trace function from yours. A mess all
around.<span class="HOEnZb"><font color="#888888"><br>
<br>
--Ned.</font></span><div><div class="h5"><br>
<br>
On 12/29/2011 4:22 AM, Leahy, Oliver wrote:
<blockquote type="cite">
<div>
<p class="MsoNormal"><span style="color:#1f497d">Thanks for the
suggestions, I’ve tried the –timid flag, hasn’t helped. And
unfortunately<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:#1f497d">(for me) it
looks like I can’t successfully reset the trace function
with settrace() because of<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:#1f497d">the way my
trace functions work; I raise exceptions when I reached
specified points in the<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:#1f497d">code, so once
the exception is raised I don’t get an opportunity to call
sys.settrace().
<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="color:#1f497d">So I’m going to
have to use mocks, as you suggest. What I really want is for
sys.settrace() to<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:#1f497d">be able to
manage a stack of functions that would be called in sequence
</span><span style="font-family:Wingdings;color:#1f497d">J</span><span style="color:#1f497d"><u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="color:#1f497d">Thanks again,<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:#1f497d">Ollie<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:#1f497d"><u></u> <u></u></span></p>
<div>
<div style="border:none;border-top:solid #b5c4df 1.0pt;padding:3.0pt 0cm 0cm 0cm">
<p class="MsoNormal"><b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif";color:windowtext">From:</span></b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif";color:windowtext">
Ned Batchelder [<a href="mailto:ned@nedbatchelder.com" target="_blank">mailto:ned@nedbatchelder.com</a>]
<br>
<b>Sent:</b> 28 December 2011 21:44<br>
<b>To:</b> Leahy, Oliver<br>
<b>Cc:</b> <a href="mailto:testing-in-python@lists.idyll.org" target="_blank">testing-in-python@lists.idyll.org</a><br>
<b>Subject:</b> Re: [TIP] Using coverage.py with code
that uses sys.settrace()<u></u><u></u></span></p>
</div>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">On 12/28/2011 10:58 AM, Leahy, Oliver
wrote: <u></u><u></u></p>
<p class="MsoNormal">What’s the recommended way to use
coverage.py with code that uses sys.settrace()<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">I’m using settrace() to simulate error
conditions at certain lines during testing, but<u></u><u></u></p>
<p class="MsoNormal">this interferes with the use coverage.py
makes of settrace() and I’m getting incorrect<u></u><u></u></p>
<p class="MsoNormal">coverage metrics.<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">I’m hacking at my code to try to store an
existing value for sys.gettrace() whenever<u></u><u></u></p>
<p class="MsoNormal">I call settrace(), and then restoring the
pre-existing trace function whenever I stop<u></u><u></u></p>
<p class="MsoNormal">tracing using my function, but it’s not
working for me at the moment.<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">Is this a sensible approach to this
problem? Would anyone have any other suggestions?<u></u><u></u></p>
<p class="MsoNormal"><span>I don't know if anyone has
tried measuring code that uses settrace. You should be able
to use gettrace and settrace to save and restore the
coverage trace function, but I won't be that surprised if it
isn't working. You should try using the --timid flag on
"coverage run" to see if it helps. It's specifically for
environments that do unusual things with settrace(). Are
you sure settrace() is the best way to accomplish your
goals? Mocking can also inject errors into running code, and
may be easier to work with in the long run.<br>
<br>
In any case, if you need more help, post more details.<br>
<br>
--Ned.<br>
<br>
<u></u><u></u></span></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">Thanks,<u></u><u></u></p>
<p class="MsoNormal">Ollie<u></u><u></u></p>
<p class="MsoNormal"><span><br>
<br>
<br>
<u></u><u></u></span></p>
<pre>_______________________________________________<u></u><u></u></pre>
<pre>testing-in-python mailing list<u></u><u></u></pre>
<pre><a href="mailto:testing-in-python@lists.idyll.org" target="_blank">testing-in-python@lists.idyll.org</a><u></u><u></u></pre>
<pre><a href="http://lists.idyll.org/listinfo/testing-in-python" target="_blank">http://lists.idyll.org/listinfo/testing-in-python</a><u></u><u></u></pre>
</div>
</blockquote>
</div></div></div>
<br>_______________________________________________<br>
testing-in-python mailing list<br>
<a href="mailto:testing-in-python@lists.idyll.org">testing-in-python@lists.idyll.org</a><br>
<a href="http://lists.idyll.org/listinfo/testing-in-python" target="_blank">http://lists.idyll.org/listinfo/testing-in-python</a><br>
<br></blockquote></div><br>