Change in pysim[master]: tlv: Don't require encoder/decoder methods for TLV without value

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

laforge gerrit-no-reply at lists.osmocom.org
Sun Oct 24 08:28:31 UTC 2021


laforge has submitted this change. ( https://gerrit.osmocom.org/c/pysim/+/25872 )

Change subject: tlv: Don't require encoder/decoder methods for TLV without value
......................................................................

tlv: Don't require encoder/decoder methods for TLV without value

There are instances where a TLV IE is used as just a flag, i.e.
length zero and no value part.  In those situations, it would require
a lot of boilerplate code to require the TLV_IE class definitions to
have _to_bytes/_from_bytes methods that do nothing.

So instead, add a shortcut: If we want to encode 'None', then return
b'', and if we want to decode b'' return None.

Change-Id: Ie8eb2830e8eefa81e94b8b8b157062c085aeb777
---
M pySim/tlv.py
1 file changed, 6 insertions(+), 2 deletions(-)

Approvals:
  Jenkins Builder: Verified
  laforge: Looks good to me, approved



diff --git a/pySim/tlv.py b/pySim/tlv.py
index 05f824e..0dc746b 100644
--- a/pySim/tlv.py
+++ b/pySim/tlv.py
@@ -80,7 +80,9 @@
     def to_bytes(self) -> bytes:
         """Convert from internal representation to binary bytes.  Store the binary result
         in the internal state and return it."""
-        if self._construct:
+        if not self.decoded:
+            do = b''
+        elif self._construct:
             do = self._construct.build(self.decoded, total_len=None)
         elif self.__class__._construct:
             do = self.__class__._construct.build(self.decoded, total_len=None)
@@ -97,7 +99,9 @@
         """Convert from binary bytes to internal representation. Store the decoded result
         in the internal state and return it."""
         self.encoded = do
-        if self._construct:
+        if self.encoded == b'':
+            self.decoded = None
+        elif self._construct:
             self.decoded = parse_construct(self._construct, do)
         elif self.__class__._construct:
             self.decoded = parse_construct(self.__class__._construct, do)

-- 
To view, visit https://gerrit.osmocom.org/c/pysim/+/25872
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: Ie8eb2830e8eefa81e94b8b8b157062c085aeb777
Gerrit-Change-Number: 25872
Gerrit-PatchSet: 2
Gerrit-Owner: laforge <laforge at osmocom.org>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: dexter <pmaier at sysmocom.de>
Gerrit-Reviewer: laforge <laforge at osmocom.org>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20211024/ccd55d27/attachment.htm>


More information about the gerrit-log mailing list