jseabold
jseabold

Reputation: 8283

set python recursion limit for a function

I have 2 solutions to a recursion problem that I need for a function (actually a method). I want it to be recursive, but I want to set the recursion limit to 10 and reset it after the function is called (or not mess with recursion limit at all). Can anyone think of a better way to do this or recommend using one over the others? I'm leaning towards the context manager because it keeps my code cleaner and no setting the tracebacklimit, but there might be caveats?

import sys

def func(i=1):
    print i
    if i > 10:
        import sys
        sys.tracebacklimit = 1
        raise ValueError("Recursion Limit")
    i += 1
    func(i)

class recursion_limit(object):
    def __init__(self, val):
        self.val = val
        self.old_val = sys.getrecursionlimit()
    def __enter__(self):
        sys.setrecursionlimit(self.val)
    def __exit__(self, *args):
        sys.setrecursionlimit(self.old_val)
        raise ValueError("Recursion Limit")

def func2(i=1):
    """
    Call as

    with recursion_limit(12):
        func2()
    """
    print i
    i += 1
    func2(i)

if __name__ == "__main__":
    #    print 'Running func1'
    #    func()

    with recursion_limit(12):
        func2()

I do see some odd behavior though with the context manager. If I put in main

with recursion_limit(12):
    func2()

It prints 1 to 10. If I do the same from the interpreter it prints 1 to 11. I assume there is something going on under the hood when I import things?

EDIT: For posterity this is what I have come up with for a function that knows its call depth. I doubt I'd use it in any production code, but it gets the job done.

import sys
import inspect
class KeepTrack(object):
    def __init__(self):
        self.calldepth = sys.maxint

    def func(self):
        zero = len(inspect.stack())
        if zero < self.calldepth:
            self.calldepth = zero
        i = len(inspect.stack())
        print i - self.calldepth
        if i - self.calldepth < 9:
            self.func()

keeping_track = KeepTrack()
keeping_track.func()

Upvotes: 13

Views: 6090

Answers (4)

andrew cooke
andrew cooke

Reputation: 46882

ignoring the more general issues, it looks like you can get the current frame depth by looking at the length of inspect.getouterframes(). that would give you a "zero point" from which you can set the depth limit (disclaimer: i haven't tried this).

edit: or len(inspect.stack()) - it's not clear to me what the difference is. i would be interested in knowing if this works, and whether they were different.

Upvotes: 1

Eli Stevens
Eli Stevens

Reputation: 1447

While somewhat tangential (I'd have put it in a comment, but I don't think there's room), it should be noted that setrecursionlimit is somewhat misleadingly named - it actually sets the maximum stack depth:

http://docs.python.org/library/sys.html#sys.setrecursionlimit

That's why the function behaves differently depending on where you call it from. Also, if func2 were to make a stdlib call (or whatever) that ended up calling a number of functions such that it added more than N to the stack, the exception would trigger early.

Also also, I wouldn't change the sys.tracebacklimit either; that will have an effect on the rest of your program. Go with Ned's answer.

Upvotes: 1

Ned Batchelder
Ned Batchelder

Reputation: 375604

You shouldn't change the system recursion limit at all. You should code your function to know how deep it is, and end the recursion when it gets too deep.

The reason the recursion limit seems differently applied in your program and the interpreter is because they have different tops of stack: the functions invoked in the interpreter to get to the point of running your code.

Upvotes: 7

Nicola Musatti
Nicola Musatti

Reputation: 18228

I'd definitely choose the first approach, it is simpler and self explaining. After all the recursion limit is your explicit choice, so why obfuscate it?

Upvotes: 0

Related Questions