annotate Lib/codehack.py @ 1746:1940892d2fe3 legacy-trunk

Merge alpha100 branch back to main trunk
author Guido van Rossum <guido@python.org>
date Mon, 01 Aug 1994 11:34:53 +0000
parents e7571a18bdef
children 91aea1af654f
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.
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 723
diff changeset
13
622
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 # Extract the function or class name from a code object.
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 # 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
16 # the name directly. So what do we do:
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 # - get the filename (which *is* in the code object)
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 # - look in the code string to find the first SET_LINENO instruction
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 # (this must be the first instruction)
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 # - get the line from the file
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 # - if the line starts with 'class' or 'def' (after possible whitespace),
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 # extract the following identifier
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 #
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 # This breaks apart when the function was read from <stdin>
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 # or constructed by exec(), when the file is not accessible,
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 # 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
27 # continued with a backslash before the function or class name.
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 #
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 # Because this is a pretty expensive hack, a cache is kept.
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 SET_LINENO = 127 # The opcode (see "opcode.h" in the Python source)
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 identchars = string.letters + string.digits + '_' # Identifier characters
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 _namecache = {} # The cache
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 def getcodename(co):
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 key = `co` # arbitrary but uniquely identifying string
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 if _namecache.has_key(key): return _namecache[key]
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 filename = co.co_filename
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 code = co.co_code
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 name = ''
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 if ord(code[0]) == SET_LINENO:
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 lineno = ord(code[1]) | ord(code[2]) << 8
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 line = linecache.getline(filename, lineno)
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 words = string.split(line)
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 if len(words) >= 2 and words[0] in ('def', 'class'):
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 name = words[1]
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 for i in range(len(name)):
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 if name[i] not in identchars:
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 name = name[:i]
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 break
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 _namecache[key] = name
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 return name
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
54
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 # Use the above routine to find a function's name.
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
56
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 def getfuncname(func):
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 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
59
e7571a18bdef Add a function to return just the line number of a code object.
Guido van Rossum <guido@python.org>
parents: 622
diff changeset
60 # 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
61
e7571a18bdef Add a function to return just the line number of a code object.
Guido van Rossum <guido@python.org>
parents: 622
diff changeset
62 def getlineno(co):
e7571a18bdef Add a function to return just the line number of a code object.
Guido van Rossum <guido@python.org>
parents: 622
diff changeset
63 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
64 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
65 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
66 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
67 return -1