Can't get source code for a method "declared" through exec using inspect in Python
python, reflection
Solution
I just looked at the inspect.py file after reading @jsbueno's answer, here's what I found :
def findsource(object):
"""Return the entire source file and starting line number for an object.
The argument may be a module, class, method, function, traceback, frame,
or code object. The source code is returned as a list of all the lines
in the file and the line number indexes a line in that list. An **IOError
is raised if the source code cannot be retrieved.**"""
try:
file = open(getsourcefile(object))
except (TypeError, IOError):
raise IOError, 'could not get source code'
lines = file.readlines() #reads the file
file.close()
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`.
Problem
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?