Skip to content
This repository has been archived by the owner on Apr 24, 2023. It is now read-only.

Commit

Permalink
BUGFIX: correct deepcopy() for tree in tree
Browse files Browse the repository at this point in the history
  • Loading branch information
mozman committed Feb 4, 2017
1 parent fc582d5 commit 2c05277
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
4 changes: 4 additions & 0 deletions NEWS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ NEWS

bintrees development stopped, use `sortedcontainers` instead: https://pypi.python.org/pypi/sortedcontainers

Version 2.0.6 - 2017-02-04

* BUGFIX: correct deepcopy() for tree in tree

Version 2.0.5 - 2017-02-04

* support for copy.deepcopy()
Expand Down
2 changes: 1 addition & 1 deletion bintrees/abctree.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def copy(self):
def __deepcopy__(self, memo):
"""copy.deepcopy(T) -> get a deep copy of T."""
def _deepcopy(key, value):
value_copy = deepcopy(value)
value_copy = deepcopy(value, memo)
tree.insert(key, value_copy)

tree = type(self)()
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def read(fname):

setup(
name='bintrees',
version='2.0.5',
version='2.0.6',
description='Package provides Binary-, RedBlack- and AVL-Trees in Python and Cython.',
author='mozman',
url='https://github.com/mozman/bintrees.git',
Expand Down
35 changes: 31 additions & 4 deletions tests/test_all_trees.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
# Created: 28.04.2010
# Copyright (c) 2010-2017 by Manfred Moitzi
# License: MIT License

import sys
PYPY = hasattr(sys, 'pypy_version_info')

import unittest
import pickle

from copy import deepcopy
from random import randint, shuffle

from bintrees import BinaryTree, AVLTree, RBTree, has_fast_tree_support
Expand Down Expand Up @@ -799,7 +798,6 @@ def collect(key, value):
self.assertEqual(list(tree.keys()), list(sorted(keys)))

def test_099_deepcopy(self):
from copy import deepcopy
data = {
2: 2,
3: 3,
Expand All @@ -816,7 +814,6 @@ def test_099_deepcopy(self):
self.assertEqual(tree[1][-1], 9)

def test_100_deepcopy_tree_in_container(self):
from copy import deepcopy
data = {
2: 2,
3: 3,
Expand All @@ -841,6 +838,36 @@ def test_100_deepcopy_tree_in_container(self):
self.assertTrue(17 in treecopy3)
self.assertFalse(17 in tree)

def test_101_deepcopy_tree_in_tree(self):
data1 = {
2: 2,
3: 3,
1: list(range(10)),
4: 4,
5: 5,
}
data2 = {
7: 7,
3: 3,
5: list(range(10)),
4: 4,
9: 9,
}
tree1 = self.TREE_CLASS(data1)
tree2 = self.TREE_CLASS(data2)
tree1[6] = tree2 # 21
tree1[8] = tree2 # 22
tree1[1][5] = tree2 # 23
copytree1 = deepcopy(tree1)
copytree21 = copytree1[6]
copytree22 = copytree1[8]
copytree23 = copytree1[1][5]
self.assertNotEqual(id(tree2), id(copytree21))
self.assertEqual(id(copytree21), id(copytree22)) # copied only once?
self.assertEqual(id(copytree21), id(copytree23)) # tree in sublist, copied only once?
self.assertEqual(id(copytree21[5]), id(copytree22[5])) # sublist copied only once?
self.assertEqual(id(copytree21[5]), id(copytree23[5])) # sublist copied only once?


class TestBinaryTree(CheckTree, unittest.TestCase):
TREE_CLASS = BinaryTree
Expand Down

0 comments on commit 2c05277

Please sign in to comment.