summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2020-01-01 12:41:15 -0500
committerNed Batchelder <ned@nedbatchelder.com>2020-01-01 13:15:42 -0500
commit016af5f6352d69206ac8f7537c2b18828767bcae (patch)
tree0b93d1381754486e769e480835ee7bf011c4720f
parent5bb5da50b182583036b7808bb32f2c8c191d9d26 (diff)
downloadpython-coveragepy-git-016af5f6352d69206ac8f7537c2b18828767bcae.tar.gz
Don't trace non-encodable file names. #891
-rw-r--r--CHANGES.rst6
-rw-r--r--coverage/inorout.py6
-rw-r--r--tests/test_oddball.py13
3 files changed, 25 insertions, 0 deletions
diff --git a/CHANGES.rst b/CHANGES.rst
index 40e8d2d2..226e3701 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -30,6 +30,11 @@ 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`_.
@@ -37,6 +42,7 @@ Unreleased
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
diff --git a/coverage/inorout.py b/coverage/inorout.py
index 0eea62ae..d5e8b226 100644
--- a/coverage/inorout.py
+++ b/coverage/inorout.py
@@ -333,6 +333,12 @@ class InOrOut(object):
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
diff --git a/tests/test_oddball.py b/tests/test_oddball.py
index 3574806c..90b92249 100644
--- a/tests/test_oddball.py
+++ b/tests/test_oddball.py
@@ -3,6 +3,7 @@
"""Oddball cases for testing coverage.py"""
+import os.path
import sys
from flaky import flaky
@@ -562,6 +563,18 @@ class ExecTest(CoverageTest):
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.