moving to scripts
This commit is contained in:
14
asq-env/lib/python3.9/site-packages/sniffio/__init__.py
Normal file
14
asq-env/lib/python3.9/site-packages/sniffio/__init__.py
Normal file
@@ -0,0 +1,14 @@
|
||||
"""Top-level package for sniffio."""
|
||||
|
||||
__all__ = [
|
||||
"current_async_library", "AsyncLibraryNotFoundError",
|
||||
"current_async_library_cvar"
|
||||
]
|
||||
|
||||
from ._version import __version__
|
||||
|
||||
from ._impl import (
|
||||
current_async_library,
|
||||
AsyncLibraryNotFoundError,
|
||||
current_async_library_cvar,
|
||||
)
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
83
asq-env/lib/python3.9/site-packages/sniffio/_impl.py
Normal file
83
asq-env/lib/python3.9/site-packages/sniffio/_impl.py
Normal file
@@ -0,0 +1,83 @@
|
||||
from contextvars import ContextVar
|
||||
from typing import Optional
|
||||
import sys
|
||||
|
||||
current_async_library_cvar = ContextVar(
|
||||
"current_async_library_cvar", default=None
|
||||
) # type: ContextVar[Optional[str]]
|
||||
|
||||
|
||||
class AsyncLibraryNotFoundError(RuntimeError):
|
||||
pass
|
||||
|
||||
|
||||
def current_async_library() -> str:
|
||||
"""Detect which async library is currently running.
|
||||
|
||||
The following libraries are currently supported:
|
||||
|
||||
================ =========== ============================
|
||||
Library Requires Magic string
|
||||
================ =========== ============================
|
||||
**Trio** Trio v0.6+ ``"trio"``
|
||||
**Curio** - ``"curio"``
|
||||
**asyncio** ``"asyncio"``
|
||||
**Trio-asyncio** v0.8.2+ ``"trio"`` or ``"asyncio"``,
|
||||
depending on current mode
|
||||
================ =========== ============================
|
||||
|
||||
Returns:
|
||||
A string like ``"trio"``.
|
||||
|
||||
Raises:
|
||||
AsyncLibraryNotFoundError: if called from synchronous context,
|
||||
or if the current async library was not recognized.
|
||||
|
||||
Examples:
|
||||
|
||||
.. code-block:: python3
|
||||
|
||||
from sniffio import current_async_library
|
||||
|
||||
async def generic_sleep(seconds):
|
||||
library = current_async_library()
|
||||
if library == "trio":
|
||||
import trio
|
||||
await trio.sleep(seconds)
|
||||
elif library == "asyncio":
|
||||
import asyncio
|
||||
await asyncio.sleep(seconds)
|
||||
# ... and so on ...
|
||||
else:
|
||||
raise RuntimeError(f"Unsupported library {library!r}")
|
||||
|
||||
"""
|
||||
value = current_async_library_cvar.get()
|
||||
if value is not None:
|
||||
return value
|
||||
|
||||
# Sniff for curio (for now)
|
||||
if 'curio' in sys.modules:
|
||||
from curio.meta import curio_running
|
||||
if curio_running():
|
||||
return 'curio'
|
||||
|
||||
# Need to sniff for asyncio
|
||||
if "asyncio" in sys.modules:
|
||||
import asyncio
|
||||
try:
|
||||
current_task = asyncio.current_task # type: ignore[attr-defined]
|
||||
except AttributeError:
|
||||
current_task = asyncio.Task.current_task # type: ignore[attr-defined]
|
||||
try:
|
||||
if current_task() is not None:
|
||||
if (3, 7) <= sys.version_info:
|
||||
# asyncio has contextvars support, and we're in a task, so
|
||||
# we can safely cache the sniffed value
|
||||
current_async_library_cvar.set("asyncio")
|
||||
return "asyncio"
|
||||
except RuntimeError:
|
||||
pass
|
||||
raise AsyncLibraryNotFoundError(
|
||||
"unknown async library, or not in async context"
|
||||
)
|
||||
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,67 @@
|
||||
import sys
|
||||
|
||||
import pytest
|
||||
|
||||
from .. import (
|
||||
current_async_library, AsyncLibraryNotFoundError,
|
||||
current_async_library_cvar
|
||||
)
|
||||
|
||||
|
||||
def test_basics():
|
||||
with pytest.raises(AsyncLibraryNotFoundError):
|
||||
current_async_library()
|
||||
|
||||
token = current_async_library_cvar.set("generic-lib")
|
||||
try:
|
||||
assert current_async_library() == "generic-lib"
|
||||
finally:
|
||||
current_async_library_cvar.reset(token)
|
||||
|
||||
with pytest.raises(AsyncLibraryNotFoundError):
|
||||
current_async_library()
|
||||
|
||||
|
||||
def test_asyncio():
|
||||
import asyncio
|
||||
|
||||
with pytest.raises(AsyncLibraryNotFoundError):
|
||||
current_async_library()
|
||||
|
||||
ran = []
|
||||
|
||||
async def this_is_asyncio():
|
||||
assert current_async_library() == "asyncio"
|
||||
# Call it a second time to exercise the caching logic
|
||||
assert current_async_library() == "asyncio"
|
||||
ran.append(True)
|
||||
|
||||
loop = asyncio.get_event_loop()
|
||||
loop.run_until_complete(this_is_asyncio())
|
||||
assert ran == [True]
|
||||
loop.close()
|
||||
|
||||
with pytest.raises(AsyncLibraryNotFoundError):
|
||||
current_async_library()
|
||||
|
||||
|
||||
@pytest.mark.skipif(sys.version_info < (3, 6), reason='Curio requires 3.6+')
|
||||
def test_curio():
|
||||
import curio
|
||||
|
||||
with pytest.raises(AsyncLibraryNotFoundError):
|
||||
current_async_library()
|
||||
|
||||
ran = []
|
||||
|
||||
async def this_is_curio():
|
||||
assert current_async_library() == "curio"
|
||||
# Call it a second time to exercise the caching logic
|
||||
assert current_async_library() == "curio"
|
||||
ran.append(True)
|
||||
|
||||
curio.run(this_is_curio)
|
||||
assert ran == [True]
|
||||
|
||||
with pytest.raises(AsyncLibraryNotFoundError):
|
||||
current_async_library()
|
||||
3
asq-env/lib/python3.9/site-packages/sniffio/_version.py
Normal file
3
asq-env/lib/python3.9/site-packages/sniffio/_version.py
Normal file
@@ -0,0 +1,3 @@
|
||||
# This file is imported from __init__.py and exec'd from setup.py
|
||||
|
||||
__version__ = "1.2.0"
|
||||
Reference in New Issue
Block a user