Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(323)

Side by Side Diff: third_party/logilab/astng/utils.py

Issue 719313003: Revert "pylint: upgrade to 1.3.1" (Closed) Base URL: https://chromium.googlesource.com/chromium/tools/depot_tools.git@master
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/logilab/astng/scoped_nodes.py ('k') | third_party/logilab/astroid/LICENSE.txt » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. 1 # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr 2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
3 # copyright 2003-2010 Sylvain Thenault, all rights reserved.
4 # contact mailto:thenault@gmail.com
3 # 5 #
4 # This file is part of astroid. 6 # This file is part of logilab-astng.
5 # 7 #
6 # astroid is free software: you can redistribute it and/or modify it 8 # logilab-astng is free software: you can redistribute it and/or modify it
7 # under the terms of the GNU Lesser General Public License as published by the 9 # under the terms of the GNU Lesser General Public License as published by the
8 # Free Software Foundation, either version 2.1 of the License, or (at your 10 # Free Software Foundation, either version 2.1 of the License, or (at your
9 # option) any later version. 11 # option) any later version.
10 # 12 #
11 # astroid is distributed in the hope that it will be useful, but 13 # logilab-astng is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 15 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
14 # for more details. 16 # for more details.
15 # 17 #
16 # You should have received a copy of the GNU Lesser General Public License along 18 # You should have received a copy of the GNU Lesser General Public License along
17 # with astroid. If not, see <http://www.gnu.org/licenses/>. 19 # with logilab-astng. If not, see <http://www.gnu.org/licenses/>.
18 """this module contains some utilities to navigate in the tree or to 20 """this module contains some utilities to navigate in the tree or to
19 extract information from it 21 extract information from it
20 """ 22 """
21 23
22 __docformat__ = "restructuredtext en" 24 __docformat__ = "restructuredtext en"
23 25
24 from astroid.exceptions import AstroidBuildingException 26 from logilab.astng.exceptions import ASTNGBuildingException
25 from astroid.builder import parse
26 27
27 28
28 class ASTWalker(object): 29 class ASTWalker:
29 """a walker visiting a tree in preorder, calling on the handler: 30 """a walker visiting a tree in preorder, calling on the handler:
30 31
31 * visit_<class name> on entering a node, where class name is the class of 32 * visit_<class name> on entering a node, where class name is the class of
32 the node in lower case 33 the node in lower case
33 34
34 * leave_<class name> on leaving a node, where class name is the class of 35 * leave_<class name> on leaving a node, where class name is the class of
35 the node in lower case 36 the node in lower case
36 """ 37 """
37 38
38 def __init__(self, handler): 39 def __init__(self, handler):
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 92
92 def visit(self, node): 93 def visit(self, node):
93 """launch the visit starting from the given node""" 94 """launch the visit starting from the given node"""
94 if node in self._visited: 95 if node in self._visited:
95 return 96 return
96 self._visited[node] = 1 # FIXME: use set ? 97 self._visited[node] = 1 # FIXME: use set ?
97 methods = self.get_callbacks(node) 98 methods = self.get_callbacks(node)
98 if methods[0] is not None: 99 if methods[0] is not None:
99 methods[0](node) 100 methods[0](node)
100 if 'locals' in node.__dict__: # skip Instance and other proxy 101 if 'locals' in node.__dict__: # skip Instance and other proxy
101 for local_node in node.values(): 102 for name, local_node in node.items():
102 self.visit(local_node) 103 self.visit(local_node)
103 if methods[1] is not None: 104 if methods[1] is not None:
104 return methods[1](node) 105 return methods[1](node)
105 106
106 107
107 def _check_children(node): 108 def _check_children(node):
108 """a helper function to check children - parent relations""" 109 """a helper function to check children - parent relations"""
109 for child in node.get_children(): 110 for child in node.get_children():
110 ok = False 111 ok = False
111 if child is None: 112 if child is None:
112 print "Hm, child of %s is None" % node 113 print "Hm, child of %s is None" % node
113 continue 114 continue
114 if not hasattr(child, 'parent'): 115 if not hasattr(child, 'parent'):
115 print " ERROR: %s has child %s %x with no parent" % ( 116 print " ERROR: %s has child %s %x with no parent" % (node, child, id (child))
116 node, child, id(child))
117 elif not child.parent: 117 elif not child.parent:
118 print " ERROR: %s has child %s %x with parent %r" % ( 118 print " ERROR: %s has child %s %x with parent %r" % (node, child, id (child), child.parent)
119 node, child, id(child), child.parent)
120 elif child.parent is not node: 119 elif child.parent is not node:
121 print " ERROR: %s %x has child %s %x with wrong parent %s" % ( 120 print " ERROR: %s %x has child %s %x with wrong parent %s" % (node,
122 node, id(node), child, id(child), child.parent) 121 id(node), child, id(child), child.parent)
123 else: 122 else:
124 ok = True 123 ok = True
125 if not ok: 124 if not ok:
126 print "lines;", node.lineno, child.lineno 125 print "lines;", node.lineno, child.lineno
127 print "of module", node.root(), node.root().name 126 print "of module", node.root(), node.root().name
128 raise AstroidBuildingException 127 raise ASTNGBuildingException
129 _check_children(child) 128 _check_children(child)
130 129
131 130
131 from _ast import PyCF_ONLY_AST
132 def parse(string):
133 return compile(string, "<string>", 'exec', PyCF_ONLY_AST)
134
132 class TreeTester(object): 135 class TreeTester(object):
133 '''A helper class to see _ast tree and compare with astroid tree 136 '''A helper class to see _ast tree and compare with astng tree
134 137
135 indent: string for tree indent representation 138 indent: string for tree indent representation
136 lineno: bool to tell if we should print the line numbers 139 lineno: bool to tell if we should print the line numbers
137 140
138 >>> tester = TreeTester('print') 141 >>> tester = TreeTester('print')
139 >>> print tester.native_tree_repr() 142 >>> print tester.native_tree_repr()
140 143
141 <Module> 144 <Module>
142 . body = [ 145 . body = [
143 . <Print> 146 . <Print>
144 . . nl = True 147 . . nl = True
145 . ] 148 . ]
146 >>> print tester.astroid_tree_repr() 149 >>> print tester.astng_tree_repr()
147 Module() 150 Module()
148 body = [ 151 body = [
149 Print() 152 Print()
150 dest = 153 dest =
151 values = [ 154 values = [
152 ] 155 ]
153 ] 156 ]
154 ''' 157 '''
155 158
156 indent = '. ' 159 indent = '. '
157 lineno = False 160 lineno = False
158 161
159 def __init__(self, sourcecode): 162 def __init__(self, sourcecode):
160 self._string = '' 163 self._string = ''
(...skipping 14 matching lines...) Expand all
175 return self._string 178 return self._string
176 179
177 180
178 def _native_repr_tree(self, node, indent, _done=None): 181 def _native_repr_tree(self, node, indent, _done=None):
179 """recursive method for the native tree representation""" 182 """recursive method for the native tree representation"""
180 from _ast import Load as _Load, Store as _Store, Del as _Del 183 from _ast import Load as _Load, Store as _Store, Del as _Del
181 from _ast import AST as Node 184 from _ast import AST as Node
182 if _done is None: 185 if _done is None:
183 _done = set() 186 _done = set()
184 if node in _done: 187 if node in _done:
185 self._string += '\nloop in tree: %r (%s)' % ( 188 self._string += '\nloop in tree: %r (%s)' % (node,
186 node, getattr(node, 'lineno', None)) 189 getattr(node, 'lineno', None))
187 return 190 return
188 _done.add(node) 191 _done.add(node)
189 self._string += '\n' + indent + '<%s>' % node.__class__.__name__ 192 self._string += '\n' + indent + '<%s>' % node.__class__.__name__
190 indent += self.indent 193 indent += self.indent
191 if not hasattr(node, '__dict__'): 194 if not hasattr(node, '__dict__'):
192 self._string += '\n' + self.indent + " ** node has no __dict__ " + s tr(node) 195 self._string += '\n' + self.indent + " ** node has no __dict__ " + s tr(node)
193 return 196 return
194 node_dict = node.__dict__ 197 node_dict = node.__dict__
195 if hasattr(node, '_attributes'): 198 if hasattr(node, '_attributes'):
196 for a in node._attributes: 199 for a in node._attributes:
197 attr = node_dict[a] 200 attr = node_dict[a]
198 if attr is None: 201 if attr is None:
199 continue 202 continue
200 if a in ("lineno", "col_offset") and not self.lineno: 203 if a in ("lineno", "col_offset") and not self.lineno:
201 continue 204 continue
202 self._string += '\n' + indent + a + " = " + repr(attr) 205 self._string +='\n' + indent + a + " = " + repr(attr)
203 for field in node._fields or (): 206 for field in node._fields or ():
204 attr = node_dict[field] 207 attr = node_dict[field]
205 if attr is None: 208 if attr is None:
206 continue 209 continue
207 if isinstance(attr, list): 210 if isinstance(attr, list):
208 if not attr: 211 if not attr:
209 continue 212 continue
210 self._string += '\n' + indent + field + ' = [' 213 self._string += '\n' + indent + field + ' = ['
211 for elt in attr: 214 for elt in attr:
212 self._native_repr_tree(elt, indent, _done) 215 self._native_repr_tree(elt, indent, _done)
213 self._string += '\n' + indent + ']' 216 self._string += '\n' + indent + ']'
214 continue 217 continue
215 if isinstance(attr, (_Load, _Store, _Del)): 218 if isinstance(attr, (_Load, _Store, _Del)):
216 continue 219 continue
217 if isinstance(attr, Node): 220 if isinstance(attr, Node):
218 self._string += '\n' + indent + field + " = " 221 self._string += '\n' + indent + field + " = "
219 self._native_repr_tree(attr, indent, _done) 222 self._native_repr_tree(attr, indent, _done)
220 else: 223 else:
221 self._string += '\n' + indent + field + " = " + repr(attr) 224 self._string += '\n' + indent + field + " = " + repr(attr)
222 225
223 226
224 def build_astroid_tree(self): 227 def build_astng_tree(self):
225 """build astroid tree from the _ast tree 228 """build astng tree from the _ast tree
226 """ 229 """
227 from astroid.builder import AstroidBuilder 230 from logilab.astng.builder import ASTNGBuilder
228 tree = AstroidBuilder().string_build(self.sourcecode) 231 tree = ASTNGBuilder().string_build(self.sourcecode)
229 return tree 232 return tree
230 233
231 def astroid_tree_repr(self, ids=False): 234 def astng_tree_repr(self, ids=False):
232 """build the astroid tree and return a nice tree representation""" 235 """build the astng tree and return a nice tree representation"""
233 mod = self.build_astroid_tree() 236 mod = self.build_astng_tree()
234 return mod.repr_tree(ids) 237 return mod.repr_tree(ids)
235 238
236 239
237 __all__ = ('LocalsVisitor', 'ASTWalker',) 240 __all__ = ('LocalsVisitor', 'ASTWalker',)
238 241
OLDNEW
« no previous file with comments | « third_party/logilab/astng/scoped_nodes.py ('k') | third_party/logilab/astroid/LICENSE.txt » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698