bcode.py

Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 # This file is Copyright 2003, 2006, 2007, 2009, 2010 Dean Hall.
00004 #
00005 # This file is part of the Python-on-a-Chip program.
00006 # Python-on-a-Chip is free software: you can redistribute it and/or modify
00007 # it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE Version 2.1.
00008 # 
00009 # Python-on-a-Chip is distributed in the hope that it will be useful,
00010 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00012 # A copy of the GNU LESSER GENERAL PUBLIC LICENSE Version 2.1
00013 # is seen in the file COPYING in this directory.
00014 
00015 """
00016 Python Byte Code utility functions.
00017 
00018 count(fn) - count the number of times each byte code
00019             appears in a code object.
00020 """
00021 
00022 ## @file
00023 #  @copybrief bcode
00024 
00025 ## @package bcode
00026 #  @brief Python Byte Code utility functions.
00027 #
00028 #  count(fn) - count the number of times each byte code
00029 #              appears in a code object.
00030 
00031 
00032 
00033 import dis, types
00034 
00035 
00036 #The highest numbered byte code
00037 MAX_BCODE = 150
00038 
00039 
00040 def count(fn):
00041     """
00042     Compile the python source file named fn.
00043     Count all the bytecodes in the file.
00044     Return a list of counts indexed by bcode.
00045     """
00046 
00047     #create a code object
00048     src = open(fn).read()
00049     co  = compile(src, fn, "exec")
00050 
00051     #init list
00052     cnt = [0,] * MAX_BCODE
00053 
00054     #iterate through the nested code objects
00055     _rcount(co, cnt)
00056 
00057     return cnt
00058 
00059 
00060 def _rcount(co, count):
00061     """
00062     Recursively descend through all the code objects
00063     in the given code object, co.
00064     Add byte code counts to the count dict.
00065     """
00066     #current byte code string
00067     str = co.co_code
00068     strlen = len(str)
00069 
00070     #count byte codes in current string
00071     i = 0
00072     while i < strlen:
00073         bcode = ord(str[i])
00074         i += 1
00075         #incr count
00076         count[bcode] += 1
00077 
00078         #if bcode has arg, incr ptr by 2 bytes
00079         if bcode >= 90:
00080             i += 2
00081 
00082     #count code objects in constant pool
00083     for obj in co.co_consts:
00084         if type(obj) == types.CodeType:
00085             _rcount(obj, count)
00086 
00087     return
00088 
00089 
00090 def main(fn):
00091     """
00092     Count the bytecodes in the file, fn,
00093     and print them out in human-readable form.
00094     """
00095     #count the bytecodes in this file
00096     c = count(fn)
00097 
00098     #print bcode name and count
00099     for i in range(len(c)):
00100         if c[i]:
00101             print dis.opname[i], ":\t", c[i]
00102     return
00103 
00104 
00105 if __name__ == "__main__":
00106     main("c:\\dwh\\tech\\cis\\py\\snap.py")

Generated on Mon Oct 18 07:40:47 2010 for Python-on-a-chip by  doxygen 1.5.9