annotate Lib/pyclbr.py @ 4566:6403c94de50d legacy-trunk

Added safeguard against failure in __del__.
author Guido van Rossum <guido@python.org>
date Wed, 27 Nov 1996 19:39:58 +0000
parents 5c86f60539f7
children e9c67c192cdb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2867
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1 '''Parse a Python file and retrieve classes and methods.
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
2
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
3 Parse enough of a Python file to recognize class and method
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
4 definitions and to find out the superclasses of a class.
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
5
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
6 The interface consists of a single function:
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
7 readmodule(module, path)
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
8 module is the name of a Python module, path is an optional list of
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
9 directories where the module is to be searched. If present, path is
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
10 prepended to the system search path sys.path.
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
11 The return value is a dictionary. The keys of the dictionary are
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
12 the names of the classes defined in the module (including classes
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
13 that are defined via the from XXX import YYY construct). The values
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
14 are class instances of the class Class defined here.
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
15
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
16 A class is described by the class Class in this module. Instances
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
17 of this class have the following instance variables:
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
18 name -- the name of the class
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
19 super -- a list of super classes (Class instances)
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
20 methods -- a dictionary of methods
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
21 file -- the file in which the class was defined
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
22 lineno -- the line in the file on which the class statement occurred
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
23 The dictionary of methods uses the method names as keys and the line
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
24 numbers on which the method was defined as values.
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
25 If the name of a super class is not recognized, the corresponding
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
26 entry in the list of super classes is not a class instance but a
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
27 string giving the name of the super class. Since import statements
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
28 are recognized and imported modules are scanned as well, this
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
29 shouldn't happen often.
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
30
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
31 BUGS
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
32 Continuation lines are not dealt with at all and strings may confuse
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
33 the hell out of the parser, but it usually works.'''
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
34
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
35 import os
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
36 import sys
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
37 import imp
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
38 import regex
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
39 import string
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
40
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
41 id = '\\(<id>[A-Za-z_][A-Za-z0-9_]*\\)' # match identifier
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
42 blank_line = regex.compile('^[ \t]*\\($\\|#\\)')
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
43 is_class = regex.symcomp('^class[ \t]+'+id+'[ \t]*\\(<sup>([^)]*)\\)?[ \t]*:')
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
44 is_method = regex.symcomp('^[ \t]+def[ \t]+'+id+'[ \t]*(')
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
45 is_import = regex.symcomp('^import[ \t]*\\(<imp>[^#]+\\)')
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
46 is_from = regex.symcomp('^from[ \t]+'+id+'[ \t]+import[ \t]+\\(<imp>[^#]+\\)')
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
47 dedent = regex.compile('^[^ \t]')
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
48 indent = regex.compile('^[^ \t]*')
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
49
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
50 _modules = {} # cache of modules we've seen
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
51
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
52 # each Python class is represented by an instance of this class
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
53 class Class:
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
54 '''Class to represent a Python class.'''
3242
313637c77daa Also remember the module a class is defined in.
Sjoerd Mullender <sjoerd@acm.org>
parents: 2867
diff changeset
55 def __init__(self, module, name, super, file, lineno):
313637c77daa Also remember the module a class is defined in.
Sjoerd Mullender <sjoerd@acm.org>
parents: 2867
diff changeset
56 self.module = module
2867
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
57 self.name = name
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
58 if super is None:
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
59 super = []
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
60 self.super = super
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
61 self.methods = {}
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
62 self.file = file
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
63 self.lineno = lineno
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
64
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
65 def _addmethod(self, name, lineno):
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
66 self.methods[name] = lineno
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
67
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
68 def readmodule(module, path = []):
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
69 '''Read a module file and return a dictionary of classes.
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
70
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
71 Search for MODULE in PATH and sys.path, read and parse the
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
72 module and return a dictionary with one entry for each class
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
73 found in the module.'''
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
74
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
75 if _modules.has_key(module):
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
76 # we've seen this module before...
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
77 return _modules[module]
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
78 if module in sys.builtin_module_names:
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
79 # this is a built-in module
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
80 dict = {}
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
81 _modules[module] = dict
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
82 return dict
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
83
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
84 # search the path for the module
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
85 f = None
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
86 suffixes = imp.get_suffixes()
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
87 for dir in path + sys.path:
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
88 for suff, mode, type in suffixes:
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
89 file = os.path.join(dir, module + suff)
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
90 try:
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
91 f = open(file, mode)
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
92 except IOError:
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
93 pass
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
94 else:
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
95 # found the module
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
96 break
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
97 if f:
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
98 break
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
99 if not f:
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
100 raise IOError, 'module ' + module + ' not found'
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
101 if type != imp.PY_SOURCE:
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
102 # not Python source, can't do anything with this module
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
103 f.close()
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
104 dict = {}
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
105 _modules[module] = dict
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
106 return dict
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
107
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
108 cur_class = None
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
109 dict = {}
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
110 _modules[module] = dict
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
111 imports = []
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
112 lineno = 0
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
113 while 1:
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
114 line = f.readline()
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
115 if not line:
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
116 break
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
117 lineno = lineno + 1 # count lines
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
118 line = line[:-1] # remove line feed
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
119 if blank_line.match(line) >= 0:
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
120 # ignore blank (and comment only) lines
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
121 continue
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
122 ## if indent.match(line) >= 0:
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
123 ## indentation = len(string.expandtabs(indent.group(0), 8))
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
124 if is_import.match(line) >= 0:
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
125 # import module
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
126 for n in string.splitfields(is_import.group('imp'), ','):
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
127 n = string.strip(n)
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
128 try:
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
129 # recursively read the
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
130 # imported module
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
131 d = readmodule(n, path)
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
132 except:
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
133 print 'module',n,'not found'
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
134 pass
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
135 continue
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
136 if is_from.match(line) >= 0:
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
137 # from module import stuff
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
138 mod = is_from.group('id')
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
139 names = string.splitfields(is_from.group('imp'), ',')
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
140 try:
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
141 # recursively read the imported module
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
142 d = readmodule(mod, path)
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
143 except:
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
144 print 'module',mod,'not found'
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
145 continue
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
146 # add any classes that were defined in the
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
147 # imported module to our name space if they
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
148 # were mentioned in the list
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
149 for n in names:
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
150 n = string.strip(n)
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
151 if d.has_key(n):
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
152 dict[n] = d[n]
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
153 elif n == '*':
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
154 # only add a name if not
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
155 # already there (to mimic what
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
156 # Python does internally)
4394
5c86f60539f7 Don't add names that start with _
Guido van Rossum <guido@python.org>
parents: 3242
diff changeset
157 # also don't add names that
5c86f60539f7 Don't add names that start with _
Guido van Rossum <guido@python.org>
parents: 3242
diff changeset
158 # start with _
2867
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
159 for n in d.keys():
4394
5c86f60539f7 Don't add names that start with _
Guido van Rossum <guido@python.org>
parents: 3242
diff changeset
160 if n[0] != '_' and \
5c86f60539f7 Don't add names that start with _
Guido van Rossum <guido@python.org>
parents: 3242
diff changeset
161 not dict.has_key(n):
2867
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
162 dict[n] = d[n]
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
163 continue
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
164 if is_class.match(line) >= 0:
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
165 # we found a class definition
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
166 class_name = is_class.group('id')
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
167 inherit = is_class.group('sup')
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
168 if inherit:
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
169 # the class inherits from other classes
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
170 inherit = string.strip(inherit[1:-1])
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
171 names = []
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
172 for n in string.splitfields(inherit, ','):
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
173 n = string.strip(n)
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
174 if dict.has_key(n):
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
175 # we know this super class
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
176 n = dict[n]
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
177 else:
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
178 c = string.splitfields(n, '.')
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
179 if len(c) > 1:
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
180 # super class
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
181 # is of the
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
182 # form module.class:
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
183 # look in
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
184 # module for class
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
185 m = c[-2]
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
186 c = c[-1]
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
187 if _modules.has_key(m):
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
188 d = _modules[m]
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
189 if d.has_key(c):
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
190 n = d[c]
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
191 names.append(n)
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
192 inherit = names
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
193 # remember this class
3242
313637c77daa Also remember the module a class is defined in.
Sjoerd Mullender <sjoerd@acm.org>
parents: 2867
diff changeset
194 cur_class = Class(module, class_name, inherit, file, lineno)
2867
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
195 dict[class_name] = cur_class
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
196 continue
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
197 if is_method.match(line) >= 0:
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
198 # found a method definition
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
199 if cur_class:
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
200 # and we know the class it belongs to
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
201 meth_name = is_method.group('id')
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
202 cur_class._addmethod(meth_name, lineno)
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
203 continue
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
204 if dedent.match(line) >= 0:
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
205 # end of class definition
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
206 cur_class = None
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
207 f.close()
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
208 return dict