Skip to content

Commit caee5ce

Browse files
committed
Avoid importing asyncio directly because that in turn initializes logging (#8)
1 parent 1312b83 commit caee5ce

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

_pytest/compat.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@
1919
# Only available in Python 3.4+ or as a backport
2020
enum = None
2121

22-
try:
23-
import asyncio
24-
except ImportError: # pragma: no cover
25-
# Only available in Python 3.4+ or as a backport
26-
asyncio = None
2722

2823
_PY3 = sys.version_info > (3, 0)
2924
_PY2 = not _PY3
@@ -49,9 +44,17 @@ def _format_args(func):
4944

5045
def is_generator(func):
5146
genfunc = inspect.isgeneratorfunction(func)
52-
if asyncio is not None:
53-
return genfunc and not asyncio.iscoroutinefunction(func)
54-
return genfunc
47+
return genfunc and not iscoroutinefunction(func)
48+
49+
50+
def iscoroutinefunction(func):
51+
"""Return True if func is a decorated coroutine function.
52+
53+
Note: copied and modified from Python 3.5's builtin couroutines.py to avoid import asyncio directly,
54+
which in turns also initializes the "logging" module as side-effect (see issue #8).
55+
"""
56+
return (getattr(func, '_is_coroutine', False) or
57+
(hasattr(inspect, 'iscoroutinefunction') and inspect.iscoroutinefunction(func)))
5558

5659

5760
def getlocation(function, curdir):

testing/test_compat.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ def foo():
1515
assert not is_generator(foo)
1616

1717

18+
@pytest.mark.skipif(sys.version_info < (3, 4), reason='asyncio available in Python 3.4+')
1819
def test_is_generator_asyncio(testdir):
19-
pytest.importorskip('asyncio')
2020
testdir.makepyfile("""
2121
from _pytest.compat import is_generator
2222
import asyncio
@@ -27,7 +27,8 @@ def baz():
2727
def test_is_generator_asyncio():
2828
assert not is_generator(baz)
2929
""")
30-
result = testdir.runpytest()
30+
# avoid importing asyncio into pytest's own process, which in turn imports logging (#8)
31+
result = testdir.runpytest_subprocess()
3132
result.stdout.fnmatch_lines(['*1 passed*'])
3233

3334

0 commit comments

Comments
 (0)