0

我正在尝试写一个变质的quine。如果没有“spawn”上下文,子进程似乎继承了堆栈,因此我最终超过了最大递归深度。使用“生成上下文”,子进程似乎不会递归。我将如何执行修改后的 AST?

def main():
    module  = sys.modules[__name__]
    source  = inspect.getsource(module)
    tree    = ast.parse(source)

    visitor = Visitor() # TODO mutate
    tree    = visitor.visit(tree)
    tree    = ast.fix_missing_locations(tree)

    ctx     = multiprocessing.get_context("spawn")
    process = ctx.Process(target=Y, args=(tree,))
    # Y() encapsulates these lines, since code objects can't be pickled
    #code    = compile(tree, filename="<ast>", mode='exec', optimize=2)
    #process = ctx.Process(target=exec, args=(code, globals())) # locals()

    process.daemon = True
    process.start()
    # TODO why do daemonized processes need to be joined in order to run?
    process.join()

    return 0

if __name__ == '__main__': exit(main())
4

1 回答 1

0

它真的很容易。with daemon.DaemonContext(): foo() 基于@user2357112 的评论支持莫妮卡。

@trace
def spawn_child(f:Callable):
    with daemon.DaemonContext(stdin=sys.stdin, stdout=sys.stdout): return f()

I = TypeVar('I')
def ai(f:Callable[[int,], I])->Callable[[int,], I]:
    def g(*args, **kwargs)->int:
        # assuming we have a higher-order function morph()
        # that has a concept of eta-equivalence
        # (e.g., a probabilistic notion),
        # then the recursive call should be "metamorphic"
        O = [morph(f), status, partial(spawn_child, f),]
        i = random.randrange(0, len(O)) # TODO something magickal
        return O[i]()
    return g

def main()->int: return Y(ai)()

if __name__ == '__main__': exit(main())

下一个问题是编译嵌套函数定义的源代码,因为 f() 不是对 ai() 的引用,而是对在 Y() 中定义的函数的引用。

于 2022-02-13T01:22:46.173 回答