6

Transcrypt Python to JavaScript 编译器中实现协程时,我遇到了以下奇怪的问题。

Transcrypt 使用 CPython 3.6 的本机解析器来生成 AST。对于异步全局函数 defs,它会生成一个 AsyncFunctionDef 节点。但是对于异步方法,它没有!尽管如此,CPython 本身似乎可以正确编译异步方法。

所以下面这段代码使用 CPython 运行,但 Transcrypt 无法运行它,因为 CPython 的 AST 模块生成的 AST 似乎缺少方法的 AsyncFunctionDef 节点(与全局函数相反)。

因此,以下代码不会生成 AsyncFunctionDef 节点:

class C:
    def __init__ (self):
        self.aTime = 2

    async def g (self, waw, asio):
        print ('g0')
        await waw (self.aTime, asio)
        print ('g1')

我错过了什么?官方支持异步方法,不是吗?在PEP 492中找不到任何具体内容。

该示例的完整代码为:

from org.transcrypt.stubs.browser import __pragma__, __envir__

# Note that CPython will ignore all pragma's



# Provide waitAWhile for Transcrypt

__pragma__ ('js', '{}', '''
    function waitAWhile (aTime, asio) {
      return new Promise (resolve => {
        setTimeout (() => {
          resolve (aTime);
        }, 1000 * aTime);
      });
    }
''')



# Provide waitAWhile for CPython

__pragma__ ('skip') # Compile time, needed because import is done compile time

import asyncio

def waitAWhile (aTime, asio):
    return asio.sleep (aTime)

__pragma__ ('noskip')



# Actual code to be tested    

async def f (waw, asio):
    print ('f0')
    await waw (2, asio)
    print ('f1')

class C:
    def __init__ (self):
        self.aTime = 2

    async def g (self, waw, asio):
        print ('g0')
        await waw (self.aTime, asio)
        print ('g1')

c = C ()


# Just call async functions for Transcrypt, since in the browser JavaScript is event driven by default

if __envir__.executor_name == __envir__.transpiler_name:
    f (waitAWhile, None)
    c.g (waitAWhile, None)
    c.g (waitAWhile, None)
    f (waitAWhile, None)



# Create event loop and tasks for CPython, since it isn't event driven by default

else:
    eventLoop = asyncio.get_event_loop ()
    tasks = [
        eventLoop.create_task (f (waitAWhile, asyncio)),
        eventLoop.create_task (c.g (waitAWhile, asyncio)),
        eventLoop.create_task (c.g (waitAWhile, asyncio)),
        eventLoop.create_task (f (waitAWhile, asyncio)),
    ]

    waitingTasks = asyncio.wait (tasks)
    eventLoop.run_until_complete (waitingTasks)
    eventLoop.close ()
4

1 回答 1

1

最终我让解析器正常工作。我最初一定是在其他地方阻止了解析。可能我忘了visit从树上更高的节点调用。不幸的是,我无法再重现该问题。

对于那些感兴趣的人,解析器代码位于:

https://github.com/QQuick/Transcrypt/blob/master/transcrypt/modules/org/transcrypt/compiler.py

第 2045 行。

最重要的是:Python 的 ast 模块可以正常工作,尽管它可以使用更多文档。

有一个(非常紧凑但可用的)第 3 方文档,位于:

https://greentreesnakes.readthedocs.io/en/latest/

于 2017-08-05T11:58:23.023 回答