Извлечение всех констант из объекта кода в Python 3 - PullRequest
0 голосов
/ 23 апреля 2019

Я хочу извлечь все константы, используемые в функции. В Python 2.7 я могу перебрать co_consts в __code__ функции и извлечь все строки. Например, Python 2.7

>>> def func():
...:     return 'foo' if True else ('bar',)

>>> print(func.__code__.co_consts)
(None, 'foo', 'bar', ('bar',)) # no need to look into the tuple as 'bar' is available outside it as well.

Это даст мне 'foo' и 'bar', как и ожидалось. Однако в Python 3.7 объект кода содержит 'bar' только внутри кортежа.

Python 3,7

>>> print(func.__code__.co_consts)
(None, True, 'foo', ('bar',))

Это означает, что мне также нужно посмотреть на кортеж в co_consts. Я путаюсь, может ли быть больше уровней вложенности внутри co_consts?

1 Ответ

1 голос
/ 23 апреля 2019

Вы можете просто рекурсивно пройтись по кортежу co_consts:

def x():
    return {
        "y": ("bla" + "ble", ("blo", ["blu", "blip"])),
        ("x", "z"): "blup",
        True: False,
        42: "hello" * 10,
        None: 4 + 1j,
    }


def get_consts(func):
    def walk(cell):
        if isinstance(cell, tuple):
            for item in cell:
                yield from walk(item)
        else:
            yield cell

    yield from walk(func.__code__.co_consts)


for const in get_consts(x):
    print(const)

отпечатки

None
blable
blo
blu
blip
blup
False
hellohellohellohellohellohellohellohellohellohello
(4+1j)
y
x
z
True
42
None

Порядок констант может не совпадать с исходным; они соответствуют порядку разборки:

  5           0 LOAD_CONST               1 ('blable')
              2 LOAD_CONST               2 ('blo')
              4 LOAD_CONST               3 ('blu')
              6 LOAD_CONST               4 ('blip')
              8 BUILD_LIST               2
             10 BUILD_TUPLE              2
             12 BUILD_TUPLE              2

  6          14 LOAD_CONST               5 ('blup')

  7          16 LOAD_CONST               6 (False)

  8          18 LOAD_CONST               7 ('hellohellohellohellohellohellohellohellohellohello')

  9          20 LOAD_CONST               8 ((4+1j))
             22 LOAD_CONST               9 (('y', ('x', 'z'), True, 42, None))
             24 BUILD_CONST_KEY_MAP      5
             26 RETURN_VALUE

РЕДАКТИРОВАТЬ: Если вам нужны исходные строки, как они есть в источнике, вам нужно будет использовать модуль ast вместо:

import ast
import inspect


class ConstantGatherer(ast.NodeVisitor):
    def __init__(self):
        super().__init__()
        self.consts = []

    def visit_Str(self, node):
        self.consts.append(node.s)

    def visit_Num(self, node):
        self.consts.append(node.n)


cg = ConstantGatherer()
cg.visit(ast.parse(inspect.getsource(x)))
print(cg.consts)

выходы

['y', 'x', 'z', 42, 'bla', 'ble', 'blo', 'blu', 'blip', 'blup', 'hello', 10, 4, 1j]
...