OLD | NEW |
| (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, 'rU', 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 _, encoding, data = open_source_file(path) | |
119 except IOError, exc: | |
120 msg = 'Unable to load file %r (%s)' % (path, exc) | |
121 raise AstroidBuildingException(msg) | |
122 except SyntaxError, exc: # py3k encoding specification error | |
123 raise AstroidBuildingException(exc) | |
124 except LookupError, exc: # unknown encoding | |
125 raise AstroidBuildingException(exc) | |
126 # get module name if necessary | |
127 if modname is None: | |
128 try: | |
129 modname = '.'.join(modpath_from_file(path)) | |
130 except ImportError: | |
131 modname = splitext(basename(path))[0] | |
132 # build astroid representation | |
133 module = self._data_build(data, modname, path) | |
134 return self._post_build(module, encoding) | |
135 | |
136 def string_build(self, data, modname='', path=None): | |
137 """build astroid from source code string and return rebuilded astroid""" | |
138 module = self._data_build(data, modname, path) | |
139 module.file_bytes = data.encode('utf-8') | |
140 return self._post_build(module, 'utf-8') | |
141 | |
142 def _post_build(self, module, encoding): | |
143 """handles encoding and delayed nodes | |
144 after a module has been built | |
145 """ | |
146 module.file_encoding = encoding | |
147 self._manager.cache_module(module) | |
148 # post tree building steps after we stored the module in the cache: | |
149 for from_node in module._from_nodes: | |
150 if from_node.modname == '__future__': | |
151 for symbol, _ in from_node.names: | |
152 module.future_imports.add(symbol) | |
153 self.add_from_names_to_locals(from_node) | |
154 # handle delayed assattr nodes | |
155 for delayed in module._delayed_assattr: | |
156 self.delayed_assattr(delayed) | |
157 return module | |
158 | |
159 def _data_build(self, data, modname, path): | |
160 """build tree node from data and add some informations""" | |
161 # this method could be wrapped with a pickle/cache function | |
162 node = parse(data + '\n') | |
163 if path is not None: | |
164 node_file = abspath(path) | |
165 else: | |
166 node_file = '<?>' | |
167 if modname.endswith('.__init__'): | |
168 modname = modname[:-9] | |
169 package = True | |
170 else: | |
171 package = path and path.find('__init__.py') > -1 or False | |
172 rebuilder = TreeRebuilder(self._manager) | |
173 module = rebuilder.visit_module(node, modname, package) | |
174 module.file = module.path = node_file | |
175 module._from_nodes = rebuilder._from_nodes | |
176 module._delayed_assattr = rebuilder._delayed_assattr | |
177 return module | |
178 | |
179 def add_from_names_to_locals(self, node): | |
180 """store imported names to the locals; | |
181 resort the locals if coming from a delayed node | |
182 """ | |
183 | |
184 _key_func = lambda node: node.fromlineno | |
185 def sort_locals(my_list): | |
186 my_list.sort(key=_key_func) | |
187 for (name, asname) in node.names: | |
188 if name == '*': | |
189 try: | |
190 imported = node.do_import_module() | |
191 except InferenceError: | |
192 continue | |
193 for name in imported.wildcard_import_names(): | |
194 node.parent.set_local(name, node) | |
195 sort_locals(node.parent.scope().locals[name]) | |
196 else: | |
197 node.parent.set_local(asname or name, node) | |
198 sort_locals(node.parent.scope().locals[asname or name]) | |
199 | |
200 def delayed_assattr(self, node): | |
201 """visit a AssAttr node -> add name to locals, handle members | |
202 definition | |
203 """ | |
204 try: | |
205 frame = node.frame() | |
206 for infered in node.expr.infer(): | |
207 if infered is YES: | |
208 continue | |
209 try: | |
210 if infered.__class__ is Instance: | |
211 infered = infered._proxied | |
212 iattrs = infered.instance_attrs | |
213 elif isinstance(infered, Instance): | |
214 # Const, Tuple, ... we may be wrong, may be not, but | |
215 # anyway we don't want to pollute builtin's namespace | |
216 continue | |
217 elif infered.is_function: | |
218 iattrs = infered.instance_attrs | |
219 else: | |
220 iattrs = infered.locals | |
221 except AttributeError: | |
222 # XXX log error | |
223 #import traceback | |
224 #traceback.print_exc() | |
225 continue | |
226 values = iattrs.setdefault(node.attrname, []) | |
227 if node in values: | |
228 continue | |
229 # get assign in __init__ first XXX useful ? | |
230 if frame.name == '__init__' and values and not \ | |
231 values[0].frame().name == '__init__': | |
232 values.insert(0, node) | |
233 else: | |
234 values.append(node) | |
235 except InferenceError: | |
236 pass | |
237 | |
OLD | NEW |