MCPcopy Index your code
hub / github.com/ipython/ipython / getoutput

Method getoutput

IPython/core/interactiveshell.py:2750–2793  ·  view source on GitHub ↗

Get output (possibly including stderr) from a subprocess. Parameters ---------- cmd : str Command to execute (can not end in '&', as background processes are not supported. split : bool, optional If True, split the output into an I

(self, cmd, split=True, depth=0)

Source from the content-addressed store, hash-verified

2748 system = system_piped
2749
2750 def getoutput(self, cmd, split=True, depth=0):
2751 """Get output (possibly including stderr) from a subprocess.
2752
2753 Parameters
2754 ----------
2755 cmd : str
2756 Command to execute (can not end in '&', as background processes are
2757 not supported.
2758 split : bool, optional
2759 If True, split the output into an IPython SList. Otherwise, an
2760 IPython LSString is returned. These are objects similar to normal
2761 lists and strings, with a few convenience attributes for easier
2762 manipulation of line-based output. You can use '?' on them for
2763 details.
2764 depth : int, optional
2765 How many frames above the caller are the local variables which should
2766 be expanded in the command string? The default (0) assumes that the
2767 expansion variables are in the stack frame calling this function.
2768 """
2769 if cmd.rstrip().endswith('&'):
2770 # this is *far* from a rigorous test
2771 raise OSError("Background processes not supported.")
2772
2773 # Get output and exit code
2774 expanded_cmd = self.var_expand(cmd, depth=depth+1)
2775 if self.system_raise_on_error:
2776 # Use get_output_error_code to get the exit code
2777 out_str, err_str, exit_code = get_output_error_code(expanded_cmd)
2778 # Combine stdout and stderr as getoutput does
2779 out_combined = out_str if not err_str else out_str + err_str
2780 self.user_ns['_exit_code'] = exit_code
2781
2782 # Raise an exception if the command failed
2783 if exit_code != 0:
2784 raise CalledProcessError(exit_code, cmd)
2785 else:
2786 # Use the original getoutput for backward compatibility
2787 out_combined = getoutput(expanded_cmd)
2788
2789 if split:
2790 out = SList(out_combined.splitlines())
2791 else:
2792 out = LSString(out_combined)
2793 return out
2794
2795 #-------------------------------------------------------------------------
2796 # Things related to aliases

Callers 2

scMethod · 0.45
sxMethod · 0.45

Calls 5

var_expandMethod · 0.95
get_output_error_codeFunction · 0.90
getoutputFunction · 0.90
SListClass · 0.90
LSStringClass · 0.90

Tested by

no test coverage detected