Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Maximum recursion depth exceeded

I have the following recursion code, at each node I call sql query to get the nodes belong to the parent node.

here is the error:

Exception RuntimeError: 'maximum recursion depth exceeded' in <bound method DictCursor.__del__ of <MySQLdb.cursors.DictCursor object at 0x879768c>> ignored  RuntimeError: maximum recursion depth exceeded while calling a Python object Exception AttributeError: "'DictCursor' object has no attribute 'connection'" in <bound method DictCursor.__del__ of <MySQLdb.cursors.DictCursor object at 0x879776c>> ignored 

Method that I call to get sql results:

def returnCategoryQuery(query, variables={}):     cursor = db.cursor(cursors.DictCursor);     catResults = [];     try:         cursor.execute(query, variables);         for categoryRow in cursor.fetchall():             catResults.append(categoryRow['cl_to']);         return catResults;     except Exception, e:         traceback.print_exc(); 

I actually don't have any issue with the above method but I put it anyways to give proper overview of the question.

Recursion Code:

def leaves(first, path=[]):     if first:         for elem in first:             if elem.lower() != 'someString'.lower():                 if elem not in path:                     queryVariable = {'title': elem}                     for sublist in leaves(returnCategoryQuery(categoryQuery, variables=queryVariable)):                         path.append(sublist)                         yield sublist                     yield elem 

Calling the recursive function

for key, value in idTitleDictionary.iteritems():     for startCategory in value[0]:         print startCategory + " ==== Start Category";         categoryResults = [];         try:             categoryRow = "";             baseCategoryTree[startCategory] = [];             #print categoryQuery % {'title': startCategory};             cursor.execute(categoryQuery, {'title': startCategory});             done = False;             while not done:                 categoryRow = cursor.fetchone();                 if not categoryRow:                     done = True;                     continue;                 rowValue = categoryRow['cl_to'];                 categoryResults.append(rowValue);         except Exception, e:             traceback.print_exc();         try:             print "Printing depth " + str(depth);             baseCategoryTree[startCategory].append(leaves(categoryResults))         except Exception, e:             traceback.print_exc(); 

Code to print the dictionary,

print "---Printing-------" for key, value in baseCategoryTree.iteritems():     print key,     for elem in value[0]:         print elem + ',';     raw_input("Press Enter to continue...")     print 

If the recursion is too deep I should be getting the error when I call my recursion function, but when I get this error when I print the dictionary.

like image 847
add-semi-colons Avatar asked Nov 18 '11 02:11

add-semi-colons


People also ask

How do I fix maximum recursion depth exceeded in Python?

The “maximum recursion depth exceeded in comparison” error is raised when you try to execute a function that exceeds Python's built in recursion limit. You can fix this error by rewriting your program to use an iterative approach or by increasing the recursion limit in Python.

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.

Does Python have a recursion limit?

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.


1 Answers

You can increment the stack depth allowed - with this, deeper recursive calls will be possible, like this:

import sys sys.setrecursionlimit(10000) # 10000 is an example, try with different values 

... But I'd advise you to first try to optimize your code, for instance, using iteration instead of recursion.

like image 87
Óscar López Avatar answered Sep 23 '22 10:09

Óscar López