from IPython.external.qt_for_kernel import QtCore<EOL>from IPython.lib.guisupport import get_app_qt4, start_event_loop_qt4<EOL>kernel.app = get_app_qt4(["<STR_LIT:U+0020>"])<EOL>kernel.app.setQuitOnLastWindowClosed(False)<EOL>kernel.timer = QtCore.QTimer()<EOL>kernel.timer.timeout.connect(kernel.do_one_iteration)<EOL>kernel.timer.start(<NUM_LIT:1000>*kernel._poll_interval)<EOL>start_event_loop_qt4(kernel.app)<EOL>

Start a kernel with PyQt4 event loop integration.


import wx<EOL>from IPython.lib.guisupport import start_event_loop_wx<EOL>doi = kernel.do_one_iteration<EOL>poll_interval = int(<NUM_LIT:1000>*kernel._poll_interval)<EOL>class TimerFrame(wx.Frame):<EOL><INDENT>def __init__(self, func):<EOL><INDENT>wx.Frame.__init__(self, None, -<NUM_LIT:1>)<EOL>self.timer = wx.Timer(self)<EOL>self.timer.Start(poll_interval)<EOL>self.Bind(wx.EVT_TIMER, self.on_timer)<EOL>self.func = func<EOL><DEDENT>def on_timer(self, event):<EOL><INDENT>self.func()<EOL><DEDENT><DEDENT>class IPWxApp(wx.App):<EOL><INDENT>def OnInit(self):<EOL><INDENT>self.frame = TimerFrame(doi)<EOL>self.frame.Show(False)<EOL>return True<EOL><DEDENT><DEDENT>kernel.app = IPWxApp(redirect=False)<EOL>import signal<EOL>if not callable(signal.getsignal(signal.SIGINT)):<EOL><INDENT>signal.signal(signal.SIGINT, signal.default_int_handler)<EOL><DEDENT>start_event_loop_wx(kernel.app)<EOL>

Start a kernel with wx event loop support.


import Tkinter<EOL>doi = kernel.do_one_iteration<EOL>poll_interval = int(<NUM_LIT:1000>*kernel._poll_interval)<EOL>class Timer(object):<EOL><INDENT>def __init__(self, func):<EOL><INDENT>self.app = Tkinter.Tk()<EOL>self.app.withdraw()<EOL>self.func = func<EOL><DEDENT>def on_timer(self):<EOL><INDENT>self.func()<EOL>self.app.after(poll_interval, self.on_timer)<EOL><DEDENT>def start(self):<EOL><INDENT>self.on_timer()  <EOL>self.app.mainloop()<EOL><DEDENT><DEDENT>kernel.timer = Timer(doi)<EOL>kernel.timer.start()<EOL>


from .gui.gtkembed import GTKEmbed<EOL>gtk_kernel = GTKEmbed(kernel)<EOL>gtk_kernel.start()<EOL>

Start the kernel, coordinating with the GTK event loop


import matplotlib<EOL>if matplotlib.__version__ < '<STR_LIT>':<EOL><INDENT>kernel.log.warn(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>)<EOL>return loop_tk(kernel)<EOL><DEDENT>from matplotlib.backends.backend_macosx import TimerMac, show<EOL>poll_interval = int(<NUM_LIT:1000>*kernel._poll_interval)<EOL>real_excepthook = sys.excepthook<EOL>def handle_int(etype, value, tb):<EOL><INDENT>"""<STR_LIT>"""<EOL>if etype is KeyboardInterrupt:<EOL><INDENT>io.raw_print("<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>real_excepthook(etype, value, tb)<EOL><DEDENT><DEDENT>def doi():<EOL><INDENT>sys.excepthook = real_excepthook<EOL>kernel.do_one_iteration()<EOL>sys.excepthook = handle_int<EOL><DEDENT>t = TimerMac(poll_interval)<EOL>t.add_callback(doi)<EOL>t.start()<EOL>poller = zmq.Poller()<EOL>if kernel.control_stream:<EOL><INDENT>poller.register(kernel.control_stream.socket, zmq.POLLIN)<EOL><DEDENT>for stream in kernel.shell_streams:<EOL><INDENT>poller.register(stream.socket, zmq.POLLIN)<EOL><DEDENT>while True:<EOL><INDENT>try:<EOL><INDENT>try:<EOL><INDENT>sys.excepthook = handle_int<EOL>show.mainloop()<EOL>sys.excepthook = real_excepthook<EOL>poller.poll(<NUM_LIT:10>*poll_interval)<EOL>kernel.do_one_iteration()<EOL><DEDENT>except:<EOL><INDENT>raise<EOL><DEDENT><DEDENT>except KeyboardInterrupt:<EOL><INDENT>io.raw_print("<STR_LIT>")<EOL><DEDENT>finally:<EOL><INDENT>sys.excepthook = real_excepthook<EOL><DEDENT><DEDENT>

Start the kernel, coordinating with the Cocoa CFRunLoop event loop
    via the matplotlib MacOSX backend.


if gui not in loop_map:<EOL><INDENT>raise ValueError("<STR_LIT>" % gui)<EOL><DEDENT>if kernel is None:<EOL><INDENT>if Application.initialized():<EOL><INDENT>kernel = getattr(Application.instance(), '<STR_LIT>', None)<EOL><DEDENT>if kernel is None:<EOL><INDENT>raise RuntimeError("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT><DEDENT>loop = loop_map[gui]<EOL>if kernel.eventloop is not None and kernel.eventloop is not loop:<EOL><INDENT>raise RuntimeError("<STR_LIT>")<EOL><DEDENT>kernel.eventloop = loop<EOL>


global IPYTHONDIR<EOL>global env<EOL>global save_get_ipython_dir<EOL>IPYTHONDIR = tempfile.mkdtemp()<EOL>env = os.environ.copy()<EOL>env["<STR_LIT>"] = IPYTHONDIR<EOL>save_get_ipython_dir = path.get_ipython_dir<EOL>path.get_ipython_dir = lambda : IPYTHONDIR<EOL>


kernel = Popen([sys.executable, '<STR_LIT:-c>', cmd], stdout=PIPE, stderr=PIPE, env=env)<EOL>connection_file = os.path.join(IPYTHONDIR,<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>' % kernel.pid<EOL>)<EOL>tic = time.time()<EOL>while not os.path.exists(connection_file) and kernel.poll() is None and time.time() < tic + <NUM_LIT:10>:<EOL><INDENT>time.sleep(<NUM_LIT:0.1>)<EOL><DEDENT>if kernel.poll() is not None:<EOL><INDENT>o,e = kernel.communicate()<EOL>e = py3compat.cast_unicode(e)<EOL>raise IOError("<STR_LIT>" % e)<EOL><DEDENT>if not os.path.exists(connection_file):<EOL><INDENT>if kernel.poll() is None:<EOL><INDENT>kernel.terminate()<EOL><DEDENT>raise IOError("<STR_LIT>" % connection_file)<EOL><DEDENT>km = BlockingKernelManager(connection_file=connection_file)<EOL>km.load_connection_file()<EOL>km.start_channels()<EOL>try:<EOL><INDENT>yield km<EOL><DEDENT>finally:<EOL><INDENT>km.stop_channels()<EOL>kernel.terminate()<EOL><DEDENT>

start an embedded kernel in a subprocess, and wait for it to be ready

    Returns
    -------
    kernel_manager: connected KernelManager instance


for channel in (KM.shell_channel, KM.sub_channel):<EOL><INDENT>while True:<EOL><INDENT>try:<EOL><INDENT>msg = channel.get_msg(block=True, timeout=<NUM_LIT:0.1>)<EOL><DEDENT>except Empty:<EOL><INDENT>break<EOL><DEDENT>else:<EOL><INDENT>list(validate_message(msg))<EOL><DEDENT><DEDENT><DEDENT>


shell = KM.shell_channel<EOL>sub = KM.sub_channel<EOL>msg_id = shell.execute(code=code, **kwargs)<EOL>reply = shell.get_msg(timeout=<NUM_LIT:2>)<EOL>list(validate_message(reply, '<STR_LIT>', msg_id))<EOL>busy = sub.get_msg(timeout=<NUM_LIT:2>)<EOL>list(validate_message(busy, '<STR_LIT:status>', msg_id))<EOL>nt.assert_equals(busy['<STR_LIT:content>']['<STR_LIT>'], '<STR_LIT>')<EOL>if not kwargs.get('<STR_LIT>'):<EOL><INDENT>pyin = sub.get_msg(timeout=<NUM_LIT:2>)<EOL>list(validate_message(pyin, '<STR_LIT>', msg_id))<EOL>nt.assert_equals(pyin['<STR_LIT:content>']['<STR_LIT:code>'], code)<EOL><DEDENT>return msg_id, reply['<STR_LIT:content>']<EOL>

wrapper for doing common steps for validating an execution request


def validate_message(msg, msg_type=None, parent=None):

RMessage().check(msg)<EOL>if msg_type:<EOL><INDENT>yield nt.assert_equals(msg['<STR_LIT>'], msg_type)<EOL><DEDENT>if parent:<EOL><INDENT>yield nt.assert_equal(msg['<STR_LIT>']['<STR_LIT>'], parent)<EOL><DEDENT>content = msg['<STR_LIT:content>']<EOL>ref = references[msg['<STR_LIT>']]<EOL>for tst in ref.check(content):<EOL><INDENT>yield tst<EOL><DEDENT>

validate a message

    This is a generator, and must be iterated through to actually
    trigger each test.

    If msg_type and/or parent are given, the msg_type and/or parent msg_id
    are compared with the given values.


for key in self.trait_names():<EOL><INDENT>yield nt.assert_true(key in d, "<STR_LIT>" % (key, d))<EOL>if d[key] is None:<EOL><INDENT>continue<EOL><DEDENT>try:<EOL><INDENT>setattr(self, key, d[key])<EOL><DEDENT>except TraitError as e:<EOL><INDENT>yield nt.assert_true(False, str(e))<EOL><DEDENT><DEDENT>


if self._in_queue.qsize() == <NUM_LIT:0>:<EOL><INDENT>return False<EOL><DEDENT>else:<EOL><INDENT>return True<EOL><DEDENT>

Is there a message that has been received?


def get_msg(self, block=True, timeout=None):

if block and timeout is None:<EOL><INDENT>timeout = <NUM_LIT><EOL><DEDENT>return self._in_queue.get(block, timeout)<EOL>

Get a message if there is one that is ready.


msgs = []<EOL>while True:<EOL><INDENT>try:<EOL><INDENT>msgs.append(self.get_msg(block=False))<EOL><DEDENT>except Empty:<EOL><INDENT>break<EOL><DEDENT><DEDENT>return msgs<EOL>

Get all messages that are currently ready.


return self._in_queue.get(block, timeout)<EOL>

Gets a message if there is one that is ready.


return not self._in_queue.empty()<EOL>


pass<EOL>


if isinstance(getattr(self.engine, '<STR_LIT:id>', None), int):<EOL><INDENT>return "<STR_LIT>"%self.engine.id<EOL><DEDENT>else:<EOL><INDENT>return "<STR_LIT>"<EOL><DEDENT>

this is a property, in case the handler is created
        before the engine gets registered with an id


def write_connection_file(fname=None, shell_port=<NUM_LIT:0>, iopub_port=<NUM_LIT:0>, stdin_port=<NUM_LIT:0>, hb_port=<NUM_LIT:0>,<EOL>ip=LOCALHOST, key=b'<STR_LIT>'):

<EOL>if not fname:<EOL><INDENT>fname = tempfile.mktemp('<STR_LIT>')<EOL><DEDENT>ports = []<EOL>ports_needed = int(shell_port <= <NUM_LIT:0>) + int(iopub_port <= <NUM_LIT:0>) +int(stdin_port <= <NUM_LIT:0>) + int(hb_port <= <NUM_LIT:0>)<EOL>for i in xrange(ports_needed):<EOL><INDENT>sock = socket.socket()<EOL>sock.bind(('<STR_LIT>', <NUM_LIT:0>))<EOL>ports.append(sock)<EOL><DEDENT>for i, sock in enumerate(ports):<EOL><INDENT>port = sock.getsockname()[<NUM_LIT:1>]<EOL>sock.close()<EOL>ports[i] = port<EOL><DEDENT>if shell_port <= <NUM_LIT:0>:<EOL><INDENT>shell_port = ports.pop(<NUM_LIT:0>)<EOL><DEDENT>if iopub_port <= <NUM_LIT:0>:<EOL><INDENT>iopub_port = ports.pop(<NUM_LIT:0>)<EOL><DEDENT>if stdin_port <= <NUM_LIT:0>:<EOL><INDENT>stdin_port = ports.pop(<NUM_LIT:0>)<EOL><DEDENT>if hb_port <= <NUM_LIT:0>:<EOL><INDENT>hb_port = ports.pop(<NUM_LIT:0>)<EOL><DEDENT>cfg = dict( shell_port=shell_port,<EOL>iopub_port=iopub_port,<EOL>stdin_port=stdin_port,<EOL>hb_port=hb_port,<EOL>)<EOL>cfg['<STR_LIT>'] = ip<EOL>cfg['<STR_LIT:key>'] = bytes_to_str(key)<EOL>with open(fname, '<STR_LIT:w>') as f:<EOL><INDENT>f.write(json.dumps(cfg, indent=<NUM_LIT:2>))<EOL><DEDENT>return fname, cfg<EOL>

Generates a JSON config file, including the selection of random ports.

    Parameters
    ----------

    fname : unicode
        The path to the file to write

    shell_port : int, optional
        The port to use for ROUTER channel.

    iopub_port : int, optional
        The port to use for the SUB channel.

    stdin_port : int, optional
        The port to use for the REQ (raw input) channel.

    hb_port : int, optional
        The port to use for the hearbeat REP channel.

    ip  : str, optional
        The ip address the kernel will bind to.

    key : str, optional
        The Session key used for HMAC authentication.


def base_launch_kernel(code, fname, stdin=None, stdout=None, stderr=None,<EOL>executable=None, independent=False, extra_arguments=[],<EOL>cwd=None):

<EOL>if executable is None:<EOL><INDENT>executable = sys.executable<EOL><DEDENT>arguments = [ executable, '<STR_LIT:-c>', code, '<STR_LIT>', fname ]<EOL>arguments.extend(extra_arguments)<EOL>redirect_in = True<EOL>_stdin = PIPE if stdin is None else stdin<EOL>redirect_out = sys.executable.endswith('<STR_LIT>')<EOL>if redirect_out:<EOL><INDENT>_stdout = PIPE if stdout is None else stdout<EOL>_stderr = PIPE if stderr is None else stderr<EOL><DEDENT>else:<EOL><INDENT>_stdout, _stderr = stdout, stderr<EOL><DEDENT>if sys.platform == '<STR_LIT:win32>':<EOL><INDENT>interrupt_event = ParentPollerWindows.create_interrupt_event()<EOL>arguments += [ '<STR_LIT>'%interrupt_event ]<EOL>if executable.endswith('<STR_LIT>'):<EOL><INDENT>if stdout is None:<EOL><INDENT>arguments.append('<STR_LIT>')<EOL><DEDENT>if stderr is None:<EOL><INDENT>arguments.append('<STR_LIT>')<EOL><DEDENT><DEDENT>if independent:<EOL><INDENT>proc = Popen(arguments,<EOL>creationflags=<NUM_LIT>, <EOL>stdin=_stdin, stdout=_stdout, stderr=_stderr)<EOL><DEDENT>else:<EOL><INDENT>try:<EOL><INDENT>from _winapi import DuplicateHandle, GetCurrentProcess,DUPLICATE_SAME_ACCESS<EOL><DEDENT>except:<EOL><INDENT>from _subprocess import DuplicateHandle, GetCurrentProcess,DUPLICATE_SAME_ACCESS<EOL><DEDENT>pid = GetCurrentProcess()<EOL>handle = DuplicateHandle(pid, pid, pid, <NUM_LIT:0>,<EOL>True, <EOL>DUPLICATE_SAME_ACCESS)<EOL>proc = Popen(arguments + ['<STR_LIT>'%int(handle)],<EOL>stdin=_stdin, stdout=_stdout, stderr=_stderr)<EOL><DEDENT>proc.win32_interrupt_event = interrupt_event<EOL><DEDENT>else:<EOL><INDENT>if independent:<EOL><INDENT>proc = Popen(arguments, preexec_fn=lambda: os.setsid(),<EOL>stdin=_stdin, stdout=_stdout, stderr=_stderr, cwd=cwd)<EOL><DEDENT>else:<EOL><INDENT>proc = Popen(arguments + ['<STR_LIT>'],<EOL>stdin=_stdin, stdout=_stdout, stderr=_stderr, cwd=cwd)<EOL><DEDENT><DEDENT>if redirect_in:<EOL><INDENT>if stdin is None:<EOL><INDENT>proc.stdin.close()<EOL><DEDENT><DEDENT>if redirect_out:<EOL><INDENT>if stdout is None:<EOL><INDENT>proc.stdout.close()<EOL><DEDENT>if stderr is None:<EOL><INDENT>proc.stderr.close()<EOL><DEDENT><DEDENT>return proc<EOL>

Launches a localhost kernel, binding to the specified ports.

    Parameters
    ----------
    code : str,
        A string of Python code that imports and executes a kernel entry point.

    stdin, stdout, stderr : optional (default None)
        Standards streams, as defined in subprocess.Popen.

    fname : unicode, optional
        The JSON connector file, containing ip/port/hmac key information.

    key : str, optional
        The Session key used for HMAC authentication.

    executable : str, optional (default sys.executable)
        The Python executable to use for the kernel process.

    independent : bool, optional (default False)
        If set, the kernel process is guaranteed to survive if this process
        dies. If not set, an effort is made to ensure that the kernel is killed
        when this process dies. Note that in this case it is still good practice
        to kill kernels manually before exiting.

    extra_arguments : list, optional
        A list of extra arguments to pass when executing the launch code.

    cwd : path, optional
        The working dir of the kernel process (default: cwd of this process).

    Returns
    -------
    A tuple of form:
        (kernel_process, shell_port, iopub_port, stdin_port, hb_port)
    where kernel_process is a Popen object and the ports are integers.


return base_launch_kernel('<STR_LIT>',<EOL>*args, **kwargs)<EOL>

Launches a localhost IPython kernel, binding to the specified ports.

    This function simply calls entry_point.base_launch_kernel with the right
    first command to start an ipkernel.  See base_launch_kernel for arguments.

    Returns
    -------
    A tuple of form:
        (kernel_process, shell_port, iopub_port, stdin_port, hb_port)
    where kernel_process is a Popen object and the ports are integers.


def embed_kernel(module=None, local_ns=None, **kwargs):

<EOL>if IPKernelApp.initialized():<EOL><INDENT>app = IPKernelApp.instance()<EOL><DEDENT>else:<EOL><INDENT>app = IPKernelApp.instance(**kwargs)<EOL>app.initialize([])<EOL>main = app.kernel.shell._orig_sys_modules_main_mod<EOL>if main is not None:<EOL><INDENT>sys.modules[app.kernel.shell._orig_sys_modules_main_name] = main<EOL><DEDENT><DEDENT>(caller_module, caller_locals) = extract_module_locals(<NUM_LIT:1>)<EOL>if module is None:<EOL><INDENT>module = caller_module<EOL><DEDENT>if local_ns is None:<EOL><INDENT>local_ns = caller_locals<EOL><DEDENT>app.kernel.user_module = module<EOL>app.kernel.user_ns = local_ns<EOL>app.shell.set_completer_frame()<EOL>app.start()<EOL>

Embed and start an IPython kernel in a given scope.

    Parameters
    ----------
    module : ModuleType, optional
        The module to load into IPython globals (default: caller)
    local_ns : dict, optional
        The namespace to load into IPython user namespace (default: caller)

    kwargs : various, optional
        Further keyword args are relayed to the KernelApp constructor,
        allowing configuration of the Kernel.  Will only have an effect
        on the first embed_kernel call for a given process.


app = IPKernelApp.instance()<EOL>app.initialize()<EOL>app.start()<EOL>


def _eventloop_changed(self, name, old, new):

loop = ioloop.IOLoop.instance()<EOL>loop.add_timeout(time.time()+<NUM_LIT:0.1>, self.enter_eventloop)<EOL>


idents,msg = self.session.feed_identities(msg, copy=False)<EOL>try:<EOL><INDENT>msg = self.session.unserialize(msg, content=True, copy=False)<EOL><DEDENT>except:<EOL><INDENT>self.log.error("<STR_LIT>", exc_info=True)<EOL>return<EOL><DEDENT>self.log.debug("<STR_LIT>", msg)<EOL>header = msg['<STR_LIT>']<EOL>msg_id = header['<STR_LIT>']<EOL>msg_type = header['<STR_LIT>']<EOL>handler = self.control_handlers.get(msg_type, None)<EOL>if handler is None:<EOL><INDENT>self.log.error("<STR_LIT>", msg_type)<EOL><DEDENT>else:<EOL><INDENT>try:<EOL><INDENT>handler(self.control_stream, idents, msg)<EOL><DEDENT>except Exception:<EOL><INDENT>self.log.error("<STR_LIT>", exc_info=True)<EOL><DEDENT><DEDENT>


<EOL>if self.control_stream:<EOL><INDENT>self.control_stream.flush()<EOL><DEDENT>idents,msg = self.session.feed_identities(msg, copy=False)<EOL>try:<EOL><INDENT>msg = self.session.unserialize(msg, content=True, copy=False)<EOL><DEDENT>except:<EOL><INDENT>self.log.error("<STR_LIT>", exc_info=True)<EOL>return<EOL><DEDENT>header = msg['<STR_LIT>']<EOL>msg_id = header['<STR_LIT>']<EOL>msg_type = msg['<STR_LIT>']['<STR_LIT>']<EOL>self.log.debug('<STR_LIT>', msg_type)<EOL>self.log.debug('<STR_LIT>', msg['<STR_LIT:content>'])<EOL>if msg_id in self.aborted:<EOL><INDENT>self.aborted.remove(msg_id)<EOL>reply_type = msg_type.split('<STR_LIT:_>')[<NUM_LIT:0>] + '<STR_LIT>'<EOL>status = {'<STR_LIT:status>' : '<STR_LIT>'}<EOL>sub = {'<STR_LIT>' : self.ident}<EOL>sub.update(status)<EOL>reply_msg = self.session.send(stream, reply_type, subheader=sub,<EOL>content=status, parent=msg, ident=idents)<EOL>return<EOL><DEDENT>handler = self.shell_handlers.get(msg_type, None)<EOL>if handler is None:<EOL><INDENT>self.log.error("<STR_LIT>", msg_type)<EOL><DEDENT>else:<EOL><INDENT>sig = signal(SIGINT, default_int_handler)<EOL>try:<EOL><INDENT>handler(stream, idents, msg)<EOL><DEDENT>except Exception:<EOL><INDENT>self.log.error("<STR_LIT>", exc_info=True)<EOL><DEDENT>finally:<EOL><INDENT>signal(SIGINT, sig)<EOL><DEDENT><DEDENT>


self.log.info("<STR_LIT>")<EOL>signal(SIGINT, default_int_handler)<EOL>while self.eventloop is not None:<EOL><INDENT>try:<EOL><INDENT>self.eventloop(self)<EOL><DEDENT>except KeyboardInterrupt:<EOL><INDENT>self.log.error("<STR_LIT>")<EOL>continue<EOL><DEDENT>else:<EOL><INDENT>self.eventloop = None<EOL>break<EOL><DEDENT><DEDENT>self.log.info("<STR_LIT>")<EOL>ioloop.IOLoop.instance().stop()<EOL>


self.shell.exit_now = False<EOL>if self.control_stream:<EOL><INDENT>self.control_stream.on_recv(self.dispatch_control, copy=False)<EOL><DEDENT>def make_dispatcher(stream):<EOL><INDENT>def dispatcher(msg):<EOL><INDENT>return self.dispatch_shell(stream, msg)<EOL><DEDENT>return dispatcher<EOL><DEDENT>for s in self.shell_streams:<EOL><INDENT>s.on_recv(make_dispatcher(s), copy=False)<EOL><DEDENT>


if self.control_stream:<EOL><INDENT>self.control_stream.flush()<EOL><DEDENT>for stream in self.shell_streams:<EOL><INDENT>stream.flush(zmq.POLLIN, <NUM_LIT:1>)<EOL>stream.flush(zmq.POLLOUT)<EOL><DEDENT>


self._recorded_ports = ports<EOL>

Record the ports that this kernel is using.

        The creator of the Kernel instance must call this methods if they
        want the :meth:`connect_request` method to return the port numbers.


return {<EOL>'<STR_LIT>' : True,<EOL>'<STR_LIT>' : self.ident,<EOL>'<STR_LIT>': datetime.now(),<EOL>}<EOL>

init subheader dict, for execute/apply_reply


def _publish_pyin(self, code, parent, execution_count):

self.session.send(self.iopub_socket, '<STR_LIT>',<EOL>{'<STR_LIT:code>':code, '<STR_LIT>': execution_count},<EOL>parent=parent, ident=self._topic('<STR_LIT>')<EOL>)<EOL>

Publish the code request on the pyin stream.


def _publish_status(self, status, parent=None):

self.session.send(self.iopub_socket,<EOL>'<STR_LIT:status>',<EOL>{'<STR_LIT>': status},<EOL>parent=parent,<EOL>ident=self._topic('<STR_LIT:status>'),<EOL>)<EOL>


def execute_request(self, stream, ident, parent):

self._publish_status('<STR_LIT>', parent)<EOL>try:<EOL><INDENT>content = parent['<STR_LIT:content>']<EOL>code = content['<STR_LIT:code>']<EOL>silent = content['<STR_LIT>']<EOL><DEDENT>except:<EOL><INDENT>self.log.error("<STR_LIT>")<EOL>self.log.error("<STR_LIT:%s>", parent)<EOL>return<EOL><DEDENT>sub = self._make_subheader()<EOL>shell = self.shell <EOL>if content.get('<STR_LIT>', False):<EOL><INDENT>raw_input = lambda prompt='<STR_LIT>': self._raw_input(prompt, ident, parent)<EOL><DEDENT>else:<EOL><INDENT>raw_input = lambda prompt='<STR_LIT>' : self._no_raw_input()<EOL><DEDENT>if py3compat.PY3:<EOL><INDENT>builtins.input = raw_input<EOL><DEDENT>else:<EOL><INDENT>builtins.raw_input = raw_input<EOL><DEDENT>shell.displayhook.set_parent(parent)<EOL>shell.display_pub.set_parent(parent)<EOL>sys.stdout.set_parent(parent)<EOL>sys.stderr.set_parent(parent)<EOL>if not silent:<EOL><INDENT>self._publish_pyin(code, parent, shell.execution_count)<EOL><DEDENT>reply_content = {}<EOL>try:<EOL><INDENT>shell.run_cell(code, store_history=not silent, silent=silent)<EOL><DEDENT>except:<EOL><INDENT>status = '<STR_LIT:error>'<EOL>etype, evalue, tb = sys.exc_info()<EOL>tb_list = traceback.format_exception(etype, evalue, tb)<EOL>reply_content.update(shell._showtraceback(etype, evalue, tb_list))<EOL><DEDENT>else:<EOL><INDENT>status = '<STR_LIT>'<EOL><DEDENT>reply_content['<STR_LIT:status>'] = status<EOL>reply_content['<STR_LIT>'] = shell.execution_count - <NUM_LIT:1><EOL>if shell._reply_content is not None:<EOL><INDENT>reply_content.update(shell._reply_content)<EOL>e_info = dict(engine_uuid=self.ident, engine_id=self.int_id, method='<STR_LIT>')<EOL>reply_content['<STR_LIT>'] = e_info<EOL>shell._reply_content = None<EOL><DEDENT>if reply_content['<STR_LIT:status>'] == '<STR_LIT>':<EOL><INDENT>reply_content['<STR_LIT>'] =shell.user_variables(content.get('<STR_LIT>', []))<EOL>reply_content['<STR_LIT>'] =shell.user_expressions(content.get('<STR_LIT>', {}))<EOL><DEDENT>else:<EOL><INDENT>reply_content['<STR_LIT>'] = {}<EOL>reply_content['<STR_LIT>'] = {}<EOL><DEDENT>reply_content['<STR_LIT>'] = shell.payload_manager.read_payload()<EOL>shell.payload_manager.clear_payload()<EOL>sys.stdout.flush()<EOL>sys.stderr.flush()<EOL>if self._execute_sleep:<EOL><INDENT>time.sleep(self._execute_sleep)<EOL><DEDENT>reply_content = json_clean(reply_content)<EOL>sub['<STR_LIT:status>'] = reply_content['<STR_LIT:status>']<EOL>if reply_content['<STR_LIT:status>'] == '<STR_LIT:error>' andreply_content['<STR_LIT>'] == '<STR_LIT>':<EOL><INDENT>sub['<STR_LIT>'] = False<EOL><DEDENT>reply_msg = self.session.send(stream, '<STR_LIT>',<EOL>reply_content, parent, subheader=sub,<EOL>ident=ident)<EOL>self.log.debug("<STR_LIT:%s>", reply_msg)<EOL>if not silent and reply_msg['<STR_LIT:content>']['<STR_LIT:status>'] == '<STR_LIT:error>':<EOL><INDENT>self._abort_queues()<EOL><DEDENT>self._publish_status('<STR_LIT>', parent)<EOL>


def abort_request(self, stream, ident, parent):

msg_ids = parent['<STR_LIT:content>'].get('<STR_LIT>', None)<EOL>if isinstance(msg_ids, str):<EOL><INDENT>msg_ids = [msg_ids]<EOL><DEDENT>if not msg_ids:<EOL><INDENT>self.abort_queues()<EOL><DEDENT>for mid in msg_ids:<EOL><INDENT>self.aborted.add(str(mid))<EOL><DEDENT>content = dict(status='<STR_LIT>')<EOL>reply_msg = self.session.send(stream, '<STR_LIT>', content=content,<EOL>parent=parent, ident=ident)<EOL>self.log.debug("<STR_LIT:%s>", reply_msg)<EOL>


def clear_request(self, stream, idents, parent):

self.shell.reset(False)<EOL>msg = self.session.send(stream, '<STR_LIT>', ident=idents, parent=parent,<EOL>content = dict(status='<STR_LIT>'))<EOL>


if self.int_id >= <NUM_LIT:0>:<EOL><INDENT>base = "<STR_LIT>" % self.int_id<EOL><DEDENT>else:<EOL><INDENT>base = "<STR_LIT>" % self.ident<EOL><DEDENT>return py3compat.cast_bytes("<STR_LIT>" % (base, topic))<EOL>


raise StdinNotImplementedError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL>

Raise StdinNotImplentedError if active frontend doesn't support
        stdin.


<EOL>if self._shutdown_message is not None:<EOL><INDENT>self.session.send(self.iopub_socket, self._shutdown_message, ident=self._topic('<STR_LIT>'))<EOL>self.log.debug("<STR_LIT:%s>", self._shutdown_message)<EOL><DEDENT>[ s.flush(zmq.POLLOUT) for s in self.shell_streams ]<EOL>

Actions taken at shutdown by the kernel, called by python's atexit.


<EOL>shell = self.shell<EOL>_showtraceback = shell._showtraceback<EOL>try:<EOL><INDENT>def print_tb(etype, evalue, stb):<EOL><INDENT>print ("<STR_LIT>",<EOL>file=io.stderr)<EOL>print (shell.InteractiveTB.stb2text(stb), file=io.stderr)<EOL><DEDENT>shell._showtraceback = print_tb<EOL>InteractiveShellApp.init_gui_pylab(self)<EOL><DEDENT>finally:<EOL><INDENT>shell._showtraceback = _showtraceback<EOL><DEDENT>

Enable GUI event loop integration, taking pylab into account.


import zmq<EOL>from zmq.eventloop import ioloop<EOL>def install():<EOL><INDENT>import tornado.ioloop<EOL>tornado.ioloop.IOLoop = ioloop.IOLoop<EOL><DEDENT>if not hasattr(ioloop, '<STR_LIT>'):<EOL><INDENT>ioloop.install = install<EOL><DEDENT>if not hasattr(zmq, '<STR_LIT>'):<EOL><INDENT>zmq.DEALER = zmq.XREQ<EOL><DEDENT>if not hasattr(zmq, '<STR_LIT>'):<EOL><INDENT>zmq.ROUTER = zmq.XREP<EOL><DEDENT>from zmq.utils import jsonapi<EOL>if jsonapi.jsonmod.__name__ == '<STR_LIT>':<EOL><INDENT>import json<EOL>jsonapi.jsonmod = json<EOL><DEDENT>

backport a few patches from newer pyzmq

    These can be removed as we bump our minimum pyzmq version


<EOL>gobject.idle_add(self._wire_kernel)<EOL>gtk.main()<EOL>

Starts the GTK main event loop and sets our kernel startup routine.


self.gtk_main, self.gtk_main_quit = self._hijack_gtk()<EOL>gobject.timeout_add(int(<NUM_LIT:1000>*self.kernel._poll_interval),<EOL>self.iterate_kernel)<EOL>return False<EOL>

Initializes the kernel inside GTK.

        This is meant to run only once at startup, so it does its job and
        returns False to ensure it doesn't get run again by GTK.


self.kernel.do_one_iteration()<EOL>return True<EOL>

Run one iteration of the kernel and return True.

        GTK timer functions must return True to be called again, so we make the
        call to :meth:`do_one_iteration` and then return True for GTK.


def dummy(*args, **kw):<EOL><INDENT>pass<EOL><DEDENT>orig_main, gtk.main = gtk.main, dummy<EOL>orig_main_quit, gtk.main_quit = gtk.main_quit, dummy<EOL>return orig_main, orig_main_quit<EOL>

Hijack a few key functions in GTK for IPython integration.

        Modifies pyGTK's main and main_quit with a dummy so user code does not
        block IPython.  This allows us to use %run to run arbitrary pygtk
        scripts from a long-lived IPython session, and when they attempt to
        start or stop

        Returns
        -------
        The original functions that have been hijacked:
        - gtk.main
        - gtk.main_quit


try:<EOL><INDENT>fname = filefind(self.connection_file, ['<STR_LIT:.>', self.profile_dir.security_dir])<EOL><DEDENT>except IOError:<EOL><INDENT>self.log.debug("<STR_LIT>", self.connection_file)<EOL>atexit.register(self.cleanup_connection_file)<EOL>return<EOL><DEDENT>self.log.debug(u"<STR_LIT>", fname)<EOL>with open(fname) as f:<EOL><INDENT>s = f.read()<EOL><DEDENT>cfg = json.loads(s)<EOL>if self.ip == LOCALHOST and '<STR_LIT>' in cfg:<EOL><INDENT>self.ip = cfg['<STR_LIT>']<EOL><DEDENT>for channel in ('<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>name = channel + '<STR_LIT>'<EOL>if getattr(self, name) == <NUM_LIT:0> and name in cfg:<EOL><INDENT>setattr(self, name, cfg[name])<EOL><DEDENT><DEDENT>if '<STR_LIT:key>' in cfg:<EOL><INDENT>self.config.Session.key = str_to_bytes(cfg['<STR_LIT:key>'])<EOL><DEDENT>

load ip/port/hmac config from JSON connection file


if os.path.basename(self.connection_file) == self.connection_file:<EOL><INDENT>cf = os.path.join(self.profile_dir.security_dir, self.connection_file)<EOL><DEDENT>else:<EOL><INDENT>cf = self.connection_file<EOL><DEDENT>write_connection_file(cf, ip=self.ip, key=self.session.key,<EOL>shell_port=self.shell_port, stdin_port=self.stdin_port, hb_port=self.hb_port,<EOL>iopub_port=self.iopub_port)<EOL>self._full_connection_file = cf<EOL>


<EOL>hb_ctx = zmq.Context()<EOL>self.heartbeat = Heartbeat(hb_ctx, (self.ip, self.hb_port))<EOL>self.hb_port = self.heartbeat.port<EOL>self.log.debug("<STR_LIT>"%self.hb_port)<EOL>self.heartbeat.start()<EOL>self.log.critical("<STR_LIT>")<EOL>


basename = os.path.basename(self.connection_file)<EOL>if basename == self.connection_file oros.path.dirname(self.connection_file) == self.profile_dir.security_dir:<EOL><INDENT>tail = basename<EOL>if self.profile != '<STR_LIT:default>':<EOL><INDENT>tail += "<STR_LIT>" % self.profile<EOL><DEDENT><DEDENT>else:<EOL><INDENT>tail = self.connection_file<EOL><DEDENT>self.log.critical("<STR_LIT>", tail)<EOL>self.ports = dict(shell=self.shell_port, iopub=self.iopub_port,<EOL>stdin=self.stdin_port, hb=self.hb_port)<EOL>

display connection info, and store ports


default_secure(self.config)<EOL>self.session = Session(config=self.config, username=u'<STR_LIT>')<EOL>


if self.no_stdout or self.no_stderr:<EOL><INDENT>blackhole = open(os.devnull, '<STR_LIT:w>')<EOL>if self.no_stdout:<EOL><INDENT>sys.stdout = sys.__stdout__ = blackhole<EOL><DEDENT>if self.no_stderr:<EOL><INDENT>sys.stderr = sys.__stderr__ = blackhole<EOL><DEDENT><DEDENT>

redirects stdout/stderr to devnull if necessary


if self.outstream_class:<EOL><INDENT>outstream_factory = import_item(str(self.outstream_class))<EOL>sys.stdout = outstream_factory(self.session, self.iopub_socket, u'<STR_LIT>')<EOL>sys.stderr = outstream_factory(self.session, self.iopub_socket, u'<STR_LIT>')<EOL><DEDENT>if self.displayhook_class:<EOL><INDENT>displayhook_factory = import_item(str(self.displayhook_class))<EOL>sys.displayhook = displayhook_factory(self.session, self.iopub_socket)<EOL><DEDENT>

Redirect input streams and set a display hook.


def __init__(self, interrupt_handle=None, parent_handle=None):

assert(interrupt_handle or parent_handle)<EOL>super(ParentPollerWindows, self).__init__()<EOL>if ctypes is None:<EOL><INDENT>raise ImportError("<STR_LIT>")<EOL><DEDENT>self.daemon = True<EOL>self.interrupt_handle = interrupt_handle<EOL>self.parent_handle = parent_handle<EOL>

Create the poller. At least one of the optional parameters must be
        provided.

        Parameters:
        -----------
        interrupt_handle : HANDLE (int), optional
            If provided, the program will generate a Ctrl+C event when this
            handle is signaled.

        parent_handle : HANDLE (int), optional
            If provided, the program will terminate immediately when this
            handle is signaled.


<EOL>class SECURITY_ATTRIBUTES(ctypes.Structure):<EOL><INDENT>_fields_ = [ ("<STR_LIT>", ctypes.c_int),<EOL>("<STR_LIT>", ctypes.c_void_p),<EOL>("<STR_LIT>", ctypes.c_int) ]<EOL><DEDENT>sa = SECURITY_ATTRIBUTES()<EOL>sa_p = ctypes.pointer(sa)<EOL>sa.nLength = ctypes.sizeof(SECURITY_ATTRIBUTES)<EOL>sa.lpSecurityDescriptor = <NUM_LIT:0><EOL>sa.bInheritHandle = <NUM_LIT:1><EOL>return ctypes.windll.kernel32.CreateEventA(<EOL>sa_p,  <EOL>False, <EOL>False, <EOL>'<STR_LIT>')<EOL>

Create an interrupt event handle.

        The parent process should use this static method for creating the
        interrupt event that is passed to the child process. It should store
        this handle and use it with ``send_interrupt`` to interrupt the child
        process.


ctypes.windll.kernel32.SetEvent(interrupt_handle)<EOL>

Sends an interrupt event using the specified handle.


try:<EOL><INDENT>from _winapi import WAIT_OBJECT_0, INFINITE<EOL><DEDENT>except ImportError:<EOL><INDENT>from _subprocess import WAIT_OBJECT_0, INFINITE<EOL><DEDENT>handles = []<EOL>if self.interrupt_handle:<EOL><INDENT>handles.append(self.interrupt_handle)<EOL><DEDENT>if self.parent_handle:<EOL><INDENT>handles.append(self.parent_handle)<EOL><DEDENT>arch = platform.architecture()[<NUM_LIT:0>]<EOL>c_int = ctypes.c_int64 if arch.startswith('<STR_LIT>') else ctypes.c_int<EOL>while True:<EOL><INDENT>result = ctypes.windll.kernel32.WaitForMultipleObjects(<EOL>len(handles),                            <EOL>(c_int * len(handles))(*handles),        <EOL>False,                                   <EOL>INFINITE)                                <EOL>if WAIT_OBJECT_0 <= result < len(handles):<EOL><INDENT>handle = handles[result - WAIT_OBJECT_0]<EOL>if handle == self.interrupt_handle:<EOL><INDENT>interrupt_main()<EOL><DEDENT>elif handle == self.parent_handle:<EOL><INDENT>os._exit(<NUM_LIT:1>)<EOL><DEDENT><DEDENT>elif result < <NUM_LIT:0>:<EOL><INDENT>warn("""<STR_LIT>""")<EOL>return<EOL><DEDENT><DEDENT>

Run the poll loop. This method never returns.


try:<EOL><INDENT>return unicode(e)<EOL><DEDENT>except UnicodeError:<EOL><INDENT>pass<EOL><DEDENT>try:<EOL><INDENT>return py3compat.str_to_unicode(str(e))<EOL><DEDENT>except UnicodeError:<EOL><INDENT>pass<EOL><DEDENT>try:<EOL><INDENT>return py3compat.str_to_unicode(repr(e))<EOL><DEDENT>except UnicodeError:<EOL><INDENT>pass<EOL><DEDENT>return u'<STR_LIT>'<EOL>

unicode(e) with various fallbacks. Used for exceptions, which may not be
    safe to call unicode() on.


self.parent_header = extract_header(parent)<EOL>


sys.stdout.flush()<EOL>sys.stderr.flush()<EOL>


if os.name == '<STR_LIT>':<EOL><INDENT>self.shell.system("<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>self.shell.system("<STR_LIT>")<EOL><DEDENT>


cont = open(arg_s).read()<EOL>if arg_s.endswith('<STR_LIT>'):<EOL><INDENT>cont = self.shell.pycolorize(cont)<EOL><DEDENT>page.page(cont)<EOL>

Show a file through the pager.

        Files ending in .py are syntax-highlighted.


from IPython.core.application import BaseIPythonApplication as BaseIPApp<EOL>if BaseIPApp.initialized():<EOL><INDENT>app = BaseIPApp.instance()<EOL>security_dir = app.profile_dir.security_dir<EOL>profile = app.profile<EOL><DEDENT>else:<EOL><INDENT>profile = '<STR_LIT:default>'<EOL>security_dir = '<STR_LIT>'<EOL><DEDENT>try:<EOL><INDENT>connection_file = get_connection_file()<EOL>info = get_connection_info(unpack=False)<EOL><DEDENT>except Exception as e:<EOL><INDENT>error("<STR_LIT>" % e)<EOL>return<EOL><DEDENT>profile_flag = "<STR_LIT>" % profile if profile != '<STR_LIT:default>' else "<STR_LIT>"<EOL>if security_dir == os.path.dirname(connection_file):<EOL><INDENT>connection_file = os.path.basename(connection_file)<EOL><DEDENT>print (info + '<STR_LIT:\n>')<EOL>print ("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>".format(<EOL>connection_file, profile_flag<EOL>)<EOL>)<EOL>

Print information for connecting other clients to this kernel

        It will print the contents of this session's connection file, as well as
        shortcuts for local clients.

        In the simplest case, when called from the most recently launched kernel,
        secondary clients can be connected, simply with:

        $> ipython <app> --existing


<EOL>try:<EOL><INDENT>from IPython.parallel import bind_kernel<EOL><DEDENT>except ImportError:<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>bind_kernel()<EOL><DEDENT>try:<EOL><INDENT>p = connect_qtconsole(argv=arg_split(arg_s, os.name=='<STR_LIT>'))<EOL><DEDENT>except Exception as e:<EOL><INDENT>error("<STR_LIT>" % e)<EOL>return<EOL><DEDENT>

Open a qtconsole connected to this kernel.

        Useful for connecting a qtconsole to running notebooks, for better
        debugging.


def _exit_now_changed(self, name, old, new):

if new:<EOL><INDENT>loop = ioloop.IOLoop.instance()<EOL>loop.add_timeout(time.time()+<NUM_LIT:0.1>, loop.stop)<EOL><DEDENT>


env = os.environ<EOL>env['<STR_LIT>'] = '<STR_LIT>'<EOL>env['<STR_LIT>'] = '<STR_LIT:1>'<EOL>env['<STR_LIT>'] = '<STR_LIT>'<EOL>env['<STR_LIT>'] = '<STR_LIT>'<EOL>install_payload_page()<EOL>


new = self.prompt_manager.render('<STR_LIT>') + cmd<EOL>payload = dict(<EOL>source='<STR_LIT>',<EOL>transformed_input=new,<EOL>)<EOL>self.payload_manager.write_payload(payload)<EOL>

Called to show the auto-rewritten input for autocall and friends.

        FIXME: this payload is currently not correctly processed by the
        frontend.


self.exit_now = True<EOL>payload = dict(<EOL>source='<STR_LIT>',<EOL>exit=True,<EOL>keepkernel=self.keepkernel_on_exit,<EOL>)<EOL>self.payload_manager.write_payload(payload)<EOL>


payload = dict(<EOL>source='<STR_LIT>',<EOL>text=text<EOL>)<EOL>self.payload_manager.write_payload(payload)<EOL>

Send the specified text to the frontend to be presented at the next
        input cell.


if isinstance(obj,dict):<EOL><INDENT>for key in obj.keys():<EOL><INDENT>obj[key] = squash_unicode(obj[key])<EOL>if isinstance(key, unicode):<EOL><INDENT>obj[squash_unicode(key)] = obj.pop(key)<EOL><DEDENT><DEDENT><DEDENT>elif isinstance(obj, list):<EOL><INDENT>for i,v in enumerate(obj):<EOL><INDENT>obj[i] = squash_unicode(v)<EOL><DEDENT><DEDENT>elif isinstance(obj, unicode):<EOL><INDENT>obj = obj.encode('<STR_LIT:utf8>')<EOL><DEDENT>return obj<EOL>


if '<STR_LIT>' in cfg:<EOL><INDENT>if '<STR_LIT:key>' in cfg.Session or '<STR_LIT>' in cfg.Session:<EOL><INDENT>return<EOL><DEDENT><DEDENT>cfg.Session.key = str_to_bytes(str(uuid.uuid4()))<EOL>

Set the default behavior for a config environment to be secure.

    If Session.key/keyfile have not been set, set Session.key to
    a new random UUID.


if not msg_or_header:<EOL><INDENT>return {}<EOL><DEDENT>try:<EOL><INDENT>h = msg_or_header['<STR_LIT>']<EOL><DEDENT>except KeyError:<EOL><INDENT>try:<EOL><INDENT>h = msg_or_header['<STR_LIT>']<EOL><DEDENT>except KeyError:<EOL><INDENT>raise<EOL><DEDENT>else:<EOL><INDENT>h = msg_or_header<EOL><DEDENT><DEDENT>if not isinstance(h, dict):<EOL><INDENT>h = dict(h)<EOL><DEDENT>return h<EOL>

Given a message or header, return the header.


super(Session, self).__init__(**kwargs)<EOL>self._check_packers()<EOL>self.none = self.pack({})<EOL>self.session<EOL>

create a Session object

        Parameters
        ----------

        debug : bool
            whether to trigger extra debugging statements
        packer/unpacker : str : 'json', 'pickle' or import_string
            importstrings for methods to serialize message parts.  If just
            'json' or 'pickle', predefined JSON and pickle packers will be used.
            Otherwise, the entire importstring must be used.

            The functions must accept at least valid JSON input, and output
            *bytes*.

            For example, to use msgpack:
            packer = 'msgpack.packb', unpacker='msgpack.unpackb'
        pack/unpack : callables
            You can also set the pack/unpack callables for serialization
            directly.
        session : unicode (must be ascii)
            the ID of this Session object.  The default is to generate a new
            UUID.
        bsession : bytes
            The session as bytes
        username : unicode
            username added to message headers.  The default is to ask the OS.
        key : bytes
            The key used to initialize an HMAC signature.  If unset, messages
            will not be signed or checked.
        keyfile : filepath
            The file containing a key.  If this is set, `key` will be
            initialized to the contents of the file.


return str(uuid.uuid4())<EOL>


pack = self.pack<EOL>unpack = self.unpack<EOL>msg = dict(a=[<NUM_LIT:1>,'<STR_LIT>'])<EOL>try:<EOL><INDENT>packed = pack(msg)<EOL><DEDENT>except Exception:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if not isinstance(packed, bytes):<EOL><INDENT>raise ValueError("<STR_LIT>"%type(packed))<EOL><DEDENT>try:<EOL><INDENT>unpacked = unpack(packed)<EOL><DEDENT>except Exception:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>msg = dict(t=datetime.now())<EOL>try:<EOL><INDENT>unpacked = unpack(pack(msg))<EOL><DEDENT>except Exception:<EOL><INDENT>self.pack = lambda o: pack(squash_dates(o))<EOL>self.unpack = lambda s: extract_dates(unpack(s))<EOL><DEDENT>

check packers for binary data and datetime support.


def msg(self, msg_type, content=None, parent=None, subheader=None, header=None):

msg = {}<EOL>header = self.msg_header(msg_type) if header is None else header<EOL>msg['<STR_LIT>'] = header<EOL>msg['<STR_LIT>'] = header['<STR_LIT>']<EOL>msg['<STR_LIT>'] = header['<STR_LIT>']<EOL>msg['<STR_LIT>'] = {} if parent is None else extract_header(parent)<EOL>msg['<STR_LIT:content>'] = {} if content is None else content<EOL>sub = {} if subheader is None else subheader<EOL>msg['<STR_LIT>'].update(sub)<EOL>return msg<EOL>

Return the nested message dict.

        This format is different from what is sent over the wire. The
        serialize/unserialize methods converts this nested message dict to the wire
        format, which is a list of message parts.


if self.auth is None:<EOL><INDENT>return b'<STR_LIT>'<EOL><DEDENT>h = self.auth.copy()<EOL>for m in msg_list:<EOL><INDENT>h.update(m)<EOL><DEDENT>return str_to_bytes(h.hexdigest())<EOL>

Sign a message with HMAC digest. If no auth, return b''.

        Parameters
        ----------
        msg_list : list
            The [p_header,p_parent,p_content] part of the message list.


content = msg.get('<STR_LIT:content>', {})<EOL>if content is None:<EOL><INDENT>content = self.none<EOL><DEDENT>elif isinstance(content, dict):<EOL><INDENT>content = self.pack(content)<EOL><DEDENT>elif isinstance(content, bytes):<EOL><INDENT>pass<EOL><DEDENT>elif isinstance(content, unicode):<EOL><INDENT>content = content.encode('<STR_LIT:utf8>')<EOL><DEDENT>else:<EOL><INDENT>raise TypeError("<STR_LIT>"%type(content))<EOL><DEDENT>real_message = [self.pack(msg['<STR_LIT>']),<EOL>self.pack(msg['<STR_LIT>']),<EOL>content<EOL>]<EOL>to_send = []<EOL>if isinstance(ident, list):<EOL><INDENT>to_send.extend(ident)<EOL><DEDENT>elif ident is not None:<EOL><INDENT>to_send.append(ident)<EOL><DEDENT>to_send.append(DELIM)<EOL>signature = self.sign(real_message)<EOL>to_send.append(signature)<EOL>to_send.extend(real_message)<EOL>return to_send<EOL>

Serialize the message components to bytes.

        This is roughly the inverse of unserialize. The serialize/unserialize
        methods work with full message lists, whereas pack/unpack work with
        the individual message parts in the message list.

        Parameters
        ----------
        msg : dict or Message
            The nexted message dict as returned by the self.msg method.

        Returns
        -------
        msg_list : list
            The list of bytes objects to be sent with the format:
            [ident1,ident2,...,DELIM,HMAC,p_header,p_parent,p_content,
             buffer1,buffer2,...]. In this list, the p_* entities are
            the packed or serialized versions, so if JSON is used, these
            are utf8 encoded JSON strings.


def send(self, stream, msg_or_type, content=None, parent=None, ident=None,<EOL>buffers=None, subheader=None, track=False, header=None):

if not isinstance(stream, (zmq.Socket, ZMQStream)):<EOL><INDENT>raise TypeError("<STR_LIT>"%type(stream))<EOL><DEDENT>elif track and isinstance(stream, ZMQStream):<EOL><INDENT>raise TypeError("<STR_LIT>")<EOL><DEDENT>if isinstance(msg_or_type, (Message, dict)):<EOL><INDENT>msg = msg_or_type<EOL><DEDENT>else:<EOL><INDENT>msg = self.msg(msg_or_type, content=content, parent=parent,<EOL>subheader=subheader, header=header)<EOL><DEDENT>buffers = [] if buffers is None else buffers<EOL>to_send = self.serialize(msg, ident)<EOL>flag = <NUM_LIT:0><EOL>if buffers:<EOL><INDENT>flag = zmq.SNDMORE<EOL>_track = False<EOL><DEDENT>else:<EOL><INDENT>_track=track<EOL><DEDENT>if track:<EOL><INDENT>tracker = stream.send_multipart(to_send, flag, copy=False, track=_track)<EOL><DEDENT>else:<EOL><INDENT>tracker = stream.send_multipart(to_send, flag, copy=False)<EOL><DEDENT>for b in buffers[:-<NUM_LIT:1>]:<EOL><INDENT>stream.send(b, flag, copy=False)<EOL><DEDENT>if buffers:<EOL><INDENT>if track:<EOL><INDENT>tracker = stream.send(buffers[-<NUM_LIT:1>], copy=False, track=track)<EOL><DEDENT>else:<EOL><INDENT>tracker = stream.send(buffers[-<NUM_LIT:1>], copy=False)<EOL><DEDENT><DEDENT>if self.debug:<EOL><INDENT>pprint.pprint(msg)<EOL>pprint.pprint(to_send)<EOL>pprint.pprint(buffers)<EOL><DEDENT>msg['<STR_LIT>'] = tracker<EOL>return msg<EOL>

Build and send a message via stream or socket.

        The message format used by this function internally is as follows:

        [ident1,ident2,...,DELIM,HMAC,p_header,p_parent,p_content,
         buffer1,buffer2,...]

        The serialize/unserialize methods convert the nested message dict into this
        format.

        Parameters
        ----------

        stream : zmq.Socket or ZMQStream
            The socket-like object used to send the data.
        msg_or_type : str or Message/dict
            Normally, msg_or_type will be a msg_type unless a message is being
            sent more than once. If a header is supplied, this can be set to
            None and the msg_type will be pulled from the header.

        content : dict or None
            The content of the message (ignored if msg_or_type is a message).
        header : dict or None
            The header dict for the message (ignores if msg_to_type is a message).
        parent : Message or dict or None
            The parent or parent header describing the parent of this message
            (ignored if msg_or_type is a message).
        ident : bytes or list of bytes
            The zmq.IDENTITY routing path.
        subheader : dict or None
            Extra header keys for this message's header (ignored if msg_or_type
            is a message).
        buffers : list or None
            The already-serialized buffers to be appended to the message.
        track : bool
            Whether to track.  Only for use with Sockets, because ZMQStream
            objects cannot track messages.

        Returns
        -------
        msg : dict
            The constructed message.
        (msg,tracker) : (dict, MessageTracker)
            if track=True, then a 2-tuple will be returned,
            the first element being the constructed
            message, and the second being the MessageTracker


def send_raw(self, stream, msg_list, flags=<NUM_LIT:0>, copy=True, ident=None):

to_send = []<EOL>if isinstance(ident, bytes):<EOL><INDENT>ident = [ident]<EOL><DEDENT>if ident is not None:<EOL><INDENT>to_send.extend(ident)<EOL><DEDENT>to_send.append(DELIM)<EOL>to_send.append(self.sign(msg_list))<EOL>to_send.extend(msg_list)<EOL>stream.send_multipart(msg_list, flags, copy=copy)<EOL>

Send a raw message via ident path.

        This method is used to send a already serialized message.

        Parameters
        ----------
        stream : ZMQStream or Socket
            The ZMQ stream or socket to use for sending the message.
        msg_list : list
            The serialized list of messages to send. This only includes the
            [p_header,p_parent,p_content,buffer1,buffer2,...] portion of
            the message.
        ident : ident or list
            A single ident or a list of idents to use in sending.


def recv(self, socket, mode=zmq.NOBLOCK, content=True, copy=True):

if isinstance(socket, ZMQStream):<EOL><INDENT>socket = socket.socket<EOL><DEDENT>try:<EOL><INDENT>msg_list = socket.recv_multipart(mode, copy=copy)<EOL><DEDENT>except zmq.ZMQError as e:<EOL><INDENT>if e.errno == zmq.EAGAIN:<EOL><INDENT>return None,None<EOL><DEDENT>else:<EOL><INDENT>raise<EOL><DEDENT><DEDENT>idents, msg_list = self.feed_identities(msg_list, copy)<EOL>try:<EOL><INDENT>return idents, self.unserialize(msg_list, content=content, copy=copy)<EOL><DEDENT>except Exception as e:<EOL><INDENT>raise e<EOL><DEDENT>

Receive and unpack a message.

        Parameters
        ----------
        socket : ZMQStream or Socket
            The socket or stream to use in receiving.

        Returns
        -------
        [idents], msg
            [idents] is a list of idents and msg is a nested message dict of
            same format as self.msg returns.


def feed_identities(self, msg_list, copy=True):

if copy:<EOL><INDENT>idx = msg_list.index(DELIM)<EOL>return msg_list[:idx], msg_list[idx+<NUM_LIT:1>:]<EOL><DEDENT>else:<EOL><INDENT>failed = True<EOL>for idx,m in enumerate(msg_list):<EOL><INDENT>if m.bytes == DELIM:<EOL><INDENT>failed = False<EOL>break<EOL><DEDENT><DEDENT>if failed:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>idents, msg_list = msg_list[:idx], msg_list[idx+<NUM_LIT:1>:]<EOL>return [m.bytes for m in idents], msg_list<EOL><DEDENT>

Split the identities from the rest of the message.

        Feed until DELIM is reached, then return the prefix as idents and
        remainder as msg_list. This is easily broken by setting an IDENT to DELIM,
        but that would be silly.

        Parameters
        ----------
        msg_list : a list of Message or bytes objects
            The message to be split.
        copy : bool
            flag determining whether the arguments are bytes or Messages

        Returns
        -------
        (idents, msg_list) : two lists
            idents will always be a list of bytes, each of which is a ZMQ
            identity. msg_list will be a list of bytes or zmq.Messages of the
            form [HMAC,p_header,p_parent,p_content,buffer1,buffer2,...] and
            should be unpackable/unserializable via self.unserialize at this
            point.


def unserialize(self, msg_list, content=True, copy=True):

minlen = <NUM_LIT:4><EOL>message = {}<EOL>if not copy:<EOL><INDENT>for i in range(minlen):<EOL><INDENT>msg_list[i] = msg_list[i].bytes<EOL><DEDENT><DEDENT>if self.auth is not None:<EOL><INDENT>signature = msg_list[<NUM_LIT:0>]<EOL>if not signature:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if signature in self.digest_history:<EOL><INDENT>raise ValueError("<STR_LIT>"%signature)<EOL><DEDENT>self.digest_history.add(signature)<EOL>check = self.sign(msg_list[<NUM_LIT:1>:<NUM_LIT:4>])<EOL>if not signature == check:<EOL><INDENT>raise ValueError("<STR_LIT>"%signature)<EOL><DEDENT><DEDENT>if not len(msg_list) >= minlen:<EOL><INDENT>raise TypeError("<STR_LIT>"%minlen)<EOL><DEDENT>header = self.unpack(msg_list[<NUM_LIT:1>])<EOL>message['<STR_LIT>'] = header<EOL>message['<STR_LIT>'] = header['<STR_LIT>']<EOL>message['<STR_LIT>'] = header['<STR_LIT>']<EOL>message['<STR_LIT>'] = self.unpack(msg_list[<NUM_LIT:2>])<EOL>if content:<EOL><INDENT>message['<STR_LIT:content>'] = self.unpack(msg_list[<NUM_LIT:3>])<EOL><DEDENT>else:<EOL><INDENT>message['<STR_LIT:content>'] = msg_list[<NUM_LIT:3>]<EOL><DEDENT>message['<STR_LIT>'] = msg_list[<NUM_LIT:4>:]<EOL>return message<EOL>

Unserialize a msg_list to a nested message dict.

        This is roughly the inverse of serialize. The serialize/unserialize
        methods work with full message lists, whereas pack/unpack work with
        the individual message parts in the message list.

        Parameters:
        -----------
        msg_list : list of bytes or Message objects
            The list of message parts of the form [HMAC,p_header,p_parent,
            p_content,buffer1,buffer2,...].
        content : bool (True)
            Whether to unpack the content dict (True), or leave it packed
            (False).
        copy : bool (True)
            Whether to return the bytes (True), or the non-copying Message
            object in each place (False).

        Returns
        -------
        msg : dict
            The nested message dict with top-level keys [header, parent_header,
            content, buffers].


self.parent_header = extract_header(parent)<EOL>


self.msg['<STR_LIT:content>']['<STR_LIT>'] = self.prompt_count<EOL>


sys.stdout.flush()<EOL>sys.stderr.flush()<EOL>self.session.send(self.pub_socket, self.msg, ident=self.topic)<EOL>self.msg = None<EOL>


return [TestResults(f, t) for f, t in runner._name2ft.values() if f > <NUM_LIT:0> ]<EOL>

Count number of failures in a doctest runner.

    Code modeled after the summarize() method in doctest.


if func.__doc__ is not None:<EOL><INDENT>func.__doc__ = ip2py(func.__doc__)<EOL><DEDENT>return func<EOL>

Change the function docstring via ip2py.


from . import globalipapp<EOL>pyps1 = '<STR_LIT>'<EOL>pyps2 = '<STR_LIT>'<EOL>pyout = '<STR_LIT>'<EOL>dnew = ds<EOL>dnew = self.rps1.sub(pyps1, dnew)<EOL>dnew = self.rps2.sub(pyps2, dnew)<EOL>dnew = self.rout.sub(pyout, dnew)<EOL>ip = globalipapp.get_ipython()<EOL>out = []<EOL>newline = out.append<EOL>for line in dnew.splitlines():<EOL><INDENT>mps1 = self.rpyps1.match(line)<EOL>if mps1 is not None:<EOL><INDENT>prompt, text = mps1.groups()<EOL>newline(prompt+ip.prefilter(text, False))<EOL>continue<EOL><DEDENT>mps2 = self.rpyps2.match(line)<EOL>if mps2 is not None:<EOL><INDENT>prompt, text = mps2.groups()<EOL>newline(prompt+ip.prefilter(text, True))<EOL>continue<EOL><DEDENT>newline(line)<EOL><DEDENT>newline('<STR_LIT>')  <EOL>return '<STR_LIT:\n>'.join(out)<EOL>

Convert IPython prompts to python ones in a string.


self.verbose = verbose<EOL>self.finder = DocTestFinder(verbose=verbose, recurse=False)<EOL>

New decorator.

        Parameters
        ----------

        verbose : boolean, optional (False)
          Passed to the doctest finder and runner to control verbosity.


<EOL>d2u = self<EOL>if func.__doc__ is not None:<EOL><INDENT>func.__doc__ = ip2py(func.__doc__)<EOL><DEDENT>class Tester(unittest.TestCase):<EOL><INDENT>def test(self):<EOL><INDENT>runner = DocTestRunner(verbose=d2u.verbose)<EOL>map(runner.run, d2u.finder.find(func, func.__name__))<EOL>failed = count_failures(runner)<EOL>if failed:<EOL><INDENT>if len(failed) > <NUM_LIT:1>:<EOL><INDENT>err = "<STR_LIT>" % failed<EOL>raise ValueError(err)<EOL><DEDENT>self.fail('<STR_LIT>' % str(failed[<NUM_LIT:0>]))<EOL><DEDENT><DEDENT><DEDENT>Tester.__name__ = func.__name__<EOL>return Tester<EOL>

Use as a decorator: doctest a function's docstring as a unittest.

        This version runs normal doctests, but the idea is to make it later run
        ipython syntax instead.


<EOL>func._generator = True<EOL>class Tester(ParametricTestCase):<EOL><INDENT>test = staticmethod(func)<EOL><DEDENT>Tester.__name__ = func.__name__<EOL>return Tester<EOL>

Decorator to make a simple function into a normal test via
 unittest.


<EOL>vs = mod.__version__.replace('<STR_LIT>', '<STR_LIT>')<EOL>tup = tuple([int(v) for v in vs.split('<STR_LIT:.>') ])<EOL>return tup<EOL>


out = [ sys_info(), '<STR_LIT:\n>']<EOL>avail = []<EOL>not_avail = []<EOL>for k, is_avail in list(have.items()):<EOL><INDENT>if is_avail:<EOL><INDENT>avail.append(k)<EOL><DEDENT>else:<EOL><INDENT>not_avail.append(k)<EOL><DEDENT><DEDENT>if avail:<EOL><INDENT>out.append('<STR_LIT>')<EOL>avail.sort()<EOL>out.append('<STR_LIT:U+0020>' + '<STR_LIT:U+0020>'.join(avail)+'<STR_LIT:\n>')<EOL><DEDENT>if not_avail:<EOL><INDENT>out.append('<STR_LIT>')<EOL>not_avail.sort()<EOL>out.append('<STR_LIT:U+0020>' + '<STR_LIT:U+0020>'.join(not_avail)+'<STR_LIT:\n>')<EOL><DEDENT>return '<STR_LIT>'.join(out)<EOL>

Return a string with a summary report of test-related variables.


<EOL>ipjoin = lambda *paths: pjoin('<STR_LIT>', *paths)<EOL>exclusions = [ipjoin('<STR_LIT>'),<EOL>ipjoin('<STR_LIT>'),<EOL>ipjoin('<STR_LIT>'),<EOL>ipjoin('<STR_LIT>', '<STR_LIT>'),<EOL>ipjoin('<STR_LIT>', '<STR_LIT>'),<EOL>ipjoin('<STR_LIT>', '<STR_LIT>'),<EOL>]<EOL>if not have['<STR_LIT>']:<EOL><INDENT>exclusions.append(ipjoin('<STR_LIT>', '<STR_LIT>', '<STR_LIT>'))<EOL>exclusions.append(ipjoin('<STR_LIT>', '<STR_LIT>'))<EOL><DEDENT>if not have['<STR_LIT>']:<EOL><INDENT>exclusions.append(ipjoin('<STR_LIT>', '<STR_LIT>'))<EOL><DEDENT>exclusions.append(ipjoin('<STR_LIT>', '<STR_LIT>'))<EOL>exclusions.append(ipjoin('<STR_LIT>', '<STR_LIT>', '<STR_LIT>'))<EOL>exclusions.append(ipjoin('<STR_LIT>', '<STR_LIT>'))<EOL>exclusions.append(ipjoin('<STR_LIT>', '<STR_LIT>', '<STR_LIT>'))<EOL>if sys.platform == '<STR_LIT:win32>':<EOL><INDENT>exclusions.append(ipjoin('<STR_LIT>', '<STR_LIT>', '<STR_LIT>'))<EOL>exclusions.append(ipjoin('<STR_LIT>', '<STR_LIT>', '<STR_LIT>'))<EOL><DEDENT>if not have['<STR_LIT>']:<EOL><INDENT>exclusions.extend([ipjoin('<STR_LIT>', '<STR_LIT>'),<EOL>ipjoin('<STR_LIT>', '<STR_LIT>', '<STR_LIT>'),<EOL>ipjoin('<STR_LIT>', '<STR_LIT>', '<STR_LIT>'),<EOL>])<EOL><DEDENT>if not have['<STR_LIT>']:<EOL><INDENT>exclusions.append(ipjoin('<STR_LIT>'))<EOL>exclusions.append(ipjoin('<STR_LIT>', '<STR_LIT>'))<EOL>exclusions.append(ipjoin('<STR_LIT>', '<STR_LIT:html>'))<EOL>exclusions.append(ipjoin('<STR_LIT>', '<STR_LIT>'))<EOL>exclusions.append(ipjoin('<STR_LIT>', '<STR_LIT>', '<STR_LIT>'))<EOL>exclusions.append(ipjoin('<STR_LIT>'))<EOL><DEDENT>elif not have['<STR_LIT>'] or not have['<STR_LIT>']:<EOL><INDENT>exclusions.append(ipjoin('<STR_LIT>', '<STR_LIT>'))<EOL><DEDENT>if not have['<STR_LIT>']:<EOL><INDENT>exclusions.append(ipjoin('<STR_LIT>', '<STR_LIT>', '<STR_LIT>'))<EOL>exclusions.append(ipjoin('<STR_LIT>', '<STR_LIT>', '<STR_LIT>'))<EOL><DEDENT>if not have['<STR_LIT>']:<EOL><INDENT>exclusions.extend([ipjoin('<STR_LIT>', '<STR_LIT>'),<EOL>ipjoin('<STR_LIT>', '<STR_LIT>', '<STR_LIT>'),<EOL>ipjoin('<STR_LIT>', '<STR_LIT>'),<EOL>])<EOL><DEDENT>if not have['<STR_LIT>']:<EOL><INDENT>exclusions.extend([ipjoin('<STR_LIT>', '<STR_LIT>')])<EOL>exclusions.extend([ipjoin('<STR_LIT>', '<STR_LIT>', '<STR_LIT>')])<EOL><DEDENT>if not have['<STR_LIT>']:<EOL><INDENT>exclusions.extend([ipjoin('<STR_LIT>', '<STR_LIT>')])<EOL>exclusions.extend([ipjoin('<STR_LIT>', '<STR_LIT>', '<STR_LIT>')])<EOL><DEDENT>if not have['<STR_LIT>']:<EOL><INDENT>exclusions.append(ipjoin('<STR_LIT>', '<STR_LIT:html>'))<EOL><DEDENT>if not have['<STR_LIT>'] or not have['<STR_LIT>']:<EOL><INDENT>exclusions.append(ipjoin('<STR_LIT>', '<STR_LIT>'))<EOL>exclusions.append(ipjoin('<STR_LIT>', '<STR_LIT>', '<STR_LIT>'))<EOL><DEDENT>if sys.platform == '<STR_LIT:win32>':<EOL><INDENT>exclusions = [s.replace('<STR_LIT:\\>','<STR_LIT>') for s in exclusions]<EOL><DEDENT>parent, _ = os.path.split(get_ipython_package_dir())<EOL>for exclusion in exclusions:<EOL><INDENT>if exclusion.endswith(('<STR_LIT>', '<STR_LIT>')):<EOL><INDENT>continue<EOL><DEDENT>fullpath = pjoin(parent, exclusion)<EOL>if not os.path.exists(fullpath) and not glob.glob(fullpath + '<STR_LIT>'):<EOL><INDENT>warn("<STR_LIT>" % exclusion)<EOL><DEDENT><DEDENT>return exclusions<EOL>

Make patterns of modules and packages to exclude from testing.

    For the IPythonDoctest plugin, we need to exclude certain patterns that
    cause testing problems.  We should strive to minimize the number of
    skipped modules, since this means untested code.

    These modules and packages will NOT get scanned by nose at all for tests.


<EOL>nose_pkg_names = ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT>' ]<EOL>if have['<STR_LIT>']:<EOL><INDENT>nose_pkg_names.append('<STR_LIT>')<EOL>nose_pkg_names.append('<STR_LIT>')<EOL><DEDENT>nose_packages = ['<STR_LIT>' % m for m in nose_pkg_names ]<EOL>runners = [ (v, IPTester('<STR_LIT>', params=v)) for v in nose_packages ]<EOL>return runners<EOL>

Define the top-level packages that need to be tested.


try:<EOL><INDENT>retcode = self._run_cmd()<EOL><DEDENT>except:<EOL><INDENT>import traceback<EOL>traceback.print_exc()<EOL>return <NUM_LIT:1>  <EOL><DEDENT>if self.coverage_xml:<EOL><INDENT>subprocess.call(["<STR_LIT>", "<STR_LIT>", "<STR_LIT>", self.coverage_xml])<EOL><DEDENT>return retcode<EOL>


if not hasattr(os, '<STR_LIT>'):<EOL><INDENT>return<EOL><DEDENT>for pid in self.pids:<EOL><INDENT>try:<EOL><INDENT>print('<STR_LIT>', pid)<EOL>os.kill(pid, signal.SIGKILL)<EOL><DEDENT>except OSError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>

Cleanup on exit by killing any leftover processes.


if inspect.isfunction(obj):<EOL><INDENT>func_obj = obj<EOL><DEDENT>elif inspect.ismethod(obj):<EOL><INDENT>func_obj = obj.__func__<EOL><DEDENT>else:<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>args, varargs, varkw = inspect.getargs(func_obj.__code__)<EOL>return args, varargs, varkw, func_obj.__defaults__<EOL>

Get the names and default values of a function's arguments.

    A tuple of four things is returned: (args, varargs, varkw, defaults).
    'args' is a list of the argument names (it may contain nested lists).
    'varargs' and 'varkw' are the names of the * and ** arguments or None.
    'defaults' is an n-tuple of the default values of the last n arguments.

    Modified version of inspect.getargspec from the Python Standard
    Library.


return self.x==y<EOL>

Example:

        >>> ff2 = FooClass(3)
        Making a FooClass.
        >>> ff2.baz(3)
        True


return '<STR_LIT>'<EOL>

Some pure python tests...

    >>> pyfunc()
    'pyfunc'

    >>> import os

    >>> 2+3
    5

    >>> for i in range(3):
    ...     print i,
    ...     print i+1,
    ...
    0 1 1 2 2 3


return '<STR_LIT>'<EOL>

Some pure python tests...

    >>> 1+1
    2


return '<STR_LIT>'<EOL>


return '<STR_LIT>'<EOL>

A function with some random output.

       Normal examples are verified as usual:
       >>> 1+3
       4

       But if you put '# random' in the output, it is ignored:
       >>> 1+3
       junk goes here...  # random

       >>> 1+2
       again,  anything goes #random
       if multiline, the random mark is only needed once.

       >>> 1+2
       You can also put the random marker at the end:
       # random

       >>> 1+2
       # random
       .. or at the beginning.

       More correct input is properly verified:
       >>> ranfunc()
       'ranfunc'


pass<EOL>

A function where we ignore the output of ALL examples.

    Examples:

      # all-random

      This mark tells the testing machinery that all subsequent examples should
      be treated as random (ignoring their output).  They are still executed,
      so if a they raise an error, it will be detected as such, but their
      output is completely ignored.

      >>> 1+3
      junk goes here...

      >>> 1+3
      klasdfj;

      >>> 1+2
      again,  anything goes
      blah...


return os.path.splitext(filename)[<NUM_LIT:1>].lower() in ('<STR_LIT>','<STR_LIT>')<EOL>

Return whether the given filename is an extension module.

    This simply checks that the extension is either .so or .pyd.


if module is None:<EOL><INDENT>return True<EOL><DEDENT>elif inspect.isfunction(object):<EOL><INDENT>return module.__dict__ is object.__globals__<EOL><DEDENT>elif inspect.isbuiltin(object):<EOL><INDENT>return module.__name__ == object.__module__<EOL><DEDENT>elif inspect.isclass(object):<EOL><INDENT>return module.__name__ == object.__module__<EOL><DEDENT>elif inspect.ismethod(object):<EOL><INDENT>return module.__name__ == object.__self__.__class__.__module__<EOL><DEDENT>elif inspect.getmodule(object) is not None:<EOL><INDENT>return module is inspect.getmodule(object)<EOL><DEDENT>elif hasattr(object, '<STR_LIT>'):<EOL><INDENT>return module.__name__ == object.__module__<EOL><DEDENT>elif isinstance(object, property):<EOL><INDENT>return True <EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>

Return true if the given object is defined in the given
module.


def check_output(self, want, got, optionflags):

<EOL>ret = doctest.OutputChecker.check_output(self, want, got,<EOL>optionflags)<EOL>if not ret and self.random_re.search(want):<EOL><INDENT>return True<EOL><DEDENT>return ret<EOL>

Check output, accepting special markers embedded in the output.

        If the output didn't pass the default validation but the special string
        '#random' is included, we accept it.


<EOL>if isinstance(self._dt_test.examples[<NUM_LIT:0>], IPExample):<EOL><INDENT>self.user_ns_orig = {}<EOL>self.user_ns_orig.update(_ip.user_ns)<EOL>_ip.user_ns.update(self._dt_test.globs)<EOL>_ip.user_ns.pop('<STR_LIT:_>', None)<EOL>_ip.user_ns['<STR_LIT>'] = builtin_mod<EOL>self._dt_test.globs = _ip.user_ns<EOL><DEDENT>super(DocTestCase, self).setUp()<EOL>

Modified test setup that syncs with ipython namespace


out = []<EOL>newline = out.append<EOL>for lnum,line in enumerate(source.strip().splitlines()):<EOL><INDENT>newline(_ip.prefilter(line,lnum><NUM_LIT:0>))<EOL><DEDENT>newline('<STR_LIT>')  <EOL>return '<STR_LIT:\n>'.join(out)<EOL>

Convert input IPython source into valid Python.


def parse(self, string, name='<STR_LIT>'):

<EOL>string = string.expandtabs()<EOL>min_indent = self._min_indent(string)<EOL>if min_indent > <NUM_LIT:0>:<EOL><INDENT>string = '<STR_LIT:\n>'.join([l[min_indent:] for l in string.split('<STR_LIT:\n>')])<EOL><DEDENT>output = []<EOL>charno, lineno = <NUM_LIT:0>, <NUM_LIT:0><EOL>if self._RANDOM_TEST.search(string):<EOL><INDENT>random_marker = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>random_marker = '<STR_LIT>'<EOL><DEDENT>ip2py = False<EOL>terms = list(self._EXAMPLE_RE_PY.finditer(string))<EOL>if terms:<EOL><INDENT>Example = doctest.Example<EOL><DEDENT>else:<EOL><INDENT>terms = list(self._EXAMPLE_RE_IP.finditer(string))<EOL>if self._EXTERNAL_IP.search(string):<EOL><INDENT>Example = IPExternalExample<EOL><DEDENT>else:<EOL><INDENT>Example = IPExample<EOL>ip2py = True<EOL><DEDENT><DEDENT>for m in terms:<EOL><INDENT>output.append(string[charno:m.start()])<EOL>lineno += string.count('<STR_LIT:\n>', charno, m.start())<EOL>(source, options, want, exc_msg) =self._parse_example(m, name, lineno,ip2py)<EOL>want += random_marker<EOL>if Example is IPExternalExample:<EOL><INDENT>options[doctest.NORMALIZE_WHITESPACE] = True<EOL>want += '<STR_LIT:\n>'<EOL><DEDENT>if not self._IS_BLANK_OR_COMMENT(source):<EOL><INDENT>output.append(Example(source, want, exc_msg,<EOL>lineno=lineno,<EOL>indent=min_indent+len(m.group('<STR_LIT>')),<EOL>options=options))<EOL><DEDENT>lineno += string.count('<STR_LIT:\n>', m.start(), m.end())<EOL>charno = m.end()<EOL><DEDENT>output.append(string[charno:])<EOL>return output<EOL>

Divide the given string into examples and intervening text,
and return them as a list of alternating Examples and strings.
Line numbers for the Examples are 0-based.  The optional
argument `name` is a name identifying this string, and is only
used for error messages.


def _parse_example(self, m, name, lineno,ip2py=False):

<EOL>indent = len(m.group('<STR_LIT>'))<EOL>source_lines = m.group('<STR_LIT:source>').split('<STR_LIT:\n>')<EOL>ps1 = m.group('<STR_LIT>')<EOL>ps2 = m.group('<STR_LIT>')<EOL>ps1_len = len(ps1)<EOL>self._check_prompt_blank(source_lines, indent, name, lineno,ps1_len)<EOL>if ps2:<EOL><INDENT>self._check_prefix(source_lines[<NUM_LIT:1>:], '<STR_LIT:U+0020>'*indent + ps2, name, lineno)<EOL><DEDENT>source = '<STR_LIT:\n>'.join([sl[indent+ps1_len+<NUM_LIT:1>:] for sl in source_lines])<EOL>if ip2py:<EOL><INDENT>source = self.ip2py(source)<EOL><DEDENT>want = m.group('<STR_LIT>')<EOL>want_lines = want.split('<STR_LIT:\n>')<EOL>if len(want_lines) > <NUM_LIT:1> and re.match(r'<STR_LIT>', want_lines[-<NUM_LIT:1>]):<EOL><INDENT>del want_lines[-<NUM_LIT:1>]  <EOL><DEDENT>self._check_prefix(want_lines, '<STR_LIT:U+0020>'*indent, name,<EOL>lineno + len(source_lines))<EOL>want_lines[<NUM_LIT:0>] = re.sub(r'<STR_LIT>','<STR_LIT>',want_lines[<NUM_LIT:0>])<EOL>want = '<STR_LIT:\n>'.join([wl[indent:] for wl in want_lines])<EOL>m = self._EXCEPTION_RE.match(want)<EOL>if m:<EOL><INDENT>exc_msg = m.group('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>exc_msg = None<EOL><DEDENT>options = self._find_options(source, name, lineno)<EOL>return source, options, want, exc_msg<EOL>

Given a regular expression match from `_EXAMPLE_RE` (`m`),
return a pair `(source, want)`, where `source` is the matched
example's source code (with prompts and indentation stripped);
and `want` is the example's expected output (with indentation
stripped).

`name` is the string's name, and `lineno` is the line number
where the example starts; both are used for error messages.

Optional:
`ip2py`: if true, filter the input via IPython to convert the syntax
into valid python.


def _check_prompt_blank(self, lines, indent, name, lineno, ps1_len):

space_idx = indent+ps1_len<EOL>min_len = space_idx+<NUM_LIT:1><EOL>for i, line in enumerate(lines):<EOL><INDENT>if len(line) >=  min_len and line[space_idx] != '<STR_LIT:U+0020>':<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>'<STR_LIT>' %<EOL>(lineno+i+<NUM_LIT:1>, name,<EOL>line[indent:space_idx], line))<EOL><DEDENT><DEDENT>

Given the lines of a source string (including prompts and
leading indentation), check to make sure that every prompt is
followed by a space character.  If any line is not followed by
a space character, then raise ValueError.

Note: IPython-modified version which takes the input prompt length as a
parameter, so that prompts of variable length can be dealt with.


if exclude_patterns is None:<EOL><INDENT>exclude_patterns = []<EOL><DEDENT>self.exclude_patterns = list(map(re.compile,exclude_patterns))<EOL>doctests.Doctest.__init__(self)<EOL>

Create a new ExtensionDoctest plugin.

        Parameters
        ----------

        exclude_patterns : sequence of strings, optional
          These patterns are compiled as regular expressions, subsequently used
          to exclude any filename which matches them from inclusion in the test
          suite (using pattern.search(), NOT pattern.match() ).


<EOL>for pat in self.exclude_patterns:<EOL><INDENT>if pat.search(filename):<EOL><INDENT>return False<EOL><DEDENT><DEDENT>if is_extension_module(filename):<EOL><INDENT>return True<EOL><DEDENT>else:<EOL><INDENT>return doctests.Doctest.wantFile(self,filename)<EOL><DEDENT>

Return whether the given filename should be scanned for tests.

        Modified version that accepts extension modules as valid containers for
        doctests.


for pat in self.exclude_patterns:<EOL><INDENT>if pat.search(directory):<EOL><INDENT>return False<EOL><DEDENT><DEDENT>return True<EOL>

Return whether the given directory should be scanned for tests.

        Modified version that supports exclusions.


<EOL>optionflags = doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS<EOL>doctests = self.finder.find(obj, module=getmodule(parent))<EOL>if doctests:<EOL><INDENT>for test in doctests:<EOL><INDENT>if len(test.examples) == <NUM_LIT:0>:<EOL><INDENT>continue<EOL><DEDENT>yield DocTestCase(test, obj=obj,<EOL>optionflags=optionflags,<EOL>checker=self.checker)<EOL><DEDENT><DEDENT>

Look for doctests in the given object, which will be a
        function, method or class.


<EOL>finder = py_file_finder(arg_s)<EOL>return get_ipython().magic_run_ori(arg_s, runner, finder)<EOL>

Modified version of %run that syncs testing namespaces.

    This is strictly needed for running doctests that call %run.


<EOL>print(self.getoutput(cmd, split=False, depth=<NUM_LIT:1>).rstrip(), end='<STR_LIT>', file=sys.stdout)<EOL>sys.stdout.flush()<EOL>

Replace the default system call with a capturing one for doctest.


def _showtraceback(self, etype, evalue, stb):

print(self.InteractiveTB.stb2text(stb), file=sys.stdout)<EOL>

Print the traceback purely on stdout for doctest to capture it.


global get_ipython<EOL>if hasattr(start_ipython, '<STR_LIT>'):<EOL><INDENT>return<EOL><DEDENT>start_ipython.already_called = True<EOL>_displayhook = sys.displayhook<EOL>_excepthook = sys.excepthook<EOL>_main = sys.modules.get('<STR_LIT:__main__>')<EOL>config = tools.default_config()<EOL>shell = TerminalInteractiveShell.instance(config=config,<EOL>)<EOL>shell.tempfiles.append(config.HistoryManager.hist_file)<EOL>shell.builtin_trap.activate()<EOL>shell.system = py3compat.MethodType(xsys, shell)<EOL>shell._showtraceback = py3compat.MethodType(_showtraceback, shell)<EOL>sys.modules['<STR_LIT:__main__>'] = _main<EOL>sys.displayhook = _displayhook<EOL>sys.excepthook = _excepthook<EOL>_ip = shell<EOL>get_ipython = _ip.get_ipython<EOL>builtin_mod._ip = _ip<EOL>builtin_mod.get_ipython = get_ipython<EOL>io.stdout = StreamProxy('<STR_LIT>')<EOL>io.stderr = StreamProxy('<STR_LIT>')<EOL>def nopage(strng, start=<NUM_LIT:0>, screen_lines=<NUM_LIT:0>, pager_cmd=None):<EOL><INDENT>print(strng)<EOL><DEDENT>page.orig_page = page.page<EOL>page.page = nopage<EOL>return _ip<EOL>

Start a global IPython shell, which we need for IPython-specific syntax.


class Tester(ParametricTestCase):<EOL><INDENT>test = staticmethod(func)<EOL><DEDENT>Tester.__name__ = func.__name__<EOL>return Tester<EOL>

Decorator to make a simple function into a normal test via unittest.


<EOL>parser = optparse.OptionParser(usage=__doc__)<EOL>newopt = parser.add_option<EOL>newopt('<STR_LIT>','<STR_LIT>',action='<STR_LIT:store_true>',dest='<STR_LIT>',default=False,<EOL>help='<STR_LIT>')<EOL>newopt('<STR_LIT>','<STR_LIT>',action='<STR_LIT:store_true>',dest='<STR_LIT>',default=False,<EOL>help='<STR_LIT>')<EOL>opts,args = parser.parse_args()<EOL>if len(args) < <NUM_LIT:1>:<EOL><INDENT>parser.error("<STR_LIT>")<EOL><DEDENT>fname = args[<NUM_LIT:0>]<EOL>auto_gen_output = False<EOL>try:<EOL><INDENT>outfname = args[<NUM_LIT:1>]<EOL><DEDENT>except IndexError:<EOL><INDENT>outfname = None<EOL><DEDENT>if fname.endswith('<STR_LIT>') and outfname is None:<EOL><INDENT>outfname = fname.replace('<STR_LIT>','<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>bname, ext = os.path.splitext(fname)<EOL>if ext in ['<STR_LIT>','<STR_LIT>']:<EOL><INDENT>auto_gen_output = True<EOL><DEDENT>if outfname is None:<EOL><INDENT>outfname = bname+'<STR_LIT>'<EOL><DEDENT><DEDENT>if auto_gen_output:<EOL><INDENT>infile = tempfile.TemporaryFile()<EOL>infile.write(TPL % fname)<EOL>infile.flush()<EOL>infile.seek(<NUM_LIT:0>)<EOL><DEDENT>else:<EOL><INDENT>infile = open(fname)<EOL><DEDENT>if opts.stdout:<EOL><INDENT>outfile = sys.stdout<EOL><DEDENT>else:<EOL><INDENT>if os.path.isfile(outfname) and not opts.force:<EOL><INDENT>fatal("<STR_LIT>"<EOL>% outfname)<EOL><DEDENT>outfile = open(outfname,'<STR_LIT:w>')<EOL><DEDENT>indentOut = IndentOut(outfile,<NUM_LIT:4>)<EOL>getRunner = RunnerFactory(indentOut)<EOL>rst_transition = '<STR_LIT:\n>'+'<STR_LIT:->'*<NUM_LIT>+'<STR_LIT>'<EOL>write = outfile.write<EOL>for line in infile:<EOL><INDENT>if line.startswith('<STR_LIT>'):<EOL><INDENT>incfname = line.split()[<NUM_LIT:1>]<EOL>write(rst_transition)<EOL>write('<STR_LIT>' % incfname)<EOL>getRunner(incfname).run_file(incfname)<EOL>write('<STR_LIT>' % incfname)<EOL>write(rst_transition)<EOL><DEDENT>else:<EOL><INDENT>write(line)<EOL><DEDENT><DEDENT>infile.close()<EOL>if outfile is not sys.stdout:<EOL><INDENT>outfile.close()<EOL><DEDENT>


self.indent_text = '<STR_LIT:U+0020>'*indent<EOL>self.indent = re.compile('<STR_LIT>',re.MULTILINE).sub<EOL>self.out = out<EOL>self._write = out.write<EOL>self.buffer = []<EOL>self._closed = False<EOL>

Create an indented writer.

        :Keywords:

        - `out` : stream (sys.stdout)
          Output stream to actually write to after indenting.

        - `indent` : int
          Number of spaces to indent every input line by.


if self._closed:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>self.buffer.append(data)<EOL>


self.out = out<EOL>self.runner = None<EOL>self.runnerClass = None<EOL>


if fname.endswith('<STR_LIT>'):<EOL><INDENT>runnerClass = irunner.PythonRunner<EOL><DEDENT>elif fname.endswith('<STR_LIT>'):<EOL><INDENT>runnerClass = irunner.IPythonRunner<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>' % fname)<EOL><DEDENT>if self.runner is None:<EOL><INDENT>return self._makeRunner(runnerClass)<EOL><DEDENT>else:<EOL><INDENT>if runnerClass==self.runnerClass:<EOL><INDENT>return self.runner<EOL><DEDENT>else:<EOL><INDENT>e='<STR_LIT>' %(self.runnerClass,fname)<EOL>raise ValueError(e)<EOL><DEDENT><DEDENT>


return ['<STR_LIT>', <EOL>'<STR_LIT>', '<STR_LIT>','<STR_LIT>',<EOL>'<STR_LIT>']<EOL>

Return a valid default argv for creating testing instances of ipython


config = Config()<EOL>config.TerminalInteractiveShell.colors = '<STR_LIT>'<EOL>config.TerminalTerminalInteractiveShell.term_title = False,<EOL>config.TerminalInteractiveShell.autocall = <NUM_LIT:0><EOL>config.HistoryManager.hist_file = tempfile.mktemp('<STR_LIT>')<EOL>config.HistoryManager.db_cache_size = <NUM_LIT><EOL>return config<EOL>

Return a config object with good defaults for testing.


if options is None: options = []<EOL>prompt_opts = [ '<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>'<EOL>]<EOL>cmdargs = '<STR_LIT:U+0020>'.join(default_argv() + prompt_opts + options)<EOL>_ip = get_ipython()<EOL>test_dir = os.path.dirname(__file__)<EOL>ipython_cmd = find_cmd('<STR_LIT>' if py3compat.PY3 else '<STR_LIT>')<EOL>full_fname = os.path.join(test_dir, fname)<EOL>full_cmd = '<STR_LIT>' % (ipython_cmd, cmdargs, full_fname)<EOL>out, err = getoutputerror(full_cmd)<EOL>if out:<EOL><INDENT>out = re.sub(r'<STR_LIT>', '<STR_LIT>', out)<EOL><DEDENT>return out, err<EOL>

Utility to call 'ipython filename'.

    Starts IPython witha minimal and safe configuration to make startup as fast
    as possible.

    Note that this starts IPython in a subprocess!

    Parameters
    ----------
    fname : str
      Name of file to be executed (should have .py or .ipy extension).

    options : optional, list
      Extra command-line flags to be passed to IPython.

    Returns
    -------
    (stdout, stderr) of ipython subprocess.


def ipexec_validate(fname, expected_out, expected_err='<STR_LIT>',<EOL>options=None):

import nose.tools as nt<EOL>out, err = ipexec(fname, options)<EOL>if err:<EOL><INDENT>if expected_err:<EOL><INDENT>nt.assert_equals(err.strip(), expected_err.strip())<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>' %<EOL>(fname, err))<EOL><DEDENT><DEDENT>nt.assert_equals(out.strip(), expected_out.strip())<EOL>

Utility to call 'ipython filename' and validate output/error.

    This function raises an AssertionError if the validation fails.

    Note that this starts IPython in a subprocess!

    Parameters
    ----------
    fname : str
      Name of the file to be executed (should have .py or .ipy extension).

    expected_out : str
      Expected stdout of the process.

    expected_err : optional, str
      Expected stderr of the process.

    options : optional, list
      Extra command-line flags to be passed to IPython.

    Returns
    -------
    None


name = getattr(func, "<STR_LIT>", getattr(func, "<STR_LIT>", "<STR_LIT>"))<EOL>for inp, expected in pairs:<EOL><INDENT>out = func(inp)<EOL>assert out == expected, pair_fail_msg.format(name, inp, expected, out)<EOL><DEDENT>

Utility function for the common case of checking a function with a
    sequence of input/output pairs.

    Parameters
    ----------
    func : callable
      The function to be tested. Should accept a single argument.
    pairs : iterable
      A list of (input, expected_output) tuples.

    Returns
    -------
    None. Raises an AssertionError if any output does not match the expected
    value.


f = open(name, '<STR_LIT:w>')<EOL>f.close()<EOL>try:<EOL><INDENT>yield<EOL><DEDENT>finally:<EOL><INDENT>os.unlink(name)<EOL><DEDENT>

Create an empty, named, temporary file for the duration of the context.


fname, f = temp_pyfile(src, ext)<EOL>self.tmpfile = f<EOL>self.fname = fname<EOL>


import nose.tools<EOL>return decorator(wrapper,nose.tools.make_decorator(func)(wrapper))<EOL>

Apply a wrapper to a function for decoration.

    This mixes Michele Simionato's decorator tool with nose's make_decorator,
    to apply a wrapper in a decorator so that all nose attributes, as well as
    function signature and other properties, survive the decoration cleanly.
    This will ensure that wrapped functions can still be well introspected via
    IPython, for example.


if isinstance(label,basestring):<EOL><INDENT>labels = [label]<EOL><DEDENT>else:<EOL><INDENT>labels = label<EOL><DEDENT>tmp = lambda : None<EOL>for label in labels:<EOL><INDENT>setattr(tmp,label,True)<EOL><DEDENT>def decor(f):<EOL><INDENT>for label in labels:<EOL><INDENT>setattr(f,label,True)<EOL><DEDENT>return f<EOL><DEDENT>if ds is None:<EOL><INDENT>ds = "<STR_LIT>" % label<EOL><DEDENT>decor.__doc__ = ds<EOL>return decor<EOL>

Factory function to create a decorator that applies one or more labels.

    Parameters
    ----------
      label : string or sequence
      One or more labels that will be applied by the decorator to the functions
    it decorates.  Labels are attributes of the decorated function with their
    value set to True.

      ds : string
      An optional docstring for the resulting decorator.  If not given, a
      default docstring is auto-generated.

    Returns
    -------
      A decorator.

    Examples
    --------

    A simple labeling decorator:
    >>> slow = make_label_dec('slow')
    >>> print slow.__doc__
    Labels a test as 'slow'.

    And one that uses multiple labels and a custom docstring:
    >>> rare = make_label_dec(['slow','hard'],
    ... "Mix labels 'slow' and 'hard' for rare tests.")
    >>> print rare.__doc__
    Mix labels 'slow' and 'hard' for rare tests.

    Now, let's test using this one:
    >>> @rare
    ... def f(): pass
    ...
    >>>
    >>> f.slow
    True
    >>> f.hard
    True


def skip_decorator(f):<EOL><INDENT>import nose<EOL>if callable(skip_condition):<EOL><INDENT>skip_val = skip_condition<EOL><DEDENT>else:<EOL><INDENT>skip_val = lambda : skip_condition<EOL><DEDENT>def get_msg(func,msg=None):<EOL><INDENT>"""<STR_LIT>"""<EOL>if msg is None: out = '<STR_LIT>'<EOL>else: out = msg<EOL>return "<STR_LIT>" % (func.__name__,out)<EOL><DEDENT>def skipper_func(*args, **kwargs):<EOL><INDENT>"""<STR_LIT>"""<EOL>if skip_val():<EOL><INDENT>raise nose.SkipTest(get_msg(f,msg))<EOL><DEDENT>else:<EOL><INDENT>return f(*args, **kwargs)<EOL><DEDENT><DEDENT>def skipper_gen(*args, **kwargs):<EOL><INDENT>"""<STR_LIT>"""<EOL>if skip_val():<EOL><INDENT>raise nose.SkipTest(get_msg(f,msg))<EOL><DEDENT>else:<EOL><INDENT>for x in f(*args, **kwargs):<EOL><INDENT>yield x<EOL><DEDENT><DEDENT><DEDENT>if nose.util.isgenerator(f):<EOL><INDENT>skipper = skipper_gen<EOL><DEDENT>else:<EOL><INDENT>skipper = skipper_func<EOL><DEDENT>return nose.tools.make_decorator(f)(skipper)<EOL><DEDENT>return skip_decorator<EOL>

Make function raise SkipTest exception if skip_condition is true

    Parameters
    ----------
    skip_condition : bool or callable.
    Flag to determine whether to skip test.  If the condition is a
    callable, it is used at runtime to dynamically make the decision.  This
    is useful for tests that may require costly imports, to delay the cost
    until the test suite is actually executed.
    msg : string
        Message to give on raising a SkipTest exception

   Returns
   -------
   decorator : function
       Decorator, which, when applied to a function, causes SkipTest
       to be raised when the skip_condition was True, and the function
       to be called normally otherwise.

    Notes
    -----
    You will see from the code that we had to further decorate the
    decorator with the nose.tools.make_decorator function in order to
    transmit function name, and various other metadata.


return skipif(True,msg)<EOL>

Decorator factory - mark a test function for skipping from test suite.

    Parameters
    ----------
      msg : string
        Optional message to be added.

    Returns
    -------
       decorator : function
         Decorator, which, when applied to a function, causes SkipTest
         to be raised, with the optional message added.


if callable(condition):<EOL><INDENT>skip_condition = lambda : not condition()<EOL><DEDENT>else:<EOL><INDENT>skip_condition = lambda : not condition<EOL><DEDENT>return skipif(skip_condition, msg)<EOL>

The reverse from skipif, see skipif for details.


try:<EOL><INDENT>mod = __import__(module)<EOL>mod_not_avail = False<EOL><DEDENT>except ImportError:<EOL><INDENT>mod_not_avail = True<EOL><DEDENT>return mod_not_avail<EOL>

Can module be imported?  Returns true if module does NOT import.

    This is used to make a decorator to skip tests that require module to be
    available, but delay the 'import numpy' to test execution time.


dct = {}<EOL>dct.setdefault('<STR_LIT>',lambda : True)<EOL>dct.setdefault('<STR_LIT>',__file__)<EOL>if adict is not None:<EOL><INDENT>dct.update(adict)<EOL><DEDENT>fm.__dict__.clear()<EOL>fm.__dict__.update(dct)<EOL>

Initialize a FakeModule instance __dict__.

    Kept as a standalone function and not a method so the FakeModule API can
    remain basically empty.

    This should be considered for private IPython use, used in managing
    namespaces for %run.

    Parameters
    ----------

    fm : FakeModule instance

    adict : dict, optional


def page_dumb(strng, start=<NUM_LIT:0>, screen_lines=<NUM_LIT>):

out_ln  = strng.splitlines()[start:]<EOL>screens = chop(out_ln,screen_lines-<NUM_LIT:1>)<EOL>if len(screens) == <NUM_LIT:1>:<EOL><INDENT>print(os.linesep.join(screens[<NUM_LIT:0>]), file=io.stdout)<EOL><DEDENT>else:<EOL><INDENT>last_escape = "<STR_LIT>"<EOL>for scr in screens[<NUM_LIT:0>:-<NUM_LIT:1>]:<EOL><INDENT>hunk = os.linesep.join(scr)<EOL>print(last_escape + hunk, file=io.stdout)<EOL>if not page_more():<EOL><INDENT>return<EOL><DEDENT>esc_list = esc_re.findall(hunk)<EOL>if len(esc_list) > <NUM_LIT:0>:<EOL><INDENT>last_escape = esc_list[-<NUM_LIT:1>]<EOL><DEDENT><DEDENT>print(last_escape + os.linesep.join(screens[-<NUM_LIT:1>]), file=io.stdout)<EOL><DEDENT>

Very dumb 'pager' in Python, for when nothing else works.

    Only moves forward, same interface as page(), except for pager_cmd and
    mode.


TERM = os.environ.get('<STR_LIT>',None)<EOL>if (TERM=='<STR_LIT>' or TERM=='<STR_LIT>') and sys.platform != '<STR_LIT>':<EOL><INDENT>local_use_curses = use_curses<EOL><DEDENT>else:<EOL><INDENT>local_use_curses = False<EOL><DEDENT>if local_use_curses:<EOL><INDENT>import termios<EOL>import curses<EOL>term_flags = termios.tcgetattr(sys.stdout)<EOL>NCURSES_NO_SETBUF = os.environ.get('<STR_LIT>', None)<EOL>os.environ['<STR_LIT>'] = '<STR_LIT>'<EOL>scr = curses.initscr()<EOL>screen_lines_real,screen_cols = scr.getmaxyx()<EOL>curses.endwin()<EOL>if NCURSES_NO_SETBUF is None:<EOL><INDENT>del os.environ['<STR_LIT>']<EOL><DEDENT>else:<EOL><INDENT>os.environ['<STR_LIT>'] = NCURSES_NO_SETBUF<EOL><DEDENT>termios.tcsetattr(sys.stdout,termios.TCSANOW,term_flags)<EOL>return screen_lines_real<EOL><DEDENT>else:<EOL><INDENT>return screen_lines_def<EOL><DEDENT>

Attempt to work out the number of lines on the screen.

    This is called by page(). It can raise an error (e.g. when run in the
    test suite), so it's separated out so it can easily be called in a try block.


def page(strng, start=<NUM_LIT:0>, screen_lines=<NUM_LIT:0>, pager_cmd=None):

<EOL>start = max(<NUM_LIT:0>, start)<EOL>ip = ipapi.get()<EOL>if ip:<EOL><INDENT>try:<EOL><INDENT>ip.hooks.show_in_pager(strng)<EOL>return<EOL><DEDENT>except TryNext:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>TERM = os.environ.get('<STR_LIT>','<STR_LIT>')<EOL>if TERM in ['<STR_LIT>','<STR_LIT>'] and os.name != '<STR_LIT>':<EOL><INDENT>print(strng)<EOL>return<EOL><DEDENT>str_lines = strng.splitlines()[start:]<EOL>str_toprint = os.linesep.join(str_lines)<EOL>num_newlines = len(str_lines)<EOL>len_str = len(str_toprint)<EOL>numlines = max(num_newlines,int(len_str/<NUM_LIT>)+<NUM_LIT:1>)<EOL>screen_lines_def = get_terminal_size()[<NUM_LIT:1>]<EOL>if screen_lines <= <NUM_LIT:0>:<EOL><INDENT>try:<EOL><INDENT>screen_lines += _detect_screen_size(use_curses, screen_lines_def)<EOL><DEDENT>except (TypeError, UnsupportedOperation):<EOL><INDENT>print(str_toprint, file=io.stdout)<EOL>return<EOL><DEDENT><DEDENT>if numlines <= screen_lines :<EOL><INDENT>print(str_toprint, file=io.stdout)<EOL><DEDENT>else:<EOL><INDENT>pager_cmd = get_pager_cmd(pager_cmd)<EOL>pager_cmd += '<STR_LIT:U+0020>' + get_pager_start(pager_cmd,start)<EOL>if os.name == '<STR_LIT>':<EOL><INDENT>if pager_cmd.startswith('<STR_LIT:type>'):<EOL><INDENT>retval = <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>tmpname = tempfile.mktemp('<STR_LIT>')<EOL>tmpfile = open(tmpname,'<STR_LIT>')<EOL>tmpfile.write(strng)<EOL>tmpfile.close()<EOL>cmd = "<STR_LIT>" % (pager_cmd,tmpname)<EOL>if os.system(cmd):<EOL><INDENT>retval = <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>retval = None<EOL><DEDENT>os.remove(tmpname)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>try:<EOL><INDENT>retval = None<EOL>pager = os.popen(pager_cmd,'<STR_LIT:w>')<EOL>pager.write(strng)<EOL>pager.close()<EOL>retval = pager.close()  <EOL><DEDENT>except IOError as msg:  <EOL><INDENT>if msg.args == (<NUM_LIT:32>,'<STR_LIT>'):<EOL><INDENT>retval = None<EOL><DEDENT>else:<EOL><INDENT>retval = <NUM_LIT:1><EOL><DEDENT><DEDENT>except OSError:<EOL><INDENT>retval = <NUM_LIT:1><EOL><DEDENT><DEDENT>if retval is not None:<EOL><INDENT>page_dumb(strng,screen_lines=screen_lines)<EOL><DEDENT><DEDENT>

Print a string, piping through a pager after a certain length.

    The screen_lines parameter specifies the number of *usable* lines of your
    terminal screen (total lines minus lines you need to reserve to show other
    information).

    If you set screen_lines to a number <=0, page() will try to auto-determine
    your screen size and will only use up to (screen_size+screen_lines) for
    printing, paging after that. That is, if you want auto-detection but need
    to reserve the bottom 3 lines of the screen, use screen_lines = -3, and for
    auto-detection without any lines reserved simply use screen_lines = 0.

    If a string won't fit in the allowed lines, it is sent through the
    specified pager command. If none given, look for PAGER in the environment,
    and ultimately default to less.

    If no system pager works, the string is sent through a 'dumb pager'
    written in python, very simplistic.


def page_file(fname, start=<NUM_LIT:0>, pager_cmd=None):

pager_cmd = get_pager_cmd(pager_cmd)<EOL>pager_cmd += '<STR_LIT:U+0020>' + get_pager_start(pager_cmd,start)<EOL>try:<EOL><INDENT>if os.environ['<STR_LIT>'] in ['<STR_LIT>','<STR_LIT>']:<EOL><INDENT>raise EnvironmentError<EOL><DEDENT>system(pager_cmd + '<STR_LIT:U+0020>' + fname)<EOL><DEDENT>except:<EOL><INDENT>try:<EOL><INDENT>if start > <NUM_LIT:0>:<EOL><INDENT>start -= <NUM_LIT:1><EOL><DEDENT>page(open(fname).read(),start)<EOL><DEDENT>except:<EOL><INDENT>print('<STR_LIT>',repr(fname))<EOL><DEDENT><DEDENT>

Page a file, using an optional pager command and starting line.


if os.name == '<STR_LIT>':<EOL><INDENT>default_pager_cmd = '<STR_LIT>'  <EOL><DEDENT>elif os.name in ['<STR_LIT>','<STR_LIT>']:<EOL><INDENT>default_pager_cmd = '<STR_LIT:type>'<EOL><DEDENT>if pager_cmd is None:<EOL><INDENT>try:<EOL><INDENT>pager_cmd = os.environ['<STR_LIT>']<EOL><DEDENT>except:<EOL><INDENT>pager_cmd = default_pager_cmd<EOL><DEDENT><DEDENT>return pager_cmd<EOL>

Return a pager command.

    Makes some attempts at finding an OS-correct one.


if pager in ['<STR_LIT>','<STR_LIT>']:<EOL><INDENT>if start:<EOL><INDENT>start_string = '<STR_LIT:+>' + str(start)<EOL><DEDENT>else:<EOL><INDENT>start_string = '<STR_LIT>'<EOL><DEDENT><DEDENT>else:<EOL><INDENT>start_string = '<STR_LIT>'<EOL><DEDENT>return start_string<EOL>

Return the string for paging files with an offset.

    This is the '+N' argument which less and more (under Unix) accept.


def snip_print(str,width = <NUM_LIT>,print_full = <NUM_LIT:0>,header = '<STR_LIT>'):

if print_full == <NUM_LIT:1>:<EOL><INDENT>page(header+str)<EOL>return <NUM_LIT:0><EOL><DEDENT>print(header, end='<STR_LIT:U+0020>')<EOL>if len(str) < width:<EOL><INDENT>print(str)<EOL>snip = <NUM_LIT:0><EOL><DEDENT>else:<EOL><INDENT>whalf = int((width -<NUM_LIT:5>)/<NUM_LIT:2>)<EOL>print(str[:whalf] + '<STR_LIT>' + str[-whalf:])<EOL>snip = <NUM_LIT:1><EOL><DEDENT>if snip and print_full == <NUM_LIT:2>:<EOL><INDENT>if input(header+'<STR_LIT>').lower() == '<STR_LIT:y>':<EOL><INDENT>page(str)<EOL><DEDENT><DEDENT>return snip<EOL>

Print a string snipping the midsection to fit in width.

    print_full: mode control:
      - 0: only snip long strings
      - 1: send to page() directly.
      - 2: snip long strings and ask for full length viewing with page()
    Return 1 if snipping was necessary, 0 otherwise.


from IPython.utils.syspathcontext import prepended_to_syspath<EOL>if module_str not in sys.modules:<EOL><INDENT>with prepended_to_syspath(self.ipython_extension_dir):<EOL><INDENT>__import__(module_str)<EOL><DEDENT><DEDENT>mod = sys.modules[module_str]<EOL>return self._call_load_ipython_extension(mod)<EOL>

Load an IPython extension by its module name.

        If :func:`load_ipython_extension` returns anything, this function
        will return that object.


if module_str in sys.modules:<EOL><INDENT>mod = sys.modules[module_str]<EOL>self._call_unload_ipython_extension(mod)<EOL><DEDENT>

Unload an IPython extension by its module name.

        This function looks up the extension's name in ``sys.modules`` and
        simply calls ``mod.unload_ipython_extension(self)``.


from IPython.utils.syspathcontext import prepended_to_syspath<EOL>with prepended_to_syspath(self.ipython_extension_dir):<EOL><INDENT>if module_str in sys.modules:<EOL><INDENT>mod = sys.modules[module_str]<EOL>importlib.reload(mod)<EOL>self._call_load_ipython_extension(mod)<EOL><DEDENT>else:<EOL><INDENT>self.load_extension(module_str)<EOL><DEDENT><DEDENT>

Reload an IPython extension by calling reload.

        If the module has not been loaded before,
        :meth:`InteractiveShell.load_extension` is called. Otherwise
        :func:`reload` is called and then the :func:`load_ipython_extension`
        function of the module, if it exists is called.


def install_extension(self, url, filename=None):

<EOL>if not os.path.isdir(self.ipython_extension_dir):<EOL><INDENT>os.makedirs(self.ipython_extension_dir, mode = <NUM_LIT>)<EOL><DEDENT>if os.path.isfile(url):<EOL><INDENT>src_filename = os.path.basename(url)<EOL>copy = copyfile<EOL><DEDENT>else:<EOL><INDENT>src_filename = urlparse(url).path.split('<STR_LIT:/>')[-<NUM_LIT:1>]<EOL>copy = urlretrieve<EOL><DEDENT>if filename is None:<EOL><INDENT>filename = src_filename<EOL><DEDENT>if os.path.splitext(filename)[<NUM_LIT:1>] not in ('<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>raise ValueError("<STR_LIT>", filename)<EOL><DEDENT>filename = os.path.join(self.ipython_extension_dir, filename)<EOL>copy(url, filename)<EOL>return filename<EOL>

Download and install an IPython extension.

        If filename is given, the file will be so named (inside the extension
        directory). Otherwise, the name from the URL will be used. The file must
        have a .py or .zip extension; otherwise, a ValueError will be raised.

        Returns the full path to the installed file.


self._ip = ip<EOL>

Will be used to set _ip point to current ipython instance b/f call

        Override this method if you don't want this to happen.


def decorate_fn_with_doc(new_fn, old_fn, additional_text="<STR_LIT>"):

def wrapper(*args, **kw):<EOL><INDENT>return new_fn(*args, **kw)<EOL><DEDENT>if old_fn.__doc__:<EOL><INDENT>wrapper.__doc__ = old_fn.__doc__ + additional_text<EOL><DEDENT>return wrapper<EOL>

Make new_fn have old_fn's doc string. This is particularly useful
    for the do_... commands that hook into the help system.
    Adapted from from a comp.lang.python posting
    by Duncan Booth.


try:<EOL><INDENT>outfile = open(fname)<EOL><DEDENT>except IOError:<EOL><INDENT>return []<EOL><DEDENT>else:<EOL><INDENT>out = outfile.readlines()<EOL>outfile.close()<EOL>return out<EOL><DEDENT>

Return the contents of a named file as a list of lines.

    This function never raises an IOError exception: if the file can't be
    read, it simply returns an empty list.


try:<EOL><INDENT>ip = get_ipython()<EOL><DEDENT>except NameError:<EOL><INDENT>BdbQuit_excepthook.excepthook_ori = sys.excepthook<EOL>sys.excepthook = BdbQuit_excepthook<EOL>def_colors = '<STR_LIT>'<EOL>try:<EOL><INDENT>import readline<EOL>readline.parse_and_bind('<STR_LIT>')<EOL><DEDENT>except ImportError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>else:<EOL><INDENT>def_colors = ip.colors<EOL>ip.set_custom_exc((bdb.BdbQuit,), BdbQuit_IPython_excepthook)<EOL><DEDENT>if colors is None:<EOL><INDENT>colors = def_colors<EOL><DEDENT>try:<EOL><INDENT>from reprlib import aRepr<EOL>aRepr.maxstring = <NUM_LIT><EOL><DEDENT>except:<EOL><INDENT>import traceback<EOL>traceback.print_exc()<EOL><DEDENT>self.debugger = Pdb(colors)<EOL>

Create a local debugger instance.

        :Parameters:

          - `colors` (None): a string containing the name of the color scheme to
        use, it must be one of IPython's valid color schemes.  If not given, the
        function will default to the current IPython scheme when running inside
        IPython, and to 'NoColor' otherwise.

        Usage example:

        from IPython.core.debugger import Tracer; debug_here = Tracer()

        ... later in your code
        debug_here()  # -> will open up the debugger at that point.

        Once the debugger activates, you can use all of its regular commands to
        step through code, set breakpoints, etc.  See the pdb documentation
        from the Python standard library for usage details.


self.debugger.set_trace(sys._getframe().f_back)<EOL>

Starts an interactive debugger at the point where called.

        This is similar to the pdb.set_trace() function from the std lib, but
        using IPython's enhanced debugger.


self.color_scheme_table.set_active_scheme(scheme)<EOL>

Shorthand access to the color table scheme selector method.


self.msg("<STR_LIT>")<EOL>return self.do_quit(arg)<EOL>

Restart command. In the context of ipython this is exactly the same
        thing as 'quit'.


filename, first, last = OldPdb.parse_list_cmd(self, arg)<EOL>if filename is not None:<EOL><INDENT>self.print_list_lines(filename, first, last)<EOL><DEDENT>

List command to use if we have a newer pydb installed


def print_list_lines(self, filename, first, last):

try:<EOL><INDENT>Colors = self.color_scheme_table.active_colors<EOL>ColorsNormal = Colors.Normal<EOL>tpl_line = '<STR_LIT>' % (Colors.lineno, ColorsNormal)<EOL>tpl_line_em = '<STR_LIT>' % (Colors.linenoEm, Colors.line, ColorsNormal)<EOL>src = []<EOL>for lineno in range(first, last+<NUM_LIT:1>):<EOL><INDENT>line = linecache.getline(filename, lineno)<EOL>if not line:<EOL><INDENT>break<EOL><DEDENT>if lineno == self.curframe.f_lineno:<EOL><INDENT>line = self.__format_line(tpl_line_em, filename, lineno, line, arrow = True)<EOL><DEDENT>else:<EOL><INDENT>line = self.__format_line(tpl_line, filename, lineno, line, arrow = False)<EOL><DEDENT>src.append(line)<EOL>self.lineno = lineno<EOL><DEDENT>print('<STR_LIT>'.join(src), file=io.stdout)<EOL><DEDENT>except KeyboardInterrupt:<EOL><INDENT>pass<EOL><DEDENT>

The printing (as opposed to the parsing part of a 'list'
        command.


namespaces = [('<STR_LIT>', self.curframe.f_locals),<EOL>('<STR_LIT>', self.curframe.f_globals)]<EOL>self.shell.find_line_magic('<STR_LIT>')(arg, namespaces=namespaces)<EOL>


namespaces = [('<STR_LIT>', self.curframe.f_locals),<EOL>('<STR_LIT>', self.curframe.f_globals)]<EOL>self.shell.find_line_magic('<STR_LIT>')("<STR_LIT>" % arg,<EOL>namespaces=namespaces)<EOL>


<EOL>line = linecache.getline(filename, lineno)<EOL>if not line:<EOL><INDENT>print('<STR_LIT>', file=self.stdout)<EOL>return <NUM_LIT:0><EOL><DEDENT>line = line.strip()<EOL>if (not line or (line[<NUM_LIT:0>] == '<STR_LIT:#>') or<EOL>(line[:<NUM_LIT:3>] == '<STR_LIT>') or line[:<NUM_LIT:3>] == "<STR_LIT>"):<EOL><INDENT>print('<STR_LIT>', file=self.stdout)<EOL>return <NUM_LIT:0><EOL><DEDENT>return lineno<EOL>

Check whether specified line seems to be executable.

        Return `lineno` if it is, 0 if not (e.g. a docstring, comment, blank
        line or EOF). Warning: testing is not comprehensive.