Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use inspect to properly detect generators. Fixes #2129 #2130

Merged
merged 4 commits into from
Dec 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

*

*
* pytest no longer recognizes coroutine functions as yield tests (`#2129`_).
Thanks to `@malinoff`_ for the PR.

*

Expand All @@ -12,6 +13,9 @@
*


.. _@malinoff: https://github.com/malinoff

.. _#2129: https://github.com/pytest-dev/pytest/issues/2129

3.0.5 (2016-12-05)
==================
Expand Down
18 changes: 13 additions & 5 deletions _pytest/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
# Only available in Python 3.4+ or as a backport
enum = None


_PY3 = sys.version_info > (3, 0)
_PY2 = not _PY3

Expand All @@ -42,11 +43,18 @@ def _format_args(func):


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)
return genfunc and not iscoroutinefunction(func)


def iscoroutinefunction(func):
"""Return True if func is a decorated coroutine function.

Note: copied and modified from Python 3.5's builtin couroutines.py to avoid import asyncio directly,
which in turns also initializes the "logging" module as side-effect (see issue #8).
"""
return (getattr(func, '_is_coroutine', False) or
(hasattr(inspect, 'iscoroutinefunction') and inspect.iscoroutinefunction(func)))


def getlocation(function, curdir):
Expand Down
50 changes: 50 additions & 0 deletions testing/test_compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import sys

import pytest
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)


@pytest.mark.skipif(sys.version_info < (3, 4), reason='asyncio available in Python 3.4+')
def test_is_generator_asyncio(testdir):
testdir.makepyfile("""
from _pytest.compat import is_generator
import asyncio
@asyncio.coroutine
def baz():
yield from [1,2,3]

def test_is_generator_asyncio():
assert not is_generator(baz)
""")
# avoid importing asyncio into pytest's own process, which in turn imports logging (#8)
result = testdir.runpytest_subprocess()
result.stdout.fnmatch_lines(['*1 passed*'])


@pytest.mark.skipif(sys.version_info < (3, 5), reason='async syntax available in Python 3.5+')
def test_is_generator_async_syntax(testdir):
testdir.makepyfile("""
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)
""")
result = testdir.runpytest()
result.stdout.fnmatch_lines(['*1 passed*'])