Skip to content

Commit 5f4bfe9

Browse files
committed
Add unicode support to BaseProperty
1 parent fe639ed commit 5f4bfe9

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

cybox/common/properties.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ def __init__(self, value=None):
3636
self.refanging_transform = None
3737

3838
def __str__(self):
39-
return str(self.serialized_value)
39+
# To be safe, return the unicode string encoded as UTF-8
40+
return self.__unicode__().encode("utf-8")
41+
42+
def __unicode__(self):
43+
return unicode(self.serialized_value)
4044

4145
def __int__(self):
4246
return int(self.serialized_value)

cybox/test/common/properties_test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ def test_integer(self):
3030
self.assertTrue(i.datatype, "Integer")
3131
self.assertTrue(i.value, 42)
3232

33+
def test_unicode_string(self):
34+
s = u"A Unicode \ufffd string"
35+
string = String(s)
36+
self.assertEqual(s, unicode(string))
37+
self.assertEqual(s.encode("utf-8"), str(string))
38+
3339
def test_cannot_create_abstract_obj(self):
3440
a = BaseProperty()
3541
self.assertRaises(NotImplementedError, a.to_obj)

0 commit comments

Comments
 (0)