From c7f90f9e930835a955df59bffe96e996dfcf11a0 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Tue, 29 Sep 2009 22:21:48 -0400 Subject: Working toward reading source from eggs, but this isn't right on Py3k yet. --- coverage/codeunit.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'coverage/codeunit.py') 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 "" % (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) -- cgit v1.2.1