blob: 8a4f611696ded8280e5a189ac5b96fba43b89cc9 [file] [log] [blame]
Guido van Rossumbabe2bf1992-01-22 22:21:31 +00001# A subroutine for extracting a function name from a code object
2# (with cache)
3
4import sys
5from stat import *
6import string
7import os
8import linecache
9
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
19#
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.
24#
25# Because this is a pretty expensive hack, a cache is kept.
26
27SET_LINENO = 127 # The opcode (see "opcode.h" in the Python source)
28identchars = string.letters + string.digits + '_' # Identifier characters
29
30_namecache = {} # The cache
31
32def 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
50
51# Use the above routine to find a function's name.
52
53def getfuncname(func):
54 return getcodename(func.func_code)