Skip to content

Commit

Permalink
Better handle ^C; filter pointless warnings on exit; edit banner
Browse files Browse the repository at this point in the history
  • Loading branch information
1st1 committed May 23, 2019
1 parent dc35401 commit 7a973fb
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions Lib/asyncio/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import sys
import threading
import types
import warnings

from . import futures

Expand All @@ -23,10 +24,20 @@ def runcode(self, code):

def callback():
global repl_future
global repl_future_interrupted

repl_future = None
repl_future_interrupted = False

func = types.FunctionType(code, self.locals)
try:
coro = func()
except SystemExit:
raise
except KeyboardInterrupt as ex:
repl_future_interrupted = True
future.set_exception(ex)
return
except BaseException as ex:
future.set_exception(ex)
return
Expand All @@ -48,15 +59,19 @@ def callback():
except SystemExit:
raise
except BaseException:
self.showtraceback()
if repl_future_interrupted:
self.write("\nKeyboardInterrupt\n")
else:
self.showtraceback()


class REPLThread(threading.Thread):

def run(self):
try:
banner = (
f'asyncio REPL {sys.version} on {sys.platform}\n'
f'asyncio REPL {sys.version} on {sys.platform}\n\n'
f'Use "await" directly instead of asyncio.run().\n'
f'Type "help", "copyright", "credits" or "license" '
f'for more information.\n\n'
f'{getattr(sys, "ps1", ">>> ")}import asyncio\n'
Expand All @@ -66,6 +81,11 @@ def run(self):
banner=banner,
exitmsg='exiting asyncio REPL...')
finally:
warnings.filterwarnings(
'ignore',
message=r'^coroutine .* was never awaited$',
category=RuntimeWarning)

loop.call_soon_threadsafe(loop.stop)


Expand All @@ -80,21 +100,25 @@ def run(self):
repl_locals[key] = locals()[key]

console = AsyncIOInteractiveConsole(repl_locals, loop)

repl_future = None
repl_future_interrupted = False

try:
import readline # NoQA
except ImportError:
pass

REPLThread().start()
repl_thread = REPLThread()
repl_thread.start()

while True:
try:
loop.run_forever()
except KeyboardInterrupt:
if repl_future and not repl_future.done():
repl_future.cancel()
repl_future_interrupted = True
continue
else:
break

0 comments on commit 7a973fb

Please sign in to comment.