Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/ptpython/repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import asyncio
import builtins
import inspect
import os
import signal
import sys
Expand Down Expand Up @@ -192,6 +193,7 @@ def run(self) -> None:
async def run_and_show_expression_async(self, text: str) -> Any:
loop = asyncio.get_running_loop()
system_exit: SystemExit | None = None
is_signal_handled = False

try:
try:
Expand All @@ -208,7 +210,10 @@ async def eval() -> Any:
system_exit = e

task = asyncio.create_task(eval())
loop.add_signal_handler(signal.SIGINT, lambda *_: task.cancel())
f = loop.add_signal_handler
if "raise NotImplementedError" not in inspect.getsource(f):
f(signal.SIGINT, lambda *_: task.cancel())
is_signal_handled = True
result = await task

if system_exit is not None:
Expand All @@ -231,7 +236,8 @@ async def eval() -> Any:
# Return the result for future consumers.
return result
finally:
loop.remove_signal_handler(signal.SIGINT)
if is_signal_handled:
loop.remove_signal_handler(signal.SIGINT)

except KeyboardInterrupt as e:
# Handle all possible `KeyboardInterrupt` errors. This can
Expand Down