From 34f9be255e1094784f15d1fdff1cb43744b3f283 Mon Sep 17 00:00:00 2001 From: Derek Arnold Date: Sun, 15 Sep 2024 16:20:50 -0500 Subject: [PATCH] fix unrelated bug when fromStr input is short hex For example, 0x0 will generate an unhandled ValueError. This was caught during a random unit test run for 3.9, so I figure I'd fix it. This is unrelated to the PR otherwise. --- meshtastic/tests/test_util.py | 8 ++++++++ meshtastic/util.py | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/meshtastic/tests/test_util.py b/meshtastic/tests/test_util.py index 09f67fe..ac8fa15 100644 --- a/meshtastic/tests/test_util.py +++ b/meshtastic/tests/test_util.py @@ -650,3 +650,11 @@ def test_fuzz_fromStr(valstr): assert isinstance(result, int) except ValueError: assert isinstance(result, str) + +def test_shorthex(): + result = fromStr('0x0') + assert result == b'\x00' + result = fromStr('0x5') + assert result == b'\x05' + result = fromStr('0xffff') + assert result == b'\xff\xff' \ No newline at end of file diff --git a/meshtastic/util.py b/meshtastic/util.py index 8cb177c..3f864a2 100644 --- a/meshtastic/util.py +++ b/meshtastic/util.py @@ -82,7 +82,7 @@ def fromStr(valstr): val = bytes() elif valstr.startswith("0x"): # if needed convert to string with asBytes.decode('utf-8') - val = bytes.fromhex(valstr[2:]) + val = bytes.fromhex(valstr[2:].zfill(2)) elif valstr.startswith("base64:"): val = base64.b64decode(valstr[7:]) elif valstr.lower() in {"t", "true", "yes"}: