#coding: utf8
"""
ipython.py - willie ipython console!
Copyright © 2014, Elad Alfassa <elad@fedoraproject.org>
Licensed under the Eiffel Forum License 2.

Willie: http://willie.dftba.net/
"""
from __future__ import unicode_literals
import willie
import sys
from IPython.frontend.terminal.embed import InteractiveShellEmbed
console = None


@willie.module.commands('console')
def interactive_shell(bot, trigger):
    """
    Starts an interactive IPython console
    """
    global console
    if not trigger.admin:
        bot.say('Only admins can start the interactive console')
        return
    if 'iconsole_running' in bot.memory and bot.memory['iconsole_running']:
        bot.say('Console already running')
        return

    # Backup stderr/stdout wrappers
    old_stdout = sys.stdout
    old_stderr = sys.stderr

    # IPython wants actual stderr and stdout
    sys.stdout = sys.__stdout__
    sys.stderr = sys.__stderr__

    banner1 = 'Willie interactive shell (embedded IPython)'
    banner2 = '`bot` and `trigger` are available. To exit, type exit'
    exitmsg = 'Interactive shell closed'

    console = InteractiveShellEmbed(banner1=banner1, banner2=banner2,
                                    exit_msg=exitmsg)

    bot.memory['iconsole_running'] = True
    bot.say('console started')
    console()
    bot.memory['iconsole_running'] = False

    # Restore stderr/stdout wrappers
    sys.stdout = old_stdout
    sys.stderr = old_stderr

@@ -254,73 +254,573 @@
 sys%0A
-from IPython.frontend.terminal.embed import InteractiveShellEmbed
+if sys.version_info.major %3E= 3:%0A    # Backup stderr/stdout wrappers%0A    old_stdout = sys.stdout%0A    old_stderr = sys.stderr%0A%0A    # IPython wants actual stderr and stdout. In Python 2, it only needed that%0A    # when actually starting the console, but in Python 3 it seems to need that%0A    # on import as well%0A    sys.stdout = sys.__stdout__%0A    sys.stderr = sys.__stderr__%0A%0Afrom IPython.frontend.terminal.embed import InteractiveShellEmbed%0A%0Aif sys.version_info.major %3E= 3:%0A    # Restore stderr/stdout wrappers%0A    sys.stdout = old_stdout%0A    sys.stderr = old_stderr%0A
 %0Acon

Make ipython work for Python 3 as well