Skip to content

Commit

Permalink
[Python] Cleanup PyChipError return values
Browse files Browse the repository at this point in the history
Use PyChipError return value where PyChipErrors are actually returned.
This then also allows us to use the typical .raise_on_error() pattern.
  • Loading branch information
agners committed Jun 17, 2024
1 parent a336e9b commit eac5a83
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 36 deletions.
11 changes: 5 additions & 6 deletions src/controller/python/chip/ChipDeviceCtrl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1013,12 +1013,8 @@ def GetRemoteSessionParameters(self, nodeid) -> typing.Optional[SessionParameter
sessionParametersStruct = SessionParametersStruct.parse(b'\x00' * SessionParametersStruct.sizeof())
sessionParametersByteArray = SessionParametersStruct.build(sessionParametersStruct)
device = self.GetConnectedDeviceSync(nodeid)
res = self._ChipStack.Call(lambda: self._dmLib.pychip_DeviceProxy_GetRemoteSessionParameters(
device.deviceProxy, ctypes.c_char_p(sessionParametersByteArray)))

# 0 is CHIP_NO_ERROR
if res != 0:
return None
self._ChipStack.Call(lambda: self._dmLib.pychip_DeviceProxy_GetRemoteSessionParameters(
device.deviceProxy, ctypes.c_char_p(sessionParametersByteArray))).raise_on_error()

sessionParametersStruct = SessionParametersStruct.parse(sessionParametersByteArray)
return SessionParameters(
Expand Down Expand Up @@ -1804,6 +1800,9 @@ def _InitLib(self):

self._dmLib.pychip_CheckInDelegate_SetOnCheckInCompleteCallback(_OnCheckInComplete)

self._dmLib.pychip_DeviceProxy_GetRemoteSessionParameters.restype = PyChipError
self._dmLib.pychip_DeviceProxy_GetRemoteSessionParameters.argtypes = [c_void_p, c_char_p]


class ChipDeviceController(ChipDeviceControllerBase):
''' The ChipDeviceCommissioner binding, named as ChipDeviceController
Expand Down
3 changes: 1 addition & 2 deletions src/controller/python/chip/discovery/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,7 @@ def FindAddressAsync(fabricid: int, nodeid: int, callback, timeout_ms=1000):
)

res = _GetDiscoveryLibraryHandle().pychip_discovery_resolve(fabricid, nodeid)
if res != 0:
raise Exception("Failed to start node resolution")
res.raise_on_error()


class _SyncAddressFinder:
Expand Down
8 changes: 3 additions & 5 deletions src/controller/python/chip/interaction_model/delegate.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ def InitIMDelegate():
setter.Set("pychip_InteractionModelDelegate_SetCommandResponseErrorCallback", None, [
_OnCommandResponseFunct])
setter.Set("pychip_InteractionModel_GetCommandSenderHandle",
c_uint32, [ctypes.POINTER(c_uint64)])
chip.native.PyChipError, [ctypes.POINTER(c_uint64)])
setter.Set("pychip_InteractionModelDelegate_SetOnWriteResponseStatusCallback", None, [
_OnWriteResponseStatusFunct])

Expand Down Expand Up @@ -389,10 +389,8 @@ def WaitCommandIndexStatus(commandHandle: int, commandIndex: int):
def GetCommandSenderHandle() -> int:
handle = chip.native.GetLibraryHandle()
resPointer = c_uint64()
res = handle.pychip_InteractionModel_GetCommandSenderHandle(
ctypes.pointer(resPointer))
if res != 0:
raise chip.exceptions.ChipStackError(res)
handle.pychip_InteractionModel_GetCommandSenderHandle(
ctypes.pointer(resPointer)).raise_on_error()
ClearCommandStatus(resPointer.value)
return resPointer.value

Expand Down
2 changes: 1 addition & 1 deletion src/controller/python/chip/native/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def __init__(self, handle):
def Set(self, methodName: str, resultType, argumentTypes: list):
method = getattr(self.handle, methodName)
method.restype = resultType
method.argtype = argumentTypes
method.argtypes = argumentTypes


@dataclass
Expand Down
33 changes: 11 additions & 22 deletions src/controller/python/chip/setup_payload/setup_payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from typing import Optional

from chip.exceptions import ChipStackError
from chip.native import GetLibraryHandle, NativeLibraryHandleMethodArguments
from chip.native import GetLibraryHandle, NativeLibraryHandleMethodArguments, PyChipError


class SetupPayload:
Expand Down Expand Up @@ -46,34 +46,23 @@ def AddVendorAttribute(tag, value):

def ParseQrCode(self, qrCode: str):
self.Clear()
err = self.chipLib.pychip_SetupPayload_ParseQrCode(qrCode.upper().encode(),
self.chipLib.pychip_SetupPayload_ParseQrCode(qrCode.upper().encode(),
self.attribute_visitor,
self.vendor_attribute_visitor)

if err != 0:
raise ChipStackError(err)

return self
self.vendor_attribute_visitor).raise_on_error()

def ParseManualPairingCode(self, manualPairingCode: str):
self.Clear()
err = self.chipLib.pychip_SetupPayload_ParseManualPairingCode(manualPairingCode.encode(),
self.chipLib.pychip_SetupPayload_ParseManualPairingCode(manualPairingCode.encode(),
self.attribute_visitor,
self.vendor_attribute_visitor)

if err != 0:
raise ChipStackError(err)
self.vendor_attribute_visitor).raise_on_error()

return self

# DEPRECATED
def PrintOnboardingCodes(self, passcode, vendorId, productId, discriminator, customFlow, capabilities, version):
self.Clear()
err = self.chipLib.pychip_SetupPayload_PrintOnboardingCodes(
passcode, vendorId, productId, discriminator, customFlow, capabilities, version)

if err != 0:
raise ChipStackError(err)
self.chipLib.pychip_SetupPayload_PrintOnboardingCodes(
passcode, vendorId, productId, discriminator, customFlow, capabilities, version).raise_on_error()

# DEPRECATED
def Print(self):
Expand Down Expand Up @@ -106,17 +95,17 @@ def __DecorateValue(self, name, value):
return None

def __InitNativeFunctions(self, chipLib):
if chipLib.pychip_SetupPayload_ParseQrCode is not None:
if chipLib.pychip_SetupPayload_ParseQrCode.argtypes is not None:
return
setter = NativeLibraryHandleMethodArguments(chipLib)
setter.Set("pychip_SetupPayload_ParseQrCode",
c_int32,
PyChipError,
[c_char_p, SetupPayload.AttributeVisitor, SetupPayload.VendorAttributeVisitor])
setter.Set("pychip_SetupPayload_ParseManualPairingCode",
c_int32,
PyChipError,
[c_char_p, SetupPayload.AttributeVisitor, SetupPayload.VendorAttributeVisitor])
setter.Set("pychip_SetupPayload_PrintOnboardingCodes",
c_int32,
PyChipError,
[c_uint32, c_uint16, c_uint16, c_uint16, c_uint8, c_uint8, c_uint8])

# Getters from parsed contents.
Expand Down

0 comments on commit eac5a83

Please sign in to comment.