I have a function which is stored in a string, which looks something like this:
func_str = "def <func_name> ..."
I am evaluating it using "exec" and using it on an input as follows:
exec func_str in locals()
locals()[func_name](inp)
Now this function might have an exception, and I would like to know which line caused it in the string. Running it in the interpreter gives me an error message which is exactly what I want:
File "<string>", line 6, in <func_name>
TypeError: can only concatenate tuple (not "int") to tuple
This tells me the 6th line in my string is causing the problem.
Is there some way of capturing this programmatically? I've looked at similar solutions but they don't address the exception coming from a string which was executed in the local scope. When attempting to use the traceback module I only got the line-number for the outer function that invoked the exec.
thanks