Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b84d723

Browse files
author
Victor Stinner
committed
(Merge 3.2) Issue #13093: Fix error handling on PyUnicode_EncodeDecimal()
1 parent cfed46e commit b84d723

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

Lib/test/test_unicode.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1824,6 +1824,12 @@ def test_encode_decimal(self):
18241824
b'123€')
18251825
self.assertEqual(unicode_encodedecimal("123\u20ac", "backslashreplace"),
18261826
b'123\\u20ac')
1827+
self.assertEqual(unicode_encodedecimal("123\u20ac\N{EM SPACE}", "replace"),
1828+
b'123? ')
1829+
self.assertEqual(unicode_encodedecimal("123\u20ac\u20ac", "replace"),
1830+
b'123??')
1831+
self.assertEqual(unicode_encodedecimal("123\u20ac\u0660", "replace"),
1832+
b'123?0')
18271833

18281834
def test_transform_decimal(self):
18291835
from _testcapi import unicode_transformdecimaltoascii as transform_decimal

Objects/unicodeobject.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8875,22 +8875,25 @@ PyUnicode_EncodeDecimal(Py_UNICODE *s,
88758875
kind = PyUnicode_KIND(unicode);
88768876
data = PyUnicode_DATA(unicode);
88778877

8878-
for (i=0; i < length; i++) {
8878+
for (i=0; i < length; ) {
88798879
Py_UCS4 ch = PyUnicode_READ(kind, data, i);
88808880
int decimal;
88818881
Py_ssize_t startpos, endpos;
88828882

88838883
if (Py_UNICODE_ISSPACE(ch)) {
88848884
*output++ = ' ';
8885+
i++;
88858886
continue;
88868887
}
88878888
decimal = Py_UNICODE_TODECIMAL(ch);
88888889
if (decimal >= 0) {
88898890
*output++ = '0' + decimal;
8891+
i++;
88908892
continue;
88918893
}
88928894
if (0 < ch && ch < 256) {
88938895
*output++ = (char)ch;
8896+
i++;
88948897
continue;
88958898
}
88968899
/* All other characters are considered unencodable */
@@ -8899,8 +8902,8 @@ PyUnicode_EncodeDecimal(Py_UNICODE *s,
88998902
for (; endpos < length; endpos++) {
89008903
ch = PyUnicode_READ(kind, data, endpos);
89018904
if ((0 < ch && ch < 256) ||
8902-
!Py_UNICODE_ISSPACE(ch) ||
8903-
Py_UNICODE_TODECIMAL(ch))
8905+
Py_UNICODE_ISSPACE(ch) ||
8906+
0 <= Py_UNICODE_TODECIMAL(ch))
89048907
break;
89058908
}
89068909
/* cache callback name lookup
@@ -8924,7 +8927,8 @@ PyUnicode_EncodeDecimal(Py_UNICODE *s,
89248927
case 2: /* replace */
89258928
for (j=startpos; j < endpos; j++)
89268929
*output++ = '?';
8927-
/* fall through */
8930+
i = endpos;
8931+
break;
89288932
case 3: /* ignore */
89298933
i = endpos;
89308934
break;

0 commit comments

Comments
 (0)