def set_close_exec(fd):
success = SetHandleInformation(fd, HANDLE_FLAG_INHERIT, 0)
if not success:
raise ctypes.GetLastError()
python类GetLastError()的实例源码
def set_close_exec(fd):
success = SetHandleInformation(fd, HANDLE_FLAG_INHERIT, 0)
if not success:
raise ctypes.GetLastError()
def set_close_exec(fd):
success = SetHandleInformation(fd, HANDLE_FLAG_INHERIT, 0)
if not success:
raise ctypes.GetLastError()
def set_close_exec(fd):
success = SetHandleInformation(fd, HANDLE_FLAG_INHERIT, 0)
if not success:
raise ctypes.GetLastError()
def set_close_exec(fd):
success = SetHandleInformation(fd, HANDLE_FLAG_INHERIT, 0)
if not success:
raise ctypes.GetLastError()
def WinError(code=None, descr=None):
if code is None:
code = ctypes.GetLastError()
if descr is None:
descr = ctypes.FormatError(code).strip()
return WindowsError(code, descr)
def set_close_exec(fd):
success = SetHandleInformation(fd, HANDLE_FLAG_INHERIT, 0)
if not success:
raise ctypes.GetLastError()
def pipe_non_blocking_is_error_blocking(ex):
if not isinstance(ex, PortableBlockingIOError):
return False
from ctypes import GetLastError
ERROR_NO_DATA = 232
return (GetLastError() == ERROR_NO_DATA)
def set_close_exec(fd):
success = SetHandleInformation(fd, HANDLE_FLAG_INHERIT, 0)
if not success:
raise ctypes.GetLastError()
def set_close_exec(fd):
success = SetHandleInformation(fd, HANDLE_FLAG_INHERIT, 0)
if not success:
raise ctypes.GetLastError()
def set_close_exec(fd):
success = SetHandleInformation(fd, HANDLE_FLAG_INHERIT, 0)
if not success:
raise ctypes.GetLastError()
def set_close_exec(fd):
success = SetHandleInformation(fd, HANDLE_FLAG_INHERIT, 0)
if not success:
raise ctypes.GetLastError()
def _bool_error_check(result, func, args):
if not result:
return GetLastError()
return 0
def get_last_error():
return ctypes.GetLastError()
def __init__(self, winerr=None):
if not winerr:
winerr = ctypes.GetLastError()
super().__init__(0, ctypes.FormatError(winerr), None, winerr)
def get_window_text(hwnd):
"""
Wrapper over the GetWindowTextW() Win32 function
Raises a OSError exception on error.
"""
length = ctypes.windll.user32.GetWindowTextLengthW(hwnd)
buff = ctypes.create_unicode_buffer(length + 1)
ctypes.windll.kernel32.SetLastError(0)
res = ctypes.windll.user32.GetWindowTextW(hwnd, buff, length + 1)
if not res and ctypes.GetLastError() != 0:
raise ctypes.WinError()
return buff.value
def process__get_executable_filename(thread_pid):
# This only finds processes, not services.
GetProcessImageFileName = windll.kernel32.K32GetProcessImageFileNameW
GetProcessImageFileName.restype = wintypes.DWORD
OpenProcess = windll.kernel32.OpenProcess
# OpenProcess.restype = wintypes.HANDLE
hproc = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, False, thread_pid)
if hproc is None:
raise WinError()
try:
filename = create_unicode_buffer(MAX_FILENAME_LENGTH + 1)
# For some reason, this isn't found in Windows 10.
res = GetProcessImageFileName(hproc, byref(filename), MAX_FILENAME_LENGTH + 1)
if res <= 0:
# raise ctypes.WinError()
return None
return str(filename.value[:res])
finally:
windll.kernel32.CloseHandle(hproc)
# def shell__open_start_menu():
# # In Windows 7, the start button is part of the desktop?
# desktop_hwnd = windll.user32.GetDesktopWindow()
#
# # FIXME DEBUG
# print("DEBUG desktop_hwnd: {0}".format(desktop_hwnd))
#
# # Find the "start" button on the tray window
# start_hwnd = windll.user32.FindWindowExW(desktop_hwnd, None, 'Button', 'Start')
# if (start_hwnd is None or start_hwnd == 0) and GetLastError() != 0:
# start_hwnd = windll.user32.FindWindowExW(desktop_hwnd, None, None, 'Start')
# if (start_hwnd is None or start_hwnd == 0) and GetLastError() != 0:
# # start_hwnd = windll.user32.FindWindowExW(desktop_hwnd, None, 'Button', None)
# if start_hwnd is None or start_hwnd == 0:
# raise WinError()
# print("Start button: {0}".format(start_hwnd))
#
# # Send a click message to the button
# res = windll.user32.SendMessageW(start_hwnd, BM_CLICK, 0, 0)
# if res == 0:
# raise WinError()
def window__set_style(hwnd, style_update):
"""
Update a window's style. "style_update" is a dictionary of style
keys that map to either True or False, depending on how the style
should be modified. Style values not specified in the update
will not be changed.
:param hwnd:
:param style_update:
:return: the original style values (usable as input to this function).
"""
assert isinstance(style_update, dict)
SetWindowLongW = windll.user32.SetWindowLongW
SetLastError = windll.kernel32.SetLastError
original_style = window__get_style(hwnd)
expected_style = dict(original_style)
std_style_update = False
std_bits = 0
for k, mask in WS_STYLE_BIT_MAP.items():
if k in style_update and original_style[k] != style_update[k]:
std_style_update = True
expected_style[k] = style_update[k]
if style_update[k]:
std_bits |= mask
elif original_style[k]:
std_bits |= mask
if std_style_update:
SetLastError(0)
res = SetWindowLongW(hwnd, GWL_STYLE, std_bits)
if res == 0 or GetLastError() != 0:
raise WinError()
ex_style_update = False
ex_bits = 0
for k, mask in WS_EX_STYLE_BIT_MAP.items():
if k in style_update and original_style[k] != style_update[k]:
ex_style_update = True
expected_style[k] = style_update[k]
if style_update[k]:
ex_bits |= mask
elif original_style[k]:
ex_bits |= mask
if ex_style_update:
SetLastError(0)
res = SetWindowLongW(hwnd, GWL_EXSTYLE, ex_bits)
if res == 0 or GetLastError() != 0:
raise WinError()
# Sometimes, it only changed some of the values.
# Double check.
final_style = window__get_style(hwnd)
if expected_style != final_style:
# raise OSError("Did not fully set style to {0}; found {1}".format(
# expected_style, final_style))
# print("Did not fully set style to {0}; found {1}".format(
# expected_style, final_style))
pass
return original_style
def _scandir_python(path=unicode('.')):
"""Like os.listdir(), but yield DirEntry objects instead of returning
a list of names.
"""
# Call FindFirstFile and handle errors
if isinstance(path, bytes):
is_bytes = True
filename = join(path.decode('mbcs', 'strict'), '*.*')
else:
is_bytes = False
filename = join(path, '*.*')
data = wintypes.WIN32_FIND_DATAW()
data_p = ctypes.byref(data)
handle = FindFirstFile(filename, data_p)
if handle == INVALID_HANDLE_VALUE:
error = ctypes.GetLastError()
if error == ERROR_FILE_NOT_FOUND:
# No files, don't yield anything
return
raise win_error(error, path)
# Call FindNextFile in a loop, stopping when no more files
try:
while True:
# Skip '.' and '..' (current and parent directory), but
# otherwise yield (filename, stat_result) tuple
name = data.cFileName
if name not in ('.', '..'):
if is_bytes:
name = name.encode('mbcs', 'replace')
yield Win32DirEntryPython(path, name, data)
data = wintypes.WIN32_FIND_DATAW()
data_p = ctypes.byref(data)
success = FindNextFile(handle, data_p)
if not success:
error = ctypes.GetLastError()
if error == ERROR_NO_MORE_FILES:
break
raise win_error(error, path)
finally:
if not FindClose(handle):
raise win_error(ctypes.GetLastError(), path)