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

Recursion error with deep trie on copy() #8

Closed
pombredanne opened this issue Jun 16, 2016 · 2 comments
Closed

Recursion error with deep trie on copy() #8

pombredanne opened this issue Jun 16, 2016 · 2 comments

Comments

@pombredanne
Copy link
Contributor

This is unfortunately a well known issue for copy, pickle and json when dealing with nested/recursive data structures. With this:

>>> from pygtrie import Trie
>>> from array import array
>>> t=Trie()
>>> for x in range(100):
...  y = array('h', range(x, 1000)).tostring()
...  t.update([(y,x,)])
... 
>>> t2=t.copy()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
[...]
  File "/home/pombreda/w421/scancode-toolkit-ref/local/lib/python2.7/site-packages/pygtrie.py", line 81, in iterate
    for step, node in sorted(self.children.iteritems()):
RuntimeError: maximum recursion depth exceeded while calling a Python object

There are two ways out IMHO:

  1. a custom de/serialization function that flattens the trie (eventually slow)
  2. a different non-nested data structure for the Trie such as an array/list-based structure (e.g. double array Trie)
mina86 added a commit that referenced this issue Jun 16, 2016
To avoid reaching Python’s maximum recursion limit, convert
_Node.iterate method to be iterative with stack stored on heap
instead.

Sadly, the same cannot be done with _Node.traverse.  An alternative
interface for traversing could be devised; perhaps in another commit.

Reported-by: Philippe Ombredanne <pombredanne@gmail.com>  # #8
@mina86
Copy link
Contributor

mina86 commented Jun 16, 2016

Fixed at HEAD.

@pombredanne
Copy link
Contributor Author

@mina86 Thank you ++ for this swift fix!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants