Skip to content

Commit

Permalink
Avoid importing asyncio directly because that in turn initializes log…
Browse files Browse the repository at this point in the history
…ging (pytest-dev#8)
  • Loading branch information
nicoddemus committed Dec 13, 2016
1 parent 1312b83 commit caee5ce
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
19 changes: 11 additions & 8 deletions _pytest/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@
# 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
Expand All @@ -49,9 +44,17 @@ def _format_args(func):

def is_generator(func):
genfunc = inspect.isgeneratorfunction(func)
if asyncio is not None:
return genfunc and not asyncio.iscoroutinefunction(func)
return genfunc
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
5 changes: 3 additions & 2 deletions testing/test_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def foo():
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):
pytest.importorskip('asyncio')
testdir.makepyfile("""
from _pytest.compat import is_generator
import asyncio
Expand All @@ -27,7 +27,8 @@ def baz():
def test_is_generator_asyncio():
assert not is_generator(baz)
""")
result = testdir.runpytest()
# avoid importing asyncio into pytest's own process, which in turn imports logging (#8)
result = testdir.runpytest_subprocess()
result.stdout.fnmatch_lines(['*1 passed*'])


Expand Down

0 comments on commit caee5ce

Please sign in to comment.