Skip to content

Commit

Permalink
[ipython] Do some sanity checks before starting the console
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Elad Alfassa committed May 4, 2014
1 parent 1330b4f commit e190120
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions ipython.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down

0 comments on commit e190120

Please sign in to comment.