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

Side by Side Diff: third_party/logilab/astroid/raw_building.py

Issue 739393004: Revert "Revert "pylint: upgrade to 1.3.1"" (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/tools/depot_tools/
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 | Annotate | Revision Log
« no previous file with comments | « third_party/logilab/astroid/protocols.py ('k') | third_party/logilab/astroid/rebuilder.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 # copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
3 #
4 # This file is part of astroid.
5 #
6 # astroid 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
8 # Free Software Foundation, either version 2.1 of the License, or (at your
9 # option) any later version.
10 #
11 # astroid is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
14 # for more details.
15 #
16 # 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/>.
18 """this module contains a set of functions to create astroid trees from scratch
19 (build_* functions) or from living object (object_build_* functions)
20 """
21
22 __docformat__ = "restructuredtext en"
23
24 import sys
25 from os.path import abspath
26 from inspect import (getargspec, isdatadescriptor, isfunction, ismethod,
27 ismethoddescriptor, isclass, isbuiltin, ismodule)
28
29 from astroid.node_classes import CONST_CLS
30 from astroid.nodes import (Module, Class, Const, const_factory, From,
31 Function, EmptyNode, Name, Arguments)
32 from astroid.bases import BUILTINS, Generator
33 from astroid.manager import AstroidManager
34 MANAGER = AstroidManager()
35
36 _CONSTANTS = tuple(CONST_CLS) # the keys of CONST_CLS eg python builtin types
37
38 def _io_discrepancy(member):
39 # _io module names itself `io`: http://bugs.python.org/issue18602
40 member_self = getattr(member, '__self__', None)
41 return (member_self and
42 ismodule(member_self) and
43 member_self.__name__ == '_io' and
44 member.__module__ == 'io')
45
46 def _attach_local_node(parent, node, name):
47 node.name = name # needed by add_local_node
48 parent.add_local_node(node)
49
50 _marker = object()
51
52 def attach_dummy_node(node, name, object=_marker):
53 """create a dummy node and register it in the locals of the given
54 node with the specified name
55 """
56 enode = EmptyNode()
57 enode.object = object
58 _attach_local_node(node, enode, name)
59
60 EmptyNode.has_underlying_object = lambda self: self.object is not _marker
61
62 def attach_const_node(node, name, value):
63 """create a Const node and register it in the locals of the given
64 node with the specified name
65 """
66 if not name in node.special_attributes:
67 _attach_local_node(node, const_factory(value), name)
68
69 def attach_import_node(node, modname, membername):
70 """create a From node and register it in the locals of the given
71 node with the specified name
72 """
73 from_node = From(modname, [(membername, None)])
74 _attach_local_node(node, from_node, membername)
75
76
77 def build_module(name, doc=None):
78 """create and initialize a astroid Module node"""
79 node = Module(name, doc, pure_python=False)
80 node.package = False
81 node.parent = None
82 return node
83
84 def build_class(name, basenames=(), doc=None):
85 """create and initialize a astroid Class node"""
86 node = Class(name, doc)
87 for base in basenames:
88 basenode = Name()
89 basenode.name = base
90 node.bases.append(basenode)
91 basenode.parent = node
92 return node
93
94 def build_function(name, args=None, defaults=None, flag=0, doc=None):
95 """create and initialize a astroid Function node"""
96 args, defaults = args or [], defaults or []
97 # first argument is now a list of decorators
98 func = Function(name, doc)
99 func.args = argsnode = Arguments()
100 argsnode.args = []
101 for arg in args:
102 argsnode.args.append(Name())
103 argsnode.args[-1].name = arg
104 argsnode.args[-1].parent = argsnode
105 argsnode.defaults = []
106 for default in defaults:
107 argsnode.defaults.append(const_factory(default))
108 argsnode.defaults[-1].parent = argsnode
109 argsnode.kwarg = None
110 argsnode.vararg = None
111 argsnode.parent = func
112 if args:
113 register_arguments(func)
114 return func
115
116
117 def build_from_import(fromname, names):
118 """create and initialize an astroid From import statement"""
119 return From(fromname, [(name, None) for name in names])
120
121 def register_arguments(func, args=None):
122 """add given arguments to local
123
124 args is a list that may contains nested lists
125 (i.e. def func(a, (b, c, d)): ...)
126 """
127 if args is None:
128 args = func.args.args
129 if func.args.vararg:
130 func.set_local(func.args.vararg, func.args)
131 if func.args.kwarg:
132 func.set_local(func.args.kwarg, func.args)
133 for arg in args:
134 if isinstance(arg, Name):
135 func.set_local(arg.name, arg)
136 else:
137 register_arguments(func, arg.elts)
138
139 def object_build_class(node, member, localname):
140 """create astroid for a living class object"""
141 basenames = [base.__name__ for base in member.__bases__]
142 return _base_class_object_build(node, member, basenames,
143 localname=localname)
144
145 def object_build_function(node, member, localname):
146 """create astroid for a living function object"""
147 args, varargs, varkw, defaults = getargspec(member)
148 if varargs is not None:
149 args.append(varargs)
150 if varkw is not None:
151 args.append(varkw)
152 func = build_function(getattr(member, '__name__', None) or localname, args,
153 defaults, member.func_code.co_flags, member.__doc__)
154 node.add_local_node(func, localname)
155
156 def object_build_datadescriptor(node, member, name):
157 """create astroid for a living data descriptor object"""
158 return _base_class_object_build(node, member, [], name)
159
160 def object_build_methoddescriptor(node, member, localname):
161 """create astroid for a living method descriptor object"""
162 # FIXME get arguments ?
163 func = build_function(getattr(member, '__name__', None) or localname,
164 doc=member.__doc__)
165 # set node's arguments to None to notice that we have no information, not
166 # and empty argument list
167 func.args.args = None
168 node.add_local_node(func, localname)
169
170 def _base_class_object_build(node, member, basenames, name=None, localname=None) :
171 """create astroid for a living class object, with a given set of base names
172 (e.g. ancestors)
173 """
174 klass = build_class(name or getattr(member, '__name__', None) or localname,
175 basenames, member.__doc__)
176 klass._newstyle = isinstance(member, type)
177 node.add_local_node(klass, localname)
178 try:
179 # limit the instantiation trick since it's too dangerous
180 # (such as infinite test execution...)
181 # this at least resolves common case such as Exception.args,
182 # OSError.errno
183 if issubclass(member, Exception):
184 instdict = member().__dict__
185 else:
186 raise TypeError
187 except:
188 pass
189 else:
190 for name, obj in instdict.items():
191 valnode = EmptyNode()
192 valnode.object = obj
193 valnode.parent = klass
194 valnode.lineno = 1
195 klass.instance_attrs[name] = [valnode]
196 return klass
197
198
199
200
201 class InspectBuilder(object):
202 """class for building nodes from living object
203
204 this is actually a really minimal representation, including only Module,
205 Function and Class nodes and some others as guessed.
206 """
207
208 # astroid from living objects ############################################## #
209
210 def __init__(self):
211 self._done = {}
212 self._module = None
213
214 def inspect_build(self, module, modname=None, path=None):
215 """build astroid from a living module (i.e. using inspect)
216 this is used when there is no python source code available (either
217 because it's a built-in module or because the .py is not available)
218 """
219 self._module = module
220 if modname is None:
221 modname = module.__name__
222 try:
223 node = build_module(modname, module.__doc__)
224 except AttributeError:
225 # in jython, java modules have no __doc__ (see #109562)
226 node = build_module(modname)
227 node.file = node.path = path and abspath(path) or path
228 node.name = modname
229 MANAGER.cache_module(node)
230 node.package = hasattr(module, '__path__')
231 self._done = {}
232 self.object_build(node, module)
233 return node
234
235 def object_build(self, node, obj):
236 """recursive method which create a partial ast from real objects
237 (only function, class, and method are handled)
238 """
239 if obj in self._done:
240 return self._done[obj]
241 self._done[obj] = node
242 for name in dir(obj):
243 try:
244 member = getattr(obj, name)
245 except AttributeError:
246 # damned ExtensionClass.Base, I know you're there !
247 attach_dummy_node(node, name)
248 continue
249 if ismethod(member):
250 member = member.im_func
251 if isfunction(member):
252 # verify this is not an imported function
253 filename = getattr(member.func_code, 'co_filename', None)
254 if filename is None:
255 assert isinstance(member, object)
256 object_build_methoddescriptor(node, member, name)
257 elif filename != getattr(self._module, '__file__', None):
258 attach_dummy_node(node, name, member)
259 else:
260 object_build_function(node, member, name)
261 elif isbuiltin(member):
262 if (not _io_discrepancy(member) and
263 self.imported_member(node, member, name)):
264 #if obj is object:
265 # print 'skippp', obj, name, member
266 continue
267 object_build_methoddescriptor(node, member, name)
268 elif isclass(member):
269 if self.imported_member(node, member, name):
270 continue
271 if member in self._done:
272 class_node = self._done[member]
273 if not class_node in node.locals.get(name, ()):
274 node.add_local_node(class_node, name)
275 else:
276 class_node = object_build_class(node, member, name)
277 # recursion
278 self.object_build(class_node, member)
279 if name == '__class__' and class_node.parent is None:
280 class_node.parent = self._done[self._module]
281 elif ismethoddescriptor(member):
282 assert isinstance(member, object)
283 object_build_methoddescriptor(node, member, name)
284 elif isdatadescriptor(member):
285 assert isinstance(member, object)
286 object_build_datadescriptor(node, member, name)
287 elif type(member) in _CONSTANTS:
288 attach_const_node(node, name, member)
289 else:
290 # create an empty node so that the name is actually defined
291 attach_dummy_node(node, name, member)
292
293 def imported_member(self, node, member, name):
294 """verify this is not an imported class or handle it"""
295 # /!\ some classes like ExtensionClass doesn't have a __module__
296 # attribute ! Also, this may trigger an exception on badly built module
297 # (see http://www.logilab.org/ticket/57299 for instance)
298 try:
299 modname = getattr(member, '__module__', None)
300 except:
301 # XXX use logging
302 print 'unexpected error while building astroid from living object'
303 import traceback
304 traceback.print_exc()
305 modname = None
306 if modname is None:
307 if name in ('__new__', '__subclasshook__'):
308 # Python 2.5.1 (r251:54863, Sep 1 2010, 22:03:14)
309 # >>> print object.__new__.__module__
310 # None
311 modname = BUILTINS
312 else:
313 attach_dummy_node(node, name, member)
314 return True
315 if {'gtk': 'gtk._gtk'}.get(modname, modname) != self._module.__name__:
316 # check if it sounds valid and then add an import node, else use a
317 # dummy node
318 try:
319 getattr(sys.modules[modname], name)
320 except (KeyError, AttributeError):
321 attach_dummy_node(node, name, member)
322 else:
323 attach_import_node(node, modname, name)
324 return True
325 return False
326
327
328 ### astroid bootstrapping ######################################################
329 Astroid_BUILDER = InspectBuilder()
330
331 _CONST_PROXY = {}
332 def astroid_bootstrapping():
333 """astroid boot strapping the builtins module"""
334 # this boot strapping is necessary since we need the Const nodes to
335 # inspect_build builtins, and then we can proxy Const
336 from logilab.common.compat import builtins
337 astroid_builtin = Astroid_BUILDER.inspect_build(builtins)
338 for cls, node_cls in CONST_CLS.items():
339 if cls is type(None):
340 proxy = build_class('NoneType')
341 proxy.parent = astroid_builtin
342 else:
343 proxy = astroid_builtin.getattr(cls.__name__)[0]
344 if cls in (dict, list, set, tuple):
345 node_cls._proxied = proxy
346 else:
347 _CONST_PROXY[cls] = proxy
348
349 astroid_bootstrapping()
350
351 # TODO : find a nicer way to handle this situation;
352 # However __proxied introduced an
353 # infinite recursion (see https://bugs.launchpad.net/pylint/+bug/456870)
354 def _set_proxied(const):
355 return _CONST_PROXY[const.value.__class__]
356 Const._proxied = property(_set_proxied)
357
358 from types import GeneratorType
359 Generator._proxied = Class(GeneratorType.__name__, GeneratorType.__doc__)
360 Astroid_BUILDER.object_build(Generator._proxied, GeneratorType)
361
OLDNEW
« no previous file with comments | « third_party/logilab/astroid/protocols.py ('k') | third_party/logilab/astroid/rebuilder.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698