Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def _convert(node):
elif isinstance(node, BinOp) and isinstance(node.op, (Add, Sub)):
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
Copy Markdown
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
Copy Markdown
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.

if isinstance(node.op, Add):
return left + right
else:
Expand Down
6 changes: 6 additions & 0 deletions Lib/test/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,7 @@ def test_literal_eval_complex(self):
self.assertRaises(ValueError, ast.literal_eval, '3+-6j')
self.assertRaises(ValueError, ast.literal_eval, '3+(0+6j)')
self.assertRaises(ValueError, ast.literal_eval, '-(3+6j)')
self.assertRaises(ValueError, ast.literal_eval, 'True+6j')

def test_bad_integer(self):
# issue13436: Bad error message with invalid numeric values
Expand Down Expand Up @@ -1297,6 +1298,11 @@ def test_literal_eval(self):

self.assertEqual(ast.literal_eval(binop), 10+20j)

new_left = ast.Constant(value=True)
ast.copy_location(new_left, binop.left)
binop.left = new_left
self.assertRaises(ValueError, ast.literal_eval, binop)

def test_string_kind(self):
c = ast.parse('"x"', mode='eval').body
self.assertEqual(c.value, "x")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ast.literal_eval() no longer accepts booleans as numbers when pass AST

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ah, I see.

argument.