| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent b319d09 commit 570b1c9
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -280,6 +280,10 @@ def test_missing_expression(self): | |||
| 280 | 280 | "f'{10:{ }}'", | |
| 281 | 281 | "f' { } '", | |
| 282 | 282 | ||
| 283 | + # The Python parser ignores also the following | ||
| 284 | + # whitespace characters in additional to a space. | ||
| 285 | + "f'''{\t\f\r\n}'''", | ||
| 286 | + | ||
| 283 | 287 | # Catch the empty expression before the | |
| 284 | 288 | # invalid conversion. | |
| 285 | 289 | "f'{!x}'", | |
@@ -300,6 +304,12 @@ def test_missing_expression(self): | |||
| 300 | 304 | "f'{:x'", | |
| 301 | 305 | ]) | |
| 302 | 306 | ||
| 307 | + # Different error message is raised for other whitespace characters. | ||
| 308 | + self.assertAllRaise(SyntaxError, 'invalid character in identifier', | ||
| 309 | + ["f'''{\xa0}'''", | ||
| 310 | + "\xa0", | ||
| 311 | + ]) | ||
| 312 | + | ||
| 303 | 313 | def test_parens_in_expressions(self): | |
| 304 | 314 | self.assertEqual(f'{3,}', '(3,)') | |
| 305 | 315 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4247,49 +4247,32 @@ fstring_compile_expr(const char *expr_start, const char *expr_end, | |||
| 4247 | 4247 | struct compiling *c, const node *n) | |
| 4248 | 4248 | ||
| 4249 | 4249 | { | |
| 4250 | - int all_whitespace = 1; | ||
| 4251 | - int kind; | ||
| 4252 | - void *data; | ||
| 4253 | 4250 | PyCompilerFlags cf; | |
| 4254 | 4251 | mod_ty mod; | |
| 4255 | 4252 | char *str; | |
| 4256 | - PyObject *o; | ||
| 4257 | 4253 | Py_ssize_t len; | |
| 4258 | - Py_ssize_t i; | ||
| 4254 | + const char *s; | ||
| 4259 | 4255 | ||
| 4260 | 4256 | assert(expr_end >= expr_start); | |
| 4261 | 4257 | assert(*(expr_start-1) == '{'); | |
| 4262 | 4258 | assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':'); | |
| 4263 | 4259 | ||
| 4264 | - /* We know there are no escapes here, because backslashes are not allowed, | ||
| 4265 | - and we know it's utf-8 encoded (per PEP 263). But, in order to check | ||
| 4266 | - that each char is not whitespace, we need to decode it to unicode. | ||
| 4267 | - Which is unfortunate, but such is life. */ | ||
| 4268 | - | ||
| 4269 | 4260 | /* If the substring is all whitespace, it's an error. We need to catch | |
| 4270 | 4261 | this here, and not when we call PyParser_ASTFromString, because turning | |
| 4271 | 4262 | the expression '' in to '()' would go from being invalid to valid. */ | |
| 4272 | - /* Note that this code says an empty string is all whitespace. That's | ||
| 4273 | - important. There's a test for it: f'{}'. */ | ||
| 4274 | - o = PyUnicode_DecodeUTF8(expr_start, expr_end-expr_start, NULL); | ||
| 4275 | - if (o == NULL) | ||
| 4276 | - return NULL; | ||
| 4277 | - len = PyUnicode_GET_LENGTH(o); | ||
| 4278 | - kind = PyUnicode_KIND(o); | ||
| 4279 | - data = PyUnicode_DATA(o); | ||
| 4280 | - for (i = 0; i < len; i++) { | ||
| 4281 | - if (!Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) { | ||
| 4282 | - all_whitespace = 0; | ||
| 4263 | + for (s = expr_start; s != expr_end; s++) { | ||
| 4264 | + char c = *s; | ||
| 4265 | + /* The Python parser ignores only the following whitespace | ||
| 4266 | + characters (\r already is converted to \n). */ | ||
| 4267 | + if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) { | ||
| 4283 | 4268 | break; | |
| 4284 | 4269 | } | |
| 4285 | 4270 | } | |
| 4286 | - Py_DECREF(o); | ||
| 4287 | - if (all_whitespace) { | ||
| 4271 | + if (s == expr_end) { | ||
| 4288 | 4272 | ast_error(c, n, "f-string: empty expression not allowed"); | |
| 4289 | 4273 | return NULL; | |
| 4290 | 4274 | } | |
| 4291 | 4275 | ||
| 4292 | - /* Reuse len to be the length of the utf-8 input string. */ | ||
| 4293 | 4276 | len = expr_end - expr_start; | |
| 4294 | 4277 | /* Allocate 3 extra bytes: open paren, close paren, null byte. */ | |
| 4295 | 4278 | str = PyMem_RawMalloc(len + 3); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments