[Python-checkins] python/dist/src/Tools/scripts trace.py,1.10,1.11

jhylton@users.sourceforge.net jhylton@users.sourceforge.net
2002年12月11日 13:28:34 -0800


Update of /cvsroot/python/python/dist/src/Tools/scripts
In directory sc8-pr-cvs1:/tmp/cvs-serv1182
Modified Files:
	trace.py 
Log Message:
Fix one bug and reformat lots of code.
The bug is a reference to co_first_lineno that should be
co_firstlineno. The only other substantial change is to speed up
localtrace_count() by avoiding *costly* calls to inspect module.
It's trivial to get the filename and lineno directly from the frame.
Otherwise, delete commented out debug code and reflow very long lines.
Index: trace.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Tools/scripts/trace.py,v
retrieving revision 1.10
retrieving revision 1.11
diff -C2 -d -r1.10 -r1.11
*** trace.py	22 Nov 2002 09:10:33 -0000	1.10
--- trace.py	11 Dec 2002 21:28:32 -0000	1.11
***************
*** 28,34 ****
 # distribution of the software without specific, written prior permission.
 #
! 
! """
! program/module to trace Python program or function execution
 
 Sample use, command line:
--- 28,32 ----
 # distribution of the software without specific, written prior permission.
 #
! """program/module to trace Python program or function execution
 
 Sample use, command line:
***************
*** 37,43 ****
 
 Sample use, programmatically
! # create a Trace object, telling it what to ignore, and whether to do tracing
! # or line-counting or both.
! trace = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix,], trace=0, count=1)
 # run the new command using the given trace
 trace.run(coverage.globaltrace, 'main()')
--- 35,42 ----
 
 Sample use, programmatically
! # create a Trace object, telling it what to ignore, and whether to
! # do tracing or line-counting or both.
! trace = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix,], trace=0,
! count=1)
 # run the new command using the given trace
 trace.run(coverage.globaltrace, 'main()')
***************
*** 168,174 ****
 self.update(self.__class__(thingie))
 elif type(thingie) is types.TupleType and len(thingie) == 2:
! (counts, calledfuncs,) = thingie
 self.update(self.__class__(counts, calledfuncs))
! except (IOError, EOFError,):
 pass
 except pickle.UnpicklingError:
--- 167,173 ----
 self.update(self.__class__(thingie))
 elif type(thingie) is types.TupleType and len(thingie) == 2:
! counts, calledfuncs = thingie
 self.update(self.__class__(counts, calledfuncs))
! except (IOError, EOFError):
 pass
 except pickle.UnpicklingError:
***************
*** 194,199 ****
 @param coverdir
 """
! for (filename, modulename, funcname,) in self.calledfuncs.keys():
! print "filename: %s, modulename: %s, funcname: %s" % (filename, modulename, funcname,)
 
 import re
--- 193,199 ----
 @param coverdir
 """
! for filename, modulename, funcname in self.calledfuncs.keys():
! print ("filename: %s, modulename: %s, funcname: %s"
! % (filename, modulename, funcname))
 
 import re
***************
*** 202,207 ****
 per_file = {}
 for thingie in self.counts.keys():
! if thingie != "calledfuncs": # backwards compatibility for abortive attempt to stuff calledfuncs into self.counts, by Zooko --Zooko 2001年10月24日
! (filename, lineno,) = thingie
 lines_hit = per_file[filename] = per_file.get(filename, {})
 lines_hit[lineno] = self.counts[(filename, lineno)]
--- 202,210 ----
 per_file = {}
 for thingie in self.counts.keys():
! if thingie != "calledfuncs":
! # backwards compatibility for abortive attempt to
! # stuff calledfuncs into self.counts, by Zooko --Zooko
! # 2001年10月24日
! filename, lineno = thingie
 lines_hit = per_file[filename] = per_file.get(filename, {})
 lines_hit[lineno] = self.counts[(filename, lineno)]
***************
*** 328,332 ****
 # try and store counts and module info into self.outfile
 try:
! pickle.dump((self.counts, self.calledfuncs,), open(self.outfile, 'w'), 1)
 except IOError, err:
 sys.stderr.write("cannot save counts files because %s" % err)
--- 331,336 ----
 # try and store counts and module info into self.outfile
 try:
! pickle.dump((self.counts, self.calledfuncs),
! open(self.outfile, 'w'), 1)
 except IOError, err:
 sys.stderr.write("cannot save counts files because %s" % err)
***************
*** 340,344 ****
 table_length = len(line_increments)
 
! lineno = code.co_first_lineno
 
 for li in line_increments:
--- 344,348 ----
 table_length = len(line_increments)
 
! lineno = code.co_firstlineno
 
 for li in line_increments:
***************
*** 466,470 ****
 """
 if why == 'call':
! (filename, lineno, funcname, context, lineindex,) = inspect.getframeinfo(frame, 0)
 if filename:
 modulename = inspect.getmodulename(filename)
--- 470,475 ----
 """
 if why == 'call':
! filename, lineno, funcname, context, lineindex = \
! inspect.getframeinfo(frame, 0)
 if filename:
 modulename = inspect.getmodulename(filename)
***************
*** 478,496 ****
 """
 if why == 'call':
! (filename, lineno, funcname, context, lineindex,) = inspect.getframeinfo(frame, 0)
! # if DEBUG_MODE and not filename:
! # print "%s.globaltrace(frame: %s, why: %s, arg: %s): filename: %s, lineno: %s, funcname: %s, context: %s, lineindex: %s\n" % (self, frame, why, arg, filename, lineno, funcname, context, lineindex,)
 if filename:
 modulename = inspect.getmodulename(filename)
 if modulename is not None:
 ignore_it = self.ignore.names(filename, modulename)
- # if DEBUG_MODE and not self.blabbed.has_key((filename, modulename,)):
- # self.blabbed[(filename, modulename,)] = None
- # print "%s.globaltrace(frame: %s, why: %s, arg: %s, filename: %s, modulename: %s, ignore_it: %s\n" % (self, frame, why, arg, filename, modulename, ignore_it,)
 if not ignore_it:
 if self.trace:
! print " --- modulename: %s, funcname: %s" % (modulename, funcname,)
! # if DEBUG_MODE:
! # print "%s.globaltrace(frame: %s, why: %s, arg: %s, filename: %s, modulename: %s, ignore_it: %s -- about to localtrace\n" % (self, frame, why, arg, filename, modulename, ignore_it,)
 return self.localtrace
 else:
--- 483,496 ----
 """
 if why == 'call':
! filename, lineno, funcname, context, lineindex = \
! inspect.getframeinfo(frame, 0)
 if filename:
 modulename = inspect.getmodulename(filename)
 if modulename is not None:
 ignore_it = self.ignore.names(filename, modulename)
 if not ignore_it:
 if self.trace:
! print (" --- modulename: %s, funcname: %s"
! % (modulename, funcname))
 return self.localtrace
 else:
***************
*** 501,517 ****
 if why == 'line':
 # record the file name and line number of every trace
! # XXX I wish inspect offered me an optimized `getfilename(frame)' to use in place of the presumably heavier `getframeinfo()'. --Zooko 2001年10月14日
! (filename, lineno, funcname, context, lineindex,) = inspect.getframeinfo(frame, 1)
 key = (filename, lineno,)
 self.counts[key] = self.counts.get(key, 0) + 1
! # XXX not convinced that this memoizing is a performance win -- I don't know enough about Python guts to tell. --Zooko 2001年10月14日
 bname = self.pathtobasename.get(filename)
 if bname is None:
! # Using setdefault faster than two separate lines? --Zooko 2001年10月14日
! bname = self.pathtobasename.setdefault(filename, os.path.basename(filename))
 try:
! print "%s(%d): %s" % (bname, lineno, context[lineindex],),
 except IndexError:
! # Uh.. sometimes getframeinfo gives me a context of length 1 and a lineindex of -2. Oh well.
 pass
 return self.localtrace
--- 501,529 ----
 if why == 'line':
 # record the file name and line number of every trace
! # XXX I wish inspect offered me an optimized
! # `getfilename(frame)' to use in place of the presumably
! # heavier `getframeinfo()'. --Zooko 2001年10月14日
! 
! filename, lineno, funcname, context, lineindex = \
! inspect.getframeinfo(frame, 1)
 key = (filename, lineno,)
 self.counts[key] = self.counts.get(key, 0) + 1
! 
! # XXX not convinced that this memoizing is a performance
! # win -- I don't know enough about Python guts to tell.
! # --Zooko 2001年10月14日
! 
 bname = self.pathtobasename.get(filename)
 if bname is None:
! 
! # Using setdefault faster than two separate lines?
! # --Zooko 2001年10月14日
! bname = self.pathtobasename.setdefault(filename,
! os.path.basename(filename))
 try:
! print "%s(%d): %s" % (bname, lineno, context[lineindex]),
 except IndexError:
! # Uh.. sometimes getframeinfo gives me a context of
! # length 1 and a lineindex of -2. Oh well.
 pass
 return self.localtrace
***************
*** 519,536 ****
 def localtrace_trace(self, frame, why, arg):
 if why == 'line':
! # XXX shouldn't do the count increment when arg is exception? But be careful to return self.localtrace when arg is exception! ? --Zooko 2001年10月14日
! # record the file name and line number of every trace
! # XXX I wish inspect offered me an optimized `getfilename(frame)' to use in place of the presumably heavier `getframeinfo()'. --Zooko 2001年10月14日
! (filename, lineno, funcname, context, lineindex,) = inspect.getframeinfo(frame)
! # if DEBUG_MODE:
! # print "%s.localtrace_trace(frame: %s, why: %s, arg: %s); filename: %s, lineno: %s, funcname: %s, context: %s, lineindex: %s\n" % (self, frame, why, arg, filename, lineno, funcname, context, lineindex,)
! # XXX not convinced that this memoizing is a performance win -- I don't know enough about Python guts to tell. --Zooko 2001年10月14日
 bname = self.pathtobasename.get(filename)
 if bname is None:
! # Using setdefault faster than two separate lines? --Zooko 2001年10月14日
 bname = self.pathtobasename.setdefault(filename, os.path.basename(filename))
 if context is not None:
 try:
! print "%s(%d): %s" % (bname, lineno, context[lineindex],),
 except IndexError:
 # Uh.. sometimes getframeinfo gives me a context of length 1 and a lineindex of -2. Oh well.
--- 531,556 ----
 def localtrace_trace(self, frame, why, arg):
 if why == 'line':
! # XXX shouldn't do the count increment when arg is
! # exception? But be careful to return self.localtrace
! # when arg is exception! ? --Zooko 2001年10月14日
! 
! # record the file name and line number of every trace XXX
! # I wish inspect offered me an optimized
! # `getfilename(frame)' to use in place of the presumably
! # heavier `getframeinfo()'. --Zooko 2001年10月14日
! filename, lineno, funcname, context, lineindex = \
! inspect.getframeinfo(frame)
! 
! # XXX not convinced that this memoizing is a performance
! # win -- I don't know enough about Python guts to tell.
! # --Zooko 2001年10月14日
 bname = self.pathtobasename.get(filename)
 if bname is None:
! # Using setdefault faster than two separate lines?
! # --Zooko 2001年10月14日
 bname = self.pathtobasename.setdefault(filename, os.path.basename(filename))
 if context is not None:
 try:
! print "%s(%d): %s" % (bname, lineno, context[lineindex]),
 except IndexError:
 # Uh.. sometimes getframeinfo gives me a context of length 1 and a lineindex of -2. Oh well.
***************
*** 542,555 ****
 def localtrace_count(self, frame, why, arg):
 if why == 'line':
! # XXX shouldn't do the count increment when arg is exception? But be careful to return self.localtrace when arg is exception! ? --Zooko 2001年10月14日
! # record the file name and line number of every trace
! # XXX I wish inspect offered me an optimized `getfilename(frame)' to use in place of the presumably heavier `getframeinfo()'. --Zooko 2001年10月14日
! (filename, lineno, funcname, context, lineindex,) = inspect.getframeinfo(frame)
! key = (filename, lineno,)
 self.counts[key] = self.counts.get(key, 0) + 1
 return self.localtrace
 
 def results(self):
! return CoverageResults(self.counts, infile=self.infile, outfile=self.outfile, calledfuncs=self._calledfuncs)
 
 def _err_exit(msg):
--- 562,575 ----
 def localtrace_count(self, frame, why, arg):
 if why == 'line':
! filename = frame.f_code.co_filename
! lineno = frame.f_lineno
! key = filename, lineno
 self.counts[key] = self.counts.get(key, 0) + 1
 return self.localtrace
 
 def results(self):
! return CoverageResults(self.counts, infile=self.infile,
! outfile=self.outfile,
! calledfuncs=self._calledfuncs)
 
 def _err_exit(msg):
***************
*** 572,576 ****
 except getopt.error, msg:
 sys.stderr.write("%s: %s\n" % (sys.argv[0], msg))
! sys.stderr.write("Try `%s --help' for more information\n" % sys.argv[0])
 sys.exit(1)
 
--- 592,597 ----
 except getopt.error, msg:
 sys.stderr.write("%s: %s\n" % (sys.argv[0], msg))
! sys.stderr.write("Try `%s --help' for more information\n"
! % sys.argv[0])
 sys.exit(1)
 
***************
*** 675,682 ****
 sys.argv = prog_argv
 progname = prog_argv[0]
! if eval(sys.version[:3])>1.3:
! sys.path[0] = os.path.split(progname)[0] # ???
 
! t = Trace(count, trace, countfuncs=listfuncs, ignoremods=ignore_modules, ignoredirs=ignore_dirs, infile=counts_file, outfile=counts_file)
 try:
 t.run('execfile(' + `progname` + ')')
--- 696,704 ----
 sys.argv = prog_argv
 progname = prog_argv[0]
! sys.path[0] = os.path.split(progname)[0]
 
! t = Trace(count, trace, countfuncs=listfuncs,
! ignoremods=ignore_modules, ignoredirs=ignore_dirs,
! infile=counts_file, outfile=counts_file)
 try:
 t.run('execfile(' + `progname` + ')')

AltStyle によって変換されたページ (->オリジナル) /