示例#1
0
文件: gen.py 项目: cpcloud/cysqlite3
def generate_function_body(func):
    sig, = func.nopython_signatures
    converters = [(arg, VALUE_EXTRACTORS[arg]) for arg in sig.args]
    resulter = RESULT_SETTERS[sig.return_type]

    args = []
    sequence = []

    for i, (argtype, converter) in enumerate(converters):
        arg = sub(load.argv, idx(i))
        converter_function = call[converter.__name__]
        argname = 'arg_{:d}'.format(i)
        if isinstance(argtype, types.Optional):
            if_statement, rest, arg = unnullify(arg, converter_function, argname, argtype)
        else:
            if_statement, rest, arg = fail_if_null(arg, converter_function, argname, argtype)

        sequence.append(if_statement)
        if rest is not None:
            sequence.append(rest)
        args.append(arg)

    result = call[func.__name__](*args)
    final_call = call[resulter.__name__](load.ctx, load.result_value)
    return sequence + [
        ast.Assign(targets=[store.result_value], value=result),
        ast.If(
            test=ast.Compare(
                left=load.result_value,
                ops=[ast.IsNot()],
                comparators=[NONE]
            ),
            body=[ast.Expr(value=final_call)],
            orelse=[ast.Expr(value=call.sqlite3_result_null(load.ctx))]
        ) if isinstance(sig.return_type, types.Optional) else (
            ast.Expr(value=final_call)
        )
    ]
示例#2
0
文件: gen.py 项目: cpcloud/cysqlite3
def gen_step(cls, name):
    class_name = cls.__name__
    sig, = cls.class_type.jitmethods['step'].nopython_signatures
    args = sig.args[1:]

    body = [
        ast.Assign(
            targets=[store['arg_{:d}'.format(i)]],
            value=sub(load.argv, idx(i))
        )
        for i, arg in enumerate(args)
    ]

    step_args = []
    statements = []

    for i, a in enumerate(args):
        if isinstance(a, types.Optional):
            func = unnullify
        else:
            func = fail_if_null
        if_statement, rest, argument = func(
            load['arg_{:d}'.format(i)],
            call[VALUE_EXTRACTORS[a].__name__],
            'value_{:d}'.format(i),
            a
        )
        statements.append(if_statement)
        if rest is not None:
            statements.append(rest)
        step_args.append(argument)

    body.extend([
        ast.Assign(
            targets=[
                store.agg_ctx
            ],
            value=call.unsafe_cast(
                call.sqlite3_aggregate_context(
                    load.ctx,
                    call.sizeof(load[class_name])
                ),
                load[class_name]
            )
        ),
        ast.If(
            test=call.not_null(load.agg_ctx),
            body=statements + [
                ast.Expr(value=call(attr.agg_ctx.step, *step_args))
            ],
            orelse=[]
        )
    ])
    decorator_list = [
        call.cfunc(
            call.void(load.voidptr, load.intc, call.CPointer(load.voidptr)),
            nopython=TRUE
        )
    ]
    function_signature = ast.arguments(
        args=[arg.ctx, arg.argc, arg.argv],
        vararg=None,
        kwonlyargs=[],
        kw_defaults=[],
        kwargs=None,
        defaults=[],
    )
    mod = ast.Module(
        body=[
            import_from.numba[alias.cfunc],
            import_from.numba.types[
                alias.void, alias.voidptr, alias.intc, alias.CPointer
            ],
            ast.FunctionDef(
                name=name,
                args=function_signature,
                body=body,
                decorator_list=decorator_list,
                returns=None
            )
        ]
    )
    return mod
示例#3
0
def test_sub(i):
    assert eq(
        sub(load.a, idx(i)),
        ast.Subscript(value=ast.Name(id="a", ctx=ast.Load()), slice=ast.Index(value=ast.Num(n=i)), ctx=ast.Load()),
    )