Skip to content

Commit fd4d20b

Browse files
maartenbreddelsZsailer
authored andcommitted
fix for tornado 6, backport of jupyter/notebook#4449
1 parent 0e5c2b8 commit fd4d20b

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

.travis.yml

+4
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ matrix:
8383
dist: xenial # required for Python >= 3.7 (travis-ci/travis-ci#9069)
8484
- python: 3.6
8585
env: GROUP=docs
86+
- python: 3.6 # extra test for older tornados
87+
env:
88+
- GROUP=python
89+
- EXTRA_PIP="tornado<5"
8690

8791
after_success:
8892
- codecov

jupyter_server/utils.py

+32-6
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,23 @@
1717

1818

1919
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
2135
from urllib.request import pathname2url
36+
2237
except ImportError:
2338
from urllib import quote, unquote, pathname2url
2439
from urlparse import urlparse, urljoin
@@ -317,18 +332,29 @@ def _check_pid_posix(pid):
317332

318333

319334
def maybe_future(obj):
320-
"""Like tornado's deprecated gen.maybe_future
321-
335+
"""Like tornado's gen.maybe_future
322336
but more compatible with asyncio for recent versions
323337
of tornado
324338
"""
325-
if inspect.isawaitable(obj):
339+
if isinstance(obj, TornadoFuture):
340+
return obj
341+
elif isawaitable(obj):
326342
return asyncio.ensure_future(obj)
327-
elif isinstance(obj, concurrent.futures.Future):
343+
elif isinstance(obj, ConcurrentFuture):
328344
return asyncio.wrap_future(obj)
329345
else:
330346
# not awaitable, wrap scalar in future
331-
f = asyncio.Future()
347+
f = TornadoFuture()
332348
f.set_result(obj)
333349
return f
334350

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

Comments
 (0)