files with incorrect encoding are ignored in Python3

Issue #351 resolved
Thomas Tanner
created an issue

I've noticed a module containing a © (0xa9) character and specifying an incorrect PEP 263 source encoding (e.g. "-- coding: utf-8 --" or no encoding instead of "-- coding: latin1 --") is completely ignored by "coverage report -i" with Python 3.4, but not by Python 2.7.9.

This is inconsistent behaviour and surprising if you port from Python 2 to 3.

EDIT: -i flag

Comments (7)

  1. Ned Batchelder repo owner

    @ttanner I made a file like you describe, and am not sure how to get Python to even run the file. Python 2.7 and 3.4 both produce SyntaxErrors when I run the file. Can you attach your file, and show how you run the file, both under coverage.py, and not? Thanks.

  2. Thomas Tanner reporter

    "mypkg/init.py":

    # -*- coding: utf-8 -*-
    
    

    test_my.py:

    import mypkg
    def test(): pass
    

    coverage run --source mypkg -m py.test

    ===================================================== test session starts =====================================================
    platform darwin -- Python 3.4.2 -- py-1.4.26 -- pytest-2.6.4
    collected 1 items
    
    test_my.py .
    
    ================================================== 1 passed in 0.02 seconds ===================================================
    

    coverage report

    Name             Stmts   Miss  Cover
    ------------------------------------
    mypkg/__init__   UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa9 in position 26: invalid start byte
    

    coverage report -i

    Name             Stmts   Miss  Cover
    ------------------------------------
    

    with Python 2.7:

    coverage run --source mypkg -m py.test

    ===================================================== test session starts =====================================================
    platform darwin -- Python 2.7.9 -- py-1.4.26 -- pytest-2.6.4
    collected 1 items
    
    test_my.py .
    
    ================================================== 1 passed in 0.02 seconds ===================================================
    

    coverage report

    Name             Stmts   Miss  Cover
    ------------------------------------
    mypkg/__init__       0      0   100%
    

    coverage report -i

    Name             Stmts   Miss  Cover
    ------------------------------------
    mypkg/__init__       0      0   100%
    
  3. Ned Batchelder repo owner

    @ttanner Thanks, I see what you mean. I'm not sure why this problem is so hard to pin down and eradicate. But reading your original issue, which behavior do you want? My inclination is to accept files with wrong encodings, and report on them anyway. Perhaps with a warning, but my feeling is that if Python can run them, then coverage.py should be able to report on them.

  4. Log in to comment