Пример #1
0
def partial_apply(func, *args, **kwds):
    """
    Same as :func:`partial_eval`, but in addition uses the provided values of
    positional and keyword arguments in the partial evaluation.
    """
    function = Function.from_object(func, ignore_decorators=True)

    if has_nested_definitions(function):
        raise ValueError(
            "A partially evaluated function cannot have nested function or class definitions")

    if is_async(function):
        raise ValueError("A partially evaluated function cannot be an async coroutine")

    if len(args) > 0 or len(kwds) > 0:
        bound_function = function.bind_partial(*args, **kwds)
    else:
        bound_function = function

    new_tree, bindings = _run_components(
        bound_function.tree,
        bound_function.get_external_variables())

    globals_ = dict(bound_function.globals)
    globals_.update(bindings)

    new_function = bound_function.replace(tree=new_tree, globals_=globals_)

    return new_function.eval()
Пример #2
0
def inline(func):
    """
    Marks the function for inlining.
    """
    function = Function.from_object(func, ignore_decorators=True)

    if has_nested_definitions(function):
        raise ValueError("An inlined function cannot have nested function or class definitions")

    if is_a_generator(function):
        raise ValueError("An inlined function cannot be a generator")

    if is_async(function):
        raise ValueError("An inlined function cannot be an async coroutine")

    if len(function.closure_vals) > 0:
        raise ValueError("An inlined function cannot have a closure")

    func.__peval_inline__ = True
    return func