At the release of 1.0.1.
[python/dscho.git] / Lib / codehack.py
blobd00d2bfec9ce2417bd9e42279c4e0ec830f88663
1 # A subroutine for extracting a function name from a code object
2 # (with cache)
4 import sys
5 from stat import *
6 import string
7 import os
8 import linecache
10 # Extract the function or class name from a code object.
11 # This is a bit of a hack, since a code object doesn't contain
12 # the name directly. So what do we do:
13 # - get the filename (which *is* in the code object)
14 # - look in the code string to find the first SET_LINENO instruction
15 # (this must be the first instruction)
16 # - get the line from the file
17 # - if the line starts with 'class' or 'def' (after possible whitespace),
18 # extract the following identifier
20 # This breaks apart when the function was read from <stdin>
21 # or constructed by exec(), when the file is not accessible,
22 # and also when the file has been modified or when a line is
23 # continued with a backslash before the function or class name.
25 # Because this is a pretty expensive hack, a cache is kept.
27 SET_LINENO = 127 # The opcode (see "opcode.h" in the Python source)
28 identchars = string.letters + string.digits + '_' # Identifier characters
30 _namecache = {} # The cache
32 def getcodename(co):
33 key = `co` # arbitrary but uniquely identifying string
34 if _namecache.has_key(key): return _namecache[key]
35 filename = co.co_filename
36 code = co.co_code
37 name = ''
38 if ord(code[0]) == SET_LINENO:
39 lineno = ord(code[1]) | ord(code[2]) << 8
40 line = linecache.getline(filename, lineno)
41 words = string.split(line)
42 if len(words) >= 2 and words[0] in ('def', 'class'):
43 name = words[1]
44 for i in range(len(name)):
45 if name[i] not in identchars:
46 name = name[:i]
47 break
48 _namecache[key] = name
49 return name
51 # Use the above routine to find a function's name.
53 def getfuncname(func):
54 return getcodename(func.func_code)
56 # A part of the above code to extract just the line number from a code object.
58 def getlineno(co):
59 code = co.co_code
60 if ord(code[0]) == SET_LINENO:
61 return ord(code[1]) | ord(code[2]) << 8
62 else:
63 return -1