我们从Python开源项目中,提取了以下35个代码示例,用于说明如何使用msvcrt.open_osfhandle()。
def main(): ''' Run code specifed by data received over pipe ''' assert is_forking(sys.argv) handle = int(sys.argv[-1]) fd = msvcrt.open_osfhandle(handle, os.O_RDONLY) from_parent = os.fdopen(fd, 'rb') process.current_process()._inheriting = True preparation_data = load(from_parent) prepare(preparation_data) self = load(from_parent) process.current_process()._inheriting = False from_parent.close() exitcode = self._bootstrap() exit(exitcode)
def main(): ''' Run code specified by data received over pipe ''' assert is_forking(sys.argv) handle = int(sys.argv[-1]) fd = msvcrt.open_osfhandle(handle, os.O_RDONLY) from_parent = os.fdopen(fd, 'rb') process.current_process()._inheriting = True preparation_data = load(from_parent) prepare(preparation_data) self = load(from_parent) process.current_process()._inheriting = False from_parent.close() exitcode = self._bootstrap() exit(exitcode)
def _winapi_childhandle_after_createprocess_child(self): """Called on Windows in the child process after the CreateProcess() system call. This is required for making the handle usable in the child. """ if WINAPI_HANDLE_TRANSFER_STEAL: # In this case the handle has not been inherited by the child # process during CreateProcess(). Steal it from the parent. new_winapihandle = multiprocessing.reduction.steal_handle( self._parent_pid, self._parent_winapihandle) del self._parent_winapihandle del self._parent_pid # Restore C file descriptor with (read/write)only flag. self._fd = msvcrt.open_osfhandle(new_winapihandle, self._fd_flag) return # In this case the handle has been inherited by the child process during # the CreateProcess() system call. Get C file descriptor from Windows # file handle. self._fd = msvcrt.open_osfhandle( self._inheritable_winapihandle, self._fd_flag) del self._inheritable_winapihandle
def __init__(self, output=sys.stdout): self._close_output = False # If output is integer, assume it is file descriptor and open it if isinstance(output, int): self._close_output = True if sys.platform == 'win32': output = msvcrt.open_osfhandle(output, 0) output = open(output, 'wb') # Get underlying buffered file object try: self.output = output.buffer except AttributeError: self.output = output # Use only one writer thread to preserve sequence of written frequencies self._executor = threadpool.ThreadPoolExecutor( max_workers=1, max_queue_size=100, thread_name_prefix='Writer_thread' )
def spawn_main(pipe_handle, parent_pid=None, tracker_fd=None): ''' Run code specifed by data received over pipe ''' assert is_forking(sys.argv) if sys.platform == 'win32': import msvcrt from .reduction import steal_handle new_handle = steal_handle(parent_pid, pipe_handle) fd = msvcrt.open_osfhandle(new_handle, os.O_RDONLY) else: from . import semaphore_tracker semaphore_tracker._semaphore_tracker._fd = tracker_fd fd = pipe_handle exitcode = _main(fd) sys.exit(exitcode)
def create_file_from_handle(handle, mode="r"): """Return a Python :class:`file` around a ``Windows`` HANDLE""" fd = msvcrt.open_osfhandle(handle, os.O_TEXT) return os.fdopen(fd, mode, 0)
def _winapi_childhandle_after_createprocess_parent(self): """Called on Windows in the parent process after the CreateProcess() system call. This method is intended to revert the actions performed within `_winapi_childhandle_prepare_transfer()`. In particular, this method is intended to prepare a subsequent call to the handle's `close()` method. """ if WINAPI_HANDLE_TRANSFER_STEAL: del self._parent_winapihandle del self._parent_pid # Setting `_fd` to None prevents the subsequent `close()` method # invocation (triggered in `start_process()` after child creation) # from actually calling `os.close()` on the file descriptor. This # must be prevented because at this point the handle either already # is or will be "stolen" by the child via a direct WinAPI call using # the DUPLICATE_CLOSE_SOURCE option (and therefore become # auto-closed, here, in the parent). The relative timing is not # predictable. If the child process steals first, os.close() here # would result in `OSError: [Errno 9] Bad file descriptor`. If # os.close() is called on the handle in the parent before the child # can steal the handle, a `OSError: [WinError 6] The handle is # invalid` will be thrown in the child upon the stealing attempt. self._fd = None return # Get C file descriptor from Windows file handle. self._fd = msvcrt.open_osfhandle( self._inheritable_winapihandle, self._fd_flag) del self._inheritable_winapihandle
def _writefd(cls, conn, data, create_dummy_fds=False): if create_dummy_fds: for i in range(0, 256): if not cls._is_fd_assigned(i): os.dup2(conn.fileno(), i) fd = reduction.recv_handle(conn) if msvcrt: fd = msvcrt.open_osfhandle(fd, os.O_WRONLY) os.write(fd, data) os.close(fd)
def from_subprocess(cls, arg): arg = int(arg) if MS_WINDOWS: fd = msvcrt.open_osfhandle(arg, os.O_WRONLY) else: fd = arg return cls(fd)
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 context.set_spawning_popen(self) try: reduction.dump(prep_data, to_child) reduction.dump(process_obj, to_child) finally: context.set_spawning_popen(None)
def File2FileObject(pipe, mode): """Make a C stdio file object out of a win32 file handle""" if mode.find('r') >= 0: wmode = os.O_RDONLY elif mode.find('w') >= 0: wmode = os.O_WRONLY if mode.find('b') >= 0: wmode = wmode | os.O_BINARY if mode.find('t') >= 0: wmode = wmode | os.O_TEXT return os.fdopen(msvcrt.open_osfhandle(pipe.Detach(),wmode),mode)
def run(self): """Set up input/output streams and execute the child function in a new thread. This is part of the `threading.Thread` interface and should not be called directly. """ if self.f is None: return if self.stdin is not None: sp_stdin = io.TextIOWrapper(self.stdin) else: sp_stdin = io.StringIO("") if ON_WINDOWS: if self.c2pwrite != -1: self.c2pwrite = msvcrt.open_osfhandle(self.c2pwrite.Detach(), 0) if self.errwrite != -1: self.errwrite = msvcrt.open_osfhandle(self.errwrite.Detach(), 0) if self.c2pwrite != -1: sp_stdout = io.TextIOWrapper(io.open(self.c2pwrite, 'wb', -1)) else: sp_stdout = sys.stdout if self.errwrite == self.c2pwrite: sp_stderr = sp_stdout elif self.errwrite != -1: sp_stderr = io.TextIOWrapper(io.open(self.errwrite, 'wb', -1)) else: sp_stderr = sys.stderr r = self.f(self.args, sp_stdin, sp_stdout, sp_stderr) self.returncode = 0 if r is None else r
def start(self, program = None, interpreter = None, arguments = None, universal_newlines = None, **kw): """ Starts a program or script for the test environment. The specified program will have the original directory prepended unless it is enclosed in a [list]. """ cmd = self.command_args(program, interpreter, arguments) cmd_string = string.join(map(self.escape, cmd), ' ') if self.verbose: sys.stderr.write(cmd_string + "\n") if universal_newlines is None: universal_newlines = self.universal_newlines # On Windows, if we make stdin a pipe when we plan to send # no input, and the test program exits before # Popen calls msvcrt.open_osfhandle, that call will fail. # So don't use a pipe for stdin if we don't need one. stdin = kw.get('stdin', None) if stdin is not None: stdin = subprocess.PIPE combine = kw.get('combine', self.combine) if combine: stderr_value = subprocess.STDOUT else: stderr_value = subprocess.PIPE return Popen(cmd, stdin=stdin, stdout=subprocess.PIPE, stderr=stderr_value, universal_newlines=universal_newlines)
def __init__(self, handle=None): if not handle: handle = WSASocket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP) self._file_handle = handle self._file_no = open_osfhandle(self._file_handle, 0)
def __init__(self, args, stdin=None, stdout=None, stderr=None, **kwargs): self.stdin = self.stdout = self.stderr = None stdin_rh = stdin_wh = stdout_rh = stdout_wh = stderr_rh = stderr_wh = None if stdin == subprocess.PIPE: stdin_rh, stdin_wh = pipe() stdin_rfd = msvcrt.open_osfhandle(stdin_rh.Detach(), os.O_RDONLY) self.stdin_rh = stdin_rh else: stdin_rfd = stdin self.stdin_rh = None if stdout == subprocess.PIPE: stdout_rh, stdout_wh = pipe() stdout_wfd = msvcrt.open_osfhandle(stdout_wh, 0) else: stdout_wfd = stdout if stderr == subprocess.PIPE: stderr_rh, stderr_wh = pipe() stderr_wfd = msvcrt.open_osfhandle(stderr_wh, 0) elif stderr == subprocess.STDOUT: stderr_wfd = stdout_wfd else: stderr_wfd = stderr try: super(Popen, self).__init__(args, stdin=stdin_rfd, stdout=stdout_wfd, stderr=stderr_wfd, **kwargs) except: for handle in (stdin_rh, stdin_wh, stdout_rh, stdout_wh, stderr_rh, stderr_wh): if handle is not None: win32file.CloseHandle(handle) raise else: if stdin_wh is not None: self.stdin = AsyncFile(stdin_wh, mode='w') if stdout_rh is not None: self.stdout = AsyncFile(stdout_rh, mode='r') if stderr_rh is not None: self.stderr = AsyncFile(stderr_rh, mode='r') finally: if stdin == subprocess.PIPE: os.close(stdin_rfd) if stdout == subprocess.PIPE: os.close(stdout_wfd) if stderr == subprocess.PIPE: os.close(stderr_wfd)
def __init__(self, args, stdin=None, stdout=None, stderr=None, **kwds): assert not kwds.get('universal_newlines') assert kwds.get('bufsize', 0) == 0 stdin_rfd = stdout_wfd = stderr_wfd = None stdin_wh = stdout_rh = stderr_rh = None if stdin == PIPE: stdin_rh, stdin_wh = pipe(overlapped=(False, True), duplex=True) stdin_rfd = msvcrt.open_osfhandle(stdin_rh, os.O_RDONLY) else: stdin_rfd = stdin if stdout == PIPE: stdout_rh, stdout_wh = pipe(overlapped=(True, False)) stdout_wfd = msvcrt.open_osfhandle(stdout_wh, 0) else: stdout_wfd = stdout if stderr == PIPE: stderr_rh, stderr_wh = pipe(overlapped=(True, False)) stderr_wfd = msvcrt.open_osfhandle(stderr_wh, 0) elif stderr == STDOUT: stderr_wfd = stdout_wfd else: stderr_wfd = stderr try: super().__init__(args, stdin=stdin_rfd, stdout=stdout_wfd, stderr=stderr_wfd, **kwds) except: for h in (stdin_wh, stdout_rh, stderr_rh): if h is not None: _winapi.CloseHandle(h) raise else: if stdin_wh is not None: self.stdin = PipeHandle(stdin_wh) if stdout_rh is not None: self.stdout = PipeHandle(stdout_rh) if stderr_rh is not None: self.stderr = PipeHandle(stderr_rh) finally: if stdin == PIPE: os.close(stdin_rfd) if stdout == PIPE: os.close(stdout_wfd) if stderr == PIPE: os.close(stderr_wfd)