Codebase list python-txaio / 61268da
remove legacy code Omer Akram 4 years ago
5 changed file(s) with 35 addition(s) and 58 deletion(s). Raw diff Collapse all Expand all
3131
3232 # This is the API
3333 # see tx.py for Twisted implementation
34 # see aio.py for asyncio/trollius implementation
34 # see aio.py for asyncio implementation
3535
3636
3737 class _Config(object):
0 ###############################################################################
1 #
2 # The MIT License (MIT)
3 #
4 # Copyright (c) Crossbar.io Technologies GmbH
5 #
6 # Permission is hereby granted, free of charge, to any person obtaining a copy
7 # of this software and associated documentation files (the "Software"), to deal
8 # in the Software without restriction, including without limitation the rights
9 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 # copies of the Software, and to permit persons to whom the Software is
11 # furnished to do so, subject to the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be included in
14 # all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 # THE SOFTWARE.
23 #
24 ###############################################################################
025
126 import math
227 from txaio.interfaces import IBatchedTimer
2323 #
2424 ###############################################################################
2525
26 import asyncio
27 from asyncio import iscoroutine
28 from asyncio import Future
29 from types import AsyncGeneratorType
2630 import io
2731 import os
2832 import sys
4044 from txaio._common import _BatchedTimer
4145 from txaio import _util
4246 from txaio import _Config
43
44
45 try:
46 import asyncio
47 from asyncio import iscoroutine
48 from asyncio import Future
49
50 except ImportError:
51 # Trollius >= 0.3 was renamed
52 # noinspection PyUnresolvedReferences
53 import trollius as asyncio
54 from trollius import iscoroutine
55 from trollius import Future
56
57 try:
58 from types import AsyncGeneratorType # python 3.5+
59 except ImportError:
60 class AsyncGeneratorType(object):
61 pass
62
63
64 def _create_future_of_loop(loop):
65 return loop.create_future()
66
67
68 def _create_future_directly(loop=None):
69 return Future(loop=loop)
70
71
72 def _create_task_of_loop(res, loop):
73 return loop.create_task(res)
74
75
76 def _create_task_directly(res, loop=None):
77 return asyncio.Task(res, loop=loop)
78
79
80 if sys.version_info >= (3, 4, 2):
81 _create_task = _create_task_of_loop
82 if sys.version_info >= (3, 5, 2):
83 _create_future = _create_future_of_loop
84 else:
85 _create_future = _create_future_directly
86 else:
87 _create_task = _create_task_directly
88 _create_future = _create_future_directly
8947
9048
9149 config = _Config()
391349 if result is not _unspecified and error is not _unspecified:
392350 raise ValueError("Cannot have both result and error.")
393351
394 f = _create_future(loop=self._config.loop)
352 f = self._config.loop.create_future()
395353 if result is not _unspecified:
396354 resolve(f, result)
397355 elif error is not _unspecified:
428386 if isinstance(res, Future):
429387 return res
430388 elif iscoroutine(res):
431 return _create_task(res, loop=self._config.loop)
389 return self._config.loop.create_task(res)
432390 elif isinstance(res, AsyncGeneratorType):
433391 raise RuntimeError(
434392 "as_future() received an async generator function; does "
2222 # THE SOFTWARE.
2323 #
2424 ###############################################################################
25
2625
2726 import txaio
2827 from contextlib import contextmanager
4545 from txaio import _util
4646
4747
48 PY3_CORO = False
49 try:
50 from twisted.internet.defer import ensureDeferred
51 from asyncio import iscoroutinefunction
52 PY3_CORO = True
53 except ImportError:
54 pass
48 from twisted.internet.defer import ensureDeferred
49 from asyncio import iscoroutinefunction
5550
5651 using_twisted = True
5752 using_asyncio = False
421416
422417 def as_future(self, fun, *args, **kwargs):
423418 # Twisted doesn't automagically deal with coroutines on Py3
424 if PY3_CORO and iscoroutinefunction(fun):
419 if iscoroutinefunction(fun):
425420 return ensureDeferred(fun(*args, **kwargs))
426421 return maybeDeferred(fun, *args, **kwargs)
427422