From ece93946b73da6314108073c77a7d39dc6132e3c Mon Sep 17 00:00:00 2001 From: Fabio Caccamo Date: Wed, 19 Jun 2019 17:57:17 +0200 Subject: [PATCH] Fixed parse_str UnicodeEncodeError on python 2. --- benedict/utils/parse_util.py | 15 ++++++++++----- tests/test_parse_dict.py | 10 +++++++--- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/benedict/utils/parse_util.py b/benedict/utils/parse_util.py index 4977b8b..68e2db0 100644 --- a/benedict/utils/parse_util.py +++ b/benedict/utils/parse_util.py @@ -5,6 +5,7 @@ from dateutil import parser as date_parser from decimal import Decimal, DecimalException from MailChecker import MailChecker from phonenumbers import phonenumberutil, PhoneNumberFormat +from six import string_types from slugify import slugify import ftfy @@ -164,10 +165,14 @@ def parse_slug(val): def parse_str(val): - val = str(val).strip() - try: - val = ftfy.fix_text(val) - except UnicodeError: - pass + if (isinstance(val, string_types)): + try: + val = ftfy.fix_text(val) + except UnicodeError: + pass + else: + val = str(val) + val = val.strip() + val = ' '.join(val.split()) return val diff --git a/tests/test_parse_dict.py b/tests/test_parse_dict.py index 855af16..21561dd 100644 --- a/tests/test_parse_dict.py +++ b/tests/test_parse_dict.py @@ -433,19 +433,23 @@ class ParseDictTestCase(unittest.TestCase): def test_get_str(self): d = { 'a': 'Hello World', - 'b': 1, + 'b': 'Hello World', + 'c': 1, } b = ParseDict(d) self.assertEqual(b.get_str('a'), 'Hello World') - self.assertEqual(b.get_str('b'), '1') + self.assertEqual(b.get_str('b'), 'Hello World') + self.assertEqual(b.get_str('c'), '1') # # only python 3 # def test_get_str_fix_encoding(self): # d = { # 'a': 'Sexâ\x80\x99n Drug', + # 'b': 'Localit\xe0', # } # b = ParseDict(d) - # self.assertEqual(b.get_str('a'), 'Sex\'n Drug') + # # self.assertEqual(b.get_str('a'), 'Sex\'n Drug') + # # self.assertEqual(b.get_str('b'), 'Località') def test_get_str_list(self): d = {