Skip to content

Commit 40bc05e

Browse files
klardotshjepler
authored andcommitted
Address dpgeorge feedback - largely simplifications
1 parent 3a7a5ba commit 40bc05e

File tree

3 files changed

+21
-30
lines changed

3 files changed

+21
-30
lines changed

py/lexer.c

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -129,29 +129,21 @@ STATIC bool is_tail_of_identifier(mp_lexer_t *lex) {
129129

130130
STATIC void swap_char_banks(mp_lexer_t *lex) {
131131
if (lex->vstr_postfix_processing) {
132-
unichar h0, h1, h2;
133-
134-
h0 = lex->chr0;
135-
h1 = lex->chr1;
136-
h2 = lex->chr2;
137-
138-
lex->chr0 = lex->vstr_postfix.len > 0 ? lex->vstr_postfix.buf[0] : 0;
139-
lex->chr1 = lex->vstr_postfix.len > 1 ? lex->vstr_postfix.buf[1] : 0;
140-
lex->chr2 = lex->vstr_postfix.len > 2 ? lex->vstr_postfix.buf[2] : 0;
141-
lex->chr3 = h0;
142-
lex->chr4 = h1;
143-
lex->chr5 = h2;
144-
145-
lex->vstr_postfix_idx = lex->vstr_postfix.len > 2 ? 3 : lex->vstr_postfix.len;
132+
lex->chr3 = lex->chr0;
133+
lex->chr4 = lex->chr1;
134+
lex->chr5 = lex->chr2;
135+
lex->chr0 = lex->vstr_postfix.buf[0];
136+
lex->chr1 = lex->vstr_postfix.buf[1];
137+
lex->chr2 = lex->vstr_postfix.buf[2];
138+
139+
lex->vstr_postfix_idx = 3;
146140
} else {
147141
// blindly reset to the "backup" bank when done postfix processing
148142
// this restores control to the mp_reader
149143
lex->chr0 = lex->chr3;
150144
lex->chr1 = lex->chr4;
151145
lex->chr2 = lex->chr5;
152-
lex->chr3 = 0;
153-
lex->chr4 = 0;
154-
lex->chr5 = 0;
146+
// willfully ignoring setting chr3-5 here - WARNING consider those garbage data now
155147

156148
vstr_reset(&lex->vstr_postfix);
157149
lex->vstr_postfix_idx = 0;

py/parse.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,6 +1178,7 @@ mp_parse_tree_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) {
11781178
exc = mp_obj_new_exception_msg(&mp_type_IndentationError,
11791179
translate("unindent does not match any outer indentation level"));
11801180
break;
1181+
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED
11811182
case MP_TOKEN_FSTRING_BACKSLASH:
11821183
exc = mp_obj_new_exception_msg(&mp_type_SyntaxError,
11831184
translate("f-string expression part cannot include a backslash"));
@@ -1202,6 +1203,17 @@ mp_parse_tree_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) {
12021203
exc = mp_obj_new_exception_msg(&mp_type_NotImplementedError,
12031204
translate("raw f-strings are not implemented"));
12041205
break;
1206+
#else
1207+
case MP_TOKEN_FSTRING_BACKSLASH:
1208+
case MP_TOKEN_FSTRING_COMMENT:
1209+
case MP_TOKEN_FSTRING_UNCLOSED:
1210+
case MP_TOKEN_FSTRING_UNOPENED:
1211+
case MP_TOKEN_FSTRING_EMPTY_EXP:
1212+
case MP_TOKEN_FSTRING_RAW:
1213+
exc = mp_obj_new_exception_msg(&mp_type_SyntaxError,
1214+
translate("malformed f-string"));
1215+
break;
1216+
#endif
12051217
default:
12061218
exc = mp_obj_new_exception_msg(&mp_type_SyntaxError,
12071219
translate("invalid syntax"));

tests/basics/string_pep498_fstring.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,3 @@ def foo():
111111

112112
# Other tests
113113
assert f'{{{4*10}}}' == '{40}'
114-
115-
try:
116-
eval("fr''")
117-
except NotImplementedError:
118-
pass
119-
else:
120-
raise RuntimeError('expected raw f-string to raise NotImplementedError')
121-
try:
122-
eval("rf''")
123-
except NotImplementedError:
124-
pass
125-
else:
126-
raise RuntimeError('expected raw f-string to raise NotImplementedError')

0 commit comments

Comments
 (0)