Cliff,

You could use this recipe as a basis for something like this. The recipe is not at all general purpose (there was some controversy about its claims), but it does illustrate how you can introspect function frames without having to introduce another stack to record what's going on - that does work in a general fashion:
http://code.activestate.com/recipes/474088/

If you're interested in doing this sort of thing, I tend to favor the inspect module.

- Jim

On Wed, Sep 8, 2010 at 9:22 AM, Cliff Hill <xlorep@darkhelm.org> wrote:
Well, I wrote a decorator that does what I need. Since I don't want the decorator visible in my algorithm code (which is designed to be able to be copied out from my app, and people could just use the code in a separate Python program if desired), I have the Java object that handles the python interpreter add in the decorator class and decorators on all of the functions automatically before compiling the code. The decorator then ties back into that same java object, which maintains a function stack while executing the sorting algorithm, and the java object identifies recursion through checking if the function exists in the stack.

The decorator is:

class Tracer(object):
    def __init__(self, f):
        self.function = f
        
    def __call__(self, *args):
        global code
        if(code != None):
            code.stackPush(self.function.__name__)
            ret = self.function(*args)
            code.stackPop()
            return ret

which is prepended to my script, and then the java object (assigned in my script to the global variable of "code" above) has the following methods:

    
public void stackPush(String funcName) {
        if (getStack().contains(funcName)) {
            getSession().incrStat(Stats.RECURSE);
        }
        getStack().push(funcName);
    }

    public String stackPop() {
        return getStack().pop();
    }

Then, I have a method in the code object that adds in needed lines to whatever script I have to make it work:

    private String adjustedCode() {
        String ret = IMPORT_LIB + "\n" + TRACER_CODE + "\n\n";

        for (String line : code.split("\\n")) {
            int pos = line.indexOf("def ");
            if (pos >= 0) {
                ret += line.substring(0, pos);
                ret += TRACER + "\n";
            }
            ret += line + "\n";
        }

        ret += "\n" + EXEC_LINE;
        return ret;
    }

IMPORT_LIB is a line I add to import the standard python library from a zip file in my app, TRACER_CODE adds in the code for the tracer, TRACER is the "@Tracer" added to the line just above each function definition, and EXEC_LINE simply adds a line to execute the sorting function.


On Wed, Sep 8, 2010 at 1:21 AM, Justin Deoliveira <jdeolive@opengeo.org> wrote:
Could you wrap your function i a decorator that maintains a counter that gets incremented everytime the function is called?

On Tue, Sep 7, 2010 at 5:14 PM, Cliff Hill <xlorep@darkhelm.org> wrote:
I'm looking for a clever way to identify recursive calls (so I can count them accurately in my sorting app I've been working on).  Right now, I am basically doing a stack trace in Java, and identifying all of the python function calls with in it, and then figuring out whenever there is a recursive call by seeing if the same function appears multiple times within the stack:

    String getPyFunc() {
        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
        List<String> funcNames = new ArrayList<String>();
        for (int i = 0; i < stackTrace.length; i++) {
            if (stackTrace[i].getMethodName().equals("call_function")) {
                String funcName = stackTrace[i - 1].getMethodName();
                funcName = funcName.substring(0, funcName.indexOf('$'));

                funcNames.add(funcName);
            }
        }

        if (!getFuncStack().isEmpty()
                && getFuncStack().size() != funcNames.size()
                && getFuncStack().get(0).equals(funcNames.get(0))) {
            log.finest("*** Recursion Identified ***");
            incrRecurses();
        }

        setFuncStack(funcNames);

        return getFuncStack().get(0);
    }

But what I am wandering is if there is a better way, where upon having a function called, the system can properly identify that it is a recursive call at execution. Does anyone have any suggestions?




--
"I'm not responcabel fer my computer's spleling errnors" - Xlorep DarkHelm
Website: http://darkhelm.org

------------------------------------------------------------------------------
This SF.net Dev2Dev email is sponsored by:

Show off your parallel programming skills.
Enter the Intel(R) Threading Challenge 2010.
http://p.sf.net/sfu/intel-thread-sfd
_______________________________________________
Jython-users mailing list
Jython-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jython-users




--
Justin Deoliveira
Enterprise support for open source geospatial.




--
"I'm not responcabel fer my computer's spleling errnors" - Xlorep DarkHelm
Website: http://darkhelm.org

------------------------------------------------------------------------------
This SF.net Dev2Dev email is sponsored by:

Show off your parallel programming skills.
Enter the Intel(R) Threading Challenge 2010.
http://p.sf.net/sfu/intel-thread-sfd
_______________________________________________
Jython-users mailing list
Jython-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jython-users