Skip to content

Commit e95aa05

Browse files
authored
Merge pull request #124 from oremanj/sniffio-via-tlocal
Modernize sniffio integration
2 parents a3ffea7 + 3e76109 commit e95aa05

File tree

8 files changed

+22
-18
lines changed

8 files changed

+22
-18
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ jobs:
5656
strategy:
5757
fail-fast: false
5858
matrix:
59-
python: ['pypy-3.7', 'pypy-3.8', 'pypy-3.9', '3.7', '3.8', '3.9', '3.10', '3.11', '3.12-dev', 'pypy-3.8-nightly', 'pypy-3.9-nightly']
59+
python: ['pypy-3.7', 'pypy-3.8', 'pypy-3.9', '3.7', '3.8', '3.9', '3.10', '3.11', 'pypy-3.8-nightly', 'pypy-3.9-nightly']
6060
check_formatting: ['0']
6161
extra_name: ['']
6262
include:

.readthedocs.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ formats:
55
- htmlzip
66
- epub
77

8+
build:
9+
os: "ubuntu-22.04"
10+
tools:
11+
python: "3.11"
12+
813
python:
9-
version: 3.7
1014
install:
1115
- requirements: docs-requirements.txt
1216

docs/source/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
#
105105
# This is also used if you do content translation via gettext catalogs.
106106
# Usually you set "language" from the command line for these cases.
107-
language = None
107+
language = "en"
108108

109109
# List of patterns, relative to source directory, that match files and
110110
# directories to ignore when looking for source files.

newsfragments/123.misc.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
trio-asyncio now indicates its presence to `sniffio` using the
2+
``sniffio.thread_local`` interface that is preferred since sniffio
3+
v1.3.0. This should be less likely than the previous approach to cause
4+
:func:`sniffio.current_async_library` to return incorrect results due
5+
to unintended inheritance of contextvars.

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
install_requires=[
6666
"trio >= 0.22.0",
6767
"outcome",
68-
"sniffio",
68+
"sniffio >= 1.3.0",
6969
"exceptiongroup >= 1.0.0; python_version < '3.11'",
7070
],
7171
# This means, just install *everything* you see under trio/, even if it

trio_asyncio/_base.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import trio
55
import heapq
66
import signal
7-
import sniffio
87
import asyncio
98
import warnings
109
import concurrent.futures
@@ -13,6 +12,7 @@
1312
from ._util import run_aio_future
1413

1514
from selectors import _BaseSelectorImpl, EVENT_READ, EVENT_WRITE
15+
from sniffio import thread_local as sniffio_library
1616

1717
try:
1818
from trio.lowlevel import wait_for_child
@@ -218,15 +218,11 @@ async def run_aio_coroutine(self, coro):
218218
"""
219219
try:
220220
self._check_closed()
221-
t = sniffio.current_async_library_cvar.set("asyncio")
222221
fut = asyncio.ensure_future(coro, loop=self)
223222
except BaseException:
224223
coro.close() # avoid unawaited coroutine error
225224
raise
226-
try:
227-
return await run_aio_future(fut)
228-
finally:
229-
sniffio.current_async_library_cvar.reset(t)
225+
return await run_aio_future(fut)
230226

231227
def trio_as_future(self, proc, *args):
232228
"""Start a new Trio task to run ``await proc(*args)`` asynchronously.
@@ -641,7 +637,6 @@ async def _main_loop(self, task_status=trio.TASK_STATUS_IGNORED):
641637

642638
self._stopped = trio.Event()
643639
task_status.started()
644-
sniffio.current_async_library_cvar.set("asyncio")
645640

646641
try:
647642
# The shield here ensures that if the context surrounding
@@ -701,10 +696,16 @@ async def _main_loop_one(self, no_wait=False):
701696
# Don't go through the expensive nursery dance
702697
# if this is a sync function.
703698
if isinstance(obj, AsyncHandle):
699+
# AsyncHandle is only used to run Trio tasks, so no need to set the
700+
# sniffio library
704701
obj._context.run(self._nursery.start_soon, obj._run, name=obj._callback)
705702
await obj._started.wait()
706703
else:
707-
obj._run()
704+
prev_library, sniffio_library.name = sniffio_library.name, "asyncio"
705+
try:
706+
obj._run()
707+
finally:
708+
sniffio_library.name = prev_library
708709

709710
async def _main_loop_exit(self):
710711
"""Finalize the loop. It may not be re-entered."""

trio_asyncio/_handles.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import trio
33
import types
44
import asyncio
5-
import sniffio
65
from asyncio.format_helpers import _format_callback, _get_function_source
76

87
if sys.version_info < (3, 11):
@@ -139,7 +138,6 @@ def propagate_cancel(f):
139138
self.cancel()
140139

141140
async def _run(self):
142-
sniffio.current_async_library_cvar.set("trio")
143141
self._started.set()
144142
if self._cancelled:
145143
return

trio_asyncio/_util.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import asyncio
66
import sys
77
import outcome
8-
import sniffio
98

109

1110
async def run_aio_future(future):
@@ -69,7 +68,6 @@ async def run_aio_generator(loop, async_generator):
6968
current_read = None
7069

7170
async def consume_next():
72-
t = sniffio.current_async_library_cvar.set("asyncio")
7371
try:
7472
item = await async_generator.__anext__()
7573
result = outcome.Value(value=item)
@@ -80,8 +78,6 @@ async def consume_next():
8078
return
8179
except Exception as e:
8280
result = outcome.Error(error=e)
83-
finally:
84-
sniffio.current_async_library_cvar.reset(t)
8581

8682
trio.lowlevel.reschedule(task, result)
8783

0 commit comments

Comments
 (0)