How to use the pythran.cxxgen.Value function in pythran

To help you get started, we’ve selected a few pythran examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github serge-sans-paille / pythran / pythran / toolchain.py View on Github external
arguments_names = HasArgument(function_name).visit(ir)
            arguments = [n for n, _ in
                         zip(arguments_names, arguments_types)]
            name_fmt = pythran_ward + "{0}::{1}::type{2}"
            args_list = ", ".join(arguments_types)
            specialized_fname = name_fmt.format(module_name,
                                                internal_func_name,
                                                "<{0}>".format(args_list)
                                                if arguments_names else "")
            result_type = "typename %s::result_type" % specialized_fname
            docstring = spec_to_string(function_name, signature)
            mod.add_capsule(
                FunctionBody(
                    FunctionDeclaration(
                        Value(result_type, function_name),
                        [Value(t, a)
                         for t, a in zip(arguments_types, arguments)]),
                    Block([ReturnStatement("{0}()({1})".format(
                        warded(module_name, internal_func_name),
                        ', '.join(arguments)))])
                ),
                function_name,
                docstring
            )

    return mod, error_checker
github serge-sans-paille / pythran / pythran / toolchain.py View on Github external
sigid)
                arguments_types = [pytype_to_ctype(t) for t in signature]
                arguments_names = HasArgument(function_name).visit(ir)
                arguments = [n for n, _ in
                             zip(arguments_names, arguments_types)]
                name_fmt = pythran_ward + "{0}::{1}::type{2}"
                args_list = ", ".join(arguments_types)
                specialized_fname = name_fmt.format(module_name,
                                                    internal_func_name,
                                                    "<{0}>".format(args_list)
                                                    if arguments_names else "")
                result_type = "typename %s::result_type" % specialized_fname
                mod.add_pyfunction(
                    FunctionBody(
                        FunctionDeclaration(
                            Value(
                                result_type,
                                numbered_function_name),
                            [Value(t + '&&', a)
                             for t, a in zip(arguments_types, arguments)]),
                        Block([Statement("""
                            PyThreadState *_save = PyEval_SaveThread();
                            try {{
                                auto res = {0}()({1});
                                PyEval_RestoreThread(_save);
                                return res;
                            }}
                            catch(...) {{
                                PyEval_RestoreThread(_save);
                                throw;
                            }}
                            """.format(warded(module_name,
github serge-sans-paille / pythran / pythran / toolchain.py View on Github external
arguments_types = [pytype_to_ctype(t) for t in signature]
            arguments_names = HasArgument(function_name).visit(ir)
            arguments = [n for n, _ in
                         zip(arguments_names, arguments_types)]
            name_fmt = pythran_ward + "{0}::{1}::type{2}"
            args_list = ", ".join(arguments_types)
            specialized_fname = name_fmt.format(module_name,
                                                internal_func_name,
                                                "<{0}>".format(args_list)
                                                if arguments_names else "")
            result_type = "typename %s::result_type" % specialized_fname
            docstring = spec_to_string(function_name, signature)
            mod.add_capsule(
                FunctionBody(
                    FunctionDeclaration(
                        Value(result_type, function_name),
                        [Value(t, a)
                         for t, a in zip(arguments_types, arguments)]),
                    Block([ReturnStatement("{0}()({1})".format(
                        warded(module_name, internal_func_name),
                        ', '.join(arguments)))])
                ),
                function_name,
                docstring
            )

    return mod, error_checker
github serge-sans-paille / pythran / pythran / backend.py View on Github external
operator_body, formal_types, formal_args = tmp

        tmp = self.prepare_types(node)
        dflt_argv, dflt_argt, result_type, callable_type, pure_type = tmp

        # a generator has a call operator that returns the iterator
        next_name = "__generator__{0}".format(cxxid(node.name))
        instanciated_next_name = "{0}{1}".format(
            next_name,
            "<{0}>".format(", ".join(formal_types)) if formal_types else "")

        operator_body.append(Statement("{}: return result_type()".format(
            CxxGenerator.FinalStatement)))

        next_declaration = [
            FunctionDeclaration(Value("result_type", "next"), []),
            EmptyStatement()]  # empty statement to force a comma ...

        # the constructors
        next_constructors = [
            FunctionBody(
                FunctionDeclaration(Value("", next_name), []),
                Line(': pythonic::yielder() {}')
                )]
        if formal_types:
            # If all parameters have a default value, we don't need default
            # constructor
            if dflt_argv and all(dflt_argv):
                next_constructors = list()
            next_constructors.append(FunctionBody(
                make_function_declaration(self, node, "", next_name,
                                          formal_types, formal_args,
github serge-sans-paille / pythran / pythran / backend.py View on Github external
Block([
                    ReturnStatement(
                        CxxGenerator.StateValue)])),
            FunctionBody(
                FunctionDeclaration(
                    Value("pythonic::types::generator_iterator<{0}>"
                          .format(next_name),
                          "begin"),
                    []),
                Block([Statement("next()"),
                       ReturnStatement(
                           "pythonic::types::generator_iterator<{0}>"
                           "(*this)".format(next_name))])),
            FunctionBody(
                FunctionDeclaration(
                    Value("pythonic::types::generator_iterator<{0}>"
                          .format(next_name),
                          "end"),
                    []),
                Block([ReturnStatement(
                    "pythonic::types::generator_iterator<{0}>()"
                    .format(next_name))]))
            ]
        next_signature = templatize(
            FunctionDeclaration(
                Value(
                    "typename {0}::result_type".format(
                        instanciated_next_name),
                    "{0}::next".format(instanciated_next_name)),
                []),
            formal_types)
github serge-sans-paille / pythran / pythran / toolchain.py View on Github external
arguments = [n for n, _ in
                             zip(arguments_names, arguments_types)]
                name_fmt = pythran_ward + "{0}::{1}::type{2}"
                args_list = ", ".join(arguments_types)
                specialized_fname = name_fmt.format(module_name,
                                                    internal_func_name,
                                                    "<{0}>".format(args_list)
                                                    if arguments_names else "")
                result_type = "typename %s::result_type" % specialized_fname
                mod.add_pyfunction(
                    FunctionBody(
                        FunctionDeclaration(
                            Value(
                                result_type,
                                numbered_function_name),
                            [Value(t + '&&', a)
                             for t, a in zip(arguments_types, arguments)]),
                        Block([Statement("""
                            PyThreadState *_save = PyEval_SaveThread();
                            try {{
                                auto res = {0}()({1});
                                PyEval_RestoreThread(_save);
                                return res;
                            }}
                            catch(...) {{
                                PyEval_RestoreThread(_save);
                                throw;
                            }}
                            """.format(warded(module_name,
                                              internal_func_name),
                                       ', '.join(arguments)))])
                    ),
github serge-sans-paille / pythran / pythran / backend.py View on Github external
if defaults is None:
        defaults = [None] * len(ftypes)
    if attributes is None:
        attributes = []

    arguments = list()
    first_default = len(node.args.args) - len(node.args.defaults)
    for i, (t, a, d) in enumerate(zip(ftypes, fargs, defaults)):
        # because universal reference and default don't get on well
        if isinstance(self, CxxGenerator) or i >= first_default:
            rvalue_ref = ""
        else:
            rvalue_ref = "&&"
        argument = Value(t + rvalue_ref, "{0}{1}".format(a, make_default(d)))
        arguments.append(argument)
    return FunctionDeclaration(Value(rtype, name), arguments, *attributes)
github serge-sans-paille / pythran / pythran / backend.py View on Github external
next_name = "__generator__{0}".format(cxxid(node.name))
        instanciated_next_name = "{0}{1}".format(
            next_name,
            "<{0}>".format(", ".join(formal_types)) if formal_types else "")

        operator_body.append(Statement("{}: return result_type()".format(
            CxxGenerator.FinalStatement)))

        next_declaration = [
            FunctionDeclaration(Value("result_type", "next"), []),
            EmptyStatement()]  # empty statement to force a comma ...

        # the constructors
        next_constructors = [
            FunctionBody(
                FunctionDeclaration(Value("", next_name), []),
                Line(': pythonic::yielder() {}')
                )]
        if formal_types:
            # If all parameters have a default value, we don't need default
            # constructor
            if dflt_argv and all(dflt_argv):
                next_constructors = list()
            next_constructors.append(FunctionBody(
                make_function_declaration(self, node, "", next_name,
                                          formal_types, formal_args,
                                          dflt_argv),
                Line(": {0} {{ }}".format(
                    ", ".join(["pythonic::yielder()"] +
                              ["{0}({0})".format(arg)
                               for arg in formal_args])))
                ))
github serge-sans-paille / pythran / pythran / backend.py View on Github external
ctx = CachedTypeVisitor(self.lctx)
        next_members = ([Statement("{0} {1}".format(ft, fa))
                         for (ft, fa) in zip(formal_types, formal_args)] +
                        [Statement("{0} {1}".format(
                            self.types[self.local_names[k]].generate(ctx),
                            k))
                         for k in self.ldecls] +
                        [Statement("{0} {1}".format(v, k))
                         for k, v in self.extra_declarations] +
                        [Statement(
                            "typename {0}::result_type {1}".format(
                                instanciated_next_name,
                                CxxGenerator.StateValue))])

        extern_typedefs = [Typedef(Value(t.generate(ctx), t.name))
                           for t in self.types[node][1]]
        iterator_typedef = [
            Typedef(
                Value("pythonic::types::generator_iterator<{0}>".format(
                    "{0}<{1}>".format(next_name, ", ".join(formal_types))
                    if formal_types else next_name),
                    "iterator")),
            Typedef(Value(result_type.generate(ctx),
                          "value_type"))]
        result_typedef = [
            Typedef(Value(result_type.generate(ctx), "result_type"))]
        extra_typedefs = (ctx.typedefs() +
                          extern_typedefs +
                          iterator_typedef +
                          result_typedef)
github serge-sans-paille / pythran / pythran / backend.py View on Github external
def make_function_declaration(self, node, rtype, name, ftypes, fargs,
                              defaults=None, attributes=None):
    if defaults is None:
        defaults = [None] * len(ftypes)
    if attributes is None:
        attributes = []

    arguments = list()
    first_default = len(node.args.args) - len(node.args.defaults)
    for i, (t, a, d) in enumerate(zip(ftypes, fargs, defaults)):
        # because universal reference and default don't get on well
        if isinstance(self, CxxGenerator) or i >= first_default:
            rvalue_ref = ""
        else:
            rvalue_ref = "&&"
        argument = Value(t + rvalue_ref, "{0}{1}".format(a, make_default(d)))
        arguments.append(argument)
    return FunctionDeclaration(Value(rtype, name), arguments, *attributes)