Fix a few stylistic nits.

This commit is contained in:
Georg Brandl 2009-04-13 08:35:05 +00:00
parent b13f61bc42
commit ae1c7f9d56
3 changed files with 58 additions and 56 deletions

View File

@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
""" """
sphinx.ext.inheritance_diagram sphinx.ext.inheritance_diagram
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -106,20 +106,20 @@ class StatementFindingAstVisitor(compiler.visitor.ASTVisitor):
self.excluded = excluded self.excluded = excluded
self.suite_spots = suite_spots self.suite_spots = suite_spots
self.excluding_suite = 0 self.excluding_suite = 0
def doRecursive(self, node): def doRecursive(self, node):
for n in node.getChildNodes(): for n in node.getChildNodes():
self.dispatch(n) self.dispatch(n)
visitStmt = visitModule = doRecursive visitStmt = visitModule = doRecursive
def doCode(self, node): def doCode(self, node):
if hasattr(node, 'decorators') and node.decorators: if hasattr(node, 'decorators') and node.decorators:
self.dispatch(node.decorators) self.dispatch(node.decorators)
self.recordAndDispatch(node.code) self.recordAndDispatch(node.code)
else: else:
self.doSuite(node, node.code) self.doSuite(node, node.code)
visitFunction = visitClass = doCode visitFunction = visitClass = doCode
def getFirstLine(self, node): def getFirstLine(self, node):
@ -139,14 +139,14 @@ class StatementFindingAstVisitor(compiler.visitor.ASTVisitor):
for n in node.getChildNodes(): for n in node.getChildNodes():
lineno = max(lineno, self.getLastLine(n)) lineno = max(lineno, self.getLastLine(n))
return lineno return lineno
def doStatement(self, node): def doStatement(self, node):
self.recordLine(self.getFirstLine(node)) self.recordLine(self.getFirstLine(node))
visitAssert = visitAssign = visitAssTuple = visitPrint = \ visitAssert = visitAssign = visitAssTuple = visitPrint = \
visitPrintnl = visitRaise = visitSubscript = visitDecorators = \ visitPrintnl = visitRaise = visitSubscript = visitDecorators = \
doStatement doStatement
def visitPass(self, node): def visitPass(self, node):
# Pass statements have weird interactions with docstrings. If this # Pass statements have weird interactions with docstrings. If this
# pass statement is part of one of those pairs, claim that the statement # pass statement is part of one of those pairs, claim that the statement
@ -155,10 +155,10 @@ class StatementFindingAstVisitor(compiler.visitor.ASTVisitor):
if l: if l:
lines = self.suite_spots.get(l, [l,l]) lines = self.suite_spots.get(l, [l,l])
self.statements[lines[1]] = 1 self.statements[lines[1]] = 1
def visitDiscard(self, node): def visitDiscard(self, node):
# Discard nodes are statements that execute an expression, but then # Discard nodes are statements that execute an expression, but then
# discard the results. This includes function calls, so we can't # discard the results. This includes function calls, so we can't
# ignore them all. But if the expression is a constant, the statement # ignore them all. But if the expression is a constant, the statement
# won't be "executed", so don't count it now. # won't be "executed", so don't count it now.
if node.expr.__class__.__name__ != 'Const': if node.expr.__class__.__name__ != 'Const':
@ -172,7 +172,7 @@ class StatementFindingAstVisitor(compiler.visitor.ASTVisitor):
return self.recordLine(self.getFirstLine(node)) return self.recordLine(self.getFirstLine(node))
else: else:
return 0 return 0
def recordLine(self, lineno): def recordLine(self, lineno):
# Returns a bool, whether the line is included or excluded. # Returns a bool, whether the line is included or excluded.
if lineno: if lineno:
@ -196,9 +196,9 @@ class StatementFindingAstVisitor(compiler.visitor.ASTVisitor):
self.statements[lineno] = 1 self.statements[lineno] = 1
return 1 return 1
return 0 return 0
default = recordNodeLine default = recordNodeLine
def recordAndDispatch(self, node): def recordAndDispatch(self, node):
self.recordNodeLine(node) self.recordNodeLine(node)
self.dispatch(node) self.dispatch(node)
@ -209,7 +209,7 @@ class StatementFindingAstVisitor(compiler.visitor.ASTVisitor):
self.excluding_suite = 1 self.excluding_suite = 1
self.recordAndDispatch(body) self.recordAndDispatch(body)
self.excluding_suite = exsuite self.excluding_suite = exsuite
def doPlainWordSuite(self, prevsuite, suite): def doPlainWordSuite(self, prevsuite, suite):
# Finding the exclude lines for else's is tricky, because they aren't # Finding the exclude lines for else's is tricky, because they aren't
# present in the compiler parse tree. Look at the previous suite, # present in the compiler parse tree. Look at the previous suite,
@ -223,11 +223,11 @@ class StatementFindingAstVisitor(compiler.visitor.ASTVisitor):
break break
else: else:
self.doSuite(None, suite) self.doSuite(None, suite)
def doElse(self, prevsuite, node): def doElse(self, prevsuite, node):
if node.else_: if node.else_:
self.doPlainWordSuite(prevsuite, node.else_) self.doPlainWordSuite(prevsuite, node.else_)
def visitFor(self, node): def visitFor(self, node):
self.doSuite(node, node.body) self.doSuite(node, node.body)
self.doElse(node.body, node) self.doElse(node.body, node)
@ -257,14 +257,14 @@ class StatementFindingAstVisitor(compiler.visitor.ASTVisitor):
else: else:
self.doSuite(a, h) self.doSuite(a, h)
self.doElse(node.handlers[-1][2], node) self.doElse(node.handlers[-1][2], node)
def visitTryFinally(self, node): def visitTryFinally(self, node):
self.doSuite(node, node.body) self.doSuite(node, node.body)
self.doPlainWordSuite(node.body, node.final) self.doPlainWordSuite(node.body, node.final)
def visitWith(self, node): def visitWith(self, node):
self.doSuite(node, node.body) self.doSuite(node, node.body)
def visitGlobal(self, node): def visitGlobal(self, node):
# "global" statements don't execute like others (they don't call the # "global" statements don't execute like others (they don't call the
# trace function), so don't record their line numbers. # trace function), so don't record their line numbers.
@ -285,7 +285,7 @@ class coverage:
# A dictionary with an entry for (Python source file name, line number # A dictionary with an entry for (Python source file name, line number
# in that file) if that line has been executed. # in that file) if that line has been executed.
c = {} c = {}
# A map from canonical Python source file name to a dictionary in # A map from canonical Python source file name to a dictionary in
# which there's an entry for each line number that has been # which there's an entry for each line number that has been
# executed. # executed.
@ -313,19 +313,19 @@ class coverage:
self.relative_dir = self.abs_file(os.curdir)+os.sep self.relative_dir = self.abs_file(os.curdir)+os.sep
self.exclude('# *pragma[: ]*[nN][oO] *[cC][oO][vV][eE][rR]') self.exclude('# *pragma[: ]*[nN][oO] *[cC][oO][vV][eE][rR]')
# t(f, x, y). This method is passed to sys.settrace as a trace function. # t(f, x, y). This method is passed to sys.settrace as a trace function.
# See [van Rossum 2001-07-20b, 9.2] for an explanation of sys.settrace and # See [van Rossum 2001-07-20b, 9.2] for an explanation of sys.settrace and
# the arguments and return value of the trace function. # the arguments and return value of the trace function.
# See [van Rossum 2001-07-20a, 3.2] for a description of frame and code # See [van Rossum 2001-07-20a, 3.2] for a description of frame and code
# objects. # objects.
def t(self, f, w, unused): #pragma: no cover def t(self, f, w, unused): #pragma: no cover
if w == 'line': if w == 'line':
self.c[(f.f_code.co_filename, f.f_lineno)] = 1 self.c[(f.f_code.co_filename, f.f_lineno)] = 1
#-for c in self.cstack: #-for c in self.cstack:
#- c[(f.f_code.co_filename, f.f_lineno)] = 1 #- c[(f.f_code.co_filename, f.f_lineno)] = 1
return self.t return self.t
def help(self, error=None): #pragma: no cover def help(self, error=None): #pragma: no cover
if error: if error:
print error print error
@ -377,14 +377,14 @@ class coverage:
args_needed = (settings.get('execute') args_needed = (settings.get('execute')
or settings.get('annotate') or settings.get('annotate')
or settings.get('report')) or settings.get('report'))
action = (settings.get('erase') action = (settings.get('erase')
or settings.get('collect') or settings.get('collect')
or args_needed) or args_needed)
if not action: if not action:
help_fn("You must specify at least one of -e, -x, -c, -r, or -a.") help_fn("You must specify at least one of -e, -x, -c, -r, or -a.")
if not args_needed and args: if not args_needed and args:
help_fn("Unexpected arguments: %s" % " ".join(args)) help_fn("Unexpected arguments: %s" % " ".join(args))
self.parallel_mode = settings.get('parallel-mode') self.parallel_mode = settings.get('parallel-mode')
self.get_ready() self.get_ready()
@ -402,7 +402,7 @@ class coverage:
self.collect() self.collect()
if not args: if not args:
args = self.cexecuted.keys() args = self.cexecuted.keys()
ignore_errors = settings.get('ignore-errors') ignore_errors = settings.get('ignore-errors')
show_missing = settings.get('show-missing') show_missing = settings.get('show-missing')
directory = settings.get('directory=') directory = settings.get('directory=')
@ -412,7 +412,7 @@ class coverage:
omit = [self.abs_file(p) for p in omit.split(',')] omit = [self.abs_file(p) for p in omit.split(',')]
else: else:
omit = [] omit = []
if settings.get('report'): if settings.get('report'):
self.report(args, show_missing, ignore_errors, omit_prefixes=omit) self.report(args, show_missing, ignore_errors, omit_prefixes=omit)
if settings.get('annotate'): if settings.get('annotate'):
@ -422,7 +422,7 @@ class coverage:
self.usecache = usecache self.usecache = usecache
if cache_file and not self.cache: if cache_file and not self.cache:
self.cache_default = cache_file self.cache_default = cache_file
def get_ready(self, parallel_mode=False): def get_ready(self, parallel_mode=False):
if self.usecache and not self.cache: if self.usecache and not self.cache:
self.cache = os.environ.get(self.cache_env, self.cache_default) self.cache = os.environ.get(self.cache_env, self.cache_default)
@ -430,7 +430,7 @@ class coverage:
self.cache += "." + gethostname() + "." + str(os.getpid()) self.cache += "." + gethostname() + "." + str(os.getpid())
self.restore() self.restore()
self.analysis_cache = {} self.analysis_cache = {}
def start(self, parallel_mode=False): def start(self, parallel_mode=False):
self.get_ready() self.get_ready()
if self.nesting == 0: #pragma: no cover if self.nesting == 0: #pragma: no cover
@ -438,7 +438,7 @@ class coverage:
if hasattr(threading, 'settrace'): if hasattr(threading, 'settrace'):
threading.settrace(self.t) threading.settrace(self.t)
self.nesting += 1 self.nesting += 1
def stop(self): def stop(self):
self.nesting -= 1 self.nesting -= 1
if self.nesting == 0: #pragma: no cover if self.nesting == 0: #pragma: no cover
@ -462,7 +462,7 @@ class coverage:
def begin_recursive(self): def begin_recursive(self):
self.cstack.append(self.c) self.cstack.append(self.c)
self.xstack.append(self.exclude_re) self.xstack.append(self.exclude_re)
def end_recursive(self): def end_recursive(self):
self.c = self.cstack.pop() self.c = self.cstack.pop()
self.exclude_re = self.xstack.pop() self.exclude_re = self.xstack.pop()
@ -568,7 +568,7 @@ class coverage:
self.canonical_filename_cache[filename] = cf self.canonical_filename_cache[filename] = cf
return self.canonical_filename_cache[filename] return self.canonical_filename_cache[filename]
# canonicalize_filenames(). Copy results from "c" to "cexecuted", # canonicalize_filenames(). Copy results from "c" to "cexecuted",
# canonicalizing filenames on the way. Clear the "c" map. # canonicalizing filenames on the way. Clear the "c" map.
def canonicalize_filenames(self): def canonicalize_filenames(self):
@ -598,7 +598,7 @@ class coverage:
# in the source code, (3) a list of lines of excluded statements, # in the source code, (3) a list of lines of excluded statements,
# and (4), a map of line numbers to multi-line line number ranges, for # and (4), a map of line numbers to multi-line line number ranges, for
# statements that cross lines. # statements that cross lines.
def analyze_morf(self, morf): def analyze_morf(self, morf):
if self.analysis_cache.has_key(morf): if self.analysis_cache.has_key(morf):
return self.analysis_cache[morf] return self.analysis_cache[morf]
@ -636,26 +636,26 @@ class coverage:
if len(tree) == 3 and type(tree[2]) == type(1): if len(tree) == 3 and type(tree[2]) == type(1):
return tree[2] return tree[2]
tree = tree[1] tree = tree[1]
def last_line_of_tree(self, tree): def last_line_of_tree(self, tree):
while True: while True:
if len(tree) == 3 and type(tree[2]) == type(1): if len(tree) == 3 and type(tree[2]) == type(1):
return tree[2] return tree[2]
tree = tree[-1] tree = tree[-1]
def find_docstring_pass_pair(self, tree, spots): def find_docstring_pass_pair(self, tree, spots):
for i in range(1, len(tree)): for i in range(1, len(tree)):
if self.is_string_constant(tree[i]) and self.is_pass_stmt(tree[i+1]): if self.is_string_constant(tree[i]) and self.is_pass_stmt(tree[i+1]):
first_line = self.first_line_of_tree(tree[i]) first_line = self.first_line_of_tree(tree[i])
last_line = self.last_line_of_tree(tree[i+1]) last_line = self.last_line_of_tree(tree[i+1])
self.record_multiline(spots, first_line, last_line) self.record_multiline(spots, first_line, last_line)
def is_string_constant(self, tree): def is_string_constant(self, tree):
try: try:
return tree[0] == symbol.stmt and tree[1][1][1][0] == symbol.expr_stmt return tree[0] == symbol.stmt and tree[1][1][1][0] == symbol.expr_stmt
except: except:
return False return False
def is_pass_stmt(self, tree): def is_pass_stmt(self, tree):
try: try:
return tree[0] == symbol.stmt and tree[1][1][1][0] == symbol.pass_stmt return tree[0] == symbol.stmt and tree[1][1][1][0] == symbol.pass_stmt
@ -665,7 +665,7 @@ class coverage:
def record_multiline(self, spots, i, j): def record_multiline(self, spots, i, j):
for l in range(i, j+1): for l in range(i, j+1):
spots[l] = (i, j) spots[l] = (i, j)
def get_suite_spots(self, tree, spots): def get_suite_spots(self, tree, spots):
""" Analyze a parse tree to find suite introducers which span a number """ Analyze a parse tree to find suite introducers which span a number
of lines. of lines.
@ -707,7 +707,7 @@ class coverage:
# treat them differently, especially in the common case of a # treat them differently, especially in the common case of a
# function with a doc string and a single pass statement. # function with a doc string and a single pass statement.
self.find_docstring_pass_pair(tree[i], spots) self.find_docstring_pass_pair(tree[i], spots)
elif tree[i][0] == symbol.simple_stmt: elif tree[i][0] == symbol.simple_stmt:
first_line = self.first_line_of_tree(tree[i]) first_line = self.first_line_of_tree(tree[i])
last_line = self.last_line_of_tree(tree[i]) last_line = self.last_line_of_tree(tree[i])
@ -732,7 +732,7 @@ class coverage:
tree = parser.suite(text+'\n\n').totuple(1) tree = parser.suite(text+'\n\n').totuple(1)
self.get_suite_spots(tree, suite_spots) self.get_suite_spots(tree, suite_spots)
#print "Suite spots:", suite_spots #print "Suite spots:", suite_spots
# Use the compiler module to parse the text and find the executable # Use the compiler module to parse the text and find the executable
# statements. We add newlines to be impervious to final partial lines. # statements. We add newlines to be impervious to final partial lines.
statements = {} statements = {}
@ -842,7 +842,7 @@ class coverage:
else: else:
globbed.append(morf) globbed.append(morf)
morfs = globbed morfs = globbed
morfs = self.filter_by_prefix(morfs, omit_prefixes) morfs = self.filter_by_prefix(morfs, omit_prefixes)
morfs.sort(self.morf_name_compare) morfs.sort(self.morf_name_compare)
@ -909,7 +909,7 @@ class coverage:
except: except:
if not ignore_errors: if not ignore_errors:
raise raise
def annotate_file(self, filename, statements, excluded, missing, directory=None): def annotate_file(self, filename, statements, excluded, missing, directory=None):
source = open(filename, 'r') source = open(filename, 'r')
if directory: if directory:
@ -937,7 +937,7 @@ class coverage:
if self.blank_re.match(line): if self.blank_re.match(line):
dest.write(' ') dest.write(' ')
elif self.else_re.match(line): elif self.else_re.match(line):
# Special logic for lines containing only 'else:'. # Special logic for lines containing only 'else:'.
# See [GDR 2001-12-04b, 3.2]. # See [GDR 2001-12-04b, 3.2].
if i >= len(statements) and j >= len(missing): if i >= len(statements) and j >= len(missing):
dest.write('! ') dest.write('! ')
@ -961,40 +961,40 @@ class coverage:
the_coverage = coverage() the_coverage = coverage()
# Module functions call methods in the singleton object. # Module functions call methods in the singleton object.
def use_cache(*args, **kw): def use_cache(*args, **kw):
return the_coverage.use_cache(*args, **kw) return the_coverage.use_cache(*args, **kw)
def start(*args, **kw): def start(*args, **kw):
return the_coverage.start(*args, **kw) return the_coverage.start(*args, **kw)
def stop(*args, **kw): def stop(*args, **kw):
return the_coverage.stop(*args, **kw) return the_coverage.stop(*args, **kw)
def erase(*args, **kw): def erase(*args, **kw):
return the_coverage.erase(*args, **kw) return the_coverage.erase(*args, **kw)
def begin_recursive(*args, **kw): def begin_recursive(*args, **kw):
return the_coverage.begin_recursive(*args, **kw) return the_coverage.begin_recursive(*args, **kw)
def end_recursive(*args, **kw): def end_recursive(*args, **kw):
return the_coverage.end_recursive(*args, **kw) return the_coverage.end_recursive(*args, **kw)
def exclude(*args, **kw): def exclude(*args, **kw):
return the_coverage.exclude(*args, **kw) return the_coverage.exclude(*args, **kw)
def analysis(*args, **kw): def analysis(*args, **kw):
return the_coverage.analysis(*args, **kw) return the_coverage.analysis(*args, **kw)
def analysis2(*args, **kw): def analysis2(*args, **kw):
return the_coverage.analysis2(*args, **kw) return the_coverage.analysis2(*args, **kw)
def report(*args, **kw): def report(*args, **kw):
return the_coverage.report(*args, **kw) return the_coverage.report(*args, **kw)
def annotate(*args, **kw): def annotate(*args, **kw):
return the_coverage.annotate(*args, **kw) return the_coverage.annotate(*args, **kw)
def annotate_file(*args, **kw): def annotate_file(*args, **kw):
return the_coverage.annotate_file(*args, **kw) return the_coverage.annotate_file(*args, **kw)
# Save coverage data when Python exits. (The atexit module wasn't # Save coverage data when Python exits. (The atexit module wasn't
@ -1008,7 +1008,7 @@ except ImportError:
def main(): def main():
the_coverage.command_line(sys.argv[1:]) the_coverage.command_line(sys.argv[1:])
# Command-line interface. # Command-line interface.
if __name__ == '__main__': if __name__ == '__main__':
main() main()
@ -1072,7 +1072,7 @@ if __name__ == '__main__':
# Thanks, Allen. # Thanks, Allen.
# #
# 2005-12-02 NMB Call threading.settrace so that all threads are measured. # 2005-12-02 NMB Call threading.settrace so that all threads are measured.
# Thanks Martin Fuzzey. Add a file argument to report so that reports can be # Thanks Martin Fuzzey. Add a file argument to report so that reports can be
# captured to a different destination. # captured to a different destination.
# #
# 2005-12-03 NMB coverage.py can now measure itself. # 2005-12-03 NMB coverage.py can now measure itself.

View File

@ -111,7 +111,8 @@ def test_inline():
def test_latex_escaping(): def test_latex_escaping():
# correct escaping in normal mode # correct escaping in normal mode
yield verify, u'Γ\\\\∞$', None, ur'\(\Gamma\)\textbackslash{}\(\infty\)\$' yield verify, u'Γ\\\\∞$', None, \
ur'\(\Gamma\)\textbackslash{}\(\infty\)\$'
# in verbatim code fragments # in verbatim code fragments
yield (verify, u'::\n\n\\∞$[]', None, yield (verify, u'::\n\n\\∞$[]', None,
u'\\begin{Verbatim}[commandchars=@\\[\\]]\n' u'\\begin{Verbatim}[commandchars=@\\[\\]]\n'