Skip to content

Commit

Permalink
Avoid setting children's parent twice
Browse files Browse the repository at this point in the history
  • Loading branch information
gousaiyang committed May 25, 2021
1 parent d82f0c5 commit 328c5cb
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
8 changes: 3 additions & 5 deletions parso/parser.py
Expand Up @@ -23,7 +23,7 @@
complexity of the ``Parser`` (there's another parser sitting inside
``Statement``, which produces ``Array`` and ``Call``).
"""
from typing import Dict
from typing import Dict, Type

from parso import tree
from parso.pgen2.generator import ReservedString
Expand Down Expand Up @@ -110,10 +110,10 @@ class BaseParser:
When a syntax error occurs, error_recovery() is called.
"""

node_map: Dict[str, type] = {}
node_map: Dict[str, Type[tree.BaseNode]] = {}
default_node = tree.Node

leaf_map: Dict[str, type] = {}
leaf_map: Dict[str, Type[tree.Leaf]] = {}
default_leaf = tree.Leaf

def __init__(self, pgen_grammar, start_nonterminal='file_input', error_recovery=False):
Expand Down Expand Up @@ -156,8 +156,6 @@ def convert_node(self, nonterminal, children):
node = self.node_map[nonterminal](children)
except KeyError:
node = self.default_node(nonterminal, children)
for c in children:
c.parent = node
return node

def convert_leaf(self, type_, value, prefix, start_pos):
Expand Down
4 changes: 0 additions & 4 deletions parso/python/parser.py
Expand Up @@ -96,8 +96,6 @@ def convert_node(self, nonterminal, children):
# prefixes. Just ignore them.
children = [children[0]] + children[2:-1]
node = self.default_node(nonterminal, children)
for c in children:
c.parent = node
return node

def convert_leaf(self, type, value, prefix, start_pos):
Expand Down Expand Up @@ -185,8 +183,6 @@ def _stack_removal(self, start_index):

if all_nodes:
node = tree.PythonErrorNode(all_nodes)
for n in all_nodes:
n.parent = node
self.stack[start_index - 1].nodes.append(node)

self.stack[start_index:] = []
Expand Down
6 changes: 0 additions & 6 deletions parso/python/tree.py
Expand Up @@ -553,8 +553,6 @@ def __init__(self, children):
for child in parameters_children:
if isinstance(child, Param):
input_has_param = True
# Fix parent relationship of Param children.
child.parent = parameters
# If input parameters list already has Param objects, keep it as is;
# otherwise, convert it to a list of Param objects.
if not input_has_param:
Expand Down Expand Up @@ -666,8 +664,6 @@ def __init__(self, children):
for child in parameters_children:
if isinstance(child, Param):
input_has_param = True
# Fix parent relationship of Param children.
child.parent = self
# If input children list already has Param objects, keep it as is;
# otherwise, convert it to a list of Param objects.
if not input_has_param:
Expand Down Expand Up @@ -1102,8 +1098,6 @@ class Param(PythonBaseNode):
def __init__(self, children, parent=None):
super().__init__(children)
self.parent = parent
for child in children:
child.parent = self

@property
def star_count(self):
Expand Down
17 changes: 17 additions & 0 deletions test/test_dump_tree.py
Expand Up @@ -163,3 +163,20 @@ def test_dump_parser_tree_invalid_args():

with pytest.raises(TypeError):
module.dump(indent=1.1)


def test_eval_dump_recovers_parent():
module = parse("lambda x, y: x + y")
module2 = eval(module.dump())
assert module2.parent is None
lambda_node = module2.children[0]
assert lambda_node.parent is module2
assert module2.children[1].parent is module2
assert lambda_node.children[0].parent is lambda_node
param_node = lambda_node.children[1]
assert param_node.parent is lambda_node
assert param_node.children[0].parent is param_node
assert param_node.children[1].parent is param_node
arith_expr_node = lambda_node.children[-1]
assert arith_expr_node.parent is lambda_node
assert arith_expr_node.children[0].parent is arith_expr_node

0 comments on commit 328c5cb

Please sign in to comment.