Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 1 | # A subroutine for extracting a function name from a code object |
| 2 | # (with cache) |
| 3 | |
| 4 | import sys |
| 5 | from stat import * |
| 6 | import string |
| 7 | import os |
| 8 | import 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 | |
| 27 | SET_LINENO = 127 # The opcode (see "opcode.h" in the Python source) |
| 28 | identchars = string.letters + string.digits + '_' # Identifier characters |
| 29 | |
| 30 | _namecache = {} # The cache |
| 31 | |
| 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 |
| 50 | |
| 51 | # Use the above routine to find a function's name. |
| 52 | |
| 53 | def getfuncname(func): |
| 54 | return getcodename(func.func_code) |
Guido van Rossum | cb7ce34 | 1992-03-27 15:13:08 +0000 | [diff] [blame] | 55 | |
| 56 | # A part of the above code to extract just the line number from a code object. |
| 57 | |
| 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 |