diff options
Diffstat (limited to 'coverage')
-rw-r--r-- | coverage/annotate.py | 79 | ||||
-rw-r--r-- | coverage/backward.py | 65 | ||||
-rw-r--r-- | coverage/cmdline.py | 25 | ||||
-rw-r--r-- | coverage/codeunit.py | 167 | ||||
-rw-r--r-- | coverage/collector.py | 95 | ||||
-rw-r--r-- | coverage/config.py | 57 | ||||
-rw-r--r-- | coverage/control.py | 88 | ||||
-rw-r--r-- | coverage/data.py | 19 | ||||
-rw-r--r-- | coverage/django.py | 61 | ||||
-rw-r--r-- | coverage/execfile.py | 124 | ||||
-rw-r--r-- | coverage/extension.py | 20 | ||||
-rw-r--r-- | coverage/files.py | 8 | ||||
-rw-r--r-- | coverage/html.py | 8 | ||||
-rw-r--r-- | coverage/misc.py | 2 | ||||
-rw-r--r-- | coverage/parser.py | 40 | ||||
-rw-r--r-- | coverage/phystokens.py | 2 | ||||
-rw-r--r-- | coverage/report.py | 20 | ||||
-rw-r--r-- | coverage/results.py | 27 | ||||
-rw-r--r-- | coverage/summary.py | 9 | ||||
-rw-r--r-- | coverage/templite.py | 45 | ||||
-rw-r--r-- | coverage/tracer.c | 24 |
21 files changed, 674 insertions, 311 deletions
diff --git a/coverage/annotate.py b/coverage/annotate.py index 19777eaf..5b96448a 100644 --- a/coverage/annotate.py +++ b/coverage/annotate.py @@ -47,55 +47,44 @@ class AnnotateReporter(Reporter): `cu` is the CodeUnit for the file to annotate. """ - if not cu.relative: - return + statements = sorted(analysis.statements) + missing = sorted(analysis.missing) + excluded = sorted(analysis.excluded) - filename = cu.filename - source = cu.source_file() if self.directory: dest_file = os.path.join(self.directory, cu.flat_rootname()) dest_file += ".py,cover" else: - dest_file = filename + ",cover" - dest = open(dest_file, 'w') - - statements = sorted(analysis.statements) - missing = sorted(analysis.missing) - excluded = sorted(analysis.excluded) - - lineno = 0 - i = 0 - j = 0 - covered = True - while True: - line = source.readline() - if line == '': - break - lineno += 1 - while i < len(statements) and statements[i] < lineno: - i += 1 - while j < len(missing) and missing[j] < lineno: - j += 1 - if i < len(statements) and statements[i] == lineno: - covered = j >= len(missing) or missing[j] > lineno - if self.blank_re.match(line): - dest.write(' ') - elif self.else_re.match(line): - # Special logic for lines containing only 'else:'. - if i >= len(statements) and j >= len(missing): - dest.write('! ') - elif i >= len(statements) or j >= len(missing): + dest_file = cu.filename + ",cover" + + with open(dest_file, 'w') as dest: + i = 0 + j = 0 + covered = True + source = cu.source() + for lineno, line in enumerate(source.splitlines(True), start=1): + while i < len(statements) and statements[i] < lineno: + i += 1 + while j < len(missing) and missing[j] < lineno: + j += 1 + if i < len(statements) and statements[i] == lineno: + covered = j >= len(missing) or missing[j] > lineno + if self.blank_re.match(line): + dest.write(' ') + elif self.else_re.match(line): + # Special logic for lines containing only 'else:'. + if i >= len(statements) and j >= len(missing): + dest.write('! ') + elif i >= len(statements) or j >= len(missing): + dest.write('> ') + elif statements[i] == missing[j]: + dest.write('! ') + else: + dest.write('> ') + elif lineno in excluded: + dest.write('- ') + elif covered: dest.write('> ') - elif statements[i] == missing[j]: - dest.write('! ') else: - dest.write('> ') - elif lineno in excluded: - dest.write('- ') - elif covered: - dest.write('> ') - else: - dest.write('! ') - dest.write(line) - source.close() - dest.close() + dest.write('! ') + dest.write(line) diff --git a/coverage/backward.py b/coverage/backward.py index e81dd199..a7888a24 100644 --- a/coverage/backward.py +++ b/coverage/backward.py @@ -1,10 +1,11 @@ """Add things to old Pythons so I can pretend they are newer.""" # This file does lots of tricky stuff, so disable a bunch of lintisms. -# pylint: disable=F0401,W0611,W0622 -# F0401: Unable to import blah -# W0611: Unused import blah -# W0622: Redefining built-in blah +# pylint: disable=redefined-builtin +# pylint: disable=import-error +# pylint: disable=no-member +# pylint: disable=unused-import +# pylint: disable=no-name-in-module import os, re, sys @@ -124,3 +125,59 @@ try: except ImportError: import md5 md5 = md5.new + + +try: + # In Py 2.x, the builtins were in __builtin__ + BUILTINS = sys.modules['__builtin__'] +except KeyError: + # In Py 3.x, they're in builtins + BUILTINS = sys.modules['builtins'] + + +# imp was deprecated in Python 3.3 +try: + import importlib, importlib.util + imp = None +except ImportError: + importlib = None + +# we only want to use importlib if it has everything we need. +try: + importlib_util_find_spec = importlib.util.find_spec +except Exception: + import imp + importlib_util_find_spec = None + +try: + PYC_MAGIC_NUMBER = importlib.util.MAGIC_NUMBER +except AttributeError: + PYC_MAGIC_NUMBER = imp.get_magic() + + +def import_local_file(modname): + """Import a local file as a module. + + Opens a file in the current directory named `modname`.py, imports it + as `modname`, and returns the module object. + + """ + try: + from importlib.machinery import SourceFileLoader + except ImportError: + SourceFileLoader = None + + modfile = modname + '.py' + if SourceFileLoader: + mod = SourceFileLoader(modname, modfile).load_module() + else: + for suff in imp.get_suffixes(): + if suff[0] == '.py': + break + + with open(modfile, 'r') as f: + # pylint: disable=W0631 + # (Using possibly undefined loop variable 'suff') + mod = imp.load_module(modname, f, modfile, suff) + + return mod diff --git a/coverage/cmdline.py b/coverage/cmdline.py index 19e0536e..bd10d5a8 100644 --- a/coverage/cmdline.py +++ b/coverage/cmdline.py @@ -1,6 +1,6 @@ """Command-line support for Coverage.""" -import optparse, os, sys, time, traceback +import glob, optparse, os, sys, time, traceback from coverage.execfile import run_python_file, run_python_module from coverage.misc import CoverageException, ExceptionDuringRun, NoSource @@ -449,7 +449,7 @@ class CoverageScript(object): # Remaining actions are reporting, with some common options. report_args = dict( - morfs = args, + morfs = unglob_args(args), ignore_errors = options.ignore_errors, omit = omit, include = include, @@ -470,6 +470,14 @@ class CoverageScript(object): total = self.coverage.xml_report(outfile=outfile, **report_args) if options.fail_under is not None: + # Total needs to be rounded, but be careful of 0 and 100. + if 0 < total < 1: + total = 1 + elif 99 < total < 100: + total = 99 + else: + total = round(total) + if total >= options.fail_under: return OK else: @@ -633,6 +641,19 @@ def unshell_list(s): return s.split(',') +def unglob_args(args): + """Interpret shell wildcards for platforms that need it.""" + if sys.platform == 'win32': + globbed = [] + for arg in args: + if '?' in arg or '*' in arg: + globbed.extend(glob.glob(arg)) + else: + globbed.append(arg) + args = globbed + return args + + HELP_TOPICS = { # ------------------------- 'classic': diff --git a/coverage/codeunit.py b/coverage/codeunit.py index 88858801..35167a72 100644 --- a/coverage/codeunit.py +++ b/coverage/codeunit.py @@ -1,20 +1,24 @@ """Code unit (module) handling for Coverage.""" -import glob, os, re +import os -from coverage.backward import open_python_source, string_class, StringIO +from coverage.backward import open_python_source, string_class from coverage.misc import CoverageException, NoSource from coverage.parser import CodeParser, PythonParser from coverage.phystokens import source_token_lines, source_encoding +from coverage.django import DjangoTracer -def code_unit_factory(morfs, file_locator): + +def code_unit_factory(morfs, file_locator, get_ext=None): """Construct a list of CodeUnits from polymorphic inputs. `morfs` is a module or a filename, or a list of same. `file_locator` is a FileLocator that can help resolve filenames. + `get_ext` TODO + Returns a list of CodeUnit objects. """ @@ -22,25 +26,28 @@ def code_unit_factory(morfs, file_locator): if not isinstance(morfs, (list, tuple)): morfs = [morfs] - # On Windows, the shell doesn't expand wildcards. Do it here. - globbed = [] - for morf in morfs: - if isinstance(morf, string_class) and ('?' in morf or '*' in morf): - globbed.extend(glob.glob(morf)) - else: - globbed.append(morf) - morfs = globbed + django_tracer = DjangoTracer() code_units = [] for morf in morfs: - # Hacked-in Mako support. Disabled for going onto trunk. - if 0 and isinstance(morf, string_class) and "/mako/" in morf: - # Super hack! Do mako both ways! - if 0: - cu = PythonCodeUnit(morf, file_locator) - cu.name += '_fako' - code_units.append(cu) - klass = MakoCodeUnit + ext = None + if isinstance(morf, string_class) and get_ext: + ext = get_ext(morf) + if ext: + klass = DjangoTracer # NOT REALLY! TODO + # Hacked-in Mako support. Define COVERAGE_MAKO_PATH as a fragment of + # the path that indicates the Python file is actually a compiled Mako + # template. THIS IS TEMPORARY! + #MAKO_PATH = os.environ.get('COVERAGE_MAKO_PATH') + #if MAKO_PATH and isinstance(morf, string_class) and MAKO_PATH in morf: + # # Super hack! Do mako both ways! + # if 0: + # cu = PythonCodeUnit(morf, file_locator) + # cu.name += '_fako' + # code_units.append(cu) + # klass = MakoCodeUnit + #elif isinstance(morf, string_class) and morf.endswith(".html"): + # klass = DjangoCodeUnit else: klass = PythonCodeUnit code_units.append(klass(morf, file_locator)) @@ -87,6 +94,10 @@ class CodeUnit(object): def __repr__(self): return "<CodeUnit name=%r filename=%r>" % (self.name, self.filename) + def _adjust_filename(self, f): + # TODO: This shouldn't be in the base class, right? + return f + # Annoying comparison operators. Py3k wants __lt__ etc, and Py2k needs all # of them defined. @@ -119,22 +130,29 @@ class CodeUnit(object): root = os.path.splitdrive(self.name)[1] return root.replace('\\', '_').replace('/', '_').replace('.', '_') - def source_file(self): - """Return an open file for reading the source of the code unit.""" + def source(self): + """Return the source code, as a string.""" if os.path.exists(self.filename): # A regular text file: open it. - return open_python_source(self.filename) + with open_python_source(self.filename) as f: + return f.read() # Maybe it's in a zip file? source = self.file_locator.get_zip_data(self.filename) if source is not None: - return StringIO(source) + return source # Couldn't find source. raise CoverageException( "No source for code '%s'." % self.filename ) + def source_token_lines(self, source): + """Return the 'tokenized' text for the code.""" + # TODO: Taking source here is wrong, change it? + for line in source.splitlines(): + yield [('txt', line)] + def should_be_python(self): """Does it seem like this file should contain Python? @@ -148,8 +166,6 @@ class CodeUnit(object): class PythonCodeUnit(CodeUnit): """Represents a Python file.""" - parser_class = PythonParser - def _adjust_filename(self, fname): # .pyc files should always refer to a .py instead. if fname.endswith(('.pyc', '.pyo')): @@ -158,7 +174,13 @@ class PythonCodeUnit(CodeUnit): fname = fname[:-9] + ".py" return fname - def find_source(self, filename): + def get_parser(self, exclude=None): + actual_filename, source = self._find_source(self.filename) + return PythonParser( + text=source, filename=actual_filename, exclude=exclude, + ) + + def _find_source(self, filename): """Find the source for `filename`. Returns two values: the actual filename, and the source. @@ -223,78 +245,61 @@ class PythonCodeUnit(CodeUnit): return source_encoding(source) -def mako_template_name(py_filename): - with open(py_filename) as f: - py_source = f.read() - - # Find the template filename. TODO: string escapes in the string. - m = re.search(r"^_template_filename = u?'([^']+)'", py_source, flags=re.MULTILINE) - if not m: - raise Exception("Couldn't find template filename in Mako file %r" % py_filename) - template_filename = m.group(1) - return template_filename - - class MakoParser(CodeParser): - def __init__(self, cu, text, filename, exclude): - self.cu = cu - self.text = text - self.filename = filename - self.exclude = exclude + def __init__(self, metadata): + self.metadata = metadata def parse_source(self): """Returns executable_line_numbers, excluded_line_numbers""" - with open(self.cu.filename) as f: - py_source = f.read() - - # Get the line numbers. - self.py_to_html = {} - html_linenum = None - for linenum, line in enumerate(py_source.splitlines(), start=1): - m_source_line = re.search(r"^\s*# SOURCE LINE (\d+)$", line) - if m_source_line: - html_linenum = int(m_source_line.group(1)) - else: - m_boilerplate_line = re.search(r"^\s*# BOILERPLATE", line) - if m_boilerplate_line: - html_linenum = None - elif html_linenum: - self.py_to_html[linenum] = html_linenum - - return set(self.py_to_html.values()), set() + executable = set(self.metadata['line_map'].values()) + return executable, set() def translate_lines(self, lines): - tlines = set(self.py_to_html.get(l, -1) for l in lines) - tlines.remove(-1) + tlines = set() + for l in lines: + try: + tlines.add(self.metadata['full_line_map'][l]) + except IndexError: + pass return tlines class MakoCodeUnit(CodeUnit): - parser_class = MakoParser - def __init__(self, *args, **kwargs): super(MakoCodeUnit, self).__init__(*args, **kwargs) - self.mako_filename = mako_template_name(self.filename) + from mako.template import ModuleInfo + py_source = open(self.filename).read() + self.metadata = ModuleInfo.get_module_source_metadata(py_source, full_line_map=True) - def source_file(self): - return open(self.mako_filename) + def source(self): + return open(self.metadata['filename']).read() - def find_source(self, filename): - """Find the source for `filename`. + def get_parser(self, exclude=None): + return MakoParser(self.metadata) - Returns two values: the actual filename, and the source. + def source_encoding(self, source): + # TODO: Taking source here is wrong, change it! + return self.metadata['source_encoding'] - """ - mako_filename = mako_template_name(filename) - with open(mako_filename) as f: - source = f.read() - return mako_filename, source +class DjangoCodeUnit(CodeUnit): + def source(self): + with open(self.filename) as f: + return f.read() - def source_token_lines(self, source): - """Return the 'tokenized' text for the code.""" - for line in source.splitlines(): - yield [('txt', line)] + def get_parser(self, exclude=None): + return DjangoParser(self.filename) def source_encoding(self, source): - return "utf-8" + return "utf8" + + +class DjangoParser(CodeParser): + def __init__(self, filename): + self.filename = filename + + def parse_source(self): + with open(self.filename) as f: + source = f.read() + executable = set(range(1, len(source.splitlines())+1)) + return executable, set() diff --git a/coverage/collector.py b/coverage/collector.py index 94af5df5..546525d2 100644 --- a/coverage/collector.py +++ b/coverage/collector.py @@ -41,17 +41,22 @@ class PyTracer(object): # used to force the use of this tracer. def __init__(self): + # Attributes set from the collector: self.data = None + self.arcs = False self.should_trace = None self.should_trace_cache = None self.warn = None + self.extensions = None + + self.extension = None + self.cur_tracename = None # TODO: This is only maintained for the if0 debugging output. Get rid of it eventually. self.cur_file_data = None self.last_line = 0 self.data_stack = [] self.data_stacks = collections.defaultdict(list) self.last_exc_back = None self.last_exc_firstlineno = 0 - self.arcs = False self.thread = None self.stopped = False self.coroutine_id_func = None @@ -64,9 +69,31 @@ class PyTracer(object): return if 0: - sys.stderr.write("trace event: %s %r @%d\n" % ( - event, frame.f_code.co_filename, frame.f_lineno + # A lot of debugging to try to understand why gevent isn't right. + import os.path, pprint + def short_ident(ident): + return "{}:{:06X}".format(ident.__class__.__name__, id(ident) & 0xFFFFFF) + + ident = None + if self.coroutine_id_func: + ident = short_ident(self.coroutine_id_func()) + sys.stdout.write("trace event: %s %s %r @%d\n" % ( + event, ident, frame.f_code.co_filename, frame.f_lineno )) + pprint.pprint( + dict( + ( + short_ident(ident), + [ + (os.path.basename(tn or ""), sorted((cfd or {}).keys()), ll) + for ex, tn, cfd, ll in data_stacks + ] + ) + for ident, data_stacks in self.data_stacks.items() + ) + , width=250) + pprint.pprint(sorted((self.cur_file_data or {}).keys()), width=250) + print("TRYING: {}".format(sorted(next((v for k,v in self.data.items() if k.endswith("try_it.py")), {}).keys()))) if self.last_exc_back: if frame == self.last_exc_back: @@ -76,7 +103,7 @@ class PyTracer(object): self.cur_file_data[pair] = None if self.coroutine_id_func: self.data_stack = self.data_stacks[self.coroutine_id_func()] - self.cur_file_data, self.last_line = self.data_stack.pop() + self.handler, _, self.cur_file_data, self.last_line = self.data_stack.pop() self.last_exc_back = None if event == 'call': @@ -85,19 +112,25 @@ class PyTracer(object): if self.coroutine_id_func: self.data_stack = self.data_stacks[self.coroutine_id_func()] self.last_coroutine = self.coroutine_id_func() - self.data_stack.append((self.cur_file_data, self.last_line)) + self.data_stack.append((self.extension, self.cur_tracename, self.cur_file_data, self.last_line)) filename = frame.f_code.co_filename - if filename not in self.should_trace_cache: - tracename = self.should_trace(filename, frame) - self.should_trace_cache[filename] = tracename - else: - tracename = self.should_trace_cache[filename] + disp = self.should_trace_cache.get(filename) + if disp is None: + disp = self.should_trace(filename, frame) + self.should_trace_cache[filename] = disp #print("called, stack is %d deep, tracename is %r" % ( # len(self.data_stack), tracename)) + tracename = disp.filename + if tracename and disp.extension: + tracename = disp.extension.file_name(frame) if tracename: if tracename not in self.data: self.data[tracename] = {} + if disp.extension: + self.extensions[tracename] = disp.extension.__name__ + self.cur_tracename = tracename self.cur_file_data = self.data[tracename] + self.extension = disp.extension else: self.cur_file_data = None # Set the last_line to -1 because the next arc will be entering a @@ -105,16 +138,24 @@ class PyTracer(object): self.last_line = -1 elif event == 'line': # Record an executed line. - #if self.coroutine_id_func: - # assert self.last_coroutine == self.coroutine_id_func() - if self.cur_file_data is not None: - if self.arcs: - #print("lin", self.last_line, frame.f_lineno) - self.cur_file_data[(self.last_line, frame.f_lineno)] = None - else: - #print("lin", frame.f_lineno) - self.cur_file_data[frame.f_lineno] = None - self.last_line = frame.f_lineno + if 0 and self.coroutine_id_func: + this_coroutine = self.coroutine_id_func() + if self.last_coroutine != this_coroutine: + print("mismatch: {0} != {1}".format(self.last_coroutine, this_coroutine)) + if self.extension: + lineno_from, lineno_to = self.extension.line_number_range(frame) + else: + lineno_from, lineno_to = frame.f_lineno, frame.f_lineno + if lineno_from != -1: + if self.cur_file_data is not None: + if self.arcs: + #print("lin", self.last_line, frame.f_lineno) + self.cur_file_data[(self.last_line, lineno_from)] = None + else: + #print("lin", frame.f_lineno) + for lineno in range(lineno_from, lineno_to+1): + self.cur_file_data[lineno] = None + self.last_line = lineno_to elif event == 'return': if self.arcs and self.cur_file_data: first = frame.f_code.co_firstlineno @@ -123,7 +164,7 @@ class PyTracer(object): if self.coroutine_id_func: self.data_stack = self.data_stacks[self.coroutine_id_func()] self.last_coroutine = self.coroutine_id_func() - self.cur_file_data, self.last_line = self.data_stack.pop() + self.extension, _, self.cur_file_data, self.last_line = self.data_stack.pop() #print("returned, stack is %d deep" % (len(self.data_stack))) elif event == 'exception': #print("exc", self.last_line, frame.f_lineno) @@ -240,6 +281,8 @@ class Collector(object): # or mapping filenames to dicts with linenumber pairs as keys. self.data = {} + self.extensions = {} + # A cache of the results from should_trace, the decision about whether # to trace execution in a file. A dict of filename to (filename or # None). @@ -258,6 +301,8 @@ class Collector(object): tracer.warn = self.warn if hasattr(tracer, 'coroutine_id_func'): tracer.coroutine_id_func = self.coroutine_id_func + if hasattr(tracer, 'extensions'): + tracer.extensions = self.extensions fn = tracer.start() self.tracers.append(tracer) return fn @@ -356,10 +401,7 @@ class Collector(object): # to show line data. line_data = {} for f, arcs in self.data.items(): - line_data[f] = ldf = {} - for l1, _ in list(arcs.keys()): - if l1: - ldf[l1] = None + line_data[f] = dict((l1, None) for l1, _ in arcs.keys() if l1) return line_data else: return self.data @@ -377,3 +419,6 @@ class Collector(object): return self.data else: return {} + + def get_extension_data(self): + return self.extensions diff --git a/coverage/config.py b/coverage/config.py index 60ec3f41..064bc1ca 100644 --- a/coverage/config.py +++ b/coverage/config.py @@ -13,6 +13,11 @@ except ImportError: class HandyConfigParser(configparser.RawConfigParser): """Our specialization of ConfigParser.""" + def __init__(self, section_prefix): + # pylint: disable=super-init-not-called + configparser.RawConfigParser.__init__(self) + self.section_prefix = section_prefix + def read(self, filename): """Read a filename as UTF-8 configuration data.""" kwargs = {} @@ -20,8 +25,30 @@ class HandyConfigParser(configparser.RawConfigParser): kwargs['encoding'] = "utf-8" return configparser.RawConfigParser.read(self, filename, **kwargs) - def get(self, *args, **kwargs): - v = configparser.RawConfigParser.get(self, *args, **kwargs) + def has_option(self, section, option): + section = self.section_prefix + section + return configparser.RawConfigParser.has_option(self, section, option) + + def has_section(self, section): + section = self.section_prefix + section + return configparser.RawConfigParser.has_section(self, section) + + def options(self, section): + section = self.section_prefix + section + return configparser.RawConfigParser.options(self, section) + + def get(self, section, *args, **kwargs): + """Get a value, replacing environment variables also. + + The arguments are the same as `RawConfigParser.get`, but in the found + value, ``$WORD`` or ``${WORD}`` are replaced by the value of the + environment variable ``WORD``. + + Returns the finished value. + + """ + section = self.section_prefix + section + v = configparser.RawConfigParser.get(self, section, *args, **kwargs) def dollar_replace(m): """Called for each $replacement.""" # Only one of the groups will have matched, just get its text. @@ -113,6 +140,7 @@ class CoverageConfig(object): self.timid = False self.source = None self.debug = [] + self.extensions = [] # Defaults for [report] self.exclude_list = DEFAULT_EXCLUDE[:] @@ -144,7 +172,7 @@ class CoverageConfig(object): if env: self.timid = ('--timid' in env) - MUST_BE_LIST = ["omit", "include", "debug"] + MUST_BE_LIST = ["omit", "include", "debug", "extensions"] def from_args(self, **kwargs): """Read config values from `kwargs`.""" @@ -154,18 +182,22 @@ class CoverageConfig(object): v = [v] setattr(self, k, v) - def from_file(self, filename): + def from_file(self, filename, section_prefix=""): """Read configuration from a .rc file. `filename` is a file name to read. + Returns True or False, whether the file could be read. + """ self.attempted_config_files.append(filename) - cp = HandyConfigParser() + cp = HandyConfigParser(section_prefix) files_read = cp.read(filename) - if files_read is not None: # return value changed in 2.4 - self.config_files.extend(files_read) + if not files_read: + return False + + self.config_files.extend(files_read) for option_spec in self.CONFIG_FILE_OPTIONS: self.set_attr_from_config_option(cp, *option_spec) @@ -175,13 +207,24 @@ class CoverageConfig(object): for option in cp.options('paths'): self.paths[option] = cp.getlist('paths', option) + return True + CONFIG_FILE_OPTIONS = [ + # These are *args for set_attr_from_config_option: + # (attr, where, type_="") + # + # attr is the attribute to set on the CoverageConfig object. + # where is the section:name to read from the configuration file. + # type_ is the optional type to apply, by using .getTYPE to read the + # configuration value from the file. + # [run] ('branch', 'run:branch', 'boolean'), ('coroutine', 'run:coroutine'), ('cover_pylib', 'run:cover_pylib', 'boolean'), ('data_file', 'run:data_file'), ('debug', 'run:debug', 'list'), + ('extensions', 'run:extensions', 'list'), ('include', 'run:include', 'list'), ('omit', 'run:omit', 'list'), ('parallel', 'run:parallel', 'boolean'), diff --git a/coverage/control.py b/coverage/control.py index 44a70bf0..cb917e52 100644 --- a/coverage/control.py +++ b/coverage/control.py @@ -9,6 +9,7 @@ from coverage.collector import Collector from coverage.config import CoverageConfig from coverage.data import CoverageData from coverage.debug import DebugControl +from coverage.extension import load_extensions from coverage.files import FileLocator, TreeMatcher, FnmatchMatcher from coverage.files import PathAliases, find_python_files, prep_patterns from coverage.html import HtmlReporter @@ -18,6 +19,7 @@ from coverage.results import Analysis, Numbers from coverage.summary import SummaryReporter from coverage.xmlreport import XmlReporter + # Pypy has some unusual stuff in the "stdlib". Consider those locations # when deciding where the stdlib is. try: @@ -97,17 +99,22 @@ class coverage(object): # 1: defaults: self.config = CoverageConfig() - # 2: from the coveragerc file: + # 2: from the .coveragerc or setup.cfg file: if config_file: + did_read_rc = should_read_setupcfg = False if config_file is True: config_file = ".coveragerc" + should_read_setupcfg = True try: - self.config.from_file(config_file) + did_read_rc = self.config.from_file(config_file) except ValueError as err: raise CoverageException( "Couldn't read config file %s: %s" % (config_file, err) ) + if not did_read_rc and should_read_setupcfg: + self.config.from_file("setup.cfg", section_prefix="coverage:") + # 3: from environment variables: self.config.from_environment('COVERAGE_OPTIONS') env_data_file = os.environ.get('COVERAGE_FILE') @@ -125,6 +132,10 @@ class coverage(object): # Create and configure the debugging controller. self.debug = DebugControl(self.config.debug, debug_file or sys.stderr) + # Load extensions + tracer_classes = load_extensions(self.config.extensions, "tracer") + self.tracer_extensions = [cls() for cls in tracer_classes] + self.auto_data = auto_data # _exclude_re is a dict mapping exclusion list names to compiled @@ -232,22 +243,24 @@ class coverage(object): This function is called from the trace function. As each new file name is encountered, this function determines whether it is traced or not. - Returns a pair of values: the first indicates whether the file should - be traced: it's a canonicalized filename if it should be traced, None - if it should not. The second value is a string, the resason for the - decision. + Returns a FileDisposition object. """ + disp = FileDisposition(filename) + if not filename: # Empty string is pretty useless - return None, "empty string isn't a filename" + return disp.nope("empty string isn't a filename") + + if filename.startswith('memory:'): + return disp.nope("memory isn't traceable") if filename.startswith('<'): # Lots of non-file execution is represented with artificial # filenames like "<string>", "<doctest readme.txt[0]>", or # "<exec_function>". Don't ever trace these executions, since we # can't do anything with the data later anyway. - return None, "not a real filename" + return disp.nope("not a real filename") self._check_for_packages() @@ -267,47 +280,51 @@ class coverage(object): canonical = self.file_locator.canonical_filename(filename) + # Try the extensions, see if they have an opinion about the file. + for tracer in self.tracer_extensions: + ext_disp = tracer.should_trace(canonical) + if ext_disp: + ext_disp.extension = tracer + return ext_disp + # If the user specified source or include, then that's authoritative # about the outer bound of what to measure and we don't have to apply # any canned exclusions. If they didn't, then we have to exclude the # stdlib and coverage.py directories. if self.source_match: if not self.source_match.match(canonical): - return None, "falls outside the --source trees" + return disp.nope("falls outside the --source trees") elif self.include_match: if not self.include_match.match(canonical): - return None, "falls outside the --include trees" + return disp.nope("falls outside the --include trees") else: # If we aren't supposed to trace installed code, then check if this # is near the Python standard library and skip it if so. if self.pylib_match and self.pylib_match.match(canonical): - return None, "is in the stdlib" + return disp.nope("is in the stdlib") # We exclude the coverage code itself, since a little of it will be # measured otherwise. if self.cover_match and self.cover_match.match(canonical): - return None, "is part of coverage.py" + return disp.nope("is part of coverage.py") # Check the file against the omit pattern. if self.omit_match and self.omit_match.match(canonical): - return None, "is inside an --omit pattern" + return disp.nope("is inside an --omit pattern") - return canonical, "because we love you" + disp.filename = canonical + return disp def _should_trace(self, filename, frame): """Decide whether to trace execution in `filename`. - Calls `_should_trace_with_reason`, and returns just the decision. + Calls `_should_trace_with_reason`, and returns the FileDisposition. """ - canonical, reason = self._should_trace_with_reason(filename, frame) + disp = self._should_trace_with_reason(filename, frame) if self.debug.should('trace'): - if not canonical: - msg = "Not tracing %r: %s" % (filename, reason) - else: - msg = "Tracing %r" % (filename,) - self.debug.write(msg) - return canonical + self.debug.write(disp.debug_message()) + return disp def _warn(self, msg): """Use `msg` as a warning.""" @@ -525,8 +542,10 @@ class coverage(object): if not self._measured: return + # TODO: seems like this parallel structure is getting kinda old... self.data.add_line_data(self.collector.get_line_data()) self.data.add_arc_data(self.collector.get_arc_data()) + self.data.add_extension_data(self.collector.get_extension_data()) self.collector.reset() # If there are still entries in the source_pkgs list, then we never @@ -594,7 +613,8 @@ class coverage(object): """ self._harvest_data() if not isinstance(it, CodeUnit): - it = code_unit_factory(it, self.file_locator)[0] + get_ext = self.data.extension_data().get + it = code_unit_factory(it, self.file_locator, get_ext)[0] return Analysis(self, it) @@ -760,6 +780,28 @@ class coverage(object): return info +class FileDisposition(object): + """A simple object for noting a number of details of files to trace.""" + def __init__(self, original_filename): + self.original_filename = original_filename + self.filename = None + self.reason = "" + self.extension = None + + def nope(self, reason): + """A helper for returning a NO answer from should_trace.""" + self.reason = reason + return self + + def debug_message(self): + """Produce a debugging message explaining the outcome.""" + if not self.filename: + msg = "Not tracing %r: %s" % (self.original_filename, self.reason) + else: + msg = "Tracing %r" % (self.original_filename,) + return msg + + def process_startup(): """Call this at Python startup to perhaps measure coverage. diff --git a/coverage/data.py b/coverage/data.py index 042b6405..b78c931d 100644 --- a/coverage/data.py +++ b/coverage/data.py @@ -21,6 +21,11 @@ class CoverageData(object): * arcs: a dict mapping filenames to sorted lists of line number pairs: { 'file1': [(17,23), (17,25), (25,26)], ... } + * extensions: a dict mapping filenames to extension names: + { 'file1': "django.coverage", ... } + # TODO: how to handle the difference between a extension module + # name, and the class in the module? + """ def __init__(self, basename=None, collector=None, debug=None): @@ -64,6 +69,14 @@ class CoverageData(object): # self.arcs = {} + # A map from canonical source file name to an extension module name: + # + # { + # 'filename1.py': 'django.coverage', + # ... + # } + self.extensions = {} + def usefile(self, use_file=True): """Set whether or not to use a disk file for data.""" self.use_file = use_file @@ -110,6 +123,9 @@ class CoverageData(object): (f, sorted(amap.keys())) for f, amap in iitems(self.arcs) ) + def extension_data(self): + return self.extensions + def write_file(self, filename): """Write the coverage data to `filename`.""" @@ -213,6 +229,9 @@ class CoverageData(object): for filename, arcs in iitems(arc_data): self.arcs.setdefault(filename, {}).update(arcs) + def add_extension_data(self, extension_data): + self.extensions.update(extension_data) + def touch_file(self, filename): """Ensure that `filename` appears in the data, empty if needed.""" self.lines.setdefault(filename, {}) diff --git a/coverage/django.py b/coverage/django.py new file mode 100644 index 00000000..00f2ed54 --- /dev/null +++ b/coverage/django.py @@ -0,0 +1,61 @@ +import sys + + +ALL_TEMPLATE_MAP = {} + +def get_line_map(filename): + if filename not in ALL_TEMPLATE_MAP: + with open(filename) as template_file: + template_source = template_file.read() + line_lengths = [len(l) for l in template_source.splitlines(True)] + ALL_TEMPLATE_MAP[filename] = list(running_sum(line_lengths)) + return ALL_TEMPLATE_MAP[filename] + +def get_line_number(line_map, offset): + for lineno, line_offset in enumerate(line_map, start=1): + if line_offset >= offset: + return lineno + return -1 + +class DjangoTracer(object): + def should_trace(self, canonical): + return "/django/template/" in canonical + + def source(self, frame): + if frame.f_code.co_name != 'render': + return None + that = frame.f_locals['self'] + return getattr(that, "source", None) + + def file_name(self, frame): + source = self.source(frame) + if not source: + return None + return source[0].name.encode(sys.getfilesystemencoding()) + + def line_number_range(self, frame): + source = self.source(frame) + if not source: + return -1, -1 + filename = source[0].name + line_map = get_line_map(filename) + start = get_line_number(line_map, source[1][0]) + end = get_line_number(line_map, source[1][1]) + if start < 0 or end < 0: + return -1, -1 + return start, end + +def running_sum(seq): + total = 0 + for num in seq: + total += num + yield total + +def ppp(obj): + ret = [] + import inspect + for name, value in inspect.getmembers(obj): + if not callable(value): + ret.append("%s=%r" % (name, value)) + attrs = ", ".join(ret) + return "%s: %s" % (obj.__class__, attrs) diff --git a/coverage/execfile.py b/coverage/execfile.py index 10c7b917..b7877b6a 100644 --- a/coverage/execfile.py +++ b/coverage/execfile.py @@ -1,23 +1,83 @@ """Execute files of Python code.""" -import imp, marshal, os, sys +import marshal, os, sys, types -from coverage.backward import open_python_source +from coverage.backward import open_python_source, BUILTINS +from coverage.backward import PYC_MAGIC_NUMBER, imp, importlib_util_find_spec from coverage.misc import ExceptionDuringRun, NoCode, NoSource -try: - # In Py 2.x, the builtins were in __builtin__ - BUILTINS = sys.modules['__builtin__'] -except KeyError: - # In Py 3.x, they're in builtins - BUILTINS = sys.modules['builtins'] +if importlib_util_find_spec: + def find_module(modulename): + """Find the module named `modulename`. + Returns the file path of the module, and the name of the enclosing + package. + """ + # pylint: disable=no-member + try: + spec = importlib_util_find_spec(modulename) + except ImportError as err: + raise NoSource(str(err)) + if not spec: + raise NoSource("No module named %r" % (modulename,)) + pathname = spec.origin + packagename = spec.name + if pathname.endswith("__init__.py"): + mod_main = modulename + ".__main__" + spec = importlib_util_find_spec(mod_main) + if not spec: + raise NoSource( + "No module named %s; " + "%r is a package and cannot be directly executed" + % (mod_main, modulename) + ) + pathname = spec.origin + packagename = spec.name + packagename = packagename.rpartition(".")[0] + return pathname, packagename +else: + def find_module(modulename): + """Find the module named `modulename`. + + Returns the file path of the module, and the name of the enclosing + package. + """ + openfile = None + glo, loc = globals(), locals() + try: + # Search for the module - inside its parent package, if any - using + # standard import mechanics. + if '.' in modulename: + packagename, name = modulename.rsplit('.', 1) + package = __import__(packagename, glo, loc, ['__path__']) + searchpath = package.__path__ + else: + packagename, name = None, modulename + searchpath = None # "top-level search" in imp.find_module() + openfile, pathname, _ = imp.find_module(name, searchpath) -def rsplit1(s, sep): - """The same as s.rsplit(sep, 1), but works in 2.3""" - parts = s.split(sep) - return sep.join(parts[:-1]), parts[-1] + # Complain if this is a magic non-file module. + if openfile is None and pathname is None: + raise NoSource( + "module does not live in a file: %r" % modulename + ) + + # If `modulename` is actually a package, not a mere module, then we + # pretend to be Python 2.7 and try running its __main__.py script. + if openfile is None: + packagename = modulename + name = '__main__' + package = __import__(packagename, glo, loc, ['__path__']) + searchpath = package.__path__ + openfile, pathname, _ = imp.find_module(name, searchpath) + except ImportError as err: + raise NoSource(str(err)) + finally: + if openfile: + openfile.close() + + return pathname, packagename def run_python_module(modulename, args): @@ -28,41 +88,8 @@ def run_python_module(modulename, args): element naming the module being executed. """ - openfile = None - glo, loc = globals(), locals() - try: - # Search for the module - inside its parent package, if any - using - # standard import mechanics. - if '.' in modulename: - packagename, name = rsplit1(modulename, '.') - package = __import__(packagename, glo, loc, ['__path__']) - searchpath = package.__path__ - else: - packagename, name = None, modulename - searchpath = None # "top-level search" in imp.find_module() - openfile, pathname, _ = imp.find_module(name, searchpath) - - # Complain if this is a magic non-file module. - if openfile is None and pathname is None: - raise NoSource( - "module does not live in a file: %r" % modulename - ) + pathname, packagename = find_module(modulename) - # If `modulename` is actually a package, not a mere module, then we - # pretend to be Python 2.7 and try running its __main__.py script. - if openfile is None: - packagename = modulename - name = '__main__' - package = __import__(packagename, glo, loc, ['__path__']) - searchpath = package.__path__ - openfile, pathname, _ = imp.find_module(name, searchpath) - except ImportError as err: - raise NoSource(str(err)) - finally: - if openfile: - openfile.close() - - # Finally, hand the file off to run_python_file for execution. pathname = os.path.abspath(pathname) args[0] = pathname run_python_file(pathname, args, package=packagename) @@ -79,7 +106,7 @@ def run_python_file(filename, args, package=None): """ # Create a module to serve as __main__ old_main_mod = sys.modules['__main__'] - main_mod = imp.new_module('__main__') + main_mod = types.ModuleType('__main__') sys.modules['__main__'] = main_mod main_mod.__file__ = filename if package: @@ -119,6 +146,7 @@ def run_python_file(filename, args, package=None): # Restore the old argv and path sys.argv = old_argv + def make_code_from_py(filename): """Get source from `filename` and make a code object of it.""" # Open the source file. @@ -150,7 +178,7 @@ def make_code_from_pyc(filename): # First four bytes are a version-specific magic number. It has to # match or we won't run the file. magic = fpyc.read(4) - if magic != imp.get_magic(): + if magic != PYC_MAGIC_NUMBER: raise NoCode("Bad magic number in .pyc file") # Skip the junk in the header that we don't need. diff --git a/coverage/extension.py b/coverage/extension.py new file mode 100644 index 00000000..8c89b88e --- /dev/null +++ b/coverage/extension.py @@ -0,0 +1,20 @@ +"""Extension management for coverage.py""" + +def load_extensions(modules, name): + """Load extensions from `modules`, finding them by `name`. + + Yields the loaded extensions. + + """ + + for module in modules: + try: + __import__(module) + mod = sys.modules[module] + except ImportError: + blah() + continue + + entry = getattr(mod, name, None) + if entry: + yield entry diff --git a/coverage/files.py b/coverage/files.py index 94388f96..08ce1e84 100644 --- a/coverage/files.py +++ b/coverage/files.py @@ -1,7 +1,7 @@ """File wrangling.""" from coverage.backward import to_string -from coverage.misc import CoverageException +from coverage.misc import CoverageException, join_regex import fnmatch, os, os.path, re, sys import ntpath, posixpath @@ -177,6 +177,7 @@ class FnmatchMatcher(object): """A matcher for files by filename pattern.""" def __init__(self, pats): self.pats = pats[:] + self.re = re.compile(join_regex([fnmatch.translate(p) for p in pats])) def __repr__(self): return "<FnmatchMatcher %r>" % self.pats @@ -187,10 +188,7 @@ class FnmatchMatcher(object): def match(self, fpath): """Does `fpath` match one of our filename patterns?""" - for pat in self.pats: - if fnmatch.fnmatch(fpath, pat): - return True - return False + return self.re.match(fpath) is not None def sep(s): diff --git a/coverage/html.py b/coverage/html.py index 42da2972..6e21efaa 100644 --- a/coverage/html.py +++ b/coverage/html.py @@ -149,9 +149,7 @@ class HtmlReporter(Reporter): def html_file(self, cu, analysis): """Generate an HTML file for one source file.""" - source_file = cu.source_file() - with source_file: - source = source_file.read() + source = cu.source() # Find out if the file on disk is already correct. flat_rootname = cu.flat_rootname() @@ -241,7 +239,9 @@ class HtmlReporter(Reporter): })) if sys.version_info < (3, 0): - html = html.decode(encoding) + # In theory, all the characters in the source can be decoded, but + # strange things happen, so use 'replace' to keep errors at bay. + html = html.decode(encoding, 'replace') html_filename = flat_rootname + ".html" html_path = os.path.join(self.directory, html_filename) diff --git a/coverage/misc.py b/coverage/misc.py index c88d4ecd..4b1dccb2 100644 --- a/coverage/misc.py +++ b/coverage/misc.py @@ -87,7 +87,7 @@ def bool_or_none(b): def join_regex(regexes): """Combine a list of regexes into one that matches any of them.""" if len(regexes) > 1: - return "|".join("(%s)" % r for r in regexes) + return "|".join("(?:%s)" % r for r in regexes) elif regexes: return regexes[0] else: diff --git a/coverage/parser.py b/coverage/parser.py index cfaf02fa..c5e95baa 100644 --- a/coverage/parser.py +++ b/coverage/parser.py @@ -14,23 +14,11 @@ class CodeParser(object): """ Base class for any code parser. """ - def _adjust_filename(self, fname): - return fname - - def first_lines(self, lines): - """Map the line numbers in `lines` to the correct first line of the - statement. - - Returns a set of the first lines. - - """ - return set(self.first_line(l) for l in lines) - - def first_line(self, line): - return line - def translate_lines(self, lines): - return lines + return set(lines) + + def translate_arcs(self, arcs): + return arcs def exit_counts(self): return {} @@ -42,7 +30,7 @@ class CodeParser(object): class PythonParser(CodeParser): """Parse code to find executable lines, excluded lines, etc.""" - def __init__(self, cu, text=None, filename=None, exclude=None): + def __init__(self, text=None, filename=None, exclude=None): """ Source can be provided as `text`, the text itself, or `filename`, from which the text will be read. Excluded lines are those that match @@ -197,6 +185,24 @@ class PythonParser(CodeParser): else: return line + def first_lines(self, lines): + """Map the line numbers in `lines` to the correct first line of the + statement. + + Returns a set of the first lines. + + """ + return set(self.first_line(l) for l in lines) + + def translate_lines(self, lines): + return self.first_lines(lines) + + def translate_arcs(self, arcs): + return [ + (self.first_line(a), self.first_line(b)) + for (a, b) in arcs + ] + def parse_source(self): """Parse source text to find executable lines, excluded lines, etc. diff --git a/coverage/phystokens.py b/coverage/phystokens.py index e79ce01f..867388f7 100644 --- a/coverage/phystokens.py +++ b/coverage/phystokens.py @@ -120,7 +120,7 @@ def source_encoding(source): # This is mostly code adapted from Py3.2's tokenize module. - cookie_re = re.compile(r"coding[:=]\s*([-\w.]+)") + cookie_re = re.compile(r"^\s*#.*coding[:=]\s*([-\w.]+)") # Do this so the detect_encode code we copied will work. readline = iter(source.splitlines(True)).next diff --git a/coverage/report.py b/coverage/report.py index 34f44422..7627d1aa 100644 --- a/coverage/report.py +++ b/coverage/report.py @@ -1,8 +1,8 @@ """Reporter foundation for Coverage.""" -import fnmatch, os +import os from coverage.codeunit import code_unit_factory -from coverage.files import prep_patterns +from coverage.files import prep_patterns, FnmatchMatcher from coverage.misc import CoverageException, NoSource, NotPython class Reporter(object): @@ -33,26 +33,24 @@ class Reporter(object): """ morfs = morfs or self.coverage.data.measured_files() file_locator = self.coverage.file_locator - self.code_units = code_unit_factory(morfs, file_locator) + get_ext = self.coverage.data.extension_data().get + self.code_units = code_unit_factory(morfs, file_locator, get_ext) if self.config.include: patterns = prep_patterns(self.config.include) + matcher = FnmatchMatcher(patterns) filtered = [] for cu in self.code_units: - for pattern in patterns: - if fnmatch.fnmatch(cu.filename, pattern): - filtered.append(cu) - break + if matcher.match(cu.filename): + filtered.append(cu) self.code_units = filtered if self.config.omit: patterns = prep_patterns(self.config.omit) + matcher = FnmatchMatcher(patterns) filtered = [] for cu in self.code_units: - for pattern in patterns: - if fnmatch.fnmatch(cu.filename, pattern): - break - else: + if not matcher.match(cu.filename): filtered.append(cu) self.code_units = filtered diff --git a/coverage/results.py b/coverage/results.py index 08329766..6cbcbfc8 100644 --- a/coverage/results.py +++ b/coverage/results.py @@ -14,11 +14,7 @@ class Analysis(object): self.code_unit = code_unit self.filename = self.code_unit.filename - actual_filename, source = self.code_unit.find_source(self.filename) - - self.parser = code_unit.parser_class( - code_unit, - text=source, filename=actual_filename, + self.parser = code_unit.get_parser( exclude=self.coverage._exclude_regex('exclude') ) self.statements, self.excluded = self.parser.parse_source() @@ -26,7 +22,6 @@ class Analysis(object): # Identify missing statements. executed = self.coverage.data.executed_lines(self.filename) executed = self.parser.translate_lines(executed) - executed = self.parser.first_lines(executed) self.missing = self.statements - executed if self.coverage.data.has_arcs(): @@ -74,8 +69,7 @@ class Analysis(object): def arcs_executed(self): """Returns a sorted list of the arcs actually executed in the code.""" executed = self.coverage.data.executed_arcs(self.filename) - m2fl = self.parser.first_line - executed = ((m2fl(l1), m2fl(l2)) for (l1,l2) in executed) + executed = self.parser.translate_arcs(executed) return sorted(executed) def arcs_missing(self): @@ -89,6 +83,23 @@ class Analysis(object): ) return sorted(missing) + def arcs_missing_formatted(self): + """ The missing branch arcs, formatted nicely. + + Returns a string like "1->2, 1->3, 16->20". Omits any mention of + missing lines, so if line 17 is missing, then 16->17 won't be included. + + """ + arcs = self.missing_branch_arcs() + missing = self.missing + line_exits = sorted(iitems(arcs)) + pairs = [] + for line, exits in line_exits: + for ex in sorted(exits): + if line not in missing and ex not in missing: + pairs.append('%d->%d' % (line, ex)) + return ', '.join(pairs) + def arcs_unpredicted(self): """Returns a sorted list of the executed arcs missing from the code.""" possible = self.arc_possibilities() diff --git a/coverage/summary.py b/coverage/summary.py index c99c5303..a6768cf9 100644 --- a/coverage/summary.py +++ b/coverage/summary.py @@ -59,7 +59,14 @@ class SummaryReporter(Reporter): args += (nums.n_branches, nums.n_missing_branches) args += (nums.pc_covered_str,) if self.config.show_missing: - args += (analysis.missing_formatted(),) + missing_fmtd = analysis.missing_formatted() + if self.branches: + branches_fmtd = analysis.arcs_missing_formatted() + if branches_fmtd: + if missing_fmtd: + missing_fmtd += ", " + missing_fmtd += branches_fmtd + args += (missing_fmtd,) outfile.write(fmt_coverage % args) total += nums except KeyboardInterrupt: # pragma: not covered diff --git a/coverage/templite.py b/coverage/templite.py index a71caf63..53824e08 100644 --- a/coverage/templite.py +++ b/coverage/templite.py @@ -15,7 +15,7 @@ class CodeBuilder(object): def __init__(self, indent=0): self.code = [] - self.ident_level = indent + self.indent_level = indent def __str__(self): return "".join(str(c) for c in self.code) @@ -26,28 +26,28 @@ class CodeBuilder(object): Indentation and newline will be added for you, don't provide them. """ - self.code.extend([" " * self.ident_level, line, "\n"]) + self.code.extend([" " * self.indent_level, line, "\n"]) - def add_subbuilder(self): + def add_section(self): """Add a section, a sub-CodeBuilder.""" - sect = CodeBuilder(self.ident_level) - self.code.append(sect) - return sect + section = CodeBuilder(self.indent_level) + self.code.append(section) + return section INDENT_STEP = 4 # PEP8 says so! def indent(self): """Increase the current indent for following lines.""" - self.ident_level += self.INDENT_STEP + self.indent_level += self.INDENT_STEP def dedent(self): """Decrease the current indent for following lines.""" - self.ident_level -= self.INDENT_STEP + self.indent_level -= self.INDENT_STEP def get_globals(self): - """Compile the code, and return a dict of globals it defines.""" + """Execute the code, and return a dict of globals it defines.""" # A check that the caller really finished all the blocks they started. - assert self.ident_level == 0 + assert self.indent_level == 0 # Get the Python source as a single string. python_source = str(self) # Execute the source, defining globals, and return them. @@ -110,21 +110,21 @@ class Templite(object): # it, and execute it to render the template. code = CodeBuilder() - code.add_line("def render_function(ctx, do_dots):") + code.add_line("def render_function(context, do_dots):") code.indent() - vars_code = code.add_subbuilder() + vars_code = code.add_section() code.add_line("result = []") - code.add_line("a = result.append") - code.add_line("e = result.extend") - code.add_line("s = str") + code.add_line("append_result = result.append") + code.add_line("extend_result = result.extend") + code.add_line("to_str = str") buffered = [] def flush_output(): """Force `buffered` to the code builder.""" if len(buffered) == 1: - code.add_line("a(%s)" % buffered[0]) + code.add_line("append_result(%s)" % buffered[0]) elif len(buffered) > 1: - code.add_line("e([%s])" % ", ".join(buffered)) + code.add_line("extend_result([%s])" % ", ".join(buffered)) del buffered[:] ops_stack = [] @@ -138,7 +138,8 @@ class Templite(object): continue elif token.startswith('{{'): # An expression to evaluate. - buffered.append("s(%s)" % self._expr_code(token[2:-2].strip())) + expr = self._expr_code(token[2:-2].strip()) + buffered.append("to_str(%s)" % expr) elif token.startswith('{%'): # Action tag: split into words and parse further. flush_output() @@ -187,7 +188,7 @@ class Templite(object): flush_output() for var_name in self.all_vars - self.loop_vars: - vars_code.add_line("c_%s = ctx[%r]" % (var_name, var_name)) + vars_code.add_line("c_%s = context[%r]" % (var_name, var_name)) code.add_line("return ''.join(result)") code.dedent() @@ -234,10 +235,10 @@ class Templite(object): """ # Make the complete context we'll use. - ctx = dict(self.context) + render_context = dict(self.context) if context: - ctx.update(context) - return self._render_function(ctx, self._do_dots) + render_context.update(context) + return self._render_function(render_context, self._do_dots) def _do_dots(self, value, *dots): """Evaluate dotted expressions at runtime.""" diff --git a/coverage/tracer.c b/coverage/tracer.c index 97dd113b..ca8d61c1 100644 --- a/coverage/tracer.c +++ b/coverage/tracer.c @@ -259,6 +259,7 @@ CTracer_trace(CTracer *self, PyFrameObject *frame, int what, PyObject *arg_unuse int ret = RET_OK; PyObject * filename = NULL; PyObject * tracename = NULL; + PyObject * disposition = NULL; #if WHAT_LOG || TRACE_LOG PyObject * ascii = NULL; #endif @@ -335,41 +336,51 @@ CTracer_trace(CTracer *self, PyFrameObject *frame, int what, PyObject *arg_unuse /* Check if we should trace this line. */ filename = frame->f_code->co_filename; - tracename = PyDict_GetItem(self->should_trace_cache, filename); - if (tracename == NULL) { + disposition = PyDict_GetItem(self->should_trace_cache, filename); + if (disposition == NULL) { STATS( self->stats.new_files++; ) /* We've never considered this file before. */ /* Ask should_trace about it. */ PyObject * args = Py_BuildValue("(OO)", filename, frame); - tracename = PyObject_Call(self->should_trace, args, NULL); + disposition = PyObject_Call(self->should_trace, args, NULL); Py_DECREF(args); - if (tracename == NULL) { + if (disposition == NULL) { /* An error occurred inside should_trace. */ STATS( self->stats.errors++; ) return RET_ERROR; } - if (PyDict_SetItem(self->should_trace_cache, filename, tracename) < 0) { + if (PyDict_SetItem(self->should_trace_cache, filename, disposition) < 0) { STATS( self->stats.errors++; ) return RET_ERROR; } } else { - Py_INCREF(tracename); + Py_INCREF(disposition); } /* If tracename is a string, then we're supposed to trace. */ + tracename = PyObject_GetAttrString(disposition, "filename"); + if (tracename == NULL) { + STATS( self->stats.errors++; ) + Py_DECREF(disposition); + return RET_ERROR; + } if (MyText_Check(tracename)) { PyObject * file_data = PyDict_GetItem(self->data, tracename); if (file_data == NULL) { file_data = PyDict_New(); if (file_data == NULL) { STATS( self->stats.errors++; ) + Py_DECREF(tracename); + Py_DECREF(disposition); return RET_ERROR; } ret = PyDict_SetItem(self->data, tracename, file_data); Py_DECREF(file_data); if (ret < 0) { STATS( self->stats.errors++; ) + Py_DECREF(tracename); + Py_DECREF(disposition); return RET_ERROR; } } @@ -385,6 +396,7 @@ CTracer_trace(CTracer *self, PyFrameObject *frame, int what, PyObject *arg_unuse } Py_DECREF(tracename); + Py_DECREF(disposition); self->last_line = -1; break; |