diff options
| author | Damian Johnson <atagar@torproject.org> | 2017-05-26 13:42:30 -0700 |
|---|---|---|
| committer | Damian Johnson <atagar@torproject.org> | 2017-05-26 13:42:30 -0700 |
| commit | f62fc95876fa2c817b7e1aa2a57fe6bd93ef7a78 (patch) | |
| tree | ffa71bcfa42ca6f37bad8b37ab3833bcbbfbbf06 | |
| parent | 56f2cf19d5782c2e3181b40500e113ce3981b329 (diff) | |
Allow interpreter to continue after tor shutsdown
When tor shuts down the next tor-prompt command immediately terminates the
interpreter. This is both unnecessary and sucks. We provide more than a control
connection and should let users continue to issue those commans after tor's
gone.
https://www.atagar.com/transfer/stem/interpreter_shutdown_prompt.png
Neat idea suggested by teor...
https://trac.torproject.org/projects/tor/ticket/22374
| -rw-r--r-- | docs/change_log.rst | 1 | ||||
| -rw-r--r-- | stem/interpreter/__init__.py | 13 | ||||
| -rw-r--r-- | stem/interpreter/commands.py | 3 |
3 files changed, 14 insertions, 3 deletions
diff --git a/docs/change_log.rst b/docs/change_log.rst index a097f5dc..a372e0ce 100644 --- a/docs/change_log.rst +++ b/docs/change_log.rst @@ -72,6 +72,7 @@ The following are only available within Stem's `git repository * **Interpreter** * Added a `'--run [command or path]' argument <tutorials/down_the_rabbit_hole.html#running-individual-commands>`_ to invoke specific commands (:trac:`21541`) + * Allowing interpreter to continue after tor shutsdown (:trac:`22374`) .. _version_1.5: diff --git a/stem/interpreter/__init__.py b/stem/interpreter/__init__.py index 033d819e..b5d9c49d 100644 --- a/stem/interpreter/__init__.py +++ b/stem/interpreter/__init__.py @@ -124,6 +124,7 @@ def main(): readline.set_completer_delims('\n') interpreter = stem.interpreter.commands.ControlInterpreter(controller) + showed_close_confirmation = False if args.run_cmd: if args.run_cmd.upper().startswith('SETEVENTS '): @@ -168,6 +169,18 @@ def main(): prompt = '... ' if interpreter.is_multiline_context else PROMPT user_input = input(prompt) if stem.prereq.is_python_3() else raw_input(prompt) interpreter.run_command(user_input, print_response = True) + except stem.SocketClosed as exc: + if showed_close_confirmation: + print(format("Unable to run tor commands. The control connection has been closed.", *ERROR_OUTPUT)) + else: + prompt = format("Tor's control port has closed. Do you want to continue this interpreter? (y/n) ", *HEADER_BOLD_OUTPUT) + user_input = input(prompt) if stem.prereq.is_python_3() else raw_input(prompt) + print('') # blank line + + if user_input.lower() in ('y', 'yes'): + showed_close_confirmation = True + else: + break except (KeyboardInterrupt, EOFError, stem.SocketClosed) as exc: print('') # move cursor to the following line break diff --git a/stem/interpreter/commands.py b/stem/interpreter/commands.py index 4d845a0d..22558aaf 100644 --- a/stem/interpreter/commands.py +++ b/stem/interpreter/commands.py @@ -309,9 +309,6 @@ class ControlInterpreter(code.InteractiveConsole): :raises: **stem.SocketClosed** if the control connection has been severed """ - if not self._controller.is_alive(): - raise stem.SocketClosed() - # Commands fall into three categories: # # * Interpreter commands. These start with a '/'. |
