From a4b0f48f73cb166aa9fb8ca273f17e614ba65ccb Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Fri, 25 Sep 2015 00:33:02 +0300 Subject: Fix a crash when inferring subscripts which returned the same object all the time. --- astroid/inference.py | 9 ++++++--- astroid/tests/unittest_inference.py | 15 ++++++++++++++- 2 files changed, 20 insertions(+), 4 deletions(-) (limited to 'astroid') diff --git a/astroid/inference.py b/astroid/inference.py index 148c8cd..db80e43 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -227,6 +227,7 @@ def _slice_value(index, context=None): return _SLICE_SENTINEL +@bases.raise_if_nothing_inferred def infer_subscript(self, context=None): """Inference for subscripts @@ -276,7 +277,7 @@ def infer_subscript(self, context=None): yield inferred nodes.Subscript._infer = bases.path_wrapper(infer_subscript) -nodes.Subscript.infer_lhs = bases.raise_if_nothing_inferred(infer_subscript) +nodes.Subscript.infer_lhs = infer_subscript @bases.raise_if_nothing_inferred @@ -388,6 +389,7 @@ def _infer_unaryop(self, context=None): yield util.YES +@bases.raise_if_nothing_inferred @bases.path_wrapper def infer_unaryop(self, context=None): """Infer what an UnaryOp should return when evaluated.""" @@ -395,7 +397,7 @@ def infer_unaryop(self, context=None): exceptions.UnaryOperationError) nodes.UnaryOp._infer_unaryop = _infer_unaryop -nodes.UnaryOp._infer = bases.raise_if_nothing_inferred(infer_unaryop) +nodes.UnaryOp._infer = infer_unaryop def _is_not_implemented(const): @@ -600,13 +602,14 @@ def _infer_binop(self, context): yield result +@bases.yes_if_nothing_inferred @bases.path_wrapper def infer_binop(self, context=None): return _filter_operation_errors(self, _infer_binop, context, exceptions.BinaryOperationError) nodes.BinOp._infer_binop = _infer_binop -nodes.BinOp._infer = bases.yes_if_nothing_inferred(infer_binop) +nodes.BinOp._infer = infer_binop def _infer_augassign(self, context=None): diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index bc75f9d..ef0886f 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -30,6 +30,7 @@ from astroid.builder import parse from astroid.inference import infer_end as inference_infer_end from astroid.bases import Instance, BoundMethod, UnboundMethod,\ path_wrapper, BUILTINS +from astroid import helpers from astroid import objects from astroid import test_utils from astroid import util @@ -2961,7 +2962,19 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): author = next(inferred.igetattr('author')) self.assertIsInstance(author, nodes.Const) self.assertEqual(author.value, 'Rushdie') - + + def test_subscript_inference_error(self): + # Used to raise StopIteration + ast_node = test_utils.extract_node(''' + class AttributeDict(dict): + def __getitem__(self, name): + return self + flow = AttributeDict() + flow['app'] = AttributeDict() + flow['app']['config'] = AttributeDict() + flow['app']['config']['doffing'] = AttributeDict() #@ + ''') + self.assertIsNone(helpers.safe_infer(ast_node.targets[0])) class GetattrTest(unittest.TestCase): -- cgit v1.2.1