annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
622
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # A subroutine for extracting a function name from a code object
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 # (with cache)
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 import sys
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 from stat import *
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 import string
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 import os
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 import linecache
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
9
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 723
diff changeset
10 # XXX The functions getcodename() and getfuncname() are now obsolete
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 723
diff changeset
11 # XXX as code and function objects now have a name attribute --
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 723
diff changeset
12 # XXX co.co_name and f.func_name.
5834
91aea1af654f This module is now completely obsolete.
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
13 # XXX getlineno() is now also obsolete because of the new attribute
91aea1af654f This module is now completely obsolete.
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
14 # XXX of code objects, co.co_firstlineno.
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 723
diff changeset
15
622
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 # Extract the function or class name from a code object.
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 # This is a bit of a hack, since a code object doesn't contain
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 # the name directly. So what do we do:
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 # - get the filename (which *is* in the code object)
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 # - look in the code string to find the first SET_LINENO instruction
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 # (this must be the first instruction)
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 # - get the line from the file
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 # - if the line starts with 'class' or 'def' (after possible whitespace),
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 # extract the following identifier
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 #
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 # This breaks apart when the function was read from <stdin>
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 # or constructed by exec(), when the file is not accessible,
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 # and also when the file has been modified or when a line is
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 # continued with a backslash before the function or class name.
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 #
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 # Because this is a pretty expensive hack, a cache is kept.
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
32
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 SET_LINENO = 127 # The opcode (see "opcode.h" in the Python source)
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 identchars = string.letters + string.digits + '_' # Identifier characters
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
35
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 _namecache = {} # The cache
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
37
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 def getcodename(co):
5834
91aea1af654f This module is now completely obsolete.
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
39 try:
91aea1af654f This module is now completely obsolete.
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
40 return co.co_name
91aea1af654f This module is now completely obsolete.
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
41 except AttributeError:
91aea1af654f This module is now completely obsolete.
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
42 pass
622
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 key = `co` # arbitrary but uniquely identifying string
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 if _namecache.has_key(key): return _namecache[key]
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 filename = co.co_filename
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 code = co.co_code
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 name = ''
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 if ord(code[0]) == SET_LINENO:
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 lineno = ord(code[1]) | ord(code[2]) << 8
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 line = linecache.getline(filename, lineno)
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 words = string.split(line)
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 if len(words) >= 2 and words[0] in ('def', 'class'):
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 name = words[1]
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 for i in range(len(name)):
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 if name[i] not in identchars:
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 name = name[:i]
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 break
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 _namecache[key] = name
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 return name
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
60
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 # Use the above routine to find a function's name.
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
62
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 def getfuncname(func):
5834
91aea1af654f This module is now completely obsolete.
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
64 try:
91aea1af654f This module is now completely obsolete.
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
65 return func.func_name
91aea1af654f This module is now completely obsolete.
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
66 except AttributeError:
91aea1af654f This module is now completely obsolete.
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
67 pass
622
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 return getcodename(func.func_code)
723
e7571a18bdef Add a function to return just the line number of a code object.
Guido van Rossum <guido@python.org>
parents: 622
diff changeset
69
e7571a18bdef Add a function to return just the line number of a code object.
Guido van Rossum <guido@python.org>
parents: 622
diff changeset
70 # A part of the above code to extract just the line number from a code object.
e7571a18bdef Add a function to return just the line number of a code object.
Guido van Rossum <guido@python.org>
parents: 622
diff changeset
71
e7571a18bdef Add a function to return just the line number of a code object.
Guido van Rossum <guido@python.org>
parents: 622
diff changeset
72 def getlineno(co):
5834
91aea1af654f This module is now completely obsolete.
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
73 try:
91aea1af654f This module is now completely obsolete.
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
74 return co.co_firstlineno
91aea1af654f This module is now completely obsolete.
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
75 except AttributeError:
91aea1af654f This module is now completely obsolete.
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
76 pass
723
e7571a18bdef Add a function to return just the line number of a code object.
Guido van Rossum <guido@python.org>
parents: 622
diff changeset
77 code = co.co_code
e7571a18bdef Add a function to return just the line number of a code object.
Guido van Rossum <guido@python.org>
parents: 622
diff changeset
78 if ord(code[0]) == SET_LINENO:
e7571a18bdef Add a function to return just the line number of a code object.
Guido van Rossum <guido@python.org>
parents: 622
diff changeset
79 return ord(code[1]) | ord(code[2]) << 8
e7571a18bdef Add a function to return just the line number of a code object.
Guido van Rossum <guido@python.org>
parents: 622
diff changeset
80 else:
e7571a18bdef Add a function to return just the line number of a code object.
Guido van Rossum <guido@python.org>
parents: 622
diff changeset
81 return -1