From 8ef37a60cb4f724e453a5b4806a99409184d9484 Mon Sep 17 00:00:00 2001 From: Song GUO Date: Wed, 20 Oct 2021 14:07:00 +0800 Subject: [PATCH] [python] Add unsigned int support to CHIP TLV Reader / Writer in Python (#10639) * Add tlv.sint and tlv.uint to distinguish signed and unsigned int when encoding * Use Pythonic NewType * Revert and add a note --- .github/workflows/build.yaml | 5 + src/controller/python/chip/tlv/__init__.py | 18 ++- .../python/test/unit_tests/test_tlv.py | 153 ++++++++++++++++++ 3 files changed, 172 insertions(+), 4 deletions(-) create mode 100644 src/controller/python/test/unit_tests/test_tlv.py diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index bf0c6697bcb19c..d3146cae769ec5 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -231,6 +231,11 @@ jobs: scripts/build/gn_build.sh scripts/tests/gn_tests.sh done + - name: Run Python library specific unit tests + timeout-minutes: 5 + run: | + scripts/run_in_build_env.sh 'pip3 install ./out/controller/python/chip-0.0-cp37-abi3-linux_x86_64.whl' + scripts/run_in_build_env.sh '(cd src/controller/python/test/unit_tests/ && python3 -m unittest -v)' # TODO Log Upload https://github.com/project-chip/connectedhomeip/issues/2227 # TODO https://github.com/project-chip/connectedhomeip/issues/1512 # - name: Run Code Coverage diff --git a/src/controller/python/chip/tlv/__init__.py b/src/controller/python/chip/tlv/__init__.py index e1d4d0a3a23a0f..4f5c771cba930a 100644 --- a/src/controller/python/chip/tlv/__init__.py +++ b/src/controller/python/chip/tlv/__init__.py @@ -112,6 +112,13 @@ } +class uint(int): + ''' + NewType will not return a class until Python 3.10, as Python 3.10 is not widely used, we still need to construct a class so it can work as a type. + ''' + pass + + class TLVWriter(object): def __init__(self, encoding=None, implicitProfile=None): self._encoding = encoding if encoding is not None else bytearray() @@ -177,11 +184,10 @@ def put(self, tag, val): self.putNull(tag) elif isinstance(val, bool): self.putBool(tag, val) + elif isinstance(val, uint): + self.putUnsignedInt(tag, val) elif isinstance(val, int): - if val < 0: - self.putSignedInt(tag, val) - else: - self.putUnsignedInt(tag, val) + self.putSignedInt(tag, val) elif isinstance(val, float): self.putFloat(tag, val) elif isinstance(val, str): @@ -553,6 +559,7 @@ def _decodeVal(self, tlv, decoding): (decoding["value"],) = struct.unpack( "