示例#1
0
    def winpopen4(orig, cmd, env=None, newlines=False, bufsize=-1):
        """Same as util.popen4, but manually creates an input pipe with a
        larger than default buffer"""
        import msvcrt

        if sys.version_info[0] < 3:
            import _subprocess

            handles = _subprocess.CreatePipe(None, pipei_bufsize)
            rfd, wfd = [msvcrt.open_osfhandle(h, 0) for h in handles]
        else:
            import _winapi

            handles = _winapi.CreatePipe(None, pipei_bufsize)
            rfd, wfd = [msvcrt.open_osfhandle(h, 0) for h in handles]
            handles = [subprocess.Handle(h) for h in handles]

        handles[0].Detach()
        handles[1].Detach()
        p = subprocess.Popen(
            cmd,
            shell=True,
            bufsize=bufsize,
            close_fds=False,
            stdin=rfd,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            universal_newlines=newlines,
            env=env,
        )
        p.stdin = util.fdopen(wfd, "wb", bufsize)
        return p.stdin, p.stdout, p.stderr, p
 def __init__(self, process_obj):
     prep_data = spawn.get_preparation_data(process_obj._name)
     rhandle, whandle = _winapi.CreatePipe(None, 0)
     wfd = msvcrt.open_osfhandle(whandle, 0)
     cmd = spawn.get_command_line(parent_pid=os.getpid(), pipe_handle=
         rhandle)
     cmd = ' '.join('"%s"' % x for x in cmd)
     with open(wfd, 'wb', closefd=True) as to_child:
         try:
             hp, ht, pid, tid = _winapi.CreateProcess(spawn.
                 get_executable(), cmd, None, None, False, 0, None, None,
                 None)
             _winapi.CloseHandle(ht)
         except:
             _winapi.CloseHandle(rhandle)
             raise
         self.pid = pid
         self.returncode = None
         self._handle = hp
         self.sentinel = int(hp)
         util.Finalize(self, _winapi.CloseHandle, (self.sentinel,))
         set_spawning_popen(self)
         try:
             reduction.dump(prep_data, to_child)
             reduction.dump(process_obj, to_child)
         finally:
             set_spawning_popen(None)
示例#3
0
    def __init__(self, process_obj):
        prep_data = spawn.get_preparation_data(process_obj._name)

        # read end of pipe will be duplicated by the child process
        # -- see spawn_main() in spawn.py.
        #
        # bpo-33929: Previously, the read end of pipe was "stolen" by the child
        # process, but it leaked a handle if the child process had been
        # terminated before it could steal the handle from the parent process.
        rhandle, whandle = _winapi.CreatePipe(None, 0)
        wfd = msvcrt.open_osfhandle(whandle, 0)
        cmd = spawn.get_command_line(parent_pid=os.getpid(),
                                     pipe_handle=rhandle)
        cmd = ' '.join('"%s"' % x for x in cmd)

        python_exe = spawn.get_executable()

        # bpo-35797: When running in a venv, we bypass the redirect
        # executor and launch our base Python.
        if WINENV and _path_eq(python_exe, sys.executable):
            python_exe = sys._base_executable
            env = os.environ.copy()
            env["__PYVENV_LAUNCHER__"] = sys.executable
        else:
            env = os.environ.copy()
        env['Y_PYTHON_ENTRY_POINT'] = ':main'

        with open(wfd, 'wb', closefd=True) as to_child:
            # start process
            try:
                hp, ht, pid, tid = _winapi.CreateProcess(
                    python_exe, cmd,
                    None, None, False, 0, env, None, None)
                _winapi.CloseHandle(ht)
            except:
                _winapi.CloseHandle(rhandle)
                raise

            # set attributes of self
            self.pid = pid
            self.returncode = None
            self._handle = hp
            self.sentinel = int(hp)
            self.finalizer = util.Finalize(self, _close_handles,
                                           (self.sentinel, int(rhandle)))

            # send information to child
            set_spawning_popen(self)
            try:
                reduction.dump(prep_data, to_child)
                reduction.dump(process_obj, to_child)
            finally:
                set_spawning_popen(None)
示例#4
0
 def _get_handles(self, stdin, stdout, stderr):
     if stdin is None and stdout is None and stderr is None:
         return (-1, -1, -1, -1, -1, -1)
     (p2cread, p2cwrite) = (-1, -1)
     (c2pread, c2pwrite) = (-1, -1)
     (errread, errwrite) = (-1, -1)
     if stdin is None:
         p2cread = _winapi.GetStdHandle(_winapi.STD_INPUT_HANDLE)
         (p2cread, _) = _winapi.CreatePipe(None, 0)
         p2cread = Handle(p2cread)
         _winapi.CloseHandle(_)
     elif stdin == PIPE:
         (p2cread, p2cwrite) = _winapi.CreatePipe(None, 0)
         (p2cread, p2cwrite) = (Handle(p2cread), Handle(p2cwrite))
     elif stdin == DEVNULL:
         p2cread = msvcrt.get_osfhandle(self._get_devnull())
     elif isinstance(stdin, int):
         p2cread = msvcrt.get_osfhandle(stdin)
     else:
         p2cread = msvcrt.get_osfhandle(stdin.fileno())
     p2cread = self._make_inheritable(p2cread)
     if stdout is None:
         c2pwrite = _winapi.GetStdHandle(_winapi.STD_OUTPUT_HANDLE)
         (_, c2pwrite) = _winapi.CreatePipe(None, 0)
         c2pwrite = Handle(c2pwrite)
         _winapi.CloseHandle(_)
     elif stdout == PIPE:
         (c2pread, c2pwrite) = _winapi.CreatePipe(None, 0)
         (c2pread, c2pwrite) = (Handle(c2pread), Handle(c2pwrite))
     elif stdout == DEVNULL:
         c2pwrite = msvcrt.get_osfhandle(self._get_devnull())
     elif isinstance(stdout, int):
         c2pwrite = msvcrt.get_osfhandle(stdout)
     else:
         c2pwrite = msvcrt.get_osfhandle(stdout.fileno())
     c2pwrite = self._make_inheritable(c2pwrite)
     if stderr is None:
         errwrite = _winapi.GetStdHandle(_winapi.STD_ERROR_HANDLE)
         (_, errwrite) = _winapi.CreatePipe(None, 0)
         errwrite = Handle(errwrite)
         _winapi.CloseHandle(_)
     elif stderr == PIPE:
         (errread, errwrite) = _winapi.CreatePipe(None, 0)
         (errread, errwrite) = (Handle(errread), Handle(errwrite))
     elif stderr == STDOUT:
         errwrite = c2pwrite
     elif stderr == DEVNULL:
         errwrite = msvcrt.get_osfhandle(self._get_devnull())
     elif isinstance(stderr, int):
         errwrite = msvcrt.get_osfhandle(stderr)
     else:
         errwrite = msvcrt.get_osfhandle(stderr.fileno())
     errwrite = self._make_inheritable(errwrite)
     return (p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite)
示例#5
0
    def __init__(self, process_obj):
        prep_data = spawn.get_preparation_data(process_obj._name)

        # read end of pipe will be duplicated by the child process
        # -- see spawn_main() in spawn.py.
        #
        # bpo-33929: Previously, the read end of pipe was "stolen" by the child
        # process, but it leaked a handle if the child process had been
        # terminated before it could steal the handle from the parent process.
        rhandle, whandle = _winapi.CreatePipe(None, 0)
        wfd = msvcrt.open_osfhandle(whandle, 0)
        cmd = spawn.get_command_line(parent_pid=os.getpid(),
                                     pipe_handle=rhandle)
        cmd = ' '.join('"%s"' % x for x in cmd)

        with open(wfd, 'wb', closefd=True) as to_child:
            # start process
            try:
                hp, ht, pid, tid = _winapi.CreateProcess(
                    spawn.get_executable(), cmd,
                    None, None, False, 0, None, None, None)
                _winapi.CloseHandle(ht)
            except:
                _winapi.CloseHandle(rhandle)
                raise

            # set attributes of self
            self.pid = pid
            self.returncode = None
            self._handle = hp
            self.sentinel = int(hp)
            self.finalizer = util.Finalize(self, _close_handles,
                                           (self.sentinel, int(rhandle)))

            # send information to child
            set_spawning_popen(self)
            try:
                reduction.dump(prep_data, to_child)
                reduction.dump(process_obj, to_child)
            finally:
                set_spawning_popen(None)
示例#6
0
    def __init__(self, process_obj):
        prep_data = spawn.get_preparation_data(process_obj._name)

        # read end of pipe will be "stolen" by the child process
        # -- see spawn_main() in spawn.py.
        rhandle, whandle = _winapi.CreatePipe(None, 0)
        wfd = msvcrt.open_osfhandle(whandle, 0)
        cmd = spawn.get_command_line(parent_pid=os.getpid(),
                                     pipe_handle=rhandle)
        cmd = ' '.join('"%s"' % x for x in cmd)

        with open(wfd, 'wb', closefd=True) as to_child:
            # start process
            try:
                hp, ht, pid, tid = _winapi.CreateProcess(
                    spawn.get_executable(), cmd, None, None, False, 0, None,
                    None, None)
                _winapi.CloseHandle(ht)
            except:
                _winapi.CloseHandle(rhandle)
                raise

            # set attributes of self
            self.pid = pid
            self.returncode = None
            self._handle = hp
            self.sentinel = int(hp)
            util.Finalize(self, _winapi.CloseHandle, (self.sentinel, ))

            # send information to child
            popen.set_spawning_popen(self)
            try:
                reduction.dump(prep_data, to_child)
                reduction.dump(process_obj, to_child)
            finally:
                popen.set_spawning_popen(None)
示例#7
0
        def _get_handles(self, stdin, stdout, stderr):
            """Construct and return tuple with IO objects:
            p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite
            """
            if stdin is None and stdout is None and stderr is None:
                return (-1, -1, -1, -1, -1, -1)

            p2cread, p2cwrite = -1, -1
            c2pread, c2pwrite = -1, -1
            errread, errwrite = -1, -1

            if stdin is None:
                p2cread = _winapi.GetStdHandle(_winapi.STD_INPUT_HANDLE)
                if p2cread is None:
                    p2cread, _ = _winapi.CreatePipe(None, 0)
                    p2cread = Handle(p2cread)
                    _winapi.CloseHandle(_)
            elif stdin == PIPE:
                p2cread, p2cwrite = _winapi.CreatePipe(None, 0)
                p2cread, p2cwrite = Handle(p2cread), Handle(p2cwrite)
            elif stdin == DEVNULL:
                p2cread = msvcrt.get_osfhandle(self._get_devnull())
            elif isinstance(stdin, int):
                p2cread = msvcrt.get_osfhandle(stdin)
            else:
                # Assuming file-like object
                p2cread = msvcrt.get_osfhandle(stdin.fileno())
            p2cread = self._make_inheritable(p2cread)

            if stdout is None:
                c2pwrite = _winapi.GetStdHandle(_winapi.STD_OUTPUT_HANDLE)
                if c2pwrite is None:
                    _, c2pwrite = _winapi.CreatePipe(None, 0)
                    c2pwrite = Handle(c2pwrite)
                    _winapi.CloseHandle(_)
            elif stdout == PIPE:
                c2pread, c2pwrite = _winapi.CreatePipe(None, 0)
                c2pread, c2pwrite = Handle(c2pread), Handle(c2pwrite)
            elif stdout == DEVNULL:
                c2pwrite = msvcrt.get_osfhandle(self._get_devnull())
            elif isinstance(stdout, int):
                c2pwrite = msvcrt.get_osfhandle(stdout)
            else:
                # Assuming file-like object
                c2pwrite = msvcrt.get_osfhandle(stdout.fileno())
            c2pwrite = self._make_inheritable(c2pwrite)

            if stderr is None:
                errwrite = _winapi.GetStdHandle(_winapi.STD_ERROR_HANDLE)
                if errwrite is None:
                    _, errwrite = _winapi.CreatePipe(None, 0)
                    errwrite = Handle(errwrite)
                    _winapi.CloseHandle(_)
            elif stderr == PIPE:
                errread, errwrite = _winapi.CreatePipe(None, 0)
                errread, errwrite = Handle(errread), Handle(errwrite)
            elif stderr == STDOUT:
                errwrite = c2pwrite
            elif stderr == DEVNULL:
                errwrite = msvcrt.get_osfhandle(self._get_devnull())
            elif isinstance(stderr, int):
                errwrite = msvcrt.get_osfhandle(stderr)
            else:
                # Assuming file-like object
                errwrite = msvcrt.get_osfhandle(stderr.fileno())
            errwrite = self._make_inheritable(errwrite)

            return (p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite)