| Index: third_party/logilab/astng/builder.py
|
| diff --git a/third_party/logilab/astroid/builder.py b/third_party/logilab/astng/builder.py
|
| similarity index 69%
|
| rename from third_party/logilab/astroid/builder.py
|
| rename to third_party/logilab/astng/builder.py
|
| index 692016a3677f19f617f5c0eb5124279a1a50bfda..9309793d7d5aedc69094d85fdc0e2f47b8b44b51 100644
|
| --- a/third_party/logilab/astroid/builder.py
|
| +++ b/third_party/logilab/astng/builder.py
|
| @@ -1,38 +1,40 @@
|
| -# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
|
| +# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
|
| # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
|
| +# copyright 2003-2010 Sylvain Thenault, all rights reserved.
|
| +# contact mailto:thenault@gmail.com
|
| #
|
| -# This file is part of astroid.
|
| +# This file is part of logilab-astng.
|
| #
|
| -# astroid is free software: you can redistribute it and/or modify it
|
| +# logilab-astng is free software: you can redistribute it and/or modify it
|
| # under the terms of the GNU Lesser General Public License as published by the
|
| # Free Software Foundation, either version 2.1 of the License, or (at your
|
| # option) any later version.
|
| #
|
| -# astroid is distributed in the hope that it will be useful, but
|
| +# logilab-astng is distributed in the hope that it will be useful, but
|
| # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
| # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
| # for more details.
|
| #
|
| # You should have received a copy of the GNU Lesser General Public License along
|
| -# with astroid. If not, see <http://www.gnu.org/licenses/>.
|
| -"""The AstroidBuilder makes astroid from living object and / or from _ast
|
| +# with logilab-astng. If not, see <http://www.gnu.org/licenses/>.
|
| +"""The ASTNGBuilder makes astng from living object and / or from _ast
|
|
|
| The builder is not thread safe and can't be used to parse different sources
|
| at the same time.
|
| """
|
| -from __future__ import with_statement
|
|
|
| __docformat__ = "restructuredtext en"
|
|
|
| -import sys
|
| -from os.path import splitext, basename, exists, abspath
|
| +import sys, re
|
| +from os.path import splitext, basename, dirname, exists, abspath
|
|
|
| -from astroid.exceptions import AstroidBuildingException, InferenceError
|
| -from astroid.raw_building import InspectBuilder
|
| -from astroid.rebuilder import TreeRebuilder
|
| -from astroid.manager import AstroidManager
|
| -from astroid.bases import YES, Instance
|
| -from astroid.modutils import modpath_from_file
|
| +from logilab.common.modutils import modpath_from_file
|
| +
|
| +from logilab.astng.exceptions import ASTNGBuildingException, InferenceError
|
| +from logilab.astng.raw_building import InspectBuilder
|
| +from logilab.astng.rebuilder import TreeRebuilder
|
| +from logilab.astng.manager import ASTNGManager
|
| +from logilab.astng.bases import YES, Instance
|
|
|
| from _ast import PyCF_ONLY_AST
|
| def parse(string):
|
| @@ -42,21 +44,21 @@ if sys.version_info >= (3, 0):
|
| from tokenize import detect_encoding
|
|
|
| def open_source_file(filename):
|
| - with open(filename, 'rb') as byte_stream:
|
| - encoding = detect_encoding(byte_stream.readline)[0]
|
| - stream = open(filename, 'rU', encoding=encoding)
|
| + byte_stream = open(filename, 'bU')
|
| + encoding = detect_encoding(byte_stream.readline)[0]
|
| + stream = open(filename, 'U', encoding=encoding)
|
| try:
|
| data = stream.read()
|
| - except UnicodeError: # wrong encodingg
|
| + except UnicodeError, uex: # wrong encodingg
|
| # detect_encoding returns utf-8 if no encoding specified
|
| msg = 'Wrong (%s) or no encoding specified' % encoding
|
| - raise AstroidBuildingException(msg)
|
| + raise ASTNGBuildingException(msg)
|
| return stream, encoding, data
|
|
|
| else:
|
| import re
|
|
|
| - _ENCODING_RGX = re.compile(r"\s*#+.*coding[:=]\s*([-\w.]+)")
|
| + _ENCODING_RGX = re.compile("\s*#+.*coding[:=]\s*([-\w.]+)")
|
|
|
| def _guess_encoding(string):
|
| """get encoding from a python file as string or return None if not found
|
| @@ -79,17 +81,17 @@ else:
|
|
|
| # ast NG builder ##############################################################
|
|
|
| -MANAGER = AstroidManager()
|
| +MANAGER = ASTNGManager()
|
|
|
| -class AstroidBuilder(InspectBuilder):
|
| - """provide astroid building methods"""
|
| +class ASTNGBuilder(InspectBuilder):
|
| + """provide astng building methods"""
|
| + rebuilder = TreeRebuilder()
|
|
|
| def __init__(self, manager=None):
|
| - InspectBuilder.__init__(self)
|
| self._manager = manager or MANAGER
|
|
|
| def module_build(self, module, modname=None):
|
| - """build an astroid from a living module instance
|
| + """build an astng from a living module instance
|
| """
|
| node = None
|
| path = getattr(module, '__file__', None)
|
| @@ -101,59 +103,46 @@ class AstroidBuilder(InspectBuilder):
|
| # this is a built-in module
|
| # get a partial representation by introspection
|
| node = self.inspect_build(module, modname=modname, path=path)
|
| - # we have to handle transformation by ourselves since the rebuilder
|
| - # isn't called for builtin nodes
|
| - #
|
| - # XXX it's then only called for Module nodes, not for underlying
|
| - # nodes
|
| - node = self._manager.transform(node)
|
| return node
|
|
|
| def file_build(self, path, modname=None):
|
| - """build astroid from a source code file (i.e. from an ast)
|
| + """build astng from a source code file (i.e. from an ast)
|
|
|
| path is expected to be a python source file
|
| """
|
| try:
|
| - _, encoding, data = open_source_file(path)
|
| + stream, encoding, data = open_source_file(path)
|
| except IOError, exc:
|
| msg = 'Unable to load file %r (%s)' % (path, exc)
|
| - raise AstroidBuildingException(msg)
|
| + raise ASTNGBuildingException(msg)
|
| except SyntaxError, exc: # py3k encoding specification error
|
| - raise AstroidBuildingException(exc)
|
| + raise ASTNGBuildingException(exc)
|
| except LookupError, exc: # unknown encoding
|
| - raise AstroidBuildingException(exc)
|
| + raise ASTNGBuildingException(exc)
|
| # get module name if necessary
|
| if modname is None:
|
| try:
|
| modname = '.'.join(modpath_from_file(path))
|
| except ImportError:
|
| modname = splitext(basename(path))[0]
|
| - # build astroid representation
|
| - module = self._data_build(data, modname, path)
|
| - return self._post_build(module, encoding)
|
| + # build astng representation
|
| + node = self.string_build(data, modname, path)
|
| + node.file_encoding = encoding
|
| + return node
|
|
|
| def string_build(self, data, modname='', path=None):
|
| - """build astroid from source code string and return rebuilded astroid"""
|
| + """build astng from source code string and return rebuilded astng"""
|
| module = self._data_build(data, modname, path)
|
| - module.file_bytes = data.encode('utf-8')
|
| - return self._post_build(module, 'utf-8')
|
| -
|
| - def _post_build(self, module, encoding):
|
| - """handles encoding and delayed nodes
|
| - after a module has been built
|
| - """
|
| - module.file_encoding = encoding
|
| - self._manager.cache_module(module)
|
| + self._manager.astng_cache[module.name] = module
|
| # post tree building steps after we stored the module in the cache:
|
| for from_node in module._from_nodes:
|
| - if from_node.modname == '__future__':
|
| - for symbol, _ in from_node.names:
|
| - module.future_imports.add(symbol)
|
| self.add_from_names_to_locals(from_node)
|
| # handle delayed assattr nodes
|
| for delayed in module._delayed_assattr:
|
| self.delayed_assattr(delayed)
|
| + if modname:
|
| + for transformer in self._manager.transformers:
|
| + transformer(module)
|
| return module
|
|
|
| def _data_build(self, data, modname, path):
|
| @@ -169,11 +158,11 @@ class AstroidBuilder(InspectBuilder):
|
| package = True
|
| else:
|
| package = path and path.find('__init__.py') > -1 or False
|
| - rebuilder = TreeRebuilder(self._manager)
|
| - module = rebuilder.visit_module(node, modname, package)
|
| + self.rebuilder.init()
|
| + module = self.rebuilder.visit_module(node, modname, package)
|
| module.file = module.path = node_file
|
| - module._from_nodes = rebuilder._from_nodes
|
| - module._delayed_assattr = rebuilder._delayed_assattr
|
| + module._from_nodes = self.rebuilder._from_nodes
|
| + module._delayed_assattr = self.rebuilder._delayed_assattr
|
| return module
|
|
|
| def add_from_names_to_locals(self, node):
|
| @@ -187,8 +176,8 @@ class AstroidBuilder(InspectBuilder):
|
| for (name, asname) in node.names:
|
| if name == '*':
|
| try:
|
| - imported = node.do_import_module()
|
| - except InferenceError:
|
| + imported = node.root().import_module(node.modname)
|
| + except ASTNGBuildingException:
|
| continue
|
| for name in imported.wildcard_import_names():
|
| node.parent.set_local(name, node)
|
|
|