Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error decoding special chars #82

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
chardet
4 changes: 4 additions & 0 deletions routeros_api/api_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import ipaddress
import re

import chardet


class Field(object):
__metaclass__ = abc.ABCMeta
Expand All @@ -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()


Expand Down
10 changes: 10 additions & 0 deletions tests/test_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, '')