Skip to content

Commit

Permalink
Merge branch 'leep_fix' into 'master'
Browse files Browse the repository at this point in the history
Fixed a bug interpreting register names as a literal address in hexadecimal when they happen to contain all hex characters.

Was being too permissive with register address representations (i.e. 'adc00' was being interpreted as '0xadc00' = 711680).

Now register names just can't:

    be all numeric
    begin with '0x'
    begin with '0b'

See merge request hdl-libraries/bedrock!204
  • Loading branch information
ldoolitt committed Oct 18, 2024
2 parents 1e39b9e + 9e69ca3 commit 4b7c5ac
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
6 changes: 5 additions & 1 deletion projects/common/leep/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ PYTHON=python3

LEEP_CORE=base.py raw.py ca.py file.py logic.py

all: test_cli
all: test_cli test_raw

test_cli: $(LEEP_CORE) cli.py
PYTHONPATH="$(THIS_DIR)/..:$(BUILD_DIR)" $(PYTHON) -m leep.test.test_cli test

.PHONY: test_raw
test_raw: raw.py
@PYTHONPATH="$(THIS_DIR)/.." $(PYTHON) -m leep.test.test_raw

# This is a test target currently only used for manual testing. Development is in progress
# for including this in automated regression tests.
server: $(LEEP_CORE) cli.py
Expand Down
10 changes: 6 additions & 4 deletions projects/common/leep/raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,13 @@ def _int(s):
return int(s)
except ValueError:
pass
bases = (2, 16, 10)
for base in bases:
if hasattr(s, 'startswith'):
try:
return int(s, base)
except (TypeError, ValueError):
if s.startswith('0x'):
return int(s, 16)
elif s.startswith('0b'):
return int(s, 2)
except ValueError:
pass
return None

Expand Down
26 changes: 26 additions & 0 deletions projects/common/leep/test/test_raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,29 @@ def test_array(self):
self.assertEqual(self.serv.data[101], 0xdeadbeef)
self.assertEqual(self.serv.data[102], 0x12345679)
self.assertEqual(self.serv.data[103], 0xdeadbeef)


def test_raw_int():
from leep.raw import _int
tests = {
"foo": None,
"123": 123,
"0x100": 0x100,
"0b1000": 0b1000,
"0xreg": None,
"0bentry": None,
}
for key, val in tests.items():
if val != _int(key):
raise Exception("Test failed _int({}) = {} != {}".format(key, _int(key), val))
return True


def doTests():
test_raw_int()
print("PASS")
return


if __name__ == "__main__":
doTests()

0 comments on commit 4b7c5ac

Please sign in to comment.