environment/lib/python2.7/site-packages/IPython/lib/irunner.py
def run_source(self,source,interact=False,get_output=False):
        """Run the given source code interactively.

        Inputs:

          - source: a string of code to be executed, or an open file object we
          can iterate over.

        Optional inputs:

          - interact(False): if true, start to interact with the running
          program at the end of the script.  Otherwise, just exit.

          - get_output(False): if true, capture the output of the child process
          (filtering the input commands out) and return it as a string.

        Returns:
          A string containing the process output, but only if requested.
          """

        # if the source is a string, chop it up in lines so we can iterate
        # over it just as if it were an open file.
        if isinstance(source, basestring):
            source = source.splitlines(True)

        if self.echo:
            # normalize all strings we write to use the native OS line
            # separators.
            linesep  = os.linesep
            stdwrite = self.out.write
            write    = lambda s: stdwrite(s.replace('\r\n',linesep))
        else:
            # Quiet mode, all writes are no-ops
            write = lambda s: None

        c = self.child
        prompts = c.compile_pattern_list(self.prompts)
        prompt_idx = c.expect_list(prompts)

        # Flag whether the script ends normally or not, to know whether we can
        # do anything further with the underlying process.
        end_normal = True

        # If the output was requested, store it in a list for return at the end
        if get_output:
            output = []
            store_output = output.append

        for cmd in source:
            # skip blank lines for all matches to the 'main' prompt, while the
            # secondary prompts do not
            if prompt_idx==0 and \
                   (cmd.isspace() or cmd.lstrip().startswith('#')):
                write(cmd)
                continue

            # write('AFTER: '+c.after)  # dbg
            write(c.after)
            c.send(cmd)
            try:
                prompt_idx = c.expect_list(prompts)
            except pexpect.EOF:
                # this will happen if the child dies unexpectedly
                write(c.before)
                end_normal = False
                break

            write(c.before)

            # With an echoing process, the output we get in c.before contains
            # the command sent, a newline, and then the actual process output
            if get_output:
                store_output(c.before[len(cmd+'\n'):])
                #write('CMD: <<%s>>' % cmd)  # dbg
                #write('OUTPUT: <<%s>>' % output[-1])  # dbg

        self.out.flush()
        if end_normal:
            if interact:
                c.send('\n')
                print '<< Starting interactive mode >>',
                try:
                    c.interact()
                except OSError:
                    # This is what fires when the child stops.  Simply print a
                    # newline so the system prompt is aligned.  The extra
                    # space is there to make sure it gets printed, otherwise
                    # OS buffering sometimes just suppresses it.
                    write(' \n')
                    self.out.flush()
        else:
            if interact:
                e="Further interaction is not possible: child process is dead."
                print >> sys.stderr, e

        # Leave the child ready for more input later on, otherwise select just
        # hangs on the second invocation.
        if c.isalive():
            c.send('\n')

        # Return any requested output
        if get_output:
            return ''.join(output)
def run_source(self,source,interact=False,get_output=False):
        """Run the given source code interactively.

        Inputs:

          - source: a string of code to be executed, or an open file object we
          can iterate over.

        Optional inputs:

          - interact(False): if true, start to interact with the running
          program at the end of the script.  Otherwise, just exit.

          - get_output(False): if true, capture the output of the child process
          (filtering the input commands out) and return it as a string.

        Returns:
          A string containing the process output, but only if requested.
          """

        # if the source is a string, chop it up in lines so we can iterate
        # over it just as if it were an open file.
        if isinstance(source, basestring):
            source = source.splitlines(True)

        if self.echo:
            # normalize all strings we write to use the native OS line
            # separators.
            linesep  = os.linesep
            stdwrite = self.out.write
            write    = lambda s: stdwrite(s.replace('\r\n',linesep))
        else:
            # Quiet mode, all writes are no-ops
            write = lambda s: None

        c = self.child
        prompts = c.compile_pattern_list(self.prompts)
        prompt_idx = c.expect_list(prompts)

        # Flag whether the script ends normally or not, to know whether we can
        # do anything further with the underlying process.
        end_normal = True

        # If the output was requested, store it in a list for return at the end
        if get_output:
            output = []
            store_output = output.append

        for cmd in source:
            # skip blank lines for all matches to the 'main' prompt, while the
            # secondary prompts do not
            if prompt_idx==0 and \
                   (cmd.isspace() or cmd.lstrip().startswith('#')):
                write(cmd)
                continue

            # write('AFTER: '+c.after)  # dbg
            write(c.after)
            c.send(cmd)
            try:
                prompt_idx = c.expect_list(prompts)
            except pexpect.EOF:
                # this will happen if the child dies unexpectedly
                write(c.before)
                end_normal = False
                break

            write(c.before)

            # With an echoing process, the output we get in c.before contains
            # the command sent, a newline, and then the actual process output
            if get_output:
                store_output(c.before[len(cmd+'\n'):])
                #write('CMD: <<%s>>' % cmd)  # dbg
                #write('OUTPUT: <<%s>>' % output[-1])  # dbg

        self.out.flush()
        if end_normal:
            if interact:
                c.send('\n')
                print '<< Starting interactive mode >>',
                try:
                    c.interact()
                except OSError:
                    # This is what fires when the child stops.  Simply print a
                    # newline so the system prompt is aligned.  The extra
                    # space is there to make sure it gets printed, otherwise
                    # OS buffering sometimes just suppresses it.
                    write(' \n')
                    self.out.flush()
        else:
            if interact:
                e="Further interaction is not possible: child process is dead."
                print >> sys.stderr, e

        # Leave the child ready for more input later on, otherwise select just
        # hangs on the second invocation.
        if c.isalive():
            c.send('\n')

        # Return any requested output
        if get_output:
            return ''.join(output)
Run the given source code interactively.

        Inputs:

          - source: a string of code to be executed, or an open file object we
          can iterate over.

        Optional inputs:

          - interact(False): if true, start to interact with the running
          program at the end of the script.  Otherwise, just exit.

          - get_output(False): if true, capture the output of the child process
          (filtering the input commands out) and return it as a string.

        Returns:
          A string containing the process output, but only if requested.