annotate Lib/lib-old/codehack.py @ 723:e7571a18bdef legacy-trunk

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