Use inspect to properly detect generators. Fixes #2129

This commit is contained in:
Dmitry Malinovsky 2016-12-11 21:59:11 +06:00
parent da40bcf97f
commit 3a59acf69f
5 changed files with 56 additions and 5 deletions

View File

@ -19,6 +19,12 @@ except ImportError: # pragma: no cover
# Only available in Python 3.4+ or as a backport
enum = None
try:
import asyncio
except ImportError: # pragma: no cover
# Only available in Python 3.4+ or as a backport
asyncio = None
_PY3 = sys.version_info > (3, 0)
_PY2 = not _PY3
@ -42,11 +48,10 @@ REGEX_TYPE = type(re.compile(''))
def is_generator(func):
try:
return _pytest._code.getrawcode(func).co_flags & 32 # generator function
except AttributeError: # builtin functions have no bytecode
# assume them to not be generators
return False
genfunc = inspect.isgeneratorfunction(func)
if asyncio is not None:
return genfunc and not asyncio.iscoroutinefunction(func)
return genfunc
def getlocation(function, curdir):

7
testing/conftest.py Normal file
View File

@ -0,0 +1,7 @@
import sys
collect_ignore = []
if sys.version_info[0] < 3:
collect_ignore.append("test_compat_3.py")
if sys.version_info < (3, 5):
collect_ignore.append("test_compat_35.py")

12
testing/test_compat.py Normal file
View File

@ -0,0 +1,12 @@
from _pytest.compat import is_generator
def test_is_generator():
def zap():
yield
def foo():
pass
assert is_generator(zap)
assert not is_generator(foo)

15
testing/test_compat_3.py Normal file
View File

@ -0,0 +1,15 @@
import pytest
from _pytest.compat import is_generator
try:
import asyncio
except ImportError:
asyncio = None
@pytest.mark.skipif(asyncio is None, reason='asyncio is not installed')
def test_is_generator():
@asyncio.coroutine
def baz():
yield from [1,2,3]
assert not is_generator(baz)

12
testing/test_compat_35.py Normal file
View File

@ -0,0 +1,12 @@
from _pytest.compat import is_generator
def test_is_generator_py35():
async def foo():
await foo()
async def bar():
pass
assert not is_generator(foo)
assert not is_generator(bar)