Repository navigation
Reject non-literal binary operations at any AST depth #715
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -136,5 +136,26 @@ def testDefaultParseValueIgnoreBinOp(self): | |||||||||
| self.assertEqual(parser.DefaultParseValue('2017-10-10'), '2017-10-10') | ||||||||||
| self.assertEqual(parser.DefaultParseValue('1+1'), '1+1') | ||||||||||
|
|
||||||||||
| def testDefaultParseValueIgnoreNestedBinOp(self): | ||||||||||
| # Binary operations are not literals, wherever they appear. | ||||||||||
| self.assertEqual(parser.DefaultParseValue('[1+1]'), '[1+1]') | ||||||||||
| self.assertEqual(parser.DefaultParseValue('{key: 1+1}'), '{key: 1+1}') | ||||||||||
| self.assertEqual(parser.DefaultParseValue('(1+1,)'), '(1+1,)') | ||||||||||
| self.assertEqual(parser.DefaultParseValue('[[2017-10-10]]'), | ||||||||||
| '[[2017-10-10]]') | ||||||||||
| self.assertEqual(parser.DefaultParseValue('{1: [a*b]}'), '{1: [a*b]}') | ||||||||||
| self.assertEqual(parser.DefaultParseValue('1+1+2j'), '1+1+2j') | ||||||||||
|
|
||||||||||
| def testDefaultParseValueComplexNumbers(self): | ||||||||||
| # The + and - of a complex number are literals, and are still parsed. | ||||||||||
| self.assertEqual(parser.DefaultParseValue('2j'), 2j) | ||||||||||
| self.assertEqual(parser.DefaultParseValue('1+2j'), 1 + 2j) | ||||||||||
| self.assertEqual(parser.DefaultParseValue('-1.5-2j'), -1.5 - 2j) | ||||||||||
| self.assertEqual(parser.DefaultParseValue('[1+2j]'), [1 + 2j]) | ||||||||||
| self.assertEqual(parser.DefaultParseValue('{alpha: 1+2j}'), | ||||||||||
| {'alpha': 1 + 2j}) | ||||||||||
| # The real part must come first, as it must for ast.literal_eval. | ||||||||||
| self.assertEqual(parser.DefaultParseValue('2j+1'), '2j+1') | ||||||||||
|
Comment on lines
+157
to
+158
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we updated
Suggested change
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm keeping this assertion. The suggested assertion would fail even with the suggested |
||||||||||
|
|
||||||||||
| if __name__ == '__main__': | ||||||||||
| testutils.main() | ||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation of
_IsComplexLiteralhas two issues:ast.literal_evalonnode.leftandnode.rightwithout checking their types first. If either operand is a large container (e.g., a list or dictionary with thousands of elements),ast.literal_evalwill fully evaluate it, leading to unnecessary overhead and potential performance degradation.1+2j). However, Python'sast.literal_evalnatively supports complex numbers with the imaginary part first (e.g.,2j+1).We can resolve both issues by:
_IsNumericNodeto verify that both operands are simple numeric literals (or unary operations on them) before callingast.literal_eval.real + imaginaryandimaginary + real) to fully support standard Python complex literals.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Checked both points against CPython. I'm keeping this code as is.
Imaginary part first:
ast.literal_evaldoes not accept it. Its BinOp branch isleft = _convert_signed_num(node.left),right = _convert_num(node.right), thenisinstance(left, (int, float)) and isinstance(right, complex). That code is the same inLib/ast.pyfrom 3.7.17 through 3.14.0, and main and 2.7.18 have the same rule.3.8.20, 3.9.6, 3.10.19, 3.11.15, 3.12.8 and 3.13.3 raise the same ValueError for
2j+1and2j-1. All of them accept1+2jand-1-2j. Also,_LiteralEvalends withast.literal_eval(root), so accepting2j+1here would change nothing: with the suggested_IsComplexLiteralapplied,DefaultParseValue('2j+1')still returns'2j+1'.Performance: a container operand does reach
ast.literal_evalhere, e.g.[0,1,...]+1j. But the walk raises at the first rejected BinOp, so at most one operand gets evaluated. A nested BinOp operand fails insideliteral_evalbefore anything is evaluated. With 50 such BinOps in one argument, a single list is evaluated. For a 108,894-byte argument, which is close to Linux's 128 KiB per-argument limit,_IsComplexLiteraltakes about 1 ms. Theast.parsecall before it takes 13 to 17 ms (3.8, 3.12, 3.14).A node-type pre-check would also need
ast.Numon 3.7, because the 3.7 parser does not emitast.Constant(the same reason_StrNodeexists). And the suggestedast.Numlookup emits a DeprecationWarning on 3.12 and 3.13. For about 1 ms on input that gets rejected anyway, I don't think the extra code is worth it.