-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ast.literal_eval does not accept strings with leading whitespaces #86053
Comments
|
Looks like skipping the leading whitespace is an undocumented behavior for eval() introduced back in 1992 (f08ab0a). I don't think that bringing it to the literal_eval is a good idea, at least after this much of a time. If so, both of them should be explicitly documented or at least mentioned in the source code (to avoid confusion). |
Hm, I'm not sure. ast.literal_eval() does accept trailing whitespace, and embedded whitespace.
So I think it should start accepting leading whitespace too (but only in a feature release, so 3.10). |
The doc for literal_eval says "evaluate ... a string containing a Python literal or container display." To me, ' 1' qualifies, just as it does as an expression for eval(). The exception comes from parsing raising IndentationError with leading whitespace even when the mode is 'eval' rather than 'exec'. This surprised me. Eval() gets strips the beginning of the string before it is parsed. If parsing remains as is, I agree that doing the same for literal_eval strings. But why should not parsing remove indents for 'eval' mode? |
[Terry]
I dunno, but it's been doing this since 1992, so I think it would be fragile to change. The best thing therefore is to make ast.literal_eval() match it exactly. |
+1 We had considerable finicky behaviour in the parser related to new lines and whitespace so I would recommend to strive for stability. |
Closing. Let’s not backport. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: