Search code examples
pythonhtmlbottlesqlitestudio

python bottle framework maximum recursion depth exceeded


I am trying to add a path for my web app but for some reason maximum recursion depth error occurs

def runSQL(sql):
    db = sqlite3.connect('zadanie.db')
    c = db.cursor()
    c.execute(sql)
    data = c.fetchall()
    db.commit()
    c.close()
    return data  

def Subjects(): 
    sql = "SELECT (here is my query)" 
    data = runSQL(sql) 
    return data

@app.route('/subjects')
def Subjects():
    sub = template('look4.html', rows=Subjects())              
    return sub

I have tried to set recursion limit to higher number, but then error segmentation fault 11 occurs.
I would be grateful for any debugging suggestions :)


Solution

  • The problem is not with the recursion depth. As you can see you define two functions named Subject.

    As a result Python will override the first one with the second. Now the second one has a call to (what you think is) the previous one. But since Subjects is overriden, it will redirect to that function again so:

    @app.route('/subjects')
    def Subjects():
        sub = template('look4.html', rows=Subjects())
        return sub

    Will get stuck in infinite recursion. The solution is to simply rename one of the two (probably it is better to rename the first one), and change the call, like:

    def subjects_query(): 
        sql = "SELECT (here is my query)" 
        data = runSQL(sql) 
        return data
    
    @app.route('/subjects')
    def subjects():
        sub = template('look4.html', rows=subjects_query())              
        return sub

    Also the convention is that functions in Python are lowercase with underscores, so you better rename the second one to subjects (like I have done here).