From e1901206c0f01a3438b8b397c8d42d78c192ce04 Mon Sep 17 00:00:00 2001 From: Elad Alfassa Date: Sun, 4 May 2014 21:42:21 +0300 Subject: [PATCH] [ipython] Do some sanity checks before starting the console * Even if IPython is not installed, restore sys.stdout (Python 3 only) * Prevent starting the console without a tty (ie. prevent starting the console when the shell is detached) * Prevent starting the console when running as a deamon --- ipython.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/ipython.py b/ipython.py index 9283459260..e37984a839 100644 --- a/ipython.py +++ b/ipython.py @@ -19,13 +19,13 @@ # on import as well sys.stdout = sys.__stdout__ sys.stderr = sys.__stderr__ - -from IPython.frontend.terminal.embed import InteractiveShellEmbed - -if sys.version_info.major >= 3: - # Restore stderr/stdout wrappers - sys.stdout = old_stdout - sys.stderr = old_stderr +try: + from IPython.frontend.terminal.embed import InteractiveShellEmbed +finally: + if sys.version_info.major >= 3: + # Restore stderr/stdout wrappers + sys.stdout = old_stdout + sys.stderr = old_stderr console = None @@ -42,6 +42,12 @@ def interactive_shell(bot, trigger): if 'iconsole_running' in bot.memory and bot.memory['iconsole_running']: bot.say('Console already running') return + if not sys.__stdout__.isatty(): + bot.say('A tty is required to start the console') + return + if bot.config._is_deamonized: + bot.say('Can\'t start console when running as a deamon') + return # Backup stderr/stdout wrappers old_stdout = sys.stdout