-
Notifications
You must be signed in to change notification settings - Fork 249
Starred tuple assignment #150
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
Merged
KevinHock
merged 4 commits into
python-security:master
from
bcaller:starred-tuple-assign
Jul 24, 2018
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| a, *b, c, d, e = f, *g, *h, f + i, j |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -327,28 +327,59 @@ def visit_Try(self, node): | |
| return ControlFlowNode(try_node, last_statements, break_statements=body.break_statements) | ||
|
|
||
| def assign_tuple_target(self, node, right_hand_side_variables): | ||
| new_assignment_nodes = list() | ||
| for i, target in enumerate(node.targets[0].elts): | ||
| value = node.value.elts[i] | ||
| new_assignment_nodes = [] | ||
| remaining_variables = list(right_hand_side_variables) | ||
| remaining_targets = list(node.targets[0].elts) | ||
| remaining_values = list(node.value.elts) # May contain duplicates | ||
|
|
||
| def visit(target, value): | ||
| label = LabelVisitor() | ||
| label.visit(target) | ||
|
|
||
| rhs_visitor = RHSVisitor() | ||
| rhs_visitor.visit(value) | ||
| if isinstance(value, ast.Call): | ||
| new_ast_node = ast.Assign(target, value) | ||
| new_ast_node.lineno = node.lineno | ||
|
|
||
| ast.copy_location(new_ast_node, node) | ||
| new_assignment_nodes.append(self.assignment_call_node(label.result, new_ast_node)) | ||
|
|
||
| else: | ||
| label.result += ' = ' | ||
| label.visit(value) | ||
|
|
||
| new_assignment_nodes.append(self.append_node(AssignmentNode( | ||
| label.result, | ||
| extract_left_hand_side(target), | ||
| ast.Assign(target, value), | ||
| right_hand_side_variables, | ||
| rhs_visitor.result, | ||
| line_number=node.lineno, | ||
| path=self.filenames[-1] | ||
| ))) | ||
| remaining_targets.remove(target) | ||
| remaining_values.remove(value) | ||
| for var in rhs_visitor.result: | ||
| remaining_variables.remove(var) | ||
|
|
||
| # Pair targets and values until a Starred node is reached | ||
|
Collaborator
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. Woah nice 😮 |
||
| for target, value in zip(node.targets[0].elts, node.value.elts): | ||
| if isinstance(target, ast.Starred) or isinstance(value, ast.Starred): | ||
| break | ||
| visit(target, value) | ||
|
|
||
| # If there was a Starred node, pair remaining targets and values from the end | ||
| for target, value in zip(reversed(list(remaining_targets)), reversed(list(remaining_values))): | ||
| if isinstance(target, ast.Starred) or isinstance(value, ast.Starred): | ||
| break | ||
| visit(target, value) | ||
|
|
||
| if remaining_targets: | ||
| label = LabelVisitor() | ||
| label.handle_comma_separated(remaining_targets) | ||
| label.result += ' = ' | ||
| label.handle_comma_separated(remaining_values) | ||
| for target in remaining_targets: | ||
| new_assignment_nodes.append(self.append_node(AssignmentNode( | ||
| label.result, | ||
| extract_left_hand_side(target), | ||
| ast.Assign(target, remaining_values[0]), | ||
| remaining_variables, | ||
| line_number=node.lineno, | ||
| path=self.filenames[-1] | ||
| ))) | ||
|
|
@@ -380,8 +411,8 @@ def assign_multi_target(self, node, right_hand_side_variables): | |
| def visit_Assign(self, node): | ||
| rhs_visitor = RHSVisitor() | ||
| rhs_visitor.visit(node.value) | ||
| if isinstance(node.targets[0], ast.Tuple): # x,y = [1,2] | ||
| if isinstance(node.value, ast.Tuple): | ||
| if isinstance(node.targets[0], (ast.Tuple, ast.List)): # x,y = [1,2] | ||
| if isinstance(node.value, (ast.Tuple, ast.List)): | ||
| return self.assign_tuple_target(node, rhs_visitor.result) | ||
| elif isinstance(node.value, ast.Call): | ||
| call = None | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| import ast | ||
|
|
||
| from .cfg_base_test_case import CFGBaseTestCase | ||
|
|
||
| from pyt.core.node_types import ( | ||
|
|
@@ -779,6 +781,45 @@ def test_assignment_tuple_value(self): | |
|
|
||
| self.assertEqual(self.cfg.nodes[node].label, 'a = (x, y)') | ||
|
|
||
| def test_assignment_starred(self): | ||
| self.cfg_create_from_file('examples/example_inputs/assignment_starred.py') | ||
|
|
||
| middle_nodes = self.cfg.nodes[1:-1] | ||
| self.assert_length(middle_nodes, expected_length=5) | ||
|
|
||
| visited = [self.cfg.nodes[0]] | ||
| while True: | ||
| current_node = visited[-1] | ||
| if len(current_node.outgoing) != 1: | ||
| break | ||
| visited.append(current_node.outgoing[0]) | ||
| self.assertCountEqual(self.cfg.nodes, visited, msg="Did not complete a path from Entry to Exit") | ||
|
|
||
| self.assertEqual(middle_nodes[0].label, 'a = f') | ||
| self.assertCountEqual( # We don't assert a specific order for the assignment nodes | ||
| [n.label for n in middle_nodes], | ||
| ['a = f', 'd = f + i', 'e = j'] + ['*b, c = *g, *h'] * 2, | ||
| ) | ||
| self.assertCountEqual( | ||
| [(n.left_hand_side, n.right_hand_side_variables) for n in middle_nodes], | ||
| [('a', ['f']), ('b', ['g', 'h']), ('c', ['g', 'h']), ('d', ['f', 'i']), ('e', ['j'])], | ||
| ) | ||
|
|
||
|
Collaborator
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. Possible pep8 nit: w/r/t one line between methods of a class. |
||
| def test_assignment_starred_list(self): | ||
| self.cfg_create_from_ast(ast.parse('[a, b, c] = *d, e')) | ||
|
|
||
| middle_nodes = self.cfg.nodes[1:-1] | ||
| self.assert_length(middle_nodes, expected_length=3) | ||
|
|
||
| self.assertCountEqual( | ||
| [n.label for n in middle_nodes], | ||
| ['a, b = *d', 'a, b = *d', 'c = e'], | ||
| ) | ||
| self.assertCountEqual( | ||
| [(n.left_hand_side, n.right_hand_side_variables) for n in middle_nodes], | ||
| [('a', ['d']), ('b', ['d']), ('c', ['e'])], | ||
| ) | ||
|
|
||
|
|
||
| class CFGComprehensionTest(CFGBaseTestCase): | ||
| def test_nodes(self): | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Nice, that's awesome.