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