Esempio n. 1
0
def ipexec(fname, options=None):
    """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.
    """
    if options is None:
        options = []

    # For these subprocess calls, eliminate all prompt printing so we only see
    # output from script execution
    prompt_opts = [
        '--InteractiveShell.prompt_in1=""',
        '--InteractiveShell.prompt_in2=""',
        '--InteractiveShell.prompt_out=""',
    ]
    cmdargs = " ".join(default_argv() + prompt_opts + options)

    _ip = get_ipython()
    test_dir = os.path.dirname(__file__)

    ipython_cmd = find_cmd("ipython")
    # Absolute path for filename
    full_fname = os.path.join(test_dir, fname)
    full_cmd = "%s %s %s" % (ipython_cmd, cmdargs, full_fname)
    # print >> sys.stderr, 'FULL CMD:', full_cmd # dbg
    out = getoutputerror(full_cmd)
    # `import readline` causes 'ESC[?1034h' to be the first output sometimes,
    # so strip that off the front of the first line if it is found
    if out:
        first = out[0]
        m = re.match(r"\x1b\[[^h]+h", first)
        if m:
            # strip initial readline escape
            out = list(out)
            out[0] = first[len(m.group()) :]
            out = tuple(out)
    return out
Esempio n. 2
0
File: tools.py Progetto: g2p/ipython
def ipexec(fname, options=None):
    """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.
    """
    if options is None: options = []

    # For these subprocess calls, eliminate all prompt printing so we only see
    # output from script execution
    prompt_opts = [ '--PromptManager.in_template=""',
                    '--PromptManager.in2_template=""',
                    '--PromptManager.out_template=""'
    ]
    cmdargs = ' '.join(default_argv() + prompt_opts + options)

    _ip = get_ipython()
    test_dir = os.path.dirname(__file__)

    ipython_cmd = find_cmd('ipython3' if py3compat.PY3 else 'ipython')
    # Absolute path for filename
    full_fname = os.path.join(test_dir, fname)
    full_cmd = '%s %s %s' % (ipython_cmd, cmdargs, full_fname)
    #print >> sys.stderr, 'FULL CMD:', full_cmd # dbg
    out = getoutputerror(full_cmd)
    # `import readline` causes 'ESC[?1034h' to be the first output sometimes,
    # so strip that off the front of the first line if it is found
    if out:
        first = out[0]
        m = re.match(r'\x1b\[[^h]+h', first)
        if m:
            # strip initial readline escape
            out = list(out)
            out[0] = first[len(m.group()):]
            out = tuple(out)
    return out
Esempio n. 3
0
def ipexec(fname, options=None):
    """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.
    """
    if options is None: options = []

    # For these subprocess calls, eliminate all prompt printing so we only see
    # output from script execution
    prompt_opts = [ '--PromptManager.in_template=""',
                    '--PromptManager.in2_template=""',
                    '--PromptManager.out_template=""'
    ]
    cmdargs = ' '.join(default_argv() + prompt_opts + options)

    _ip = get_ipython()
    test_dir = os.path.dirname(__file__)

    ipython_cmd = find_cmd('ipython3' if py3compat.PY3 else 'ipython')
    # Absolute path for filename
    full_fname = os.path.join(test_dir, fname)
    full_cmd = '%s %s %s' % (ipython_cmd, cmdargs, full_fname)
    #print >> sys.stderr, 'FULL CMD:', full_cmd # dbg
    out, err = getoutputerror(full_cmd)
    # `import readline` causes 'ESC[?1034h' to be output sometimes,
    # so strip that out before doing comparisons
    if out:
        out = re.sub(r'\x1b\[[^h]+h', '', out)
    return out, err
Esempio n. 4
0
def ipexec(fname, options=None):
    """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.
    """
    if options is None: options = []
    
    # For these subprocess calls, eliminate all prompt printing so we only see
    # output from script execution
    prompt_opts = [ 'InteractiveShell.prompt_in1=""', 
                    'InteractiveShell.prompt_in2=""', 
                    'InteractiveShell.prompt_out=""'
    ]
    cmdargs = ' '.join(default_argv() + prompt_opts + options)
    
    _ip = get_ipython()
    test_dir = os.path.dirname(__file__)

    ipython_cmd = find_cmd('ipython3')
    # Absolute path for filename
    full_fname = os.path.join(test_dir, fname)
    full_cmd = '%s %s %s' % (ipython_cmd, cmdargs, full_fname)
    #print >> sys.stderr, 'FULL CMD:', full_cmd # dbg
    return getoutputerror(full_cmd)
Esempio n. 5
0
def ipexec(fname, options=None):
    """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.
    """
    if options is None: options = []

    # For these subprocess calls, eliminate all prompt printing so we only see
    # output from script execution
    prompt_opts = [
        'InteractiveShell.prompt_in1=""', 'InteractiveShell.prompt_in2=""',
        'InteractiveShell.prompt_out=""'
    ]
    cmdargs = ' '.join(default_argv() + prompt_opts + options)

    _ip = get_ipython()
    test_dir = os.path.dirname(__file__)

    ipython_cmd = find_cmd('ipython')
    # Absolute path for filename
    full_fname = os.path.join(test_dir, fname)
    full_cmd = '%s %s %s' % (ipython_cmd, cmdargs, full_fname)
    #print >> sys.stderr, 'FULL CMD:', full_cmd # dbg
    return getoutputerror(full_cmd)
Esempio n. 6
0
 def test_getoutput_error(self):
     out, err = getoutputerror('%s "%s"' % (python, self.fname))
     self.assertEqual(out, 'on stdout')
     self.assertEqual(err, 'on stderr')
 def test_getoutput(self):
     out, err = getoutputerror('python "%s"' % self.fname)
     self.assertEquals(out, 'on stdout')
     self.assertEquals(err, 'on stderr')
Esempio n. 8
0
 def test_getoutput_error(self):
     out, err = getoutputerror('%s "%s"' % (python, self.fname))
     self.assertEqual(out, 'on stdout')
     self.assertEqual(err, 'on stderr')
Esempio n. 9
0
 def test_getoutput(self):
     out, err = getoutputerror('python "%s"' % self.fname)
     self.assertEquals(out, 'on stdout')
     self.assertEquals(err, 'on stderr')
Esempio n. 10
0
 def test_getoutput_error(self):
     out, err = getoutputerror(f'{python} "{self.fname}"')
     self.assertEqual(out, 'on stdout')
     self.assertEqual(err, 'on stderr')