[Python-checkins] python/dist/src/Lib dis.py,1.46,1.47

arigo at users.sourceforge.net arigo at users.sourceforge.net
Tue Oct 28 07:17:27 EST 2003


Update of /cvsroot/python/python/dist/src/Lib
In directory sc8-pr-cvs1:/tmp/cvs-serv32563/Lib

Modified Files:
	dis.py 
Log Message:
Fixed dis.disassemble_string().
Added dis.findlinestarts().
SF bug 811294


Index: dis.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/dis.py,v
retrieving revision 1.46
retrieving revision 1.47
diff -C2 -d -r1.46 -r1.47
*** dis.py	7 Mar 2003 17:30:45 -0000	1.46
--- dis.py	28 Oct 2003 12:17:25 -0000	1.47
***************
*** 61,79 ****
      """Disassemble a code object."""
      code = co.co_code
- 
-     byte_increments = [ord(c) for c in co.co_lnotab[0::2]]
-     line_increments = [ord(c) for c in co.co_lnotab[1::2]]
-     table_length = len(byte_increments) # == len(line_increments)
- 
-     lineno = co.co_firstlineno
-     table_index = 0
-     while (table_index < table_length
-            and byte_increments[table_index] == 0):
-         lineno += line_increments[table_index]
-         table_index += 1
-     addr = 0
-     line_incr = 0
- 
      labels = findlabels(code)
      n = len(code)
      i = 0
--- 61,66 ----
      """Disassemble a code object."""
      code = co.co_code
      labels = findlabels(code)
+     linestarts = dict(findlinestarts(co))
      n = len(code)
      i = 0
***************
*** 83,100 ****
          c = code[i]
          op = ord(c)
! 
!         if i >= addr:
!             lineno += line_incr
!             while table_index < table_length:
!                 addr += byte_increments[table_index]
!                 line_incr = line_increments[table_index]
!                 table_index += 1
!                 if line_incr:
!                     break
!             else:
!                 addr = sys.maxint
              if i > 0:
                  print
!             print "%3d"%lineno,
          else:
              print '   ',
--- 70,77 ----
          c = code[i]
          op = ord(c)
!         if i in linestarts:
              if i > 0:
                  print
!             print "%3d" % linestarts[i],
          else:
              print '   ',
***************
*** 138,143 ****
          c = code[i]
          op = ord(c)
-         if op == opmap['SET_LINENO'] and i > 0:
-             print # Extra blank line
          if i == lasti: print '-->',
          else: print '   ',
--- 115,118 ----
***************
*** 200,203 ****
--- 175,199 ----
      return labels
  
+ def findlinestarts(code):
+     """Find the offsets in a byte code which are start of lines in the source.
+ 
+     Generate pairs (offset, lineno) as described in Python/compile.c.
+ 
+     """
+     byte_increments = [ord(c) for c in code.co_lnotab[0::2]]
+     line_increments = [ord(c) for c in code.co_lnotab[1::2]]
+ 
+     lastlineno = None
+     lineno = code.co_firstlineno
+     addr = 0
+     for byte_incr, line_incr in zip(byte_increments, line_increments):
+         if byte_incr:
+             if lineno != lastlineno:
+                 yield (addr, lineno)
+                 lastlineno = lineno
+             addr += byte_incr
+         lineno += line_incr
+     if lineno != lastlineno:
+         yield (addr, lineno)
  
  def _test():





More information about the Python-checkins mailing list