Skip to content

Commit 18cb350

Browse files
authored
Use loop.create_task with interact() (#270)
* Use loop.create_task with interact() Passing coroutines directly to asyncio.wait is deprecated since Python 3.8, as it leads to confusing behavior: https://docs.python.org/3.10/library/asyncio-task.html#asyncio-example-wait-coroutine In Python 3.11, wait() no longer accepts coroutines and must be given Tasks. * Put task handling in an async function and use asyncio.run() * Drop Python 3.6 support and switch to 3.7+ API * setup.py: Remove Minimal Python version sanity check
1 parent bcf17a3 commit 18cb350

File tree

4 files changed

+22
-63
lines changed

4 files changed

+22
-63
lines changed

.github/workflows/python-package.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
runs-on: ubuntu-latest
1616
strategy:
1717
matrix:
18-
python-version: ['3.6', '3.7', '3.8', '3.9', '3.10']
18+
python-version: ['3.7', '3.8', '3.9', '3.10']
1919

2020
steps:
2121
- uses: actions/checkout@v2

jupyter_console/ptshell.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -649,31 +649,31 @@ async def interact(self, loop=None, display_banner=None):
649649
if code:
650650
self.run_cell(code, store_history=True)
651651

652+
async def _main_task(self):
653+
loop = asyncio.get_running_loop()
654+
tasks = [asyncio.create_task(self.interact(loop=loop))]
655+
656+
if self.include_other_output:
657+
# only poll the iopub channel asynchronously if we
658+
# wish to include external content
659+
tasks.append(asyncio.create_task(self.handle_external_iopub(loop=loop)))
660+
661+
_, pending = await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)
662+
663+
for task in pending:
664+
task.cancel()
665+
try:
666+
await asyncio.gather(*pending)
667+
except asyncio.CancelledError:
668+
pass
669+
652670
def mainloop(self):
653671
self.keepkernel = not self.own_kernel
654-
loop = asyncio.get_event_loop()
655672
# An extra layer of protection in case someone mashing Ctrl-C breaks
656673
# out of our internal code.
657674
while True:
658675
try:
659-
tasks = [self.interact(loop=loop)]
660-
661-
if self.include_other_output:
662-
# only poll the iopub channel asynchronously if we
663-
# wish to include external content
664-
tasks.append(self.handle_external_iopub(loop=loop))
665-
666-
main_task = asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)
667-
_, pending = loop.run_until_complete(main_task)
668-
669-
for task in pending:
670-
task.cancel()
671-
try:
672-
loop.run_until_complete(asyncio.gather(*pending))
673-
except asyncio.CancelledError:
674-
pass
675-
loop.stop()
676-
loop.close()
676+
asyncio.run(self._main_task())
677677
break
678678
except KeyboardInterrupt:
679679
print("\nKeyboardInterrupt escaped interact()\n")

mypy.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[mypy]
2-
python_version = 3.6
2+
python_version = 3.7
33
ignore_missing_imports = True
44
follow_imports = silent

setup.py

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,46 +9,6 @@
99
# the name of the project
1010
name = 'jupyter_console'
1111

12-
#-----------------------------------------------------------------------------
13-
# Minimal Python version sanity check
14-
#-----------------------------------------------------------------------------
15-
16-
import sys
17-
18-
19-
if sys.version_info < (3, 6):
20-
pip_message = 'This may be due to an out of date pip. Make sure you have pip >= 9.0.1.'
21-
try:
22-
import pip
23-
pip_version = tuple([int(x) for x in pip.__version__.split('.')[:3]])
24-
if pip_version < (9, 0, 1) :
25-
pip_message = 'Your pip version is out of date, please install pip >= 9.0.1. '\
26-
'pip {} detected.'.format(pip.__version__)
27-
else:
28-
# pip is new enough - it must be something else
29-
pip_message = ''
30-
except Exception:
31-
pass
32-
33-
34-
error = """
35-
Jupyter_Console 6.2+ supports Python 3.6 and above.
36-
When using Python 2.7, please install and older version of Jupyter Console
37-
Python 3.3 and 3.4 were supported up to Jupyter Console 5.x.
38-
Python 3.5 was supported up to Jupyter Console 6.1.0.
39-
40-
Python {py} detected.
41-
{pip}
42-
""".format(py=sys.version_info, pip=pip_message )
43-
44-
print(error, file=sys.stderr)
45-
sys.exit(1)
46-
47-
48-
#-----------------------------------------------------------------------------
49-
# get on with it
50-
#-----------------------------------------------------------------------------
51-
5212
import os
5313

5414
from setuptools import setup
@@ -82,7 +42,6 @@
8242
'License :: OSI Approved :: BSD License',
8343
'Programming Language :: Python',
8444
'Programming Language :: Python :: 3',
85-
'Programming Language :: Python :: 3.6',
8645
'Programming Language :: Python :: 3.7',
8746
'Programming Language :: Python :: 3.8',
8847
'Programming Language :: Python :: 3.9',
@@ -97,7 +56,7 @@
9756
extras_require={
9857
'test:sys_platform != "win32"': ['pexpect'],
9958
},
100-
python_requires='>=3.6',
59+
python_requires='>=3.7',
10160
entry_points={
10261
'console_scripts': [
10362
'jupyter-console = jupyter_console.app:main',

0 commit comments

Comments
 (0)