Fixed parse_str UnicodeEncodeError on python 2.

This commit is contained in:
Fabio Caccamo 2019-06-19 17:57:17 +02:00
parent 158f7180d3
commit ece93946b7
2 changed files with 17 additions and 8 deletions

View File

@ -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()
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

View File

@ -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 = {