
Daniel Stutzbach wrote:
<another way to get to possibly relevant code>
It seems to me the question is, among the single-statement try blocks, what kinds of (non-function call, non-assignment) statements are the inner statement, and what frequency does this occur. On Python5, 25, and 30, I get quite similar numbers: 2.5 2.6 3.0 10 15 14 with 15 11 13 yield 25 26 22 class 32 23 *8 print # print is a function in 3.0, called 8 times 37 33 24 while 44 35 *26 exec # exec is a function in 3.0, called 26 times 9 42 41 pass 49 44 64 raise 67 45 29 del 95 61 40 for 142 68 42 if 182 105 63 from 230 163 107 import 588 307 150 return These numbers are pulled from the output of the attached imperfect program, and hand-editted together. The numbers seem to me to say that try-for and try-while substantially exceed try-with statements. --Scott David Daniels @Acm.Org import os import collections __version__ = '0.5' def hunt_try(*roots): singles = multi = count = 0 starting = collections.defaultdict(int) for root in roots: for b,d,f in os.walk(os.path.expanduser(root)): d.sort() for name in sorted(nm for nm in f if nm.endswith(('.py', '.pyw'))): with open(os.path.join(b, name)) as src: gen = enumerate(src) for n, line in gen: text = line.lstrip() if text.startswith('try:'): outer = line[: - len(text)] count += 1 for n1, line1 in gen: lst = line1.lstrip() if lst and not lst.startswith('#'): break else: if name is not None: print src.name name = None print 'Broken at %s: %s..%s' % (n, n1) continue indent = line1[: -len(lst)] if indent == outer and lst.startswith('except' ) or lst.startswith('finally:'): singles += 1 kind = line.split(None, 2)[1] starting[kind] += 1 continue for n2, line2 in gen: lst = line2.lstrip() if lst and not lst.startswith('#'): if line2.startswith(indent): if not line2[len(indent)].isspace(): multi += 1 break # multi-try elif line2.startswith(outer) and not line2[ len(outer)].isspace(): kind = line1.split(None, 1)[0] starting[kind] += 1 singles += 1 break else: if name is not None: print src.name name = None print 'Broken at %s..%s..%s' % ( n, n1, n2) break else: if name is not None: print src.name name = None print 'final try broken: %s..%s..%s' % ( n, n1, n2) return singles, multi, count, starting if __name__ == '__main__': import sys singles, multi, count, sng = hunt_try(*sys.argv[1:] or [r'C:\Python30']) print 'v%s, %s singles + %s multis + %s confused = %s "try:"s' % ( __version__, singles, multi, count - singles - multi, count) for pair in sorted((n, nm) for nm, n in sng.iteritems() if n > 9): print '%4d %s' % pair