From 45969a656b26452cb980c9537dcc087977811573 Mon Sep 17 00:00:00 2001 From: Jeroen Demeyer Date: Mon, 18 Jun 2012 21:26:44 +0000 Subject: [PATCH] Clean up interrupt() --- src/sage/interfaces/expect.py | 44 ++++++++++++----------------------- 1 file changed, 15 insertions(+), 29 deletions(-) diff --git a/src/sage/interfaces/expect.py b/src/sage/interfaces/expect.py index f1660a09beb..034df830ed3 100644 --- a/src/sage/interfaces/expect.py +++ b/src/sage/interfaces/expect.py @@ -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()) @@ -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 @@ -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() @@ -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. @@ -971,7 +970,7 @@ def _before(self): EXAMPLES:: - sage: singular(2+3) + sage: singular('2+3') 5 sage: singular._before() '5\r\n' @@ -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"""