comparison Lib/codehack.py @ 5834:91aea1af654f legacy-trunk

This module is now completely obsolete. Noted this in the XXX comments. Also, changed all three functions to use the attributes if they exist.
author Guido van Rossum <guido@python.org>
date Fri, 18 Jul 1997 16:48:30 +0000
parents 1940892d2fe3
children
comparison
equal deleted inserted replaced
5833:86c279a848eb 5834:91aea1af654f
8 import linecache 8 import linecache
9 9
10 # XXX The functions getcodename() and getfuncname() are now obsolete 10 # XXX The functions getcodename() and getfuncname() are now obsolete
11 # XXX as code and function objects now have a name attribute -- 11 # XXX as code and function objects now have a name attribute --
12 # XXX co.co_name and f.func_name. 12 # XXX co.co_name and f.func_name.
13 # XXX getlineno() is now also obsolete because of the new attribute
14 # XXX of code objects, co.co_firstlineno.
13 15
14 # Extract the function or class name from a code object. 16 # Extract the function or class name from a code object.
15 # This is a bit of a hack, since a code object doesn't contain 17 # This is a bit of a hack, since a code object doesn't contain
16 # the name directly. So what do we do: 18 # the name directly. So what do we do:
17 # - get the filename (which *is* in the code object) 19 # - get the filename (which *is* in the code object)
32 identchars = string.letters + string.digits + '_' # Identifier characters 34 identchars = string.letters + string.digits + '_' # Identifier characters
33 35
34 _namecache = {} # The cache 36 _namecache = {} # The cache
35 37
36 def getcodename(co): 38 def getcodename(co):
39 try:
40 return co.co_name
41 except AttributeError:
42 pass
37 key = `co` # arbitrary but uniquely identifying string 43 key = `co` # arbitrary but uniquely identifying string
38 if _namecache.has_key(key): return _namecache[key] 44 if _namecache.has_key(key): return _namecache[key]
39 filename = co.co_filename 45 filename = co.co_filename
40 code = co.co_code 46 code = co.co_code
41 name = '' 47 name = ''
53 return name 59 return name
54 60
55 # Use the above routine to find a function's name. 61 # Use the above routine to find a function's name.
56 62
57 def getfuncname(func): 63 def getfuncname(func):
64 try:
65 return func.func_name
66 except AttributeError:
67 pass
58 return getcodename(func.func_code) 68 return getcodename(func.func_code)
59 69
60 # A part of the above code to extract just the line number from a code object. 70 # A part of the above code to extract just the line number from a code object.
61 71
62 def getlineno(co): 72 def getlineno(co):
73 try:
74 return co.co_firstlineno
75 except AttributeError:
76 pass
63 code = co.co_code 77 code = co.co_code
64 if ord(code[0]) == SET_LINENO: 78 if ord(code[0]) == SET_LINENO:
65 return ord(code[1]) | ord(code[2]) << 8 79 return ord(code[1]) | ord(code[2]) << 8
66 else: 80 else:
67 return -1 81 return -1