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

Skip to content

Commit 202803a

Browse files
committed
c_encoding can never be NULL, which allows some code simplification
1 parent 7812dbc commit 202803a

1 file changed

Lines changed: 43 additions & 47 deletions

File tree

Python/ast.c

Lines changed: 43 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4002,55 +4002,52 @@ decode_unicode(struct compiling *c, const char *s, size_t len, const char *encod
40024002
char *p;
40034003
const char *end;
40044004

4005-
if (encoding == NULL) {
4006-
u = NULL;
4007-
} else {
4008-
/* check for integer overflow */
4009-
if (len > PY_SIZE_MAX / 6)
4010-
return NULL;
4011-
/* "ä" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4012-
"\ä" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4013-
u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4014-
if (u == NULL)
4015-
return NULL;
4016-
p = buf = PyBytes_AsString(u);
4017-
end = s + len;
4018-
while (s < end) {
4019-
if (*s == '\\') {
4020-
*p++ = *s++;
4021-
if (*s & 0x80) {
4022-
strcpy(p, "u005c");
4023-
p += 5;
4024-
}
4005+
/* check for integer overflow */
4006+
if (len > PY_SIZE_MAX / 6)
4007+
return NULL;
4008+
/* "ä" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4009+
"\ä" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4010+
u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4011+
if (u == NULL)
4012+
return NULL;
4013+
p = buf = PyBytes_AsString(u);
4014+
end = s + len;
4015+
while (s < end) {
4016+
if (*s == '\\') {
4017+
*p++ = *s++;
4018+
if (*s & 0x80) {
4019+
strcpy(p, "u005c");
4020+
p += 5;
40254021
}
4026-
if (*s & 0x80) { /* XXX inefficient */
4027-
PyObject *w;
4028-
int kind;
4029-
void *data;
4030-
Py_ssize_t len, i;
4031-
w = decode_utf8(c, &s, end);
4032-
if (w == NULL) {
4033-
Py_DECREF(u);
4034-
return NULL;
4035-
}
4036-
kind = PyUnicode_KIND(w);
4037-
data = PyUnicode_DATA(w);
4038-
len = PyUnicode_GET_LENGTH(w);
4039-
for (i = 0; i < len; i++) {
4040-
Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4041-
sprintf(p, "\\U%08x", chr);
4042-
p += 10;
4043-
}
4044-
/* Should be impossible to overflow */
4045-
assert(p - buf <= Py_SIZE(u));
4046-
Py_DECREF(w);
4047-
} else {
4048-
*p++ = *s++;
4022+
}
4023+
if (*s & 0x80) { /* XXX inefficient */
4024+
PyObject *w;
4025+
int kind;
4026+
void *data;
4027+
Py_ssize_t len, i;
4028+
w = decode_utf8(c, &s, end);
4029+
if (w == NULL) {
4030+
Py_DECREF(u);
4031+
return NULL;
40494032
}
4033+
kind = PyUnicode_KIND(w);
4034+
data = PyUnicode_DATA(w);
4035+
len = PyUnicode_GET_LENGTH(w);
4036+
for (i = 0; i < len; i++) {
4037+
Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4038+
sprintf(p, "\\U%08x", chr);
4039+
p += 10;
4040+
}
4041+
/* Should be impossible to overflow */
4042+
assert(p - buf <= Py_SIZE(u));
4043+
Py_DECREF(w);
4044+
} else {
4045+
*p++ = *s++;
40504046
}
4051-
len = p - buf;
4052-
s = buf;
40534047
}
4048+
len = p - buf;
4049+
s = buf;
4050+
40544051
v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
40554052
Py_XDECREF(u);
40564053
return v;
@@ -4994,8 +4991,7 @@ parsestr(struct compiling *c, const node *n, int *bytesmode, int *fmode)
49944991
}
49954992
}
49964993
}
4997-
need_encoding = (!*bytesmode && c->c_encoding != NULL &&
4998-
strcmp(c->c_encoding, "utf-8") != 0);
4994+
need_encoding = !*bytesmode && strcmp(c->c_encoding, "utf-8") != 0;
49994995
if (rawmode || strchr(s, '\\') == NULL) {
50004996
if (need_encoding) {
50014997
PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);

0 commit comments

Comments
 (0)