Spaces:
Runtime error
Runtime error
Upload 11 files
Browse files- sniffio/__init__.py +17 -0
- sniffio/__pycache__/__init__.cpython-314.pyc +0 -0
- sniffio/__pycache__/_impl.cpython-314.pyc +0 -0
- sniffio/__pycache__/_version.cpython-314.pyc +0 -0
- sniffio/_impl.py +95 -0
- sniffio/_tests/__init__.py +0 -0
- sniffio/_tests/__pycache__/__init__.cpython-314.pyc +0 -0
- sniffio/_tests/__pycache__/test_sniffio.cpython-314.pyc +0 -0
- sniffio/_tests/test_sniffio.py +84 -0
- sniffio/_version.py +3 -0
- sniffio/py.typed +0 -0
sniffio/__init__.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Top-level package for sniffio."""
|
| 2 |
+
|
| 3 |
+
__all__ = [
|
| 4 |
+
"current_async_library",
|
| 5 |
+
"AsyncLibraryNotFoundError",
|
| 6 |
+
"current_async_library_cvar",
|
| 7 |
+
"thread_local",
|
| 8 |
+
]
|
| 9 |
+
|
| 10 |
+
from ._version import __version__
|
| 11 |
+
|
| 12 |
+
from ._impl import (
|
| 13 |
+
current_async_library,
|
| 14 |
+
AsyncLibraryNotFoundError,
|
| 15 |
+
current_async_library_cvar,
|
| 16 |
+
thread_local,
|
| 17 |
+
)
|
sniffio/__pycache__/__init__.cpython-314.pyc
ADDED
|
Binary file (444 Bytes). View file
|
|
|
sniffio/__pycache__/_impl.cpython-314.pyc
ADDED
|
Binary file (3.3 kB). View file
|
|
|
sniffio/__pycache__/_version.cpython-314.pyc
ADDED
|
Binary file (201 Bytes). View file
|
|
|
sniffio/_impl.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from contextvars import ContextVar
|
| 2 |
+
from typing import Optional
|
| 3 |
+
import sys
|
| 4 |
+
import threading
|
| 5 |
+
|
| 6 |
+
current_async_library_cvar = ContextVar(
|
| 7 |
+
"current_async_library_cvar", default=None
|
| 8 |
+
) # type: ContextVar[Optional[str]]
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class _ThreadLocal(threading.local):
|
| 12 |
+
# Since threading.local provides no explicit mechanism is for setting
|
| 13 |
+
# a default for a value, a custom class with a class attribute is used
|
| 14 |
+
# instead.
|
| 15 |
+
name = None # type: Optional[str]
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
thread_local = _ThreadLocal()
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
class AsyncLibraryNotFoundError(RuntimeError):
|
| 22 |
+
pass
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def current_async_library() -> str:
|
| 26 |
+
"""Detect which async library is currently running.
|
| 27 |
+
|
| 28 |
+
The following libraries are currently supported:
|
| 29 |
+
|
| 30 |
+
================ =========== ============================
|
| 31 |
+
Library Requires Magic string
|
| 32 |
+
================ =========== ============================
|
| 33 |
+
**Trio** Trio v0.6+ ``"trio"``
|
| 34 |
+
**Curio** - ``"curio"``
|
| 35 |
+
**asyncio** ``"asyncio"``
|
| 36 |
+
**Trio-asyncio** v0.8.2+ ``"trio"`` or ``"asyncio"``,
|
| 37 |
+
depending on current mode
|
| 38 |
+
================ =========== ============================
|
| 39 |
+
|
| 40 |
+
Returns:
|
| 41 |
+
A string like ``"trio"``.
|
| 42 |
+
|
| 43 |
+
Raises:
|
| 44 |
+
AsyncLibraryNotFoundError: if called from synchronous context,
|
| 45 |
+
or if the current async library was not recognized.
|
| 46 |
+
|
| 47 |
+
Examples:
|
| 48 |
+
|
| 49 |
+
.. code-block:: python3
|
| 50 |
+
|
| 51 |
+
from sniffio import current_async_library
|
| 52 |
+
|
| 53 |
+
async def generic_sleep(seconds):
|
| 54 |
+
library = current_async_library()
|
| 55 |
+
if library == "trio":
|
| 56 |
+
import trio
|
| 57 |
+
await trio.sleep(seconds)
|
| 58 |
+
elif library == "asyncio":
|
| 59 |
+
import asyncio
|
| 60 |
+
await asyncio.sleep(seconds)
|
| 61 |
+
# ... and so on ...
|
| 62 |
+
else:
|
| 63 |
+
raise RuntimeError(f"Unsupported library {library!r}")
|
| 64 |
+
|
| 65 |
+
"""
|
| 66 |
+
value = thread_local.name
|
| 67 |
+
if value is not None:
|
| 68 |
+
return value
|
| 69 |
+
|
| 70 |
+
value = current_async_library_cvar.get()
|
| 71 |
+
if value is not None:
|
| 72 |
+
return value
|
| 73 |
+
|
| 74 |
+
# Need to sniff for asyncio
|
| 75 |
+
if "asyncio" in sys.modules:
|
| 76 |
+
import asyncio
|
| 77 |
+
try:
|
| 78 |
+
current_task = asyncio.current_task # type: ignore[attr-defined]
|
| 79 |
+
except AttributeError:
|
| 80 |
+
current_task = asyncio.Task.current_task # type: ignore[attr-defined]
|
| 81 |
+
try:
|
| 82 |
+
if current_task() is not None:
|
| 83 |
+
return "asyncio"
|
| 84 |
+
except RuntimeError:
|
| 85 |
+
pass
|
| 86 |
+
|
| 87 |
+
# Sniff for curio (for now)
|
| 88 |
+
if 'curio' in sys.modules:
|
| 89 |
+
from curio.meta import curio_running
|
| 90 |
+
if curio_running():
|
| 91 |
+
return 'curio'
|
| 92 |
+
|
| 93 |
+
raise AsyncLibraryNotFoundError(
|
| 94 |
+
"unknown async library, or not in async context"
|
| 95 |
+
)
|
sniffio/_tests/__init__.py
ADDED
|
File without changes
|
sniffio/_tests/__pycache__/__init__.cpython-314.pyc
ADDED
|
Binary file (180 Bytes). View file
|
|
|
sniffio/_tests/__pycache__/test_sniffio.cpython-314.pyc
ADDED
|
Binary file (4.19 kB). View file
|
|
|
sniffio/_tests/test_sniffio.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
|
| 4 |
+
import pytest
|
| 5 |
+
|
| 6 |
+
from .. import (
|
| 7 |
+
current_async_library, AsyncLibraryNotFoundError,
|
| 8 |
+
current_async_library_cvar, thread_local
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def test_basics_cvar():
|
| 13 |
+
with pytest.raises(AsyncLibraryNotFoundError):
|
| 14 |
+
current_async_library()
|
| 15 |
+
|
| 16 |
+
token = current_async_library_cvar.set("generic-lib")
|
| 17 |
+
try:
|
| 18 |
+
assert current_async_library() == "generic-lib"
|
| 19 |
+
finally:
|
| 20 |
+
current_async_library_cvar.reset(token)
|
| 21 |
+
|
| 22 |
+
with pytest.raises(AsyncLibraryNotFoundError):
|
| 23 |
+
current_async_library()
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def test_basics_tlocal():
|
| 27 |
+
with pytest.raises(AsyncLibraryNotFoundError):
|
| 28 |
+
current_async_library()
|
| 29 |
+
|
| 30 |
+
old_name, thread_local.name = thread_local.name, "generic-lib"
|
| 31 |
+
try:
|
| 32 |
+
assert current_async_library() == "generic-lib"
|
| 33 |
+
finally:
|
| 34 |
+
thread_local.name = old_name
|
| 35 |
+
|
| 36 |
+
with pytest.raises(AsyncLibraryNotFoundError):
|
| 37 |
+
current_async_library()
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def test_asyncio():
|
| 41 |
+
import asyncio
|
| 42 |
+
|
| 43 |
+
with pytest.raises(AsyncLibraryNotFoundError):
|
| 44 |
+
current_async_library()
|
| 45 |
+
|
| 46 |
+
ran = []
|
| 47 |
+
|
| 48 |
+
async def this_is_asyncio():
|
| 49 |
+
assert current_async_library() == "asyncio"
|
| 50 |
+
# Call it a second time to exercise the caching logic
|
| 51 |
+
assert current_async_library() == "asyncio"
|
| 52 |
+
ran.append(True)
|
| 53 |
+
|
| 54 |
+
asyncio.run(this_is_asyncio())
|
| 55 |
+
assert ran == [True]
|
| 56 |
+
|
| 57 |
+
with pytest.raises(AsyncLibraryNotFoundError):
|
| 58 |
+
current_async_library()
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
@pytest.mark.skipif(
|
| 62 |
+
sys.version_info >= (3, 12),
|
| 63 |
+
reason=
|
| 64 |
+
"curio broken on 3.12 (https://github.com/python-trio/sniffio/pull/42)",
|
| 65 |
+
)
|
| 66 |
+
def test_curio():
|
| 67 |
+
import curio
|
| 68 |
+
|
| 69 |
+
with pytest.raises(AsyncLibraryNotFoundError):
|
| 70 |
+
current_async_library()
|
| 71 |
+
|
| 72 |
+
ran = []
|
| 73 |
+
|
| 74 |
+
async def this_is_curio():
|
| 75 |
+
assert current_async_library() == "curio"
|
| 76 |
+
# Call it a second time to exercise the caching logic
|
| 77 |
+
assert current_async_library() == "curio"
|
| 78 |
+
ran.append(True)
|
| 79 |
+
|
| 80 |
+
curio.run(this_is_curio)
|
| 81 |
+
assert ran == [True]
|
| 82 |
+
|
| 83 |
+
with pytest.raises(AsyncLibraryNotFoundError):
|
| 84 |
+
current_async_library()
|
sniffio/_version.py
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# This file is imported from __init__.py and exec'd from setup.py
|
| 2 |
+
|
| 3 |
+
__version__ = "1.3.1"
|
sniffio/py.typed
ADDED
|
File without changes
|