[Python-checkins] python/dist/src/Tools/scripts trace.py,1.7,1.8

mwh@users.sourceforge.net mwh@users.sourceforge.net
Thu, 15 Aug 2002 07:59:04 -0700


Update of /cvsroot/python/python/dist/src/Tools/scripts
In directory usw-pr-cvs1:/tmp/cvs-serv10908/Tools/scripts

Modified Files:
	trace.py 
Log Message:
This is my patch

[ 587993 ] SET_LINENO killer

Remove SET_LINENO.  Tracing is now supported by inspecting co_lnotab.

Many sundry changes to document and adapt to this change.


Index: trace.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Tools/scripts/trace.py,v
retrieving revision 1.7
retrieving revision 1.8
diff -C2 -d -r1.7 -r1.8
*** trace.py	25 Jul 2002 16:09:35 -0000	1.7
--- trace.py	15 Aug 2002 14:59:02 -0000	1.8
***************
*** 371,409 ****
                  sys.stderr.write("cannot save counts files because %s" % err)
  
! # Given a code string, return the SET_LINENO information
! def _find_LINENO_from_string(co_code):
!     """return all of the SET_LINENO information from a code string"""
!     import dis
      linenos = {}
  
!     # This code was filched from the `dis' module then modified
!     n = len(co_code)
!     i = 0
!     prev_op = None
!     prev_lineno = 0
!     while i < n:
!         c = co_code[i]
!         op = ord(c)
!         if op == dis.SET_LINENO:
!             if prev_op == op:
!                 # two SET_LINENO in a row, so the previous didn't
!                 # indicate anything.  This occurs with triple
!                 # quoted strings (?).  Remove the old one.
!                 del linenos[prev_lineno]
!             prev_lineno = ord(co_code[i+1]) + ord(co_code[i+2])*256
!             linenos[prev_lineno] = 1
!         if op >= dis.HAVE_ARGUMENT:
!             i = i + 3
!         else:
!             i = i + 1
!         prev_op = op
      return linenos
  
  def _find_LINENO(code):
!     """return all of the SET_LINENO information from a code object"""
      import types
  
      # get all of the lineno information from the code of this scope level
!     linenos = _find_LINENO_from_string(code.co_code)
  
      # and check the constants for references to other code objects
--- 371,397 ----
                  sys.stderr.write("cannot save counts files because %s" % err)
  
! def _find_LINENO_from_code(code):
!     """return the numbers of the lines containing the source code that
!     was compiled into code"""
      linenos = {}
  
!     line_increments = [ord(c) for c in code.co_lnotab[1::2]]
!     table_length = len(line_increments)
! 
!     lineno = code.co_first_lineno
! 
!     for li in line_increments:
!         linenos[lineno] = 1
!         lineno += li
!     linenos[lineno] = 1
! 
      return linenos
  
  def _find_LINENO(code):
!     """return all of the lineno information from a code object"""
      import types
  
      # get all of the lineno information from the code of this scope level
!     linenos = _find_LINENO_from_code(code)
  
      # and check the constants for references to other code objects
***************
*** 417,423 ****
      """return a dict of the line numbers from executable statements in a file
  
-     Works by finding all of the code-like objects in the module then searching
-     the byte code for 'SET_LINENO' terms (so this won't work one -O files).
- 
      """
      import parser
--- 405,408 ----
***************
*** 428,435 ****
      ast = parser.suite(prog)
      code = parser.compileast(ast, filename)
- 
-     # The only way I know to find line numbers is to look for the
-     # SET_LINENO instructions.  Isn't there some way to get it from
-     # the AST?
  
      return _find_LINENO(code)
--- 413,416 ----