Can't get source code for a method “declared”

2019-05-06 15:08发布

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?

2条回答
The star\"
2楼-- · 2019-05-06 15:22

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.

查看更多
Emotional °昔
3楼-- · 2019-05-06 15:35

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 calling inspect.getsource.

查看更多
登录 后发表回答