1 # A subroutine for extracting a function name from a code object
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
33 key
= `co`
# arbitrary but uniquely identifying string
34 if _namecache
.has_key(key
): return _namecache
[key
]
35 filename
= co
.co_filename
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'):
44 for i
in range(len(name
)):
45 if name
[i
] not in identchars
:
48 _namecache
[key
] = 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.
60 if ord(code
[0]) == SET_LINENO
:
61 return ord(code
[1]) |
ord(code
[2]) << 8