Example #1
0
def _system_compat(shell, cmd, also_return_output=False):
    """Compatibility function for IPython's built-in system command.

  The system command has the following semantics:
    * No return value, and thus the "_" variable is not set
    * Sets the _exit_code variable to the return value of the called process
    * Unicode characters are preserved
    * If the process was interrupted, "^C" is printed.

  Args:
    shell: An InteractiveShell instance.
    cmd: Command to execute. This is the same as the corresponding argument to
      InteractiveShell.system_piped.
    also_return_output: if True, return any output from this function, along
      with printing it. Otherwise, print output and return None.
  Returns:
    LSString if also_return_output=True, else None.
  """
    # We set a higher depth than the IPython system command since a shell object
    # is expected to call this function, thus adding one level of nesting to the
    # stack.
    result = _run_command(shell.var_expand(cmd, depth=2),
                          clear_streamed_output=False)
    shell.user_ns['_exit_code'] = result.returncode
    if -result.returncode in _INTERRUPTED_SIGNALS:
        print('^C')

    if also_return_output:
        output = result.output
        if six.PY2:
            # Backwards compatibility. Python 2 getoutput() expects the result as a
            # str, not a unicode.
            output = output.encode(_ENCODING)
        return text.LSString(output)
Example #2
0
def _getoutput_compat(shell, cmd, split=True, depth=0):
    """Compatibility function for IPython's built-in getoutput command.

  The getoutput command has the following semantics:
    * Returns a SList containing an array of output
    * SList items are of type "str". In Python 2, the str object is utf-8
      encoded. In Python 3, the "str" type already supports Unicode.
    * The _exit_code attribute is not set
    * If the process was interrupted, "^C" is printed.

  Args:
    shell: An InteractiveShell instance.
    cmd: Command to execute. This is the same as the corresponding argument to
      InteractiveShell.getoutput.
    split: Same as the corresponding argument to InteractiveShell.getoutput.
    depth: Same as the corresponding argument to InteractiveShell.getoutput.
  Returns:
    The output as a SList if split was true, otherwise an LSString.
  """
    # We set a higher depth than the IPython system command since a shell object
    # is expected to call this function, thus adding one level of nesting to the
    # stack.
    result = _run_command(shell.var_expand(cmd, depth=depth + 2),
                          clear_streamed_output=True)
    if -result.returncode in _INTERRUPTED_SIGNALS:
        print('^C')

    output = result.output
    if six.PY2:
        # Backwards compatibility. Python 2 getoutput() expects the result as a
        # str, not a unicode.
        output = output.encode(_ENCODING)

    if split:
        return text.SList(output.splitlines())
    else:
        return text.LSString(output)
Example #3
0
def test_LSString():
    lss = text.LSString("abc\ndef")
    nt.assert_equal(lss.l, ['abc', 'def'])
    nt.assert_equal(lss.s, 'abc def')
    lss = text.LSString(os.getcwd())
    nt.assert_is_instance(lss.p[0], Path)
Example #4
0
def test_LSString():
    lss = text.LSString("abc\ndef")
    nt.assert_equal(lss.l, ["abc", "def"])
    nt.assert_equal(lss.s, "abc def")
    lss = text.LSString(os.getcwd())
    nt.assert_is_instance(lss.p[0], Path)
Example #5
0
def test_LSString():
    lss = text.LSString("abc\ndef")
    assert lss.l == ["abc", "def"]
    assert lss.s == "abc def"
    lss = text.LSString(os.getcwd())
    assert isinstance(lss.p[0], Path)
Example #6
0
def test_LSString():
    lss = text.LSString("abc\ndef")
    nt.assert_equal(lss.l, ['abc', 'def'])
    nt.assert_equal(lss.s, 'abc def')