Auto merge of #22882 - alexcrichton:stabilize-process, r=aturon

This commits blanket marks the API of the `std::process` module as `#[stable]`.
The module's API is very similar to the old `std::old_io::process` API and has
generally had quite a bit of time to bake both before and after the new module
landed.
This commit is contained in:
bors 2015-03-02 23:18:36 +00:00
commit b4c965ee80

View File

@ -10,7 +10,7 @@
//! Working with processes. //! Working with processes.
#![unstable(feature = "process", reason = "recently added via RFC 579")] #![stable(feature = "process", since = "1.0.0")]
#![allow(non_upper_case_globals)] #![allow(non_upper_case_globals)]
use prelude::v1::*; use prelude::v1::*;
@ -48,6 +48,7 @@ use thread;
/// let contents = output.stdout; /// let contents = output.stdout;
/// assert!(output.status.success()); /// assert!(output.status.success());
/// ``` /// ```
#[stable(feature = "process", since = "1.0.0")]
pub struct Child { pub struct Child {
handle: ProcessImp, handle: ProcessImp,
@ -55,20 +56,25 @@ pub struct Child {
status: Option<ExitStatusImp>, status: Option<ExitStatusImp>,
/// The handle for writing to the child's stdin, if it has been captured /// The handle for writing to the child's stdin, if it has been captured
#[stable(feature = "process", since = "1.0.0")]
pub stdin: Option<ChildStdin>, pub stdin: Option<ChildStdin>,
/// The handle for reading from the child's stdout, if it has been captured /// The handle for reading from the child's stdout, if it has been captured
#[stable(feature = "process", since = "1.0.0")]
pub stdout: Option<ChildStdout>, pub stdout: Option<ChildStdout>,
/// The handle for reading from the child's stderr, if it has been captured /// The handle for reading from the child's stderr, if it has been captured
#[stable(feature = "process", since = "1.0.0")]
pub stderr: Option<ChildStderr>, pub stderr: Option<ChildStderr>,
} }
/// A handle to a child procesess's stdin /// A handle to a child procesess's stdin
#[stable(feature = "process", since = "1.0.0")]
pub struct ChildStdin { pub struct ChildStdin {
inner: AnonPipe inner: AnonPipe
} }
#[stable(feature = "process", since = "1.0.0")]
impl Write for ChildStdin { impl Write for ChildStdin {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> { fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.inner.write(buf) self.inner.write(buf)
@ -80,10 +86,12 @@ impl Write for ChildStdin {
} }
/// A handle to a child procesess's stdout /// A handle to a child procesess's stdout
#[stable(feature = "process", since = "1.0.0")]
pub struct ChildStdout { pub struct ChildStdout {
inner: AnonPipe inner: AnonPipe
} }
#[stable(feature = "process", since = "1.0.0")]
impl Read for ChildStdout { impl Read for ChildStdout {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.inner.read(buf) self.inner.read(buf)
@ -91,10 +99,12 @@ impl Read for ChildStdout {
} }
/// A handle to a child procesess's stderr /// A handle to a child procesess's stderr
#[stable(feature = "process", since = "1.0.0")]
pub struct ChildStderr { pub struct ChildStderr {
inner: AnonPipe inner: AnonPipe
} }
#[stable(feature = "process", since = "1.0.0")]
impl Read for ChildStderr { impl Read for ChildStderr {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.inner.read(buf) self.inner.read(buf)
@ -108,8 +118,6 @@ impl Read for ChildStderr {
/// to be changed (for example, by adding arguments) prior to spawning: /// to be changed (for example, by adding arguments) prior to spawning:
/// ///
/// ``` /// ```
/// # #![feature(process)]
///
/// use std::process::Command; /// use std::process::Command;
/// ///
/// let output = Command::new("sh").arg("-c").arg("echo hello").output().unwrap_or_else(|e| { /// let output = Command::new("sh").arg("-c").arg("echo hello").output().unwrap_or_else(|e| {
@ -117,6 +125,7 @@ impl Read for ChildStderr {
/// }); /// });
/// let hello = output.stdout; /// let hello = output.stdout;
/// ``` /// ```
#[stable(feature = "process", since = "1.0.0")]
pub struct Command { pub struct Command {
inner: CommandImp, inner: CommandImp,
@ -137,6 +146,7 @@ impl Command {
/// ///
/// Builder methods are provided to change these defaults and /// Builder methods are provided to change these defaults and
/// otherwise configure the process. /// otherwise configure the process.
#[stable(feature = "process", since = "1.0.0")]
pub fn new<S: AsOsStr + ?Sized>(program: &S) -> Command { pub fn new<S: AsOsStr + ?Sized>(program: &S) -> Command {
Command { Command {
inner: CommandImp::new(program.as_os_str()), inner: CommandImp::new(program.as_os_str()),
@ -147,12 +157,14 @@ impl Command {
} }
/// Add an argument to pass to the program. /// Add an argument to pass to the program.
#[stable(feature = "process", since = "1.0.0")]
pub fn arg<S: AsOsStr + ?Sized>(&mut self, arg: &S) -> &mut Command { pub fn arg<S: AsOsStr + ?Sized>(&mut self, arg: &S) -> &mut Command {
self.inner.arg(arg.as_os_str()); self.inner.arg(arg.as_os_str());
self self
} }
/// Add multiple arguments to pass to the program. /// Add multiple arguments to pass to the program.
#[stable(feature = "process", since = "1.0.0")]
pub fn args<S: AsOsStr>(&mut self, args: &[S]) -> &mut Command { pub fn args<S: AsOsStr>(&mut self, args: &[S]) -> &mut Command {
self.inner.args(args.iter().map(AsOsStr::as_os_str)); self.inner.args(args.iter().map(AsOsStr::as_os_str));
self self
@ -162,26 +174,30 @@ impl Command {
/// ///
/// Note that environment variable names are case-insensitive (but case-preserving) on Windows, /// Note that environment variable names are case-insensitive (but case-preserving) on Windows,
/// and case-sensitive on all other platforms. /// and case-sensitive on all other platforms.
pub fn env<S: ?Sized, T: ?Sized>(&mut self, key: &S, val: &T) -> &mut Command where #[stable(feature = "process", since = "1.0.0")]
S: AsOsStr, T: AsOsStr pub fn env<K: ?Sized, V: ?Sized>(&mut self, key: &K, val: &V) -> &mut Command
where K: AsOsStr, V: AsOsStr
{ {
self.inner.env(key.as_os_str(), val.as_os_str()); self.inner.env(key.as_os_str(), val.as_os_str());
self self
} }
/// Removes an environment variable mapping. /// Removes an environment variable mapping.
pub fn env_remove<S: ?Sized + AsOsStr>(&mut self, key: &S) -> &mut Command { #[stable(feature = "process", since = "1.0.0")]
pub fn env_remove<K: ?Sized + AsOsStr>(&mut self, key: &K) -> &mut Command {
self.inner.env_remove(key.as_os_str()); self.inner.env_remove(key.as_os_str());
self self
} }
/// Clears the entire environment map for the child process. /// Clears the entire environment map for the child process.
#[stable(feature = "process", since = "1.0.0")]
pub fn env_clear(&mut self) -> &mut Command { pub fn env_clear(&mut self) -> &mut Command {
self.inner.env_clear(); self.inner.env_clear();
self self
} }
/// Set the working directory for the child process. /// Set the working directory for the child process.
#[stable(feature = "process", since = "1.0.0")]
pub fn current_dir<P: AsPath + ?Sized>(&mut self, dir: &P) -> &mut Command { pub fn current_dir<P: AsPath + ?Sized>(&mut self, dir: &P) -> &mut Command {
self.inner.cwd(dir.as_path().as_os_str()); self.inner.cwd(dir.as_path().as_os_str());
self self
@ -189,6 +205,7 @@ impl Command {
/// Configuration for the child process's stdin handle (file descriptor 0). /// Configuration for the child process's stdin handle (file descriptor 0).
/// Defaults to `CreatePipe(true, false)` so the input can be written to. /// Defaults to `CreatePipe(true, false)` so the input can be written to.
#[stable(feature = "process", since = "1.0.0")]
pub fn stdin(&mut self, cfg: Stdio) -> &mut Command { pub fn stdin(&mut self, cfg: Stdio) -> &mut Command {
self.stdin = Some(cfg.0); self.stdin = Some(cfg.0);
self self
@ -196,6 +213,7 @@ impl Command {
/// Configuration for the child process's stdout handle (file descriptor 1). /// Configuration for the child process's stdout handle (file descriptor 1).
/// Defaults to `CreatePipe(false, true)` so the output can be collected. /// Defaults to `CreatePipe(false, true)` so the output can be collected.
#[stable(feature = "process", since = "1.0.0")]
pub fn stdout(&mut self, cfg: Stdio) -> &mut Command { pub fn stdout(&mut self, cfg: Stdio) -> &mut Command {
self.stdout = Some(cfg.0); self.stdout = Some(cfg.0);
self self
@ -203,6 +221,7 @@ impl Command {
/// Configuration for the child process's stderr handle (file descriptor 2). /// Configuration for the child process's stderr handle (file descriptor 2).
/// Defaults to `CreatePipe(false, true)` so the output can be collected. /// Defaults to `CreatePipe(false, true)` so the output can be collected.
#[stable(feature = "process", since = "1.0.0")]
pub fn stderr(&mut self, cfg: Stdio) -> &mut Command { pub fn stderr(&mut self, cfg: Stdio) -> &mut Command {
self.stderr = Some(cfg.0); self.stderr = Some(cfg.0);
self self
@ -234,6 +253,7 @@ impl Command {
/// Executes the command as a child process, returning a handle to it. /// Executes the command as a child process, returning a handle to it.
/// ///
/// By default, stdin, stdout and stderr are inherited by the parent. /// By default, stdin, stdout and stderr are inherited by the parent.
#[stable(feature = "process", since = "1.0.0")]
pub fn spawn(&mut self) -> io::Result<Child> { pub fn spawn(&mut self) -> io::Result<Child> {
self.spawn_inner(StdioImp::Inherit) self.spawn_inner(StdioImp::Inherit)
} }
@ -258,8 +278,9 @@ impl Command {
/// println!("stdout: {}", String::from_utf8_lossy(output.stdout.as_slice())); /// println!("stdout: {}", String::from_utf8_lossy(output.stdout.as_slice()));
/// println!("stderr: {}", String::from_utf8_lossy(output.stderr.as_slice())); /// println!("stderr: {}", String::from_utf8_lossy(output.stderr.as_slice()));
/// ``` /// ```
#[stable(feature = "process", since = "1.0.0")]
pub fn output(&mut self) -> io::Result<Output> { pub fn output(&mut self) -> io::Result<Output> {
self.spawn_inner(StdioImp::Capture).and_then(|p| p.wait_with_output()) self.spawn_inner(StdioImp::Piped).and_then(|p| p.wait_with_output())
} }
/// Executes a command as a child process, waiting for it to finish and /// Executes a command as a child process, waiting for it to finish and
@ -279,6 +300,7 @@ impl Command {
/// ///
/// println!("process exited with: {}", status); /// println!("process exited with: {}", status);
/// ``` /// ```
#[stable(feature = "process", since = "1.0.0")]
pub fn status(&mut self) -> io::Result<ExitStatus> { pub fn status(&mut self) -> io::Result<ExitStatus> {
self.spawn().and_then(|mut p| p.wait()) self.spawn().and_then(|mut p| p.wait())
} }
@ -317,7 +339,7 @@ fn setup_io(io: &StdioImp, fd: libc::c_int, readable: bool)
Inherit => { Inherit => {
(Some(AnonPipe::from_fd(fd)), None) (Some(AnonPipe::from_fd(fd)), None)
} }
Capture => { Piped => {
let (reader, writer) = try!(unsafe { pipe2::anon_pipe() }); let (reader, writer) = try!(unsafe { pipe2::anon_pipe() });
if readable { if readable {
(Some(reader), Some(writer)) (Some(reader), Some(writer))
@ -330,45 +352,60 @@ fn setup_io(io: &StdioImp, fd: libc::c_int, readable: bool)
/// The output of a finished process. /// The output of a finished process.
#[derive(PartialEq, Eq, Clone)] #[derive(PartialEq, Eq, Clone)]
#[stable(feature = "process", since = "1.0.0")]
pub struct Output { pub struct Output {
/// The status (exit code) of the process. /// The status (exit code) of the process.
#[stable(feature = "process", since = "1.0.0")]
pub status: ExitStatus, pub status: ExitStatus,
/// The data that the process wrote to stdout. /// The data that the process wrote to stdout.
#[stable(feature = "process", since = "1.0.0")]
pub stdout: Vec<u8>, pub stdout: Vec<u8>,
/// The data that the process wrote to stderr. /// The data that the process wrote to stderr.
#[stable(feature = "process", since = "1.0.0")]
pub stderr: Vec<u8>, pub stderr: Vec<u8>,
} }
/// Describes what to do with a standard io stream for a child process. /// Describes what to do with a standard io stream for a child process.
#[stable(feature = "process", since = "1.0.0")]
pub struct Stdio(StdioImp); pub struct Stdio(StdioImp);
// The internal enum for stdio setup; see below for descriptions. // The internal enum for stdio setup; see below for descriptions.
#[derive(Clone)] #[derive(Clone)]
enum StdioImp { enum StdioImp {
Capture, Piped,
Inherit, Inherit,
Null, Null,
} }
impl Stdio { impl Stdio {
/// A new pipe should be arranged to connect the parent and child processes. /// A new pipe should be arranged to connect the parent and child processes.
pub fn capture() -> Stdio { Stdio(StdioImp::Capture) } #[unstable(feature = "process_capture")]
#[deprecated(since = "1.0.0", reason = "renamed to `Stdio::piped`")]
pub fn capture() -> Stdio { Stdio::piped() }
/// A new pipe should be arranged to connect the parent and child processes.
#[stable(feature = "process", since = "1.0.0")]
pub fn piped() -> Stdio { Stdio(StdioImp::Piped) }
/// The child inherits from the corresponding parent descriptor. /// The child inherits from the corresponding parent descriptor.
#[stable(feature = "process", since = "1.0.0")]
pub fn inherit() -> Stdio { Stdio(StdioImp::Inherit) } pub fn inherit() -> Stdio { Stdio(StdioImp::Inherit) }
/// This stream will be ignored. This is the equivalent of attaching the /// This stream will be ignored. This is the equivalent of attaching the
/// stream to `/dev/null` /// stream to `/dev/null`
#[stable(feature = "process", since = "1.0.0")]
pub fn null() -> Stdio { Stdio(StdioImp::Null) } pub fn null() -> Stdio { Stdio(StdioImp::Null) }
} }
/// Describes the result of a process after it has terminated. /// Describes the result of a process after it has terminated.
#[derive(PartialEq, Eq, Clone, Copy, Debug)] #[derive(PartialEq, Eq, Clone, Copy, Debug)]
#[stable(feature = "process", since = "1.0.0")]
pub struct ExitStatus(ExitStatusImp); pub struct ExitStatus(ExitStatusImp);
impl ExitStatus { impl ExitStatus {
/// Was termination successful? Signal termination not considered a success, /// Was termination successful? Signal termination not considered a success,
/// and success is defined as a zero exit status. /// and success is defined as a zero exit status.
#[stable(feature = "process", since = "1.0.0")]
pub fn success(&self) -> bool { pub fn success(&self) -> bool {
self.0.success() self.0.success()
} }
@ -378,6 +415,7 @@ impl ExitStatus {
/// On Unix, this will return `None` if the process was terminated /// On Unix, this will return `None` if the process was terminated
/// by a signal; `std::os::unix` provides an extension trait for /// by a signal; `std::os::unix` provides an extension trait for
/// extracting the signal and other details from the `ExitStatus`. /// extracting the signal and other details from the `ExitStatus`.
#[stable(feature = "process", since = "1.0.0")]
pub fn code(&self) -> Option<i32> { pub fn code(&self) -> Option<i32> {
self.0.code() self.0.code()
} }
@ -387,6 +425,7 @@ impl AsInner<ExitStatusImp> for ExitStatus {
fn as_inner(&self) -> &ExitStatusImp { &self.0 } fn as_inner(&self) -> &ExitStatusImp { &self.0 }
} }
#[stable(feature = "process", since = "1.0.0")]
impl fmt::Display for ExitStatus { impl fmt::Display for ExitStatus {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f) self.0.fmt(f)
@ -396,6 +435,7 @@ impl fmt::Display for ExitStatus {
impl Child { impl Child {
/// Forces the child to exit. This is equivalent to sending a /// Forces the child to exit. This is equivalent to sending a
/// SIGKILL on unix platforms. /// SIGKILL on unix platforms.
#[stable(feature = "process", since = "1.0.0")]
pub fn kill(&mut self) -> io::Result<()> { pub fn kill(&mut self) -> io::Result<()> {
#[cfg(unix)] fn collect_status(p: &mut Child) { #[cfg(unix)] fn collect_status(p: &mut Child) {
// On Linux (and possibly other unices), a process that has exited will // On Linux (and possibly other unices), a process that has exited will
@ -436,6 +476,7 @@ impl Child {
/// before waiting. This helps avoid deadlock: it ensures that the /// before waiting. This helps avoid deadlock: it ensures that the
/// child does not block waiting for input from the parent, while /// child does not block waiting for input from the parent, while
/// the parent waits for the child to exit. /// the parent waits for the child to exit.
#[stable(feature = "process", since = "1.0.0")]
pub fn wait(&mut self) -> io::Result<ExitStatus> { pub fn wait(&mut self) -> io::Result<ExitStatus> {
drop(self.stdin.take()); drop(self.stdin.take());
match self.status { match self.status {
@ -456,6 +497,7 @@ impl Child {
/// before waiting. This helps avoid deadlock: it ensures that the /// before waiting. This helps avoid deadlock: it ensures that the
/// child does not block waiting for input from the parent, while /// child does not block waiting for input from the parent, while
/// the parent waits for the child to exit. /// the parent waits for the child to exit.
#[stable(feature = "process", since = "1.0.0")]
pub fn wait_with_output(mut self) -> io::Result<Output> { pub fn wait_with_output(mut self) -> io::Result<Output> {
drop(self.stdin.take()); drop(self.stdin.take());
fn read<T: Read + Send + 'static>(stream: Option<T>) -> Receiver<io::Result<Vec<u8>>> { fn read<T: Read + Send + 'static>(stream: Option<T>) -> Receiver<io::Result<Vec<u8>>> {
@ -557,7 +599,7 @@ mod tests {
#[test] #[test]
fn stdout_works() { fn stdout_works() {
let mut cmd = Command::new("echo"); let mut cmd = Command::new("echo");
cmd.arg("foobar").stdout(Stdio::capture()); cmd.arg("foobar").stdout(Stdio::piped());
assert_eq!(run_output(cmd), "foobar\n"); assert_eq!(run_output(cmd), "foobar\n");
} }
@ -567,7 +609,7 @@ mod tests {
let mut cmd = Command::new("/bin/sh"); let mut cmd = Command::new("/bin/sh");
cmd.arg("-c").arg("pwd") cmd.arg("-c").arg("pwd")
.current_dir("/") .current_dir("/")
.stdout(Stdio::capture()); .stdout(Stdio::piped());
assert_eq!(run_output(cmd), "/\n"); assert_eq!(run_output(cmd), "/\n");
} }
@ -576,8 +618,8 @@ mod tests {
fn stdin_works() { fn stdin_works() {
let mut p = Command::new("/bin/sh") let mut p = Command::new("/bin/sh")
.arg("-c").arg("read line; echo $line") .arg("-c").arg("read line; echo $line")
.stdin(Stdio::capture()) .stdin(Stdio::piped())
.stdout(Stdio::capture()) .stdout(Stdio::piped())
.spawn().unwrap(); .spawn().unwrap();
p.stdin.as_mut().unwrap().write("foobar".as_bytes()).unwrap(); p.stdin.as_mut().unwrap().write("foobar".as_bytes()).unwrap();
drop(p.stdin.take()); drop(p.stdin.take());
@ -675,7 +717,7 @@ mod tests {
#[cfg(not(target_os="android"))] #[cfg(not(target_os="android"))]
#[test] #[test]
fn test_wait_with_output_once() { fn test_wait_with_output_once() {
let prog = Command::new("echo").arg("hello").stdout(Stdio::capture()) let prog = Command::new("echo").arg("hello").stdout(Stdio::piped())
.spawn().unwrap(); .spawn().unwrap();
let Output {status, stdout, stderr} = prog.wait_with_output().unwrap(); let Output {status, stdout, stderr} = prog.wait_with_output().unwrap();
let output_str = str::from_utf8(stdout.as_slice()).unwrap(); let output_str = str::from_utf8(stdout.as_slice()).unwrap();