python3:从代码对象中获取定义的函数?

时间:2017-09-02 18:01:46

标签: python-3.x introspection

在python3中,我有以下代码:

path = '/path/to/file/containing/python/code'
source = open(path, 'r').read()
codeobject = compile(source, path, 'exec')

我已经检查了codeobject,但我没有看到任何方法来获取该对象中定义的所有函数的列表。

我知道我可以在source字符串中搜索以def开头的行,但我想从代码对象中获取此信息(如果可能的话)。

我错过了什么?

1 个答案:

答案 0 :(得分:1)

代码对象是嵌套结构;函数在执行代码对象时创建,其主体作为独立的代码对象嵌入,这些代码对象是常量的一部分:

>>> example = '''\
... def foobar():
...     print('Hello world!')
... '''
>>> codeobject = compile(example, '', 'exec')
>>> codeobject
<code object <module> at 0x11049ff60, file "", line 1>
>>> codeobject.co_consts
(<code object foobar at 0x11049fe40, file "", line 1>, 'foobar', None)
>>> codeobject.co_consts[0]
<code object foobar at 0x11049fe40, file "", line 1>
>>> codeobject.co_consts[0].co_name
'foobar'

当您反汇编顶级代码对象时,您可以看到函数对象是从这些代码对象创建的:

>>> import dis
>>> dis.dis(codeobject)
  1           0 LOAD_CONST               0 (<code object foobar at 0x11049fe40, file "", line 1>)
              2 LOAD_CONST               1 ('foobar')
              4 MAKE_FUNCTION            0
              6 STORE_NAME               0 (foobar)
              8 LOAD_CONST               2 (None)
             10 RETURN_VALUE

MAKE_FUNCTION opcode从堆栈中获取代码对象,以及函数名和堆栈中的任何默认参数值;你可以看到它前面放置代码对象和名称的LOAD_CONST opcodes

并非所有代码对象都是函数:

>>> compile('[i for i in range(10)]', '', 'exec').co_consts
(<code object <listcomp> at 0x1105cb030, file "", line 1>, '<listcomp>', 10, None)
>>> compile('class Foo: pass', '', 'exec').co_consts
(<code object Foo at 0x1105cb0c0, file "", line 1>, 'Foo', None)

如果要列出字节码中加载的函数,最好的办法是使用反汇编,而不是查找代码对象:

import dis
from itertools import islice

# old itertools example to create a sliding window over a generator
def window(seq, n=2):
    """Returns a sliding window (of width n) over data from the iterable
       s -> (s0,s1,...s[n-1]), (s1,s2,...,sn), ...
    """
    it = iter(seq)
    result = tuple(islice(it, n))
    if len(result) == n:
        yield result    
    for elem in it:
        result = result[1:] + (elem,)
        yield result

def extract_functions(codeobject):
    codetype = type(codeobject)
    signature = ('LOAD_CONST', 'LOAD_CONST', 'MAKE_FUNCTION', 'STORE_NAME')
    for op1, op2, op3, op4 in window(dis.get_instructions(codeobject), 4):
        if (op1.opname, op2.opname, op3.opname, op4.opname) == signature:
            # Function loaded
            fname = op2.argval
            assert isinstance(op1.argval, codetype)
            yield fname, op1.argval

这会为给定代码对象中加载的所有函数生成(name, codeobject)个元组。