Skip to content

Commit 93ff727

Browse files
committed
bpo-23894: lib2to3 doesn't recognize rb'...' and f'...'
Note: this doesn't unpack f-strings into the underlying JOINEDSTR AST.
1 parent d9b0b8f commit 93ff727

2 files changed

Lines changed: 20 additions & 4 deletions

File tree

Lib/lib2to3/pgen2/tokenize.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,10 @@ def maybe(*choices): return group(*choices) + '?'
9595
Token = Ignore + PlainToken
9696

9797
# First (or only) line of ' or " string.
98-
ContStr = group(r"[uUbB]?[rR]?'[^\n'\\]*(?:\\.[^\n'\\]*)*" +
98+
_litprefix = r"(?:[uUrRbBfF]|[rR][bB]|[bBuU][rR])?"
99+
ContStr = group(_litprefix + r"'[^\n'\\]*(?:\\.[^\n'\\]*)*" +
99100
group("'", r'\\\r?\n'),
100-
r'[uUbB]?[rR]?"[^\n"\\]*(?:\\.[^\n"\\]*)*' +
101+
_litprefix + r'"[^\n"\\]*(?:\\.[^\n"\\]*)*' +
101102
group('"', r'\\\r?\n'))
102103
PseudoExtras = group(r'\\\r?\n', Comment, Triple)
103104
PseudoToken = Whitespace + group(PseudoExtras, Number, Funny, ContStr, Name)
@@ -108,41 +109,54 @@ def maybe(*choices): return group(*choices) + '?'
108109
"'''": single3prog, '"""': double3prog,
109110
"r'''": single3prog, 'r"""': double3prog,
110111
"u'''": single3prog, 'u"""': double3prog,
112+
"f'''": single3prog, 'f"""': double3prog,
111113
"b'''": single3prog, 'b"""': double3prog,
112114
"ur'''": single3prog, 'ur"""': double3prog,
113115
"br'''": single3prog, 'br"""': double3prog,
116+
"rb'''": single3prog, 'rb"""': double3prog,
114117
"R'''": single3prog, 'R"""': double3prog,
115118
"U'''": single3prog, 'U"""': double3prog,
119+
"F'''": single3prog, 'F"""': double3prog,
116120
"B'''": single3prog, 'B"""': double3prog,
117121
"uR'''": single3prog, 'uR"""': double3prog,
118122
"Ur'''": single3prog, 'Ur"""': double3prog,
119123
"UR'''": single3prog, 'UR"""': double3prog,
120124
"bR'''": single3prog, 'bR"""': double3prog,
121125
"Br'''": single3prog, 'Br"""': double3prog,
122126
"BR'''": single3prog, 'BR"""': double3prog,
127+
"rB'''": single3prog, 'rB"""': double3prog,
128+
"Rb'''": single3prog, 'Rb"""': double3prog,
129+
"RB'''": single3prog, 'RB"""': double3prog,
123130
'r': None, 'R': None,
124131
'u': None, 'U': None,
132+
'f': None, 'F': None,
125133
'b': None, 'B': None}
126134

127135
triple_quoted = {}
128136
for t in ("'''", '"""',
129137
"r'''", 'r"""', "R'''", 'R"""',
130138
"u'''", 'u"""', "U'''", 'U"""',
139+
"f'''", 'f"""', "F'''", 'F"""',
131140
"b'''", 'b"""', "B'''", 'B"""',
132141
"ur'''", 'ur"""', "Ur'''", 'Ur"""',
133142
"uR'''", 'uR"""', "UR'''", 'UR"""',
134143
"br'''", 'br"""', "Br'''", 'Br"""',
135-
"bR'''", 'bR"""', "BR'''", 'BR"""',):
144+
"bR'''", 'bR"""', "BR'''", 'BR"""',
145+
"rb'''", 'rb"""', "Rb'''", 'Rb"""',
146+
"rB'''", 'rB"""', "RB'''", 'RB"""',):
136147
triple_quoted[t] = t
137148
single_quoted = {}
138149
for t in ("'", '"',
139150
"r'", 'r"', "R'", 'R"',
140151
"u'", 'u"', "U'", 'U"',
152+
"f'", 'f"', "F'", 'F"',
141153
"b'", 'b"', "B'", 'B"',
142154
"ur'", 'ur"', "Ur'", 'Ur"',
143155
"uR'", 'uR"', "UR'", 'UR"',
144156
"br'", 'br"', "Br'", 'Br"',
145-
"bR'", 'bR"', "BR'", 'BR"', ):
157+
"bR'", 'bR"', "BR'", 'BR"',
158+
"rb'", 'rb"', "Rb'", 'Rb"',
159+
"rB'", 'rB"', "RB'", 'RB"',):
146160
single_quoted[t] = t
147161

148162
tabsize = 8

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ Library
2929
exception) to exception(s) raised in the dispatched methods.
3030
Patch by Petr Motejlek.
3131

32+
- bpo-23894: lib2to3 now parses rb'...' and f'...' strings.
33+
3234

3335
What's New in Python 3.6.1 release candidate 1
3436
==============================================

0 commit comments

Comments
 (0)