예제 #1
0
파일: spec.py 프로젝트: xmar/pythran
def specs_to_docstrings(specs, docstrings):
    for function_name, signatures in specs.items():
        sigdocs = []
        for sigid, signature in enumerate(signatures):
            arguments_types = [pytype_to_pretty_type(t) for t in signature]
            function_signatures = "{}({})".format(function_name, ", ".join(arguments_types))
            sigdocs.append(function_signatures)
        docstrings[function_name] = "Supported prototypes:{}\n{}".format(
            "".join("\n    - " + sigdoc for sigdoc in sigdocs), docstrings.get(function_name, "")
        )
예제 #2
0
def specs_to_docstrings(specs, docstrings):
    for function_name, signatures in specs.items():
        sigdocs = []
        for sigid, signature in enumerate(signatures):
            arguments_types = [pytype_to_pretty_type(t) for t in signature]
            function_signatures = '{}({})'.format(function_name,
                                                  ', '.join(arguments_types))
            sigdocs.append(function_signatures)
        docstrings[function_name] = "Supported prototypes:{}\n{}".format(
            "".join("\n    - " + sigdoc for sigdoc in sigdocs),
            docstrings.get(function_name, ''))
예제 #3
0
    def __str__(self):
        """Generate (i.e. yield) the source code of the
        module line-by-line.
        """
        themethods = []
        theextraobjects = []
        theoverloads = []
        for vname in self.global_vars:
            theextraobjects.append(
                'PyModule_AddObject(theModule, "{0}", {0});'.format(vname))

        for fname, overloads in self.functions.items():
            tryall = []
            candidates = []
            for overload, ctypes, signature in overloads:
                try_ = dedent("""
                    if(PyObject* obj = {name}(self, args, kw))
                        return obj;
                    PyErr_Clear();
                    """.format(name=overload))
                tryall.append(try_)
                thecall = "{}({})".format(fname,
                                          ",".join(pytype_to_pretty_type(t)
                                                   for t in signature))
                candidates.append(thecall)

            wrapper_name = pythran_ward + 'wrapall_' + fname

            candidate = dedent('''
            static PyObject *
            {wname}(PyObject *self, PyObject *args, PyObject *kw)
            {{
                return pythonic::handle_python_exception([self, args, kw]()
                -> PyObject* {{
                {tryall}
                return pythonic::python::raise_invalid_argument(
                               "{name}", "{candidates}", args, kw);
                }});
            }}
            '''.format(name=fname,
                       tryall="\n".join(tryall),
                       candidates="\\n".join("   " + c for c in candidates),
                       wname=wrapper_name))

            fdoc = self.docstring(self.docstrings.get(fname, ''))
            themethod = dedent('''{{
                "{name}",
                (PyCFunction){wname},
                METH_VARARGS | METH_KEYWORDS,
                {doc}}}'''.format(name=fname,
                                  wname=wrapper_name,
                                  doc=fdoc))
            themethods.append(themethod)
            theoverloads.append(candidate)

        for ptrname, sig in self.capsules:
            capsule = '''
            PyModule_AddObject(theModule, "{ptrname}",
                               PyCapsule_New((void*)&{ptrname}, "{sig}", NULL)
            );'''.format(ptrname=ptrname,
                         sig=sig)
            theextraobjects.append(capsule)

        methods = dedent('''
            static PyMethodDef Methods[] = {{
                {methods}
                {{NULL, NULL, 0, NULL}}
            }};
            '''.format(methods="".join(m + "," for m in themethods)))

        module = dedent('''
            #if PY_MAJOR_VERSION >= 3
              static struct PyModuleDef moduledef = {{
                PyModuleDef_HEAD_INIT,
                "{name}",            /* m_name */
                {moduledoc},         /* m_doc */
                -1,                  /* m_size */
                Methods,             /* m_methods */
                NULL,                /* m_reload */
                NULL,                /* m_traverse */
                NULL,                /* m_clear */
                NULL,                /* m_free */
              }};
            #define PYTHRAN_RETURN return theModule
            #define PYTHRAN_MODULE_INIT(s) PyInit_##s
            #else
            #define PYTHRAN_RETURN return
            #define PYTHRAN_MODULE_INIT(s) init##s
            #endif
            PyMODINIT_FUNC
            PYTHRAN_MODULE_INIT({name})(void)
            #ifndef _WIN32
            __attribute__ ((visibility("default")))
            __attribute__ ((externally_visible))
            #endif
            ;
            PyMODINIT_FUNC
            PYTHRAN_MODULE_INIT({name})(void) {{
                #ifdef PYTHONIC_TYPES_NDARRAY_HPP
                    import_array()
                #endif
                #if PY_MAJOR_VERSION >= 3
                PyObject* theModule = PyModule_Create(&moduledef);
                #else
                PyObject* theModule = Py_InitModule3("{name}",
                                                     Methods,
                                                     {moduledoc}
                );
                #endif
                if(! theModule)
                    PYTHRAN_RETURN;
                PyObject * theDoc = Py_BuildValue("(sss)",
                                                  "{version}",
                                                  "{date}",
                                                  "{hash}");
                if(! theDoc)
                    PYTHRAN_RETURN;
                PyModule_AddObject(theModule,
                                   "__pythran__",
                                   theDoc);

                {extraobjects}
                PYTHRAN_RETURN;
            }}
            '''.format(name=self.name,
                       extraobjects='\n'.join(theextraobjects),
                       **self.metadata))

        body = (self.preamble +
                self.includes +
                self.implems +
                [Line('#ifdef ENABLE_PYTHON_MODULE')] +
                self.python_implems +
                [Line(code) for code in self.wrappers + theoverloads] +
                [Line(methods), Line(module), Line('#endif')])

        return "\n".join(Module(body).generate())
예제 #4
0
파일: spec.py 프로젝트: mm1860/pythran
def spec_to_string(function_name, spec):
    arguments_types = [pytype_to_pretty_type(t) for t in spec]
    return '{}({})'.format(function_name, ', '.join(arguments_types))
예제 #5
0
def spec_to_string(function_name, spec):
    arguments_types = [pytype_to_pretty_type(t) for t in spec]
    return '{}({})'.format(function_name, ', '.join(arguments_types))