Skip to content

Commit b3548dc

Browse files
committed
[Python] Cleanup PyChipError return values
Use PyChipError return value where PyChipErrors are actually returned. This then also allows us to use the typical .raise_on_error() pattern.
1 parent 10b82e2 commit b3548dc

File tree

5 files changed

+22
-40
lines changed

5 files changed

+22
-40
lines changed

src/controller/python/chip/ChipDeviceCtrl.py

+5-8
Original file line numberDiff line numberDiff line change
@@ -1013,12 +1013,8 @@ def GetRemoteSessionParameters(self, nodeid) -> typing.Optional[SessionParameter
10131013
sessionParametersStruct = SessionParametersStruct.parse(b'\x00' * SessionParametersStruct.sizeof())
10141014
sessionParametersByteArray = SessionParametersStruct.build(sessionParametersStruct)
10151015
device = self.GetConnectedDeviceSync(nodeid)
1016-
res = self._ChipStack.Call(lambda: self._dmLib.pychip_DeviceProxy_GetRemoteSessionParameters(
1017-
device.deviceProxy, ctypes.c_char_p(sessionParametersByteArray)))
1018-
1019-
# 0 is CHIP_NO_ERROR
1020-
if res != 0:
1021-
return None
1016+
self._ChipStack.Call(lambda: self._dmLib.pychip_DeviceProxy_GetRemoteSessionParameters(
1017+
device.deviceProxy, ctypes.c_char_p(sessionParametersByteArray))).raise_on_error()
10221018

10231019
sessionParametersStruct = SessionParametersStruct.parse(sessionParametersByteArray)
10241020
return SessionParameters(
@@ -1030,8 +1026,6 @@ def GetRemoteSessionParameters(self, nodeid) -> typing.Optional[SessionParameter
10301026
specficiationVersion=sessionParametersStruct.SpecificationVersion if sessionParametersStruct.SpecificationVersion != 0 else None,
10311027
maxPathsPerInvoke=sessionParametersStruct.MaxPathsPerInvoke)
10321028

1033-
return res
1034-
10351029
async def TestOnlySendBatchCommands(self, nodeid: int, commands: typing.List[ClusterCommand.InvokeRequestInfo],
10361030
timedRequestTimeoutMs: typing.Optional[int] = None,
10371031
interactionTimeoutMs: typing.Optional[int] = None, busyWaitMs: typing.Optional[int] = None,
@@ -1804,6 +1798,9 @@ def _InitLib(self):
18041798

18051799
self._dmLib.pychip_CheckInDelegate_SetOnCheckInCompleteCallback(_OnCheckInComplete)
18061800

1801+
self._dmLib.pychip_DeviceProxy_GetRemoteSessionParameters.restype = PyChipError
1802+
self._dmLib.pychip_DeviceProxy_GetRemoteSessionParameters.argtypes = [c_void_p, c_char_p]
1803+
18071804

18081805
class ChipDeviceController(ChipDeviceControllerBase):
18091806
''' The ChipDeviceCommissioner binding, named as ChipDeviceController

src/controller/python/chip/discovery/__init__.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,7 @@ def FindAddressAsync(fabricid: int, nodeid: int, callback, timeout_ms=1000):
236236
)
237237

238238
res = _GetDiscoveryLibraryHandle().pychip_discovery_resolve(fabricid, nodeid)
239-
if res != 0:
240-
raise Exception("Failed to start node resolution")
239+
res.raise_on_error()
241240

242241

243242
class _SyncAddressFinder:

src/controller/python/chip/interaction_model/delegate.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ def InitIMDelegate():
330330
setter.Set("pychip_InteractionModelDelegate_SetCommandResponseErrorCallback", None, [
331331
_OnCommandResponseFunct])
332332
setter.Set("pychip_InteractionModel_GetCommandSenderHandle",
333-
c_uint32, [ctypes.POINTER(c_uint64)])
333+
chip.native.PyChipError, [ctypes.POINTER(c_uint64)])
334334
setter.Set("pychip_InteractionModelDelegate_SetOnWriteResponseStatusCallback", None, [
335335
_OnWriteResponseStatusFunct])
336336

@@ -389,10 +389,8 @@ def WaitCommandIndexStatus(commandHandle: int, commandIndex: int):
389389
def GetCommandSenderHandle() -> int:
390390
handle = chip.native.GetLibraryHandle()
391391
resPointer = c_uint64()
392-
res = handle.pychip_InteractionModel_GetCommandSenderHandle(
393-
ctypes.pointer(resPointer))
394-
if res != 0:
395-
raise chip.exceptions.ChipStackError(res)
392+
handle.pychip_InteractionModel_GetCommandSenderHandle(
393+
ctypes.pointer(resPointer)).raise_on_error()
396394
ClearCommandStatus(resPointer.value)
397395
return resPointer.value
398396

src/controller/python/chip/native/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ def __init__(self, handle):
199199
def Set(self, methodName: str, resultType, argumentTypes: list):
200200
method = getattr(self.handle, methodName)
201201
method.restype = resultType
202-
method.argtype = argumentTypes
202+
method.argtypes = argumentTypes
203203

204204

205205
@dataclass

src/controller/python/chip/setup_payload/setup_payload.py

+12-24
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@
1414
# limitations under the License.
1515
#
1616

17-
from ctypes import CFUNCTYPE, c_char_p, c_int32, c_uint8, c_uint16, c_uint32
17+
from ctypes import CFUNCTYPE, c_char_p, c_uint8, c_uint16, c_uint32
1818
from typing import Optional
1919

20-
from chip.exceptions import ChipStackError
21-
from chip.native import GetLibraryHandle, NativeLibraryHandleMethodArguments
20+
from chip.native import GetLibraryHandle, NativeLibraryHandleMethodArguments, PyChipError
2221

2322

2423
class SetupPayload:
@@ -46,34 +45,23 @@ def AddVendorAttribute(tag, value):
4645

4746
def ParseQrCode(self, qrCode: str):
4847
self.Clear()
49-
err = self.chipLib.pychip_SetupPayload_ParseQrCode(qrCode.upper().encode(),
48+
self.chipLib.pychip_SetupPayload_ParseQrCode(qrCode.upper().encode(),
5049
self.attribute_visitor,
51-
self.vendor_attribute_visitor)
52-
53-
if err != 0:
54-
raise ChipStackError(err)
55-
56-
return self
50+
self.vendor_attribute_visitor).raise_on_error()
5751

5852
def ParseManualPairingCode(self, manualPairingCode: str):
5953
self.Clear()
60-
err = self.chipLib.pychip_SetupPayload_ParseManualPairingCode(manualPairingCode.encode(),
54+
self.chipLib.pychip_SetupPayload_ParseManualPairingCode(manualPairingCode.encode(),
6155
self.attribute_visitor,
62-
self.vendor_attribute_visitor)
63-
64-
if err != 0:
65-
raise ChipStackError(err)
56+
self.vendor_attribute_visitor).raise_on_error()
6657

6758
return self
6859

6960
# DEPRECATED
7061
def PrintOnboardingCodes(self, passcode, vendorId, productId, discriminator, customFlow, capabilities, version):
7162
self.Clear()
72-
err = self.chipLib.pychip_SetupPayload_PrintOnboardingCodes(
73-
passcode, vendorId, productId, discriminator, customFlow, capabilities, version)
74-
75-
if err != 0:
76-
raise ChipStackError(err)
63+
self.chipLib.pychip_SetupPayload_PrintOnboardingCodes(
64+
passcode, vendorId, productId, discriminator, customFlow, capabilities, version).raise_on_error()
7765

7866
# DEPRECATED
7967
def Print(self):
@@ -106,17 +94,17 @@ def __DecorateValue(self, name, value):
10694
return None
10795

10896
def __InitNativeFunctions(self, chipLib):
109-
if chipLib.pychip_SetupPayload_ParseQrCode is not None:
97+
if chipLib.pychip_SetupPayload_ParseQrCode.argtypes is not None:
11098
return
11199
setter = NativeLibraryHandleMethodArguments(chipLib)
112100
setter.Set("pychip_SetupPayload_ParseQrCode",
113-
c_int32,
101+
PyChipError,
114102
[c_char_p, SetupPayload.AttributeVisitor, SetupPayload.VendorAttributeVisitor])
115103
setter.Set("pychip_SetupPayload_ParseManualPairingCode",
116-
c_int32,
104+
PyChipError,
117105
[c_char_p, SetupPayload.AttributeVisitor, SetupPayload.VendorAttributeVisitor])
118106
setter.Set("pychip_SetupPayload_PrintOnboardingCodes",
119-
c_int32,
107+
PyChipError,
120108
[c_uint32, c_uint16, c_uint16, c_uint16, c_uint8, c_uint8, c_uint8])
121109

122110
# Getters from parsed contents.

0 commit comments

Comments
 (0)