summaryrefslogtreecommitdiff
path: root/coverage
diff options
context:
space:
mode:
Diffstat (limited to 'coverage')
-rw-r--r--coverage/backward.py3
-rw-r--r--coverage/codeunit.py18
-rw-r--r--coverage/files.py5
3 files changed, 20 insertions, 6 deletions
diff --git a/coverage/backward.py b/coverage/backward.py
index d21e6a8..5e6b76f 100644
--- a/coverage/backward.py
+++ b/coverage/backward.py
@@ -29,8 +29,9 @@ except NameError:
try:
from cStringIO import StringIO
+ BytesIO = StringIO
except ImportError:
- from io import StringIO
+ from io import StringIO, BytesIO
# What's a string called?
diff --git a/coverage/codeunit.py b/coverage/codeunit.py
index 53c98fc..3b0407a 100644
--- a/coverage/codeunit.py
+++ b/coverage/codeunit.py
@@ -2,7 +2,8 @@
import glob, os
-from coverage.backward import string_class
+from coverage.backward import string_class, BytesIO
+from coverage.misc import CoverageException
def code_unit_factory(morfs, file_locator, omit_prefixes=None):
@@ -59,6 +60,8 @@ class CodeUnit:
"""
def __init__(self, morf, file_locator):
+ self.file_locator = file_locator
+
if hasattr(morf, '__file__'):
f = morf.__file__
else:
@@ -66,14 +69,14 @@ class CodeUnit:
# .pyc files should always refer to a .py instead.
if f.endswith('.pyc'):
f = f[:-1]
- self.filename = file_locator.canonical_filename(f)
+ self.filename = self.file_locator.canonical_filename(f)
if hasattr(morf, '__name__'):
n = modname = morf.__name__
self.relative = True
else:
n = os.path.splitext(morf)[0]
- rel = file_locator.relative_filename(n)
+ rel = self.file_locator.relative_filename(n)
if os.path.isabs(n):
self.relative = (rel != n)
else:
@@ -83,6 +86,7 @@ class CodeUnit:
self.name = n
self.modname = modname
+
def __repr__(self):
return "<CodeUnit name=%r filename=%r>" % (self.name, self.filename)
@@ -125,4 +129,12 @@ class CodeUnit:
def source_file(self):
"""Return an open file for reading the source of the code unit."""
+ if not os.path.exists(self.filename):
+ source = self.file_locator.get_zip_data(self.filename)
+ if source is None:
+ raise CoverageException(
+ "No source for code %r." % self.filename
+ )
+ return BytesIO(source)
+
return open(self.filename)
diff --git a/coverage/files.py b/coverage/files.py
index c16f113..cce33c9 100644
--- a/coverage/files.py
+++ b/coverage/files.py
@@ -34,7 +34,7 @@ class FileLocator:
if filename not in self.canonical_filename_cache:
f = filename
if os.path.isabs(f) and not os.path.exists(f):
- if not self.get_zip_data(f):
+ if self.get_zip_data(f) is None:
f = os.path.basename(f)
if not os.path.isabs(f):
for path in [os.curdir] + sys.path:
@@ -50,7 +50,8 @@ class FileLocator:
"""Get data from `filename` if it is a zip file path.
Returns the data read from the zip file, or None if no zip file could
- be found or `filename` isn't in it.
+ be found or `filename` isn't in it. The data returned might be "" if
+ the file is empty.
"""
import zipimport