Avoid importing asyncio directly because that in turn initializes logging (#8)
This commit is contained in:
parent
1312b83866
commit
caee5ce489
|
@ -19,11 +19,6 @@ except ImportError: # pragma: no cover
|
||||||
# Only available in Python 3.4+ or as a backport
|
# Only available in Python 3.4+ or as a backport
|
||||||
enum = None
|
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)
|
_PY3 = sys.version_info > (3, 0)
|
||||||
_PY2 = not _PY3
|
_PY2 = not _PY3
|
||||||
|
@ -49,9 +44,17 @@ REGEX_TYPE = type(re.compile(''))
|
||||||
|
|
||||||
def is_generator(func):
|
def is_generator(func):
|
||||||
genfunc = inspect.isgeneratorfunction(func)
|
genfunc = inspect.isgeneratorfunction(func)
|
||||||
if asyncio is not None:
|
return genfunc and not iscoroutinefunction(func)
|
||||||
return genfunc and not asyncio.iscoroutinefunction(func)
|
|
||||||
return genfunc
|
|
||||||
|
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):
|
def getlocation(function, curdir):
|
||||||
|
|
|
@ -15,8 +15,8 @@ def test_is_generator():
|
||||||
assert not is_generator(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):
|
def test_is_generator_asyncio(testdir):
|
||||||
pytest.importorskip('asyncio')
|
|
||||||
testdir.makepyfile("""
|
testdir.makepyfile("""
|
||||||
from _pytest.compat import is_generator
|
from _pytest.compat import is_generator
|
||||||
import asyncio
|
import asyncio
|
||||||
|
@ -27,7 +27,8 @@ def test_is_generator_asyncio(testdir):
|
||||||
def test_is_generator_asyncio():
|
def test_is_generator_asyncio():
|
||||||
assert not is_generator(baz)
|
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*'])
|
result.stdout.fnmatch_lines(['*1 passed*'])
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue