Turns the name, signature, code in functions into complete functions and lists them in a methods_table. Then turns the methods_table into a ``PyMethodDef`` structure and returns the resulting code fragment ready for compilation
(functions, modname)
| 121 | |
| 122 | |
| 123 | def _make_methods(functions, modname): |
| 124 | """ Turns the name, signature, code in functions into complete functions |
| 125 | and lists them in a methods_table. Then turns the methods_table into a |
| 126 | ``PyMethodDef`` structure and returns the resulting code fragment ready |
| 127 | for compilation |
| 128 | """ |
| 129 | methods_table = [] |
| 130 | codes = [] |
| 131 | for funcname, flags, code in functions: |
| 132 | cfuncname = "%s_%s" % (modname, funcname) |
| 133 | if 'METH_KEYWORDS' in flags: |
| 134 | signature = '(PyObject *self, PyObject *args, PyObject *kwargs)' |
| 135 | else: |
| 136 | signature = '(PyObject *self, PyObject *args)' |
| 137 | methods_table.append( |
| 138 | "{\"%s\", (PyCFunction)%s, %s}," % (funcname, cfuncname, flags)) |
| 139 | func_code = """ |
| 140 | static PyObject* {cfuncname}{signature} |
| 141 | {{ |
| 142 | {code} |
| 143 | }} |
| 144 | """.format(cfuncname=cfuncname, signature=signature, code=code) |
| 145 | codes.append(func_code) |
| 146 | |
| 147 | body = "\n".join(codes) + """ |
| 148 | static PyMethodDef methods[] = { |
| 149 | %(methods)s |
| 150 | { NULL } |
| 151 | }; |
| 152 | static struct PyModuleDef moduledef = { |
| 153 | PyModuleDef_HEAD_INIT, |
| 154 | "%(modname)s", /* m_name */ |
| 155 | NULL, /* m_doc */ |
| 156 | -1, /* m_size */ |
| 157 | methods, /* m_methods */ |
| 158 | }; |
| 159 | """ % dict(methods='\n'.join(methods_table), modname=modname) |
| 160 | return body |
| 161 | |
| 162 | |
| 163 | def _make_source(name, init, body): |
no test coverage detected