From 00e43d45853170621c67bd56b34ce115c7f5a8be Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Mon, 12 May 2014 18:46:17 +0200 Subject: [PATCH] except can also catch multiple exceptions in one statement --- jedi/evaluate/analysis.py | 14 ++++++++++++-- test/static_analysis/try_except.py | 13 +++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/jedi/evaluate/analysis.py b/jedi/evaluate/analysis.py index d44f3d1d..d2dab132 100644 --- a/jedi/evaluate/analysis.py +++ b/jedi/evaluate/analysis.py @@ -73,14 +73,24 @@ def add(evaluator, name, jedi_obj, typ=Error): def _check_for_exception_catch(evaluator, jedi_obj, exception): + def check_match(cls): + return isinstance(cls, CompiledObject) and cls.obj == exception + def check_try_for_except(obj): while obj.next is not None: obj = obj.next for i in obj.inputs: except_classes = evaluator.eval_statement(i) for cls in except_classes: - if isinstance(cls, CompiledObject) and cls.obj == exception: - return True + from jedi.evaluate import iterable + if isinstance(cls, iterable.Array) and cls.type == 'tuple': + # multiple exceptions + for c in cls.values(): + if check_match(c): + return True + else: + if check_match(cls): + return True return False while jedi_obj is not None and not jedi_obj.isinstance(pr.Function, pr.Class): diff --git a/test/static_analysis/try_except.py b/test/static_analysis/try_except.py index a7d6dc4c..01702dbc 100644 --- a/test/static_analysis/try_except.py +++ b/test/static_analysis/try_except.py @@ -21,6 +21,19 @@ try: except AttributeError: pass +# ----------------- +# multi except +# ----------------- +try: + str.not_existing +except (TypeError, AttributeError): pass + +try: + str.not_existing +except ImportError: + pass +except (NotImplementedError, AttributeError): pass + # ----------------- # detailed except # -----------------