Skip to content

orm.py 模块excute函数里面affected变量可能存在referenced before assignment. #46

Description

@ehds
async def execute(sql, args, autocommit = True):
 
   ....
    async with __pool.get() as conn:
       ...
        try:
            async with conn.cursor(aiomysql.DictCursor) as cur:
                await cur.execute(sql.replace('?','%s'),args)
                affected = cur.rowcount
                 ....
        except BaseException:
              .....
        return affected

在这个代码里面 cur.execute(sql.replace('?','%s'),args) 如果执行错误的话(例如重复插入unique key相同的数据),下面的local variable affected 变量不会被申明且赋值,而在try外面 affected 又被返回,此时会报错,且在调用excute函数的外面并没有捕获异常导致程序崩溃.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions