Skip to content

Commit 47f5e27

Browse files
committed
bugfix for #625
1 parent 8c1b424 commit 47f5e27

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

src/canmatrix/canmatrix.py

+18-3
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,12 @@ def add_attribute(self, attribute, value): # type (attribute: str, value: typin
106106
:param str attribute: Attribute name
107107
:param any value: Attribute value
108108
"""
109-
self.attributes[attribute] = value
109+
try:
110+
self.attributes[attribute] = str(value)
111+
except UnicodeDecodeError:
112+
self.attributes[attribute] = value
113+
if type(self.attributes[attribute]) == str:
114+
self.attributes[attribute] = self.attributes[attribute].strip()
110115

111116
def del_attribute(self, attribute):
112117
if attribute in self.attributes:
@@ -282,7 +287,12 @@ def add_attribute(self, attribute, value):
282287
:param str attribute: attribute name
283288
:param value: attribute value
284289
"""
285-
self.attributes[attribute] = value
290+
try:
291+
self.attributes[attribute] = str(value)
292+
except UnicodeDecodeError:
293+
self.attributes[attribute] = value
294+
if type(self.attributes[attribute]) == str:
295+
self.attributes[attribute] = self.attributes[attribute].strip()
286296

287297
def del_attribute(self, attribute):
288298
"""
@@ -1715,7 +1725,12 @@ def add_attribute(self, attribute, value): # type: (str, typing.Any) -> None
17151725
:param str attribute: attribute name
17161726
:param value: attribute value
17171727
"""
1718-
self.attributes[attribute] = value
1728+
try:
1729+
self.attributes[attribute] = str(value)
1730+
except UnicodeDecodeError:
1731+
self.attributes[attribute] = value
1732+
if type(self.attributes[attribute]) == str:
1733+
self.attributes[attribute] = self.attributes[attribute].strip()
17191734

17201735
def add_signal_defines(self, type, definition):
17211736
"""

src/canmatrix/formats/dbc.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def create_attribute_string(attribute, attribute_class, name, value, is_string):
9191
# type: (str, str, str, typing.Any, bool) -> str
9292
if is_string:
9393
value = '"' + value + '"'
94-
elif not value:
94+
elif value is None:
9595
value = '""'
9696

9797
attribute_string = 'BA_ "' + attribute + '" ' + attribute_class + ' ' + name + ' ' + str(value) + ';\n'

src/canmatrix/tests/test_dbc.py

+13
Original file line numberDiff line numberDiff line change
@@ -523,11 +523,24 @@ def test_int_attribute_zero():
523523
frame = canmatrix.Frame("some Frame")
524524
frame.add_signal(canmatrix.Signal("signal_name", size=1, start_bit=1))
525525
db.add_frame(frame)
526+
db.add_ecu_defines("ecu_define", "INT 0 10")
527+
db.add_ecu_defines("ecu_define2", "INT 0 10")
528+
db.add_ecu_defines("ecu_define3", "INT 0 10")
529+
526530
db.add_frame_defines("test", "INT 0 10")
527531
db.add_frame_defines("test2", "INT 0 10")
528532
frame.add_attribute("test", 7)
529533
frame.add_attribute("test2", 0)
534+
535+
ecu = canmatrix.Ecu('TestEcu')
536+
ecu.add_attribute('ecu_define', 1)
537+
ecu.add_attribute('ecu_define2', 0)
538+
539+
db.add_ecu(ecu)
540+
530541
outdbc = io.BytesIO()
531542
canmatrix.formats.dump(db, outdbc, "dbc")
532543
assert 'BO_ 0 7' in outdbc.getvalue().decode('utf8')
533544
assert 'BO_ 0 0' in outdbc.getvalue().decode('utf8')
545+
assert 'TestEcu 1' in outdbc.getvalue().decode('utf8')
546+
assert 'TestEcu 0' in outdbc.getvalue().decode('utf8')

0 commit comments

Comments
 (0)