From 562770be06efd98e157f16cb838bce781be91775 Mon Sep 17 00:00:00 2001 From: Rene Zhang Date: Thu, 20 Aug 2015 18:07:16 -0700 Subject: Fix crash when handling generator inside with context --- pylint/checkers/typecheck.py | 7 ++++--- pylint/test/functional/with_using_generator.py | 14 ++++++++++++++ pylint/test/functional/with_using_generator.txt | 1 + 3 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 pylint/test/functional/with_using_generator.py create mode 100644 pylint/test/functional/with_using_generator.txt diff --git a/pylint/checkers/typecheck.py b/pylint/checkers/typecheck.py index a0f1e5e..04b2c9d 100644 --- a/pylint/checkers/typecheck.py +++ b/pylint/checkers/typecheck.py @@ -29,6 +29,7 @@ from astroid import ( MroError, SuperError, YES, Instance ) from astroid.bases import BUILTINS +from astroid.bases import NodeNG from astroid import objects from astroid import helpers import six @@ -48,11 +49,11 @@ _ZOPE_DEPRECATED = ( def _unflatten(iterable): for elem in iterable: - elem = elem[0] - if isinstance(elem, collections.Sequence): + if (isinstance(elem, collections.Sequence) and + not isinstance(elem, basestring)): for subelem in _unflatten(elem): yield subelem - else: + elif isinstance(elem, NodeNG): yield elem diff --git a/pylint/test/functional/with_using_generator.py b/pylint/test/functional/with_using_generator.py new file mode 100644 index 0000000..25c6b37 --- /dev/null +++ b/pylint/test/functional/with_using_generator.py @@ -0,0 +1,14 @@ +""" Testing with statements that use generators. This should not crash. """ + +class Base(object): + """ Base class. """ + val = 0 + + def gen(self): + """ A generator. """ + yield self.val + + def fun(self): + """ With statement using a generator. """ + with self.gen(): # [not-context-manager] + pass diff --git a/pylint/test/functional/with_using_generator.txt b/pylint/test/functional/with_using_generator.txt new file mode 100644 index 0000000..276b05c --- /dev/null +++ b/pylint/test/functional/with_using_generator.txt @@ -0,0 +1 @@ +not-context-manager:13:Base.fun:Context manager 'generator' doesn't implement __enter__ and __exit__. -- cgit v1.2.1