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

Skip to content

Commit 768921c

Browse files
committed
rewrite parsestr() so it's comprehensible; remove dead code
1 parent 202803a commit 768921c

1 file changed

Lines changed: 12 additions & 24 deletions

File tree

Python/ast.c

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3995,7 +3995,7 @@ decode_utf8(struct compiling *c, const char **sPtr, const char *end)
39953995
}
39963996

39973997
static PyObject *
3998-
decode_unicode(struct compiling *c, const char *s, size_t len, const char *encoding)
3998+
decode_unicode_with_escapes(struct compiling *c, const char *s, size_t len)
39993999
{
40004000
PyObject *v, *u;
40014001
char *buf;
@@ -4921,7 +4921,6 @@ parsestr(struct compiling *c, const node *n, int *bytesmode, int *fmode)
49214921
const char *s = STR(n);
49224922
int quote = Py_CHARMASK(*s);
49234923
int rawmode = 0;
4924-
int need_encoding;
49254924
if (Py_ISALPHA(quote)) {
49264925
while (!*bytesmode || !rawmode) {
49274926
if (quote == 'b' || quote == 'B') {
@@ -4977,11 +4976,10 @@ parsestr(struct compiling *c, const node *n, int *bytesmode, int *fmode)
49774976
return NULL;
49784977
}
49794978
}
4980-
if (!*bytesmode && !rawmode) {
4981-
return decode_unicode(c, s, len, c->c_encoding);
4982-
}
4979+
/* Avoid invoking escape decoding routines if possible. */
4980+
rawmode = rawmode || strchr(s, '\\') == NULL;
49834981
if (*bytesmode) {
4984-
/* Disallow non-ascii characters (but not escapes) */
4982+
/* Disallow non-ASCII characters. */
49854983
const char *ch;
49864984
for (ch = s; *ch; ch++) {
49874985
if (Py_CHARMASK(*ch) >= 0x80) {
@@ -4990,26 +4988,16 @@ parsestr(struct compiling *c, const node *n, int *bytesmode, int *fmode)
49904988
return NULL;
49914989
}
49924990
}
4993-
}
4994-
need_encoding = !*bytesmode && strcmp(c->c_encoding, "utf-8") != 0;
4995-
if (rawmode || strchr(s, '\\') == NULL) {
4996-
if (need_encoding) {
4997-
PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
4998-
if (u == NULL || !*bytesmode)
4999-
return u;
5000-
v = PyUnicode_AsEncodedString(u, c->c_encoding, NULL);
5001-
Py_DECREF(u);
5002-
return v;
5003-
} else if (*bytesmode) {
4991+
if (rawmode)
50044992
return PyBytes_FromStringAndSize(s, len);
5005-
} else if (strcmp(c->c_encoding, "utf-8") == 0) {
5006-
return PyUnicode_FromStringAndSize(s, len);
5007-
} else {
5008-
return PyUnicode_DecodeLatin1(s, len, NULL);
5009-
}
4993+
else
4994+
return PyBytes_DecodeEscape(s, len, NULL, /* ignored */ 0, NULL);
4995+
} else {
4996+
if (rawmode)
4997+
return PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
4998+
else
4999+
return decode_unicode_with_escapes(c, s, len);
50105000
}
5011-
return PyBytes_DecodeEscape(s, len, NULL, 1,
5012-
need_encoding ? c->c_encoding : NULL);
50135001
}
50145002

50155003
/* Accepts a STRING+ atom, and produces an expr_ty node. Run through

0 commit comments

Comments
 (0)