* Added exceptions to packer and parser

* added exception tests

* improve style

* linter is annoying

* static analysis fix

* static analysis fix2

* static analysis fix3

* clean up the code

* update exceptions

* style1

* style2
This commit is contained in:
grekiki 2020-09-10 15:01:34 +02:00 committed by GitHub
parent 42e105ec1c
commit ccd7d3e438
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 7 deletions

View File

@ -20,9 +20,11 @@ cdef class CANPacker:
map[int, int] address_to_size
def __init__(self, dbc_name):
self.packer = new cpp_CANPacker(dbc_name)
self.dbc = dbc_lookup(dbc_name)
if not self.dbc:
raise RuntimeError("Can't lookup" + dbc_name)
self.packer = new cpp_CANPacker(dbc_name)
num_msgs = self.dbc[0].num_msgs
for i in range(num_msgs):
msg = self.dbc[0].msgs[i]

View File

@ -1,2 +1,2 @@
from opendbc.can.parser_pyx import CANParser # pylint: disable=no-name-in-module, import-error
assert CANParser
from opendbc.can.parser_pyx import CANParser, CANDefine # pylint: disable=no-name-in-module, import-error
assert CANParser, CANDefine

View File

@ -17,7 +17,6 @@ from collections import defaultdict
cdef int CAN_INVALID_CNT = 5
cdef class CANParser:
cdef:
cpp_CANParser *can
@ -37,10 +36,11 @@ cdef class CANParser:
def __init__(self, dbc_name, signals, checks=None, bus=0):
if checks is None:
checks = []
self.can_valid = True
self.dbc_name = dbc_name
self.dbc = dbc_lookup(dbc_name)
if not self.dbc:
raise RuntimeError("Can't lookup" + dbc_name)
self.vl = {}
self.ts = {}
@ -148,6 +148,8 @@ cdef class CANDefine():
def __init__(self, dbc_name):
self.dbc_name = dbc_name
self.dbc = dbc_lookup(dbc_name)
if not self.dbc:
raise RuntimeError("Can't lookup" + dbc_name)
num_vals = self.dbc[0].num_vals

View File

@ -0,0 +1,31 @@
#!/usr/bin/env python3
import unittest
from opendbc.can.parser import CANParser, CANDefine
from opendbc.can.packer import CANPacker
class TestCanParserPackerExceptions(unittest.TestCase):
def test_civic_exceptions(self):
dbc_file = "honda_civic_touring_2016_can_generated"
dbc_invalid = dbc_file + "abcdef"
signals = [
("STEER_TORQUE", "STEERING_CONTROL", 0),
("STEER_TORQUE_REQUEST", "STEERING_CONTROL", 0),
]
checks = []
with self.assertRaises(RuntimeError):
CANParser(dbc_invalid, signals, checks, 0)
with self.assertRaises(RuntimeError):
CANPacker(dbc_invalid)
with self.assertRaises(RuntimeError):
CANDefine(dbc_invalid)
# Everything is supposed to work below
CANParser(dbc_file, signals, checks, 0)
CANPacker(dbc_file)
CANDefine(dbc_file)
if __name__ == "__main__":
unittest.main()