diff --git a/aprslib/parse.py b/aprslib/parse.py index 8ee2f1e..887aa82 100644 --- a/aprslib/parse.py +++ b/aprslib/parse.py @@ -79,15 +79,16 @@ def parse(packet): """ # attempt to detect encoding - try: - packet = packet.decode('utf-8') - except UnicodeDecodeError: - res = chardet.detect(packet) + if isinstance(packet, str): + try: + packet = packet.decode('utf-8') + except UnicodeDecodeError: + res = chardet.detect(packet) - if res['confidence'] > 0.7: - packet = packet.decode(res['encoding']) - else: - packet = packet.decode('latin-1') + if res['confidence'] > 0.7: + packet = packet.decode(res['encoding']) + else: + packet = packet.decode('latin-1') packet = packet.rstrip("\r\n") logger.debug("Parsing: %s", packet) diff --git a/req.txt b/req.txt index a8c4082..324ce1a 100644 --- a/req.txt +++ b/req.txt @@ -2,3 +2,4 @@ pylint nose coverage mox +chardet diff --git a/tests/test_parse.py b/tests/test_parse.py new file mode 100644 index 0000000..4987e28 --- /dev/null +++ b/tests/test_parse.py @@ -0,0 +1,36 @@ +# encoding: utf-8 + +import unittest + +from aprslib.parse import parse + + +class ParseTestCase(unittest.TestCase): + def test_unicode(self): + # 7bit ascii + result = parse("A>B:>status") + + self.assertIsInstance(result['status'], unicode) + self.assertEqual(result['status'], u"status") + + # string with degree sign + result = parse("A>B:>status\xb0") + + self.assertIsInstance(result['status'], unicode) + self.assertEqual(result['status'], u"status\xb0") + + # str with unicode + result = parse("A>B:>статус") + + self.assertIsInstance(result['status'], unicode) + self.assertEqual(result['status'], u"статус") + + # uncide input + result = parse(u"A>B:>статус") + + self.assertIsInstance(result['status'], unicode) + self.assertEqual(result['status'], u"статус") + + +if __name__ == '__main__': + unittest.main()