@@ -4274,49 +4274,32 @@ fstring_compile_expr(const char *expr_start, const char *expr_end,
4274
4274
struct compiling * c , const node * n )
4275
4275
4276
4276
{
4277
- int all_whitespace = 1 ;
4278
- int kind ;
4279
- void * data ;
4280
4277
PyCompilerFlags cf ;
4281
4278
mod_ty mod ;
4282
4279
char * str ;
4283
- PyObject * o ;
4284
4280
Py_ssize_t len ;
4285
- Py_ssize_t i ;
4281
+ const char * s ;
4286
4282
4287
4283
assert (expr_end >= expr_start );
4288
4284
assert (* (expr_start - 1 ) == '{' );
4289
4285
assert (* expr_end == '}' || * expr_end == '!' || * expr_end == ':' );
4290
4286
4291
- /* We know there are no escapes here, because backslashes are not allowed,
4292
- and we know it's utf-8 encoded (per PEP 263). But, in order to check
4293
- that each char is not whitespace, we need to decode it to unicode.
4294
- Which is unfortunate, but such is life. */
4295
-
4296
4287
/* If the substring is all whitespace, it's an error. We need to catch
4297
4288
this here, and not when we call PyParser_ASTFromString, because turning
4298
4289
the expression '' in to '()' would go from being invalid to valid. */
4299
- /* Note that this code says an empty string is all whitespace. That's
4300
- important. There's a test for it: f'{}'. */
4301
- o = PyUnicode_DecodeUTF8 (expr_start , expr_end - expr_start , NULL );
4302
- if (o == NULL )
4303
- return NULL ;
4304
- len = PyUnicode_GET_LENGTH (o );
4305
- kind = PyUnicode_KIND (o );
4306
- data = PyUnicode_DATA (o );
4307
- for (i = 0 ; i < len ; i ++ ) {
4308
- if (!Py_UNICODE_ISSPACE (PyUnicode_READ (kind , data , i ))) {
4309
- all_whitespace = 0 ;
4290
+ for (s = expr_start ; s != expr_end ; s ++ ) {
4291
+ char c = * s ;
4292
+ /* The Python parser ignores only the following whitespace
4293
+ characters (\r already is converted to \n). */
4294
+ if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f' )) {
4310
4295
break ;
4311
4296
}
4312
4297
}
4313
- Py_DECREF (o );
4314
- if (all_whitespace ) {
4298
+ if (s == expr_end ) {
4315
4299
ast_error (c , n , "f-string: empty expression not allowed" );
4316
4300
return NULL ;
4317
4301
}
4318
4302
4319
- /* Reuse len to be the length of the utf-8 input string. */
4320
4303
len = expr_end - expr_start ;
4321
4304
/* Allocate 3 extra bytes: open paren, close paren, null byte. */
4322
4305
str = PyMem_RawMalloc (len + 3 );
0 commit comments