# This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at http://mozilla.org/MPL/2.0/.
# A module to expose various thread/process/job related structures and # methods from kernel32 # # The MIT License # # Copyright (c) 2003-2004 by Peter Astrand <astrand@lysator.liu.se> # # Additions and modifications written by Benjamin Smedberg # <benjamin@smedbergs.us> are Copyright (c) 2006 by the Mozilla Foundation # <http://www.mozilla.org/> # # More Modifications # Copyright (c) 2006-2007 by Mike Taylor <bear@code-bear.com> # Copyright (c) 2007-2008 by Mikeal Rogers <mikeal@mozilla.com> # # By obtaining, using, and/or copying this software and/or its # associated documentation, you agree that you have read, understood, # and will comply with the following terms and conditions: # # Permission to use, copy, modify, and distribute this software and # its associated documentation for any purpose and without fee is # hereby granted, provided that the above copyright notice appears in # all copies, and that both that copyright notice and this permission # notice appear in supporting documentation, and that the name of the # author not be used in advertising or publicity pertaining to # distribution of the software without specific, written prior # permission. # # THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, # INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR # CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS # OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
import subprocess import sys from ctypes import (
POINTER,
WINFUNCTYPE,
Structure,
WinError,
c_ulong,
c_void_p,
cast,
create_unicode_buffer,
sizeof,
windll,
) from ctypes.wintypes import BOOL, BYTE, DWORD, HANDLE, LPCWSTR, LPWSTR, UINT, WORD
def ErrCheckBool(result, func, args): """errcheck function for Windows functions that return a BOOL True
on success""" ifnot result: raise WinError() return args
# AutoHANDLE
class AutoHANDLE(HANDLE): """Subclass of HANDLE which will call CloseHandle() on deletion."""
def Close(self): if self.value and self.value != HANDLE(-1).value:
self.CloseHandle(self)
self.value = 0
def __del__(self):
self.Close()
def __int__(self): return self.value
def ErrCheckHandle(result, func, args): """errcheck function for Windows functions that return a HANDLE.""" ifnot result: raise WinError() return AutoHANDLE(result)
class EnvironmentBlock: """An object which can be passed as the lpEnv parameter of CreateProcess.
It is initialized with a dictionary."""
def __init__(self, env): ifnot env:
self._as_parameter_ = None else:
values = []
fs_encoding = sys.getfilesystemencoding() or"mbcs" for k, v in env.items(): if isinstance(k, bytes):
k = k.decode(fs_encoding, "replace") if isinstance(v, bytes):
v = v.decode(fs_encoding, "replace")
values.append("{}={}".format(k, v))
# The lpEnvironment parameter of the 'CreateProcess' function expects a series # of null terminated strings followed by a final null terminator. We write this # value to a buffer and then cast it to LPCWSTR to avoid a Python ctypes bug # that probihits embedded null characters (https://bugs.python.org/issue32745).
values = create_unicode_buffer("\0".join(values) + "\0")
self._as_parameter_ = cast(values, LPCWSTR)
# Flags for IOCompletion ports (some of these would probably be defined if # we used the win32 extensions for python, but we don't want to do that if we # can help it.
INVALID_HANDLE_VALUE = HANDLE(-1) # From winbase.h
# Self Defined Constants for IOPort <--> Job Object communication
COMPKEY_TERMINATE = c_ulong(0)
COMPKEY_JOBOBJECT = c_ulong(1)
# GetQueuedCompletionPortStatus - # http://msdn.microsoft.com/en-us/library/aa364986%28v=vs.85%29.aspx
GetQueuedCompletionStatusProto = WINFUNCTYPE(
BOOL, # Return Type
HANDLE, # Completion Port
LPDWORD, # Msg ID
LPULONG, # Completion Key # PID Returned from the call (may be null)
LPULONG,
DWORD,
) # milliseconds to wait
GetQueuedCompletionStatusFlags = (
(1, "CompletionPort", INVALID_HANDLE_VALUE),
(1, "lpNumberOfBytes", None),
(1, "lpCompletionKey", None),
(1, "lpPID", None),
(1, "dwMilliseconds", 0),
)
GetQueuedCompletionStatus = GetQueuedCompletionStatusProto(
("GetQueuedCompletionStatus", windll.kernel32), GetQueuedCompletionStatusFlags
)
# CreateIOCompletionPort # Note that the completion key is just a number, not a pointer.
CreateIoCompletionPortProto = WINFUNCTYPE(
HANDLE, # Return Type
HANDLE, # File Handle
HANDLE, # Existing Completion Port
c_ulong, # Completion Key
DWORD,
) # Number of Threads
# SetInformationJobObject
SetInformationJobObjectProto = WINFUNCTYPE(
BOOL, # Return Type
HANDLE, # Job Handle
DWORD, # Type of Class next param is
LPVOID, # Job Object Class
DWORD,
) # Job Object Class Length
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.