def run_execve(program, args=None, env=None, do_path_lookup=False): if args is None: args = [program] else: args = [program] + args if env is None: env = {} # we cannot directly call execve() because it replaces the # current process. fd_read, fd_write = os.pipe() childpid = os.fork() if childpid == 0: # in the child os.close(fd_read) os.dup2(fd_write, 1) # stdout os.close(fd_write) if do_path_lookup: os.execvp(program, args) else: rposix.execve(program, args, env) assert 0, "should not arrive here" else: # in the parent os.close(fd_write) child_stdout = [] while True: data = os.read(fd_read, 4096) if not data: break # closed child_stdout.append(data) pid, status = os.waitpid(childpid, 0) os.close(fd_read) return status, ''.join(child_stdout)