Skip to content

Commit 4f5d0a6

Browse files
committed
Always return a string object on Python 2.x
Before this patch, unidecode() returned a unicode object on Python 2.x if the input was a unicode object that contained ASCII characters. Behaviour on Python 3.x remains unchanged.
1 parent 37cf555 commit 4f5d0a6

3 files changed

Lines changed: 17 additions & 9 deletions

File tree

tests/basic_2.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,10 @@ def test_specific(self):
115115
''),
116116
]
117117

118-
for input, output in TESTS:
119-
self.failUnlessEqual(unidecode(input), output)
118+
for input, correct_output in TESTS:
119+
test_output = unidecode(input)
120+
self.failUnlessEqual(test_output, correct_output)
121+
self.failUnless(isinstance(test_output, str))
120122

121123
@unittest.skipIf(sys.maxunicode < 0x10000, "narrow build")
122124
def test_specific_wide(self):
@@ -131,8 +133,10 @@ def test_specific_wide(self):
131133
'km/h'),
132134
]
133135

134-
for input, output in TESTS:
135-
self.failUnlessEqual(unidecode(input), output)
136+
for input, correct_output in TESTS:
137+
test_output = unidecode(input)
138+
self.failUnlessEqual(test_output, correct_output)
139+
self.failUnless(isinstance(test_output, str))
136140

137141
if __name__ == "__main__":
138142
unittest.main()

tests/basic_3.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,10 @@ def test_specific(self):
101101
''),
102102
]
103103

104-
for instr, output in TESTS:
105-
self.failUnlessEqual(unidecode(instr), output)
104+
for instr, correct_output in TESTS:
105+
test_output = unidecode(instr)
106+
self.failUnlessEqual(test_output, correct_output)
107+
self.failUnless(isinstance(test_output, str))
106108

107109
@unittest.skipIf(sys.maxunicode < 0x10000, "narrow build")
108110
def test_specific_wide(self):
@@ -117,5 +119,7 @@ def test_specific_wide(self):
117119
'km/h'),
118120
]
119121

120-
for instr, output in TESTS:
121-
self.failUnlessEqual(unidecode(instr), output)
122+
for instr, correct_output in TESTS:
123+
test_output = unidecode(instr)
124+
self.failUnlessEqual(test_output, correct_output)
125+
self.failUnless(isinstance(test_output, str))

unidecode/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def unidecode(string):
2828
codepoint = ord(char)
2929

3030
if codepoint < 0x80: # Basic ASCII
31-
retval.append(char)
31+
retval.append(str(char))
3232
continue
3333

3434
if codepoint > 0xeffff:

0 commit comments

Comments
 (0)