|
17 | 17 |
|
18 | 18 |
|
19 | 19 | try:
|
20 |
| - from urllib.parse import quote, unquote, urlparse, urljoin |
| 20 | + from inspect import isawaitable |
| 21 | +except ImportError: |
| 22 | + def isawaitable(f): |
| 23 | + """If isawaitable is undefined, nothing is awaitable""" |
| 24 | + return False |
| 25 | + |
| 26 | +try: |
| 27 | + from concurrent.futures import Future as ConcurrentFuture |
| 28 | +except ImportError: |
| 29 | + class ConcurrentFuture: |
| 30 | + """If concurrent.futures isn't importable, nothing will be a c.f.Future""" |
| 31 | + pass |
| 32 | + |
| 33 | +try: |
| 34 | + from urllib.parse import quote, unquote, urlparse |
21 | 35 | from urllib.request import pathname2url
|
| 36 | + |
22 | 37 | except ImportError:
|
23 | 38 | from urllib import quote, unquote, pathname2url
|
24 | 39 | from urlparse import urlparse, urljoin
|
@@ -317,18 +332,29 @@ def _check_pid_posix(pid):
|
317 | 332 |
|
318 | 333 |
|
319 | 334 | def maybe_future(obj):
|
320 |
| - """Like tornado's deprecated gen.maybe_future |
321 |
| -
|
| 335 | + """Like tornado's gen.maybe_future |
322 | 336 | but more compatible with asyncio for recent versions
|
323 | 337 | of tornado
|
324 | 338 | """
|
325 |
| - if inspect.isawaitable(obj): |
| 339 | + if isinstance(obj, TornadoFuture): |
| 340 | + return obj |
| 341 | + elif isawaitable(obj): |
326 | 342 | return asyncio.ensure_future(obj)
|
327 |
| - elif isinstance(obj, concurrent.futures.Future): |
| 343 | + elif isinstance(obj, ConcurrentFuture): |
328 | 344 | return asyncio.wrap_future(obj)
|
329 | 345 | else:
|
330 | 346 | # not awaitable, wrap scalar in future
|
331 |
| - f = asyncio.Future() |
| 347 | + f = TornadoFuture() |
332 | 348 | f.set_result(obj)
|
333 | 349 | return f
|
334 | 350 |
|
| 351 | +# monkeypatch tornado gen.maybe_future |
| 352 | +# on Python 3 |
| 353 | +# TODO: remove monkeypatch after backporting smaller fix to 5.x |
| 354 | +try: |
| 355 | + import asyncio |
| 356 | +except ImportError: |
| 357 | + pass |
| 358 | +else: |
| 359 | + import tornado.gen |
| 360 | + tornado.gen.maybe_future = maybe_future |
0 commit comments