caesar0301 / treelib

An efficient implementation of tree data structure in python 2/3.
http://treelib.readthedocs.io/en/latest/
Other
801 stars 185 forks source link

two issues with deepcopy in Tree.paste #83

Closed chasezheng closed 5 years ago

chasezheng commented 6 years ago

https://github.com/caesar0301/treelib/blob/1200ec1d7a1c5cda4508dddbfcc0bc13545ff575/treelib/tree.py#L535

  1. node is already an identifier, and so node.identifier raises an error
  2. deepcopy is a boolean and not callable
neverfox commented 6 years ago

Yeah, the argument should just be called something like deep.

neverfox commented 6 years ago

There's another issue I found after fixing the other two:

~/.pyenv/versions/ling571/lib/python3.4/site-packages/treelib/tree.py in paste(self, nid, new_tree, deep)
    516             self._nodes.update(new_tree._nodes)
    517         self.__update_fpointer(nid, new_tree.root, Node.ADD)
--> 518         self.__update_bpointer(new_tree.root, nid)
    519 
    520     def paths_to_leaves(self):

~/.pyenv/versions/ling571/lib/python3.4/site-packages/treelib/tree.py in __update_bpointer(self, nid, parent_id)
    226     def __update_bpointer(self, nid, parent_id):
    227         """set self[nid].bpointer"""
--> 228         self[nid].update_bpointer(parent_id)
    229 
    230     def __update_fpointer(self, nid, child_id, mode):

AttributeError: 'str' object has no attribute 'update_bpointer'

It was deep copying the string id not the node. I believe the PR fixes that as well. The code moves between node and nid names so often, it's easy to get confused about what you're dealing with at any given moment.