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

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

Issue 1920403002: [content/test/gpu] Run pylint check of gpu tests in unittest instead of PRESUBMIT (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Update path to LICENSE.txt of logilab/README.chromium Created 4 years, 7 months 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
OLDNEW
(Empty)
1 # copyright 2003-2014 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 """The AstroidBuilder makes astroid from living object and / or from _ast
19
20 The builder is not thread safe and can't be used to parse different sources
21 at the same time.
22 """
23 from __future__ import with_statement
24
25 __docformat__ = "restructuredtext en"
26
27 import sys
28 from os.path import splitext, basename, exists, abspath
29
30 from astroid.exceptions import AstroidBuildingException, InferenceError
31 from astroid.raw_building import InspectBuilder
32 from astroid.rebuilder import TreeRebuilder
33 from astroid.manager import AstroidManager
34 from astroid.bases import YES, Instance
35 from astroid.modutils import modpath_from_file
36
37 from _ast import PyCF_ONLY_AST
38 def parse(string):
39 return compile(string, "<string>", 'exec', PyCF_ONLY_AST)
40
41 if sys.version_info >= (3, 0):
42 from tokenize import detect_encoding
43
44 def open_source_file(filename):
45 with open(filename, 'rb') as byte_stream:
46 encoding = detect_encoding(byte_stream.readline)[0]
47 stream = open(filename, 'r', newline=None, encoding=encoding)
48 try:
49 data = stream.read()
50 except UnicodeError: # wrong encodingg
51 # detect_encoding returns utf-8 if no encoding specified
52 msg = 'Wrong (%s) or no encoding specified' % encoding
53 raise AstroidBuildingException(msg)
54 return stream, encoding, data
55
56 else:
57 import re
58
59 _ENCODING_RGX = re.compile(r"\s*#+.*coding[:=]\s*([-\w.]+)")
60
61 def _guess_encoding(string):
62 """get encoding from a python file as string or return None if not found
63 """
64 # check for UTF-8 byte-order mark
65 if string.startswith('\xef\xbb\xbf'):
66 return 'UTF-8'
67 for line in string.split('\n', 2)[:2]:
68 # check for encoding declaration
69 match = _ENCODING_RGX.match(line)
70 if match is not None:
71 return match.group(1)
72
73 def open_source_file(filename):
74 """get data for parsing a file"""
75 stream = open(filename, 'U')
76 data = stream.read()
77 encoding = _guess_encoding(data)
78 return stream, encoding, data
79
80 # ast NG builder ##############################################################
81
82 MANAGER = AstroidManager()
83
84 class AstroidBuilder(InspectBuilder):
85 """provide astroid building methods"""
86
87 def __init__(self, manager=None):
88 InspectBuilder.__init__(self)
89 self._manager = manager or MANAGER
90
91 def module_build(self, module, modname=None):
92 """build an astroid from a living module instance
93 """
94 node = None
95 path = getattr(module, '__file__', None)
96 if path is not None:
97 path_, ext = splitext(module.__file__)
98 if ext in ('.py', '.pyc', '.pyo') and exists(path_ + '.py'):
99 node = self.file_build(path_ + '.py', modname)
100 if node is None:
101 # this is a built-in module
102 # get a partial representation by introspection
103 node = self.inspect_build(module, modname=modname, path=path)
104 # we have to handle transformation by ourselves since the rebuilder
105 # isn't called for builtin nodes
106 #
107 # XXX it's then only called for Module nodes, not for underlying
108 # nodes
109 node = self._manager.transform(node)
110 return node
111
112 def file_build(self, path, modname=None):
113 """build astroid from a source code file (i.e. from an ast)
114
115 path is expected to be a python source file
116 """
117 try:
118 stream, encoding, data = open_source_file(path)
119 except IOError as exc:
120 msg = 'Unable to load file %r (%s)' % (path, exc)
121 raise AstroidBuildingException(msg)
122 except SyntaxError as exc: # py3k encoding specification error
123 raise AstroidBuildingException(exc)
124 except LookupError as exc: # unknown encoding
125 raise AstroidBuildingException(exc)
126 with stream:
127 # get module name if necessary
128 if modname is None:
129 try:
130 modname = '.'.join(modpath_from_file(path))
131 except ImportError:
132 modname = splitext(basename(path))[0]
133 # build astroid representation
134 module = self._data_build(data, modname, path)
135 return self._post_build(module, encoding)
136
137 def string_build(self, data, modname='', path=None):
138 """build astroid from source code string and return rebuilded astroid"""
139 module = self._data_build(data, modname, path)
140 module.file_bytes = data.encode('utf-8')
141 return self._post_build(module, 'utf-8')
142
143 def _post_build(self, module, encoding):
144 """handles encoding and delayed nodes
145 after a module has been built
146 """
147 module.file_encoding = encoding
148 self._manager.cache_module(module)
149 # post tree building steps after we stored the module in the cache:
150 for from_node in module._from_nodes:
151 if from_node.modname == '__future__':
152 for symbol, _ in from_node.names:
153 module.future_imports.add(symbol)
154 self.add_from_names_to_locals(from_node)
155 # handle delayed assattr nodes
156 for delayed in module._delayed_assattr:
157 self.delayed_assattr(delayed)
158 return module
159
160 def _data_build(self, data, modname, path):
161 """build tree node from data and add some informations"""
162 # this method could be wrapped with a pickle/cache function
163 try:
164 node = parse(data + '\n')
165 except TypeError as exc:
166 raise AstroidBuildingException(exc)
167 if path is not None:
168 node_file = abspath(path)
169 else:
170 node_file = '<?>'
171 if modname.endswith('.__init__'):
172 modname = modname[:-9]
173 package = True
174 else:
175 package = path and path.find('__init__.py') > -1 or False
176 rebuilder = TreeRebuilder(self._manager)
177 module = rebuilder.visit_module(node, modname, node_file, package)
178 module._from_nodes = rebuilder._from_nodes
179 module._delayed_assattr = rebuilder._delayed_assattr
180 return module
181
182 def add_from_names_to_locals(self, node):
183 """store imported names to the locals;
184 resort the locals if coming from a delayed node
185 """
186
187 _key_func = lambda node: node.fromlineno
188 def sort_locals(my_list):
189 my_list.sort(key=_key_func)
190 for (name, asname) in node.names:
191 if name == '*':
192 try:
193 imported = node.do_import_module()
194 except InferenceError:
195 continue
196 for name in imported.wildcard_import_names():
197 node.parent.set_local(name, node)
198 sort_locals(node.parent.scope().locals[name])
199 else:
200 node.parent.set_local(asname or name, node)
201 sort_locals(node.parent.scope().locals[asname or name])
202
203 def delayed_assattr(self, node):
204 """visit a AssAttr node -> add name to locals, handle members
205 definition
206 """
207 try:
208 frame = node.frame()
209 for infered in node.expr.infer():
210 if infered is YES:
211 continue
212 try:
213 if infered.__class__ is Instance:
214 infered = infered._proxied
215 iattrs = infered.instance_attrs
216 elif isinstance(infered, Instance):
217 # Const, Tuple, ... we may be wrong, may be not, but
218 # anyway we don't want to pollute builtin's namespace
219 continue
220 elif infered.is_function:
221 iattrs = infered.instance_attrs
222 else:
223 iattrs = infered.locals
224 except AttributeError:
225 # XXX log error
226 #import traceback
227 #traceback.print_exc()
228 continue
229 values = iattrs.setdefault(node.attrname, [])
230 if node in values:
231 continue
232 # get assign in __init__ first XXX useful ?
233 if frame.name == '__init__' and values and not \
234 values[0].frame().name == '__init__':
235 values.insert(0, node)
236 else:
237 values.append(node)
238 except InferenceError:
239 pass
240
OLDNEW
« no previous file with comments | « third_party/logilab/logilab/astroid/brain/pysix_moves.py ('k') | third_party/logilab/logilab/astroid/exceptions.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698