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

Skip to content

Commit 6ecf77b

Browse files
committed
Basic support for PEP 414 without docs or tests.
1 parent 745ccf8 commit 6ecf77b

3 files changed

Lines changed: 32 additions & 11 deletions

File tree

Lib/tokenize.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,10 @@ def maybe(*choices): return group(*choices) + '?'
135135
Single3 = r"[^'\\]*(?:(?:\\.|'(?!''))[^'\\]*)*'''"
136136
# Tail end of """ string.
137137
Double3 = r'[^"\\]*(?:(?:\\.|"(?!""))[^"\\]*)*"""'
138-
Triple = group("[bB]?[rR]?'''", '[bB]?[rR]?"""')
138+
Triple = group("[bBuU]?[rR]?'''", '[bBuU]?[rR]?"""')
139139
# Single-line ' or " string.
140-
String = group(r"[bB]?[rR]?'[^\n'\\]*(?:\\.[^\n'\\]*)*'",
141-
r'[bB]?[rR]?"[^\n"\\]*(?:\\.[^\n"\\]*)*"')
140+
String = group(r"[bBuU]?[rR]?'[^\n'\\]*(?:\\.[^\n'\\]*)*'",
141+
r'[bBuU]?[rR]?"[^\n"\\]*(?:\\.[^\n"\\]*)*"')
142142

143143
# Because of leftmost-then-longest match semantics, be sure to put the
144144
# longest operators first (e.g., if = came before ==, == would get
@@ -156,9 +156,9 @@ def maybe(*choices): return group(*choices) + '?'
156156
Token = Ignore + PlainToken
157157

158158
# First (or only) line of ' or " string.
159-
ContStr = group(r"[bB]?[rR]?'[^\n'\\]*(?:\\.[^\n'\\]*)*" +
159+
ContStr = group(r"[bBuU]?[rR]?'[^\n'\\]*(?:\\.[^\n'\\]*)*" +
160160
group("'", r'\\\r?\n'),
161-
r'[bB]?[rR]?"[^\n"\\]*(?:\\.[^\n"\\]*)*' +
161+
r'[bBuU]?[rR]?"[^\n"\\]*(?:\\.[^\n"\\]*)*' +
162162
group('"', r'\\\r?\n'))
163163
PseudoExtras = group(r'\\\r?\n', Comment, Triple)
164164
PseudoToken = Whitespace + group(PseudoExtras, Number, Funny, ContStr, Name)
@@ -176,21 +176,35 @@ def _compile(expr):
176176
"bR'''": Single3, 'bR"""': Double3,
177177
"Br'''": Single3, 'Br"""': Double3,
178178
"BR'''": Single3, 'BR"""': Double3,
179-
'r': None, 'R': None, 'b': None, 'B': None}
179+
"u'''": Single3, 'u"""': Double3,
180+
"ur'''": Single3, 'ur"""': Double3,
181+
"R'''": Single3, 'R"""': Double3,
182+
"U'''": Single3, 'U"""': Double3,
183+
"uR'''": Single3, 'uR"""': Double3,
184+
"Ur'''": Single3, 'Ur"""': Double3,
185+
"UR'''": Single3, 'UR"""': Double3,
186+
'r': None, 'R': None, 'b': None, 'B': None,
187+
'u': None, 'U': None}
180188

181189
triple_quoted = {}
182190
for t in ("'''", '"""',
183191
"r'''", 'r"""', "R'''", 'R"""',
184192
"b'''", 'b"""', "B'''", 'B"""',
185193
"br'''", 'br"""', "Br'''", 'Br"""',
186-
"bR'''", 'bR"""', "BR'''", 'BR"""'):
194+
"bR'''", 'bR"""', "BR'''", 'BR"""',
195+
"u'''", 'u"""', "U'''", 'U"""',
196+
"ur'''", 'ur"""', "Ur'''", 'Ur"""',
197+
"uR'''", 'uR"""', "UR'''", 'UR"""'):
187198
triple_quoted[t] = t
188199
single_quoted = {}
189200
for t in ("'", '"',
190201
"r'", 'r"', "R'", 'R"',
191202
"b'", 'b"', "B'", 'B"',
192203
"br'", 'br"', "Br'", 'Br"',
193-
"bR'", 'bR"', "BR'", 'BR"' ):
204+
"bR'", 'bR"', "BR'", 'BR"' ,
205+
"u'", 'u"', "U'", 'U"',
206+
"ur'", 'ur"', "Ur'", 'Ur"',
207+
"uR'", 'uR"', "UR'", 'UR"' ):
194208
single_quoted[t] = t
195209

196210
tabsize = 8

Parser/tokenizer.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1412,11 +1412,15 @@ tok_get(register struct tok_state *tok, char **p_start, char **p_end)
14121412
/* Identifier (most frequent token!) */
14131413
nonascii = 0;
14141414
if (is_potential_identifier_start(c)) {
1415-
/* Process b"", r"", br"" and rb"" */
1416-
int saw_b = 0, saw_r = 0;
1415+
/* Process b"", r"", u"", br"", rb"" and ur"" */
1416+
int saw_b = 0, saw_r = 0, saw_u = 0;
14171417
while (1) {
1418-
if (!saw_b && (c == 'b' || c == 'B'))
1418+
if (!(saw_b || saw_u) && (c == 'b' || c == 'B'))
14191419
saw_b = 1;
1420+
/* Since this is a backwards compatibility support literal we don't
1421+
want to support it in arbitrary order like byte literals. */
1422+
else if (!(saw_b || saw_u || saw_r) && (c == 'u' || c == 'U'))
1423+
saw_u = 1;
14201424
else if (!saw_r && (c == 'r' || c == 'R'))
14211425
saw_r = 1;
14221426
else

Python/ast.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3796,6 +3796,9 @@ parsestr(struct compiling *c, const node *n, int *bytesmode)
37963796
quote = *++s;
37973797
*bytesmode = 1;
37983798
}
3799+
else if (quote == 'u' || quote == 'U') {
3800+
quote = *++s;
3801+
}
37993802
else if (quote == 'r' || quote == 'R') {
38003803
quote = *++s;
38013804
rawmode = 1;

0 commit comments

Comments
 (0)