Skip to content

Commit

Permalink
Clean up interrupt()
Browse files Browse the repository at this point in the history
  • Loading branch information
jdemeyer committed Jun 23, 2015
1 parent 05875d5 commit 45969a6
Showing 1 changed file with 15 additions and 29 deletions.
44 changes: 15 additions & 29 deletions src/sage/interfaces/expect.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,8 @@ def _send_interrupt(self):
"""
Send an interrupt to the application. This is used internally
by :meth:`interrupt`.
First CTRL-C to stop the current command, then quit.
"""
self._expect.sendline(chr(3))
self._expect.sendline(self._quit_string())
Expand Down Expand Up @@ -838,7 +840,7 @@ def _eval_line(self, line, allow_use_file=True, wait_for_prompt=True, restart_if
Since the test of the next method would fail, we re-start
Singular now. ::
sage: singular(2+3)
sage: singular('2+3')
Singular crashed -- automatically restarting.
5
Expand Down Expand Up @@ -938,11 +940,10 @@ def _keyboard_interrupt(self):
self._expect.expect(self._prompt)
raise KeyboardInterrupt("Ctrl-c pressed while running %s"%self)

def interrupt(self, tries=20, timeout=0.3, quit_on_fail=True):
def interrupt(self, tries=5, timeout=2.0, quit_on_fail=True):
E = self._expect
if E is None:
return True
success = False
try:
for i in range(tries):
self._send_interrupt()
Expand All @@ -951,15 +952,13 @@ def interrupt(self, tries=20, timeout=0.3, quit_on_fail=True):
except (pexpect.TIMEOUT, pexpect.EOF):
pass
else:
success = True
break
return True # Success
except Exception:
pass
if success:
pass
elif quit_on_fail:
# Failed to interrupt...
if quit_on_fail:
self.quit()
return success
return False

###########################################################################
# BEGIN Synchronization code.
Expand All @@ -971,7 +970,7 @@ def _before(self):
EXAMPLES::
sage: singular(2+3)
sage: singular('2+3')
5
sage: singular._before()
'5\r\n'
Expand Down Expand Up @@ -1066,35 +1065,22 @@ def _expect_expr(self, expr=None, timeout=None):
"""
if expr is None:
# the following works around gap._prompt_wait not being defined
expr = (hasattr(self,'_prompt_wait') and self._prompt_wait) or self._prompt
expr = getattr(self, '_prompt_wait', None) or self._prompt
if self._expect is None:
self._start()
try:
if timeout:
i = self._expect.expect(expr,timeout=timeout)
i = self._expect.expect(expr, timeout=timeout)
else:
i = self._expect.expect(expr)
if i > 0:
v = self._expect.before
self.quit()
raise ValueError("%s\nComputation failed due to a bug in %s -- NOTE: Had to restart."%(v, self))
except KeyboardInterrupt as err:
i = 0
while True:
try:
print "Control-C pressed. Interrupting %s. Please wait a few seconds..."%self
self._sendstr('quit;\n'+chr(3))
self._sendstr('quit;\n'+chr(3))
self.interrupt()
self.interrupt()
except KeyboardInterrupt:
i += 1
if i > 10:
break
pass
else:
break
raise err
except KeyboardInterrupt:
print("Control-C pressed. Interrupting %s. Please wait a few seconds..."%self)
self.interrupt()
raise

def _sendstr(self, str):
r"""
Expand Down

0 comments on commit 45969a6

Please sign in to comment.