mirror of
https://github.com/hakril/PythonForWindows
synced 2026-06-08 14:31:45 +00:00
ComInterface now expose a callback for ctypes error handling. Allow COM interface to change/handle the raised Exception
This commit is contained in:
@@ -4,13 +4,38 @@ import ctypes
|
||||
|
||||
generate_IID = IID.from_raw
|
||||
|
||||
|
||||
class COMHRESULT(HRESULT):
|
||||
_type_ = HRESULT._type_
|
||||
def _check_retval_(self):
|
||||
# We CAN NOT try to adapt the self.value and transform it with flags
|
||||
# here, we need to do it with the errcheck
|
||||
# So we have the peer-interface callback system on errcheck :)
|
||||
return self.value # The value will be send to errcheck :)
|
||||
|
||||
class COMInterface(ctypes.c_void_p):
|
||||
_functions_ = {
|
||||
}
|
||||
|
||||
# So COMInterface completely bypass the HRESULT
|
||||
# return value check on restype by setting the restype to COMHRESULT
|
||||
# But we add the 'errcheck' callbakc capacity for all COMInterface and subclasses
|
||||
# So the default implem of the callbakc must have the same behavior as
|
||||
# standard HRESULT restype.
|
||||
# This is why default errcheck callback call ctypes._check_HRESULT
|
||||
def _default_errcheck(self, result, func, args):
|
||||
ctypes._check_HRESULT(result)
|
||||
return args
|
||||
|
||||
def __getattr__(self, name):
|
||||
if name in self._functions_:
|
||||
return functools.partial(self._functions_[name], self)
|
||||
winfunc = self._functions_[name]
|
||||
# Hacking the HRESULT _check_retval_ and
|
||||
# letting COMInterface.errcheck do the work of validating / raising
|
||||
winfunc.restype = COMHRESULT
|
||||
effective_errcheck = getattr(self, "errcheck", self._default_errcheck)
|
||||
winfunc.errcheck = effective_errcheck
|
||||
return functools.partial(winfunc, self)
|
||||
return super(COMInterface, self).__getattribute__(name)
|
||||
|
||||
def __repr__(self):
|
||||
@@ -23,3 +48,11 @@ class COMInterface(ctypes.c_void_p):
|
||||
self.QueryInterface(interface.IID, interface)
|
||||
return interface
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -25,12 +25,7 @@ class WinproxyError(WindowsError):
|
||||
|
||||
|
||||
# winproxy Error check
|
||||
|
||||
# Try None instead :')
|
||||
def no_error_check(func_name, result, func, args):
|
||||
"""No error check"""
|
||||
return args
|
||||
|
||||
no_error_check = None
|
||||
|
||||
def fail_on_minus_one(func_name, result, func, args):
|
||||
"""Raise WinproxyError if call result is -1"""
|
||||
|
||||
Reference in New Issue
Block a user