Skip to content

Commit ad21d37

Browse files
authoredFeb 2, 2018
Merge pull request #6213 from nyalldawson/interrupt_console
[console] Allow breaking execution of scripts via Ctrl-C
2 parents 5c5ef3b + f6269c4 commit ad21d37

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed
 

‎python/console/console_output.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def __init__(self, shellOut, out=None, style=None):
3939
self.sO = shellOut
4040
self.out = None
4141
self.style = style
42+
self.fire_keyboard_interrupt = False
4243

4344
def write(self, m):
4445
if self.style == "_traceback":
@@ -62,6 +63,10 @@ def write(self, m):
6263
if self.style != "_traceback":
6364
QCoreApplication.processEvents()
6465

66+
if self.fire_keyboard_interrupt:
67+
self.fire_keyboard_interrupt = False
68+
raise KeyboardInterrupt
69+
6570
def move_cursor_to_end(self):
6671
"""Move cursor to end of text"""
6772
line, index = self.get_end_pos()

‎python/console/console_sci.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,9 +378,16 @@ def keyPressEvent(self, e):
378378
if not self.is_cursor_on_edition_zone() or startLine < endLine:
379379
# allow copying and selecting
380380
if e.modifiers() & (Qt.ControlModifier | Qt.MetaModifier):
381-
if e.key() in (Qt.Key_C, Qt.Key_A):
381+
if e.key() == Qt.Key_C:
382+
# only catch and return from Ctrl-C here if there's a selection
383+
if self.hasSelectedText():
384+
QsciScintilla.keyPressEvent(self, e)
385+
return
386+
elif e.key() == Qt.Key_A:
382387
QsciScintilla.keyPressEvent(self, e)
383-
return
388+
return
389+
else:
390+
return
384391
# allow selection
385392
if e.modifiers() & Qt.ShiftModifier:
386393
if e.key() in (Qt.Key_Left, Qt.Key_Right, Qt.Key_Home, Qt.Key_End):
@@ -389,6 +396,11 @@ def keyPressEvent(self, e):
389396
# all other keystrokes get sent to the input line
390397
self.move_cursor_to_end()
391398

399+
if e.modifiers() & (Qt.ControlModifier | Qt.MetaModifier) and e.key() == Qt.Key_C and not self.hasSelectedText():
400+
# keyboard interrupt
401+
sys.stdout.fire_keyboard_interrupt = True
402+
return
403+
392404
line, index = self.getCursorPosition()
393405
cmd = self.text(line)
394406

@@ -602,12 +614,14 @@ def write(self, txt):
602614
sys.stderr.write(txt)
603615

604616
def writeCMD(self, txt):
617+
sys.stdout.fire_keyboard_interrupt = False
605618
if len(txt) > 0:
606619
getCmdString = self.text()
607620
prompt = getCmdString[0:4]
608621
sys.stdout.write(prompt + txt + '\n')
609622

610623
def runsource(self, source, filename='<input>', symbol='single'):
624+
sys.stdout.fire_keyboard_interrupt = False
611625
hook = sys.excepthook
612626
try:
613627
def excepthook(etype, value, tb):

0 commit comments

Comments
 (0)
Please sign in to comment.