annotate Lib/pyclbr.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents 2d2079593212
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
26507
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
1 """Parse a Python module and describe its classes and methods.
2867
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
2
26507
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
3 Parse enough of a Python file to recognize imports and class and
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
4 method definitions, and to find out the superclasses of a class.
2867
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:
26507
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
7 readmodule_ex(module [, path])
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
8 where module is the name of a Python module, and path is an optional
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
9 list of directories where the module is to be searched. If present,
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
10 path is prepended to the system search path sys.path. The return
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
11 value is a dictionary. The keys of the dictionary are the names of
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
12 the classes defined in the module (including classes that are defined
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
13 via the from XXX import YYY construct). The values are class
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
14 instances of the class Class defined here. One special key/value pair
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
15 is present for packages: the key '__path__' has a list as its value
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
16 which contains the package search path.
2867
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
17
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
18 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
19 of this class have the following instance variables:
26495
5467c6e1f541 Moderately heavy reorganization of pyclbr to fix package-related bugs.
Guido van Rossum <guido@python.org>
parents: 25289
diff changeset
20 module -- the module name
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11708
diff changeset
21 name -- the name of the class
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11708
diff changeset
22 super -- a list of super classes (Class instances)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11708
diff changeset
23 methods -- a dictionary of methods
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11708
diff changeset
24 file -- the file in which the class was defined
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11708
diff changeset
25 lineno -- the line in the file on which the class statement occurred
2867
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
26 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
27 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
28 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
29 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
30 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
31 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
32 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
33
26495
5467c6e1f541 Moderately heavy reorganization of pyclbr to fix package-related bugs.
Guido van Rossum <guido@python.org>
parents: 25289
diff changeset
34 A function is described by the class Function in this module.
5467c6e1f541 Moderately heavy reorganization of pyclbr to fix package-related bugs.
Guido van Rossum <guido@python.org>
parents: 25289
diff changeset
35 Instances of this class have the following instance variables:
5467c6e1f541 Moderately heavy reorganization of pyclbr to fix package-related bugs.
Guido van Rossum <guido@python.org>
parents: 25289
diff changeset
36 module -- the module name
5467c6e1f541 Moderately heavy reorganization of pyclbr to fix package-related bugs.
Guido van Rossum <guido@python.org>
parents: 25289
diff changeset
37 name -- the name of the class
5467c6e1f541 Moderately heavy reorganization of pyclbr to fix package-related bugs.
Guido van Rossum <guido@python.org>
parents: 25289
diff changeset
38 file -- the file in which the class was defined
5467c6e1f541 Moderately heavy reorganization of pyclbr to fix package-related bugs.
Guido van Rossum <guido@python.org>
parents: 25289
diff changeset
39 lineno -- the line in the file on which the class statement occurred
11708
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 10994
diff changeset
40 """
2867
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
41
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
42 import sys
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
43 import imp
46587
576fa2440a0c Minor cleanups:
Skip Montanaro <skip@pobox.com>
parents: 39932
diff changeset
44 import tokenize
576fa2440a0c Minor cleanups:
Skip Montanaro <skip@pobox.com>
parents: 39932
diff changeset
45 from token import NAME, DEDENT, OP
31054
3d1f0df75a80 Apply extract functions instead of lambda.
Raymond Hettinger <python@rcn.com>
parents: 26507
diff changeset
46 from operator import itemgetter
2867
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
47
26495
5467c6e1f541 Moderately heavy reorganization of pyclbr to fix package-related bugs.
Guido van Rossum <guido@python.org>
parents: 25289
diff changeset
48 __all__ = ["readmodule", "readmodule_ex", "Class", "Function"]
16562
623456576d6a __all__ for several more modules
Skip Montanaro <skip@pobox.com>
parents: 16535
diff changeset
49
10924
5259703f4b5c Experimental speedup patch by Tim Peters (please test!):
Guido van Rossum <guido@python.org>
parents: 10237
diff changeset
50 _modules = {} # cache of modules we've seen
2867
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:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11708
diff changeset
54 '''Class to represent a Python class.'''
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11708
diff changeset
55 def __init__(self, module, name, super, file, lineno):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11708
diff changeset
56 self.module = module
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11708
diff changeset
57 self.name = name
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11708
diff changeset
58 if super is None:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11708
diff changeset
59 super = []
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11708
diff changeset
60 self.super = super
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11708
diff changeset
61 self.methods = {}
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11708
diff changeset
62 self.file = file
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11708
diff changeset
63 self.lineno = lineno
2867
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
64
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11708
diff changeset
65 def _addmethod(self, name, lineno):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11708
diff changeset
66 self.methods[name] = lineno
2867
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
67
26495
5467c6e1f541 Moderately heavy reorganization of pyclbr to fix package-related bugs.
Guido van Rossum <guido@python.org>
parents: 25289
diff changeset
68 class Function:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11708
diff changeset
69 '''Class to represent a top-level Python function'''
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11708
diff changeset
70 def __init__(self, module, name, file, lineno):
26495
5467c6e1f541 Moderately heavy reorganization of pyclbr to fix package-related bugs.
Guido van Rossum <guido@python.org>
parents: 25289
diff changeset
71 self.module = module
5467c6e1f541 Moderately heavy reorganization of pyclbr to fix package-related bugs.
Guido van Rossum <guido@python.org>
parents: 25289
diff changeset
72 self.name = name
5467c6e1f541 Moderately heavy reorganization of pyclbr to fix package-related bugs.
Guido van Rossum <guido@python.org>
parents: 25289
diff changeset
73 self.file = file
5467c6e1f541 Moderately heavy reorganization of pyclbr to fix package-related bugs.
Guido van Rossum <guido@python.org>
parents: 25289
diff changeset
74 self.lineno = lineno
10954
4d76cabbf99e Co-production with Tim Peters, implementing a suggestion by Mark
Guido van Rossum <guido@python.org>
parents: 10948
diff changeset
75
46587
576fa2440a0c Minor cleanups:
Skip Montanaro <skip@pobox.com>
parents: 39932
diff changeset
76 def readmodule(module, path=None):
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11708
diff changeset
77 '''Backwards compatible interface.
10954
4d76cabbf99e Co-production with Tim Peters, implementing a suggestion by Mark
Guido van Rossum <guido@python.org>
parents: 10948
diff changeset
78
26495
5467c6e1f541 Moderately heavy reorganization of pyclbr to fix package-related bugs.
Guido van Rossum <guido@python.org>
parents: 25289
diff changeset
79 Call readmodule_ex() and then only keep Class objects from the
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11708
diff changeset
80 resulting dictionary.'''
10954
4d76cabbf99e Co-production with Tim Peters, implementing a suggestion by Mark
Guido van Rossum <guido@python.org>
parents: 10948
diff changeset
81
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11708
diff changeset
82 res = {}
46587
576fa2440a0c Minor cleanups:
Skip Montanaro <skip@pobox.com>
parents: 39932
diff changeset
83 for key, value in _readmodule(module, path or []).items():
26495
5467c6e1f541 Moderately heavy reorganization of pyclbr to fix package-related bugs.
Guido van Rossum <guido@python.org>
parents: 25289
diff changeset
84 if isinstance(value, Class):
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11708
diff changeset
85 res[key] = value
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11708
diff changeset
86 return res
10954
4d76cabbf99e Co-production with Tim Peters, implementing a suggestion by Mark
Guido van Rossum <guido@python.org>
parents: 10948
diff changeset
87
46587
576fa2440a0c Minor cleanups:
Skip Montanaro <skip@pobox.com>
parents: 39932
diff changeset
88 def readmodule_ex(module, path=None):
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11708
diff changeset
89 '''Read a module file and return a dictionary of classes.
2867
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
90
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11708
diff changeset
91 Search for MODULE in PATH and sys.path, read and parse the
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11708
diff changeset
92 module and return a dictionary with one entry for each class
26495
5467c6e1f541 Moderately heavy reorganization of pyclbr to fix package-related bugs.
Guido van Rossum <guido@python.org>
parents: 25289
diff changeset
93 found in the module.
46587
576fa2440a0c Minor cleanups:
Skip Montanaro <skip@pobox.com>
parents: 39932
diff changeset
94 '''
576fa2440a0c Minor cleanups:
Skip Montanaro <skip@pobox.com>
parents: 39932
diff changeset
95 return _readmodule(module, path or [])
26495
5467c6e1f541 Moderately heavy reorganization of pyclbr to fix package-related bugs.
Guido van Rossum <guido@python.org>
parents: 25289
diff changeset
96
46587
576fa2440a0c Minor cleanups:
Skip Montanaro <skip@pobox.com>
parents: 39932
diff changeset
97 def _readmodule(module, path, inpackage=None):
576fa2440a0c Minor cleanups:
Skip Montanaro <skip@pobox.com>
parents: 39932
diff changeset
98 '''Do the hard work for readmodule[_ex].
576fa2440a0c Minor cleanups:
Skip Montanaro <skip@pobox.com>
parents: 39932
diff changeset
99
576fa2440a0c Minor cleanups:
Skip Montanaro <skip@pobox.com>
parents: 39932
diff changeset
100 If INPACKAGE is given, it must be the dotted name of the package in
26495
5467c6e1f541 Moderately heavy reorganization of pyclbr to fix package-related bugs.
Guido van Rossum <guido@python.org>
parents: 25289
diff changeset
101 which we are searching for a submodule, and then PATH must be the
5467c6e1f541 Moderately heavy reorganization of pyclbr to fix package-related bugs.
Guido van Rossum <guido@python.org>
parents: 25289
diff changeset
102 package search path; otherwise, we are searching for a top-level
5467c6e1f541 Moderately heavy reorganization of pyclbr to fix package-related bugs.
Guido van Rossum <guido@python.org>
parents: 25289
diff changeset
103 module, and PATH is combined with sys.path.
5467c6e1f541 Moderately heavy reorganization of pyclbr to fix package-related bugs.
Guido van Rossum <guido@python.org>
parents: 25289
diff changeset
104 '''
5467c6e1f541 Moderately heavy reorganization of pyclbr to fix package-related bugs.
Guido van Rossum <guido@python.org>
parents: 25289
diff changeset
105 # Compute the full module name (prepending inpackage if set)
46587
576fa2440a0c Minor cleanups:
Skip Montanaro <skip@pobox.com>
parents: 39932
diff changeset
106 if inpackage is not None:
26495
5467c6e1f541 Moderately heavy reorganization of pyclbr to fix package-related bugs.
Guido van Rossum <guido@python.org>
parents: 25289
diff changeset
107 fullmodule = "%s.%s" % (inpackage, module)
5467c6e1f541 Moderately heavy reorganization of pyclbr to fix package-related bugs.
Guido van Rossum <guido@python.org>
parents: 25289
diff changeset
108 else:
5467c6e1f541 Moderately heavy reorganization of pyclbr to fix package-related bugs.
Guido van Rossum <guido@python.org>
parents: 25289
diff changeset
109 fullmodule = module
5467c6e1f541 Moderately heavy reorganization of pyclbr to fix package-related bugs.
Guido van Rossum <guido@python.org>
parents: 25289
diff changeset
110
5467c6e1f541 Moderately heavy reorganization of pyclbr to fix package-related bugs.
Guido van Rossum <guido@python.org>
parents: 25289
diff changeset
111 # Check in the cache
5467c6e1f541 Moderately heavy reorganization of pyclbr to fix package-related bugs.
Guido van Rossum <guido@python.org>
parents: 25289
diff changeset
112 if fullmodule in _modules:
5467c6e1f541 Moderately heavy reorganization of pyclbr to fix package-related bugs.
Guido van Rossum <guido@python.org>
parents: 25289
diff changeset
113 return _modules[fullmodule]
5467c6e1f541 Moderately heavy reorganization of pyclbr to fix package-related bugs.
Guido van Rossum <guido@python.org>
parents: 25289
diff changeset
114
5467c6e1f541 Moderately heavy reorganization of pyclbr to fix package-related bugs.
Guido van Rossum <guido@python.org>
parents: 25289
diff changeset
115 # Initialize the dict for this module's contents
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11708
diff changeset
116 dict = {}
10948
b928f7b9dfa2 Fix by Sjoerd for a package related bug: If you have a non-empy
Guido van Rossum <guido@python.org>
parents: 10929
diff changeset
117
26495
5467c6e1f541 Moderately heavy reorganization of pyclbr to fix package-related bugs.
Guido van Rossum <guido@python.org>
parents: 25289
diff changeset
118 # Check if it is a built-in module; we don't do much for these
46587
576fa2440a0c Minor cleanups:
Skip Montanaro <skip@pobox.com>
parents: 39932
diff changeset
119 if module in sys.builtin_module_names and inpackage is None:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11708
diff changeset
120 _modules[module] = dict
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11708
diff changeset
121 return dict
2867
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
122
26495
5467c6e1f541 Moderately heavy reorganization of pyclbr to fix package-related bugs.
Guido van Rossum <guido@python.org>
parents: 25289
diff changeset
123 # Check for a dotted module name
5467c6e1f541 Moderately heavy reorganization of pyclbr to fix package-related bugs.
Guido van Rossum <guido@python.org>
parents: 25289
diff changeset
124 i = module.rfind('.')
5467c6e1f541 Moderately heavy reorganization of pyclbr to fix package-related bugs.
Guido van Rossum <guido@python.org>
parents: 25289
diff changeset
125 if i >= 0:
5467c6e1f541 Moderately heavy reorganization of pyclbr to fix package-related bugs.
Guido van Rossum <guido@python.org>
parents: 25289
diff changeset
126 package = module[:i]
5467c6e1f541 Moderately heavy reorganization of pyclbr to fix package-related bugs.
Guido van Rossum <guido@python.org>
parents: 25289
diff changeset
127 submodule = module[i+1:]
26507
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
128 parent = _readmodule(package, path, inpackage)
46587
576fa2440a0c Minor cleanups:
Skip Montanaro <skip@pobox.com>
parents: 39932
diff changeset
129 if inpackage is not None:
26495
5467c6e1f541 Moderately heavy reorganization of pyclbr to fix package-related bugs.
Guido van Rossum <guido@python.org>
parents: 25289
diff changeset
130 package = "%s.%s" % (inpackage, package)
77041
2d2079593212 #14798: pyclbr now raises ImportError instead of KeyError for missing packages
Petri Lehtinen <petri@digip.org>
parents: 46587
diff changeset
131 if not '__path__' in parent:
2d2079593212 #14798: pyclbr now raises ImportError instead of KeyError for missing packages
Petri Lehtinen <petri@digip.org>
parents: 46587
diff changeset
132 raise ImportError('No package named {}'.format(package))
26507
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
133 return _readmodule(submodule, parent['__path__'], package)
26495
5467c6e1f541 Moderately heavy reorganization of pyclbr to fix package-related bugs.
Guido van Rossum <guido@python.org>
parents: 25289
diff changeset
134
5467c6e1f541 Moderately heavy reorganization of pyclbr to fix package-related bugs.
Guido van Rossum <guido@python.org>
parents: 25289
diff changeset
135 # Search the path for the module
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11708
diff changeset
136 f = None
46587
576fa2440a0c Minor cleanups:
Skip Montanaro <skip@pobox.com>
parents: 39932
diff changeset
137 if inpackage is not None:
576fa2440a0c Minor cleanups:
Skip Montanaro <skip@pobox.com>
parents: 39932
diff changeset
138 f, fname, (_s, _m, ty) = imp.find_module(module, path)
26495
5467c6e1f541 Moderately heavy reorganization of pyclbr to fix package-related bugs.
Guido van Rossum <guido@python.org>
parents: 25289
diff changeset
139 else:
46587
576fa2440a0c Minor cleanups:
Skip Montanaro <skip@pobox.com>
parents: 39932
diff changeset
140 f, fname, (_s, _m, ty) = imp.find_module(module, path + sys.path)
576fa2440a0c Minor cleanups:
Skip Montanaro <skip@pobox.com>
parents: 39932
diff changeset
141 if ty == imp.PKG_DIRECTORY:
576fa2440a0c Minor cleanups:
Skip Montanaro <skip@pobox.com>
parents: 39932
diff changeset
142 dict['__path__'] = [fname]
576fa2440a0c Minor cleanups:
Skip Montanaro <skip@pobox.com>
parents: 39932
diff changeset
143 path = [fname] + path
576fa2440a0c Minor cleanups:
Skip Montanaro <skip@pobox.com>
parents: 39932
diff changeset
144 f, fname, (_s, _m, ty) = imp.find_module('__init__', [fname])
26495
5467c6e1f541 Moderately heavy reorganization of pyclbr to fix package-related bugs.
Guido van Rossum <guido@python.org>
parents: 25289
diff changeset
145 _modules[fullmodule] = dict
46587
576fa2440a0c Minor cleanups:
Skip Montanaro <skip@pobox.com>
parents: 39932
diff changeset
146 if ty != imp.PY_SOURCE:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11708
diff changeset
147 # not Python source, can't do anything with this module
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11708
diff changeset
148 f.close()
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11708
diff changeset
149 return dict
2867
a169e68734ff Module with one function to read Python modules and extract class and
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
150
26507
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
151 stack = [] # stack of (class, indent) pairs
10924
5259703f4b5c Experimental speedup patch by Tim Peters (please test!):
Guido van Rossum <guido@python.org>
parents: 10237
diff changeset
152
24976
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
153 g = tokenize.generate_tokens(f.readline)
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
154 try:
46587
576fa2440a0c Minor cleanups:
Skip Montanaro <skip@pobox.com>
parents: 39932
diff changeset
155 for tokentype, token, start, _end, _line in g:
26507
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
156 if tokentype == DEDENT:
24976
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
157 lineno, thisindent = start
26507
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
158 # close nested classes and defs
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
159 while stack and stack[-1][1] >= thisindent:
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
160 del stack[-1]
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
161 elif token == 'def':
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
162 lineno, thisindent = start
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
163 # close previous nested classes and defs
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
164 while stack and stack[-1][1] >= thisindent:
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
165 del stack[-1]
46587
576fa2440a0c Minor cleanups:
Skip Montanaro <skip@pobox.com>
parents: 39932
diff changeset
166 tokentype, meth_name, start = g.next()[0:3]
24976
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
167 if tokentype != NAME:
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
168 continue # Syntax error
26507
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
169 if stack:
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
170 cur_class = stack[-1][0]
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
171 if isinstance(cur_class, Class):
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
172 # it's a method
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
173 cur_class._addmethod(meth_name, lineno)
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
174 # else it's a nested def
24976
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
175 else:
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
176 # it's a function
46587
576fa2440a0c Minor cleanups:
Skip Montanaro <skip@pobox.com>
parents: 39932
diff changeset
177 dict[meth_name] = Function(fullmodule, meth_name,
576fa2440a0c Minor cleanups:
Skip Montanaro <skip@pobox.com>
parents: 39932
diff changeset
178 fname, lineno)
26507
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
179 stack.append((None, thisindent)) # Marker for nested fns
24976
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
180 elif token == 'class':
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
181 lineno, thisindent = start
26507
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
182 # close previous nested classes and defs
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
183 while stack and stack[-1][1] >= thisindent:
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
184 del stack[-1]
46587
576fa2440a0c Minor cleanups:
Skip Montanaro <skip@pobox.com>
parents: 39932
diff changeset
185 tokentype, class_name, start = g.next()[0:3]
24976
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
186 if tokentype != NAME:
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
187 continue # Syntax error
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
188 # parse what follows the class name
46587
576fa2440a0c Minor cleanups:
Skip Montanaro <skip@pobox.com>
parents: 39932
diff changeset
189 tokentype, token, start = g.next()[0:3]
24976
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
190 inherit = None
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
191 if token == '(':
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
192 names = [] # List of superclasses
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
193 # there's a list of superclasses
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
194 level = 1
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
195 super = [] # Tokens making up current superclass
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
196 while True:
46587
576fa2440a0c Minor cleanups:
Skip Montanaro <skip@pobox.com>
parents: 39932
diff changeset
197 tokentype, token, start = g.next()[0:3]
24976
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
198 if token in (')', ',') and level == 1:
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
199 n = "".join(super)
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
200 if n in dict:
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
201 # we know this super class
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
202 n = dict[n]
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
203 else:
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
204 c = n.split('.')
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
205 if len(c) > 1:
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
206 # super class is of the form
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
207 # module.class: look in module for
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
208 # class
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
209 m = c[-2]
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
210 c = c[-1]
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
211 if m in _modules:
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
212 d = _modules[m]
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
213 if c in d:
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
214 n = d[c]
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
215 names.append(n)
26507
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
216 super = []
24976
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
217 if token == '(':
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
218 level += 1
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
219 elif token == ')':
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
220 level -= 1
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
221 if level == 0:
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
222 break
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
223 elif token == ',' and level == 1:
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
224 pass
38482
6743a6a06f15 Fix #1494787 (pyclbr counts whitespace as superclass name)
Georg Brandl <georg@python.org>
parents: 32888
diff changeset
225 # only use NAME and OP (== dot) tokens for type name
6743a6a06f15 Fix #1494787 (pyclbr counts whitespace as superclass name)
Georg Brandl <georg@python.org>
parents: 32888
diff changeset
226 elif tokentype in (NAME, OP) and level == 1:
24976
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
227 super.append(token)
38482
6743a6a06f15 Fix #1494787 (pyclbr counts whitespace as superclass name)
Georg Brandl <georg@python.org>
parents: 32888
diff changeset
228 # expressions in the base list are not supported
24976
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
229 inherit = names
46587
576fa2440a0c Minor cleanups:
Skip Montanaro <skip@pobox.com>
parents: 39932
diff changeset
230 cur_class = Class(fullmodule, class_name, inherit,
576fa2440a0c Minor cleanups:
Skip Montanaro <skip@pobox.com>
parents: 39932
diff changeset
231 fname, lineno)
26507
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
232 if not stack:
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
233 dict[class_name] = cur_class
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
234 stack.append((cur_class, thisindent))
24976
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
235 elif token == 'import' and start[1] == 0:
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
236 modules = _getnamelist(g)
46587
576fa2440a0c Minor cleanups:
Skip Montanaro <skip@pobox.com>
parents: 39932
diff changeset
237 for mod, _mod2 in modules:
25289
752f6895cc63 When recursively attempting to find the modules imported by an
Guido van Rossum <guido@python.org>
parents: 24976
diff changeset
238 try:
752f6895cc63 When recursively attempting to find the modules imported by an
Guido van Rossum <guido@python.org>
parents: 24976
diff changeset
239 # Recursively read the imported module
46587
576fa2440a0c Minor cleanups:
Skip Montanaro <skip@pobox.com>
parents: 39932
diff changeset
240 if inpackage is None:
26507
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
241 _readmodule(mod, path)
26495
5467c6e1f541 Moderately heavy reorganization of pyclbr to fix package-related bugs.
Guido van Rossum <guido@python.org>
parents: 25289
diff changeset
242 else:
5467c6e1f541 Moderately heavy reorganization of pyclbr to fix package-related bugs.
Guido van Rossum <guido@python.org>
parents: 25289
diff changeset
243 try:
26507
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
244 _readmodule(mod, path, inpackage)
26495
5467c6e1f541 Moderately heavy reorganization of pyclbr to fix package-related bugs.
Guido van Rossum <guido@python.org>
parents: 25289
diff changeset
245 except ImportError:
26507
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
246 _readmodule(mod, [])
25289
752f6895cc63 When recursively attempting to find the modules imported by an
Guido van Rossum <guido@python.org>
parents: 24976
diff changeset
247 except:
752f6895cc63 When recursively attempting to find the modules imported by an
Guido van Rossum <guido@python.org>
parents: 24976
diff changeset
248 # If we can't find or parse the imported module,
752f6895cc63 When recursively attempting to find the modules imported by an
Guido van Rossum <guido@python.org>
parents: 24976
diff changeset
249 # too bad -- don't die here.
752f6895cc63 When recursively attempting to find the modules imported by an
Guido van Rossum <guido@python.org>
parents: 24976
diff changeset
250 pass
24976
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
251 elif token == 'from' and start[1] == 0:
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
252 mod, token = _getname(g)
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
253 if not mod or token != "import":
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
254 continue
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
255 names = _getnamelist(g)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11708
diff changeset
256 try:
25289
752f6895cc63 When recursively attempting to find the modules imported by an
Guido van Rossum <guido@python.org>
parents: 24976
diff changeset
257 # Recursively read the imported module
26507
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
258 d = _readmodule(mod, path, inpackage)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11708
diff changeset
259 except:
25289
752f6895cc63 When recursively attempting to find the modules imported by an
Guido van Rossum <guido@python.org>
parents: 24976
diff changeset
260 # If we can't find or parse the imported module,
752f6895cc63 When recursively attempting to find the modules imported by an
Guido van Rossum <guido@python.org>
parents: 24976
diff changeset
261 # too bad -- don't die here.
24976
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
262 continue
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
263 # add any classes that were defined in the imported module
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
264 # to our name space if they were mentioned in the list
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
265 for n, n2 in names:
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
266 if n in d:
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
267 dict[n2 or n] = d[n]
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
268 elif n == '*':
26507
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
269 # don't add names that start with _
24976
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
270 for n in d:
26507
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
271 if n[0] != '_':
24976
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
272 dict[n] = d[n]
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
273 except StopIteration:
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
274 pass
10924
5259703f4b5c Experimental speedup patch by Tim Peters (please test!):
Guido van Rossum <guido@python.org>
parents: 10237
diff changeset
275
24976
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
276 f.close()
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11708
diff changeset
277 return dict
10929
84f1cbe00943 Tim Peters: Taught it more "real Python" rules without slowing it
Guido van Rossum <guido@python.org>
parents: 10924
diff changeset
278
24976
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
279 def _getnamelist(g):
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
280 # Helper to get a comma-separated list of dotted names plus 'as'
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
281 # clauses. Return a list of pairs (name, name2) where name2 is
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
282 # the 'as' name, or None if there is no 'as' clause.
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
283 names = []
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
284 while True:
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
285 name, token = _getname(g)
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
286 if not name:
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
287 break
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
288 if token == 'as':
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
289 name2, token = _getname(g)
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
290 else:
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
291 name2 = None
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
292 names.append((name, name2))
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
293 while token != "," and "\n" not in token:
46587
576fa2440a0c Minor cleanups:
Skip Montanaro <skip@pobox.com>
parents: 39932
diff changeset
294 token = g.next()[1]
24976
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
295 if token != ",":
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
296 break
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
297 return names
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
298
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
299 def _getname(g):
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
300 # Helper to get a dotted name, return a pair (name, token) where
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
301 # name is the dotted name, or None if there was no dotted name,
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
302 # and token is the next input token.
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
303 parts = []
46587
576fa2440a0c Minor cleanups:
Skip Montanaro <skip@pobox.com>
parents: 39932
diff changeset
304 tokentype, token = g.next()[0:2]
24976
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
305 if tokentype != NAME and token != '*':
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
306 return (None, token)
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
307 parts.append(token)
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
308 while True:
46587
576fa2440a0c Minor cleanups:
Skip Montanaro <skip@pobox.com>
parents: 39932
diff changeset
309 tokentype, token = g.next()[0:2]
24976
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
310 if token != '.':
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
311 break
46587
576fa2440a0c Minor cleanups:
Skip Montanaro <skip@pobox.com>
parents: 39932
diff changeset
312 tokentype, token = g.next()[0:2]
24976
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
313 if tokentype != NAME:
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
314 break
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
315 parts.append(token)
063f2031660c Rewritten using the tokenize module, which gives us a real tokenizer
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
316 return (".".join(parts), token)
26507
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
317
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
318 def _main():
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
319 # Main program for testing.
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
320 import os
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
321 mod = sys.argv[1]
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
322 if os.path.exists(mod):
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
323 path = [os.path.dirname(mod)]
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
324 mod = os.path.basename(mod)
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
325 if mod.lower().endswith(".py"):
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
326 mod = mod[:-3]
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
327 else:
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
328 path = []
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
329 dict = readmodule_ex(mod, path)
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
330 objs = dict.values()
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
331 objs.sort(lambda a, b: cmp(getattr(a, 'lineno', 0),
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
332 getattr(b, 'lineno', 0)))
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
333 for obj in objs:
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
334 if isinstance(obj, Class):
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
335 print "class", obj.name, obj.super, obj.lineno
31210
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31054
diff changeset
336 methods = sorted(obj.methods.iteritems(), key=itemgetter(1))
26507
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
337 for name, lineno in methods:
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
338 if name != "__path__":
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
339 print " def", name, lineno
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
340 elif isinstance(obj, Function):
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
341 print "def", obj.name, obj.lineno
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
342
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
343 if __name__ == "__main__":
fa4532ef99c9 Another big update, fixing all known bugs related to nesting functions
Guido van Rossum <guido@python.org>
parents: 26495
diff changeset
344 _main()