tempfile module bug

Issue #336 invalid
Okko Willeboordse
created an issue

Consider the following code;

import tempfile
import coverage

c = coverage.coverage(source = ['io'])
c.start()
import io
c.stop()
print c.html_report()

With the tempfile import the script gives the following exception;

Coverage.py warning: No data was collected.
Traceback (most recent call last):
  File "test.py", line 8, in <module>
    print c.html_report()
  File "C:\Python27\lib\site-packages\coverage\control.py", line 662, in html_report
    return reporter.report(morfs)
  File "C:\Python27\lib\site-packages\coverage\html.py", line 113, in report
    self.report_files(self.html_file, morfs, self.config.html_dir)
  File "C:\Python27\lib\site-packages\coverage\report.py", line 76, in report_files
    raise CoverageException("No data to report.")
coverage.misc.CoverageException: No data to report.

Without tempfile import the script prints 100.0 which is correct in my case (io is empty).

If I add;

def okko(): 
  pass 

to io and;

print io.okko()

to above script, it prints 50.0 (incorrect) with tempfile import and 100.0 (correct) without tempfile import.

Comments (3)

  1. Ned Batchelder repo owner

    io is a module in the standard library. tempfile must be importing it. It's finding your io.py, so the import happens before coverage is started.

    You have a few options:

    1) rename your module so that it doesn't shadow a standard library module.

    2) Don't use the coverage API for your program, but instead, use the coverage command line to run your program: coverage run my_program.py. This is easier, and will assure that all of your code is measured.

  2. Okko Willeboordse reporter

    Your analysis is correct! My module name really is 'odin.io' so there will be no problems.

    I was just experimenting with coverage a thought I had made things simpler in my experimentation setup by removing the odin layer :-(.

    Thanks for your help

  3. Log in to comment