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 decimal import Decimal, DecimalException
from MailChecker import MailChecker from MailChecker import MailChecker
from phonenumbers import phonenumberutil, PhoneNumberFormat from phonenumbers import phonenumberutil, PhoneNumberFormat
from six import string_types
from slugify import slugify from slugify import slugify
import ftfy import ftfy
@ -164,10 +165,14 @@ def parse_slug(val):
def parse_str(val): def parse_str(val):
val = str(val).strip() if (isinstance(val, string_types)):
try: try:
val = ftfy.fix_text(val) val = ftfy.fix_text(val)
except UnicodeError: except UnicodeError:
pass pass
else:
val = str(val)
val = val.strip()
val = ' '.join(val.split())
return val return val

View File

@ -433,19 +433,23 @@ class ParseDictTestCase(unittest.TestCase):
def test_get_str(self): def test_get_str(self):
d = { d = {
'a': 'Hello World', 'a': 'Hello World',
'b': 1, 'b': 'Hello World',
'c': 1,
} }
b = ParseDict(d) b = ParseDict(d)
self.assertEqual(b.get_str('a'), 'Hello World') 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 # # only python 3
# def test_get_str_fix_encoding(self): # def test_get_str_fix_encoding(self):
# d = { # d = {
# 'a': 'Sexâ\x80\x99n Drug', # 'a': 'Sexâ\x80\x99n Drug',
# 'b': 'Localit\xe0',
# } # }
# b = ParseDict(d) # 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): def test_get_str_list(self):
d = { d = {