| 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 handle python protocols for nodes | |
| 21 where it makes sense. | |
| 22 """ | |
| 23 | |
| 24 __doctype__ = "restructuredtext en" | |
| 25 | |
| 26 from logilab.astng.exceptions import InferenceError, NoDefault | |
| 27 from logilab.astng.node_classes import unpack_infer | |
| 28 from logilab.astng.bases import copy_context, \ | |
| 29 raise_if_nothing_infered, yes_if_nothing_infered, Instance, Generator, YES | |
| 30 from logilab.astng.nodes import const_factory | |
| 31 from logilab.astng import nodes | |
| 32 | |
| 33 # unary operations ############################################################ | |
| 34 | |
| 35 def tl_infer_unary_op(self, operator): | |
| 36 if operator == 'not': | |
| 37 return const_factory(not bool(self.elts)) | |
| 38 raise TypeError() # XXX log unsupported operation | |
| 39 nodes.Tuple.infer_unary_op = tl_infer_unary_op | |
| 40 nodes.List.infer_unary_op = tl_infer_unary_op | |
| 41 | |
| 42 | |
| 43 def dict_infer_unary_op(self, operator): | |
| 44 if operator == 'not': | |
| 45 return const_factory(not bool(self.items)) | |
| 46 raise TypeError() # XXX log unsupported operation | |
| 47 nodes.Dict.infer_unary_op = dict_infer_unary_op | |
| 48 | |
| 49 | |
| 50 def const_infer_unary_op(self, operator): | |
| 51 if operator == 'not': | |
| 52 return const_factory(not self.value) | |
| 53 # XXX log potentially raised TypeError | |
| 54 elif operator == '+': | |
| 55 return const_factory(+self.value) | |
| 56 else: # operator == '-': | |
| 57 return const_factory(-self.value) | |
| 58 nodes.Const.infer_unary_op = const_infer_unary_op | |
| 59 | |
| 60 | |
| 61 # binary operations ########################################################### | |
| 62 | |
| 63 BIN_OP_IMPL = {'+': lambda a, b: a + b, | |
| 64 '-': lambda a, b: a - b, | |
| 65 '/': lambda a, b: a / b, | |
| 66 '//': lambda a, b: a // b, | |
| 67 '*': lambda a, b: a * b, | |
| 68 '**': lambda a, b: a ** b, | |
| 69 '%': lambda a, b: a % b, | |
| 70 '&': lambda a, b: a & b, | |
| 71 '|': lambda a, b: a | b, | |
| 72 '^': lambda a, b: a ^ b, | |
| 73 '<<': lambda a, b: a << b, | |
| 74 '>>': lambda a, b: a >> b, | |
| 75 } | |
| 76 for key, impl in BIN_OP_IMPL.items(): | |
| 77 BIN_OP_IMPL[key+'='] = impl | |
| 78 | |
| 79 def const_infer_binary_op(self, operator, other, context): | |
| 80 for other in other.infer(context): | |
| 81 if isinstance(other, nodes.Const): | |
| 82 try: | |
| 83 impl = BIN_OP_IMPL[operator] | |
| 84 | |
| 85 try: | |
| 86 yield const_factory(impl(self.value, other.value)) | |
| 87 except Exception: | |
| 88 # ArithmeticError is not enough: float >> float is a TypeErr
or | |
| 89 # TODO : let pylint know about the problem | |
| 90 pass | |
| 91 except TypeError: | |
| 92 # XXX log TypeError | |
| 93 continue | |
| 94 elif other is YES: | |
| 95 yield other | |
| 96 else: | |
| 97 try: | |
| 98 for val in other.infer_binary_op(operator, self, context): | |
| 99 yield val | |
| 100 except AttributeError: | |
| 101 yield YES | |
| 102 nodes.Const.infer_binary_op = yes_if_nothing_infered(const_infer_binary_op) | |
| 103 | |
| 104 | |
| 105 def tl_infer_binary_op(self, operator, other, context): | |
| 106 for other in other.infer(context): | |
| 107 if isinstance(other, self.__class__) and operator == '+': | |
| 108 node = self.__class__() | |
| 109 elts = [n for elt in self.elts for n in elt.infer(context) | |
| 110 if not n is YES] | |
| 111 elts += [n for elt in other.elts for n in elt.infer(context) | |
| 112 if not n is YES] | |
| 113 node.elts = elts | |
| 114 yield node | |
| 115 elif isinstance(other, nodes.Const) and operator == '*': | |
| 116 if not isinstance(other.value, int): | |
| 117 yield YES | |
| 118 continue | |
| 119 node = self.__class__() | |
| 120 elts = [n for elt in self.elts for n in elt.infer(context) | |
| 121 if not n is YES] * other.value | |
| 122 node.elts = elts | |
| 123 yield node | |
| 124 elif isinstance(other, Instance) and not isinstance(other, nodes.Const): | |
| 125 yield YES | |
| 126 # XXX else log TypeError | |
| 127 nodes.Tuple.infer_binary_op = yes_if_nothing_infered(tl_infer_binary_op) | |
| 128 nodes.List.infer_binary_op = yes_if_nothing_infered(tl_infer_binary_op) | |
| 129 | |
| 130 | |
| 131 def dict_infer_binary_op(self, operator, other, context): | |
| 132 for other in other.infer(context): | |
| 133 if isinstance(other, Instance) and isinstance(other._proxied, nodes.Clas
s): | |
| 134 yield YES | |
| 135 # XXX else log TypeError | |
| 136 nodes.Dict.infer_binary_op = yes_if_nothing_infered(dict_infer_binary_op) | |
| 137 | |
| 138 | |
| 139 # assignment ################################################################## | |
| 140 | |
| 141 """the assigned_stmts method is responsible to return the assigned statement | |
| 142 (e.g. not inferred) according to the assignment type. | |
| 143 | |
| 144 The `asspath` argument is used to record the lhs path of the original node. | |
| 145 For instance if we want assigned statements for 'c' in 'a, (b,c)', asspath | |
| 146 will be [1, 1] once arrived to the Assign node. | |
| 147 | |
| 148 The `context` argument is the current inference context which should be given | |
| 149 to any intermediary inference necessary. | |
| 150 """ | |
| 151 | |
| 152 def _resolve_looppart(parts, asspath, context): | |
| 153 """recursive function to resolve multiple assignments on loops""" | |
| 154 asspath = asspath[:] | |
| 155 index = asspath.pop(0) | |
| 156 for part in parts: | |
| 157 if part is YES: | |
| 158 continue | |
| 159 # XXX handle __iter__ and log potentially detected errors | |
| 160 if not hasattr(part, 'itered'): | |
| 161 continue | |
| 162 try: | |
| 163 itered = part.itered() | |
| 164 except TypeError: | |
| 165 continue # XXX log error | |
| 166 for stmt in itered: | |
| 167 try: | |
| 168 assigned = stmt.getitem(index, context) | |
| 169 except (AttributeError, IndexError): | |
| 170 continue | |
| 171 except TypeError, exc: # stmt is unsubscriptable Const | |
| 172 continue | |
| 173 if not asspath: | |
| 174 # we achieved to resolved the assignment path, | |
| 175 # don't infer the last part | |
| 176 yield assigned | |
| 177 elif assigned is YES: | |
| 178 break | |
| 179 else: | |
| 180 # we are not yet on the last part of the path | |
| 181 # search on each possibly inferred value | |
| 182 try: | |
| 183 for infered in _resolve_looppart(assigned.infer(context), | |
| 184 asspath, context): | |
| 185 yield infered | |
| 186 except InferenceError: | |
| 187 break | |
| 188 | |
| 189 | |
| 190 def for_assigned_stmts(self, node, context=None, asspath=None): | |
| 191 if asspath is None: | |
| 192 for lst in self.iter.infer(context): | |
| 193 if isinstance(lst, (nodes.Tuple, nodes.List)): | |
| 194 for item in lst.elts: | |
| 195 yield item | |
| 196 else: | |
| 197 for infered in _resolve_looppart(self.iter.infer(context), | |
| 198 asspath, context): | |
| 199 yield infered | |
| 200 | |
| 201 nodes.For.assigned_stmts = raise_if_nothing_infered(for_assigned_stmts) | |
| 202 nodes.Comprehension.assigned_stmts = raise_if_nothing_infered(for_assigned_stmts
) | |
| 203 | |
| 204 | |
| 205 def mulass_assigned_stmts(self, node, context=None, asspath=None): | |
| 206 if asspath is None: | |
| 207 asspath = [] | |
| 208 asspath.insert(0, self.elts.index(node)) | |
| 209 return self.parent.assigned_stmts(self, context, asspath) | |
| 210 nodes.Tuple.assigned_stmts = mulass_assigned_stmts | |
| 211 nodes.List.assigned_stmts = mulass_assigned_stmts | |
| 212 | |
| 213 | |
| 214 def assend_assigned_stmts(self, context=None): | |
| 215 return self.parent.assigned_stmts(self, context=context) | |
| 216 nodes.AssName.assigned_stmts = assend_assigned_stmts | |
| 217 nodes.AssAttr.assigned_stmts = assend_assigned_stmts | |
| 218 | |
| 219 | |
| 220 def _arguments_infer_argname(self, name, context): | |
| 221 # arguments information may be missing, in which case we can't do anything | |
| 222 # more | |
| 223 if not (self.args or self.vararg or self.kwarg): | |
| 224 yield YES | |
| 225 return | |
| 226 # first argument of instance/class method | |
| 227 if self.args and getattr(self.args[0], 'name', None) == name: | |
| 228 functype = self.parent.type | |
| 229 if functype == 'method': | |
| 230 yield Instance(self.parent.parent.frame()) | |
| 231 return | |
| 232 if functype == 'classmethod': | |
| 233 yield self.parent.parent.frame() | |
| 234 return | |
| 235 if name == self.vararg: | |
| 236 yield const_factory(()) | |
| 237 return | |
| 238 if name == self.kwarg: | |
| 239 yield const_factory({}) | |
| 240 return | |
| 241 # if there is a default value, yield it. And then yield YES to reflect | |
| 242 # we can't guess given argument value | |
| 243 try: | |
| 244 context = copy_context(context) | |
| 245 for infered in self.default_value(name).infer(context): | |
| 246 yield infered | |
| 247 yield YES | |
| 248 except NoDefault: | |
| 249 yield YES | |
| 250 | |
| 251 | |
| 252 def arguments_assigned_stmts(self, node, context, asspath=None): | |
| 253 if context.callcontext: | |
| 254 # reset call context/name | |
| 255 callcontext = context.callcontext | |
| 256 context = copy_context(context) | |
| 257 context.callcontext = None | |
| 258 for infered in callcontext.infer_argument(self.parent, node.name, contex
t): | |
| 259 yield infered | |
| 260 return | |
| 261 for infered in _arguments_infer_argname(self, node.name, context): | |
| 262 yield infered | |
| 263 nodes.Arguments.assigned_stmts = arguments_assigned_stmts | |
| 264 | |
| 265 | |
| 266 def assign_assigned_stmts(self, node, context=None, asspath=None): | |
| 267 if not asspath: | |
| 268 yield self.value | |
| 269 return | |
| 270 for infered in _resolve_asspart(self.value.infer(context), asspath, context)
: | |
| 271 yield infered | |
| 272 nodes.Assign.assigned_stmts = raise_if_nothing_infered(assign_assigned_stmts) | |
| 273 nodes.AugAssign.assigned_stmts = raise_if_nothing_infered(assign_assigned_stmts) | |
| 274 | |
| 275 | |
| 276 def _resolve_asspart(parts, asspath, context): | |
| 277 """recursive function to resolve multiple assignments""" | |
| 278 asspath = asspath[:] | |
| 279 index = asspath.pop(0) | |
| 280 for part in parts: | |
| 281 if hasattr(part, 'getitem'): | |
| 282 try: | |
| 283 assigned = part.getitem(index, context) | |
| 284 # XXX raise a specific exception to avoid potential hiding of | |
| 285 # unexpected exception ? | |
| 286 except (TypeError, IndexError): | |
| 287 return | |
| 288 if not asspath: | |
| 289 # we achieved to resolved the assignment path, don't infer the | |
| 290 # last part | |
| 291 yield assigned | |
| 292 elif assigned is YES: | |
| 293 return | |
| 294 else: | |
| 295 # we are not yet on the last part of the path search on each | |
| 296 # possibly inferred value | |
| 297 try: | |
| 298 for infered in _resolve_asspart(assigned.infer(context), | |
| 299 asspath, context): | |
| 300 yield infered | |
| 301 except InferenceError: | |
| 302 return | |
| 303 | |
| 304 | |
| 305 def excepthandler_assigned_stmts(self, node, context=None, asspath=None): | |
| 306 for assigned in unpack_infer(self.type): | |
| 307 if isinstance(assigned, nodes.Class): | |
| 308 assigned = Instance(assigned) | |
| 309 yield assigned | |
| 310 nodes.ExceptHandler.assigned_stmts = raise_if_nothing_infered(excepthandler_assi
gned_stmts) | |
| 311 | |
| 312 | |
| 313 def with_assigned_stmts(self, node, context=None, asspath=None): | |
| 314 if asspath is None: | |
| 315 for lst in self.vars.infer(context): | |
| 316 if isinstance(lst, (nodes.Tuple, nodes.List)): | |
| 317 for item in lst.nodes: | |
| 318 yield item | |
| 319 nodes.With.assigned_stmts = raise_if_nothing_infered(with_assigned_stmts) | |
| 320 | |
| 321 | |
| OLD | NEW |