Skip to content

Commit 12c19a3

Browse files
committed
Underscore-prefix module names, fix circular dependencies, add actual deprecations
1 parent 42dd575 commit 12c19a3

File tree

12 files changed

+395
-398
lines changed

12 files changed

+395
-398
lines changed

tests/conftest.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
# XX this does not belong here -- b/c it's here, these things only apply to
2-
# the tests in trio/_core/tests, not in trio/tests. For now there's some
3-
# copy-paste...
4-
#
5-
# this stuff should become a proper pytest plugin
6-
71
import pytest
82
import asyncio
93
import trio_asyncio

trio_asyncio/__init__.py

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,74 @@
11
# This code implements basic asyncio compatibility
22

3+
# Submodules are organized into the following layers, from highest to lowest;
4+
# to avoid circular dependencies, submodules can only depend on other submodules
5+
# in a strictly lower layer.
6+
#
7+
# nice facade: _adapter
8+
# event loop dispatch and policy: _loop
9+
# event loop implementations: _async, _sync
10+
# event loop base: _base
11+
# utilities: _handles, _util, _child, _deprecate, _version
12+
313
from ._version import __version__ # noqa
414

5-
from .base import * # noqa
6-
from .loop import * # noqa
7-
from .util import * # noqa
8-
from .async_ import * # noqa
9-
from .adapter import * # noqa
15+
from ._deprecate import TrioAsyncioDeprecationWarning
16+
from ._util import run_aio_future, run_aio_generator
17+
from ._base import BaseTrioEventLoop, TrioExecutor
18+
from ._async import TrioEventLoop
19+
from ._loop import (
20+
# main entry point:
21+
open_loop,
22+
# trio.run() + trio_asyncio.open_loop():
23+
run,
24+
# loop selection:
25+
TrioChildWatcher,
26+
TrioPolicy,
27+
current_loop,
28+
current_policy,
29+
# forwarders to event loop methods:
30+
run_trio_task,
31+
run_trio,
32+
run_aio_coroutine,
33+
# forwarders to deprecated event loop methods:
34+
run_future,
35+
run_coroutine,
36+
run_asyncio,
37+
wrap_generator,
38+
run_iterator,
39+
)
40+
from ._adapter import (
41+
aio_as_trio,
42+
trio_as_aio,
43+
# aliases for the above:
44+
asyncio_as_trio,
45+
trio_as_asyncio,
46+
# deprecated aliases for the above:
47+
trio2aio,
48+
aio2trio,
49+
# additional experimental goodie:
50+
allow_asyncio,
51+
)
52+
53+
import importlib as _importlib
54+
from . import _deprecate, _util
55+
56+
_deprecate.enable_attribute_deprecations(__name__)
57+
__deprecated_attributes__ = {
58+
name: _deprecate.DeprecatedAttribute(
59+
_importlib.import_module("trio_asyncio._" + name.rstrip("_")),
60+
"0.11.0",
61+
issue=64,
62+
instead="an import from the top-level trio_asyncio package",
63+
)
64+
for name in (
65+
"adapter", "async_", "base", "child", "handles", "loop", "sync", "util"
66+
)
67+
}
68+
69+
# Provide aliases in the old place for names that moved between modules.
70+
# Remove these when the non-underscore-prefixed module names are removed.
71+
from . import _loop, _async
72+
_async.open_loop = _loop.open_loop
73+
74+
_util.fixup_module_metadata(__name__, globals())

trio_asyncio/adapter.py renamed to trio_asyncio/_adapter.py

Lines changed: 10 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,15 @@
88
import trio_asyncio
99
from contextvars import ContextVar
1010

11-
from .util import run_aio_generator, run_aio_future, run_trio_generator
12-
13-
current_loop = ContextVar('trio_aio_loop', default=None)
14-
current_policy = ContextVar('trio_aio_policy', default=None)
11+
from ._deprecate import deprecated_alias
12+
from ._util import run_aio_generator, run_aio_future, run_trio_generator
13+
from ._loop import current_loop, current_policy
1514

1615
# import logging
1716
# logger = logging.getLogger(__name__)
1817

1918
from functools import wraps, partial
2019

21-
__all__ = [
22-
'trio2aio', 'aio2trio', 'aio_as_trio', 'trio_as_aio', 'allow_asyncio', 'current_loop',
23-
'current_policy', 'asyncio_as_trio', 'trio_as_asyncio'
24-
]
25-
26-
27-
def trio2aio(proc):
28-
"""Call asyncio code from Trio.
29-
30-
Deprecated: Use aio_as_trio() instead.
31-
32-
"""
33-
warnings.warn("Use 'aio_as_trio(proc)' instead'", DeprecationWarning)
34-
35-
return aio_as_trio(proc)
36-
3720

3821
async def _call_defer(proc, *args, **kwargs):
3922
return await proc(*args, **kwargs)
@@ -210,30 +193,6 @@ def trio_as_aio(proc, *, loop=None):
210193
trio_as_asyncio = trio_as_aio
211194

212195

213-
def aio2trio(proc):
214-
"""Call asyncio code from Trio.
215-
216-
Deprecated: Use aio_as_trio() instead.
217-
218-
"""
219-
warnings.warn("Use 'trio_as_aio(proc)' instead'", DeprecationWarning)
220-
221-
return trio_as_aio(proc)
222-
223-
224-
def aio2trio_task(proc):
225-
warnings.warn("Use loop.run_trio_task() instead", DeprecationWarning)
226-
227-
@wraps(proc)
228-
async def call(*args, **kwargs):
229-
proc_ = proc
230-
if kwargs:
231-
proc_ = partial(proc_, **kwargs)
232-
trio_asyncio.run_trio_task(proc_, *args)
233-
234-
return call
235-
236-
237196
@types.coroutine
238197
def _allow_asyncio(fn, *args):
239198
shim = trio_asyncio.base._shim_running
@@ -261,6 +220,8 @@ def _allow_asyncio(fn, *args):
261220
p, a = coro.send, next_send
262221

263222

223+
_shim_running = ContextVar("shim_running", default=False)
224+
264225
async def allow_asyncio(fn, *args):
265226
"""
266227
This wrapper allows you to indiscrimnately mix :mod:`trio` and
@@ -287,11 +248,15 @@ async def main():
287248
288249
This function must be called from :mod:`trio` context.
289250
"""
290-
shim = trio_asyncio.base._shim_running
251+
shim = _shim_running
291252
if shim.get(): # nested call: skip
292253
return await fn(*args)
293254
token = shim.set(True)
294255
try:
295256
return await _allow_asyncio(fn, *args)
296257
finally:
297258
shim.reset(token)
259+
260+
261+
trio2aio = deprecated_alias("trio_asyncio.trio2aio", aio_as_trio, "0.10.0", issue=38)
262+
aio2trio = deprecated_alias("trio_asyncio.aio2trio", trio_as_aio, "0.10.0", issue=38)

trio_asyncio/async_.py renamed to trio_asyncio/_async.py

Lines changed: 2 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import trio
22

3-
from async_generator import async_generator, yield_, asynccontextmanager
4-
5-
from .base import BaseTrioEventLoop
6-
from .handles import Handle
7-
8-
__all__ = ['TrioEventLoop', 'open_loop']
3+
from ._base import BaseTrioEventLoop
4+
from ._handles import Handle
95

106

117
class TrioEventLoop(BaseTrioEventLoop):
@@ -81,53 +77,3 @@ def _close(self):
8177
if not self._stopped.is_set():
8278
raise RuntimeError("You need to stop the loop before closing it")
8379
super()._close()
84-
85-
86-
@asynccontextmanager
87-
@async_generator
88-
async def open_loop(queue_len=None):
89-
"""Main entry point: run an asyncio loop on top of Trio.
90-
91-
This is a context manager.
92-
93-
Example usage::
94-
95-
async def async_main(*args):
96-
async with trio_asyncio.open_loop() as loop:
97-
pass
98-
# async part of your main program here
99-
100-
"""
101-
102-
# TODO: make sure that there is no asyncio loop already running
103-
104-
def _main_loop_exit(self):
105-
super()._main_loop_exit()
106-
self._thread = None
107-
108-
from .loop import current_loop, current_policy, TrioPolicy
109-
110-
async with trio.open_nursery() as nursery:
111-
policy = current_policy.get()
112-
if not isinstance(policy, TrioPolicy):
113-
policy = TrioPolicy()
114-
old_policy = current_policy.set(policy)
115-
116-
loop = TrioEventLoop(queue_len=queue_len)
117-
old_loop = current_loop.set(loop)
118-
try:
119-
loop._closed = False
120-
await loop._main_loop_init(nursery)
121-
await nursery.start(loop._main_loop)
122-
await yield_(loop)
123-
finally:
124-
try:
125-
await loop.stop().wait()
126-
finally:
127-
try:
128-
await loop._main_loop_exit()
129-
finally:
130-
loop.close()
131-
nursery.cancel_scope.cancel()
132-
current_loop.reset(old_loop)
133-
current_policy.reset(old_policy)

0 commit comments

Comments
 (0)