Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

Pythons inspect get source returning unrelated source

$
0
0

I need to get the source of a function 'get_id' and inspect.getsource() is returning the source of the function 'update_creds' and I have no idea why

Some specifics about the environment: Its running on Splunks SOAR, which looking at the stack trace seems like it is running a wrapper program to fetch, decode, execute the script so just asking inspect to find the source doesn't seem to work unless I manually import the file into itself.

Here's the code I am debugging:

    import inspect    import re    import sys    # We have to add the git repo to the system path so we can import the file    sys.path.insert(0, '/scm/git/')    # Get the file name from __file__ (its formatted within angle brackets, so we get rid of them    source=__import__(re.sub(r'(<|>)','',__file__))    # use eval to specifially refer to this files function object.    # using debug comments, the eval does indeed refer to the correct object I want the source from    # However, the return of inspect.getsource() gives me the source of another function    function_source=inspect.getsource(eval("source."+kwargs["function"]))    generated_code=re.findall(r'phantom.act\((.*)\)', function_source)[-1]    return generated_code.replace("old_parameter", "new_parameter")

If you really want to know why I am doing this, the environment uses code blocks to generate python code, specifically function definitions and the final function call to continue execution. However, I need to add a parameter to the next function call, so I am using inspect.getsource() to get the final function call, add the parameter, call it, and do an early return.

I tried following where it loses reference to the correct function, which seems to be when inspect.getsource() is called. I am expecting the source of the 'get_id' function instead of the 'update_creds' function.

Looking into how getsource works, I thought it might have something to do with the name of the function, but the names do not have anything in common from what I can see. The only other thing I can think of would be if getsource refers to line numbers and its incorrectly using the line number from one file to another, but I would have no idea how to force inspect to make sure its referring to the same file


Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>