Example #1
0
    def __init__(self,
                 user_ns=None,
                 global_ns=None,
                 translator=None,
                 magic=None,
                 display_formatters=None,
                 traceback_formatters=None,
                 output_trap=None,
                 history=None,
                 message_cache=None,
                 filename='<string>',
                 config=None):

        # The local/global namespaces for code execution
        local_ns = user_ns  # compatibility name
        if local_ns is None:
            local_ns = {}
        self.user_ns = local_ns
        # The local namespace
        if global_ns is None:
            global_ns = {}
        self.user_global_ns = global_ns

        # An object that will translate commands into executable Python.
        # The current translator does not work properly so for now we are going
        # without!
        # if translator is None:
        #             from IPython.kernel.core.translator import IPythonTranslator
        #             translator = IPythonTranslator()
        self.translator = translator

        # An object that maintains magic commands.
        if magic is None:
            from IPython.kernel.core.magic import Magic
            magic = Magic(self)
        self.magic = magic

        # A list of formatters for the displayhook.
        if display_formatters is None:
            display_formatters = default_display_formatters()
        self.display_formatters = display_formatters

        # A list of formatters for tracebacks.
        if traceback_formatters is None:
            traceback_formatters = default_traceback_formatters()
        self.traceback_formatters = traceback_formatters

        # The object trapping stdout/stderr.
        if output_trap is None:
            from IPython.kernel.core.output_trap import OutputTrap
            output_trap = OutputTrap()
        self.output_trap = output_trap

        # An object that manages the history.
        if history is None:
            from IPython.kernel.core.history import InterpreterHistory
            history = InterpreterHistory()
        self.history = history
        self.get_history_item = history.get_history_item
        self.get_history_input_cache = history.get_input_cache
        self.get_history_input_after = history.get_input_after

        # An object that caches all of the return messages.
        if message_cache is None:
            from IPython.kernel.core.message_cache import SimpleMessageCache
            message_cache = SimpleMessageCache()
        self.message_cache = message_cache

        # The "filename" of the code that is executed in this interpreter.
        self.filename = filename

        # An object that contains much configuration information.
        if config is None:
            # fixme: Move this constant elsewhere!
            config = Bunch(ESC_MAGIC='%')
        self.config = config

        # Hook managers.
        # fixme: make the display callbacks configurable. In the meantime,
        # enable macros.
        self.display_trap = DisplayTrap(
            formatters=self.display_formatters,
            callbacks=[self._possible_macro],
        )
        self.traceback_trap = TracebackTrap(
            formatters=self.traceback_formatters)

        # This is used temporarily for reformating exceptions in certain
        # cases.  It will go away once the ultraTB stuff is ported
        # to ipython1
        self.tbHandler = ultraTB.FormattedTB(color_scheme='NoColor',
                                             mode='Context',
                                             tb_offset=2)

        # An object that can compile commands and remember __future__
        # statements.
        self.command_compiler = codeop.CommandCompiler()

        # A replacement for the raw_input() and input() builtins. Change these
        # attributes later to configure them.
        self.raw_input_builtin = raw_input
        self.input_builtin = input

        # The number of the current cell.
        self.current_cell_number = 1

        # Initialize cache, set in/out prompts and printing system
        self.outputcache = CachedOutput(self,
                                        rc.cache_size,
                                        rc.pprint,
                                        input_sep=rc.separate_in,
                                        output_sep=rc.separate_out,
                                        output_sep2=rc.separate_out2,
                                        ps1=rc.prompt_in1,
                                        ps2=rc.prompt_in2,
                                        ps_out=rc.prompt_out,
                                        pad_left=rc.prompts_pad_left)

        # Need to decide later if this is the right approach, but clients
        # commonly use sys.ps1/2, so it may be best to just set them here
        sys.ps1 = self.outputcache.prompt1.p_str
        sys.ps2 = self.outputcache.prompt2.p_str

        # This is the message dictionary assigned temporarily when running the
        # code.
        self.message = None

        self.setup_namespace()