Skip to content

bpo-32893: ast.literal_eval() no longer accepts booleans as numbers in AST.#5798

Closed
serhiy-storchaka wants to merge 4 commits intopython:masterfrom
serhiy-storchaka:ast-literal_eval-bool
Closed

bpo-32893: ast.literal_eval() no longer accepts booleans as numbers in AST.#5798
serhiy-storchaka wants to merge 4 commits intopython:masterfrom
serhiy-storchaka:ast-literal_eval-bool

Conversation

@serhiy-storchaka
Copy link
Member

@serhiy-storchaka serhiy-storchaka commented Feb 21, 2018

left = _convert_signed_num(node.left)
right = _convert_num(node.right)
if isinstance(left, (int, float)) and isinstance(right, complex):
if type(left) in (int, float) and type(right) is complex:
Copy link
Member

Choose a reason for hiding this comment

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

I don't think it affects much but looks better to me

Suggested change
if type(left) in (int, float) and type(right) is complex:
if type(left) in {int, float} and type(right) is complex:

Copy link
Member Author

Choose a reason for hiding this comment

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

It is faster to create a tuple than a set.

@@ -0,0 +1,2 @@
ast.literal_eval() no longer accepts booleans as numbers when pass AST
Copy link
Member

Choose a reason for hiding this comment

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

Is there a need for when pass AST argument. part? You can give argument as string (like ast.literal_eval("True+6j") and still get ValueError.

Copy link
Member Author

Choose a reason for hiding this comment

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

You get ValueError in ast.literal_eval("True+6j"). This PR only changes the behavior of ast.literal_eval() when pass AST argument.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I see.

@isidentical
Copy link
Member

@serhiy-storchaka does this PR still relevant to master (and 3.8)? I couldn't reproduce the examples you gave on tracker or the tests here. They all raise error as intended.

@serhiy-storchaka
Copy link
Member Author

Indeed, it is no longer reproduced.

@serhiy-storchaka serhiy-storchaka deleted the ast-literal_eval-bool branch May 16, 2020 19:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants