Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RuntimeError: maximum recursion depth exceeded while calling a Python object #523

Closed
Pitometsu opened this issue Dec 25, 2017 · 18 comments
Closed

Comments

@Pitometsu
Copy link

Pitometsu commented Dec 25, 2017

v = 1
t = int
for _ in range(0, 1000):
    v = (1, v, 1)
    t = tp.Tuple[int, t, int]
e = (v, t)
print e

Traceback (most recent call last):
  File "/Users/netsu/.virtualenvs/csr-44kfYsMA/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2882, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-174-01b42d124d17>", line 5, in <module>
    t = tp.Tuple[int, t, int]
  File "/Users/netsu/.local/share/virtualenvs/csr-44kfYsMA/lib/python2.7/site-packages/typing.py", line 754, in inner
    value = func(*args)
  File "/Users/netsu/.local/share/virtualenvs/csr-44kfYsMA/lib/python2.7/site-packages/typing.py", line 1374, in __getitem__
    return super(TupleMeta, self).__getitem__(parameters)
  File "/Users/netsu/.local/share/virtualenvs/csr-44kfYsMA/lib/python2.7/site-packages/typing.py", line 754, in inner
    value = func(*args)
  File "/Users/netsu/.local/share/virtualenvs/csr-44kfYsMA/lib/python2.7/site-packages/typing.py", line 1254, in __getitem__
    orig_bases=self.__orig_bases__)
  File "/Users/netsu/.local/share/virtualenvs/csr-44kfYsMA/lib/python2.7/site-packages/typing.py", line 1113, in __new__
    self.__tree_hash__ = (hash(self._subs_tree()) if origin else
  File "/Users/netsu/.local/share/virtualenvs/csr-44kfYsMA/lib/python2.7/site-packages/typing.py", line 1197, in _subs_tree
    tree_args = _subs_tree(self, tvars, args)
  File "/Users/netsu/.local/share/virtualenvs/csr-44kfYsMA/lib/python2.7/site-packages/typing.py", line 675, in _subs_tree
    tree_args.append(_replace_arg(arg, tvars, args))
  File "/Users/netsu/.local/share/virtualenvs/csr-44kfYsMA/lib/python2.7/site-packages/typing.py", line 634, in _replace_arg
    return arg._subs_tree(tvars, args)
  File "/Users/netsu/.local/share/virtualenvs/csr-44kfYsMA/lib/python2.7/site-packages/typing.py", line 1197, in _subs_tree
    tree_args = _subs_tree(self, tvars, args)
  File "/Users/netsu/.local/share/virtualenvs/csr-44kfYsMA/lib/python2.7/site-packages/typing.py", line 675, in _subs_tree
    tree_args.append(_replace_arg(arg, tvars, args))
  File "/Users/netsu/.local/share/virtualenvs/csr-44kfYsMA/lib/python2.7/site-packages/typing.py", line 634, in _replace_arg
    return arg._subs_tree(tvars, args)
  File "/Users/netsu/.local/share/virtualenvs/csr-44kfYsMA/lib/python2.7/site-packages/typing.py", line 1197, in _subs_tree
    tree_args = _subs_tree(self, tvars, args)


 . . .

  File "/Users/netsu/.local/share/virtualenvs/csr-44kfYsMA/lib/python2.7/site-packages/typing.py", line 675, in _subs_tree
    tree_args.append(_replace_arg(arg, tvars, args))
  File "/Users/netsu/.local/share/virtualenvs/csr-44kfYsMA/lib/python2.7/site-packages/typing.py", line 635, in _replace_arg
    if isinstance(arg, TypeVar):
RuntimeError: maximum recursion depth exceeded while calling a Python object
@Pitometsu
Copy link
Author

Pitometsu commented Dec 25, 2017

Slouldn't recursion be avoided here because of python's lack of tail recursion optimization and use any of: iteration, continuations, stack machine, exceptions, etc.?

@Pitometsu
Copy link
Author

With just value – everything fine

v = 1
for _ in range(0, 1000):
    v = (1, v, 1)
print v
(1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, 1, 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)

@Pitometsu
Copy link
Author

@JelleZijlstra
Copy link
Member

Is there a practical use case for your example? Printing deeply recursive data structures will naturally require recursion, even if there are shortcuts for built-in datatypes like tuples.

@ilevkivskyi
Copy link
Member

I don't think we should sacrifice (already limited) clarity of code to save call stack consumption for this very rare situation. (Also it will be already consumed less in Python 3.7 with PEP 560). For your things just use sys.setrecursionlimit(<something large>).

@Pitometsu
Copy link
Author

@ilevkivskyi you can use something like https://github.com/baruchel/tco as decorator to keep relative clarity. It just need to be rewritten as tail recursion to be safe.
Would you accept PR?

@Pitometsu
Copy link
Author

Pitometsu commented Dec 26, 2017

Unfortunately, I can't just enhance stack size, because it need to be checked that my code work with any stack size, and for now it sadly limited by typing implementation.

@Pitometsu
Copy link
Author

Pitometsu commented Dec 26, 2017

@JelleZijlstra practical reason is to cover by typing expressions that actually can exist in python (as you see in example, they are not limited by stack size).

@ilevkivskyi
Copy link
Member

Would you accept PR?

I am not against a PR with a tail call optimization, but I am quite sure @gvanrossum will be against TCO.

@Pitometsu
Copy link
Author

Pitometsu commented Dec 28, 2017

I'm about PR with rewriting recursion to something that doesn't overflowing stack. To keep code clarity, it can emulate pseudo-recursion by doing necessary transformations to loop/continuation/stack machine in decorator. Mentioned before https://github.com/baruchel/tco as exist example. Actually, python, as a script language, lack ability to have real TCO by design, you right. But it doesn't really need the real recursion to keep code clean and free of stack overflow bombs. It have decorators. Anyway, issue isn't solved, could you reopen it, please – in case if someone would have ideas how to solve it in elegant way?

@gvanrossum
Copy link
Member

gvanrossum commented Dec 28, 2017 via email

@Pitometsu
Copy link
Author

@gvanrossum did you even read the topic? Is stack overflow ok for you?

@proton1k
Copy link

That's a topic...
1 http://neopythonic.blogspot.cz/2009/04/tail-recursion-elimination.html
2 http://www.kylem.net/programming/tailcall.html
again, you may initiate your own BDFLism by implementing it your own way

@gvanrossum
Copy link
Member

gvanrossum commented Dec 28, 2017 via email

@cegprakash
Copy link

I still have this issue

@Pitometsu
Copy link
Author

@cegprakash it's Python: nobody cares.

@liuming5467
Copy link

import sys
sys.setrecursionlimit(100000)

vltr added a commit to vltr/middle-schema that referenced this issue Aug 1, 2018
@naazneen
Copy link

naazneen commented Apr 8, 2020

import sys
sys.setrecursionlimit(100000)

Where do you write this? in spec file?

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

No branches or pull requests

8 participants