动态响应拆包任务

时间:2019-04-09 16:29:47

标签: python iteration variable-assignment abstract-syntax-tree inspect

在拆包分配语句中,被分配的对象可以检查分配给它的变量的数量吗?

class MyObject:

    def __iter__(self):
        n = some_diabolical_hack()
        print(f"yielding {n} vals")
        return iter(["potato"]*n)

类似的东西:

>>> x, y = MyObject()
yielding 2 vals
>>> a, b, c = MyObject()
yielding 3 vals

在更一般的情况下,是否可以内省作业中使用的target_list的“形状”?

>>> first, *blob, d[k], (x, y), L[3:7], obj.attr, last = MyObject()
unpacking to <_ast.Tuple object at 0xcafef00d>

示例潜在用例:改进的MagicMock(),当用于修补右侧的某些对象时,无需预先配置固定的迭代长度赋值语句。

2 个答案:

答案 0 :(得分:3)

您可以使用追溯模块:

import traceback

def diabolically_invoke_traceback():
    call = traceback.extract_stack()[-2]
    print call[3]
    unpackers = call[3].split('=')[0].split(',')
    print len (unpackers)
    return range(len(unpackers))

In [63]: a, b, c = diabolically_invoke_traceback()
a, b, c = diabolically_invoke_traceback()
3

In [64]: a
Out[64]: 0

In [65]: b
Out[65]: 1

In [66]: c
Out[66]: 2

答案 1 :(得分:2)

(免责声明:我不建议在生产质量的代码中使用恶魔般的技术。此答案中的所有内容可能不适用于与我不同的计算机,或与我不同的Python版本,或在非CPython发行版中,并且明天早上可能无法正常工作。)

也许您可以通过检查调用帧的字节码来做到这一点。如果我正确地阅读了bytecode guide,则根据目标列表是否带有星号,由指令UNPACK_SEQUENCEUNPACK_EX处理多个分配。这两个指令在其参数中都提供了有关目标列表形状的信息。

您可以编写您的恶魔般的函数来爬升框架层次结构,直到找到调用框架为止,并检查在表示分配右侧的FUNCTION_CALL之后出现的字节码指令。 (这假设您对MyObject()的调用是语句右侧的唯一内容)。然后,您可以从指令的参数中提取目标列表大小并返回。

import inspect
import dis
import itertools

def diabolically_retrieve_target_list_size():
    #one f_back takes us to `get_diabolically_sized_list`'s frame. A second one takes us to the frame of the caller of `get_diabolically_sized_list`.
    frame = inspect.currentframe().f_back.f_back
    #explicitly delete frame when we're done with it to avoid reference cycles.
    try:
        #get the bytecode instruction that immediately follows the CALL_FUNCTION that is executing right now
        bytecode_idx = frame.f_lasti // 2
        unresolved_bytecodes = itertools.islice(dis.get_instructions(frame.f_code), bytecode_idx+1, bytecode_idx+3)
        next_bytecode = next(unresolved_bytecodes)
        if next_bytecode.opname == "UNPACK_SEQUENCE":   #simple multiple assignment, like `a,b,c = ...`
            return next_bytecode.arg
        elif next_bytecode.opname == "EXTENDED_ARG":    #multiple assignment with splat, like `a, *b, c = ...`
            next_bytecode = next(unresolved_bytecodes)
            if next_bytecode.opname != "UNPACK_EX":
                raise Exception(f"Expected UNPACK_EX after EXTENDED_ARG, got {next_bytecode.opname} instead")
            args_before_star = next_bytecode.arg % 256
            args_after_star = next_bytecode.arg >> 8
            return args_before_star + args_after_star
        elif next_bytecode.opname in ("STORE_FAST", "STORE_NAME"): #single assignment, like `a = ...`
            return 1
        else:
            raise Exception(f"Unrecognized bytecode: {frame.f_lasti} {next_bytecode.opname}")
    finally:
        del frame

def get_diabolically_sized_list():
    count = diabolically_retrieve_target_list_size()
    return list(range(count))

a,b,c = get_diabolically_sized_list()
print(a,b,c)
d,e,f,g,h,i = get_diabolically_sized_list()
print(d,e,f,g,h,i)
j, *k, l = get_diabolically_sized_list()
print(j,k,l)
x = get_diabolically_sized_list()
print(x)

结果:

0 1 2
0 1 2 3 4 5
0 [] 1
[0]