Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code executed by exec excluded from coverage #380

Closed
nedbat opened this issue Jul 1, 2015 · 6 comments
Closed

Code executed by exec excluded from coverage #380

nedbat opened this issue Jul 1, 2015 · 6 comments
Labels
bug Something isn't working

Comments

@nedbat
Copy link
Owner

nedbat commented Jul 1, 2015

Originally reported by mb2297 (Bitbucket: mb2297, GitHub: Unknown)


It might be unrealistic to expect this to work, but python files that are read and then executed with exec (or with execfile) aren't counted towards the coverage report.

Example:

#!python
# impl.py

def foo():
    print "foo"
 
def bar():
    print "bar"
#!python
# impl2.py

print "the answer to life, the universe and everything is: {}".format(undeclared_var)
#!python
# test.py

from impl import foo
 
foo()
 
namespace = {
    'undeclared_var': 42
}
 
with open('impl2.py') as f:
    code = compile(f.read(), 'impl2.py', 'exec')
    exec(code, globals(), namespace)

Results in the following on both OS X and Ubuntu:

$ coverage run --source . test.py
foo
the answer to life, the universe and everything is: 42
$ coverage report
Name    Stmts   Miss  Cover
---------------------------
impl        4      1    75%
impl2       1      1     0%
test        6      0   100%
---------------------------
TOTAL      11      2    82%

The example is also at https://gist.github.com/mattbennett/f57a5286bd77ee52651e if you want to clone and verify.


@nedbat
Copy link
Owner Author

nedbat commented Jul 2, 2015

Looks like exec'ing code gets the called code and the calling code confused in the frame. I've added a warning when this happens here: b981d945481a (bb). There's probably a way to get this right, still working on it.

@nedbat
Copy link
Owner Author

nedbat commented Jul 11, 2015

If file disagrees with the frame, use the frame info. Fixes #380.

→ <<cset 2372daa1d74b (bb)>>

@nedbat
Copy link
Owner Author

nedbat commented Jul 11, 2015

@nedbat nedbat closed this as completed Jul 11, 2015
@nedbat nedbat added minor bug Something isn't working labels Jun 23, 2018
blueyed added a commit to blueyed/pdbpp that referenced this issue Nov 2, 2019
@blueyed
Copy link
Contributor

blueyed commented Nov 2, 2019

I've just came across this, and wanted to note that it does not work when the exec'd (base) filename is the same, as with pdbpp's hack: pdbpp/pdbpp@f67ad89
Setting __file__ explicitly helped, and I wanted to do this anyway already.
Also renaming the exec'd (main) file to pdbpp.py is planned.

@brondsem
Copy link

brondsem commented May 14, 2021

Another note: I had code like exec(fp.read()) which wasn't working. I had to change it to exec(compile(fp.read(), filename, 'exec')) for coverage to be recorded. I didn't have to set __file__.

@rohitsharma1113
Copy link

rohitsharma1113 commented Oct 19, 2022

How can I include code executed with exec ?
The links in this discussion are broken (bit bucket)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants