How do I fix RecursionError maximum recursion depth exceeded while calling a Python object?

How do I fix RecursionError maximum recursion depth exceeded while calling a Python object?

Thanks for your subscription!

hello guys, i want to know if there bugs by odoo in bank statement, so i want to import bank statement data with date 2021-01-31 (today date is 2021-09-08) but i get this error

RecursionError: maximum recursion depth exceeded while calling a Python object

when i check further log it said this error:

File "/usr/lib/python3/dist-packages/odoo/addons/account/models/account_bank_statement.py", line 152, in _compute_ending_balance statement.balance_end_real = statement.previous_statement_id.balance_end_real + total_entry_encoding

even after i create a bank statement then change the date it will also show this error

anyone know what is it? thank you

1Answer

Yes, there is a problem in algorithm. When you create a statement with date earlier than several existing statements, is required to recompute starting and ending balances of these statements. And here we gets the RecursionError if there is a large enough statements amount to recompute.

When i tried to import into Odoo a large amount of statements, i got the same issue. And the only workaround i found was to sort data before import for avoiding this recomputing.

In your case, seems, you can export statements, newer than one to create, to xlsx, delete them from the system, create your statement and then import exported statements back and reconcile them with payments again.
---

upd: Python recursion check is based on interpreter stack length, and when Odoo trying to recompute large statements amount, it just exceed this limit,  which  is 1000 by default. So you can try to increase  python recursion limit setting in your code using `sys.setrecursionlimit(some_limit_value)` that will be enough to perform all statements recomputing.

View Discussion

Improve Article

Save Article

When you execute a recursive function in Python on a large input ( > 10^4), you might encounter a “maximum recursion depth exceeded error”. This is a common error when executing algorithms such as DFS, factorial, etc. on large inputs. This is also common in competitive programming on multiple platforms when you are trying to run a recursive algorithm on various test cases. 
In this article, we shall look at why this error occurs and how to handle it in Python. To understand this, we need to first look at tail recursion.
Tail recursion
In a typical recursive function, we usually make the recursive calls first, and then take the return value of the recursive call to calculate the result. Therefore, we only get the final result after all the recursive calls have returned some value. But in a tail recursive function, the various calculations and statements are performed first and the recursive call to the function is made after that. By doing this, we pass the results of the current step to the next recursive call to the function. Hence, the last statement in a Tail recursive function is the recursive call to the function. 
This means that when we perform the next recursive call to the function, the current stack frame (occupied by the current function call) is not needed anymore. This allows us to optimize the code. We Simply reuse the current stack frame for the next recursive step and repeat this process for all the other function calls.
Using regular recursion, each recursive call pushes another entry onto the call stack. When the functions return, they are popped from the stack. In the case of tail recursion, we can optimize it so that only one stack entry is used for all the recursive calls of the function. This means that even on large inputs, there can be no stack overflow. This is called Tail recursion optimization.
Languages such as lisp and c/c++ have this sort of optimization. But, the Python interpreter doesn’t perform tail recursion optimization. Due to this, the recursion limit of python is usually set to a small value (approx, 10^4). This means that when you provide a large input to the recursive function, you will get an error. This is done to avoid a stack overflow. The Python interpreter limits the recursion limit so that infinite recursions are avoided. 
  
Handling recursion limit –
The “sys” module in Python provides a function called setrecursionlimit() to modify the recursion limit in Python. It takes one parameter, the value of the new recursion limit. By default, this value is usually 10^3. If you are dealing with large inputs, you can set it to, 10^6 so that large inputs can be handled without any errors.
Example:
Consider a program to compute the factorial of a number using recursion. When given a large input, the program crashes and gives a “maximum recursion depth exceeded error”.
 

Python3

def fact(n):

    if(n == 0):

        return 1

    return n * fact(n - 1)

if __name__ == '__main__':

    f = int(input('Enter the number: \n'))

    print(fact(f))

Output : 
 

How do I fix RecursionError maximum recursion depth exceeded while calling a Python object?

Using the setrecursionlimit() method, we can increase the recursion limit and the program can be executed without errors even on large inputs.
 

Python3

import sys

sys.setrecursionlimit(10**6)

def fact(n):

    if(n == 0):

        return 1

    return n * fact(n - 1)

if __name__ == '__main__':

    f = int(input('Enter the number: \n'))

    print(fact(f))

Output : 
 

How do I fix RecursionError maximum recursion depth exceeded while calling a Python object?


How do you overcome maximum recursion depth in Python?

Conclusion. The recursion depth limit in Python is by default 1000 . You can change it using sys. setrecursionlimit() function.

How do I fix RecursionError in Python?

A Python RecursionError exception is raised when the execution of your program exceeds the recursion limit of the Python interpreter. Two ways to address this exception are increasing the Python recursion limit or refactoring your code using iteration instead of recursion.

How can you prevent maximum recursion depth exceeded?

Try increasing the recursion limit ( sys. setrecursionlimit ) or re-writing your code without recursion. Return the current value of the recursion limit, the maximum depth of the Python interpreter stack. This limit prevents infinite recursion from causing an overflow of the C stack and crashing Python.