Exemplo n.º 1
0
    def __init__(self, ipython0=None, *args, **kwargs):
        """ Parameters
            ----------

            ipython0: an optional ipython0 instance to use for command
            prefiltering and completion.
        """
        LineFrontEndBase.__init__(self, *args, **kwargs)
        self.shell.output_trap = RedirectorOutputTrap(
            out_callback=self.write,
            err_callback=self.write,
        )
        self.shell.traceback_trap = SyncTracebackTrap(
            formatters=self.shell.traceback_trap.formatters, )

        # Start the ipython0 instance:
        self.save_output_hooks()
        if ipython0 is None:
            # Instanciate an IPython0 InteractiveShell to be able to use the
            # prefiltering.
            # Suppress all key input, to avoid waiting
            def my_rawinput(x=None):
                return '\n'

            old_rawinput = __builtin__.raw_input
            __builtin__.raw_input = my_rawinput
            ipython0 = InteractiveShell(
                parent=None,
                user_ns=self.shell.user_ns,
                user_global_ns=self.shell.user_global_ns)
            __builtin__.raw_input = old_rawinput
        self.ipython0 = ipython0
        # Set the pager:
        self.ipython0.set_hook('show_in_pager',
                               lambda s, string: self.write("\n" + string))
        self.ipython0.write = self.write
        self._ip = _ip = self.ipython0
        # Make sure the raw system call doesn't get called, as we don't
        # have a stdin accessible.
        self._ip.system = self.system_call
        # XXX: Muck around with magics so that they work better
        # in our environment
        if not sys.platform.startswith('win'):
            self.ipython0.magic_ls = mk_system_call(self.system_call, 'ls -CF')
        # And now clean up the mess created by ipython0
        self.release_output()

        if not 'banner' in kwargs and self.banner is None:
            self.banner = self.ipython0.banner

        # FIXME: __init__ and start should be two different steps
        self.start()
Exemplo n.º 2
0
    def __init__(self, ipython0=None, *args, **kwargs):
        """ Parameters:
            -----------

            ipython0: an optional ipython0 instance to use for command
            prefiltering and completion.
        """
        LineFrontEndBase.__init__(self, *args, **kwargs)
        self.shell.output_trap = RedirectorOutputTrap(
                            out_callback=self.write,
                            err_callback=self.write,
                                            )
        self.shell.traceback_trap = SyncTracebackTrap(
                        formatters=self.shell.traceback_trap.formatters,
                            )

        # Start the ipython0 instance:
        self.save_output_hooks()
        if ipython0 is None:
            # Instanciate an IPython0 interpreter to be able to use the
            # prefiltering.
            # XXX: argv=[] is a bit bold.
            ipython0 = make_IPython(argv=[], 
                                    user_ns=self.shell.user_ns,
                                    user_global_ns=self.shell.user_global_ns)
        self.ipython0 = ipython0
        # Set the pager:
        self.ipython0.set_hook('show_in_pager', 
                    lambda s, string: self.write("\n" + string))
        self.ipython0.write = self.write
        self._ip = _ip = IPApi(self.ipython0)
        # Make sure the raw system call doesn't get called, as we don't
        # have a stdin accessible.
        self._ip.system = self.system_call
        # XXX: Muck around with magics so that they work better
        # in our environment
        self.ipython0.magic_ls = mk_system_call(self.system_call, 
                                                            'ls -CF')
        # And now clean up the mess created by ipython0
        self.release_output()


        if not 'banner' in kwargs and self.banner is None:
            self.banner = self.ipython0.BANNER + """
This is the wx frontend, by Gael Varoquaux. This is EXPERIMENTAL code."""

        self.start()
Exemplo n.º 3
0
def test_redirector_output_trap():
    """ This test check not only that the redirector_output_trap does
        trap the output, but also that it does it in a gready way, that
        is by calling the callback ASAP.
    """
    from IPython.kernel.core.redirector_output_trap import RedirectorOutputTrap
    out = StringIO()
    trap = RedirectorOutputTrap(out.write, out.write)
    try:
        trap.set()
        for i in range(10):
            os.system('echo %ic' % i)
            print "%ip" % i
            print >> out, i
    except:
        trap.unset()
        raise
    trap.unset()
    result1 = out.getvalue()
    result2 = "".join("%ic\n%ip\n%i\n" % (i, i, i) for i in range(10))
    assert result1 == result2
def test_redirector_output_trap():
    """ This test check not only that the redirector_output_trap does
        trap the output, but also that it does it in a gready way, that
        is by calling the callback ASAP.
    """
    from IPython.kernel.core.redirector_output_trap import RedirectorOutputTrap
    out = StringIO()
    trap = RedirectorOutputTrap(out.write, out.write)
    try:
        trap.set()
        for i in range(10):
            os.system('echo %ic' % i)
            print "%ip" % i
            print >>out, i
    except:
        trap.unset()
        raise
    trap.unset()
    result1 = out.getvalue()
    result2 = "".join("%ic\n%ip\n%i\n" %(i, i, i) for i in range(10))
    assert result1 == result2
Exemplo n.º 5
0
    def test_redirector_output_trap(self):
        """Check the greedy trapping behavior of the traps.
        
        This test check not only that the redirector_output_trap does
        trap the output, but also that it does it in a gready way, that
        is by calling the callback ASAP.
        """
        from IPython.kernel.core.redirector_output_trap import RedirectorOutputTrap

        out = StringIO()
        trap = RedirectorOutputTrap(out.write, out.write)
        try:
            trap.set()
            for i in range(10):
                os.system("echo %ic" % i)
                print("%ip" % i)
                print(i, file=out)
        except:
            trap.unset()
            raise
        trap.unset()
        result1 = out.getvalue()
        result2 = "".join("%ic\n%ip\n%i\n" % (i, i, i) for i in range(10))
        self.assertEquals(result1, result2)
Exemplo n.º 6
0
    def __init__(self, ipython0=None, argv=None, *args, **kwargs):
        """ Parameters
            ----------

            ipython0: an optional ipython0 instance to use for command
            prefiltering and completion.

            argv : list, optional
              Used as the instance's argv value.  If not given, [] is used.
        """
        if argv is None:
            argv = []
        # This is a hack to avoid the IPython exception hook to trigger
        # on exceptions (https://bugs.launchpad.net/bugs/337105)
        # XXX: This is horrible: module-leve monkey patching -> side
        # effects.
        from IPython import iplib
        iplib.InteractiveShell.isthreaded = True

        LineFrontEndBase.__init__(self, *args, **kwargs)
        self.shell.output_trap = RedirectorOutputTrap(
                            out_callback=self.write,
                            err_callback=self.write,
                                            )
        self.shell.traceback_trap = SyncTracebackTrap(
                        formatters=self.shell.traceback_trap.formatters,
                            )

        # Start the ipython0 instance:
        self.save_output_hooks()
        if ipython0 is None:
            # Instanciate an IPython0 interpreter to be able to use the
            # prefiltering.
            # Suppress all key input, to avoid waiting
            def my_rawinput(x=None):
                return '\n'
            old_rawinput = __builtin__.raw_input
            __builtin__.raw_input = my_rawinput
            # XXX: argv=[] is a bit bold.
            ipython0 = make_IPython(argv=argv, 
                                    user_ns=self.shell.user_ns,
                                    user_global_ns=self.shell.user_global_ns)
            __builtin__.raw_input = old_rawinput
        self.ipython0 = ipython0
        # Set the pager:
        self.ipython0.set_hook('show_in_pager', 
                    lambda s, string: self.write("\n" + string))
        self.ipython0.write = self.write
        self._ip = _ip = IPApi(self.ipython0)
        # Make sure the raw system call doesn't get called, as we don't
        # have a stdin accessible.
        self._ip.system = self.system_call
        # XXX: Muck around with magics so that they work better
        # in our environment
        if not sys.platform.startswith('win'):
            self.ipython0.magic_ls = mk_system_call(self.system_call, 
                                                                'ls -CF')
        # And now clean up the mess created by ipython0
        self.release_output()


        if not 'banner' in kwargs and self.banner is None:
            self.banner = self.ipython0.BANNER

        # FIXME: __init__ and start should be two different steps
        self.start()