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

Skip to content

Commit 5aa88f0

Browse files
committed
Marc-Andre Lemburg: support for Unicode string literals (u"...", ur"...").
1 parent 09095f3 commit 5aa88f0

1 file changed

Lines changed: 22 additions & 3 deletions

File tree

Python/compile.c

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -875,8 +875,18 @@ parsestr(s)
875875
int c;
876876
int first = *s;
877877
int quote = first;
878-
if (isalpha(quote) || quote == '_')
879-
quote = *++s;
878+
int rawmode = 0;
879+
int unicode = 0;
880+
if (isalpha(quote) || quote == '_') {
881+
if (quote == 'u' || quote == 'U') {
882+
quote = *++s;
883+
unicode = 1;
884+
}
885+
if (quote == 'r' || quote == 'R') {
886+
quote = *++s;
887+
rawmode = 1;
888+
}
889+
}
880890
if (quote != '\'' && quote != '\"') {
881891
PyErr_BadInternalCall();
882892
return NULL;
@@ -895,8 +905,17 @@ parsestr(s)
895905
return NULL;
896906
}
897907
}
898-
if (first != quote || strchr(s, '\\') == NULL)
908+
if (unicode) {
909+
if (rawmode)
910+
return PyUnicode_DecodeRawUnicodeEscape(
911+
s, len, NULL);
912+
else
913+
return PyUnicode_DecodeUnicodeEscape(
914+
s, len, NULL);
915+
}
916+
else if (rawmode || strchr(s, '\\') == NULL) {
899917
return PyString_FromStringAndSize(s, len);
918+
}
900919
v = PyString_FromStringAndSize((char *)NULL, len);
901920
p = buf = PyString_AsString(v);
902921
end = s + len;

0 commit comments

Comments
 (0)