The following code throws an exception:
import inspect
def work():
my_function_code = """def print_hello():
print('Hi!')
"""
exec(my_function_code, globals())
inspect.getsource(print_hello)
The code above throws an exception IOError. If I declare the function without using exec (like below), I can get its source code just fine.
import inspect
def work():
def print_hello():
print('Hi!')
inspect.getsource(print_hello)
There's a good reason for me to do something like this.
Is there a workaround for this? Is it possible to do something like this? If not, why?
I just looked at the inspect.py file after reading @jsbueno's answer, here's what I found :
It clearly indicates that it tries to open the source file and then reads its content, which is why it is not possible in case of
exec
.That is not even possible. What python does to get to the source of any code it is running is loading the source code file - on disk. It locates this file by looking at the
__file__
attribute on the code's module.The string used to generate a code object trough "exec" or "compiled" is not kept around by the objects resulting from these calls.
You probably could get to look at the code if you set a
__file__
variable on the global dictionary of your generated code, and write your source string to that file, prior to callinginspect.getsource
.