Skip to content

Commit

Permalink
Don't trace non-encodable file names. #891
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Jan 1, 2020
1 parent 5bb5da5 commit 6c3221b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,19 @@ Unreleased
information about the config files read now shows absolute paths to the
files.

- The handling of source files with non-encodable file names has changed.
Previously, if a file name could not be encoded as UTF-8, an error occurred,
as described in `issue 891`_. Now, those files will not be measured, since
their data would not be recordable.

- A new warning ("dynamic-conflict") is issued if two mechanisms are trying to
change the dynamic context. Closes `issue 901`_.

- ``coverage run --debug=sys`` would fail with an AttributeError. This is now
fixed (`issue 907`_).

.. _issue 890: https://github.com/nedbat/coveragepy/issues/890
.. _issue 891: https://github.com/nedbat/coveragepy/issues/891
.. _issue 901: https://github.com/nedbat/coveragepy/issues/901
.. _issue 907: https://github.com/nedbat/coveragepy/issues/907

Expand Down
9 changes: 8 additions & 1 deletion coverage/inorout.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from coverage.disposition import FileDisposition, disposition_init
from coverage.files import TreeMatcher, FnmatchMatcher, ModuleMatcher
from coverage.files import prep_patterns, find_python_files, canonical_filename
from coverage.misc import CoverageException
from coverage.misc import CoverageException, contract
from coverage.python import source_for_file, source_for_morf


Expand Down Expand Up @@ -296,6 +296,7 @@ def nope(disp, reason):

return disp

@contract(filename='unicode')
def check_include_omit_etc(self, filename, frame):
"""Check a file name against the include, omit, etc, rules.
Expand Down Expand Up @@ -333,6 +334,12 @@ def check_include_omit_etc(self, filename, frame):
if self.omit_match and self.omit_match.match(filename):
return "is inside an --omit pattern"

# No point tracing a file we can't later write to SQLite.
try:
filename.encode("utf8")
except UnicodeEncodeError:
return "non-encodable filename"

# No reason found to skip this file.
return None

Expand Down
13 changes: 13 additions & 0 deletions tests/test_oddball.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

"""Oddball cases for testing coverage.py"""

import os.path
import sys

from flaky import flaky
Expand Down Expand Up @@ -562,6 +563,18 @@ def test_correct_filename(self):
self.assertEqual(statements, [31])
self.assertEqual(missing, [])

def test_unencodable_filename(self):
# https://github.com/nedbat/coveragepy/issues/891
if env.PYVERSION < (3, 0):
self.skipTest("Python 2 can't seem to compile the file.")
self.make_file("bug891.py", r"""exec(compile("pass", "\udcff.py", "exec"))""")
cov = coverage.Coverage()
self.start_import_stop(cov, "bug891")
# Saving would fail trying to encode \udcff.py
cov.save()
files = [os.path.basename(f) for f in cov.get_data().measured_files()]
assert "bug891.py" in files


class MockingProtectionTest(CoverageTest):
"""Tests about protecting ourselves from aggressive mocking.
Expand Down

0 comments on commit 6c3221b

Please sign in to comment.