ComInterface now expose a callback for ctypes error handling. Allow COM interface to change/handle the raised Exception

This commit is contained in:
hakril
2018-11-24 21:42:07 +01:00
parent b167c6e70f
commit cf6c813002
2 changed files with 35 additions and 7 deletions
+34 -1
View File
@@ -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
+1 -6
View File
@@ -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"""