| OLD | NEW |
| 1 # copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. | 1 # copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
| 2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr | 2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
| 3 # | 3 # |
| 4 # This file is part of astroid. | 4 # This file is part of astroid. |
| 5 # | 5 # |
| 6 # astroid is free software: you can redistribute it and/or modify it | 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 | 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 | 8 # Free Software Foundation, either version 2.1 of the License, or (at your |
| 9 # option) any later version. | 9 # option) any later version. |
| 10 # | 10 # |
| 11 # astroid is distributed in the hope that it will be useful, but | 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 | 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 | 13 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 14 # for more details. | 14 # for more details. |
| 15 # | 15 # |
| 16 # You should have received a copy of the GNU Lesser General Public License along | 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/>. | 17 # with astroid. If not, see <http://www.gnu.org/licenses/>. |
| 18 """this module contains a set of functions to handle python protocols for nodes | 18 """this module contains a set of functions to handle python protocols for nodes |
| 19 where it makes sense. | 19 where it makes sense. |
| 20 """ | 20 """ |
| 21 | 21 |
| 22 __doctype__ = "restructuredtext en" | 22 __doctype__ = "restructuredtext en" |
| 23 | 23 |
| 24 from astroid.exceptions import InferenceError, NoDefault, NotFoundError | 24 from astroid.exceptions import InferenceError, NoDefault, NotFoundError |
| 25 from astroid.node_classes import unpack_infer | 25 from astroid.node_classes import unpack_infer |
| 26 from astroid.bases import copy_context, \ | 26 from astroid.bases import InferenceContext, \ |
| 27 raise_if_nothing_infered, yes_if_nothing_infered, Instance, YES | 27 raise_if_nothing_infered, yes_if_nothing_infered, Instance, YES |
| 28 from astroid.nodes import const_factory | 28 from astroid.nodes import const_factory |
| 29 from astroid import nodes | 29 from astroid import nodes |
| 30 | 30 |
| 31 BIN_OP_METHOD = {'+': '__add__', | 31 BIN_OP_METHOD = {'+': '__add__', |
| 32 '-': '__sub__', | 32 '-': '__sub__', |
| 33 '/': '__div__', | 33 '/': '__div__', |
| 34 '//': '__floordiv__', | 34 '//': '__floordiv__', |
| 35 '*': '__mul__', | 35 '*': '__mul__', |
| 36 '**': '__power__', | 36 '**': '__power__', |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 '//': lambda a, b: a // b, | 84 '//': lambda a, b: a // b, |
| 85 '*': lambda a, b: a * b, | 85 '*': lambda a, b: a * b, |
| 86 '**': lambda a, b: a ** b, | 86 '**': lambda a, b: a ** b, |
| 87 '%': lambda a, b: a % b, | 87 '%': lambda a, b: a % b, |
| 88 '&': lambda a, b: a & b, | 88 '&': lambda a, b: a & b, |
| 89 '|': lambda a, b: a | b, | 89 '|': lambda a, b: a | b, |
| 90 '^': lambda a, b: a ^ b, | 90 '^': lambda a, b: a ^ b, |
| 91 '<<': lambda a, b: a << b, | 91 '<<': lambda a, b: a << b, |
| 92 '>>': lambda a, b: a >> b, | 92 '>>': lambda a, b: a >> b, |
| 93 } | 93 } |
| 94 for key, impl in BIN_OP_IMPL.items(): | 94 for key, impl in list(BIN_OP_IMPL.items()): |
| 95 BIN_OP_IMPL[key+'='] = impl | 95 BIN_OP_IMPL[key+'='] = impl |
| 96 | 96 |
| 97 def const_infer_binary_op(self, operator, other, context): | 97 def const_infer_binary_op(self, operator, other, context): |
| 98 for other in other.infer(context): | 98 for other in other.infer(context): |
| 99 if isinstance(other, nodes.Const): | 99 if isinstance(other, nodes.Const): |
| 100 try: | 100 try: |
| 101 impl = BIN_OP_IMPL[operator] | 101 impl = BIN_OP_IMPL[operator] |
| 102 | 102 |
| 103 try: | 103 try: |
| 104 yield const_factory(impl(self.value, other.value)) | 104 yield const_factory(impl(self.value, other.value)) |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 275 yield vararg | 275 yield vararg |
| 276 return | 276 return |
| 277 if name == self.kwarg: | 277 if name == self.kwarg: |
| 278 kwarg = const_factory({}) | 278 kwarg = const_factory({}) |
| 279 kwarg.parent = self | 279 kwarg.parent = self |
| 280 yield kwarg | 280 yield kwarg |
| 281 return | 281 return |
| 282 # if there is a default value, yield it. And then yield YES to reflect | 282 # if there is a default value, yield it. And then yield YES to reflect |
| 283 # we can't guess given argument value | 283 # we can't guess given argument value |
| 284 try: | 284 try: |
| 285 context = copy_context(context) | 285 if context is None: |
| 286 context = InferenceContext() |
| 286 for infered in self.default_value(name).infer(context): | 287 for infered in self.default_value(name).infer(context): |
| 287 yield infered | 288 yield infered |
| 288 yield YES | 289 yield YES |
| 289 except NoDefault: | 290 except NoDefault: |
| 290 yield YES | 291 yield YES |
| 291 | 292 |
| 292 | 293 |
| 293 def arguments_assigned_stmts(self, node, context, asspath=None): | 294 def arguments_assigned_stmts(self, node, context, asspath=None): |
| 294 if context.callcontext: | 295 if context.callcontext: |
| 295 # reset call context/name | 296 # reset call context/name |
| 296 callcontext = context.callcontext | 297 callcontext = context.callcontext |
| 297 context = copy_context(context) | 298 return callcontext.infer_argument(self.parent, node.name, context) |
| 298 context.callcontext = None | 299 return _arguments_infer_argname(self, node.name, context) |
| 299 for infered in callcontext.infer_argument(self.parent, node.name, contex
t): | |
| 300 yield infered | |
| 301 return | |
| 302 for infered in _arguments_infer_argname(self, node.name, context): | |
| 303 yield infered | |
| 304 nodes.Arguments.assigned_stmts = arguments_assigned_stmts | 300 nodes.Arguments.assigned_stmts = arguments_assigned_stmts |
| 305 | 301 |
| 306 | 302 |
| 307 def assign_assigned_stmts(self, node, context=None, asspath=None): | 303 def assign_assigned_stmts(self, node, context=None, asspath=None): |
| 308 if not asspath: | 304 if not asspath: |
| 309 yield self.value | 305 yield self.value |
| 310 return | 306 return |
| 311 for infered in _resolve_asspart(self.value.infer(context), asspath, context)
: | 307 for infered in _resolve_asspart(self.value.infer(context), asspath, context)
: |
| 312 yield infered | 308 yield infered |
| 313 nodes.Assign.assigned_stmts = raise_if_nothing_infered(assign_assigned_stmts) | 309 nodes.Assign.assigned_stmts = raise_if_nothing_infered(assign_assigned_stmts) |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 356 for _, vars in self.items: | 352 for _, vars in self.items: |
| 357 if vars is None: | 353 if vars is None: |
| 358 continue | 354 continue |
| 359 for lst in vars.infer(context): | 355 for lst in vars.infer(context): |
| 360 if isinstance(lst, (nodes.Tuple, nodes.List)): | 356 if isinstance(lst, (nodes.Tuple, nodes.List)): |
| 361 for item in lst.nodes: | 357 for item in lst.nodes: |
| 362 yield item | 358 yield item |
| 363 nodes.With.assigned_stmts = raise_if_nothing_infered(with_assigned_stmts) | 359 nodes.With.assigned_stmts = raise_if_nothing_infered(with_assigned_stmts) |
| 364 | 360 |
| 365 | 361 |
| OLD | NEW |