Skip to content

Commit f6a9710

Browse files
authored
Merge pull request #7303 from biolab/tree-rec-limit
Print "..." in deep subtrees of a Tree instead of raising RecursionError
2 parents 04cfbe4 + 22b58a4 commit f6a9710

3 files changed

Lines changed: 33 additions & 2 deletions

File tree

Orange/clustering/hierarchical.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,11 @@ def __iter__(self):
147147
return iter((self.__value, self.__branches))
148148

149149
def __repr__(self):
150-
return ("{0.__name__}(value={1!r}, branches={2!r})"
151-
.format(type(self), self.value, self.branches))
150+
try:
151+
return ("{0.__name__}(value={1!r}, branches={2!r})"
152+
.format(type(self), self.value, self.branches))
153+
except RecursionError:
154+
return ("{0.__name__}(...)".format(type(self)))
152155

153156
@property
154157
def is_leaf(self):
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import unittest
2+
import sys
3+
4+
from Orange.clustering.hierarchical import Tree
5+
6+
7+
class TestTree(unittest.TestCase):
8+
def test_repr(self):
9+
tree = Tree(2)
10+
self.assertEqual(repr(tree), "Tree(value=2, branches=())")
11+
12+
tree2 = Tree(3, (Tree(2), Tree(1)))
13+
self.assertEqual(repr(tree2), "Tree(value=3, branches=(Tree(value=2, branches=()), Tree(value=1, branches=())))")
14+
15+
for i in range(100):
16+
tree = Tree(i, (tree, tree))
17+
18+
rec_limit = sys.getrecursionlimit()
19+
try:
20+
sys.setrecursionlimit(30)
21+
repr(tree) # don't care about the result, just that it doesn't crash
22+
finally:
23+
sys.setrecursionlimit(rec_limit)
24+
25+
26+
if __name__ == "__main__":
27+
unittest.main()

i18n/si/msgs.jaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,7 @@ clustering/hierarchical.py:
860860
__hash: false
861861
def `__repr__`:
862862
{0.__name__}(value={1!r}, branches={2!r}): false
863+
{0.__name__}(...): false
863864
_Tree__value: false
864865
_Tree__branches: false
865866
Cluster: false

0 commit comments

Comments
 (0)