"Who called me" Issue

Hello here!

Here is my issue. I’m writing a logging function, which needs to know ‘Who called me’. I use inspect to to that. I want to know the filename, the function name and all the arguments. Here is the code:

import inspect
def GetCallerInfo():

stack            = inspect.stack()
functionName     = stack[2][3]
callerInfo       = stack[2][0]
f_code           = callerInfo.f_code
completeFilename = f_code.co_filename

# Remove the .py extension from filename
filename         = os.path.basename(completeFilename)[0:-3] 

f_back           = callerInfo.f_back

f_locals         = f_back.f_locals
args             = f_locals.items()
    
argsCount        = f_code.co_argcount
args             = args[0:argsCount]

return filename, functionName, args

This works well in debug mode, after having used the play button and having set at least 1 break point at some place. After that, I always get the correct answer, debugging or not. However, if I use that without having done any debugging before (play button without any break point), the ‘args’ output fails. It looks like that

print args → [(‘name’, ‘main’), (‘exit’, <System.Action object at 0x000000000000009E [System.Action]>), (‘file’, ‘C:\Users\User\Documents\Job\Bitbucket\python\Buttons\Button_Load.py’)]

In debug mode with a break point (and thereafter), what I get is like:

print args → [(‘param1’, value1), (‘param2’, ‘string2’), (‘param3’, False)]

Any idea what is going on? Is there a clean way to do that? Or at least tell Rhino “Yes I have debugged the stuff using a break point” without doing so?

Thank you!

Alexandre

Hello,

I wonder whether you need to look further up or down the stack to find the information you are looking for because there is some python going on to operate the play button…? Can you print the whole stack and find the information you want?

You could use a simple decorator to get the called function and arguments but I don’t know how to get the caller.

Hello,

Yes, I did look up & down the stack. The information is nowhere in it, unfortunately.

Thanks!