File tree 2 files changed +27
-5
lines changed
2 files changed +27
-5
lines changed Original file line number Diff line number Diff line change @@ -68,6 +68,24 @@ def test_ascii(self):
68
68
69
69
wlog .stop ()
70
70
71
+
72
+ def test_ignore (self ):
73
+ wlog = WarningLogger ()
74
+ wlog .start ("should be ignored" )
75
+
76
+ r = self .unidecode (u'æøåÆØÅ' , ignore = u'æøå' )
77
+ self .assertEqual (r , u'æøåAEOA' )
78
+
79
+ if sys .version_info [0 ] >= 3 :
80
+ self .assertEqual (type (r ), str )
81
+ else :
82
+ self .assertEqual (type (r ), unicode )
83
+
84
+ # unicode objects shouldn't raise warnings
85
+ self .assertEqual (0 , len (wlog .log ))
86
+
87
+ wlog .stop ()
88
+
71
89
def test_bmp (self ):
72
90
for n in range (0 ,0x10000 ):
73
91
# skip over surrogate pairs, which throw a warning
Original file line number Diff line number Diff line change @@ -28,7 +28,7 @@ def _warn_if_not_unicode(string):
28
28
RuntimeWarning , 2 )
29
29
30
30
31
- def unidecode_expect_ascii (string ):
31
+ def unidecode_expect_ascii (string , ignore = u'' ):
32
32
"""Transliterate an Unicode object into an ASCII string
33
33
34
34
>>> unidecode(u"\u5317 \u4EB0 ")
@@ -47,30 +47,34 @@ def unidecode_expect_ascii(string):
47
47
try :
48
48
bytestring = string .encode ('ASCII' )
49
49
except UnicodeEncodeError :
50
- return _unidecode (string )
50
+ return _unidecode (string , ignore )
51
51
if version_info [0 ] >= 3 :
52
52
return string
53
53
else :
54
54
return bytestring
55
55
56
- def unidecode_expect_nonascii (string ):
56
+ def unidecode_expect_nonascii (string , ignore = u'' ):
57
57
"""Transliterate an Unicode object into an ASCII string
58
58
59
59
>>> unidecode(u"\u5317 \u4EB0 ")
60
60
"Bei Jing "
61
61
"""
62
62
63
63
_warn_if_not_unicode (string )
64
- return _unidecode (string )
64
+ return _unidecode (string , ignore )
65
65
66
66
unidecode = unidecode_expect_ascii
67
67
68
- def _unidecode (string ):
68
+ def _unidecode (string , ignore = u'' ):
69
69
retval = []
70
70
71
71
for char in string :
72
72
codepoint = ord (char )
73
73
74
+ if char in ignore :
75
+ retval .append (char )
76
+ continue
77
+
74
78
if codepoint < 0x80 : # Basic ASCII
75
79
retval .append (str (char ))
76
80
continue
You can’t perform that action at this time.
0 commit comments