From 0a843db4c31f8649eda7e9becca2f2aedc6341ad Mon Sep 17 00:00:00 2001 From: Song Guo Date: Fri, 15 Oct 2021 11:06:06 +0800 Subject: [PATCH 1/3] Add tlv.sint and tlv.uint to distinguish signed and unsigned int when encoding --- .github/workflows/build.yaml | 5 + src/controller/python/chip/tlv/__init__.py | 15 +- .../python/test/unit_tests/test_tlv.py | 153 ++++++++++++++++++ 3 files changed, 169 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 3a6a6c0fecfb01..ffe4d4017994cf 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..feb27103d360c0 100644 --- a/src/controller/python/chip/tlv/__init__.py +++ b/src/controller/python/chip/tlv/__init__.py @@ -112,6 +112,10 @@ } +class uint(int): + pass + + class TLVWriter(object): def __init__(self, encoding=None, implicitProfile=None): self._encoding = encoding if encoding is not None else bytearray() @@ -177,11 +181,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 +556,7 @@ def _decodeVal(self, tlv, decoding): (decoding["value"],) = struct.unpack( " Date: Tue, 19 Oct 2021 22:29:52 +0800 Subject: [PATCH 2/3] Use Pythonic NewType --- src/controller/python/chip/tlv/__init__.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/controller/python/chip/tlv/__init__.py b/src/controller/python/chip/tlv/__init__.py index feb27103d360c0..be19ad7ff78ac2 100644 --- a/src/controller/python/chip/tlv/__init__.py +++ b/src/controller/python/chip/tlv/__init__.py @@ -29,6 +29,7 @@ from __future__ import print_function import struct +from typing import NewType from collections import Mapping, Sequence, OrderedDict @@ -111,9 +112,7 @@ 0xE0: "Fully Qualified 8-byte", } - -class uint(int): - pass +uint = NewType('uint', int) class TLVWriter(object): From 9c6506329f3873d6a6dadbb99de737eb404b73f7 Mon Sep 17 00:00:00 2001 From: Song Guo Date: Wed, 20 Oct 2021 10:19:44 +0800 Subject: [PATCH 3/3] Revert and add a note --- src/controller/python/chip/tlv/__init__.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/controller/python/chip/tlv/__init__.py b/src/controller/python/chip/tlv/__init__.py index be19ad7ff78ac2..4f5c771cba930a 100644 --- a/src/controller/python/chip/tlv/__init__.py +++ b/src/controller/python/chip/tlv/__init__.py @@ -29,7 +29,6 @@ from __future__ import print_function import struct -from typing import NewType from collections import Mapping, Sequence, OrderedDict @@ -112,7 +111,12 @@ 0xE0: "Fully Qualified 8-byte", } -uint = NewType('uint', int) + +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):