| OLD | NEW |
| 1 # copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. | 1 # copyright 2003-2013 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 # | 3 # |
| 4 # This file is part of astroid. | 4 # This file is part of astroid. |
| 5 # | 5 # |
| 6 # astroid is free software: you can redistribute it and/or modify it | 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 | 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 | 8 # Free Software Foundation, either version 2.1 of the License, or (at your |
| 9 # option) any later version. | 9 # option) any later version. |
| 10 # | 10 # |
| 11 # astroid is distributed in the hope that it will be useful, but | 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 | 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 | 13 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 14 # for more details. | 14 # for more details. |
| 15 # | 15 # |
| 16 # You should have received a copy of the GNU Lesser General Public License along | 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/>. | 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 | 18 """this module contains a set of functions to create astroid trees from scratch |
| 19 (build_* functions) or from living object (object_build_* functions) | 19 (build_* functions) or from living object (object_build_* functions) |
| 20 """ | 20 """ |
| 21 | 21 |
| 22 __docformat__ = "restructuredtext en" | 22 __docformat__ = "restructuredtext en" |
| 23 | 23 |
| 24 import sys | 24 import sys |
| 25 from os.path import abspath | 25 from os.path import abspath |
| 26 from inspect import (getargspec, isdatadescriptor, isfunction, ismethod, | 26 from inspect import (getargspec, isdatadescriptor, isfunction, ismethod, |
| 27 ismethoddescriptor, isclass, isbuiltin, ismodule) | 27 ismethoddescriptor, isclass, isbuiltin, ismodule) |
| 28 import six |
| 28 | 29 |
| 29 from astroid.node_classes import CONST_CLS | 30 from astroid.node_classes import CONST_CLS |
| 30 from astroid.nodes import (Module, Class, Const, const_factory, From, | 31 from astroid.nodes import (Module, Class, Const, const_factory, From, |
| 31 Function, EmptyNode, Name, Arguments) | 32 Function, EmptyNode, Name, Arguments) |
| 32 from astroid.bases import BUILTINS, Generator | 33 from astroid.bases import BUILTINS, Generator |
| 33 from astroid.manager import AstroidManager | 34 from astroid.manager import AstroidManager |
| 34 MANAGER = AstroidManager() | 35 MANAGER = AstroidManager() |
| 35 | 36 |
| 36 _CONSTANTS = tuple(CONST_CLS) # the keys of CONST_CLS eg python builtin types | 37 _CONSTANTS = tuple(CONST_CLS) # the keys of CONST_CLS eg python builtin types |
| 37 | 38 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 50 _marker = object() | 51 _marker = object() |
| 51 | 52 |
| 52 def attach_dummy_node(node, name, object=_marker): | 53 def attach_dummy_node(node, name, object=_marker): |
| 53 """create a dummy node and register it in the locals of the given | 54 """create a dummy node and register it in the locals of the given |
| 54 node with the specified name | 55 node with the specified name |
| 55 """ | 56 """ |
| 56 enode = EmptyNode() | 57 enode = EmptyNode() |
| 57 enode.object = object | 58 enode.object = object |
| 58 _attach_local_node(node, enode, name) | 59 _attach_local_node(node, enode, name) |
| 59 | 60 |
| 60 EmptyNode.has_underlying_object = lambda self: self.object is not _marker | 61 def _has_underlying_object(self): |
| 62 return hasattr(self, 'object') and self.object is not _marker |
| 63 |
| 64 EmptyNode.has_underlying_object = _has_underlying_object |
| 61 | 65 |
| 62 def attach_const_node(node, name, value): | 66 def attach_const_node(node, name, value): |
| 63 """create a Const node and register it in the locals of the given | 67 """create a Const node and register it in the locals of the given |
| 64 node with the specified name | 68 node with the specified name |
| 65 """ | 69 """ |
| 66 if not name in node.special_attributes: | 70 if not name in node.special_attributes: |
| 67 _attach_local_node(node, const_factory(value), name) | 71 _attach_local_node(node, const_factory(value), name) |
| 68 | 72 |
| 69 def attach_import_node(node, modname, membername): | 73 def attach_import_node(node, modname, membername): |
| 70 """create a From node and register it in the locals of the given | 74 """create a From node and register it in the locals of the given |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 return self._done[obj] | 244 return self._done[obj] |
| 241 self._done[obj] = node | 245 self._done[obj] = node |
| 242 for name in dir(obj): | 246 for name in dir(obj): |
| 243 try: | 247 try: |
| 244 member = getattr(obj, name) | 248 member = getattr(obj, name) |
| 245 except AttributeError: | 249 except AttributeError: |
| 246 # damned ExtensionClass.Base, I know you're there ! | 250 # damned ExtensionClass.Base, I know you're there ! |
| 247 attach_dummy_node(node, name) | 251 attach_dummy_node(node, name) |
| 248 continue | 252 continue |
| 249 if ismethod(member): | 253 if ismethod(member): |
| 250 member = member.im_func | 254 member = six.get_method_function(member) |
| 251 if isfunction(member): | 255 if isfunction(member): |
| 252 # verify this is not an imported function | 256 # verify this is not an imported function |
| 253 filename = getattr(member.func_code, 'co_filename', None) | 257 filename = getattr(six.get_function_code(member), |
| 258 'co_filename', None) |
| 254 if filename is None: | 259 if filename is None: |
| 255 assert isinstance(member, object) | 260 assert isinstance(member, object) |
| 256 object_build_methoddescriptor(node, member, name) | 261 object_build_methoddescriptor(node, member, name) |
| 257 elif filename != getattr(self._module, '__file__', None): | 262 elif filename != getattr(self._module, '__file__', None): |
| 258 attach_dummy_node(node, name, member) | 263 attach_dummy_node(node, name, member) |
| 259 else: | 264 else: |
| 260 object_build_function(node, member, name) | 265 object_build_function(node, member, name) |
| 261 elif isbuiltin(member): | 266 elif isbuiltin(member): |
| 262 if (not _io_discrepancy(member) and | 267 if (not _io_discrepancy(member) and |
| 263 self.imported_member(node, member, name)): | 268 self.imported_member(node, member, name)): |
| 264 #if obj is object: | |
| 265 # print 'skippp', obj, name, member | |
| 266 continue | 269 continue |
| 267 object_build_methoddescriptor(node, member, name) | 270 object_build_methoddescriptor(node, member, name) |
| 268 elif isclass(member): | 271 elif isclass(member): |
| 269 if self.imported_member(node, member, name): | 272 if self.imported_member(node, member, name): |
| 270 continue | 273 continue |
| 271 if member in self._done: | 274 if member in self._done: |
| 272 class_node = self._done[member] | 275 class_node = self._done[member] |
| 273 if not class_node in node.locals.get(name, ()): | 276 if not class_node in node.locals.get(name, ()): |
| 274 node.add_local_node(class_node, name) | 277 node.add_local_node(class_node, name) |
| 275 else: | 278 else: |
| (...skipping 16 matching lines...) Expand all Loading... |
| 292 | 295 |
| 293 def imported_member(self, node, member, name): | 296 def imported_member(self, node, member, name): |
| 294 """verify this is not an imported class or handle it""" | 297 """verify this is not an imported class or handle it""" |
| 295 # /!\ some classes like ExtensionClass doesn't have a __module__ | 298 # /!\ some classes like ExtensionClass doesn't have a __module__ |
| 296 # attribute ! Also, this may trigger an exception on badly built module | 299 # attribute ! Also, this may trigger an exception on badly built module |
| 297 # (see http://www.logilab.org/ticket/57299 for instance) | 300 # (see http://www.logilab.org/ticket/57299 for instance) |
| 298 try: | 301 try: |
| 299 modname = getattr(member, '__module__', None) | 302 modname = getattr(member, '__module__', None) |
| 300 except: | 303 except: |
| 301 # XXX use logging | 304 # XXX use logging |
| 302 print 'unexpected error while building astroid from living object' | 305 print('unexpected error while building astroid from living object') |
| 303 import traceback | 306 import traceback |
| 304 traceback.print_exc() | 307 traceback.print_exc() |
| 305 modname = None | 308 modname = None |
| 306 if modname is None: | 309 if modname is None: |
| 307 if name in ('__new__', '__subclasshook__'): | 310 if name in ('__new__', '__subclasshook__'): |
| 308 # Python 2.5.1 (r251:54863, Sep 1 2010, 22:03:14) | 311 # Python 2.5.1 (r251:54863, Sep 1 2010, 22:03:14) |
| 309 # >>> print object.__new__.__module__ | 312 # >>> print object.__new__.__module__ |
| 310 # None | 313 # None |
| 311 modname = BUILTINS | 314 modname = BUILTINS |
| 312 else: | 315 else: |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 352 # However __proxied introduced an | 355 # However __proxied introduced an |
| 353 # infinite recursion (see https://bugs.launchpad.net/pylint/+bug/456870) | 356 # infinite recursion (see https://bugs.launchpad.net/pylint/+bug/456870) |
| 354 def _set_proxied(const): | 357 def _set_proxied(const): |
| 355 return _CONST_PROXY[const.value.__class__] | 358 return _CONST_PROXY[const.value.__class__] |
| 356 Const._proxied = property(_set_proxied) | 359 Const._proxied = property(_set_proxied) |
| 357 | 360 |
| 358 from types import GeneratorType | 361 from types import GeneratorType |
| 359 Generator._proxied = Class(GeneratorType.__name__, GeneratorType.__doc__) | 362 Generator._proxied = Class(GeneratorType.__name__, GeneratorType.__doc__) |
| 360 Astroid_BUILDER.object_build(Generator._proxied, GeneratorType) | 363 Astroid_BUILDER.object_build(Generator._proxied, GeneratorType) |
| 361 | 364 |
| OLD | NEW |