Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
gevent fix for code coverage
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
ggw  
View profile  
 More options Jul 3 2012, 5:19 pm
From: ggw <jonathan.newbro...@gyregroup.com>
Date: Tue, 3 Jul 2012 14:19:26 -0700 (PDT)
Local: Tues, Jul 3 2012 5:19 pm
Subject: gevent fix for code coverage

I see already reported on the forum that coverage.py and gevent don't play
nicely with each other.  I think I have a fix for that:

    def switch(self):
        cur = getcurrent()
        assert cur is not self, 'Cannot switch to MAINLOOP from MAINLOOP'
        exc_type, exc_value = sys.exc_info()[:2]
*        trace = sys.gettrace()
        sys.settrace(None)
*        try:
            switch_out = getattr(cur, 'switch_out', None)
            if switch_out is not None:
                try:
                    switch_out()
                except:
                    traceback.print_exc()
            sys.exc_clear()
            return greenlet.switch(self)
        finally:
            core.set_exc_info(exc_type, exc_value)
*            sys.settrace(trace)

*I am fairly new to python and gevent, so I'm not sure if this might break
anything for anyone else, but it seems to restore coverage reporting.  The
issue seems to be that sys.settrace() used by coverage.py doesn't like to
see the gevent frames without expecting them.  changing the trace to None
and restoring after hub.switch seems to fix the issue and restore correct
coverage results.  Note to coverage users: you have to specifically enable
"timid" in coverage tools.  I'm not sure why this doesn't help with the
pre-compiled trace function.

Has anyone tried anything like this?  Any issues you can think of?

Jonathan


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Denis Bilenko  
View profile  
 More options Jul 5 2012, 3:12 am
From: Denis Bilenko <denis.bile...@gmail.com>
Date: Thu, 5 Jul 2012 11:12:28 +0400
Local: Thurs, Jul 5 2012 3:12 am
Subject: Re: [gevent] gevent fix for code coverage

On Wed, Jul 4, 2012 at 1:19 AM, ggw <jonathan.newbro...@gyregroup.com> wrote:
> I am fairly new to python and gevent, so I'm not sure if this might break
> anything for anyone else, but it seems to restore coverage reporting.

Did you check if spawned greenlets are also covered by coverage.py
with this patch? From the code it seems that trace function is None
most of the time, except when the MAIN greenlet is running, so event
loop callbacks and spawned greenlets are excluded.

>The issue seems to be that sys.settrace() used by coverage.py doesn't like to
> see the gevent frames without expecting them.

I'm not sure what do you mean by 'gevent frame', so it's hard to
comment on it and everything below.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
ggw  
View profile   Translate to Translated (View Original)
 More options Jul 24 2012, 7:47 pm
From: ggw <jonathan.newbro...@gyregroup.com>
Date: Tue, 24 Jul 2012 16:47:24 -0700 (PDT)
Local: Tues, Jul 24 2012 7:47 pm
Subject: Re: [gevent] gevent fix for code coverage

> ...event loop callbacks and spawned greenlets are excluded...

That helps!  I can now handle spawned greenlets, but I'm still a little
unclear on the event loop callbacks.  Is there an example you can point me
to?

> I'm not sure what do you mean by 'gevent frame'

Not sure if python uses the same terms, but I'm used to describing my
location with the terms thread, stack and frame.  A thread is a path of
execution through the program (possibly simultaneous with other threads), a
stack  describes the nested calls made by the thread, a frame is a single
layer on the stack (a function call, with associated local variables and
arguments).

The issue I was raising with "gevent frame" was that the coverage trace
function kept a representation of the stack and expected it to remain the
same.  So if we are running coverage on a function with a blocking call in
the middle, the stack before and after that blocking call may look
different.  After the blocking call, the stack starts with gevent's
Hub.switch() but before the blocking call the stack looked different -- the
trace function found a frame there that hadn't been there before the
blocking function.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
ggw  
View profile   Translate to Translated (View Original)
 More options Jul 24 2012, 7:56 pm
From: ggw <jonathan.newbro...@gyregroup.com>
Date: Tue, 24 Jul 2012 16:56:06 -0700 (PDT)
Local: Tues, Jul 24 2012 7:56 pm
Subject: Re: [gevent] gevent fix for code coverage

The latest implementation works for our project's unit tests but requires
that both coverage and gevent have slight changes.  Here are public
repositories in case anyone wants to try out the combination.  One caveat
-- so far, this works for line coverage metrics, but disables branch
coverage.  There is certainly work to do, but for now I'm just hoping
anyone out there can point out cases that won't show correct coverage
metrics or places I might break gevent.

Each project for now just has two commits.  The first is the untouched
original code from the gevent or coverage repository.  The next commit is
my changes.  Just a few lines in a couple files.

coverage source <https://github.com/newbrough/coverage> - edited trace
function to ignore the unexpected change to the stack after a blocking call
gevent source <https://github.com/newbrough/gevent> - edited Hub.switch to
save and restore the trace function; Greenlet.__init__ to apply the global
trace function set with threading.settrace


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions Older topic »