diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..79236f2 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +chardet diff --git a/routeros_api/api_structure.py b/routeros_api/api_structure.py index 7899956..09ff78e 100644 --- a/routeros_api/api_structure.py +++ b/routeros_api/api_structure.py @@ -4,6 +4,8 @@ import ipaddress import re +import chardet + class Field(object): __metaclass__ = abc.ABCMeta @@ -28,6 +30,8 @@ def get_mikrotik_value(self, string): return string.encode() def get_python_value(self, bytes): + if bytes: + return bytes.decode(chardet.detect(bytes)['encoding']) return bytes.decode() diff --git a/tests/test_resource.py b/tests/test_resource.py index 90ada5d..ff68766 100644 --- a/tests/test_resource.py +++ b/tests/test_resource.py @@ -68,3 +68,13 @@ def test_boolean_resource_set(self): communicator.call.assert_called_with( '/boolean/', 'set', arguments={'boolean': b'yes'}, queries={}, additional_queries=()) + + def test_unicode_decode_error(self): + string = structure.StringField + output = string.get_python_value(string,bytes=b'\xc2') + self.assertEqual(output, 'Â') + + def test_blank_decode_byte(self): + string = structure.StringField + output = string.get_python_value(string,bytes=b'') + self.assertEqual(output, '')