Skip to content

bpo-31778: Make ast.literal_eval() more strict.#4035

Merged
serhiy-storchaka merged 4 commits intopython:masterfrom
serhiy-storchaka:strict-literal_eval
Jan 4, 2018
Merged

bpo-31778: Make ast.literal_eval() more strict.#4035
serhiy-storchaka merged 4 commits intopython:masterfrom
serhiy-storchaka:strict-literal_eval

Conversation

@serhiy-storchaka
Copy link
Copy Markdown
Member

@serhiy-storchaka serhiy-storchaka commented Oct 18, 2017

Addition and subtraction of arbitrary numbers no longer allowed.

https://bugs.python.org/issue31778

Addition and subtraction of arbitrary numbers no longer allowed.
@serhiy-storchaka serhiy-storchaka added the type-bug An unexpected behavior, bug, or error label Oct 18, 2017
Copy link
Copy Markdown

@dbieber dbieber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me. Two test suggestions inline.

I'm the one who filed https://bugs.python.org/issue31778, but I'm not a Python contributor, so presumably someone else needs to review this too.

Comment thread Lib/ast.py
right = _convert(node.right)
if isinstance(left, _NUM_TYPES) and isinstance(right, _NUM_TYPES):
left = _convert_signed_num(node.left)
right = _convert_num(node.right)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps it would be useful to add to the test assertRaises(ValueError, ast.literal_eval, '3 + (0 + 2j)') to catch unintended behavior changes to _convert_num in future edits.

Comment thread Lib/test/test_ast.py
self.assertEqual(ast.literal_eval('+6'), 6)
self.assertEqual(ast.literal_eval('-6'), -6)
self.assertEqual(ast.literal_eval('-6j+3'), 3-6j)
self.assertEqual(ast.literal_eval('3.25'), 3.25)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps it would also be useful to assertRaises(ValueError for an expression such as 1j+2j or 1+2j+3j.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type-bug An unexpected behavior, bug, or error

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants